3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace owasp\csp; 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; } function toString() { return $this->__toString(); } 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 {} use owasp\csp\ContentSecurityPolicy as CSP; $csp = new CSP(); $csp->addSource(CSP::DEFAULT_SRC, CSP::SOURCE_SELF) ->addSource(CSP::SCRIPT_SRC, CSP::SOURCE_SELF) ->addSource(CSP::SCRIPT_SRC, 'http://code.jquery.com') ->addSource(CSP::STYLE_SRC, ContentSecurityPolicy::SOURCE_SELF) ->addSource(CSP::STYLE_SRC, 'https://bootstrapcdn.com') ->addSource(CSP::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/390sg
function name:  (null)
number of ops:  42
compiled vars:  !0 = $csp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   DECLARE_CLASS                                            'owasp%5Ccsp%5Ccontentsecuritypolicy'
   75     1        NEW                                              $1      'owasp%5Ccsp%5CContentSecurityPolicy'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   76     4        INIT_METHOD_CALL                                         !0, 'addSource'
          5        FETCH_CLASS_CONSTANT                             ~4      'owasp%5Ccsp%5CContentSecurityPolicy', 'DEFAULT_SRC'
          6        SEND_VAL_EX                                              ~4
          7        FETCH_CLASS_CONSTANT                             ~5      'owasp%5Ccsp%5CContentSecurityPolicy', 'SOURCE_SELF'
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0  $6      
         10        INIT_METHOD_CALL                                         $6, 'addSource'
         11        FETCH_CLASS_CONSTANT                             ~7      'owasp%5Ccsp%5CContentSecurityPolicy', 'SCRIPT_SRC'
         12        SEND_VAL_EX                                              ~7
         13        FETCH_CLASS_CONSTANT                             ~8      'owasp%5Ccsp%5CContentSecurityPolicy', 'SOURCE_SELF'
         14        SEND_VAL_EX                                              ~8
         15        DO_FCALL                                      0  $9      
   77    16        INIT_METHOD_CALL                                         $9, 'addSource'
         17        FETCH_CLASS_CONSTANT                             ~10     'owasp%5Ccsp%5CContentSecurityPolicy', 'SCRIPT_SRC'
         18        SEND_VAL_EX                                              ~10
         19        SEND_VAL_EX                                              'http%3A%2F%2Fcode.jquery.com'
         20        DO_FCALL                                      0  $11     
   78    21        INIT_METHOD_CALL                                         $11, 'addSource'
         22        FETCH_CLASS_CONSTANT                             ~12     'owasp%5Ccsp%5CContentSecurityPolicy', 'STYLE_SRC'
         23        SEND_VAL_EX                                              ~12
         24        FETCH_CLASS_CONSTANT                             ~13     'owasp%5Ccsp%5CContentSecurityPolicy', 'SOURCE_SELF'
         25        SEND_VAL_EX                                              ~13
         26        DO_FCALL                                      0  $14     
   79    27        INIT_METHOD_CALL                                         $14, 'addSource'
         28        FETCH_CLASS_CONSTANT                             ~15     'owasp%5Ccsp%5CContentSecurityPolicy', 'STYLE_SRC'
         29        SEND_VAL_EX                                              ~15
         30        SEND_VAL_EX                                              'https%3A%2F%2Fbootstrapcdn.com'
         31        DO_FCALL                                      0  $16     
   80    32        INIT_METHOD_CALL                                         $16, 'addSource'
         33        FETCH_CLASS_CONSTANT                             ~17     'owasp%5Ccsp%5CContentSecurityPolicy', 'FONT_SRC'
         34        SEND_VAL_EX                                              ~17
         35        SEND_VAL_EX                                              'https%3A%2F%2Ffonts.googleapis.com'
         36        DO_FCALL                                      0          
   81    37        INIT_METHOD_CALL                                         !0, 'toString'
         38        DO_FCALL                                      0  $19     
         39        ECHO                                                     $19
   83    40      > EXIT                                                     '%0ADone%21%0A'
         41*     > RETURN                                                   1

Class owasp\csp\ContentSecurityPolicy:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/390sg
function name:  __construct
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN_OBJ                                               'policy'
          1        OP_DATA                                                  <array>
   23     2        INIT_METHOD_CALL                                         'setPolicySourceDirectives'
          3        DO_FCALL                                      0          
   24     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/390sg
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
-------------------------------------------------------------------------------------
   28     0  E >   NEW                                              $4      'ReflectionClass'
          1        SEND_VAL_EX                                              'owasp%5Ccsp%5CContentSecurityPolicy'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   29     4        ASSIGN                                                   !1, '%2F.%2B_SRC%24%2F'
   30     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
   31    10        INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Cpreg_match'
         11        SEND_VAR_EX                                              !1
         12        SEND_VAR_EX                                              !3
         13        DO_FCALL                                      0  $12     
         14      > JMPZ                                                     $12, ->22
   32    15    >   INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Cconstant'
         16        CONCAT                                           ~14     'owasp%5Ccsp%5CContentSecurityPolicy%3A%3A', !3
         17        SEND_VAL_EX                                              ~14
         18        DO_FCALL                                      0  $15     
         19        FETCH_OBJ_W                                      $13     'policy'
         20        ASSIGN_DIM                                               $13, $15
         21        OP_DATA                                                  <array>
   30    22    > > JMP                                                      ->8
         23    >   FE_FREE                                                  $9
   35    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 = 19
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 19
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 17
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 17
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/390sg
function name:  copy
number of ops:  22
compiled vars:  !0 = $retval, !1 = $sources, !2 = $directive, !3 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   NEW                                              $4      'owasp%5Ccsp%5CContentSecurityPolicy'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $4
   39     3        FETCH_OBJ_R                                      ~7      'policy'
          4      > FE_RESET_R                                       $8      ~7, ->19
          5    > > FE_FETCH_R                                       ~9      $8, !1, ->19
          6    >   ASSIGN                                                   !2, ~9
   40     7      > FE_RESET_R                                       $11     !1, ->17
          8    > > FE_FETCH_R                                               $11, !3, ->17
   41     9    >   INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Carray_push'
         10        CHECK_FUNC_ARG                                           
         11        FETCH_OBJ_FUNC_ARG                               $12     !0, 'policy'
         12        FETCH_DIM_FUNC_ARG                               $13     $12, !2
         13        SEND_FUNC_ARG                                            $13
         14        SEND_VAR_EX                                              !3
         15        DO_FCALL                                      0          
   40    16      > JMP                                                      ->8
         17    >   FE_FREE                                                  $11
   39    18      > JMP                                                      ->5
         19    >   FE_FREE                                                  $8
   45    20      > RETURN                                                   !0
   46    21*     > 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/390sg
function name:  addSource
number of ops:  17
compiled vars:  !0 = $directive, !1 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   49     2        FETCH_OBJ_IS                                     ~2      'policy'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
   50     6    >   NEW                                              $5      'owasp%5Ccsp%5CCSPException'
          7        SEND_VAL_EX                                              'Invalid+directive'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $5
   52    10    >   FETCH_OBJ_W                                      $7      'policy'
         11        FETCH_DIM_W                                      $8      $7, !0
         12        ASSIGN_DIM                                               $8
         13        OP_DATA                                                  !1
   53    14        FETCH_THIS                                       ~10     
         15      > RETURN                                                   ~10
   54    16*     > RETURN                                                   null

End of function addsource

Function tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/390sg
function name:  toString
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   INIT_METHOD_CALL                                         '__toString'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
   58     3*     > RETURN                                                   null

End of function tostring

Function __tostring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 23
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 22
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
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/390sg
function name:  __toString
number of ops:  33
compiled vars:  !0 = $retval, !1 = $sources, !2 = $directive
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   ASSIGN                                                   !0, <array>
   62     1        FETCH_OBJ_R                                      ~4      'policy'
          2      > FE_RESET_R                                       $5      ~4, ->23
          3    > > FE_FETCH_R                                       ~6      $5, !1, ->23
          4    >   ASSIGN                                                   !2, ~6
   63     5        INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Csizeof'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $8      
          8        IS_SMALLER                                               0, $8
          9      > JMPZ                                                     ~9, ->22
   64    10    >   INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Cjoin'
         11        SEND_VAL_EX                                              '+'
         12        INIT_ARRAY                                       ~11     !2
         13        INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Cjoin'
         14        SEND_VAL_EX                                              '+'
         15        SEND_VAR_EX                                              !1
         16        DO_FCALL                                      0  $12     
         17        ADD_ARRAY_ELEMENT                                ~11     $12
         18        SEND_VAL_EX                                              ~11
         19        DO_FCALL                                      0  $13     
         20        ASSIGN_DIM                                               !0
         21        OP_DATA                                                  $13
   62    22    > > JMP                                                      ->3
         23    >   FE_FREE                                                  $5
   67    24        INIT_NS_FCALL_BY_NAME                                    'owasp%5Ccsp%5Cjoin'
         25        SEND_VAL_EX                                              '%3B+'
         26        SEND_VAR_EX                                              !0
         27        DO_FCALL                                      0  $14     
         28        CONCAT                                           ~15     'Content-Security-Policy%3A+', $14
         29        VERIFY_RETURN_TYPE                                       ~15
         30      > RETURN                                                   ~15
   68    31*       VERIFY_RETURN_TYPE                                       
         32*     > RETURN                                                   null

End of function __tostring

End of class owasp\csp\ContentSecurityPolicy.

Class owasp\csp\CSPException: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.29 ms | 1412 KiB | 23 Q