-
Notifications
You must be signed in to change notification settings - Fork 8
/
csp_report.php
177 lines (158 loc) · 6.07 KB
/
csp_report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This is an admin_externalpage 'local_csp_report' for displaying the recorded csp reports.
*
* @package local_csp
* @author Suan Kan <suankan@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
$viewblockeddomain = optional_param('blockeddomain', false, PARAM_TEXT);
if ($viewblockeddomain) {
$viewdirective = required_param('blockeddirective', PARAM_TEXT);
}
$removedirective = optional_param('removedirective', false, PARAM_TEXT);
$removedomain = optional_param('removedomain', false, PARAM_TEXT);
$removerecordwithid = optional_param('removerecordwithid', false, PARAM_TEXT);
$download = optional_param('download', '', PARAM_ALPHA);
admin_externalpage_setup('local_csp_report', '', null, '', array('pagelayout' => 'report'));
// Delete violation class if param set.
if ($removedirective && $removedomain && confirm_sesskey()) {
$DB->delete_records('local_csp', [
'violateddirective' => $removedirective,
'blockeddomain' => $removedomain
]
);
$PAGE->set_url('/local/csp/csp_report.php', array(
'page' => optional_param('redirecttopage', 0, PARAM_INT),
));
redirect($PAGE->url);
}
// Delete individual violation records if set.
if ($removerecordwithid && confirm_sesskey()) {
$DB->delete_records('local_csp', array('id' => $removerecordwithid));
$PAGE->set_url('/local/csp/csp_report.php', array(
'page' => optional_param('redirecttopage', 0, PARAM_INT),
));
redirect($PAGE->url);
}
$PAGE->set_url('/local/csp/csp_report.php', [
'blockeddomain' => $viewblockeddomain,
'blockeddirective' => $viewdirective ?? '',
'removedirective' => $removedirective,
'removedomain' => $removedomain,
'removerecordwithid' => $removerecordwithid,
]);
$resetallcspstatistics = optional_param('resetallcspstatistics', 0, PARAM_INT);
if ($resetallcspstatistics == 1 && confirm_sesskey()) {
$DB->delete_records('local_csp');
redirect(new moodle_url('/local/csp/csp_report.php'));
}
$title = get_string('cspreports', 'local_csp');
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->set_pagelayout('admin');
$table = new \local_csp\table\csp_report('cspreportstable');
$table->is_downloading($download, 'csp_report', 'csp_report');
$blockeduri = get_string('blockeduri', 'local_csp');
$blockeddomain = get_string('blockeddomain', 'local_csp');
$blockedurlpath = get_string('blockedurlpaths', 'local_csp');
$highestviolaters = get_string('highestviolaters', 'local_csp');
$violateddirective = get_string('violateddirective', 'local_csp');
$documenturi = get_string('documenturi', 'local_csp');
$courses = get_string('courses');
$failcounter = get_string('failcounter', 'local_csp');
$timeupdated = get_string('timeupdated', 'local_csp');
$action = get_string('action', 'local_csp');
$table->define_baseurl($PAGE->url);
$table->sortable(true, 'failcounter', SORT_DESC);
$table->set_attribute('class', 'generaltable generalbox table-sm');
$table->define_columns(array(
'failcounter',
'violateddirective',
'blockeddomain',
'blockedurlpaths',
'highestviolaters',
'courses',
'timecreated',
'action',
));
$table->no_sorting('blockedurlpaths');
$table->no_sorting('highestviolaters');
$table->no_sorting('courses');
$table->define_headers(array(
$failcounter,
$violateddirective,
$blockeddomain,
$blockedurlpath,
$highestviolaters,
$courses,
$timeupdated,
$action,
));
// If user has clicked on a violation to view all violation entries.
if ($viewblockeddomain && $viewdirective) {
$fields = 'id, sha1hash, blockeduri, blockeddomain, violateddirective, failcounter, timeupdated, documenturi';
$from = "{local_csp}";
$where = "blockeddomain = ? AND violateddirective = ?";
$params = [$viewblockeddomain, $viewdirective];
// Redefine columns to display Violation source.
$table->define_columns(array(
'failcounter',
'violateddirective',
'blockeduri',
'documenturi',
'timeupdated',
'action',
));
$table->define_headers(array(
$failcounter,
$violateddirective,
$blockeduri,
$documenturi,
$timeupdated,
$action,
));
} else {
$fields = 'id, blockeddomain, violateddirective, failcounter, timecreated';
// Select the first blockedURI of a type, and collapse the rest while summing failcounter.
$from = "(SELECT MAX(id) AS id,
blockeddomain,
violateddirective,
SUM(failcounter) AS failcounter,
MAX(timecreated) AS timecreated
FROM {local_csp}
GROUP BY violateddirective, blockeddomain) AS A";
$where = '1 = 1';
$params = array();
}
if (!$table->is_downloading()) {
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$action = new \confirm_action(get_string('areyousuretodeleteallrecords', 'local_csp'));
$urlresetallcspstatistics = new moodle_url($PAGE->url, array(
'resetallcspstatistics' => 1,
'sesskey' => sesskey(),
));
echo $OUTPUT->single_button($urlresetallcspstatistics,
get_string('resetallcspstatistics', 'local_csp'), 'post', array('actions' => array($action)));
}
$table->set_sql($fields, $from, $where, $params);
$table->out(30, true);
echo $OUTPUT->footer();