You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.2 KiB

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