| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | -/* | |
| 2 | +/** | |
| 3 | 3 | +=====================================================================+ |
| 4 | 4 | | ____ _ ____ __ _ _ | |
| 5 | 5 | | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ | |
| 6 | 6 | | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 | 7 | | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 | 8 | | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 | 9 | | | |
| 10 | - | (c) Jerome Bruandet ~ https://code-profiler.com/ | | |
| 10 | + | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ | | |
| 11 | 11 | +=====================================================================+ |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } |
| @@ -33,9 +33,8 @@ | ||
| 33 | 33 | 'unlink' => 0, 'url_stat' => 0 |
| 34 | 34 | ]; |
| 35 | 35 | static $io_read = 0; |
| 36 | 36 | static $io_write = 0; |
| 37 | - static $strlen = 18; | |
| 38 | 37 | public $context; |
| 39 | 38 | public $resource; |
| 40 | 39 | protected static $protocol = 'file'; |
| 41 | 40 | private $script; |
| @@ -50,9 +49,9 @@ | ||
| 50 | 49 | public static function stop() { |
| 51 | 50 | stream_wrapper_restore( self::$protocol ); |
| 52 | 51 | } |
| 53 | 52 | |
| 54 | - /* | |
| 53 | + /** | |
| 55 | 54 | * Opens file or URL |
| 56 | 55 | */ |
| 57 | 56 | public function stream_open( $path, $mode, $options, &$opened_path ) { |
| 58 | 57 | self::$io_stats['open']++; |
| @@ -62,13 +61,38 @@ | ||
| 62 | 61 | } else { |
| 63 | 62 | $this->resource = fopen( $path, $mode, $options ); |
| 64 | 63 | } |
| 65 | 64 | self::start(); |
| 66 | - $this->script = "{$path[-4]}{$path[-3]}{$path[-2]}{$path[-1]}" == '.php'; | |
| 65 | + | |
| 66 | + if ( version_compare( PHP_VERSION, '8.0', '<') ) { | |
| 67 | + if ( in_array( $mode, ['rb', 'rt', 'r']) && | |
| 68 | + "{$path[-4]}{$path[-3]}{$path[-2]}{$path[-1]}" == '.php') { | |
| 69 | + | |
| 70 | + $this->script = $this->check_exclusions( $path ); | |
| 71 | + } | |
| 72 | + } else { | |
| 73 | + if ( str_ends_with( $path, '.php') && in_array( $mode, ['rb', 'rt', 'r']) ) { | |
| 74 | + $this->script = $this->check_exclusions( $path ); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 67 | 78 | return $this->resource !== false; |
| 68 | 79 | } |
| 69 | 80 | |
| 70 | - /* | |
| 81 | + /** | |
| 82 | + * Check file and folder exclusions. | |
| 83 | + */ | |
| 84 | + private function check_exclusions( $path ) { | |
| 85 | + | |
| 86 | + foreach( CodeProfiler_Profiler::$exclusions as $item ) { | |
| 87 | + if ( $item && strpos( $path, $item ) !== false ) { | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + } | |
| 91 | + return 1; | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 71 | 95 | * Close a resource |
| 72 | 96 | */ |
| 73 | 97 | public function stream_close() { |
| 74 | 98 | self::$io_stats['close']++; |
| @@ -74,9 +98,9 @@ | ||
| 74 | 98 | self::$io_stats['close']++; |
| 75 | 99 | return fclose( $this->resource ); |
| 76 | 100 | } |
| 77 | 101 | |
| 78 | - /* | |
| 102 | + /** | |
| 79 | 103 | * Tests for end-of-file on a file pointer |
| 80 | 104 | */ |
| 81 | 105 | public function stream_eof() { |
| 82 | 106 | self::$io_stats['eof']++; |
| @@ -82,9 +106,9 @@ | ||
| 82 | 106 | self::$io_stats['eof']++; |
| 83 | 107 | return feof( $this->resource ); |
| 84 | 108 | } |
| 85 | 109 | |
| 86 | - /* | |
| 110 | + /** | |
| 87 | 111 | * Flushes the output |
| 88 | 112 | */ |
| 89 | 113 | public function stream_flush() { |
| 90 | 114 | self::$io_stats['flush']++; |
| @@ -90,9 +114,9 @@ | ||
| 90 | 114 | self::$io_stats['flush']++; |
| 91 | 115 | return fflush( $this->resource ); |
| 92 | 116 | } |
| 93 | 117 | |
| 94 | - /* | |
| 118 | + /** | |
| 95 | 119 | * Seeks to specific location in a stream |
| 96 | 120 | */ |
| 97 | 121 | public function stream_seek( $offset, $whence = SEEK_SET ) { |
| 98 | 122 | self::$io_stats['seek']++; |
| @@ -98,9 +122,9 @@ | ||
| 98 | 122 | self::$io_stats['seek']++; |
| 99 | 123 | return fseek( $this->resource, $offset, $whence ) === 0; |
| 100 | 124 | } |
| 101 | 125 | |
| 102 | - /* | |
| 126 | + /** | |
| 103 | 127 | * Delete a file |
| 104 | 128 | */ |
| 105 | 129 | public function unlink( $path ) { |
| 106 | 130 | self::$io_stats['unlink']++; |
| @@ -113,9 +137,9 @@ | ||
| 113 | 137 | self::start(); |
| 114 | 138 | return $res; |
| 115 | 139 | } |
| 116 | 140 | |
| 117 | - /* | |
| 141 | + /** | |
| 118 | 142 | * Renames a file or directory |
| 119 | 143 | */ |
| 120 | 144 | public function rename( $path_from, $path_to ) { |
| 121 | 145 | self::$io_stats['rename']++; |
| @@ -128,9 +152,9 @@ | ||
| 128 | 152 | self::start(); |
| 129 | 153 | return $res; |
| 130 | 154 | } |
| 131 | 155 | |
| 132 | - /* | |
| 156 | + /** | |
| 133 | 157 | * Removes a directory |
| 134 | 158 | */ |
| 135 | 159 | public function rmdir( $path, $options ) { |
| 136 | 160 | self::$io_stats['rmdir']++; |
| @@ -143,9 +167,9 @@ | ||
| 143 | 167 | self::start(); |
| 144 | 168 | return $res; |
| 145 | 169 | } |
| 146 | 170 | |
| 147 | - /* | |
| 171 | + /** | |
| 148 | 172 | * Retrieve the underlaying resource |
| 149 | 173 | */ |
| 150 | 174 | public function stream_cast( $cast_as ) { |
| 151 | 175 | self::$io_stats['cast']++; |
| @@ -151,9 +175,9 @@ | ||
| 151 | 175 | self::$io_stats['cast']++; |
| 152 | 176 | return $this->resource; |
| 153 | 177 | } |
| 154 | 178 | |
| 155 | - /* | |
| 179 | + /** | |
| 156 | 180 | * Advisory file locking |
| 157 | 181 | */ |
| 158 | 182 | public function stream_lock( $operation ) { |
| 159 | 183 | self::$io_stats['lock']++; |
| @@ -162,9 +186,9 @@ | ||
| 162 | 186 | } |
| 163 | 187 | return flock( $this->resource, $operation ); |
| 164 | 188 | } |
| 165 | 189 | |
| 166 | - /* | |
| 190 | + /** | |
| 167 | 191 | * Truncate stream |
| 168 | 192 | */ |
| 169 | 193 | public function stream_truncate( $new_size ) { |
| 170 | 194 | self::$io_stats['truncate']++; |
| @@ -178,9 +202,9 @@ | ||
| 178 | 202 | self::$io_write += $write; |
| 179 | 203 | return $write; |
| 180 | 204 | } |
| 181 | 205 | |
| 182 | - /* | |
| 206 | + /** | |
| 183 | 207 | * Change stream options |
| 184 | 208 | */ |
| 185 | 209 | public function stream_set_option( $option, $arg1, $arg2 ) { |
| 186 | 210 | self::$io_stats['set_option']++; |
| @@ -197,9 +221,9 @@ | ||
| 197 | 221 | return false; |
| 198 | 222 | } |
| 199 | 223 | } |
| 200 | 224 | |
| 201 | - /* | |
| 225 | + /** | |
| 202 | 226 | * Change stream metadata |
| 203 | 227 | */ |
| 204 | 228 | public function stream_metadata( $path, $option, $value ) { |
| 205 | 229 | self::stop(); |
| @@ -231,9 +255,9 @@ | ||
| 231 | 255 | self::start(); |
| 232 | 256 | return $res; |
| 233 | 257 | } |
| 234 | 258 | |
| 235 | - /* | |
| 259 | + /** | |
| 236 | 260 | * Create a directory |
| 237 | 261 | */ |
| 238 | 262 | public function mkdir( $path, $mode, $options ) { |
| 239 | 263 | self::$io_stats['mkdir']++; |
| @@ -246,9 +270,9 @@ | ||
| 246 | 270 | self::start(); |
| 247 | 271 | return $res; |
| 248 | 272 | } |
| 249 | 273 | |
| 250 | - /* | |
| 274 | + /** | |
| 251 | 275 | * Open directory handle |
| 252 | 276 | */ |
| 253 | 277 | public function dir_opendir( $path, $options ) { |
| 254 | 278 | self::$io_stats['opendir']++; |
| @@ -261,9 +285,9 @@ | ||
| 261 | 285 | self::start(); |
| 262 | 286 | return $this->resource; |
| 263 | 287 | } |
| 264 | 288 | |
| 265 | - /* | |
| 289 | + /** | |
| 266 | 290 | * Close directory handle |
| 267 | 291 | */ |
| 268 | 292 | public function dir_closedir() { |
| 269 | 293 | self::$io_stats['closedir']++; |
| @@ -270,9 +294,9 @@ | ||
| 270 | 294 | // closedir returns no value |
| 271 | 295 | return closedir( $this->resource ); |
| 272 | 296 | } |
| 273 | 297 | |
| 274 | - /* | |
| 298 | + /** | |
| 275 | 299 | * Read entry from directory handle |
| 276 | 300 | */ |
| 277 | 301 | public function dir_readdir() { |
| 278 | 302 | self::$io_stats['readdir']++; |
| @@ -278,9 +302,9 @@ | ||
| 278 | 302 | self::$io_stats['readdir']++; |
| 279 | 303 | return readdir( $this->resource ); |
| 280 | 304 | } |
| 281 | 305 | |
| 282 | - /* | |
| 306 | + /** | |
| 283 | 307 | * Rewind directory handle |
| 284 | 308 | */ |
| 285 | 309 | public function dir_rewinddir() { |
| 286 | 310 | self::$io_stats['rewinddir']++; |
| @@ -286,9 +310,9 @@ | ||
| 286 | 310 | self::$io_stats['rewinddir']++; |
| 287 | 311 | return rewinddir( $this->resource ); |
| 288 | 312 | } |
| 289 | 313 | |
| 290 | - /* | |
| 314 | + /** | |
| 291 | 315 | * Retrieve information about a file |
| 292 | 316 | */ |
| 293 | 317 | public function url_stat( $path, $flags ) { |
| 294 | 318 | self::$io_stats['url_stat']++; |
| @@ -304,27 +328,38 @@ | ||
| 304 | 328 | self::start(); |
| 305 | 329 | return $res; |
| 306 | 330 | } |
| 307 | 331 | |
| 308 | - /* | |
| 332 | + /** | |
| 309 | 333 | * Read from stream & enable ticks |
| 310 | 334 | */ |
| 311 | 335 | public function stream_read( $count ) { |
| 312 | 336 | self::$io_stats['read']++; |
| 313 | - self::stop(); | |
| 314 | - if ( $this->script && ftell( $this->resource ) == 0 ) { | |
| 337 | + if ( $this->script ) { | |
| 338 | + self::stop(); | |
| 339 | + if ( ftell( $this->resource ) == 0 ) { | |
| 340 | + self::start(); | |
| 341 | + $read = fread( $this->resource, $count - CODE_PROFILER_LENGTH ); | |
| 342 | + self::$io_read += strlen( $read ); | |
| 343 | + $pos = stripos( $read, '<?php' ); | |
| 344 | + if ( $pos !== false ) { | |
| 345 | + return substr_replace( | |
| 346 | + $read, | |
| 347 | + '<?php declare(ticks='. CODE_PROFILER_TICKS .');', | |
| 348 | + $pos, | |
| 349 | + 5 | |
| 350 | + ); | |
| 351 | + } | |
| 352 | + return $read; | |
| 353 | + } | |
| 315 | 354 | self::start(); |
| 316 | - $read = fread( $this->resource, $count - self::$strlen ); | |
| 317 | - self::$io_read += strlen( $read ); | |
| 318 | - return preg_replace( '/<\?php/i', '<?php declare(ticks=1);', $read, 1 ); | |
| 319 | 355 | } |
| 320 | - self::start(); | |
| 321 | 356 | $read = fread( $this->resource, $count ); |
| 322 | 357 | self::$io_read += strlen( $read ); |
| 323 | 358 | return $read; |
| 324 | - } | |
| 359 | + } | |
| 325 | 360 | |
| 326 | - /* | |
| 361 | + /** | |
| 327 | 362 | * Retrieve information about a file resource |
| 328 | 363 | */ |
| 329 | 364 | public function stream_stat() { |
| 330 | 365 | self::$io_stats['stat']++; |
| @@ -329,15 +364,15 @@ | ||
| 329 | 364 | public function stream_stat() { |
| 330 | 365 | self::$io_stats['stat']++; |
| 331 | 366 | $res = fstat( $this->resource ); |
| 332 | 367 | if ( $this->script ) { |
| 333 | - $res['size'] += self::$strlen; | |
| 334 | - $res[7] += self::$strlen; | |
| 368 | + $res['size'] += CODE_PROFILER_LENGTH; | |
| 369 | + $res[7] += CODE_PROFILER_LENGTH; | |
| 335 | 370 | } |
| 336 | 371 | return $res; |
| 337 | 372 | } |
| 338 | 373 | |
| 339 | - /* | |
| 374 | + /** | |
| 340 | 375 | * Retrieve the current position of a stream |
| 341 | 376 | */ |
| 342 | 377 | function stream_tell() { |
| 343 | 378 | self::$io_stats['tell']++; |
| @@ -342,9 +377,9 @@ | ||
| 342 | 377 | function stream_tell() { |
| 343 | 378 | self::$io_stats['tell']++; |
| 344 | 379 | $res = ftell( $this->resource ); |
| 345 | 380 | if ( $this->script ) { |
| 346 | - $res += self::$strlen; | |
| 381 | + $res += CODE_PROFILER_LENGTH; | |
| 347 | 382 | } |
| 348 | 383 | return $res; |
| 349 | 384 | } |
| 350 | 385 | } |