Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/spamcleaner/index.php
Go to the documentation of this file.
00001 <?php
00002 
00012 // List of known spammy keywords, please add more here
00013 
00015 
00016 require_once('../../../config.php');
00017 require_once($CFG->libdir.'/adminlib.php');
00018 
00019 
00020 // Configuration
00021 
00022 $autokeywords = array(
00023                     "<img",
00024                     "fuck",
00025                     "casino",
00026                     "porn",
00027                     "xxx",
00028                     "cialis",
00029                     "viagra",
00030                     "poker",
00031                     "warcraft"
00032                 );
00033 
00034 $keyword = optional_param('keyword', '', PARAM_RAW);
00035 $autodetect = optional_param('autodetect', '', PARAM_RAW);
00036 $del = optional_param('del', '', PARAM_RAW);
00037 $delall = optional_param('delall', '', PARAM_RAW);
00038 $ignore = optional_param('ignore', '', PARAM_RAW);
00039 $reset = optional_param('reset', '', PARAM_RAW);
00040 $id = optional_param('id', '', PARAM_INT);
00041 
00042 require_login();
00043 admin_externalpage_setup('toolspamcleaner');
00044 
00045 // Delete one user
00046 if (!empty($del) && confirm_sesskey() && ($id != $USER->id)) {
00047     if (isset($SESSION->users_result[$id])) {
00048         $user = $SESSION->users_result[$id];
00049         if (delete_user($user)) {
00050             unset($SESSION->users_result[$id]);
00051             echo json_encode(true);
00052         } else {
00053             echo json_encode(false);
00054         }
00055     } else {
00056         echo json_encode(false);
00057     }
00058     exit;
00059 }
00060 
00061 // Delete lots of users
00062 if (!empty($delall) && confirm_sesskey()) {
00063     if (!empty($SESSION->users_result)) {
00064         foreach ($SESSION->users_result as $userid => $user) {
00065             if ($userid != $USER->id) {
00066                 if (delete_user($user)) {
00067                     unset($SESSION->users_result[$userid]);
00068                 }
00069             }
00070         }
00071     }
00072     echo json_encode(true);
00073     exit;
00074 }
00075 
00076 if (!empty($ignore)) {
00077     unset($SESSION->users_result[$id]);
00078     echo json_encode(true);
00079     exit;
00080 }
00081 
00082 $PAGE->requires->js_init_call('M.tool_spamcleaner.init', array(me()), true);
00083 $strings = Array('spaminvalidresult','spamdeleteallconfirm','spamcannotdelete','spamdeleteconfirm');
00084 $PAGE->requires->strings_for_js($strings, 'tool_spamcleaner');
00085 
00086 echo $OUTPUT->header();
00087 
00088 // Print headers and things
00089 echo $OUTPUT->box(get_string('spamcleanerintro', 'tool_spamcleaner'));
00090 
00091 echo $OUTPUT->box_start();     // The forms section at the top
00092 
00093 ?>
00094 
00095 <div class="mdl-align">
00096 
00097 <form method="post" action="index.php">
00098   <div>
00099     <input type="text" name="keyword" id="keyword_el" value="<?php p($keyword) ?>" />
00100     <input type="hidden" name="sesskey" value="<?php echo sesskey();?>" />
00101     <input type="submit" value="<?php echo get_string('spamsearch', 'tool_spamcleaner')?>" />
00102   </div>
00103 </form>
00104 <p><?php echo get_string('spameg', 'tool_spamcleaner');?></p>
00105 
00106 <hr />
00107 
00108 <form method="post"  action="index.php">
00109   <div>
00110     <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'tool_spamcleaner');?>" />
00111   </div>
00112 </form>
00113 
00114 
00115 </div>
00116 
00117 <?php
00118 echo $OUTPUT->box_end();
00119 
00120 echo '<div id="result" class="mdl-align">';
00121 
00122 // Print list of resulting profiles
00123 
00124 if (!empty($keyword)) {               // Use the keyword(s) supplied by the user
00125     $keywords = explode(',', $keyword);
00126     foreach ($keywords as $key => $keyword) {
00127         $keywords[$key] = trim($keyword);
00128     }
00129     search_spammers($keywords);
00130 
00131 } else if (!empty($autodetect)) {     // Use the inbuilt keyword list to detect users
00132     search_spammers($autokeywords);
00133 }
00134 
00135 echo '</div>';
00136 
00138 
00139 
00141 
00142 
00143 function search_spammers($keywords) {
00144 
00145     global $CFG, $USER, $DB, $OUTPUT;
00146 
00147     if (!is_array($keywords)) {
00148         $keywords = array($keywords);    // Make it into an array
00149     }
00150 
00151     $params = array('userid'=>$USER->id);
00152 
00153     $keywordfull = array();
00154     $i = 0;
00155     foreach ($keywords as $keyword) {
00156         $keywordfull[] = $DB->sql_like('description', ':descpat'.$i, false);
00157         $params['descpat'.$i] = "%$keyword%";
00158         $keywordfull2[] = $DB->sql_like('p.summary', ':sumpat'.$i, false);
00159         $params['sumpat'.$i] = "%$keyword%";
00160         $keywordfull3[] = $DB->sql_like('p.subject', ':subpat'.$i, false);
00161         $params['subpat'.$i] = "%$keyword%";
00162         $keywordfull4[] = $DB->sql_like('c.content', ':contpat'.$i, false);
00163         $params['contpat'.$i] = "%$keyword%";
00164         $keywordfull5[] = $DB->sql_like('m.fullmessage', ':msgpat'.$i, false);
00165         $params['msgpat'.$i] = "%$keyword%";
00166         $keywordfull6[] = $DB->sql_like('fp.message', ':forumpostpat'.$i, false);
00167         $params['forumpostpat'.$i] = "%$keyword%";
00168         $keywordfull7[] = $DB->sql_like('fp.subject', ':forumpostsubpat'.$i, false);
00169         $params['forumpostsubpat'.$i] = "%$keyword%";
00170         $i++;
00171     }
00172     $conditions = '( '.implode(' OR ', $keywordfull).' )';
00173     $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
00174     $conditions3 = '( '.implode(' OR ', $keywordfull3).' )';
00175     $conditions4 = '( '.implode(' OR ', $keywordfull4).' )';
00176     $conditions5 = '( '.implode(' OR ', $keywordfull5).' )';
00177     $conditions6 = '( '.implode(' OR ', $keywordfull6).' )';
00178     $conditions7 = '( '.implode(' OR ', $keywordfull7).' )';
00179 
00180     $sql  = "SELECT * FROM {user} WHERE deleted = 0 AND id <> :userid AND $conditions";  // Exclude oneself
00181     $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";
00182     $sql3 = "SELECT u.*, p.subject as postsubject FROM {user} AS u, {post} AS p WHERE $conditions3 AND u.deleted = 0 AND u.id=p.userid AND u.id <> :userid";
00183     $sql4 = "SELECT u.*, c.content FROM {user} AS u, {comments} AS c WHERE $conditions4 AND u.deleted = 0 AND u.id=c.userid AND u.id <> :userid";
00184     $sql5 = "SELECT u.*, m.fullmessage FROM {user} AS u, {message} AS m WHERE $conditions5 AND u.deleted = 0 AND u.id=m.useridfrom AND u.id <> :userid";
00185     $sql6 = "SELECT u.*, fp.message FROM {user} AS u, {forum_posts} AS fp WHERE $conditions6 AND u.deleted = 0 AND u.id=fp.userid AND u.id <> :userid";
00186     $sql7 = "SELECT u.*, fp.subject FROM {user} AS u, {forum_posts} AS fp WHERE $conditions7 AND u.deleted = 0 AND u.id=fp.userid AND u.id <> :userid";
00187 
00188     $spamusers_desc = $DB->get_recordset_sql($sql, $params);
00189     $spamusers_blog = $DB->get_recordset_sql($sql2, $params);
00190     $spamusers_blogsub = $DB->get_recordset_sql($sql3, $params);
00191     $spamusers_comment = $DB->get_recordset_sql($sql4, $params);
00192     $spamusers_message = $DB->get_recordset_sql($sql5, $params);
00193     $spamusers_forumpost = $DB->get_recordset_sql($sql6, $params);
00194     $spamusers_forumpostsub = $DB->get_recordset_sql($sql7, $params);
00195 
00196     $keywordlist = implode(', ', $keywords);
00197     echo $OUTPUT->box(get_string('spamresult', 'tool_spamcleaner').s($keywordlist)).' ...';
00198 
00199     print_user_list(array($spamusers_desc,
00200                           $spamusers_blog,
00201                           $spamusers_blogsub,
00202                           $spamusers_comment,
00203                           $spamusers_message,
00204                           $spamusers_forumpost,
00205                           $spamusers_forumpostsub
00206                          ),
00207                          $keywords);
00208 }
00209 
00210 
00211 
00212 function print_user_list($users_rs, $keywords) {
00213     global $CFG, $SESSION;
00214 
00215     // reset session everytime this function is called
00216     $SESSION->users_result = array();
00217     $count = 0;
00218 
00219     foreach ($users_rs as $rs){
00220         foreach ($rs as $user) {
00221             if (!$count) {
00222                 echo '<table border="1" width="100%" id="data-grid"><tr><th>&nbsp;</th><th>'.get_string('user','admin').'</th><th>'.get_string('spamdesc', 'tool_spamcleaner').'</th><th>'.get_string('spamoperation', 'tool_spamcleaner').'</th></tr>';
00223             }
00224             $count++;
00225             filter_user($user, $keywords, $count);
00226         }
00227     }
00228 
00229     if (!$count) {
00230         echo get_string('spamcannotfinduser', 'tool_spamcleaner');
00231 
00232     } else {
00233         echo '</table>';
00234         echo '<div class="mld-align">
00235               <button id="removeall_btn">'.get_string('spamdeleteall', 'tool_spamcleaner').'</button>
00236               </div>';
00237     }
00238 }
00239 function filter_user($user, $keywords, $count) {
00240     global $CFG;
00241     $image_search = false;
00242     if (in_array('<img', $keywords)) {
00243         $image_search = true;
00244     }
00245     if (isset($user->summary)) {
00246         $user->description = '<h3>'.get_string('spamfromblog', 'tool_spamcleaner').'</h3>'.$user->summary;
00247         unset($user->summary);
00248     } else if (isset($user->postsubject)) {
00249         $user->description = '<h3>'.get_string('spamfromblog', 'tool_spamcleaner').'</h3>'.$user->postsubject;
00250         unset($user->postsubject);
00251     } else if (isset($user->content)) {
00252         $user->description = '<h3>'.get_string('spamfromcomments', 'tool_spamcleaner').'</h3>'.$user->content;
00253         unset($user->content);
00254     } else if (isset($user->fullmessage)) {
00255         $user->description = '<h3>'.get_string('spamfrommessages', 'tool_spamcleaner').'</h3>'.$user->fullmessage;
00256         unset($user->fullmessage);
00257     } else if (isset($user->message)) {
00258         $user->description = '<h3>'.get_string('spamfromforumpost', 'tool_spamcleaner').'</h3>'.$user->message;
00259         unset($user->message);
00260     } else if (isset($user->subject)) {
00261         $user->description = '<h3>'.get_string('spamfromforumpost', 'tool_spamcleaner').'</h3>'.$user->subject;
00262         unset($user->subject);
00263     }
00264 
00265     if (preg_match('#<img.*src=[\"\']('.$CFG->wwwroot.')#', $user->description, $matches)
00266         && $image_search) {
00267         $result = false;
00268         foreach ($keywords as $keyword) {
00269             if (preg_match('#'.$keyword.'#', $user->description)
00270                 && ($keyword != '<img')) {
00271                 $result = true;
00272             }
00273         }
00274         if ($result) {
00275             echo print_user_entry($user, $keywords, $count);
00276         } else {
00277             unset($user);
00278         }
00279     } else {
00280         echo print_user_entry($user, $keywords, $count);
00281     }
00282 }
00283 
00284 
00285 function print_user_entry($user, $keywords, $count) {
00286 
00287     global $SESSION, $CFG;
00288 
00289     $smalluserobject = new stdClass();      // All we need to delete them later
00290     $smalluserobject->id = $user->id;
00291     $smalluserobject->email = $user->email;
00292     $smalluserobject->auth = $user->auth;
00293     $smalluserobject->firstname = $user->firstname;
00294     $smalluserobject->lastname = $user->lastname;
00295     $smalluserobject->username = $user->username;
00296 
00297     if (empty($SESSION->users_result[$user->id])) {
00298         $SESSION->users_result[$user->id] = $smalluserobject;
00299         $html = '<tr valign="top" id="row-'.$user->id.'" class="result-row">';
00300         $html .= '<td width="10">'.$count.'</td>';
00301         $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>';
00302 
00303         $html .= "<ul>";
00304         $profile_set = array('city'=>true, 'country'=>true, 'email'=>true);
00305         foreach ($profile_set as $key=>$value) {
00306             if (isset($user->$key)){
00307                 $html .= '<li>'.$user->$key.'</li>';
00308             }
00309         }
00310         $html .= "</ul>";
00311         $html .= '</td>';
00312 
00313         foreach ($keywords as $keyword) {
00314             $user->description = highlight($keyword, $user->description);
00315         }
00316 
00317         if (!isset($user->descriptionformat)) {
00318             $user->descriptionformat = FORMAT_MOODLE;
00319         }
00320 
00321         $html .= '<td align="left">'.format_text($user->description, $user->descriptionformat, array('overflowdiv'=>true)).'</td>';
00322         $html .= '<td width="100px" align="center">';
00323         $html .= '<button onclick="M.tool_spamcleaner.del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
00324         $html .= '<button onclick="M.tool_spamcleaner.ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
00325         $html .= '</td>';
00326         $html .= '</tr>';
00327         return $html;
00328     } else {
00329         return null;
00330     }
00331 
00332 
00333 }
00334 
00335 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations