3v4l.org

run code in 300+ PHP versions simultaneously
#!/usr/bin/php <?php // input parameters $api_key = "YOUR_API_KEY"; // default arguments $backup_tag = "daily"; $num_of_backups = 3; // sunday = weekly, 1st day of month = monthly if(intval(date('w')) == 0) $backup_tag = "weekly"; if(intval( date('d')) == 1) $backup_tag = "monthly"; $api_addr = "https://api.vultr.com/v1/"; $curl = "curl -s -H 'API-Key: " . $api_key . "' " . $api_addr; $host = trim(shell_exec("hostname")); $ip = trim(shell_exec("hostname -I")); // find a subid that matches the current ip $json = shell_exec( $curl . "server/list" ); $obj = json_decode($json,true); $subid = 0; foreach ($obj as $k => $v) { if( $v['main_ip'] == $ip ){ $subid = $k; break; } } if($subid == 0){ echo "Couldn't find a Vultr instance with ip " . $ip; exit(1); } // find old snapshot(s) to remove $json = shell_exec( $curl . "snapshot/list" ); $obj = json_decode($json,true); // filter non-backup snapshots $obj = array_filter($obj, function ($x){ return strpos($x['description'], $GLOBALS['ip'] . "-" . $GLOBALS['backup_tag']) !== false; }); // reverse sort by creation time usort($obj, function($x, $y){ return $y['date_created'] <=> $x['date_created']; }); // delete oldest snapshot(s) for($i=0;$i<count($obj);$i++){ if($i >= $num_of_backups-1){ $id = $obj[$i]['SNAPSHOTID']; $cmd = $curl . "snapshot/destroy"; shell_exec( $cmd . " --data 'SNAPSHOTID=" . $id . "'" ); } } // create new snapshot $cmd = $curl . "snapshot/create"; $cmd .= " --data 'SUBID=" . $subid . "'"; $cmd .= " --data 'description=" . $host . "-" . $ip . "-" . $backup_tag . "'"; shell_exec( $cmd ); ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 49, Position 2 = 57
Branch analysis from position: 49
2 jumps found. (Code = 78) Position 1 = 50, Position 2 = 57
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 63
Branch analysis from position: 60
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 101
Branch analysis from position: 101
2 jumps found. (Code = 44) Position 1 = 104, Position 2 = 86
Branch analysis from position: 104
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 86
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 100
Branch analysis from position: 89
2 jumps found. (Code = 44) Position 1 = 104, Position 2 = 86
Branch analysis from position: 104
Branch analysis from position: 86
Branch analysis from position: 100
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
Branch analysis from position: 57
Branch analysis from position: 57
Branch analysis from position: 18
Branch analysis from position: 11
filename:       /in/tI4uO
function name:  (null)
number of ops:  120
compiled vars:  !0 = $api_key, !1 = $backup_tag, !2 = $num_of_backups, !3 = $api_addr, !4 = $curl, !5 = $host, !6 = $ip, !7 = $json, !8 = $obj, !9 = $subid, !10 = $v, !11 = $k, !12 = $i, !13 = $id, !14 = $cmd
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    1     0  E >   ECHO                                                     '%0A'
    6     1        ASSIGN                                                   !0, 'YOUR_API_KEY'
    9     2        ASSIGN                                                   !1, 'daily'
   10     3        ASSIGN                                                   !2, 3
   13     4        INIT_FCALL                                               'date'
          5        SEND_VAL                                                 'w'
          6        DO_ICALL                                         $18     
          7        CAST                                          4  ~19     $18
          8        IS_EQUAL                                                 ~19, 0
          9      > JMPZ                                                     ~20, ->11
         10    >   ASSIGN                                                   !1, 'weekly'
   14    11    >   INIT_FCALL                                               'date'
         12        SEND_VAL                                                 'd'
         13        DO_ICALL                                         $22     
         14        CAST                                          4  ~23     $22
         15        IS_EQUAL                                                 ~23, 1
         16      > JMPZ                                                     ~24, ->18
         17    >   ASSIGN                                                   !1, 'monthly'
   16    18    >   ASSIGN                                                   !3, 'https%3A%2F%2Fapi.vultr.com%2Fv1%2F'
   17    19        CONCAT                                           ~27     'curl+-s+-H+%27API-Key%3A+', !0
         20        CONCAT                                           ~28     ~27, '%27+'
         21        CONCAT                                           ~29     ~28, !3
         22        ASSIGN                                                   !4, ~29
   18    23        INIT_FCALL                                               'trim'
         24        INIT_FCALL                                               'shell_exec'
         25        SEND_VAL                                                 'hostname'
         26        DO_ICALL                                         $31     
         27        SEND_VAR                                                 $31
         28        DO_ICALL                                         $32     
         29        ASSIGN                                                   !5, $32
   19    30        INIT_FCALL                                               'trim'
         31        INIT_FCALL                                               'shell_exec'
         32        SEND_VAL                                                 'hostname+-I'
         33        DO_ICALL                                         $34     
         34        SEND_VAR                                                 $34
         35        DO_ICALL                                         $35     
         36        ASSIGN                                                   !6, $35
   22    37        INIT_FCALL                                               'shell_exec'
         38        CONCAT                                           ~37     !4, 'server%2Flist'
         39        SEND_VAL                                                 ~37
         40        DO_ICALL                                         $38     
         41        ASSIGN                                                   !7, $38
   23    42        INIT_FCALL                                               'json_decode'
         43        SEND_VAR                                                 !7
         44        SEND_VAL                                                 <true>
         45        DO_ICALL                                         $40     
         46        ASSIGN                                                   !8, $40
   25    47        ASSIGN                                                   !9, 0
   26    48      > FE_RESET_R                                       $43     !8, ->57
         49    > > FE_FETCH_R                                       ~44     $43, !10, ->57
         50    >   ASSIGN                                                   !11, ~44
   27    51        FETCH_DIM_R                                      ~46     !10, 'main_ip'
         52        IS_EQUAL                                                 !6, ~46
         53      > JMPZ                                                     ~47, ->56
   28    54    >   ASSIGN                                                   !9, !11
   29    55      > JMP                                                      ->57
   26    56    > > JMP                                                      ->49
         57    >   FE_FREE                                                  $43
   33    58        IS_EQUAL                                                 !9, 0
         59      > JMPZ                                                     ~49, ->63
   34    60    >   CONCAT                                           ~50     'Couldn%27t+find+a+Vultr+instance+with+ip+', !6
         61        ECHO                                                     ~50
   35    62      > EXIT                                                     1
   39    63    >   INIT_FCALL                                               'shell_exec'
         64        CONCAT                                           ~51     !4, 'snapshot%2Flist'
         65        SEND_VAL                                                 ~51
         66        DO_ICALL                                         $52     
         67        ASSIGN                                                   !7, $52
   40    68        INIT_FCALL                                               'json_decode'
         69        SEND_VAR                                                 !7
         70        SEND_VAL                                                 <true>
         71        DO_ICALL                                         $54     
         72        ASSIGN                                                   !8, $54
   43    73        INIT_FCALL                                               'array_filter'
         74        SEND_VAR                                                 !8
         75        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FtI4uO%3A43%240'
   45    76        SEND_VAL                                                 ~56
         77        DO_ICALL                                         $57     
   43    78        ASSIGN                                                   !8, $57
   48    79        INIT_FCALL                                               'usort'
         80        SEND_REF                                                 !8
         81        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FtI4uO%3A48%241'
   50    82        SEND_VAL                                                 ~59
         83        DO_ICALL                                                 
   53    84        ASSIGN                                                   !12, 0
         85      > JMP                                                      ->101
   54    86    >   SUB                                              ~62     !2, 1
         87        IS_SMALLER_OR_EQUAL                                      ~62, !12
         88      > JMPZ                                                     ~63, ->100
   55    89    >   FETCH_DIM_R                                      ~64     !8, !12
         90        FETCH_DIM_R                                      ~65     ~64, 'SNAPSHOTID'
         91        ASSIGN                                                   !13, ~65
   56    92        CONCAT                                           ~67     !4, 'snapshot%2Fdestroy'
         93        ASSIGN                                                   !14, ~67
   57    94        INIT_FCALL                                               'shell_exec'
         95        CONCAT                                           ~69     !14, '+--data+%27SNAPSHOTID%3D'
         96        CONCAT                                           ~70     ~69, !13
         97        CONCAT                                           ~71     ~70, '%27'
         98        SEND_VAL                                                 ~71
         99        DO_ICALL                                                 
   53   100    >   PRE_INC                                                  !12
        101    >   COUNT                                            ~74     !8
        102        IS_SMALLER                                               !12, ~74
        103      > JMPNZ                                                    ~75, ->86
   62   104    >   CONCAT                                           ~76     !4, 'snapshot%2Fcreate'
        105        ASSIGN                                                   !14, ~76
   63   106        CONCAT                                           ~78     '+--data+%27SUBID%3D', !9
        107        CONCAT                                           ~79     ~78, '%27'
        108        ASSIGN_OP                                     8          !14, ~79
   64   109        CONCAT                                           ~81     '+--data+%27description%3D', !5
        110        CONCAT                                           ~82     ~81, '-'
        111        CONCAT                                           ~83     ~82, !6
        112        CONCAT                                           ~84     ~83, '-'
        113        CONCAT                                           ~85     ~84, !1
        114        CONCAT                                           ~86     ~85, '%27'
        115        ASSIGN_OP                                     8          !14, ~86
   65   116        INIT_FCALL                                               'shell_exec'
        117        SEND_VAR                                                 !14
        118        DO_ICALL                                                 
   67   119      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FtI4uO%3A43%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tI4uO
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   44     1        INIT_FCALL                                               'strpos'
          2        FETCH_DIM_R                                      ~1      !0, 'description'
          3        SEND_VAL                                                 ~1
          4        FETCH_R                      global              ~2      'GLOBALS'
          5        FETCH_DIM_R                                      ~3      ~2, 'ip'
          6        CONCAT                                           ~4      ~3, '-'
          7        FETCH_R                      global              ~5      'GLOBALS'
          8        FETCH_DIM_R                                      ~6      ~5, 'backup_tag'
          9        CONCAT                                           ~7      ~4, ~6
         10        SEND_VAL                                                 ~7
         11        DO_ICALL                                         $8      
         12        TYPE_CHECK                                  1018  ~9      $8
         13      > RETURN                                                   ~9
   45    14*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FtI4uO%3A43%240

Function %00%7Bclosure%7D%2Fin%2FtI4uO%3A48%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tI4uO
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $x, !1 = $y
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   49     2        FETCH_DIM_R                                      ~2      !1, 'date_created'
          3        FETCH_DIM_R                                      ~3      !0, 'date_created'
          4        SPACESHIP                                        ~4      ~2, ~3
          5      > RETURN                                                   ~4
   50     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FtI4uO%3A48%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.56 ms | 1408 KiB | 27 Q