[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/pomo/ -> streams.php (source)

   1  <?php
   2  /**
   3   * Classes, which help reading streams of data from files.
   4   * Based on the classes from Danilo Segan <danilo@kvota.net>
   5   *
   6   * @version $Id: streams.php 1184 2026-03-30 00:40:06Z dd32 $
   7   * @package pomo
   8   * @subpackage streams
   9   */
  10  
  11  if ( ! class_exists( 'POMO_Reader', false ) ) :
  12      #[AllowDynamicProperties]
  13      class POMO_Reader {
  14  
  15          public $endian = 'little';
  16          public $_pos;
  17          public $is_overloaded;
  18  
  19          /**
  20           * PHP5 constructor.
  21           */
  22  		public function __construct() {
  23              if ( function_exists( 'mb_substr' )
  24                  && ( (int) ini_get( 'mbstring.func_overload' ) & 2 ) // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated
  25              ) {
  26                  $this->is_overloaded = true;
  27              } else {
  28                  $this->is_overloaded = false;
  29              }
  30  
  31              $this->_pos = 0;
  32          }
  33  
  34          /**
  35           * PHP4 constructor.
  36           *
  37           * @deprecated 5.4.0 Use __construct() instead.
  38           *
  39           * @see POMO_Reader::__construct()
  40           */
  41  		public function POMO_Reader() {
  42              if ( function_exists( '_deprecated_constructor' ) ) {
  43                  _deprecated_constructor( self::class, '5.4.0', static::class );
  44              }
  45              self::__construct();
  46          }
  47  
  48          /**
  49           * Sets the endianness of the file.
  50           *
  51           * @param string $endian Set the endianness of the file. Accepts 'big', or 'little'.
  52           */
  53  		public function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  54              $this->endian = $endian;
  55          }
  56  
  57          /**
  58           * Reads a 32bit Integer from the Stream
  59           *
  60           * @return mixed The integer, corresponding to the next 32 bits from
  61           *  the stream of false if there are not enough bytes or on error
  62           */
  63  		public function readint32() {
  64              $bytes = $this->read( 4 );
  65              if ( 4 !== $this->strlen( $bytes ) ) {
  66                  return false;
  67              }
  68              $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V';
  69              $int           = unpack( $endian_letter, $bytes );
  70              return reset( $int );
  71          }
  72  
  73          /**
  74           * Reads an array of 32-bit Integers from the Stream
  75           *
  76           * @param int $count How many elements should be read
  77           * @return mixed Array of integers or false if there isn't
  78           *  enough data or on error
  79           */
  80  		public function readint32array( $count ) {
  81              $bytes = $this->read( 4 * $count );
  82              if ( 4 * $count !== $this->strlen( $bytes ) ) {
  83                  return false;
  84              }
  85              $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V';
  86              return unpack( $endian_letter . $count, $bytes );
  87          }
  88  
  89          /**
  90           * @param string $input_string
  91           * @param int    $start
  92           * @param int    $length
  93           * @return string
  94           */
  95  		public function substr( $input_string, $start, $length ) {
  96              if ( $this->is_overloaded ) {
  97                  return mb_substr( $input_string, $start, $length, 'ascii' );
  98              } else {
  99                  return substr( $input_string, $start, $length );
 100              }
 101          }
 102  
 103          /**
 104           * @param string $input_string
 105           * @return int
 106           */
 107  		public function strlen( $input_string ) {
 108              if ( $this->is_overloaded ) {
 109                  return mb_strlen( $input_string, 'ascii' );
 110              } else {
 111                  return strlen( $input_string );
 112              }
 113          }
 114  
 115          /**
 116           * @param string $input_string
 117           * @param int    $chunk_size
 118           * @return array
 119           */
 120  		public function str_split( $input_string, $chunk_size ) {
 121              if ( ! function_exists( 'str_split' ) ) {
 122                  $length = $this->strlen( $input_string );
 123                  $out    = array();
 124                  for ( $i = 0; $i < $length; $i += $chunk_size ) {
 125                      $out[] = $this->substr( $input_string, $i, $chunk_size );
 126                  }
 127                  return $out;
 128              } else {
 129                  return str_split( $input_string, $chunk_size );
 130              }
 131          }
 132  
 133          /**
 134           * @return int
 135           */
 136  		public function pos() {
 137              return $this->_pos;
 138          }
 139  
 140          /**
 141           * @return true
 142           */
 143  		public function is_resource() {
 144              return true;
 145          }
 146  
 147          /**
 148           * @return true
 149           */
 150  		public function close() {
 151              return true;
 152          }
 153      }
 154  endif;
 155  
 156  if ( ! class_exists( 'POMO_FileReader', false ) ) :
 157      class POMO_FileReader extends POMO_Reader {
 158  
 159          /**
 160           * File pointer resource.
 161           *
 162           * @var resource|false
 163           */
 164          public $_f;
 165  
 166          /**
 167           * @param string $filename
 168           */
 169  		public function __construct( $filename ) {
 170              parent::__construct();
 171              $this->_f = fopen( $filename, 'rb' );
 172          }
 173  
 174          /**
 175           * PHP4 constructor.
 176           *
 177           * @deprecated 5.4.0 Use __construct() instead.
 178           *
 179           * @see POMO_FileReader::__construct()
 180           */
 181  		public function POMO_FileReader( $filename ) {
 182              if ( function_exists( '_deprecated_constructor' ) ) {
 183                  _deprecated_constructor( self::class, '5.4.0', static::class );
 184              }
 185              self::__construct( $filename );
 186          }
 187  
 188          /**
 189           * @param int $bytes
 190           * @return string|false Returns read string, otherwise false.
 191           */
 192  		public function read( $bytes ) {
 193              return fread( $this->_f, $bytes );
 194          }
 195  
 196          /**
 197           * @param int $pos
 198           * @return bool
 199           */
 200  		public function seekto( $pos ) {
 201              if ( -1 === fseek( $this->_f, $pos, SEEK_SET ) ) {
 202                  return false;
 203              }
 204              $this->_pos = $pos;
 205              return true;
 206          }
 207  
 208          /**
 209           * @return bool
 210           */
 211  		public function is_resource() {
 212              return is_resource( $this->_f );
 213          }
 214  
 215          /**
 216           * @return bool
 217           */
 218  		public function feof() {
 219              return feof( $this->_f );
 220          }
 221  
 222          /**
 223           * @return bool
 224           */
 225  		public function close() {
 226              return fclose( $this->_f );
 227          }
 228  
 229          /**
 230           * @return string
 231           */
 232  		public function read_all() {
 233              return stream_get_contents( $this->_f );
 234          }
 235      }
 236  endif;
 237  
 238  if ( ! class_exists( 'POMO_StringReader', false ) ) :
 239      /**
 240       * Provides file-like methods for manipulating a string instead
 241       * of a physical file.
 242       */
 243      class POMO_StringReader extends POMO_Reader {
 244  
 245          public $_str = '';
 246  
 247          /**
 248           * PHP5 constructor.
 249           */
 250  		public function __construct( $str = '' ) {
 251              parent::__construct();
 252              $this->_str = $str;
 253              $this->_pos = 0;
 254          }
 255  
 256          /**
 257           * PHP4 constructor.
 258           *
 259           * @deprecated 5.4.0 Use __construct() instead.
 260           *
 261           * @see POMO_StringReader::__construct()
 262           */
 263  		public function POMO_StringReader( $str = '' ) {
 264              if ( function_exists( '_deprecated_constructor' ) ) {
 265                  _deprecated_constructor( self::class, '5.4.0', static::class );
 266              }
 267              self::__construct( $str );
 268          }
 269  
 270          /**
 271           * @param string $bytes
 272           * @return string
 273           */
 274  		public function read( $bytes ) {
 275              $data        = $this->substr( $this->_str, $this->_pos, $bytes );
 276              $this->_pos += $bytes;
 277              if ( $this->strlen( $this->_str ) < $this->_pos ) {
 278                  $this->_pos = $this->strlen( $this->_str );
 279              }
 280              return $data;
 281          }
 282  
 283          /**
 284           * @param int $pos
 285           * @return int
 286           */
 287  		public function seekto( $pos ) {
 288              $this->_pos = $pos;
 289              if ( $this->strlen( $this->_str ) < $this->_pos ) {
 290                  $this->_pos = $this->strlen( $this->_str );
 291              }
 292              return $this->_pos;
 293          }
 294  
 295          /**
 296           * @return int
 297           */
 298  		public function length() {
 299              return $this->strlen( $this->_str );
 300          }
 301  
 302          /**
 303           * @return string
 304           */
 305  		public function read_all() {
 306              return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) );
 307          }
 308      }
 309  endif;
 310  
 311  if ( ! class_exists( 'POMO_CachedFileReader', false ) ) :
 312      /**
 313       * Reads the contents of the file in the beginning.
 314       */
 315      class POMO_CachedFileReader extends POMO_StringReader {
 316          /**
 317           * PHP5 constructor.
 318           */
 319  		public function __construct( $filename ) {
 320              parent::__construct();
 321              $this->_str = file_get_contents( $filename );
 322              if ( false === $this->_str ) {
 323                  return false;
 324              }
 325              $this->_pos = 0;
 326          }
 327  
 328          /**
 329           * PHP4 constructor.
 330           *
 331           * @deprecated 5.4.0 Use __construct() instead.
 332           *
 333           * @see POMO_CachedFileReader::__construct()
 334           */
 335  		public function POMO_CachedFileReader( $filename ) {
 336              if ( function_exists( '_deprecated_constructor' ) ) {
 337                  _deprecated_constructor( self::class, '5.4.0', static::class );
 338              }
 339              self::__construct( $filename );
 340          }
 341      }
 342  endif;
 343  
 344  if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) :
 345      /**
 346       * Reads the contents of the file in the beginning.
 347       */
 348      class POMO_CachedIntFileReader extends POMO_CachedFileReader {
 349          /**
 350           * PHP5 constructor.
 351           */
 352  		public function __construct( $filename ) {
 353              parent::__construct( $filename );
 354          }
 355  
 356          /**
 357           * PHP4 constructor.
 358           *
 359           * @deprecated 5.4.0 Use __construct() instead.
 360           *
 361           * @see POMO_CachedIntFileReader::__construct()
 362           */
 363  		public function POMO_CachedIntFileReader( $filename ) {
 364              if ( function_exists( '_deprecated_constructor' ) ) {
 365                  _deprecated_constructor( self::class, '5.4.0', static::class );
 366              }
 367              self::__construct( $filename );
 368          }
 369      }
 370  endif;


Generated: Sun Apr 5 01:00:11 2026 Cross-referenced by PHPXref 0.7.1