/Zend/Pdf/FileParserDataSource/File.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 204 lines · 81 code · 23 blank · 100 comment · 15 complexity · d799d5d01470cf07b83491ae250f9986 MD5 · raw file

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