6 * Helps an admin to clean up spam in Moodle
8 * @authors Dongsheng Cai, Martin Dougiamas, Amr Hourani
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 // List of known spammy keywords, please add more here
14 /////////////////////////////////////////////////////////////////////////////////
16 require_once('../../../config.php');
17 require_once($CFG->libdir.'/adminlib.php');
22 $autokeywords = array(
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);
43 admin_externalpage_setup('reportspamcleaner');
46 if (!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);
53 echo json_encode(false);
56 echo json_encode(false);
61 // Delete lots of users
62 if (!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]);
72 echo json_encode(true);
76 if (!empty($ignore)) {
77 unset($SESSION->users_result[$id]);
78 echo json_encode(true);
82 $PAGE->requires->js_init_call('M.report_spamcleaner.init', array(me()), true);
83 $strings = Array('spaminvalidresult','spamdeleteallconfirm','spamcannotdelete','spamdeleteconfirm');
84 $PAGE->requires->strings_for_js($strings, 'report_spamcleaner');
86 echo $OUTPUT->header();
88 // Print headers and things
89 echo $OUTPUT->box(get_string('spamcleanerintro', 'report_spamcleaner'));
91 echo $OUTPUT->box_start(); // The forms section at the top
95 <div class="mdl-align">
97 <form method="post" action="index.php">
99 <input type="text" name="keyword" id="keyword_el" value="<?php p($keyword) ?>" />
100 <input type="hidden" name="sesskey" value="<?php echo sesskey();?>" />
101 <input type="submit" value="<?php echo get_string('spamsearch', 'report_spamcleaner')?>" />
104 <p><?php echo get_string('spameg', 'report_spamcleaner');?></p>
108 <form method="post" action="index.php">
110 <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'report_spamcleaner');?>" />
118 echo $OUTPUT->box_end();
120 echo '<div id="result" class="mdl-align">';
122 // Print list of resulting profiles
124 if (!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);
129 search_spammers($keywords);
131 } else if (!empty($autodetect)) { // Use the inbuilt keyword list to detect users
132 search_spammers($autokeywords);
137 /////////////////////////////////////////////////////////////////////////////////
143 function search_spammers($keywords) {
145 global $CFG, $USER, $DB, $OUTPUT;
147 if (!is_array($keywords)) {
148 $keywords = array($keywords); // Make it into an array
151 $params = array('userid'=>$USER->id);
153 $keywordfull = array();
155 foreach ($keywords as $keyword) {
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%";
162 $conditions = '( '.implode(' OR ', $keywordfull).' )';
163 $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
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";
167 $spamusers_desc = $DB->get_recordset_sql($sql, $params);
168 $spamusers_blog = $DB->get_recordset_sql($sql2, $params);
170 $keywordlist = implode(', ', $keywords);
171 echo $OUTPUT->box(get_string('spamresult', 'report_spamcleaner').s($keywordlist)).' ...';
173 print_user_list(array($spamusers_desc, $spamusers_blog), $keywords);
179 function print_user_list($users_rs, $keywords) {
180 global $CFG, $SESSION;
182 // reset session everytime this function is called
183 $SESSION->users_result = array();
186 foreach ($users_rs as $rs){
187 foreach ($rs as $user) {
189 echo '<table border="1" width="100%" id="data-grid"><tr><th> </th><th>'.get_string('user','admin').'</th><th>'.get_string('spamdesc', 'report_spamcleaner').'</th><th>'.get_string('spamoperation', 'report_spamcleaner').'</th></tr>';
192 filter_user($user, $keywords, $count);
197 echo get_string('spamcannotfinduser', 'report_spamcleaner');
201 echo '<div class="mld-align">
202 <button id="removeall_btn">'.get_string('spamdeleteall', 'report_spamcleaner').'</button>
206 function filter_user($user, $keywords, $count) {
208 $image_search = false;
209 if (in_array('<img', $keywords)) {
210 $image_search = true;
212 if (isset($user->summary)) {
213 $user->description = '<h3>'.get_string('spamfromblog', 'report_spamcleaner').'</h3>'.$user->summary;
214 unset($user->summary);
216 if (preg_match('#<img.*src=[\"\']('.$CFG->wwwroot.')#', $user->description, $matches)
219 foreach ($keywords as $keyword) {
220 if (preg_match('#'.$keyword.'#', $user->description)
221 && ($keyword != '<img')) {
226 echo print_user_entry($user, $keywords, $count);
231 echo print_user_entry($user, $keywords, $count);
236 function print_user_entry($user, $keywords, $count) {
238 global $SESSION, $CFG;
240 $smalluserobject = new stdClass(); // All we need to delete them later
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;
246 $smalluserobject->username = $user->username;
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>';
252 $html .= '<td width="30%" align="left"><a href="'.$CFG->wwwroot."/user/view.php?course=1&id=".$user->id.'" title="'.s($user->username).'">'.fullname($user).'</a>';
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>';
264 foreach ($keywords as $keyword) {
265 $user->description = highlight($keyword, $user->description);
268 if (!isset($user->descriptionformat)) {
269 $user->descriptionformat = FORMAT_MOODLE;
272 $html .= '<td align="left">'.format_text($user->description, $user->descriptionformat, array('overflowdiv'=>true)).'</td>';
273 $html .= '<td width="100px" align="center">';
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>';
286 echo $OUTPUT->footer();