PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/spamcleaner/index.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 336 lines | 256 code | 62 blank | 18 comment | 46 complexity | c86fee83b8053ef2d5242d4d8362ec85 MD5 | raw file
  1. <?php
  2. /**
  3. * Spam Cleaner
  4. *
  5. * Helps an admin to clean up spam in Moodle
  6. *
  7. * @authors Dongsheng Cai, Martin Dougiamas, Amr Hourani
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  9. */
  10. // List of known spammy keywords, please add more here
  11. /////////////////////////////////////////////////////////////////////////////////
  12. require_once('../../../config.php');
  13. require_once($CFG->libdir.'/adminlib.php');
  14. // Configuration
  15. $autokeywords = array(
  16. "<img",
  17. "fuck",
  18. "casino",
  19. "porn",
  20. "xxx",
  21. "cialis",
  22. "viagra",
  23. "poker",
  24. "warcraft"
  25. );
  26. $keyword = optional_param('keyword', '', PARAM_RAW);
  27. $autodetect = optional_param('autodetect', '', PARAM_RAW);
  28. $del = optional_param('del', '', PARAM_RAW);
  29. $delall = optional_param('delall', '', PARAM_RAW);
  30. $ignore = optional_param('ignore', '', PARAM_RAW);
  31. $reset = optional_param('reset', '', PARAM_RAW);
  32. $id = optional_param('id', '', PARAM_INT);
  33. require_login();
  34. admin_externalpage_setup('toolspamcleaner');
  35. // Delete one user
  36. if (!empty($del) && confirm_sesskey() && ($id != $USER->id)) {
  37. if (isset($SESSION->users_result[$id])) {
  38. $user = $SESSION->users_result[$id];
  39. if (delete_user($user)) {
  40. unset($SESSION->users_result[$id]);
  41. echo json_encode(true);
  42. } else {
  43. echo json_encode(false);
  44. }
  45. } else {
  46. echo json_encode(false);
  47. }
  48. exit;
  49. }
  50. // Delete lots of users
  51. if (!empty($delall) && confirm_sesskey()) {
  52. if (!empty($SESSION->users_result)) {
  53. foreach ($SESSION->users_result as $userid => $user) {
  54. if ($userid != $USER->id) {
  55. if (delete_user($user)) {
  56. unset($SESSION->users_result[$userid]);
  57. }
  58. }
  59. }
  60. }
  61. echo json_encode(true);
  62. exit;
  63. }
  64. if (!empty($ignore)) {
  65. unset($SESSION->users_result[$id]);
  66. echo json_encode(true);
  67. exit;
  68. }
  69. $PAGE->requires->js_init_call('M.tool_spamcleaner.init', array(me()), true);
  70. $strings = Array('spaminvalidresult','spamdeleteallconfirm','spamcannotdelete','spamdeleteconfirm');
  71. $PAGE->requires->strings_for_js($strings, 'tool_spamcleaner');
  72. echo $OUTPUT->header();
  73. // Print headers and things
  74. echo $OUTPUT->box(get_string('spamcleanerintro', 'tool_spamcleaner'));
  75. echo $OUTPUT->box_start(); // The forms section at the top
  76. ?>
  77. <div class="mdl-align">
  78. <form method="post" action="index.php">
  79. <div>
  80. <label class="accesshide" for="keyword_el"><?php print_string('spamkeyword', 'tool_spamcleaner') ?></label>
  81. <input type="text" name="keyword" id="keyword_el" value="<?php p($keyword) ?>" />
  82. <input type="hidden" name="sesskey" value="<?php echo sesskey();?>" />
  83. <input type="submit" value="<?php echo get_string('spamsearch', 'tool_spamcleaner')?>" />
  84. </div>
  85. </form>
  86. <p><?php echo get_string('spameg', 'tool_spamcleaner');?></p>
  87. <hr />
  88. <form method="post" action="index.php">
  89. <div>
  90. <input type="submit" name="autodetect" value="<?php echo get_string('spamauto', 'tool_spamcleaner');?>" />
  91. </div>
  92. </form>
  93. </div>
  94. <?php
  95. echo $OUTPUT->box_end();
  96. echo '<div id="result" class="mdl-align">';
  97. // Print list of resulting profiles
  98. if (!empty($keyword)) { // Use the keyword(s) supplied by the user
  99. $keywords = explode(',', $keyword);
  100. foreach ($keywords as $key => $keyword) {
  101. $keywords[$key] = trim($keyword);
  102. }
  103. search_spammers($keywords);
  104. } else if (!empty($autodetect)) { // Use the inbuilt keyword list to detect users
  105. search_spammers($autokeywords);
  106. }
  107. echo '</div>';
  108. /////////////////////////////////////////////////////////////////////////////////
  109. /// Functions
  110. function search_spammers($keywords) {
  111. global $CFG, $USER, $DB, $OUTPUT;
  112. if (!is_array($keywords)) {
  113. $keywords = array($keywords); // Make it into an array
  114. }
  115. $params = array('userid'=>$USER->id);
  116. $keywordfull = array();
  117. $i = 0;
  118. foreach ($keywords as $keyword) {
  119. $keywordfull[] = $DB->sql_like('description', ':descpat'.$i, false);
  120. $params['descpat'.$i] = "%$keyword%";
  121. $keywordfull2[] = $DB->sql_like('p.summary', ':sumpat'.$i, false);
  122. $params['sumpat'.$i] = "%$keyword%";
  123. $keywordfull3[] = $DB->sql_like('p.subject', ':subpat'.$i, false);
  124. $params['subpat'.$i] = "%$keyword%";
  125. $keywordfull4[] = $DB->sql_like('c.content', ':contpat'.$i, false);
  126. $params['contpat'.$i] = "%$keyword%";
  127. $keywordfull5[] = $DB->sql_like('m.fullmessage', ':msgpat'.$i, false);
  128. $params['msgpat'.$i] = "%$keyword%";
  129. $keywordfull6[] = $DB->sql_like('fp.message', ':forumpostpat'.$i, false);
  130. $params['forumpostpat'.$i] = "%$keyword%";
  131. $keywordfull7[] = $DB->sql_like('fp.subject', ':forumpostsubpat'.$i, false);
  132. $params['forumpostsubpat'.$i] = "%$keyword%";
  133. $i++;
  134. }
  135. $conditions = '( '.implode(' OR ', $keywordfull).' )';
  136. $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
  137. $conditions3 = '( '.implode(' OR ', $keywordfull3).' )';
  138. $conditions4 = '( '.implode(' OR ', $keywordfull4).' )';
  139. $conditions5 = '( '.implode(' OR ', $keywordfull5).' )';
  140. $conditions6 = '( '.implode(' OR ', $keywordfull6).' )';
  141. $conditions7 = '( '.implode(' OR ', $keywordfull7).' )';
  142. $sql = "SELECT * FROM {user} WHERE deleted = 0 AND id <> :userid AND $conditions"; // Exclude oneself
  143. $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";
  144. $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";
  145. $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";
  146. $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";
  147. $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";
  148. $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";
  149. $spamusers_desc = $DB->get_recordset_sql($sql, $params);
  150. $spamusers_blog = $DB->get_recordset_sql($sql2, $params);
  151. $spamusers_blogsub = $DB->get_recordset_sql($sql3, $params);
  152. $spamusers_comment = $DB->get_recordset_sql($sql4, $params);
  153. $spamusers_message = $DB->get_recordset_sql($sql5, $params);
  154. $spamusers_forumpost = $DB->get_recordset_sql($sql6, $params);
  155. $spamusers_forumpostsub = $DB->get_recordset_sql($sql7, $params);
  156. $keywordlist = implode(', ', $keywords);
  157. echo $OUTPUT->box(get_string('spamresult', 'tool_spamcleaner').s($keywordlist)).' ...';
  158. print_user_list(array($spamusers_desc,
  159. $spamusers_blog,
  160. $spamusers_blogsub,
  161. $spamusers_comment,
  162. $spamusers_message,
  163. $spamusers_forumpost,
  164. $spamusers_forumpostsub
  165. ),
  166. $keywords);
  167. }
  168. function print_user_list($users_rs, $keywords) {
  169. global $CFG, $SESSION;
  170. // reset session everytime this function is called
  171. $SESSION->users_result = array();
  172. $count = 0;
  173. foreach ($users_rs as $rs){
  174. foreach ($rs as $user) {
  175. if (!$count) {
  176. 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>';
  177. }
  178. $count++;
  179. filter_user($user, $keywords, $count);
  180. }
  181. }
  182. if (!$count) {
  183. echo get_string('spamcannotfinduser', 'tool_spamcleaner');
  184. } else {
  185. echo '</table>';
  186. echo '<div class="mld-align">
  187. <button id="removeall_btn">'.get_string('spamdeleteall', 'tool_spamcleaner').'</button>
  188. </div>';
  189. }
  190. }
  191. function filter_user($user, $keywords, $count) {
  192. global $CFG;
  193. $image_search = false;
  194. if (in_array('<img', $keywords)) {
  195. $image_search = true;
  196. }
  197. if (isset($user->summary)) {
  198. $user->description = '<h3>'.get_string('spamfromblog', 'tool_spamcleaner').'</h3>'.$user->summary;
  199. unset($user->summary);
  200. } else if (isset($user->postsubject)) {
  201. $user->description = '<h3>'.get_string('spamfromblog', 'tool_spamcleaner').'</h3>'.$user->postsubject;
  202. unset($user->postsubject);
  203. } else if (isset($user->content)) {
  204. $user->description = '<h3>'.get_string('spamfromcomments', 'tool_spamcleaner').'</h3>'.$user->content;
  205. unset($user->content);
  206. } else if (isset($user->fullmessage)) {
  207. $user->description = '<h3>'.get_string('spamfrommessages', 'tool_spamcleaner').'</h3>'.$user->fullmessage;
  208. unset($user->fullmessage);
  209. } else if (isset($user->message)) {
  210. $user->description = '<h3>'.get_string('spamfromforumpost', 'tool_spamcleaner').'</h3>'.$user->message;
  211. unset($user->message);
  212. } else if (isset($user->subject)) {
  213. $user->description = '<h3>'.get_string('spamfromforumpost', 'tool_spamcleaner').'</h3>'.$user->subject;
  214. unset($user->subject);
  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. }
  233. }
  234. function print_user_entry($user, $keywords, $count) {
  235. global $SESSION, $CFG;
  236. $smalluserobject = new stdClass(); // All we need to delete them later
  237. $smalluserobject->id = $user->id;
  238. $smalluserobject->email = $user->email;
  239. $smalluserobject->auth = $user->auth;
  240. $smalluserobject->firstname = $user->firstname;
  241. $smalluserobject->lastname = $user->lastname;
  242. $smalluserobject->username = $user->username;
  243. if (empty($SESSION->users_result[$user->id])) {
  244. $SESSION->users_result[$user->id] = $smalluserobject;
  245. $html = '<tr valign="top" id="row-'.$user->id.'" class="result-row">';
  246. $html .= '<td width="10">'.$count.'</td>';
  247. $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>';
  248. $html .= "<ul>";
  249. $profile_set = array('city'=>true, 'country'=>true, 'email'=>true);
  250. foreach ($profile_set as $key=>$value) {
  251. if (isset($user->$key)){
  252. $html .= '<li>'.$user->$key.'</li>';
  253. }
  254. }
  255. $html .= "</ul>";
  256. $html .= '</td>';
  257. foreach ($keywords as $keyword) {
  258. $user->description = highlight($keyword, $user->description);
  259. }
  260. if (!isset($user->descriptionformat)) {
  261. $user->descriptionformat = FORMAT_MOODLE;
  262. }
  263. $html .= '<td align="left">'.format_text($user->description, $user->descriptionformat, array('overflowdiv'=>true)).'</td>';
  264. $html .= '<td width="100px" align="center">';
  265. $html .= '<button onclick="M.tool_spamcleaner.del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
  266. $html .= '<button onclick="M.tool_spamcleaner.ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
  267. $html .= '</td>';
  268. $html .= '</tr>';
  269. return $html;
  270. } else {
  271. return null;
  272. }
  273. }
  274. echo $OUTPUT->footer();