Dotclear


Ignore:
Timestamp:
03/08/18 17:58:39 (8 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Code formatting (PSR-2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin/csp_report.php

    r3482 r3730  
    88header('X-Content-Type-Options: "nosniff"'); 
    99 
    10 require dirname(__FILE__).'/../inc/admin/prepend.php'; 
     10require dirname(__FILE__) . '/../inc/admin/prepend.php'; 
    1111 
    1212// Specify admin CSP log file if necessary 
    1313if (!defined('LOGFILE')) { 
    14      define('LOGFILE',path::real(DC_VAR).'/csp/csp_report.json'); 
     14    define('LOGFILE', path::real(DC_VAR) . '/csp/csp_report.json'); 
    1515} 
    1616 
     
    2020// Only continue if it’s valid JSON that is not just `null`, `0`, `false` or an 
    2121// empty string, i.e. if it could be a CSP violation report. 
    22 if ($data = json_decode($data,true)) { 
     22if ($data = json_decode($data, true)) { 
    2323 
    24      // get source-file and blocked-URI to perform some tests 
    25      $source_file = isset($data['csp-report']['source-file']) ? $data['csp-report']['source-file'] : ''; 
    26      $line_number = isset($data['csp-report']['line-number']) ? $data['csp-report']['line-number'] : ''; 
    27      $blocked_uri = isset($data['csp-report']['blocked-uri']) ? $data['csp-report']['blocked-uri'] : ''; 
    28      $document_uri = isset($data['csp-report']['document-uri']) ? $data['csp-report']['document-uri'] : ''; 
    29      $violated_directive = isset($data['csp-report']['violated-directive']) ? $data['csp-report']['violated-directive'] : ''; 
     24    // get source-file and blocked-URI to perform some tests 
     25    $source_file        = isset($data['csp-report']['source-file']) ? $data['csp-report']['source-file'] : ''; 
     26    $line_number        = isset($data['csp-report']['line-number']) ? $data['csp-report']['line-number'] : ''; 
     27    $blocked_uri        = isset($data['csp-report']['blocked-uri']) ? $data['csp-report']['blocked-uri'] : ''; 
     28    $document_uri      = isset($data['csp-report']['document-uri']) ? $data['csp-report']['document-uri'] : ''; 
     29    $violated_directive = isset($data['csp-report']['violated-directive']) ? $data['csp-report']['violated-directive'] : ''; 
    3030 
    31      if ( 
    32           // avoid false positives notifications coming from Chrome extensions (Wappalyzer, MuteTab, etc.) 
    33           // bug here https://code.google.com/p/chromium/issues/detail?id=524356 
    34           strpos($source_file, 'chrome-extension://') === false 
     31    if ( 
     32        // avoid false positives notifications coming from Chrome extensions (Wappalyzer, MuteTab, etc.) 
     33        // bug here https://code.google.com/p/chromium/issues/detail?id=524356 
     34        strpos($source_file, 'chrome-extension://') === false 
    3535 
    36           // avoid false positives notifications coming from Safari extensions (diigo, evernote, etc.) 
    37           && strpos($source_file, 'safari-extension://') === false 
    38           && strpos($blocked_uri, 'safari-extension://') === false 
     36        // avoid false positives notifications coming from Safari extensions (diigo, evernote, etc.) 
     37         && strpos($source_file, 'safari-extension://') === false 
     38        && strpos($blocked_uri, 'safari-extension://') === false 
    3939 
    40           // search engine extensions ? 
    41           && strpos($source_file, 'se-extension://') === false 
     40        // search engine extensions ? 
     41         && strpos($source_file, 'se-extension://') === false 
    4242 
    43           // added by browsers in webviews 
    44           && strpos($blocked_uri, 'webviewprogressproxy://') === false 
     43        // added by browsers in webviews 
     44         && strpos($blocked_uri, 'webviewprogressproxy://') === false 
    4545 
    46           // Google Search App see for details https://github.com/nico3333fr/CSP-useful/commit/ecc8f9b0b379ae643bc754d2db33c8b47e185fd1 
    47           && strpos($blocked_uri, 'gsa://onpageload') === false 
     46        // Google Search App see for details https://github.com/nico3333fr/CSP-useful/commit/ecc8f9b0b379ae643bc754d2db33c8b47e185fd1 
     47         && strpos($blocked_uri, 'gsa://onpageload') === false 
    4848 
    49      ) { 
    50           // Prepare report data (hash => info) 
    51           $hash = hash('md5',$blocked_uri.$document_uri.$source_file.$line_number.$violated_directive); 
     49    ) { 
     50        // Prepare report data (hash => info) 
     51        $hash = hash('md5', $blocked_uri . $document_uri . $source_file . $line_number . $violated_directive); 
    5252 
    53           try { 
    54                // Check report dir (create it if necessary) 
    55                files::makeDir(dirname(LOGFILE),true); 
     53        try { 
     54            // Check report dir (create it if necessary) 
     55            files::makeDir(dirname(LOGFILE), true); 
    5656 
    57                // Check if report is not already stored in log file 
    58                $contents = ''; 
    59                if (file_exists(LOGFILE)) { 
    60                     $contents = file_get_contents(LOGFILE); 
    61                     if ($contents && $contents != '') { 
    62                          if (substr($contents,-1) == ',') { 
    63                               // Remove final comma if present 
    64                               $contents = substr($contents,0,-1); 
    65                          } 
    66                          if ($contents != '') { 
    67                               $list = json_decode('['.$contents.']',true); 
    68                               if (is_array($list)) { 
    69                                    foreach ($list as $idx => $value) { 
    70                                         if (isset($value['hash']) && $value['hash'] == $hash) { 
    71                                              // Already stored, ignore 
    72                                              return; 
    73                                         } 
    74                                    } 
    75                               } 
    76                          } 
    77                     } 
    78                } 
     57            // Check if report is not already stored in log file 
     58            $contents = ''; 
     59            if (file_exists(LOGFILE)) { 
     60                $contents = file_get_contents(LOGFILE); 
     61                if ($contents && $contents != '') { 
     62                    if (substr($contents, -1) == ',') { 
     63                        // Remove final comma if present 
     64                        $contents = substr($contents, 0, -1); 
     65                    } 
     66                    if ($contents != '') { 
     67                        $list = json_decode('[' . $contents . ']', true); 
     68                        if (is_array($list)) { 
     69                            foreach ($list as $idx => $value) { 
     70                                if (isset($value['hash']) && $value['hash'] == $hash) { 
     71                                    // Already stored, ignore 
     72                                    return; 
     73                                } 
     74                            } 
     75                        } 
     76                    } 
     77                } 
     78            } 
    7979 
    80                // Add report to the file 
    81                if (!($fp = @fopen(LOGFILE,'a'))) { 
    82                     // Unable to open file, ignore 
    83                     return; 
    84                } 
     80            // Add report to the file 
     81            if (!($fp = @fopen(LOGFILE, 'a'))) { 
     82                // Unable to open file, ignore 
     83                return; 
     84            } 
    8585 
    86                // Prettify the JSON-formatted data 
    87                $violation = array_merge(array('hash' => $hash),$data['csp-report']); 
    88                $output = json_encode($violation,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); 
     86            // Prettify the JSON-formatted data 
     87            $violation = array_merge(array('hash' => $hash), $data['csp-report']); 
     88            $output    = json_encode($violation, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); 
    8989 
    90                // The file content will have to be enclosed in brackets [] before 
    91                // beeing decoded with json_decoded(<content>,true); 
    92                fprintf($fp,($contents != '' ? ',' : '').'%s',$output); 
     90            // The file content will have to be enclosed in brackets [] before 
     91            // beeing decoded with json_decoded(<content>,true); 
     92            fprintf($fp, ($contents != '' ? ',' : '') . '%s', $output); 
    9393 
    94           } catch (Exception $e) { 
    95                return; 
    96           } 
    97      } 
     94        } catch (Exception $e) { 
     95            return; 
     96        } 
     97    } 
    9898} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map