MDL-24321 switching to stdClass in /mod/
[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();
154 foreach ($keywords as $keyword) {
323f79a3 155 $keywordfull[] = $DB->sql_like('description', ':descpat', false);
4287c1c2 156 $params['descpat'] = "%$keyword%";
323f79a3 157 $keywordfull2[] = $DB->sql_like('p.summary', ':sumpat', false);
4287c1c2 158 $params['sumpat'] = "%$keyword%";
2e3f66d7 159 }
160 $conditions = '( '.implode(' OR ', $keywordfull).' )';
161 $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
162
4287c1c2
PS
163 $sql = "SELECT * FROM {user} WHERE deleted = 0 AND id <> :userid 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 <> :userid";
2e3f66d7 165 $spamusers_desc = $DB->get_recordset_sql($sql);
166 $spamusers_blog = $DB->get_recordset_sql($sql2);
167
168 $keywordlist = implode(', ', $keywords);
20486a5a 169 echo $OUTPUT->box(get_string('spamresult', 'report_spamcleaner').s($keywordlist)).' ...';
2e3f66d7 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 }
5d3b9994 214 if (preg_match('#<img.*src=[\"\']('.$CFG->wwwroot.')#', $user->description, $matches)
2e3f66d7 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;
9edea336 244 $smalluserobject->username = $user->username;
2e3f66d7 245
246 if (empty($SESSION->users_result[$user->id])) {
247 $SESSION->users_result[$user->id] = $smalluserobject;
248 $html = '<tr valign="top" id="row-'.$user->id.'" class="result-row">';
249 $html .= '<td width="10">'.$count.'</td>';
209c122b 250 $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 251
252 $html .= "<ul>";
253 $profile_set = array('city'=>true, 'country'=>true, 'email'=>true);
254 foreach ($profile_set as $key=>$value) {
255 if (isset($user->$key)){
256 $html .= '<li>'.$user->$key.'</li>';
257 }
258 }
259 $html .= "</ul>";
260 $html .= '</td>';
261
262 foreach ($keywords as $keyword) {
1e12b912 263 $user->description = highlight($keyword, $user->description);
2e3f66d7 264 }
265
8bdc9cac
SH
266 if (!isset($user->descriptionformat)) {
267 $user->descriptionformat = FORMAT_MOODLE;
268 }
269
270 $html .= '<td align="left">'.format_text($user->description, $user->descriptionformat).'</td>';
2e3f66d7 271 $html .= '<td width="100px" align="center">';
9edea336
PS
272 $html .= '<button onclick="M.report_spamcleaner.del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
273 $html .= '<button onclick="M.report_spamcleaner.ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
2e3f66d7 274 $html .= '</td>';
275 $html .= '</tr>';
276 return $html;
277 } else {
278 return null;
279 }
280
281
282}
283
73d6f52f 284echo $OUTPUT->footer();