1<?php 2 3/** 4 * Swift Mailer File Stream Wrapper 5 * Please read the LICENSE file 6 * @copyright Chris Corbyn <chris@w3style.co.uk> 7 * @author Chris Corbyn <chris@w3style.co.uk> 8 * @package Swift 9 * @license GNU Lesser General Public License 10 */ 11 12require_once dirname(__FILE__) . "/ClassLoader.php"; 13Swift_ClassLoader::load("Swift_FileException"); 14 15/** 16 * Swift File stream abstraction layer 17 * Reads bytes from a file 18 * @package Swift 19 * @author Chris Corbyn <chris@w3style.co.uk> 20 */ 21class Swift_File 22{ 23 /** 24 * The accessible path to the file 25 * @var string 26 */ 27 protected $path = null; 28 /** 29 * The name of the file 30 * @var string 31 */ 32 protected $name = null; 33 /** 34 * The resource returned by fopen() against the path 35 * @var resource 36 */ 37 protected $handle = null; 38 /** 39 * The status of magic_quotes in php.ini 40 * @var boolean 41 */ 42 protected $magic_quotes = false; 43 44 /** 45 * Constructor 46 * @param string The path the the file 47 * @throws Swift_FileException If the file cannot be found 48 */ 49 public function __construct($path) 50 { 51 $this->setPath($path); 52 $this->magic_quotes = get_magic_quotes_runtime(); 53 } 54 /** 55 * Set the path to the file 56 * @param string The path to the file 57 * @throws Swift_FileException If the file cannot be found 58 */ 59 public function setPath($path) 60 { 61 if (!file_exists($path)) 62 { 63 throw new Swift_FileException("No such file '" . $path ."'"); 64 } 65 $this->handle = null; 66 $this->path = $path; 67 $this->name = null; 68 $this->name = $this->getFileName(); 69 } 70 /** 71 * Get the path to the file 72 * @return string 73 */ 74 public function getPath() 75 { 76 return $this->path; 77 } 78 /** 79 * Get the name of the file without it's full path 80 * @return string 81 */ 82 public function getFileName() 83 { 84 if ($this->name !== null) 85 { 86 return $this->name; 87 } 88 else 89 { 90 return basename($this->getPath()); 91 } 92 } 93 /** 94 * Establish an open file handle on the file if the file is not yet opened 95 * @throws Swift_FileException If the file cannot be opened for reading 96 */ 97 protected function createHandle() 98 { 99 if ($this->handle === null) 100 { 101 if (!$this->handle = fopen($this->path, "rb")) 102 { 103 throw new Swift_FileException("Unable to open file '" . $this->path . " for reading. Check the file permissions."); 104 } 105 } 106 } 107 /** 108 * Check if the pointer as at the end of the file 109 * @return boolean 110 * @throws Swift_FileException If the file cannot be read 111 */ 112 public function EOF() 113 { 114 $this->createHandle(); 115 return feof($this->handle); 116 } 117 /** 118 * Get a single byte from the file 119 * Returns false past EOF 120 * @return string 121 * @throws Swift_FileException If the file cannot be read 122 */ 123 public function getByte() 124 { 125 $this->createHandle(); 126 return $this->read(1); 127 } 128 /** 129 * Read one full line from the file including the line ending 130 * Returns false past EOF 131 * @return string 132 * @throws Swift_FileException If the file cannot be read 133 */ 134 public function readln() 135 { 136 set_magic_quotes_runtime(0); 137 $this->createHandle(); 138 if (!$this->EOF()) 139 { 140 $ret = fgets($this->handle); 141 } 142 else $ret = false; 143 144 set_magic_quotes_runtime($this->magic_quotes); 145 146 return $ret; 147 } 148 /** 149 * Get the entire file contents as a string 150 * @return string 151 * @throws Swift_FileException If the file cannot be read 152 */ 153 public function readFull() 154 { 155 $ret = ""; 156 set_magic_quotes_runtime(0); 157 while (false !== $chunk = $this->read(8192, false)) $ret .= $chunk; 158 set_magic_quotes_runtime($this->magic_quotes); 159 return $ret; 160 } 161 /** 162 * Read a given number of bytes from the file 163 * Returns false past EOF 164 * @return string 165 * @throws Swift_FileException If the file cannot be read 166 */ 167 public function read($bytes, $unquote=true) 168 { 169 if ($unquote) set_magic_quotes_runtime(0); 170 $this->createHandle(); 171 if (!$this->EOF()) 172 { 173 $ret = fread($this->handle, $bytes); 174 } 175 else $ret = false; 176 177 if ($unquote) set_magic_quotes_runtime($this->magic_quotes); 178 179 return $ret; 180 } 181 /** 182 * Get the size of the file in bytes 183 * @return int 184 */ 185 public function length() 186 { 187 return filesize($this->path); 188 } 189 /** 190 * Close the open handle on the file 191 * @throws Swift_FileException If the file cannot be read 192 */ 193 public function close() 194 { 195 $this->createHandle(); 196 fclose($this->handle); 197 $this->handle = null; 198 } 199 /** 200 * Reset the file pointer back to zero 201 */ 202 public function reset() 203 { 204 $this->createHandle(); 205 fseek($this->handle, 0); 206 } 207 /** 208 * Destructor 209 * Closes the file 210 */ 211 public function __destruct() 212 { 213 if ($this->handle !== null) $this->close(); 214 } 215} 216