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->setPolicySourceDirectives(); } private function setPolicySourceDirectives(){ $refl = new ReflectionClass(__CLASS__); $srcPattern = '/.+_SRC$/'; foreach ($refl->getConstants() as $constant => $value){ if (preg_match($srcPattern, $constant)){ $this->policy[constant(__CLASS__.'::'.$constant)] = array(); } } } 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; } public function __toString(){ $retval = array(); foreach ($this->policy as $directive => $sources) { if (sizeof($sources) > 0) { $retval[] = join(' ', array($directive, join(' ', $sources))); } } return 'Content-Security-Policy: ' . 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/2JGM7
function name:  (null)
number of ops:  42
compiled vars:  !0 = $CSP
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   DECLARE_CLASS                                            'contentsecuritypolicy'
   69     1        NEW                                              $1      'ContentSecurityPolicy'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   70     4        INIT_METHOD_CALL                                         !0, 'addSource'
          5        FETCH_CLASS_CONSTANT                             ~4      'ContentSecurityPolicy', 'DEFAULT_SRC'
          6        SEND_VAL_EX                                              ~4
          7        FETCH_CLASS_CONSTANT                             ~5      'ContentSecurityPolicy', 'SOURCE_SELF'
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0  $6      
         10        INIT_METHOD_CALL                                         $6, 'addSource'
         11        FETCH_CLASS_CONSTANT                             ~7      'ContentSecurityPolicy', 'SCRIPT_SRC'
         12        SEND_VAL_EX                                              ~7
         13        FETCH_CLASS_CONSTANT                             ~8      'ContentSecurityPolicy', 'SOURCE_SELF'
         14        SEND_VAL_EX                                              ~8
         15        DO_FCALL                                      0  $9      
   71    16        INIT_METHOD_CALL                                         $9, 'addSource'
         17        FETCH_CLASS_CONSTANT                             ~10     'ContentSecurityPolicy', 'SCRIPT_SRC'
         18        SEND_VAL_EX                                              ~10
         19        SEND_VAL_EX                                              'http%3A%2F%2Fcode.jquery.com'
         20        DO_FCALL                                      0  $11     
   72    21        INIT_METHOD_CALL                                         $11, 'addSource'
         22        FETCH_CLASS_CONSTANT                             ~12     'ContentSecurityPolicy', 'STYLE_SRC'
         23        SEND_VAL_EX                                              ~12
         24        FETCH_CLASS_CONSTANT                             ~13     'ContentSecurityPolicy', 'SOURCE_SELF'
         25        SEND_VAL_EX                                              ~13
         26        DO_FCALL                                      0  $14     
   73    27        INIT_METHOD_CALL                                         $14, 'addSource'
         28        FETCH_CLASS_CONSTANT                             ~15     'ContentSecurityPolicy', 'STYLE_SRC'
         29        SEND_VAL_EX                                              ~15
         30        SEND_VAL_EX                                              'https%3A%2F%2Fbootstrapcdn.com'
         31        DO_FCALL                                      0  $16     
   74    32        INIT_METHOD_CALL                                         $16, 'addSource'
         33        FETCH_CLASS_CONSTANT                             ~17     'ContentSecurityPolicy', 'FONT_SRC'
         34        SEND_VAL_EX                                              ~17
         35        SEND_VAL_EX                                              'https%3A%2F%2Ffonts.googleapis.com'
         36        DO_FCALL                                      0          
   75    37        INIT_METHOD_CALL                                         !0, 'toString'
         38        DO_FCALL                                      0  $19     
         39        ECHO                                                     $19
   77    40      > EXIT                                                     '%0ADone%21%0A'
         41*     > RETURN                                                   1

Class ContentSecurityPolicy:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2JGM7
function name:  __construct
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   ASSIGN_OBJ                                               'policy'
          1        OP_DATA                                                  <array>
   21     2        INIT_METHOD_CALL                                         'setPolicySourceDirectives'
          3        DO_FCALL                                      0          
   22     4      > 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 = 23
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 23
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 22
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 22
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/2JGM7
function name:  setPolicySourceDirectives
number of ops:  25
compiled vars:  !0 = $refl, !1 = $srcPattern, !2 = $value, !3 = $constant
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   NEW                                              $4      'ReflectionClass'
          1        SEND_VAL_EX                                              'ContentSecurityPolicy'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   27     4        ASSIGN                                                   !1, '%2F.%2B_SRC%24%2F'
   28     5        INIT_METHOD_CALL                                         !0, 'getConstants'
          6        DO_FCALL                                      0  $8      
          7      > FE_RESET_R                                       $9      $8, ->23
          8    > > FE_FETCH_R                                       ~10     $9, !2, ->23
          9    >   ASSIGN                                                   !3, ~10
   29    10        INIT_FCALL                                               'preg_match'
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $12     
         14      > JMPZ                                                     $12, ->22
   30    15    >   INIT_FCALL                                               'constant'
         16        CONCAT                                           ~14     'ContentSecurityPolicy%3A%3A', !3
         17        SEND_VAL                                                 ~14
         18        DO_ICALL                                         $15     
         19        FETCH_OBJ_W                                      $13     'policy'
         20        ASSIGN_DIM                                               $13, $15
         21        OP_DATA                                                  <array>
   28    22    > > JMP                                                      ->8
         23    >   FE_FREE                                                  $9
   33    24      > 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/2JGM7
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
-------------------------------------------------------------------------------------
   36     0  E >   NEW                                              $4      'ContentSecurityPolicy'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $4
   37     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
   38     7      > FE_RESET_R                                       $11     !1, ->16
          8    > > FE_FETCH_R                                               $11, !3, ->16
   39     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                                                 
   38    15      > JMP                                                      ->8
         16    >   FE_FREE                                                  $11
   37    17      > JMP                                                      ->5
         18    >   FE_FREE                                                  $8
   43    19      > RETURN                                                   !0
   44    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/2JGM7
function name:  addSource
number of ops:  17
compiled vars:  !0 = $directive, !1 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        FETCH_OBJ_IS                                     ~2      'policy'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
   48     6    >   NEW                                              $5      'CSPException'
          7        SEND_VAL_EX                                              'Invalid+directive'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $5
   50    10    >   FETCH_OBJ_W                                      $7      'policy'
         11        FETCH_DIM_W                                      $8      $7, !0
         12        ASSIGN_DIM                                               $8
         13        OP_DATA                                                  !1
   51    14        FETCH_THIS                                       ~10     
         15      > RETURN                                                   ~10
   52    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/2JGM7
function name:  __toString
number of ops:  31
compiled vars:  !0 = $retval, !1 = $sources, !2 = $directive
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   ASSIGN                                                   !0, <array>
   58     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
   59     5        COUNT                                            ~8      !1
          6        IS_SMALLER                                               0, ~8
          7      > JMPZ                                                     ~9, ->20
   60     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
   58    20    > > JMP                                                      ->3
         21    >   FE_FREE                                                  $5
   63    22        INIT_FCALL                                               'join'
         23        SEND_VAL                                                 '%3B+'
         24        SEND_VAR                                                 !0
         25        DO_ICALL                                         $14     
         26        CONCAT                                           ~15     'Content-Security-Policy%3A+', $14
         27        VERIFY_RETURN_TYPE                                       ~15
         28      > RETURN                                                   ~15
   64    29*       VERIFY_RETURN_TYPE                                       
         30*     > 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:
153.52 ms | 1404 KiB | 21 Q