| 1 |
<?php |
| 2 |
/** |
| 3 |
+=====================================================================+ |
| 4 |
| ____ _ ____ __ _ _ | |
| 5 |
| / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ | |
| 6 |
| | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 |
| | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 |
| \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 |
| | |
| 10 |
| (c) Jerome Bruandet ~ https://code-profiler.com/ | |
| 11 |
+=====================================================================+ |
| 12 |
*/ |
| 13 |
|
| 14 |
if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } |
| 15 |
|
| 16 |
// ===================================================================== |
| 17 |
|
| 18 |
class CodeProfiler_Stream { |
| 19 |
|
| 20 |
static $io_stats = [ |
| 21 |
'cast' => 0, 'chgrp' => 0, |
| 22 |
'chmod' => 0, 'chown' => 0, |
| 23 |
'close' => 0, 'closedir' => 0, |
| 24 |
'eof' => 0, 'flush' => 0, |
| 25 |
'lock' => 0, 'mkdir' => 0, |
| 26 |
'open' => 0, 'opendir' => 0, |
| 27 |
'readdir' => 0, 'rewinddir' => 0, |
| 28 |
'read' => 0, 'rename' => 0, |
| 29 |
'rmdir' => 0, 'seek' => 0, |
| 30 |
'set_option'=> 0, 'stat' => 0, |
| 31 |
'tell' => 0, 'truncate' => 0, |
| 32 |
'write' => 0, 'touch' => 0, |
| 33 |
'unlink' => 0, 'url_stat' => 0 |
| 34 |
]; |
| 35 |
static $io_read = 0; |
| 36 |
static $io_write = 0; |
| 37 |
public $context; |
| 38 |
public $resource; |
| 39 |
protected static $protocol = 'file'; |
| 40 |
private $script; |
| 41 |
|
| 42 |
|
| 43 |
public static function start() { |
| 44 |
stream_wrapper_unregister( self::$protocol ); |
| 45 |
stream_wrapper_register( self::$protocol, CodeProfiler_Stream::class ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public static function stop() { |
| 50 |
stream_wrapper_restore( self::$protocol ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Opens file or URL |
| 55 |
*/ |
| 56 |
public function stream_open( $path, $mode, $options, &$opened_path ) { |
| 57 |
self::$io_stats['open']++; |
| 58 |
self::stop(); |
| 59 |
if ( isset( $this->context) ) { |
| 60 |
$this->resource = fopen( $path, $mode, $options, $this->context ); |
| 61 |
} else { |
| 62 |
$this->resource = fopen( $path, $mode, $options ); |
| 63 |
} |
| 64 |
self::start(); |
| 65 |
if ( in_array( $mode, ['rb', 'rt', 'r']) && "{$path[-4]}{$path[-3]}{$path[-2]}{$path[-1]}" == '.php') { |
| 66 |
$this->script = 1; |
| 67 |
} |
| 68 |
return $this->resource !== false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Close a resource |
| 73 |
*/ |
| 74 |
public function stream_close() { |
| 75 |
self::$io_stats['close']++; |
| 76 |
return fclose( $this->resource ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Tests for end-of-file on a file pointer |
| 81 |
*/ |
| 82 |
public function stream_eof() { |
| 83 |
self::$io_stats['eof']++; |
| 84 |
return feof( $this->resource ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Flushes the output |
| 89 |
*/ |
| 90 |
public function stream_flush() { |
| 91 |
self::$io_stats['flush']++; |
| 92 |
return fflush( $this->resource ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Seeks to specific location in a stream |
| 97 |
*/ |
| 98 |
public function stream_seek( $offset, $whence = SEEK_SET ) { |
| 99 |
self::$io_stats['seek']++; |
| 100 |
return fseek( $this->resource, $offset, $whence ) === 0; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Delete a file |
| 105 |
*/ |
| 106 |
public function unlink( $path ) { |
| 107 |
self::$io_stats['unlink']++; |
| 108 |
self::stop(); |
| 109 |
if ( isset( $this->context ) ) { |
| 110 |
$res = unlink( $path, $this->context ); |
| 111 |
} else { |
| 112 |
$res = unlink( $path ); |
| 113 |
} |
| 114 |
self::start(); |
| 115 |
return $res; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Renames a file or directory |
| 120 |
*/ |
| 121 |
public function rename( $path_from, $path_to ) { |
| 122 |
self::$io_stats['rename']++; |
| 123 |
self::stop(); |
| 124 |
if ( isset( $this->context ) ) { |
| 125 |
$res = rename( $path_from, $path_to, $this->context ); |
| 126 |
} else { |
| 127 |
$res = rename( $path_from, $path_to ); |
| 128 |
} |
| 129 |
self::start(); |
| 130 |
return $res; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Removes a directory |
| 135 |
*/ |
| 136 |
public function rmdir( $path, $options ) { |
| 137 |
self::$io_stats['rmdir']++; |
| 138 |
self::stop(); |
| 139 |
if ( isset( $this->context ) ) { |
| 140 |
$res = rmdir( $path, $this->context ); |
| 141 |
} else { |
| 142 |
$res = rmdir( $path ); |
| 143 |
} |
| 144 |
self::start(); |
| 145 |
return $res; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Retrieve the underlaying resource |
| 150 |
*/ |
| 151 |
public function stream_cast( $cast_as ) { |
| 152 |
self::$io_stats['cast']++; |
| 153 |
return $this->resource; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Advisory file locking |
| 158 |
*/ |
| 159 |
public function stream_lock( $operation ) { |
| 160 |
self::$io_stats['lock']++; |
| 161 |
if (! $operation ) { |
| 162 |
$operation = LOCK_EX; |
| 163 |
} |
| 164 |
return flock( $this->resource, $operation ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Truncate stream |
| 169 |
*/ |
| 170 |
public function stream_truncate( $new_size ) { |
| 171 |
self::$io_stats['truncate']++; |
| 172 |
return ftruncate( $this->resource, $new_size ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
public function stream_write( $data ) { |
| 177 |
self::$io_stats['write']++; |
| 178 |
$write = fwrite( $this->resource, $data ); |
| 179 |
self::$io_write += $write; |
| 180 |
return $write; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Change stream options |
| 185 |
*/ |
| 186 |
public function stream_set_option( $option, $arg1, $arg2 ) { |
| 187 |
self::$io_stats['set_option']++; |
| 188 |
switch ( $option ) { |
| 189 |
case STREAM_OPTION_BLOCKING: |
| 190 |
return stream_set_blocking( $this->resource, $arg1 ); |
| 191 |
case STREAM_OPTION_READ_TIMEOUT: |
| 192 |
return stream_set_timeout( $this->resource, $arg1, $arg2 ); |
| 193 |
case STREAM_OPTION_WRITE_BUFFER: |
| 194 |
return stream_set_write_buffer( $this->resource, $arg1 ); |
| 195 |
case STREAM_OPTION_READ_BUFFER: |
| 196 |
return stream_set_read_buffer( $this->resource, $arg1 ); |
| 197 |
default: |
| 198 |
return false; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Change stream metadata |
| 204 |
*/ |
| 205 |
public function stream_metadata( $path, $option, $value ) { |
| 206 |
self::stop(); |
| 207 |
$res = false; |
| 208 |
switch ( $option ) { |
| 209 |
case STREAM_META_ACCESS: |
| 210 |
$res = chmod( $path, $value ); |
| 211 |
self::$io_stats['chmod']++; |
| 212 |
break; |
| 213 |
case STREAM_META_GROUP: |
| 214 |
case STREAM_META_GROUP_NAME: |
| 215 |
$res = chgrp( $path, $value ); |
| 216 |
self::$io_stats['chgrp']++; |
| 217 |
break; |
| 218 |
case STREAM_META_OWNER: |
| 219 |
case STREAM_META_OWNER_NAME: |
| 220 |
$res = chown( $path, $value ); |
| 221 |
self::$io_stats['chown']++; |
| 222 |
break; |
| 223 |
case STREAM_META_TOUCH: |
| 224 |
if (! empty( $value ) ) { |
| 225 |
$res = touch( $path, $value[0], $value[1] ); |
| 226 |
} else { |
| 227 |
$res = touch( $path ); |
| 228 |
} |
| 229 |
self::$io_stats['touch']++; |
| 230 |
break; |
| 231 |
} |
| 232 |
self::start(); |
| 233 |
return $res; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Create a directory |
| 238 |
*/ |
| 239 |
public function mkdir( $path, $mode, $options ) { |
| 240 |
self::$io_stats['mkdir']++; |
| 241 |
self::stop(); |
| 242 |
if ( isset( $this->context ) ) { |
| 243 |
$res = mkdir( $path, $mode, $options, $this->context ); |
| 244 |
} else { |
| 245 |
$res = mkdir( $path, $mode, $options ); |
| 246 |
} |
| 247 |
self::start(); |
| 248 |
return $res; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Open directory handle |
| 253 |
*/ |
| 254 |
public function dir_opendir( $path, $options ) { |
| 255 |
self::$io_stats['opendir']++; |
| 256 |
self::stop(); |
| 257 |
if ( isset( $this->context ) ) { |
| 258 |
$this->resource = opendir( $path, $this->context ); |
| 259 |
} else { |
| 260 |
$this->resource = opendir( $path ); |
| 261 |
} |
| 262 |
self::start(); |
| 263 |
return $this->resource; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Close directory handle |
| 268 |
*/ |
| 269 |
public function dir_closedir() { |
| 270 |
self::$io_stats['closedir']++; |
| 271 |
// closedir returns no value |
| 272 |
return closedir( $this->resource ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Read entry from directory handle |
| 277 |
*/ |
| 278 |
public function dir_readdir() { |
| 279 |
self::$io_stats['readdir']++; |
| 280 |
return readdir( $this->resource ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Rewind directory handle |
| 285 |
*/ |
| 286 |
public function dir_rewinddir() { |
| 287 |
self::$io_stats['rewinddir']++; |
| 288 |
return rewinddir( $this->resource ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Retrieve information about a file |
| 293 |
*/ |
| 294 |
public function url_stat( $path, $flags ) { |
| 295 |
self::$io_stats['url_stat']++; |
| 296 |
self::stop(); |
| 297 |
// Catch error and exception |
| 298 |
set_error_handler( function() {} ); |
| 299 |
try { |
| 300 |
$res = stat( $path ); |
| 301 |
} catch ( \Exception $e ) { |
| 302 |
$res = null; |
| 303 |
} |
| 304 |
restore_error_handler(); |
| 305 |
self::start(); |
| 306 |
return $res; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Read from stream & enable ticks |
| 311 |
*/ |
| 312 |
public function stream_read( $count ) { |
| 313 |
self::$io_stats['read']++; |
| 314 |
if ( $this->script ) { |
| 315 |
self::stop(); |
| 316 |
if ( ftell( $this->resource ) == 0 ) { |
| 317 |
self::start(); |
| 318 |
$read = fread( $this->resource, $count - CODE_PROFILER_LENGTH ); |
| 319 |
self::$io_read += strlen( $read ); |
| 320 |
return preg_replace( |
| 321 |
'/<\?php/i', |
| 322 |
'<?php declare(ticks='. CODE_PROFILER_TICKS .');', |
| 323 |
$read, |
| 324 |
1 |
| 325 |
); |
| 326 |
} |
| 327 |
self::start(); |
| 328 |
} |
| 329 |
$read = fread( $this->resource, $count ); |
| 330 |
self::$io_read += strlen( $read ); |
| 331 |
return $read; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Retrieve information about a file resource |
| 336 |
*/ |
| 337 |
public function stream_stat() { |
| 338 |
self::$io_stats['stat']++; |
| 339 |
$res = fstat( $this->resource ); |
| 340 |
if ( $this->script ) { |
| 341 |
$res['size'] += CODE_PROFILER_LENGTH; |
| 342 |
$res[7] += CODE_PROFILER_LENGTH; |
| 343 |
} |
| 344 |
return $res; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Retrieve the current position of a stream |
| 349 |
*/ |
| 350 |
function stream_tell() { |
| 351 |
self::$io_stats['tell']++; |
| 352 |
$res = ftell( $this->resource ); |
| 353 |
if ( $this->script ) { |
| 354 |
$res += CODE_PROFILER_LENGTH; |
| 355 |
} |
| 356 |
return $res; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// ===================================================================== |
| 361 |
// EOF |
| 362 |
|