PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/spamcleaner/index.php

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