A tumblelog CMS built on AJAX, PHP and MySQL.

streams.class.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. if (!defined('entry') || !entry) {
  3. die('Not a valid page');
  4. }
  5. /*
  6. Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>.
  7. This file is part of PHP-gettext.
  8. PHP-gettext is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. PHP-gettext is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with PHP-gettext; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. // Simple class to wrap file streams, string streams, etc.
  21. // seek is essential, and it should be byte stream
  22. class StreamReader
  23. {
  24. // should return a string [FIXME: perhaps return array of bytes?]
  25. public function read($bytes)
  26. {
  27. return false;
  28. }
  29. // should return new position
  30. public function seekto($position)
  31. {
  32. return false;
  33. }
  34. // returns current position
  35. public function currentpos()
  36. {
  37. return false;
  38. }
  39. // returns length of entire stream (limit for seekto()s)
  40. public function length()
  41. {
  42. return false;
  43. }
  44. }
  45. class StringReader
  46. {
  47. public $_pos;
  48. public $_str;
  49. public function __construct($str='')
  50. {
  51. $this->_str = $str;
  52. $this->_pos = 0;
  53. }
  54. public function read($bytes)
  55. {
  56. $data = substr($this->_str, $this->_pos, $bytes);
  57. $this->_pos += $bytes;
  58. if (strlen($this->_str)<$this->_pos) {
  59. $this->_pos = strlen($this->_str);
  60. }
  61. return $data;
  62. }
  63. public function seekto($pos)
  64. {
  65. $this->_pos = $pos;
  66. if (strlen($this->_str)<$this->_pos) {
  67. $this->_pos = strlen($this->_str);
  68. }
  69. return $this->_pos;
  70. }
  71. public function currentpos()
  72. {
  73. return $this->_pos;
  74. }
  75. public function length()
  76. {
  77. return strlen($this->_str);
  78. }
  79. }
  80. class FileReader
  81. {
  82. public $_pos;
  83. public $_fd;
  84. public $_length;
  85. public function __construct($filename)
  86. {
  87. if (file_exists($filename)) {
  88. $this->_length=filesize($filename);
  89. $this->_pos = 0;
  90. $this->_fd = fopen($filename, 'rb');
  91. if (!$this->_fd) {
  92. $this->error = 3; // Cannot read file, probably permissions
  93. return false;
  94. }
  95. } else {
  96. $this->error = 2; // File doesn't exist
  97. return false;
  98. }
  99. }
  100. public function read($bytes)
  101. {
  102. if ($bytes) {
  103. fseek($this->_fd, $this->_pos);
  104. // PHP 5.1.1 does not read more than 8192 bytes in one fread()
  105. // the discussions at PHP Bugs suggest it's the intended behaviour
  106. $data="";
  107. while ($bytes > 0) {
  108. $chunk = fread($this->_fd, $bytes);
  109. $data .= $chunk;
  110. $bytes -= strlen($chunk);
  111. }
  112. $this->_pos = ftell($this->_fd);
  113. return $data;
  114. } else {
  115. return '';
  116. }
  117. }
  118. public function seekto($pos)
  119. {
  120. fseek($this->_fd, $pos);
  121. $this->_pos = ftell($this->_fd);
  122. return $this->_pos;
  123. }
  124. public function currentpos()
  125. {
  126. return $this->_pos;
  127. }
  128. public function length()
  129. {
  130. return $this->_length;
  131. }
  132. public function close()
  133. {
  134. fclose($this->_fd);
  135. }
  136. }
  137. // Preloads entire file in memory first, then creates a StringReader
  138. // over it (it assumes knowledge of StringReader internals)
  139. class CachedFileReader extends StringReader
  140. {
  141. public function __construct($filename)
  142. {
  143. if (file_exists($filename)) {
  144. $length=filesize($filename);
  145. $fd = fopen($filename, 'rb');
  146. if (!$fd) {
  147. $this->error = 3; // Cannot read file, probably permissions
  148. return false;
  149. }
  150. $this->_str = fread($fd, $length);
  151. fclose($fd);
  152. } else {
  153. $this->error = 2; // File doesn't exist
  154. return false;
  155. }
  156. }
  157. }