b5f1880031fac52a9222b5e05e8bed7220eab557
[moodle.git] / admin / report / spamcleaner / index.php
1 <?php
3 /**
4  * Spam Cleaner
5  *
6  * Helps an admin to clean up spam in Moodle
7  *
8  * @authors Dongsheng Cai, Martin Dougiamas, Amr Hourani
9  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10  */
12 // List of known spammy keywords, please add more here
14 /////////////////////////////////////////////////////////////////////////////////
16 require_once('../../../config.php');
17 require_once($CFG->libdir.'/adminlib.php');
20 // Configuration
22 $autokeywords = array(
23                     "<img",
24                     "fuck",
25                     "casino",
26                     "porn",
27                     "xxx",
28                     "cialis",
29                     "viagra",
30                     "poker",
31                     "warcraft"
32                 );
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);
42 require_login();
43 admin_externalpage_setup('reportspamcleaner');
45 // Delete one user
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);
52         } else {
53             echo json_encode(false);
54         }
55     } else {
56         echo json_encode(false);
57     }
58     exit;
59 }
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]);
68                 }
69             }
70         }
71     }
72     echo json_encode(true);
73     exit;
74 }
76 if (!empty($ignore)) {
77     unset($SESSION->users_result[$id]);
78     echo json_encode(true);
79     exit;
80 }
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
93 ?>
95 <div class="mdl-align">
97 <form method="post" action="index.php">
98   <div>
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')?>" />
102   </div>
103 </form>
104 <p><?php echo get_string('spameg', 'report_spamcleaner');?></p>
106 <hr />
108 <form method="post"  action="index.php">
109   <div>
110     <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'report_spamcleaner');?>" />
111   </div>
112 </form>
115 </div>
117 <?php
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);
128     }
129     search_spammers($keywords);
131 } else if (!empty($autodetect)) {     // Use the inbuilt keyword list to detect users
132     search_spammers($autokeywords);
135 echo '</div>';
137 /////////////////////////////////////////////////////////////////////////////////
140 ///  Functions
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
149     }
151     $params = array('userid'=>$USER->id);
153     $keywordfull = array();
154     $i = 0;
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%";
160         $i++;
161     }
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();
184     $count = 0;
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     }
196     if (!$count) {
197         echo get_string('spamcannotfinduser', 'report_spamcleaner');
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     }
206 function 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     }
216     if (preg_match('#<img.*src=[\"\']('.$CFG->wwwroot.')#', $user->description, $matches)
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     }
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&amp;id=".$user->id.'" title="'.s($user->username).'">'.fullname($user).'</a>';
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>';
264         foreach ($keywords as $keyword) {
265             $user->description = highlight($keyword, $user->description);
266         }
268         if (!isset($user->descriptionformat)) {
269             $user->descriptionformat = FORMAT_MOODLE;
270         }
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>';
276         $html .= '</td>';
277         $html .= '</tr>';
278         return $html;
279     } else {
280         return null;
281     }
286 echo $OUTPUT->footer();