PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/report/spamcleaner/index.php

https://github.com/henriquecrang/e-UNI
PHP | 373 lines | 281 code | 70 blank | 22 comment | 49 complexity | 36ea3c973baff68172a39192347c4491 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Spam Cleaner
  4. *
  5. * Helps an admin to clean up spam in Moodle
  6. *
  7. * @version $Id: index.php,v 1.1.2.6 2009/09/22 17:11:02 tjhunt Exp $
  8. * @authors Dongsheng Cai, Martin Dougiamas, Amr Hourani
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  10. */
  11. // Configuration
  12. // List of known spammy keywords, please add more here
  13. $autokeywords = array(
  14. "<img",
  15. "fuck",
  16. "casino",
  17. "porn",
  18. "xxx",
  19. "cialis",
  20. "viagra",
  21. "poker",
  22. "warcraft"
  23. );
  24. /////////////////////////////////////////////////////////////////////////////////
  25. require_once('../../../config.php');
  26. require_once($CFG->libdir.'/adminlib.php');
  27. require_js(array('yui_dom-event', 'yui_connection', 'yui_json'));
  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('reportspamcleaner');
  37. // Implement some AJAX calls
  38. // Delete one user
  39. if (!empty($del) && confirm_sesskey() && ($id != $USER->id)) {
  40. if (isset($SESSION->users_result[$id])) {
  41. $user = $SESSION->users_result[$id];
  42. if (delete_user($user)) {
  43. unset($SESSION->users_result[$id]);
  44. echo json_encode(true);
  45. } else {
  46. echo json_encode(false);
  47. }
  48. } else {
  49. echo json_encode(false);
  50. }
  51. exit;
  52. }
  53. // Delete lots of users
  54. if (!empty($delall) && confirm_sesskey()) {
  55. if (!empty($SESSION->users_result)) {
  56. foreach ($SESSION->users_result as $userid => $user) {
  57. if ($userid != $USER->id) {
  58. if (delete_user($user)) {
  59. unset($SESSION->users_result[$userid]);
  60. }
  61. }
  62. }
  63. }
  64. echo json_encode(true);
  65. exit;
  66. }
  67. if (!empty($ignore)) {
  68. unset($SESSION->users_result[$id]);
  69. echo json_encode(true);
  70. exit;
  71. }
  72. admin_externalpage_print_header();
  73. // Print headers and things
  74. print_spamcleaner_javascript();
  75. print_box(get_string('spamcleanerintro', 'report_spamcleaner'));
  76. print_box_start(); // The forms section at the top
  77. ?>
  78. <div class="mdl-align">
  79. <form method="post" action="index.php">
  80. <div>
  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', 'report_spamcleaner')?>" />
  84. </div>
  85. </form>
  86. <p><?php echo get_string('spameg', 'report_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', 'report_spamcleaner');?>" />
  91. </div>
  92. </form>
  93. </div>
  94. <?php
  95. print_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;
  112. if (!is_array($keywords)) {
  113. $keywords = array($keywords); // Make it into an array
  114. }
  115. $like = sql_ilike();
  116. $keywordfull = array();
  117. foreach ($keywords as $keyword) {
  118. $keyword = addslashes($keyword); // Just to be safe
  119. $keywordfull[] = " description $like '%$keyword%' ";
  120. $keywordfull2[] = " p.summary $like '%$keyword%' ";
  121. }
  122. $conditions = '( '.implode(' OR ', $keywordfull).' )';
  123. $conditions2 = '( '.implode(' OR ', $keywordfull2).' )';
  124. $sql = "SELECT * FROM {$CFG->prefix}user WHERE deleted = 0 AND id <> {$USER->id} AND $conditions"; // Exclude oneself
  125. $sql2= "SELECT u.*, p.summary FROM {$CFG->prefix}user AS u, {$CFG->prefix}post AS p WHERE $conditions2 AND u.deleted = 0 AND u.id=p.userid AND u.id <> {$USER->id}";
  126. $spamusers_desc = get_recordset_sql($sql);
  127. $spamusers_blog = get_recordset_sql($sql2);
  128. $keywordlist = implode(', ', $keywords);
  129. print_box(get_string('spamresult', 'report_spamcleaner').s($keywordlist)).' ...';
  130. print_user_list(array($spamusers_desc, $spamusers_blog), $keywords);
  131. }
  132. function print_user_list($users_rs, $keywords) {
  133. global $CFG, $SESSION;
  134. // reset session everytime this function is called
  135. $SESSION->users_result = array();
  136. $count = 0;
  137. foreach ($users_rs as $rs) {
  138. while ($user = rs_fetch_next_record($rs)) {
  139. if (!$count) {
  140. 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>';
  141. }
  142. $count++;
  143. filter_user($user, $keywords, $count);
  144. }
  145. }
  146. if (!$count) {
  147. echo get_string('spamcannotfinduser', 'report_spamcleaner');
  148. } else {
  149. echo '</table>';
  150. echo '<div class="mld-align">
  151. <button id="removeall_btn">'.get_string('spamdeleteall', 'report_spamcleaner').'</button>
  152. </div>';
  153. }
  154. }
  155. function filter_user($user, $keywords, $count) {
  156. global $CFG;
  157. $image_search = false;
  158. if (in_array('<img', $keywords)) {
  159. $image_search = true;
  160. }
  161. if (isset($user->summary)) {
  162. $user->description = '<h3>'.get_string('spamfromblog', 'report_spamcleaner').'</h3>'.$user->summary;
  163. unset($user->summary);
  164. }
  165. if (preg_match('#<img.*src=[\"\']('.$CFG->pixpath.')#', $user->description, $matches)
  166. && $image_search) {
  167. $result = false;
  168. foreach ($keywords as $keyword) {
  169. if (preg_match('#'.$keyword.'#', $user->description)
  170. && ($keyword != '<img')) {
  171. $result = true;
  172. }
  173. }
  174. if ($result) {
  175. echo print_user_entry($user, $keywords, $count);
  176. } else {
  177. unset($user);
  178. }
  179. } else {
  180. echo print_user_entry($user, $keywords, $count);
  181. }
  182. }
  183. function print_user_entry($user, $keywords, $count) {
  184. global $SESSION, $CFG;
  185. $smalluserobject = new object; // All we need to delete them later
  186. $smalluserobject->id = $user->id;
  187. $smalluserobject->email = $user->email;
  188. $smalluserobject->auth = $user->auth;
  189. $smalluserobject->firstname = $user->firstname;
  190. $smalluserobject->lastname = $user->lastname;
  191. if (empty($SESSION->users_result[$user->id])) {
  192. $SESSION->users_result[$user->id] = $smalluserobject;
  193. $html = '<tr valign="top" id="row-'.$user->id.'" class="result-row">';
  194. $html .= '<td width="10">'.$count.'</td>';
  195. $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>';
  196. $html .= "<ul>";
  197. $profile_set = array('city'=>true, 'country'=>true, 'email'=>true);
  198. foreach ($profile_set as $key=>$value) {
  199. if (isset($user->$key)){
  200. $html .= '<li>'.$user->$key.'</li>';
  201. }
  202. }
  203. $html .= "</ul>";
  204. $html .= '</td>';
  205. foreach ($keywords as $keyword) {
  206. $user->description = highlight($keyword, $user->description);
  207. }
  208. $html .= '<td align="left">'.format_text($user->description, FORMAT_MOODLE).'</td>';
  209. $html .= '<td width="100px" align="center">';
  210. $html .= '<button onclick="del_user(this,'.$user->id.')">'.get_string('deleteuser', 'admin').'</button><br />';
  211. $html .= '<button onclick="ignore_user(this,'.$user->id.')">'.get_string('ignore', 'admin').'</button>';
  212. $html .= '</td>';
  213. $html .= '</tr>';
  214. return $html;
  215. } else {
  216. return null;
  217. }
  218. }
  219. function print_spamcleaner_javascript() {
  220. $sesskey = sesskey();
  221. ?>
  222. <script type="text/javascript">
  223. //<![CDATA[
  224. var row = null;
  225. var delall_cb = {
  226. success: function(o){
  227. try {
  228. var resp = YAHOO.lang.JSON.parse(o.responseText);
  229. } catch(e) {
  230. alert('<?php echo get_string('spaminvalidresult', 'report_spamcleaner');?>');
  231. return;
  232. }
  233. if(resp == true){
  234. window.location.href=window.location.href;
  235. }
  236. }
  237. }
  238. function init() {
  239. YAHOO.util.Event.addListener("removeall_btn", "click", function(){
  240. var yes = confirm('<?php echo get_string('spamdeleteallconfirm', 'report_spamcleaner');?>');
  241. if(yes){
  242. var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php echo me();?>?delall=yes&sesskey=<?php echo $sesskey;?>', delall_cb);
  243. }
  244. });
  245. }
  246. var del_cb = {
  247. success: function(o) {
  248. try {
  249. var resp = YAHOO.lang.JSON.parse(o.responseText);
  250. } catch(e) {
  251. alert('<?php echo get_string('spaminvalidresult', 'report_spamcleaner');?>');
  252. return;
  253. }
  254. if(row) {
  255. if(resp == true){
  256. while(row.tagName != 'TR') {
  257. row = row.parentNode;
  258. }
  259. row.parentNode.removeChild(row);
  260. row = null;
  261. } else {
  262. alert('<?php echo get_string('spamcannotdelete', 'report_spamcleaner');?>');
  263. }
  264. }
  265. }
  266. }
  267. var ignore_cb = {
  268. success: function(o){
  269. try {
  270. var resp = YAHOO.lang.JSON.parse(o.responseText);
  271. } catch(e) {
  272. alert('<?php echo get_string('spaminvalidresult', 'report_spamcleaner');?>');
  273. return;
  274. }
  275. if(row) {
  276. if(resp == true){
  277. while(row.tagName != 'TR') {
  278. row = row.parentNode;
  279. }
  280. row.parentNode.removeChild(row);
  281. row = null;
  282. }
  283. }
  284. }
  285. }
  286. function del_user(obj, id) {
  287. var yes = confirm('<?php echo get_string('spamdeleteconfirm', 'report_spamcleaner');?>');
  288. if(yes){
  289. row = obj;
  290. var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php echo me();?>?del=yes&sesskey=<?php echo $sesskey;?>&id='+id, del_cb);
  291. }
  292. }
  293. function ignore_user(obj, id) {
  294. row = obj;
  295. var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php echo me();?>?ignore=yes&sesskey=<?php echo $sesskey;?>&id='+id, ignore_cb);
  296. }
  297. YAHOO.util.Event.onDOMReady(init);
  298. //]]>
  299. </script>
  300. <?php
  301. }
  302. admin_externalpage_print_footer();
  303. ?>