kollapsminoriteten/wp-includes/pomo/streams.php

354 lines
7.5 KiB
PHP
Raw Normal View History

2019-11-02 10:38:58 +01:00
<?php
/**
* Classes, which help reading streams of data from files.
* Based on the classes from Danilo Segan <danilo@kvota.net>
*
* @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $
* @package pomo
* @subpackage streams
*/
if ( ! class_exists( 'POMO_Reader', false ) ) :
class POMO_Reader {
2020-12-10 14:06:04 +01:00
public $endian = 'little';
public $_post = '';
2019-11-02 10:38:58 +01:00
/**
* PHP5 constructor.
*/
2022-04-02 10:26:41 +02:00
public function __construct() {
2021-07-23 11:58:50 +02:00
if ( function_exists( 'mb_substr' )
&& ( (int) ini_get( 'mbstring.func_overload' ) & 2 ) // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated
) {
$this->is_overloaded = true;
} else {
$this->is_overloaded = false;
}
$this->_pos = 0;
2019-11-02 10:38:58 +01:00
}
/**
* PHP4 constructor.
2020-05-06 17:23:38 +02:00
*
* @deprecated 5.4.0 Use __construct() instead.
*
* @see POMO_Reader::__construct()
2019-11-02 10:38:58 +01:00
*/
public function POMO_Reader() {
2020-05-06 17:23:38 +02:00
_deprecated_constructor( self::class, '5.4.0', static::class );
2019-11-02 10:38:58 +01:00
self::__construct();
}
/**
* Sets the endianness of the file.
*
* @param string $endian Set the endianness of the file. Accepts 'big', or 'little'.
*/
2022-04-02 10:26:41 +02:00
public function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
2019-11-02 10:38:58 +01:00
$this->endian = $endian;
}
/**
* Reads a 32bit Integer from the Stream
*
* @return mixed The integer, corresponding to the next 32 bits from
* the stream of false if there are not enough bytes or on error
*/
2022-04-02 10:26:41 +02:00
public function readint32() {
2019-11-02 10:38:58 +01:00
$bytes = $this->read( 4 );
if ( 4 != $this->strlen( $bytes ) ) {
return false;
}
2020-09-15 14:29:22 +02:00
$endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V';
2019-11-02 10:38:58 +01:00
$int = unpack( $endian_letter, $bytes );
return reset( $int );
}
/**
* Reads an array of 32-bit Integers from the Stream
*
2020-12-10 14:06:04 +01:00
* @param int $count How many elements should be read
2019-11-02 10:38:58 +01:00
* @return mixed Array of integers or false if there isn't
* enough data or on error
*/
2022-04-02 10:26:41 +02:00
public function readint32array( $count ) {
2019-11-02 10:38:58 +01:00
$bytes = $this->read( 4 * $count );
if ( 4 * $count != $this->strlen( $bytes ) ) {
return false;
}
2020-09-15 14:29:22 +02:00
$endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V';
2019-11-02 10:38:58 +01:00
return unpack( $endian_letter . $count, $bytes );
}
/**
* @param string $string
* @param int $start
* @param int $length
* @return string
*/
2022-04-02 10:26:41 +02:00
public function substr( $string, $start, $length ) {
2019-11-02 10:38:58 +01:00
if ( $this->is_overloaded ) {
return mb_substr( $string, $start, $length, 'ascii' );
} else {
return substr( $string, $start, $length );
}
}
/**
* @param string $string
* @return int
*/
2022-04-02 10:26:41 +02:00
public function strlen( $string ) {
2019-11-02 10:38:58 +01:00
if ( $this->is_overloaded ) {
return mb_strlen( $string, 'ascii' );
} else {
return strlen( $string );
}
}
/**
* @param string $string
* @param int $chunk_size
* @return array
*/
2022-04-02 10:26:41 +02:00
public function str_split( $string, $chunk_size ) {
2019-11-02 10:38:58 +01:00
if ( ! function_exists( 'str_split' ) ) {
$length = $this->strlen( $string );
$out = array();
for ( $i = 0; $i < $length; $i += $chunk_size ) {
$out[] = $this->substr( $string, $i, $chunk_size );
}
return $out;
} else {
return str_split( $string, $chunk_size );
}
}
/**
* @return int
*/
2022-04-02 10:26:41 +02:00
public function pos() {
2019-11-02 10:38:58 +01:00
return $this->_pos;
}
/**
* @return true
*/
2022-04-02 10:26:41 +02:00
public function is_resource() {
2019-11-02 10:38:58 +01:00
return true;
}
/**
* @return true
*/
2022-04-02 10:26:41 +02:00
public function close() {
2019-11-02 10:38:58 +01:00
return true;
}
}
endif;
if ( ! class_exists( 'POMO_FileReader', false ) ) :
class POMO_FileReader extends POMO_Reader {
/**
* @param string $filename
*/
2022-04-02 10:26:41 +02:00
public function __construct( $filename ) {
2020-05-06 17:23:38 +02:00
parent::__construct();
2019-11-02 10:38:58 +01:00
$this->_f = fopen( $filename, 'rb' );
}
/**
* PHP4 constructor.
2020-05-06 17:23:38 +02:00
*
* @deprecated 5.4.0 Use __construct() instead.
*
* @see POMO_FileReader::__construct()
2019-11-02 10:38:58 +01:00
*/
public function POMO_FileReader( $filename ) {
2020-05-06 17:23:38 +02:00
_deprecated_constructor( self::class, '5.4.0', static::class );
2019-11-02 10:38:58 +01:00
self::__construct( $filename );
}
/**
* @param int $bytes
* @return string|false Returns read string, otherwise false.
*/
2022-04-02 10:26:41 +02:00
public function read( $bytes ) {
2019-11-02 10:38:58 +01:00
return fread( $this->_f, $bytes );
}
/**
* @param int $pos
2020-12-10 14:06:04 +01:00
* @return bool
2019-11-02 10:38:58 +01:00
*/
2022-04-02 10:26:41 +02:00
public function seekto( $pos ) {
2019-11-02 10:38:58 +01:00
if ( -1 == fseek( $this->_f, $pos, SEEK_SET ) ) {
return false;
}
$this->_pos = $pos;
return true;
}
/**
* @return bool
*/
2022-04-02 10:26:41 +02:00
public function is_resource() {
2019-11-02 10:38:58 +01:00
return is_resource( $this->_f );
}
/**
* @return bool
*/
2022-04-02 10:26:41 +02:00
public function feof() {
2019-11-02 10:38:58 +01:00
return feof( $this->_f );
}
/**
* @return bool
*/
2022-04-02 10:26:41 +02:00
public function close() {
2019-11-02 10:38:58 +01:00
return fclose( $this->_f );
}
/**
* @return string
*/
2022-04-02 10:26:41 +02:00
public function read_all() {
2022-06-16 14:03:35 +02:00
return stream_get_contents( $this->_f );
2019-11-02 10:38:58 +01:00
}
}
endif;
if ( ! class_exists( 'POMO_StringReader', false ) ) :
/**
* Provides file-like methods for manipulating a string instead
* of a physical file.
*/
class POMO_StringReader extends POMO_Reader {
2020-12-10 14:06:04 +01:00
public $_str = '';
2019-11-02 10:38:58 +01:00
/**
* PHP5 constructor.
*/
2022-04-02 10:26:41 +02:00
public function __construct( $str = '' ) {
2020-05-06 17:23:38 +02:00
parent::__construct();
2019-11-02 10:38:58 +01:00
$this->_str = $str;
$this->_pos = 0;
}
/**
* PHP4 constructor.
2020-05-06 17:23:38 +02:00
*
* @deprecated 5.4.0 Use __construct() instead.
*
* @see POMO_StringReader::__construct()
2019-11-02 10:38:58 +01:00
*/
public function POMO_StringReader( $str = '' ) {
2020-05-06 17:23:38 +02:00
_deprecated_constructor( self::class, '5.4.0', static::class );
2019-11-02 10:38:58 +01:00
self::__construct( $str );
}
/**
* @param string $bytes
* @return string
*/
2022-04-02 10:26:41 +02:00
public function read( $bytes ) {
2019-11-02 10:38:58 +01:00
$data = $this->substr( $this->_str, $this->_pos, $bytes );
$this->_pos += $bytes;
if ( $this->strlen( $this->_str ) < $this->_pos ) {
$this->_pos = $this->strlen( $this->_str );
}
return $data;
}
/**
* @param int $pos
* @return int
*/
2022-04-02 10:26:41 +02:00
public function seekto( $pos ) {
2019-11-02 10:38:58 +01:00
$this->_pos = $pos;
if ( $this->strlen( $this->_str ) < $this->_pos ) {
$this->_pos = $this->strlen( $this->_str );
}
return $this->_pos;
}
/**
* @return int
*/
2022-04-02 10:26:41 +02:00
public function length() {
2019-11-02 10:38:58 +01:00
return $this->strlen( $this->_str );
}
/**
* @return string
*/
2022-04-02 10:26:41 +02:00
public function read_all() {
2019-11-02 10:38:58 +01:00
return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) );
}
}
endif;
if ( ! class_exists( 'POMO_CachedFileReader', false ) ) :
/**
* Reads the contents of the file in the beginning.
*/
class POMO_CachedFileReader extends POMO_StringReader {
/**
* PHP5 constructor.
*/
2022-04-02 10:26:41 +02:00
public function __construct( $filename ) {
2020-05-06 17:23:38 +02:00
parent::__construct();
2019-11-02 10:38:58 +01:00
$this->_str = file_get_contents( $filename );
if ( false === $this->_str ) {
return false;
}
$this->_pos = 0;
}
/**
* PHP4 constructor.
2020-05-06 17:23:38 +02:00
*
* @deprecated 5.4.0 Use __construct() instead.
*
* @see POMO_CachedFileReader::__construct()
2019-11-02 10:38:58 +01:00
*/
public function POMO_CachedFileReader( $filename ) {
2020-05-06 17:23:38 +02:00
_deprecated_constructor( self::class, '5.4.0', static::class );
2019-11-02 10:38:58 +01:00
self::__construct( $filename );
}
}
endif;
if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) :
/**
* Reads the contents of the file in the beginning.
*/
class POMO_CachedIntFileReader extends POMO_CachedFileReader {
/**
* PHP5 constructor.
*/
public function __construct( $filename ) {
2020-05-06 17:23:38 +02:00
parent::__construct( $filename );
2019-11-02 10:38:58 +01:00
}
/**
* PHP4 constructor.
2020-05-06 17:23:38 +02:00
*
* @deprecated 5.4.0 Use __construct() instead.
*
* @see POMO_CachedIntFileReader::__construct()
2019-11-02 10:38:58 +01:00
*/
2022-04-02 10:26:41 +02:00
public function POMO_CachedIntFileReader( $filename ) {
2020-05-06 17:23:38 +02:00
_deprecated_constructor( self::class, '5.4.0', static::class );
2019-11-02 10:38:58 +01:00
self::__construct( $filename );
}
}
endif;