PageRenderTime 977ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/swift/Swift/File.php

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