File.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_Pdf
  16. * @subpackage FileParser
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Pdf_FileParserDataSource */
  21. require_once 'Zend/Pdf/FileParserDataSource.php';
  22. /**
  23. * Concrete subclass of {@link Zend_Pdf_FileParserDataSource} that provides an
  24. * interface to filesystem objects.
  25. *
  26. * Note that this class cannot be used for other sources that may be supported
  27. * by {@link fopen()} (through URL wrappers). It may be used for local
  28. * filesystem objects only.
  29. *
  30. * @package Zend_Pdf
  31. * @subpackage FileParser
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Pdf_FileParserDataSource_File extends Zend_Pdf_FileParserDataSource
  36. {
  37. /**** Instance Variables ****/
  38. /**
  39. * Fully-qualified path to the file.
  40. * @var string
  41. */
  42. protected $_filePath = '';
  43. /**
  44. * File resource handle .
  45. * @var resource
  46. */
  47. protected $_fileResource = null;
  48. /**** Public Interface ****/
  49. /* Concrete Class Implementation */
  50. /**
  51. * Object constructor.
  52. *
  53. * Validates the path to the file, ensures that it is readable, then opens
  54. * it for reading.
  55. *
  56. * Throws an exception if the file is missing or cannot be opened.
  57. *
  58. * @param string $filePath Fully-qualified path to the file.
  59. * @throws Zend_Pdf_Exception
  60. */
  61. public function __construct($filePath)
  62. {
  63. if (! (is_file($filePath) || is_link($filePath))) {
  64. throw new Zend_Pdf_Exception("Invalid file path: $filePath",
  65. Zend_Pdf_Exception::BAD_FILE_PATH);
  66. }
  67. if (! is_readable($filePath)) {
  68. throw new Zend_Pdf_Exception("File is not readable: $filePath",
  69. Zend_Pdf_Exception::NOT_READABLE);
  70. }
  71. if (($this->_size = @filesize($filePath)) === false) {
  72. throw new Zend_Pdf_Exception("Error while obtaining file size: $filePath",
  73. Zend_Pdf_Exception::CANT_GET_FILE_SIZE);
  74. }
  75. if (($this->_fileResource = @fopen($filePath, 'rb')) === false) {
  76. throw new Zend_Pdf_Exception("Cannot open file for reading: $filePath",
  77. Zend_Pdf_Exception::CANT_OPEN_FILE);
  78. }
  79. $this->_filePath = $filePath;
  80. }
  81. /**
  82. * Object destructor.
  83. *
  84. * Closes the file if it had been successfully opened.
  85. */
  86. public function __destruct()
  87. {
  88. if (is_resource($this->_fileResource)) {
  89. @fclose($this->_fileResource);
  90. }
  91. }
  92. /**
  93. * Returns the specified number of raw bytes from the file at the byte
  94. * offset of the current read position.
  95. *
  96. * Advances the read position by the number of bytes read.
  97. *
  98. * Throws an exception if an error was encountered while reading the file or
  99. * if there is insufficient data to completely fulfill the request.
  100. *
  101. * @param integer $byteCount Number of bytes to read.
  102. * @return string
  103. * @throws Zend_Pdf_Exception
  104. */
  105. public function readBytes($byteCount)
  106. {
  107. $bytes = @fread($this->_fileResource, $byteCount);
  108. if ($bytes === false) {
  109. throw new Zend_Pdf_Exception('Unexpected error while reading file',
  110. Zend_Pdf_Exception::ERROR_DURING_READ);
  111. }
  112. if (strlen($bytes) != $byteCount) {
  113. throw new Zend_Pdf_Exception("Insufficient data to read $byteCount bytes",
  114. Zend_Pdf_Exception::INSUFFICIENT_DATA);
  115. }
  116. $this->_offset += $byteCount;
  117. return $bytes;
  118. }
  119. /**
  120. * Returns the entire contents of the file as a string.
  121. *
  122. * Preserves the current file seek position.
  123. *
  124. * @return string
  125. */
  126. public function readAllBytes()
  127. {
  128. return file_get_contents($this->_filePath);
  129. }
  130. /* Object Magic Methods */
  131. /**
  132. * Returns the full filesystem path of the file.
  133. *
  134. * @return string
  135. */
  136. public function __toString()
  137. {
  138. return $this->_filePath;
  139. }
  140. /* Primitive Methods */
  141. /**
  142. * Seeks the file read position to the specified byte offset.
  143. *
  144. * Throws an exception if the file pointer cannot be moved or if it is
  145. * moved beyond EOF (end of file).
  146. *
  147. * @param integer $offset Destination byte offset.
  148. * @throws Zend_Pdf_Exception
  149. */
  150. public function moveToOffset($offset)
  151. {
  152. if ($this->_offset == $offset) {
  153. return; // Not moving; do nothing.
  154. }
  155. parent::moveToOffset($offset);
  156. $result = @fseek($this->_fileResource, $offset, SEEK_SET);
  157. if ($result !== 0) {
  158. throw new Zend_Pdf_Exception('Error while setting new file position',
  159. Zend_Pdf_Exception::CANT_SET_FILE_POSITION);
  160. }
  161. if (feof($this->_fileResource)) {
  162. throw new Zend_Pdf_Exception('Moved beyond the end of the file',
  163. Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
  164. }
  165. }
  166. }