MDL-21400 global Y revisited, I really think it is better to pass it around properly...
[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
12// Configuration
13
14// List of known spammy keywords, please add more here
15
16$autokeywords = array(
17 "<img",
18 "fuck",
19 "casino",
20 "porn",
21 "xxx",
22 "cialis",
23 "viagra",
24 "poker",
25 "warcraft"
26 );
27
28
29/////////////////////////////////////////////////////////////////////////////////
30
31require_once('../../../config.php');
32require_once($CFG->libdir.'/adminlib.php');
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');
f44b10ed
PS
44$PAGE->requires->yui2_lib('json');
45$PAGE->requires->yui2_lib('connection');
2e3f66d7 46
20486a5a 47// Implement some AJAX calls
2e3f66d7 48
49// Delete one user
50if (!empty($del) && confirm_sesskey() && ($id != $USER->id)) {
51 if (isset($SESSION->users_result[$id])) {
52 $user = $SESSION->users_result[$id];
53 if (delete_user($user)) {
54 unset($SESSION->users_result[$id]);
55 echo json_encode(true);
56 } else {
57 echo json_encode(false);
58 }
59 } else {
60 echo json_encode(false);
61 }
62 exit;
63}
64
65// Delete lots of users
66if (!empty($delall) && confirm_sesskey()) {
67 if (!empty($SESSION->users_result)) {
68 foreach ($SESSION->users_result as $userid => $user) {
69 if ($userid != $USER->id) {
70 if (delete_user($user)) {
71 unset($SESSION->users_result[$userid]);
72 }
73 }
74 }
75 }
76 echo json_encode(true);
77 exit;
78}
79
80if (!empty($ignore)) {
81 unset($SESSION->users_result[$id]);
82 echo json_encode(true);
83 exit;
84}
85
86
87admin_externalpage_print_header();
88
89// Print headers and things
90
91print_spamcleaner_javascript();
92
20486a5a 93echo $OUTPUT->box(get_string('spamcleanerintro', 'report_spamcleaner'));
2e3f66d7 94
20486a5a 95echo $OUTPUT->box_start(); // The forms section at the top
2e3f66d7 96
97?>
98
99<div class="mdl-align">
100
6acb6a3d 101<form method="post" action="index.php">
102 <div>
20486a5a 103 <input type="text" name="keyword" id="keyword_el" value="<?php p($keyword) ?>" />
2e3f66d7 104 <input type="hidden" name="sesskey" value="<?php echo sesskey();?>" />
105 <input type="submit" value="<?php echo get_string('spamsearch', 'report_spamcleaner')?>" />
6acb6a3d 106 </div>
2e3f66d7 107</form>
108<p><?php echo get_string('spameg', 'report_spamcleaner');?></p>
109
110<hr />
111
6acb6a3d 112<form method="post" action="index.php">
113 <div>
2e3f66d7 114 <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'report_spamcleaner');?>" />
6acb6a3d 115 </div>
2e3f66d7 116</form>
117
118
119</div>
120
121<?php
20486a5a 122echo $OUTPUT->box_end();
2e3f66d7 123
124echo '<div id="result" class="mdl-align">';
125
126// Print list of resulting profiles
127
128if (!empty($keyword)) { // Use the keyword(s) supplied by the user
129 $keywords = explode(',', $keyword);
130 foreach ($keywords as $key => $keyword) {
131 $keywords[$key] = trim($keyword);
132 }
133 search_spammers($keywords);
134
135} else if (!empty($autodetect)) { // Use the inbuilt keyword list to detect users
136 search_spammers($autokeywords);
137}
138
139echo '</div>';
140
141/////////////////////////////////////////////////////////////////////////////////
142
143
20486a5a 144/// Functions
2e3f66d7 145
146
147function search_spammers($keywords) {
148
20486a5a 149 global $CFG, $USER, $DB, $OUTPUT;
2e3f66d7 150
151 if (!is_array($keywords)) {
152 $keywords = array($keywords); // Make it into an array
153 }
154
155 $like = $DB->sql_ilike();
156
157 $keywordfull = array();
158 foreach ($keywords as $keyword) {
159 $keyword = addslashes($keyword); // Just to be safe
160 $keywordfull[] = " description $like '%$keyword%' ";
161 $keywordfull2[] = " p.summary $like '%$keyword%' ";
162 }
163 $conditions = '( '.implode(' OR ', $keywordfull).' )';
164 $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
165
166 $sql = "SELECT * FROM {user} WHERE deleted = 0 AND id <> {$USER->id} AND $conditions"; // Exclude oneself
167 $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}";
168 $spamusers_desc = $DB->get_recordset_sql($sql);
169 $spamusers_blog = $DB->get_recordset_sql($sql2);
170
171 $keywordlist = implode(', ', $keywords);
20486a5a 172 echo $OUTPUT->box(get_string('spamresult', 'report_spamcleaner').s($keywordlist)).' ...';
2e3f66d7 173
174 print_user_list(array($spamusers_desc, $spamusers_blog), $keywords);
175
176}
177
178
179
180function print_user_list($users_rs, $keywords) {
181 global $CFG, $SESSION;
182
183 // reset session everytime this function is called
184 $SESSION->users_result = array();
185 $count = 0;
186
187 foreach ($users_rs as $rs){
188 foreach ($rs as $user) {
189 if (!$count) {
190 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>';
191 }
192 $count++;
193 filter_user($user, $keywords, $count);
194 }
195 }
196
197 if (!$count) {
198 echo get_string('spamcannotfinduser', 'report_spamcleaner');
199
200 } else {
201 echo '</table>';
202 echo '<div class="mld-align">
203 <button id="removeall_btn">'.get_string('spamdeleteall', 'report_spamcleaner').'</button>
204 </div>';
205 }
206}
207function filter_user($user, $keywords, $count) {
208 global $CFG;
209 $image_search = false;
210 if (in_array('<img', $keywords)) {
211 $image_search = true;
212 }
213 if (isset($user->summary)) {
214 $user->description = '<h3>'.get_string('spamfromblog', 'report_spamcleaner').'</h3>'.$user->summary;
215 unset($user->summary);
216 }
5d3b9994 217 if (preg_match('#<img.*src=[\"\']('.$CFG->wwwroot.')#', $user->description, $matches)
2e3f66d7 218 && $image_search) {
219 $result = false;
220 foreach ($keywords as $keyword) {
221 if (preg_match('#'.$keyword.'#', $user->description)
222 && ($keyword != '<img')) {
223 $result = true;
224 }
225 }
226 if ($result) {
227 echo print_user_entry($user, $keywords, $count);
228 } else {
229 unset($user);
230 }
231 } else {
232 echo print_user_entry($user, $keywords, $count);
233 }
234}
235
236
237function print_user_entry($user, $keywords, $count) {
238
239 global $SESSION, $CFG;
240
241 $smalluserobject = new object; // All we need to delete them later
242 $smalluserobject->id = $user->id;
243 $smalluserobject->email = $user->email;
244 $smalluserobject->auth = $user->auth;
245 $smalluserobject->firstname = $user->firstname;
246 $smalluserobject->lastname = $user->lastname;
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
272 $html .= '<td align="left">'.format_text($user->description, $user->descriptionformat).'</td>';
2e3f66d7 273 $html .= '<td width="100px" align="center">';
274 $html .= '<button onclick="del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
275 $html .= '<button onclick="ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
276 $html .= '</td>';
277 $html .= '</tr>';
278 return $html;
279 } else {
280 return null;
281 }
282
283
284}
285
286function print_spamcleaner_javascript() {
2a29ba7e 287 global $PAGE;
9dec75db 288 $PAGE->requires->js('/admin/report/spamcleaner/spamcleaner.js');
2a29ba7e 289 $strings = Array('spaminvalidresult','spamdeleteallconfirm','spamcannotdelete','spamdeleteconfirm');
290 $PAGE->requires->strings_for_js($strings, 'report_spamcleaner');
291 $PAGE->requires->data_for_js('spamcleaner', Array('me'=>me()));
292 //$sesskey = sesskey();
2e3f66d7 293}
294
73d6f52f 295echo $OUTPUT->footer();