3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ContentSecurityPolicy { const DEFAULT_SRC = 'default-src'; const SCRIPT_SRC = 'script-src'; const OBJECT_SRC = 'object-src'; const STYLE_SRC = 'style-src'; const IMG_SRC = 'img-src'; const MEDIA_SRC = 'media-src'; const FRAME_SRC = 'frame-src'; const FONT_SRC = 'font-src'; const CONNECT_SRC = 'connect-src'; const SOURCE_NONE = "'none'"; const SOURCE_SELF = "'self'"; const SOURCE_UNSAFE_INLINE = "'unsafe-inline'"; const SOURCE_UNSAFE_EVAL = "'unsafe-eval'"; private $policy; public function __construct() { $this->policy = array(); $this->policy[self::DEFAULT_SRC] = array(); $this->policy[self::SCRIPT_SRC] = array(); $this->policy[self::OBJECT_SRC] = array(); $this->policy[self::STYLE_SRC] = array(); $this->policy[self::IMG_SRC] = array(); $this->policy[self::MEDIA_SRC] = array(); $this->policy[self::FRAME_SRC] = array(); $this->policy[self::FONT_SRC] = array(); $this->policy[self::CONNECT_SRC] = array(); $this->setPolicySourceDirectives(); } private function setPolicySourceDirectives(){ $refl = new ReflectionClass(__CLASS__); $srcPattern = '/.+_SRC$/'; foreach ($refl->getConstants() as $constant => $value){ if (preg_match($srcPattern, $constant)){ print self::${$constant} . "\n"; } } exit; } private function copy() { $retval = new ContentSecurityPolicy(); foreach ($this->policy as $directive => $sources) { foreach ($sources as $source) { array_push($retval->policy[$directive], $source); } } return $retval; } function addSource($directive, $source) { if (!isset($this->policy[$directive])) { throw new CSPException("Invalid directive"); } $this->policy[$directive][] = $source; return $this; } function toString() { $retval = array(); foreach ($this->policy as $directive => $sources) { if (sizeof($sources) > 0) { $retval[] = join(' ', [$directive, join(' ', $sources)]); } } return join('; ', $retval); } } class CSPException extends \Exception {} $CSP = new ContentSecurityPolicy(); $CSP->addSource(ContentSecurityPolicy::DEFAULT_SRC, ContentSecurityPolicy::SOURCE_SELF) ->addSource(ContentSecurityPolicy::SCRIPT_SRC, ContentSecurityPolicy::SOURCE_SELF) ->addSource(ContentSecurityPolicy::SCRIPT_SRC, 'http://code.jquery.com') ->addSource(ContentSecurityPolicy::STYLE_SRC, ContentSecurityPolicy::SOURCE_SELF) ->addSource(ContentSecurityPolicy::STYLE_SRC, 'https://bootstrapcdn.com') ->addSource(ContentSecurityPolicy::FONT_SRC, 'https://fonts.googleapis.com'); echo $CSP->toString(); exit("\nDone!\n");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 79) Position 1 = -2
filename:       /in/Z3HGq
function name:  (null)
number of ops:  32
compiled vars:  !0 = $CSP
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   NEW                                              $1      'ContentSecurityPolicy'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   79     3        INIT_METHOD_CALL                                         !0, 'addSource'
          4        SEND_VAL_EX                                              'default-src'
          5        SEND_VAL_EX                                              '%27self%27'
          6        DO_FCALL                                      0  $4      
          7        INIT_METHOD_CALL                                         $4, 'addSource'
          8        SEND_VAL_EX                                              'script-src'
          9        SEND_VAL_EX                                              '%27self%27'
         10        DO_FCALL                                      0  $5      
   80    11        INIT_METHOD_CALL                                         $5, 'addSource'
         12        SEND_VAL_EX                                              'script-src'
         13        SEND_VAL_EX                                              'http%3A%2F%2Fcode.jquery.com'
         14        DO_FCALL                                      0  $6      
   81    15        INIT_METHOD_CALL                                         $6, 'addSource'
         16        SEND_VAL_EX                                              'style-src'
         17        SEND_VAL_EX                                              '%27self%27'
         18        DO_FCALL                                      0  $7      
   82    19        INIT_METHOD_CALL                                         $7, 'addSource'
         20        SEND_VAL_EX                                              'style-src'
         21        SEND_VAL_EX                                              'https%3A%2F%2Fbootstrapcdn.com'
         22        DO_FCALL                                      0  $8      
   83    23        INIT_METHOD_CALL                                         $8, 'addSource'
         24        SEND_VAL_EX                                              'font-src'
         25        SEND_VAL_EX                                              'https%3A%2F%2Ffonts.googleapis.com'
         26        DO_FCALL                                      0          
   84    27        INIT_METHOD_CALL                                         !0, 'toString'
         28        DO_FCALL                                      0  $10     
         29        ECHO                                                     $10
   86    30      > EXIT                                                     '%0ADone%21%0A'
         31*     > RETURN                                                   1

Class ContentSecurityPolicy:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z3HGq
function name:  __construct
number of ops:  32
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   ASSIGN_OBJ                                               'policy'
          1        OP_DATA                                                  <array>
   21     2        FETCH_OBJ_W                                      $1      'policy'
          3        ASSIGN_DIM                                               $1, 'default-src'
          4        OP_DATA                                                  <array>
   22     5        FETCH_OBJ_W                                      $3      'policy'
          6        ASSIGN_DIM                                               $3, 'script-src'
          7        OP_DATA                                                  <array>
   23     8        FETCH_OBJ_W                                      $5      'policy'
          9        ASSIGN_DIM                                               $5, 'object-src'
         10        OP_DATA                                                  <array>
   24    11        FETCH_OBJ_W                                      $7      'policy'
         12        ASSIGN_DIM                                               $7, 'style-src'
         13        OP_DATA                                                  <array>
   25    14        FETCH_OBJ_W                                      $9      'policy'
         15        ASSIGN_DIM                                               $9, 'img-src'
         16        OP_DATA                                                  <array>
   26    17        FETCH_OBJ_W                                      $11     'policy'
         18        ASSIGN_DIM                                               $11, 'media-src'
         19        OP_DATA                                                  <array>
   27    20        FETCH_OBJ_W                                      $13     'policy'
         21        ASSIGN_DIM                                               $13, 'frame-src'
         22        OP_DATA                                                  <array>
   28    23        FETCH_OBJ_W                                      $15     'policy'
         24        ASSIGN_DIM                                               $15, 'font-src'
         25        OP_DATA                                                  <array>
   29    26        FETCH_OBJ_W                                      $17     'policy'
         27        ASSIGN_DIM                                               $17, 'connect-src'
         28        OP_DATA                                                  <array>
   30    29        INIT_METHOD_CALL                                         'setPolicySourceDirectives'
         30        DO_FCALL                                      0          
   32    31      > RETURN                                                   null

End of function __construct

Function setpolicysourcedirectives:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 19
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 18
Branch analysis from position: 19
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 19
filename:       /in/Z3HGq
function name:  setPolicySourceDirectives
number of ops:  22
compiled vars:  !0 = $refl, !1 = $srcPattern, !2 = $value, !3 = $constant
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   NEW                                              $4      'ReflectionClass'
          1        SEND_VAL_EX                                              'ContentSecurityPolicy'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   37     4        ASSIGN                                                   !1, '%2F.%2B_SRC%24%2F'
   38     5        INIT_METHOD_CALL                                         !0, 'getConstants'
          6        DO_FCALL                                      0  $8      
          7      > FE_RESET_R                                       $9      $8, ->19
          8    > > FE_FETCH_R                                       ~10     $9, !2, ->19
          9    >   ASSIGN                                                   !3, ~10
   39    10        INIT_FCALL                                               'preg_match'
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $12     
         14      > JMPZ                                                     $12, ->18
   40    15    >   FETCH_STATIC_PROP_R          unknown             ~13     !3
         16        CONCAT                                           ~14     ~13, '%0A'
         17        ECHO                                                     ~14
   38    18    > > JMP                                                      ->8
         19    >   FE_FREE                                                  $9
   43    20      > EXIT                                                     
   44    21*     > RETURN                                                   null

End of function setpolicysourcedirectives

Function copy:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 18
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 18
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/Z3HGq
function name:  copy
number of ops:  21
compiled vars:  !0 = $retval, !1 = $sources, !2 = $directive, !3 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $4      'ContentSecurityPolicy'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $4
   48     3        FETCH_OBJ_R                                      ~7      'policy'
          4      > FE_RESET_R                                       $8      ~7, ->18
          5    > > FE_FETCH_R                                       ~9      $8, !1, ->18
          6    >   ASSIGN                                                   !2, ~9
   49     7      > FE_RESET_R                                       $11     !1, ->16
          8    > > FE_FETCH_R                                               $11, !3, ->16
   50     9    >   INIT_FCALL                                               'array_push'
         10        FETCH_OBJ_W                                      $12     !0, 'policy'
         11        FETCH_DIM_W                                      $13     $12, !2
         12        SEND_REF                                                 $13
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                                 
   49    15      > JMP                                                      ->8
         16    >   FE_FREE                                                  $11
   48    17      > JMP                                                      ->5
         18    >   FE_FREE                                                  $8
   54    19      > RETURN                                                   !0
   55    20*     > RETURN                                                   null

End of function copy

Function addsource:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z3HGq
function name:  addSource
number of ops:  17
compiled vars:  !0 = $directive, !1 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   58     2        FETCH_OBJ_IS                                     ~2      'policy'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
   59     6    >   NEW                                              $5      'CSPException'
          7        SEND_VAL_EX                                              'Invalid+directive'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $5
   61    10    >   FETCH_OBJ_W                                      $7      'policy'
         11        FETCH_DIM_W                                      $8      $7, !0
         12        ASSIGN_DIM                                               $8
         13        OP_DATA                                                  !1
   62    14        FETCH_THIS                                       ~10     
         15      > RETURN                                                   ~10
   63    16*     > RETURN                                                   null

End of function addsource

Function tostring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 21
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 20
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/Z3HGq
function name:  toString
number of ops:  28
compiled vars:  !0 = $retval, !1 = $sources, !2 = $directive
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   ASSIGN                                                   !0, <array>
   67     1        FETCH_OBJ_R                                      ~4      'policy'
          2      > FE_RESET_R                                       $5      ~4, ->21
          3    > > FE_FETCH_R                                       ~6      $5, !1, ->21
          4    >   ASSIGN                                                   !2, ~6
   68     5        COUNT                                            ~8      !1
          6        IS_SMALLER                                               0, ~8
          7      > JMPZ                                                     ~9, ->20
   69     8    >   INIT_FCALL                                               'join'
          9        SEND_VAL                                                 '+'
         10        INIT_ARRAY                                       ~11     !2
         11        INIT_FCALL                                               'join'
         12        SEND_VAL                                                 '+'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $12     
         15        ADD_ARRAY_ELEMENT                                ~11     $12
         16        SEND_VAL                                                 ~11
         17        DO_ICALL                                         $13     
         18        ASSIGN_DIM                                               !0
         19        OP_DATA                                                  $13
   67    20    > > JMP                                                      ->3
         21    >   FE_FREE                                                  $5
   72    22        INIT_FCALL                                               'join'
         23        SEND_VAL                                                 '%3B+'
         24        SEND_VAR                                                 !0
         25        DO_ICALL                                         $14     
         26      > RETURN                                                   $14
   73    27*     > RETURN                                                   null

End of function tostring

End of class ContentSecurityPolicy.

Class CSPException: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.35 ms | 1412 KiB | 19 Q