PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/xoops_trust_path/vendor/PEAR/File/Archive/Reader/File.php

https://github.com/nouphet/momoxo
PHP | 301 lines | 168 code | 23 blank | 110 comment | 30 complexity | 919b29a3bc1790d0c79579515edab1f4 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Reader that represents a single file
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
  21. *
  22. * @category File Formats
  23. * @package File_Archive
  24. * @author Vincent Lascaux <vincentlascaux@php.net>
  25. * @copyright 1997-2005 The PHP Group
  26. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  27. * @version CVS: $Id$
  28. * @link http://pear.php.net/package/File_Archive
  29. */
  30. require_once "File/Archive/Reader.php";
  31. require_once "MIME/Type.php";
  32. /**
  33. * Reader that represents a single file
  34. */
  35. class File_Archive_Reader_File extends File_Archive_Reader
  36. {
  37. /**
  38. * @var object Handle to the file being read
  39. * @access private
  40. */
  41. var $handle = null;
  42. /**
  43. * @var string Name of the physical file being read
  44. * @access private
  45. */
  46. var $filename;
  47. /**
  48. * @var string Name of the file returned by the reader
  49. * @access private
  50. */
  51. var $symbolic;
  52. /**
  53. * @var array Stats of the file
  54. * Will only be set after a call to $this->getStat()
  55. * @access private
  56. */
  57. var $stat = null;
  58. /**
  59. * @var string Mime type of the file
  60. * Will only be set after a call to $this->getMime()
  61. */
  62. var $mime = null;
  63. /**
  64. * @var boolean Has the file already been read
  65. * @access private
  66. */
  67. var $alreadyRead = false;
  68. /**
  69. * $filename is the physical file to read
  70. * $symbolic is the name declared by the reader
  71. * If $symbolic is not specified, $filename is assumed
  72. */
  73. function File_Archive_Reader_File($filename, $symbolic = null, $mime = null)
  74. {
  75. $this->filename = $filename;
  76. $this->mime = $mime;
  77. if ($symbolic === null) {
  78. $this->symbolic = $this->getStandardURL($filename);
  79. } else {
  80. $this->symbolic = $this->getStandardURL($symbolic);
  81. }
  82. }
  83. /**
  84. * @see File_Archive_Reader::close()
  85. *
  86. * Close the file handle
  87. */
  88. function close()
  89. {
  90. $this->alreadyRead = false;
  91. if ($this->handle !== null) {
  92. fclose($this->handle);
  93. $this->handle = null;
  94. }
  95. }
  96. /**
  97. * @see File_Archive_Reader::next()
  98. *
  99. * The first time next is called, it will open the file handle and return
  100. * true. Then it will return false
  101. * Raise an error if the file does not exist
  102. */
  103. function next()
  104. {
  105. if ($this->alreadyRead) {
  106. return false;
  107. } else {
  108. $this->alreadyRead = true;
  109. return true;
  110. }
  111. }
  112. /**
  113. * @see File_Archive_Reader::getFilename()
  114. */
  115. function getFilename() { return $this->symbolic; }
  116. /**
  117. * @see File_Archive_Reader::getDataFilename()
  118. *
  119. * Return the name of the file
  120. */
  121. function getDataFilename() { return $this->filename; }
  122. /**
  123. * @see File_Archive_Reader::getStat() stat()
  124. */
  125. function getStat()
  126. {
  127. if ($this->stat === null) {
  128. $this->stat = @stat($this->filename);
  129. //If we can't use the stat function
  130. if ($this->stat === false) {
  131. $this->stat = array();
  132. }
  133. }
  134. return $this->stat;
  135. }
  136. /**
  137. * @see File_Archive_Reader::getMime
  138. */
  139. function getMime()
  140. {
  141. if ($this->mime === null) {
  142. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  143. $this->mime = MIME_Type::autoDetect($this->getDataFilename());
  144. PEAR::popErrorHandling();
  145. if (PEAR::isError($this->mime)) {
  146. $this->mime = parent::getMime();
  147. }
  148. }
  149. return $this->mime;
  150. }
  151. /**
  152. * Opens the file if it was not already opened
  153. */
  154. function _ensureFileOpened()
  155. {
  156. if ($this->handle === null) {
  157. $this->handle = @fopen($this->filename, "r");
  158. if (!is_resource($this->handle)) {
  159. $this->handle = null;
  160. return PEAR::raiseError("Can't open {$this->filename} for reading");
  161. }
  162. if ($this->handle === false) {
  163. $this->handle = null;
  164. return PEAR::raiseError("File {$this->filename} not found");
  165. }
  166. }
  167. }
  168. /**
  169. * @see File_Archive_Reader::getData()
  170. */
  171. function getData($length = -1)
  172. {
  173. $error = $this->_ensureFileOpened();
  174. if (PEAR::isError($error)) {
  175. return $error;
  176. }
  177. if (feof($this->handle)) {
  178. return null;
  179. }
  180. if ($length == -1) {
  181. $contents = '';
  182. $blockSize = File_Archive::getOption('blockSize');
  183. // Ensure that magic_quote_runtime isn't set,
  184. // if we don't want to have corrupted archives.
  185. $saveMagicQuotes = get_magic_quotes_runtime();
  186. set_magic_quotes_runtime(0);
  187. while (!feof($this->handle)) {
  188. $contents .= fread($this->handle, $blockSize);
  189. }
  190. set_magic_quotes_runtime($saveMagicQuotes);
  191. return $contents;
  192. } else {
  193. if ($length == 0) {
  194. return "";
  195. } else {
  196. return fread($this->handle, $length);
  197. }
  198. }
  199. }
  200. /**
  201. * @see File_Archive_Reader::skip()
  202. */
  203. function skip($length = -1)
  204. {
  205. $error = $this->_ensureFileOpened();
  206. if (PEAR::isError($error)) {
  207. return $error;
  208. }
  209. $before = ftell($this->handle);
  210. if (($length == -1 && @fseek($this->handle, 0, SEEK_END) === -1) ||
  211. ($length >= 0 && @fseek($this->handle, $length, SEEK_CUR) === -1)) {
  212. return parent::skip($length);
  213. } else {
  214. return ftell($this->handle) - $before;
  215. }
  216. }
  217. /**
  218. * @see File_Archive_Reader::rewind
  219. */
  220. function rewind($length = -1)
  221. {
  222. if ($this->handle === null) {
  223. return 0;
  224. }
  225. $before = ftell($this->handle);
  226. if (($length == -1 && @fseek($this->handle, 0, SEEK_SET) === -1) ||
  227. ($length >= 0 && @fseek($this->handle, -$length, SEEK_CUR) === -1)) {
  228. return parent::rewind($length);
  229. } else {
  230. return $before - ftell($this->handle);
  231. }
  232. }
  233. /**
  234. * @see File_Archive_Reader::tell()
  235. */
  236. function tell()
  237. {
  238. if ($this->handle === null) {
  239. return 0;
  240. } else {
  241. return ftell($this->handle);
  242. }
  243. }
  244. /**
  245. * @see File_Archive_Reader::makeWriterRemoveFiles()
  246. */
  247. function makeWriterRemoveFiles($pred)
  248. {
  249. return PEAR::raiseError(
  250. 'File_Archive_Reader_File represents a single file, you cant remove it');
  251. }
  252. /**
  253. * @see File_Archive_Reader::makeWriterRemoveBlocks()
  254. */
  255. function makeWriterRemoveBlocks($blocks, $seek = 0)
  256. {
  257. require_once "File/Archive/Writer/Files.php";
  258. $writer = new File_Archive_Writer_Files();
  259. $file = $this->getDataFilename();
  260. $pos = $this->tell();
  261. $this->close();
  262. $writer->openFileRemoveBlock($file, $pos + $seek, $blocks);
  263. return $writer;
  264. }
  265. /**
  266. * @see File_Archive_Reader::makeAppendWriter
  267. */
  268. function makeAppendWriter()
  269. {
  270. return PEAR::raiseError(
  271. 'File_Archive_Reader_File represents a single file.'.
  272. ' makeAppendWriter cant be executed on it'
  273. );
  274. }
  275. }
  276. ?>