MDL-29509 move qeupgradehelper to admin tools
[moodle.git] / admin / report / spamcleaner / index.php
CommitLineData
2e3f66d7 1<?php
2
3/**
4 * Spam Cleaner
5 *
6 * Helps an admin to clean up spam in Moodle
7 *
2e3f66d7 8 * @authors Dongsheng Cai, Martin Dougiamas, Amr Hourani
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 */
11
2e3f66d7 12// List of known spammy keywords, please add more here
13
9edea336
PS
14/////////////////////////////////////////////////////////////////////////////////
15
16require_once('../../../config.php');
17require_once($CFG->libdir.'/adminlib.php');
18
19
20// Configuration
21
2e3f66d7 22$autokeywords = array(
23 "<img",
24 "fuck",
25 "casino",
26 "porn",
27 "xxx",
28 "cialis",
29 "viagra",
30 "poker",
31 "warcraft"
32 );
33
2e3f66d7 34$keyword = optional_param('keyword', '', PARAM_RAW);
35$autodetect = optional_param('autodetect', '', PARAM_RAW);
36$del = optional_param('del', '', PARAM_RAW);
37$delall = optional_param('delall', '', PARAM_RAW);
38$ignore = optional_param('ignore', '', PARAM_RAW);
39$reset = optional_param('reset', '', PARAM_RAW);
40$id = optional_param('id', '', PARAM_INT);
41
42require_login();
43admin_externalpage_setup('reportspamcleaner');
2e3f66d7 44
45// Delete one user
46if (!empty($del) && confirm_sesskey() && ($id != $USER->id)) {
47 if (isset($SESSION->users_result[$id])) {
48 $user = $SESSION->users_result[$id];
49 if (delete_user($user)) {
50 unset($SESSION->users_result[$id]);
51 echo json_encode(true);
52 } else {
53 echo json_encode(false);
54 }
55 } else {
56 echo json_encode(false);
57 }
58 exit;
59}
60
61// Delete lots of users
62if (!empty($delall) && confirm_sesskey()) {
63 if (!empty($SESSION->users_result)) {
64 foreach ($SESSION->users_result as $userid => $user) {
65 if ($userid != $USER->id) {
66 if (delete_user($user)) {
67 unset($SESSION->users_result[$userid]);
68 }
69 }
70 }
71 }
72 echo json_encode(true);
73 exit;
74}
75
76if (!empty($ignore)) {
77 unset($SESSION->users_result[$id]);
78 echo json_encode(true);
79 exit;
80}
81
2b8c3f8c 82$PAGE->requires->js_init_call('M.report_spamcleaner.init', array(me()), true);
9edea336
PS
83$strings = Array('spaminvalidresult','spamdeleteallconfirm','spamcannotdelete','spamdeleteconfirm');
84$PAGE->requires->strings_for_js($strings, 'report_spamcleaner');
2e3f66d7 85
61ef8f9f 86echo $OUTPUT->header();
2e3f66d7 87
88// Print headers and things
20486a5a 89echo $OUTPUT->box(get_string('spamcleanerintro', 'report_spamcleaner'));
2e3f66d7 90
20486a5a 91echo $OUTPUT->box_start(); // The forms section at the top
2e3f66d7 92
93?>
94
95<div class="mdl-align">
96
6acb6a3d 97<form method="post" action="index.php">
98 <div>
20486a5a 99 <input type="text" name="keyword" id="keyword_el" value="<?php p($keyword) ?>" />
2e3f66d7 100 <input type="hidden" name="sesskey" value="<?php echo sesskey();?>" />
101 <input type="submit" value="<?php echo get_string('spamsearch', 'report_spamcleaner')?>" />
6acb6a3d 102 </div>
2e3f66d7 103</form>
104<p><?php echo get_string('spameg', 'report_spamcleaner');?></p>
105
106<hr />
107
6acb6a3d 108<form method="post" action="index.php">
109 <div>
2e3f66d7 110 <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'report_spamcleaner');?>" />
6acb6a3d 111 </div>
2e3f66d7 112</form>
113
114
115</div>
116
117<?php
20486a5a 118echo $OUTPUT->box_end();
2e3f66d7 119
120echo '<div id="result" class="mdl-align">';
121
122// Print list of resulting profiles
123
124if (!empty($keyword)) { // Use the keyword(s) supplied by the user
125 $keywords = explode(',', $keyword);
126 foreach ($keywords as $key => $keyword) {
127 $keywords[$key] = trim($keyword);
128 }
129 search_spammers($keywords);
130
131} else if (!empty($autodetect)) { // Use the inbuilt keyword list to detect users
132 search_spammers($autokeywords);
133}
134
135echo '</div>';
136
137/////////////////////////////////////////////////////////////////////////////////
138
139
20486a5a 140/// Functions
2e3f66d7 141
142
143function search_spammers($keywords) {
144
20486a5a 145 global $CFG, $USER, $DB, $OUTPUT;
2e3f66d7 146
147 if (!is_array($keywords)) {
148 $keywords = array($keywords); // Make it into an array
149 }
150
4287c1c2 151 $params = array('userid'=>$USER->id);
2e3f66d7 152
153 $keywordfull = array();
d38f419d 154 $i = 0;
2e3f66d7 155 foreach ($keywords as $keyword) {
d38f419d
PS
156 $keywordfull[] = $DB->sql_like('description', ':descpat'.$i, false);
157 $params['descpat'.$i] = "%$keyword%";
158 $keywordfull2[] = $DB->sql_like('p.summary', ':sumpat'.$i, false);
159 $params['sumpat'.$i] = "%$keyword%";
160 $i++;
2e3f66d7 161 }
162 $conditions = '( '.implode(' OR ', $keywordfull).' )';
163 $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
164
4287c1c2
PS
165 $sql = "SELECT * FROM {user} WHERE deleted = 0 AND id <> :userid AND $conditions"; // Exclude oneself
166 $sql2 = "SELECT u.*, p.summary FROM {user} AS u, {post} AS p WHERE $conditions2 AND u.deleted = 0 AND u.id=p.userid AND u.id <> :userid";
d38f419d
PS
167 $spamusers_desc = $DB->get_recordset_sql($sql, $params);
168 $spamusers_blog = $DB->get_recordset_sql($sql2, $params);
2e3f66d7 169
170 $keywordlist = implode(', ', $keywords);
20486a5a 171 echo $OUTPUT->box(get_string('spamresult', 'report_spamcleaner').s($keywordlist)).' ...';
2e3f66d7 172
173 print_user_list(array($spamusers_desc, $spamusers_blog), $keywords);
174
175}
176
177
178
179function print_user_list($users_rs, $keywords) {
180 global $CFG, $SESSION;
181
182 // reset session everytime this function is called
183 $SESSION->users_result = array();
184 $count = 0;
185
186 foreach ($users_rs as $rs){
187 foreach ($rs as $user) {
188 if (!$count) {
189 echo '<table border="1" width="100%" id="data-grid"><tr><th>&nbsp;</th><th>'.get_string('user','admin').'</th><th>'.get_string('spamdesc', 'report_spamcleaner').'</th><th>'.get_string('spamoperation', 'report_spamcleaner').'</th></tr>';
190 }
191 $count++;
192 filter_user($user, $keywords, $count);
193 }
194 }
195
196 if (!$count) {
197 echo get_string('spamcannotfinduser', 'report_spamcleaner');
198
199 } else {
200 echo '</table>';
201 echo '<div class="mld-align">
202 <button id="removeall_btn">'.get_string('spamdeleteall', 'report_spamcleaner').'</button>
203 </div>';
204 }
205}
206function filter_user($user, $keywords, $count) {
207 global $CFG;
208 $image_search = false;
209 if (in_array('<img', $keywords)) {
210 $image_search = true;
211 }
212 if (isset($user->summary)) {
213 $user->description = '<h3>'.get_string('spamfromblog', 'report_spamcleaner').'</h3>'.$user->summary;
214 unset($user->summary);
215 }
5d3b9994 216 if (preg_match('#<img.*src=[\"\']('.$CFG->wwwroot.')#', $user->description, $matches)
2e3f66d7 217 && $image_search) {
218 $result = false;
219 foreach ($keywords as $keyword) {
220 if (preg_match('#'.$keyword.'#', $user->description)
221 && ($keyword != '<img')) {
222 $result = true;
223 }
224 }
225 if ($result) {
226 echo print_user_entry($user, $keywords, $count);
227 } else {
228 unset($user);
229 }
230 } else {
231 echo print_user_entry($user, $keywords, $count);
232 }
233}
234
235
236function print_user_entry($user, $keywords, $count) {
237
238 global $SESSION, $CFG;
239
a226a972 240 $smalluserobject = new stdClass(); // All we need to delete them later
2e3f66d7 241 $smalluserobject->id = $user->id;
242 $smalluserobject->email = $user->email;
243 $smalluserobject->auth = $user->auth;
244 $smalluserobject->firstname = $user->firstname;
245 $smalluserobject->lastname = $user->lastname;
9edea336 246 $smalluserobject->username = $user->username;
2e3f66d7 247
248 if (empty($SESSION->users_result[$user->id])) {
249 $SESSION->users_result[$user->id] = $smalluserobject;
250 $html = '<tr valign="top" id="row-'.$user->id.'" class="result-row">';
251 $html .= '<td width="10">'.$count.'</td>';
209c122b 252 $html .= '<td width="30%" align="left"><a href="'.$CFG->wwwroot."/user/view.php?course=1&amp;id=".$user->id.'" title="'.s($user->username).'">'.fullname($user).'</a>';
2e3f66d7 253
254 $html .= "<ul>";
255 $profile_set = array('city'=>true, 'country'=>true, 'email'=>true);
256 foreach ($profile_set as $key=>$value) {
257 if (isset($user->$key)){
258 $html .= '<li>'.$user->$key.'</li>';
259 }
260 }
261 $html .= "</ul>";
262 $html .= '</td>';
263
264 foreach ($keywords as $keyword) {
1e12b912 265 $user->description = highlight($keyword, $user->description);
2e3f66d7 266 }
267
8bdc9cac
SH
268 if (!isset($user->descriptionformat)) {
269 $user->descriptionformat = FORMAT_MOODLE;
270 }
271
367a75fa 272 $html .= '<td align="left">'.format_text($user->description, $user->descriptionformat, array('overflowdiv'=>true)).'</td>';
2e3f66d7 273 $html .= '<td width="100px" align="center">';
9edea336
PS
274 $html .= '<button onclick="M.report_spamcleaner.del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
275 $html .= '<button onclick="M.report_spamcleaner.ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
2e3f66d7 276 $html .= '</td>';
277 $html .= '</tr>';
278 return $html;
279 } else {
280 return null;
281 }
282
283
284}
285
73d6f52f 286echo $OUTPUT->footer();