3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * Instagram's proxy API * reads Instagram pages and gets embedded data, * then returns it as json or jsonp. * By Francisco Diaz :: picssel.com * Revision March 2015 */ /*$sawOrigin = false; $originHeader = $_SERVER['HTTP_ORIGIN']; $originHeader = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : null; if ($originHeader) { echo "Origin header value: $originHeader"; }*/ /***** get request origin and set domain restrictions *****/ $HTTP_ORIGIN = $_SERVER['HTTP_ORIGIN']; // 1). ALL domains // header("Access-Control-Allow-Origin: *"); // 2). Single domain /* if ($HTTP_ORIGIN == "http://www.picssel.com"){ header("Access-Control-Allow-Origin: $HTTP_ORIGIN"); } */ // 3). Several domains, e.g. http://picssel.com, http://fiddle.jshell.net, etc. $domains_allowed = array("http://www.picssel.com", "http://www.picssel.ca"); if(in_array( $HTTP_ORIGIN, $domains_allowed )){ header("Access-Control-Allow-Origin: $HTTP_ORIGIN"); } /***** functions *****/ // sanitize input function sanitize_input($input){ $input = trim($input); $input = stripslashes($input); $input = strip_tags($input); $input = htmlspecialchars($input); return $input; }; // process data function process_data($dataFile, $requestType){ $data_length = strlen($dataFile); if( $data_length > 0 ){ $start_position = strpos( $dataFile ,'{"static_root"' ); // start position $trimmed_before = trim( substr($dataFile, $start_position) ); // trim preceding content $end_position = strpos( $trimmed_before, '</script>'); // end position $trimmed = trim( substr( $trimmed_before, 0, $end_position) ); // trim content $jsondata = substr( $trimmed, 0, -1); // remove extra trailing ";" header("HTTP/1.0 200 OK"); // JSONP response if(array_key_exists('callback', $_GET)){ header('Content-Type: text/javascript; charset=utf8'); $callback = $_GET['callback']; return $callback."(".$jsondata.");"; } // JSON response else { header('Content-Type: application/json; charset=utf-8'); return $jsondata; } } else { // invalid username or media header("HTTP/1.0 400 BAD REQUEST"); header('Content-Type: text/html; charset=utf-8'); die("invalid $requestType"); } }; /***** Get user's input *****/ $user = sanitize_input( $_GET['user'] ); // expects something like "instagram" (username) $media = sanitize_input( $_GET['media'] ); // expects something like "mOFsFhAp4f" (shortcode) /***** set context *****/ $context = stream_context_create(array( 'http' => array( 'timeout' => 10 // in seconds ) ) ); /***** validate request type and return response *****/ // user, including last 20 media posts if( !empty($user) && empty($media) ){ $requestType = "user"; $dataFile = @ file_get_contents("http://instagram.com/".$user, NULL, $context); echo process_data($dataFile, $requestType); } // media elseif( empty($user) && !empty($media) ){ $requestType = "media"; $dataFile = @ file_get_contents("http://instagram.com/p/".$media, NULL, $context); echo process_data($dataFile, $requestType); } // invalid : two or more parameters were passed elseif( !empty($user) && !empty($media) ){ header("HTTP/1.0 400 BAD REQUEST"); header('Content-Type: text/html; charset=utf-8'); die("only one parameter allowed"); } // invalid : none or invalid parameters were passed elseif( empty($user) && empty($media) ){ header("HTTP/1.0 400 BAD REQUEST"); header('Content-Type: text/html; charset=utf-8'); die("invalid parameters"); }; ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 52
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 101
Branch analysis from position: 101
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 57
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 74
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 101
Branch analysis from position: 101
Branch analysis from position: 74
2 jumps found. (Code = 46) Position 1 = 77, Position 2 = 80
Branch analysis from position: 77
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 89
Branch analysis from position: 81
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 89
2 jumps found. (Code = 46) Position 1 = 91, Position 2 = 93
Branch analysis from position: 91
2 jumps found. (Code = 43) Position 1 = 94, Position 2 = 101
Branch analysis from position: 94
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 101
Branch analysis from position: 93
Branch analysis from position: 80
Branch analysis from position: 57
Branch analysis from position: 35
Branch analysis from position: 14
filename:       /in/oq58j
function name:  (null)
number of ops:  102
compiled vars:  !0 = $HTTP_ORIGIN, !1 = $domains_allowed, !2 = $user, !3 = $media, !4 = $context, !5 = $requestType, !6 = $dataFile
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   FETCH_R                      global              ~7      '_SERVER'
          1        FETCH_DIM_R                                      ~8      ~7, 'HTTP_ORIGIN'
          2        ASSIGN                                                   !0, ~8
   28     3        ASSIGN                                                   !1, <array>
   29     4        INIT_FCALL                                               'in_array'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $11     
          8      > JMPZ                                                     $11, ->14
   30     9    >   INIT_FCALL                                               'header'
         10        NOP                                                      
         11        FAST_CONCAT                                      ~12     'Access-Control-Allow-Origin%3A+', !0
         12        SEND_VAL                                                 ~12
         13        DO_ICALL                                                 
   72    14    >   INIT_FCALL                                               'sanitize_input'
         15        FETCH_R                      global              ~14     '_GET'
         16        FETCH_DIM_R                                      ~15     ~14, 'user'
         17        SEND_VAL                                                 ~15
         18        DO_FCALL                                      0  $16     
         19        ASSIGN                                                   !2, $16
   73    20        INIT_FCALL                                               'sanitize_input'
         21        FETCH_R                      global              ~18     '_GET'
         22        FETCH_DIM_R                                      ~19     ~18, 'media'
         23        SEND_VAL                                                 ~19
         24        DO_FCALL                                      0  $20     
         25        ASSIGN                                                   !3, $20
   76    26        INIT_FCALL                                               'stream_context_create'
   78    27        SEND_VAL                                                 <array>
         28        DO_ICALL                                         $22     
   76    29        ASSIGN                                                   !4, $22
   85    30        ISSET_ISEMPTY_CV                                 ~24     !2
         31        BOOL_NOT                                         ~25     ~24
         32      > JMPZ_EX                                          ~25     ~25, ->35
         33    >   ISSET_ISEMPTY_CV                                 ~26     !3
         34        BOOL                                             ~25     ~26
         35    > > JMPZ                                                     ~25, ->52
   86    36    >   ASSIGN                                                   !5, 'user'
   87    37        BEGIN_SILENCE                                    ~28     
         38        INIT_FCALL                                               'file_get_contents'
         39        CONCAT                                           ~29     'http%3A%2F%2Finstagram.com%2F', !2
         40        SEND_VAL                                                 ~29
         41        SEND_VAL                                                 null
         42        SEND_VAR                                                 !4
         43        DO_ICALL                                         $30     
         44        END_SILENCE                                              ~28
         45        ASSIGN                                                   !6, $30
   88    46        INIT_FCALL                                               'process_data'
         47        SEND_VAR                                                 !6
         48        SEND_VAR                                                 !5
         49        DO_FCALL                                      0  $32     
         50        ECHO                                                     $32
         51      > JMP                                                      ->101
   91    52    >   ISSET_ISEMPTY_CV                                 ~33     !2
         53      > JMPZ_EX                                          ~33     ~33, ->57
         54    >   ISSET_ISEMPTY_CV                                 ~34     !3
         55        BOOL_NOT                                         ~35     ~34
         56        BOOL                                             ~33     ~35
         57    > > JMPZ                                                     ~33, ->74
   92    58    >   ASSIGN                                                   !5, 'media'
   93    59        BEGIN_SILENCE                                    ~37     
         60        INIT_FCALL                                               'file_get_contents'
         61        CONCAT                                           ~38     'http%3A%2F%2Finstagram.com%2Fp%2F', !3
         62        SEND_VAL                                                 ~38
         63        SEND_VAL                                                 null
         64        SEND_VAR                                                 !4
         65        DO_ICALL                                         $39     
         66        END_SILENCE                                              ~37
         67        ASSIGN                                                   !6, $39
   94    68        INIT_FCALL                                               'process_data'
         69        SEND_VAR                                                 !6
         70        SEND_VAR                                                 !5
         71        DO_FCALL                                      0  $41     
         72        ECHO                                                     $41
         73      > JMP                                                      ->101
   97    74    >   ISSET_ISEMPTY_CV                                 ~42     !2
         75        BOOL_NOT                                         ~43     ~42
         76      > JMPZ_EX                                          ~43     ~43, ->80
         77    >   ISSET_ISEMPTY_CV                                 ~44     !3
         78        BOOL_NOT                                         ~45     ~44
         79        BOOL                                             ~43     ~45
         80    > > JMPZ                                                     ~43, ->89
   98    81    >   INIT_FCALL                                               'header'
         82        SEND_VAL                                                 'HTTP%2F1.0+400+BAD+REQUEST'
         83        DO_ICALL                                                 
   99    84        INIT_FCALL                                               'header'
         85        SEND_VAL                                                 'Content-Type%3A+text%2Fhtml%3B+charset%3Dutf-8'
         86        DO_ICALL                                                 
  100    87      > EXIT                                                     'only+one+parameter+allowed'
         88*       JMP                                                      ->101
  103    89    >   ISSET_ISEMPTY_CV                                 ~48     !2
         90      > JMPZ_EX                                          ~48     ~48, ->93
         91    >   ISSET_ISEMPTY_CV                                 ~49     !3
         92        BOOL                                             ~48     ~49
         93    > > JMPZ                                                     ~48, ->101
  104    94    >   INIT_FCALL                                               'header'
         95        SEND_VAL                                                 'HTTP%2F1.0+400+BAD+REQUEST'
         96        DO_ICALL                                                 
  105    97        INIT_FCALL                                               'header'
         98        SEND_VAL                                                 'Content-Type%3A+text%2Fhtml%3B+charset%3Dutf-8'
         99        DO_ICALL                                                 
  106   100      > EXIT                                                     'invalid+parameters'
  109   101    > > RETURN                                                   1

Function sanitize_input:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oq58j
function name:  sanitize_input
number of ops:  19
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   36     1        INIT_FCALL                                               'trim'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        ASSIGN                                                   !0, $1
   37     5        INIT_FCALL                                               'stripslashes'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !0, $3
   38     9        INIT_FCALL                                               'strip_tags'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $5      
         12        ASSIGN                                                   !0, $5
   39    13        INIT_FCALL                                               'htmlspecialchars'
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $7      
         16        ASSIGN                                                   !0, $7
   40    17      > RETURN                                                   !0
   41    18*     > RETURN                                                   null

End of function sanitize_input

Function process_data:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 61
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 56
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
1 jumps found. (Code = 79) Position 1 = -2
filename:       /in/oq58j
function name:  process_data
number of ops:  71
compiled vars:  !0 = $dataFile, !1 = $requestType, !2 = $data_length, !3 = $start_position, !4 = $trimmed_before, !5 = $end_position, !6 = $trimmed, !7 = $jsondata, !8 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   44     2        STRLEN                                           ~9      !0
          3        ASSIGN                                                   !2, ~9
   45     4        IS_SMALLER                                               0, !2
          5      > JMPZ                                                     ~11, ->61
   46     6    >   INIT_FCALL                                               'strpos'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 '%7B%22static_root%22'
          9        DO_ICALL                                         $12     
         10        ASSIGN                                                   !3, $12
   47    11        INIT_FCALL                                               'trim'
         12        INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !3
         15        DO_ICALL                                         $14     
         16        SEND_VAR                                                 $14
         17        DO_ICALL                                         $15     
         18        ASSIGN                                                   !4, $15
   48    19        INIT_FCALL                                               'strpos'
         20        SEND_VAR                                                 !4
         21        SEND_VAL                                                 '%3C%2Fscript%3E'
         22        DO_ICALL                                         $17     
         23        ASSIGN                                                   !5, $17
   49    24        INIT_FCALL                                               'trim'
         25        INIT_FCALL                                               'substr'
         26        SEND_VAR                                                 !4
         27        SEND_VAL                                                 0
         28        SEND_VAR                                                 !5
         29        DO_ICALL                                         $19     
         30        SEND_VAR                                                 $19
         31        DO_ICALL                                         $20     
         32        ASSIGN                                                   !6, $20
   50    33        INIT_FCALL                                               'substr'
         34        SEND_VAR                                                 !6
         35        SEND_VAL                                                 0
         36        SEND_VAL                                                 -1
         37        DO_ICALL                                         $22     
         38        ASSIGN                                                   !7, $22
   51    39        INIT_FCALL                                               'header'
         40        SEND_VAL                                                 'HTTP%2F1.0+200+OK'
         41        DO_ICALL                                                 
   53    42        FETCH_R                      global              ~25     '_GET'
         43        ARRAY_KEY_EXISTS                                         'callback', ~25
         44      > JMPZ                                                     ~26, ->56
   54    45    >   INIT_FCALL                                               'header'
         46        SEND_VAL                                                 'Content-Type%3A+text%2Fjavascript%3B+charset%3Dutf8'
         47        DO_ICALL                                                 
   55    48        FETCH_R                      global              ~28     '_GET'
         49        FETCH_DIM_R                                      ~29     ~28, 'callback'
         50        ASSIGN                                                   !8, ~29
   56    51        CONCAT                                           ~31     !8, '%28'
         52        CONCAT                                           ~32     ~31, !7
         53        CONCAT                                           ~33     ~32, '%29%3B'
         54      > RETURN                                                   ~33
         55*       JMP                                                      ->60
   60    56    >   INIT_FCALL                                               'header'
         57        SEND_VAL                                                 'Content-Type%3A+application%2Fjson%3B+charset%3Dutf-8'
         58        DO_ICALL                                                 
   61    59      > RETURN                                                   !7
         60*       JMP                                                      ->70
   65    61    >   INIT_FCALL                                               'header'
         62        SEND_VAL                                                 'HTTP%2F1.0+400+BAD+REQUEST'
         63        DO_ICALL                                                 
   66    64        INIT_FCALL                                               'header'
         65        SEND_VAL                                                 'Content-Type%3A+text%2Fhtml%3B+charset%3Dutf-8'
         66        DO_ICALL                                                 
   67    67        NOP                                                      
         68        FAST_CONCAT                                      ~37     'invalid+', !1
         69      > EXIT                                                     ~37
   69    70*     > RETURN                                                   null

End of function process_data

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
190.89 ms | 1415 KiB | 37 Q