MDL-18223 merged spam cleaner into head + removed legacy code + fixed report title
[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 *
8 * @version $Id$
9 * @authors Dongsheng Cai, Martin Dougiamas, Amr Hourani
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 */
12
13// Configuration
14
15// List of known spammy keywords, please add more here
16
17$autokeywords = array(
18 "<img",
19 "fuck",
20 "casino",
21 "porn",
22 "xxx",
23 "cialis",
24 "viagra",
25 "poker",
26 "warcraft"
27 );
28
29
30/////////////////////////////////////////////////////////////////////////////////
31
32require_once('../../../config.php');
33require_once($CFG->libdir.'/adminlib.php');
34
35require_js(array('yui_dom-event', 'yui_connection', 'yui_json'));
36
37$keyword = optional_param('keyword', '', PARAM_RAW);
38$autodetect = optional_param('autodetect', '', PARAM_RAW);
39$del = optional_param('del', '', PARAM_RAW);
40$delall = optional_param('delall', '', PARAM_RAW);
41$ignore = optional_param('ignore', '', PARAM_RAW);
42$reset = optional_param('reset', '', PARAM_RAW);
43$id = optional_param('id', '', PARAM_INT);
44
45require_login();
46admin_externalpage_setup('reportspamcleaner');
47
48// Implement some AJAX calls
49
50// Delete one user
51if (!empty($del) && confirm_sesskey() && ($id != $USER->id)) {
52 if (isset($SESSION->users_result[$id])) {
53 $user = $SESSION->users_result[$id];
54 if (delete_user($user)) {
55 unset($SESSION->users_result[$id]);
56 echo json_encode(true);
57 } else {
58 echo json_encode(false);
59 }
60 } else {
61 echo json_encode(false);
62 }
63 exit;
64}
65
66// Delete lots of users
67if (!empty($delall) && confirm_sesskey()) {
68 if (!empty($SESSION->users_result)) {
69 foreach ($SESSION->users_result as $userid => $user) {
70 if ($userid != $USER->id) {
71 if (delete_user($user)) {
72 unset($SESSION->users_result[$userid]);
73 }
74 }
75 }
76 }
77 echo json_encode(true);
78 exit;
79}
80
81if (!empty($ignore)) {
82 unset($SESSION->users_result[$id]);
83 echo json_encode(true);
84 exit;
85}
86
87
88admin_externalpage_print_header();
89
90// Print headers and things
91
92print_spamcleaner_javascript();
93
94print_box(get_string('spamcleanerintro', 'report_spamcleaner'));
95
96print_box_start(); // The forms section at the top
97
98?>
99
100<div class="mdl-align">
101
102<form method="POST">
103 <input type="text" name="keyword" id="keyword_el" value="<?php p($keyword) ?>" />
104 <input type="hidden" name="sesskey" value="<?php echo sesskey();?>" />
105 <input type="submit" value="<?php echo get_string('spamsearch', 'report_spamcleaner')?>" />
106</form>
107<p><?php echo get_string('spameg', 'report_spamcleaner');?></p>
108
109<hr />
110
111<form method="POST">
112 <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'report_spamcleaner');?>" />
113</form>
114
115
116</div>
117
118<?php
119print_box_end();
120
121echo '<div id="result" class="mdl-align">';
122
123// Print list of resulting profiles
124
125if (!empty($keyword)) { // Use the keyword(s) supplied by the user
126 $keywords = explode(',', $keyword);
127 foreach ($keywords as $key => $keyword) {
128 $keywords[$key] = trim($keyword);
129 }
130 search_spammers($keywords);
131
132} else if (!empty($autodetect)) { // Use the inbuilt keyword list to detect users
133 search_spammers($autokeywords);
134}
135
136echo '</div>';
137
138/////////////////////////////////////////////////////////////////////////////////
139
140
141/// Functions
142
143
144function search_spammers($keywords) {
145
146 global $CFG, $USER, $DB;
147
148 if (!is_array($keywords)) {
149 $keywords = array($keywords); // Make it into an array
150 }
151
152 $like = $DB->sql_ilike();
153
154 $keywordfull = array();
155 foreach ($keywords as $keyword) {
156 $keyword = addslashes($keyword); // Just to be safe
157 $keywordfull[] = " description $like '%$keyword%' ";
158 $keywordfull2[] = " p.summary $like '%$keyword%' ";
159 }
160 $conditions = '( '.implode(' OR ', $keywordfull).' )';
161 $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
162
163 $sql = "SELECT * FROM {user} WHERE deleted = 0 AND id <> {$USER->id} AND $conditions"; // Exclude oneself
164 $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 <> {$USER->id}";
165 $spamusers_desc = $DB->get_recordset_sql($sql);
166 $spamusers_blog = $DB->get_recordset_sql($sql2);
167
168 $keywordlist = implode(', ', $keywords);
169 print_box(get_string('spamresult', 'report_spamcleaner').s($keywordlist)).' ...';
170
171 print_user_list(array($spamusers_desc, $spamusers_blog), $keywords);
172
173}
174
175
176
177function print_user_list($users_rs, $keywords) {
178 global $CFG, $SESSION;
179
180 // reset session everytime this function is called
181 $SESSION->users_result = array();
182 $count = 0;
183
184 foreach ($users_rs as $rs){
185 foreach ($rs as $user) {
186 if (!$count) {
187 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>';
188 }
189 $count++;
190 filter_user($user, $keywords, $count);
191 }
192 }
193
194 if (!$count) {
195 echo get_string('spamcannotfinduser', 'report_spamcleaner');
196
197 } else {
198 echo '</table>';
199 echo '<div class="mld-align">
200 <button id="removeall_btn">'.get_string('spamdeleteall', 'report_spamcleaner').'</button>
201 </div>';
202 }
203}
204function filter_user($user, $keywords, $count) {
205 global $CFG;
206 $image_search = false;
207 if (in_array('<img', $keywords)) {
208 $image_search = true;
209 }
210 if (isset($user->summary)) {
211 $user->description = '<h3>'.get_string('spamfromblog', 'report_spamcleaner').'</h3>'.$user->summary;
212 unset($user->summary);
213 }
214 if (preg_match('#<img.*src=[\"\']('.$CFG->pixpath.')#', $user->description, $matches)
215 && $image_search) {
216 $result = false;
217 foreach ($keywords as $keyword) {
218 if (preg_match('#'.$keyword.'#', $user->description)
219 && ($keyword != '<img')) {
220 $result = true;
221 }
222 }
223 if ($result) {
224 echo print_user_entry($user, $keywords, $count);
225 } else {
226 unset($user);
227 }
228 } else {
229 echo print_user_entry($user, $keywords, $count);
230 }
231}
232
233
234function print_user_entry($user, $keywords, $count) {
235
236 global $SESSION, $CFG;
237
238 $smalluserobject = new object; // All we need to delete them later
239 $smalluserobject->id = $user->id;
240 $smalluserobject->email = $user->email;
241 $smalluserobject->auth = $user->auth;
242 $smalluserobject->firstname = $user->firstname;
243 $smalluserobject->lastname = $user->lastname;
244
245 if (empty($SESSION->users_result[$user->id])) {
246 $SESSION->users_result[$user->id] = $smalluserobject;
247 $html = '<tr valign="top" id="row-'.$user->id.'" class="result-row">';
248 $html .= '<td width="10">'.$count.'</td>';
249 $html .= '<td width="30%" align="left"><a href="'.$CFG->wwwroot."/user/view.php?course=1&amp;id=".$user->id.'" title="'.$user->username.'">'.fullname($user).'</a>';
250
251 $html .= "<ul>";
252 $profile_set = array('city'=>true, 'country'=>true, 'email'=>true);
253 foreach ($profile_set as $key=>$value) {
254 if (isset($user->$key)){
255 $html .= '<li>'.$user->$key.'</li>';
256 }
257 }
258 $html .= "</ul>";
259 $html .= '</td>';
260
261 foreach ($keywords as $keyword) {
262 $user->description = format_text(highlight($keyword, $user->description), FORMAT_MOODLE);
263 }
264
265 $html .= '<td align="left">'.$user->description.'</td>';
266 $html .= '<td width="100px" align="center">';
267 $html .= '<button onclick="del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
268 $html .= '<button onclick="ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
269 $html .= '</td>';
270 $html .= '</tr>';
271 return $html;
272 } else {
273 return null;
274 }
275
276
277}
278
279function print_spamcleaner_javascript() {
280
281$sesskey = sesskey();
282
283?>
284
285<script type="text/javascript">
286var row = null;
287var delall_cb = {
288 success: function(o){
289 try {
290 var resp = YAHOO.lang.JSON.parse(o.responseText);
291 } catch(e) {
292 alert('<?php echo get_string('spaminvalidresult', 'report_spamcleaner');?>');
293 return;
294 }
295 if(resp == true){
296 window.location.href=window.location.href;
297 }
298 }
299}
300function init() {
301 YAHOO.util.Event.addListener("removeall_btn", "click", function(){
302 var yes = confirm('<?php echo get_string('spamdeleteallconfirm', 'report_spamcleaner');?>');
303 if(yes){
304 var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php echo me();?>?delall=yes&sesskey=<?php echo $sesskey;?>', delall_cb);
305 }
306 });
307}
308var del_cb = {
309 success: function(o) {
310 try {
311 var resp = YAHOO.lang.JSON.parse(o.responseText);
312 } catch(e) {
313 alert('<?php echo get_string('spaminvalidresult', 'report_spamcleaner');?>');
314 return;
315 }
316 if(row) {
317 if(resp == true){
318 while(row.tagName != 'TR') {
319 row = row.parentNode;
320 }
321 row.parentNode.removeChild(row);
322 row = null;
323 } else {
324 alert('<?php echo get_string('spamcannotdelete', 'report_spamcleaner');?>');
325 }
326 }
327 }
328}
329var ignore_cb = {
330 success: function(o){
331 try {
332 var resp = YAHOO.lang.JSON.parse(o.responseText);
333 } catch(e) {
334 alert('<?php echo get_string('spaminvalidresult', 'report_spamcleaner');?>');
335 return;
336 }
337 if(row) {
338 if(resp == true){
339 while(row.tagName != 'TR') {
340 row = row.parentNode;
341 }
342 row.parentNode.removeChild(row);
343 row = null;
344 }
345 }
346 }
347}
348function del_user(obj, id) {
349 var yes = confirm('<?php echo get_string('spamdeleteconfirm', 'report_spamcleaner');?>');
350 if(yes){
351 row = obj;
352 var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php echo me();?>?del=yes&sesskey=<?php echo $sesskey;?>&id='+id, del_cb);
353 }
354}
355function ignore_user(obj, id) {
356 row = obj;
357 var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php echo me();?>?ignore=yes&sesskey=<?php echo $sesskey;?>&id='+id, ignore_cb);
358}
359YAHOO.util.Event.onDOMReady(init);
360</script>
361
362<?php
363}
364
365admin_externalpage_print_footer();
366
367?>