3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); $fh = fopen(__FILE__, 'r'); fseek($fh, __COMPILER_HALT_OFFSET__, SEEK_SET); $mh = new MimeHalt($fh); var_dump($mh); eval('?>' . $mh->getFile('/test.php')); exit; class MimeHalt { public $config; protected $fh; protected $offset_diff = 0; protected $offset_after_main_header = 0; protected $offset_after_first_file_header = 0; protected $mime_boundary; public function __construct($fh) { $this->fh = $fh; $this->offset_diff = ftell($this->fh); $this->init(); } public function init() { $this->offset_after_first_file_header = 0; $this->resetOffset(); $first_line = stream_get_line($this->fh, PHP_INT_MAX, "\n"); $matches = array(); if(!preg_match('/^content\-type: multipart\/related; boundary=(.+)(?:;\s+.+)?$/i', $first_line, $matches)) { throw new RuntimeException('Invalid or corrupt file'); } $this->mime_boundary = '--' . $matches[1]; $this->resetOffset(); $header = static::readNextHeaderChunk($this->fh); #var_dump(static::processHeader($header)); fseek($this->fh, strlen($this->mime_boundary) + 1, SEEK_CUR); $this->offset_after_main_header = ftell($this->fh) - $this->offset_diff; $json_header = static::readNextHeaderChunk($this->fh); $json_body = static::readNextBodyChunk($this->fh, $this->mime_boundary); #var_dump($json_body); $this->config = json_decode($json_body, true); $this->offset_after_first_file_header = ftell($this->fh) - $this->offset_diff; $this->resetOffset(); if(!is_array($this->config['offset_map']) || !count($this->config['offset_map'])) $this->buildOffsetMap(); } public function resetOffset() { fseek($this->fh, $this->offset_diff + $this->offset_after_first_file_header, SEEK_SET); } public function buildOffsetMap() { #echo "Starting to rebuild offset map starting at position ", ftell($this->fh), "\n"; fseek($this->fh, $this->offset_diff + $this->offset_after_main_header, SEEK_SET); $header_text = ''; while($header_text = static::readNextHeaderChunk($this->fh)) { $headers = static::processHeader($header_text); #var_dump([$headers, $header_text]); $this->config['offset_map'][$headers['Content-ID']] = ftell($this->fh); static::readNextBodyChunk($this->fh, $this->mime_boundary); } #var_dump($this->config['offset_map']); } public function getFile($filename) { $content_id = $this->config['files'][$filename]; $offset = $this->config['offset_map'][$content_id]; fseek($this->fh, $offset, SEEK_SET); return static::readNextBodyChunk($this->fh, $this->mime_boundary); } protected static function readNextHeaderChunk($fh) { $line = stream_get_line($fh, PHP_INT_MAX, "\n\n"); if($line === false) return false; return $line . "\n"; } protected static function readNextBodyChunk($fh, $boundary) { $line = stream_get_line($fh, PHP_INT_MAX, "\n" . $boundary . "\n"); $end_of_body_string = "\n" . $boundary . "--\n"; $eobs_length = strlen($end_of_body_string); if(substr($line, $eobs_length * -1) == $end_of_body_string) $line = substr($line, 0, $eobs_length * -1); return $line; } protected static function processHeader($header_text) { $headers = array(); $matches = array(); $found = preg_match_all('/^(.+): ((.|\r\n\s)+)\r?\n/m', $header_text, $matches, PREG_SET_ORDER); if($found) { foreach($matches as $set) $headers[ $set[1] ] = $set[2]; } return $headers; } } __halt_compiler();Content-type: multipart/related; boundary=mimehalt_outer X-MimeHalt-Version: 1.0.0 --mimehalt_outer Content-Type: application/json Content-ID: 100000001 { "files": { "/mimehalt.json": "100000001", "/test.php": "100000002" }, "dirs": [ "/" ], "offset_map": null } --mimehalt_outer Content-Type: application/x-php Content-ID: 100000002 <?php echo "Hello, world!"; ?> --mimehalt_outer--
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 79) Position 1 = -2
filename:       /in/eG1qd
function name:  (null)
number of ops:  27
compiled vars:  !0 = $fh, !1 = $mh
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 -1
          2        DO_ICALL                                                 
    5     3        INIT_FCALL                                               'fopen'
          4        SEND_VAL                                                 '%2Fin%2FeG1qd'
          5        SEND_VAL                                                 'r'
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !0, $3
    6     8        INIT_FCALL                                               'fseek'
          9        SEND_VAR                                                 !0
         10        SEND_VAL                                                 4207
         11        SEND_VAL                                                 0
         12        DO_ICALL                                                 
    8    13        NEW                                              $6      'MimeHalt'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !1, $6
    9    17        INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                                 
   10    20        INIT_METHOD_CALL                                         !1, 'getFile'
         21        SEND_VAL_EX                                              '%2Ftest.php'
         22        DO_FCALL                                      0  $10     
         23        CONCAT                                           ~11     '%3F%3E', $10
         24        INCLUDE_OR_EVAL                                          ~11, EVAL
   11    25      > EXIT                                                     
  111    26*     > RETURN                                                   1

Class MimeHalt:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/eG1qd
function name:  __construct
number of ops:  12
compiled vars:  !0 = $fh
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        ASSIGN_OBJ                                               'fh'
          2        OP_DATA                                                  !0
   26     3        INIT_FCALL                                               'ftell'
          4        FETCH_OBJ_R                                      ~3      'fh'
          5        SEND_VAL                                                 ~3
          6        DO_ICALL                                         $4      
          7        ASSIGN_OBJ                                               'offset_diff'
          8        OP_DATA                                                  $4
   27     9        INIT_METHOD_CALL                                         'init'
         10        DO_FCALL                                      0          
   28    11      > RETURN                                                   null

End of function __construct

Function init:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 47) Position 1 = 88, Position 2 = 93
Branch analysis from position: 88
2 jumps found. (Code = 43) Position 1 = 94, Position 2 = 96
Branch analysis from position: 94
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 96
Branch analysis from position: 93
filename:       /in/eG1qd
function name:  init
number of ops:  97
compiled vars:  !0 = $first_line, !1 = $matches, !2 = $header, !3 = $json_header, !4 = $json_body
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   ASSIGN_OBJ                                               'offset_after_first_file_header'
          1        OP_DATA                                                  0
   32     2        INIT_METHOD_CALL                                         'resetOffset'
          3        DO_FCALL                                      0          
   34     4        INIT_FCALL                                               'stream_get_line'
          5        FETCH_OBJ_R                                      ~7      'fh'
          6        SEND_VAL                                                 ~7
          7        SEND_VAL                                                 9223372036854775807
          8        SEND_VAL                                                 '%0A'
          9        DO_ICALL                                         $8      
         10        ASSIGN                                                   !0, $8
   35    11        ASSIGN                                                   !1, <array>
   36    12        INIT_FCALL                                               'preg_match'
         13        SEND_VAL                                                 '%2F%5Econtent%5C-type%3A+multipart%5C%2Frelated%3B+boundary%3D%28.%2B%29%28%3F%3A%3B%5Cs%2B.%2B%29%3F%24%2Fi'
         14        SEND_VAR                                                 !0
         15        SEND_REF                                                 !1
         16        DO_ICALL                                         $11     
         17        BOOL_NOT                                         ~12     $11
         18      > JMPZ                                                     ~12, ->23
   37    19    >   NEW                                              $13     'RuntimeException'
         20        SEND_VAL_EX                                              'Invalid+or+corrupt+file'
         21        DO_FCALL                                      0          
         22      > THROW                                         0          $13
   39    23    >   FETCH_DIM_R                                      ~16     !1, 1
         24        CONCAT                                           ~17     '--', ~16
         25        ASSIGN_OBJ                                               'mime_boundary'
         26        OP_DATA                                                  ~17
   40    27        INIT_METHOD_CALL                                         'resetOffset'
         28        DO_FCALL                                      0          
   41    29        INIT_STATIC_METHOD_CALL                                  'readNextHeaderChunk'
         30        CHECK_FUNC_ARG                                           
         31        FETCH_OBJ_FUNC_ARG                               $19     'fh'
         32        SEND_FUNC_ARG                                            $19
         33        DO_FCALL                                      0  $20     
         34        ASSIGN                                                   !2, $20
   43    35        INIT_FCALL                                               'fseek'
         36        FETCH_OBJ_R                                      ~22     'fh'
         37        SEND_VAL                                                 ~22
         38        FETCH_OBJ_R                                      ~23     'mime_boundary'
         39        STRLEN                                           ~24     ~23
         40        ADD                                              ~25     ~24, 1
         41        SEND_VAL                                                 ~25
         42        SEND_VAL                                                 1
         43        DO_ICALL                                                 
   44    44        INIT_FCALL                                               'ftell'
         45        FETCH_OBJ_R                                      ~28     'fh'
         46        SEND_VAL                                                 ~28
         47        DO_ICALL                                         $29     
         48        FETCH_OBJ_R                                      ~30     'offset_diff'
         49        SUB                                              ~31     $29, ~30
         50        ASSIGN_OBJ                                               'offset_after_main_header'
         51        OP_DATA                                                  ~31
   46    52        INIT_STATIC_METHOD_CALL                                  'readNextHeaderChunk'
         53        CHECK_FUNC_ARG                                           
         54        FETCH_OBJ_FUNC_ARG                               $32     'fh'
         55        SEND_FUNC_ARG                                            $32
         56        DO_FCALL                                      0  $33     
         57        ASSIGN                                                   !3, $33
   47    58        INIT_STATIC_METHOD_CALL                                  'readNextBodyChunk'
         59        CHECK_FUNC_ARG                                           
         60        FETCH_OBJ_FUNC_ARG                               $35     'fh'
         61        SEND_FUNC_ARG                                            $35
         62        CHECK_FUNC_ARG                                           
         63        FETCH_OBJ_FUNC_ARG                               $36     'mime_boundary'
         64        SEND_FUNC_ARG                                            $36
         65        DO_FCALL                                      0  $37     
         66        ASSIGN                                                   !4, $37
   49    67        INIT_FCALL                                               'json_decode'
         68        SEND_VAR                                                 !4
         69        SEND_VAL                                                 <true>
         70        DO_ICALL                                         $40     
         71        ASSIGN_OBJ                                               'config'
         72        OP_DATA                                                  $40
   51    73        INIT_FCALL                                               'ftell'
         74        FETCH_OBJ_R                                      ~42     'fh'
         75        SEND_VAL                                                 ~42
         76        DO_ICALL                                         $43     
         77        FETCH_OBJ_R                                      ~44     'offset_diff'
         78        SUB                                              ~45     $43, ~44
         79        ASSIGN_OBJ                                               'offset_after_first_file_header'
         80        OP_DATA                                                  ~45
   52    81        INIT_METHOD_CALL                                         'resetOffset'
         82        DO_FCALL                                      0          
   54    83        FETCH_OBJ_R                                      ~47     'config'
         84        FETCH_DIM_R                                      ~48     ~47, 'offset_map'
         85        TYPE_CHECK                                  128  ~49     ~48
         86        BOOL_NOT                                         ~50     ~49
         87      > JMPNZ_EX                                         ~50     ~50, ->93
         88    >   FETCH_OBJ_R                                      ~51     'config'
         89        FETCH_DIM_R                                      ~52     ~51, 'offset_map'
         90        COUNT                                            ~53     ~52
         91        BOOL_NOT                                         ~54     ~53
         92        BOOL                                             ~50     ~54
         93    > > JMPZ                                                     ~50, ->96
   55    94    >   INIT_METHOD_CALL                                         'buildOffsetMap'
         95        DO_FCALL                                      0          
   56    96    > > RETURN                                                   null

End of function init

Function resetoffset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/eG1qd
function name:  resetOffset
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   INIT_FCALL                                               'fseek'
          1        FETCH_OBJ_R                                      ~0      'fh'
          2        SEND_VAL                                                 ~0
          3        FETCH_OBJ_R                                      ~1      'offset_diff'
          4        FETCH_OBJ_R                                      ~2      'offset_after_first_file_header'
          5        ADD                                              ~3      ~1, ~2
          6        SEND_VAL                                                 ~3
          7        SEND_VAL                                                 0
          8        DO_ICALL                                                 
   60     9      > RETURN                                                   null

End of function resetoffset

Function buildoffsetmap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 11
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 11
Branch analysis from position: 39
Branch analysis from position: 11
filename:       /in/eG1qd
function name:  buildOffsetMap
number of ops:  40
compiled vars:  !0 = $header_text, !1 = $headers
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   INIT_FCALL                                               'fseek'
          1        FETCH_OBJ_R                                      ~2      'fh'
          2        SEND_VAL                                                 ~2
          3        FETCH_OBJ_R                                      ~3      'offset_diff'
          4        FETCH_OBJ_R                                      ~4      'offset_after_main_header'
          5        ADD                                              ~5      ~3, ~4
          6        SEND_VAL                                                 ~5
          7        SEND_VAL                                                 0
          8        DO_ICALL                                                 
   65     9        ASSIGN                                                   !0, ''
   66    10      > JMP                                                      ->32
   67    11    >   INIT_STATIC_METHOD_CALL                                  'processHeader'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0  $8      
         14        ASSIGN                                                   !1, $8
   69    15        FETCH_DIM_R                                      ~12     !1, 'Content-ID'
         16        INIT_FCALL                                               'ftell'
         17        FETCH_OBJ_R                                      ~14     'fh'
         18        SEND_VAL                                                 ~14
         19        DO_ICALL                                         $15     
         20        FETCH_OBJ_W                                      $10     'config'
         21        FETCH_DIM_W                                      $11     $10, 'offset_map'
         22        ASSIGN_DIM                                               $11, ~12
         23        OP_DATA                                                  $15
   70    24        INIT_STATIC_METHOD_CALL                                  'readNextBodyChunk'
         25        CHECK_FUNC_ARG                                           
         26        FETCH_OBJ_FUNC_ARG                               $16     'fh'
         27        SEND_FUNC_ARG                                            $16
         28        CHECK_FUNC_ARG                                           
         29        FETCH_OBJ_FUNC_ARG                               $17     'mime_boundary'
         30        SEND_FUNC_ARG                                            $17
         31        DO_FCALL                                      0          
   66    32    >   INIT_STATIC_METHOD_CALL                                  'readNextHeaderChunk'
         33        CHECK_FUNC_ARG                                           
         34        FETCH_OBJ_FUNC_ARG                               $19     'fh'
         35        SEND_FUNC_ARG                                            $19
         36        DO_FCALL                                      0  $20     
         37        ASSIGN                                           ~21     !0, $20
         38      > JMPNZ                                                    ~21, ->11
   73    39    > > RETURN                                                   null

End of function buildoffsetmap

Function getfile:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/eG1qd
function name:  getFile
number of ops:  25
compiled vars:  !0 = $filename, !1 = $content_id, !2 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV                                             !0      
   76     1        FETCH_OBJ_R                                      ~3      'config'
          2        FETCH_DIM_R                                      ~4      ~3, 'files'
          3        FETCH_DIM_R                                      ~5      ~4, !0
          4        ASSIGN                                                   !1, ~5
   77     5        FETCH_OBJ_R                                      ~7      'config'
          6        FETCH_DIM_R                                      ~8      ~7, 'offset_map'
          7        FETCH_DIM_R                                      ~9      ~8, !1
          8        ASSIGN                                                   !2, ~9
   78     9        INIT_FCALL                                               'fseek'
         10        FETCH_OBJ_R                                      ~11     'fh'
         11        SEND_VAL                                                 ~11
         12        SEND_VAR                                                 !2
         13        SEND_VAL                                                 0
         14        DO_ICALL                                                 
   79    15        INIT_STATIC_METHOD_CALL                                  'readNextBodyChunk'
         16        CHECK_FUNC_ARG                                           
         17        FETCH_OBJ_FUNC_ARG                               $13     'fh'
         18        SEND_FUNC_ARG                                            $13
         19        CHECK_FUNC_ARG                                           
         20        FETCH_OBJ_FUNC_ARG                               $14     'mime_boundary'
         21        SEND_FUNC_ARG                                            $14
         22        DO_FCALL                                      0  $15     
         23      > RETURN                                                   $15
   80    24*     > RETURN                                                   null

End of function getfile

Function readnextheaderchunk:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/eG1qd
function name:  readNextHeaderChunk
number of ops:  13
compiled vars:  !0 = $fh, !1 = $line
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   RECV                                             !0      
   83     1        INIT_FCALL                                               'stream_get_line'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 9223372036854775807
          4        SEND_VAL                                                 '%0A%0A'
          5        DO_ICALL                                         $2      
          6        ASSIGN                                                   !1, $2
   84     7        TYPE_CHECK                                    4          !1
          8      > JMPZ                                                     ~4, ->10
   85     9    > > RETURN                                                   <false>
   86    10    >   CONCAT                                           ~5      !1, '%0A'
         11      > RETURN                                                   ~5
   87    12*     > RETURN                                                   null

End of function readnextheaderchunk

Function readnextbodychunk:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/eG1qd
function name:  readNextBodyChunk
number of ops:  31
compiled vars:  !0 = $fh, !1 = $boundary, !2 = $line, !3 = $end_of_body_string, !4 = $eobs_length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   90     2        INIT_FCALL                                               'stream_get_line'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 9223372036854775807
          5        CONCAT                                           ~5      '%0A', !1
          6        CONCAT                                           ~6      ~5, '%0A'
          7        SEND_VAL                                                 ~6
          8        DO_ICALL                                         $7      
          9        ASSIGN                                                   !2, $7
   91    10        CONCAT                                           ~9      '%0A', !1
         11        CONCAT                                           ~10     ~9, '--%0A'
         12        ASSIGN                                                   !3, ~10
   92    13        STRLEN                                           ~12     !3
         14        ASSIGN                                                   !4, ~12
   93    15        INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !2
         17        MUL                                              ~14     !4, -1
         18        SEND_VAL                                                 ~14
         19        DO_ICALL                                         $15     
         20        IS_EQUAL                                                 !3, $15
         21      > JMPZ                                                     ~16, ->29
   94    22    >   INIT_FCALL                                               'substr'
         23        SEND_VAR                                                 !2
         24        SEND_VAL                                                 0
         25        MUL                                              ~17     !4, -1
         26        SEND_VAL                                                 ~17
         27        DO_ICALL                                         $18     
         28        ASSIGN                                                   !2, $18
   95    29    > > RETURN                                                   !2
   96    30*     > RETURN                                                   null

End of function readnextbodychunk

Function processheader:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 19
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
Branch analysis from position: 19
filename:       /in/eG1qd
function name:  processHeader
number of ops:  21
compiled vars:  !0 = $header_text, !1 = $headers, !2 = $matches, !3 = $found, !4 = $set
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   RECV                                             !0      
   99     1        ASSIGN                                                   !1, <array>
  100     2        ASSIGN                                                   !2, <array>
  101     3        INIT_FCALL                                               'preg_match_all'
          4        SEND_VAL                                                 '%2F%5E%28.%2B%29%3A+%28%28.%7C%5Cr%5Cn%5Cs%29%2B%29%5Cr%3F%5Cn%2Fm'
          5        SEND_VAR                                                 !0
          6        SEND_REF                                                 !2
          7        SEND_VAL                                                 2
          8        DO_ICALL                                         $7      
          9        ASSIGN                                                   !3, $7
  102    10      > JMPZ                                                     !3, ->19
  103    11    > > FE_RESET_R                                       $9      !2, ->18
         12    > > FE_FETCH_R                                               $9, !4, ->18
  104    13    >   FETCH_DIM_R                                      ~10     !4, 1
         14        FETCH_DIM_R                                      ~12     !4, 2
         15        ASSIGN_DIM                                               !1, ~10
         16        OP_DATA                                                  ~12
  103    17      > JMP                                                      ->12
         18    >   FE_FREE                                                  $9
  106    19    > > RETURN                                                   !1
  107    20*     > RETURN                                                   null

End of function processheader

End of class MimeHalt.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.15 ms | 1424 KiB | 33 Q