File.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-2009 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-2009 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. throw new Zend_Pdf_Exception("Invalid file path: $filePath",
  67. Zend_Pdf_Exception::BAD_FILE_PATH);
  68. }
  69. if (! is_readable($filePath)) {
  70. throw new Zend_Pdf_Exception("File is not readable: $filePath",
  71. Zend_Pdf_Exception::NOT_READABLE);
  72. }
  73. if (($this->_size = @filesize($filePath)) === false) {
  74. throw new Zend_Pdf_Exception("Error while obtaining file size: $filePath",
  75. Zend_Pdf_Exception::CANT_GET_FILE_SIZE);
  76. }
  77. if (($this->_fileResource = @fopen($filePath, 'rb')) === false) {
  78. throw new Zend_Pdf_Exception("Cannot open file for reading: $filePath",
  79. Zend_Pdf_Exception::CANT_OPEN_FILE);
  80. }
  81. $this->_filePath = $filePath;
  82. }
  83. /**
  84. * Object destructor.
  85. *
  86. * Closes the file if it had been successfully opened.
  87. */
  88. public function __destruct()
  89. {
  90. if (is_resource($this->_fileResource)) {
  91. @fclose($this->_fileResource);
  92. }
  93. }
  94. /**
  95. * Returns the specified number of raw bytes from the file at the byte
  96. * offset of the current read position.
  97. *
  98. * Advances the read position by the number of bytes read.
  99. *
  100. * Throws an exception if an error was encountered while reading the file or
  101. * if there is insufficient data to completely fulfill the request.
  102. *
  103. * @param integer $byteCount Number of bytes to read.
  104. * @return string
  105. * @throws Zend_Pdf_Exception
  106. */
  107. public function readBytes($byteCount)
  108. {
  109. $bytes = @fread($this->_fileResource, $byteCount);
  110. if ($bytes === false) {
  111. throw new Zend_Pdf_Exception('Unexpected error while reading file',
  112. Zend_Pdf_Exception::ERROR_DURING_READ);
  113. }
  114. if (strlen($bytes) != $byteCount) {
  115. throw new Zend_Pdf_Exception("Insufficient data to read $byteCount bytes",
  116. Zend_Pdf_Exception::INSUFFICIENT_DATA);
  117. }
  118. $this->_offset += $byteCount;
  119. return $bytes;
  120. }
  121. /**
  122. * Returns the entire contents of the file as a string.
  123. *
  124. * Preserves the current file seek position.
  125. *
  126. * @return string
  127. */
  128. public function readAllBytes()
  129. {
  130. return file_get_contents($this->_filePath);
  131. }
  132. /* Object Magic Methods */
  133. /**
  134. * Returns the full filesystem path of the file.
  135. *
  136. * @return string
  137. */
  138. public function __toString()
  139. {
  140. return $this->_filePath;
  141. }
  142. /* Primitive Methods */
  143. /**
  144. * Seeks the file read position to the specified byte offset.
  145. *
  146. * Throws an exception if the file pointer cannot be moved or if it is
  147. * moved beyond EOF (end of file).
  148. *
  149. * @param integer $offset Destination byte offset.
  150. * @throws Zend_Pdf_Exception
  151. */
  152. public function moveToOffset($offset)
  153. {
  154. if ($this->_offset == $offset) {
  155. return; // Not moving; do nothing.
  156. }
  157. parent::moveToOffset($offset);
  158. $result = @fseek($this->_fileResource, $offset, SEEK_SET);
  159. if ($result !== 0) {
  160. throw new Zend_Pdf_Exception('Error while setting new file position',
  161. Zend_Pdf_Exception::CANT_SET_FILE_POSITION);
  162. }
  163. if (feof($this->_fileResource)) {
  164. throw new Zend_Pdf_Exception('Moved beyond the end of the file',
  165. Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
  166. }
  167. }
  168. }