PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/pomo/streams.php

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