Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / typo3 / contrib / swiftmailer / classes / Swift / ByteStream / FileByteStream.php
1 <?php
2
3 /*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 //@require 'Swift/ByteStream/AbstractFilterableInputStream.php';
12 //@require 'Swift/InputByteStream.php';
13 //@require 'Swift/FileStream.php';
14 //@require 'Swift/IoException.php';
15
16 /**
17 * Allows reading and writing of bytes to and from a file.
18 * @package Swift
19 * @subpackage ByteStream
20 * @author Chris Corbyn
21 */
22 class Swift_ByteStream_FileByteStream
23 extends Swift_ByteStream_AbstractFilterableInputStream
24 implements Swift_FileStream
25 {
26
27 /** The internal pointer offset */
28 private $_offset = 0;
29
30 /** The path to the file */
31 private $_path;
32
33 /** The mode this file is opened in for writing */
34 private $_mode;
35
36 /** A lazy-loaded resource handle for reading the file */
37 private $_reader;
38
39 /** A lazy-loaded resource handle for writing the file */
40 private $_writer;
41
42 /** If magic_quotes_runtime is on, this will be true */
43 private $_quotes = false;
44
45 /**
46 * Create a new FileByteStream for $path.
47 * @param string $path
48 * @param string $writable if true
49 */
50 public function __construct($path, $writable = false)
51 {
52 $this->_path = $path;
53 $this->_mode = $writable ? 'w+b' : 'rb';
54 $this->_quotes = get_magic_quotes_runtime();
55 }
56
57 /**
58 * Get the complete path to the file.
59 * @return string
60 */
61 public function getPath()
62 {
63 return $this->_path;
64 }
65
66 /**
67 * Reads $length bytes from the stream into a string and moves the pointer
68 * through the stream by $length. If less bytes exist than are requested the
69 * remaining bytes are given instead. If no bytes are remaining at all, boolean
70 * false is returned.
71 * @param int $length
72 * @return string
73 * @throws Swift_IoException
74 */
75 public function read($length)
76 {
77 $fp = $this->_getReadHandle();
78 if (!feof($fp))
79 {
80 if ($this->_quotes)
81 {
82 set_magic_quotes_runtime(0);
83 }
84 $bytes = fread($fp, $length);
85 if ($this->_quotes)
86 {
87 set_magic_quotes_runtime(1);
88 }
89 $this->_offset = ftell($fp);
90 return $bytes;
91 }
92 else
93 {
94 return false;
95 }
96 }
97
98 /**
99 * Move the internal read pointer to $byteOffset in the stream.
100 * @param int $byteOffset
101 * @return boolean
102 */
103 public function setReadPointer($byteOffset)
104 {
105 if (isset($this->_reader))
106 {
107 fseek($this->_reader, $byteOffset, SEEK_SET);
108 }
109 $this->_offset = $byteOffset;
110 }
111
112 // -- Private methods
113
114 /** Just write the bytes to the file */
115 protected function _commit($bytes)
116 {
117 fwrite($this->_getWriteHandle(), $bytes);
118 $this->_resetReadHandle();
119 }
120
121 /** Not used */
122 protected function _flush()
123 {
124 }
125
126 /** Get the resource for reading */
127 private function _getReadHandle()
128 {
129 if (!isset($this->_reader))
130 {
131 if (!$this->_reader = fopen($this->_path, 'rb'))
132 {
133 throw new Swift_IoException(
134 'Unable to open file for reading [' . $this->_path . ']'
135 );
136 }
137 fseek($this->_reader, $this->_offset, SEEK_SET);
138 }
139 return $this->_reader;
140 }
141
142 /** Get the resource for writing */
143 private function _getWriteHandle()
144 {
145 if (!isset($this->_writer))
146 {
147 if (!$this->_writer = fopen($this->_path, $this->_mode))
148 {
149 throw new Swift_IoException(
150 'Unable to open file for writing [' . $this->_path . ']'
151 );
152 }
153 }
154 return $this->_writer;
155 }
156
157 /** Force a reload of the resource for writing */
158 private function _resetWriteHandle()
159 {
160 if (isset($this->_writer))
161 {
162 fclose($this->_writer);
163 $this->_writer = null;
164 }
165 }
166
167 /** Force a reload of the resource for reading */
168 private function _resetReadHandle()
169 {
170 if (isset($this->_reader))
171 {
172 fclose($this->_reader);
173 $this->_reader = null;
174 }
175 }
176
177 }