info) $hash = hash('md5', $blocked_uri . $document_uri . $source_file . $line_number . $violated_directive); try { // Check report dir (create it if necessary) files::makeDir(dirname(LOGFILE), true); // Check if report is not already stored in log file $contents = ''; if (file_exists(LOGFILE)) { $contents = file_get_contents(LOGFILE); if ($contents && $contents != '') { if (substr($contents, -1) == ',') { // Remove final comma if present $contents = substr($contents, 0, -1); } if ($contents != '') { $list = json_decode('[' . $contents . ']', true); if (is_array($list)) { foreach ($list as $idx => $value) { if (isset($value['hash']) && $value['hash'] == $hash) { // Already stored, ignore return; } } } } } } // Add report to the file if (!($fp = @fopen(LOGFILE, 'a'))) { // Unable to open file, ignore return; } // Prettify the JSON-formatted data $violation = array_merge(['hash' => $hash], $data['csp-report']); $output = json_encode($violation, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // The file content will have to be enclosed in brackets [] before // beeing decoded with json_decoded(,true); fprintf($fp, ($contents != '' ? ',' : '') . '%s', $output); } catch (Exception $e) { return; } } }