spambot.pages.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. function spambot_user_spam_admin_form($form, &$form_state, $account) {
  3. $node_count = db_select('node')->condition('uid', $account->uid, '=')
  4. ->countQuery()->execute()->fetchField();
  5. if (module_exists('comment')) {
  6. $comment_count = db_select('comment')->condition('uid', $account->uid, '=')
  7. ->countQuery()->execute()->fetchField();
  8. $status = t('This account has @n nodes and @c comments.', array('@n' => $node_count, '@c' => $comment_count));
  9. }
  10. else {
  11. $status = t('This account has @n nodes.', array('@n' => $node_count));
  12. }
  13. $form['check'] = array(
  14. '#type' => 'submit',
  15. '#value' => t('Check if this account matches a known spammer'),
  16. );
  17. $form['action'] = array(
  18. '#type' => 'fieldset',
  19. '#title' => t('Take action against this account'),
  20. '#collapsible' => TRUE,
  21. '#description' => $status,
  22. );
  23. $form['action']['unpublish_content'] = array(
  24. '#type' => 'checkbox',
  25. '#title' => t('Unpublish nodes and comments by this account'),
  26. '#default_value' => TRUE,
  27. );
  28. $form['action']['delete_content'] = array(
  29. '#type' => 'checkbox',
  30. '#title' => t('Delete nodes and comments by this account'),
  31. '#default_value' => FALSE,
  32. );
  33. $form['action']['report'] = array(
  34. '#type' => 'fieldset',
  35. '#title' => t('Report this account to www.stopforumspam.com'),
  36. '#tree' => TRUE,
  37. '#collapsible' => TRUE,
  38. '#description' => t('An API key from <a href="http://www.stopforumspam.com">www.stopforumspam.com</a> is required to report spammers.<br />Select one or more posts below to report them to www.stopforumspam.com.'),
  39. );
  40. // Fetch a list of reportable nodes
  41. $form['action']['report']['nids'] = array();
  42. $result = db_select('node_spambot')->fields('node_spambot', array('nid', 'hostname'))->condition('uid', $account->uid)->orderBy('nid', 'DESC')->range(0, 20)->execute();
  43. $nid_hostnames = array();
  44. foreach ($result as $record) {
  45. $nid_hostnames[$record->nid] = $record->hostname;
  46. }
  47. foreach ($nid_hostnames as $nid => $hostname) {
  48. $node = node_load($nid);
  49. if (!empty($node->nid)) {
  50. $form['action']['report']['nids'][$nid] = array(
  51. '#type' => 'checkbox',
  52. '#title' => l(mb_strimwidth($node->title, 0, 128, '...'), 'node/' . $nid, array('attributes' => array('title' => mb_strimwidth($node->body['und'][0]['summary'] . "\n\n" . $node->body['und'][0]['value'], 0, 256, '...')))) . ' ' . t('(node, ip=@ip)', array('@ip' => $hostname)),
  53. );
  54. }
  55. }
  56. // Fetch a list of reportable comments
  57. if (module_exists('comment')) {
  58. $form['action']['report']['cids'] = array();
  59. $result = db_select('comment')->fields('comment', array('cid'))->condition('uid', $account->uid)->orderBy('cid', 'DESC')->range(0, 20)->execute();
  60. $cids = array();
  61. foreach ($result as $record) {
  62. $cids[$record->cid] = $record->cid;
  63. }
  64. foreach ($cids as $cid) {
  65. $comment = comment_load($cid);
  66. if (!empty($comment->cid)) {
  67. $form['action']['report']['cids'][$cid] = array(
  68. '#type' => 'checkbox',
  69. '#title' => l(mb_strimwidth($comment->subject, 0, 128, '...'), 'node/' . $comment->nid, array('fragment' => 'comment-'. $comment->cid, 'attributes' => array('title' => mb_strimwidth($comment->comment_body['und'][0]['value'], 0, 256, '...')))) . ' ' . t('(comment, ip=@ip)', array('@ip' => $comment->hostname)),
  70. );
  71. }
  72. }
  73. }
  74. $form['action']['block_user'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Block this account'),
  77. '#default_value' => TRUE,
  78. );
  79. $form['action']['delete_user'] = array(
  80. '#type' => 'checkbox',
  81. '#title' => t('Delete this account'),
  82. '#default_value' => FALSE,
  83. );
  84. $form['action']['action'] = array(
  85. '#type' => 'submit',
  86. '#value' => t('Take action'),
  87. );
  88. $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  89. return $form;
  90. }
  91. function spambot_user_spam_admin_form_validate($form, &$form_state) {
  92. $key_required = (!empty($form_state['values']['report']['nids']) && count(array_filter($form_state['values']['report']['nids']))) ? TRUE : FALSE;
  93. if (module_exists('comment')) {
  94. $key_required = (!empty($form_state['values']['report']['cids']) && count(array_filter($form_state['values']['report']['cids']))) || $key_required;
  95. }
  96. if ($key_required && (!variable_get('spambot_sfs_api_key', FALSE))) {
  97. form_set_error('', t('To report spammers to www.stopforumspam.com, you need to register for an API key at <a href="http://www.stopforumspam.com">www.stopforumspam.com</a> and enter it into the !page.', array('!page' => l('spambot settings', 'admin/config/system/spambot'))));
  98. }
  99. }
  100. function spambot_user_spam_admin_form_submit($form, &$form_state) {
  101. $account = user_load($form_state['values']['uid']);
  102. if ($form_state['values']['op'] == $form_state['values']['check']) {
  103. // This is a more comprehensive check than the automated criteria checks.
  104. // This tests everything.
  105. $messages = array();
  106. $service_down = FALSE;
  107. // Check email and username
  108. $request = array('email' => $account->mail, 'username' => $account->name);
  109. $data = array();
  110. if (spambot_sfs_request($request, $data)) {
  111. if (!empty($data['email']['appears'])) {
  112. $messages[] = t('This account\'s email address matches @num times: !link', array('!link' => l($request['email'], 'http://www.stopforumspam.com/search?q=' . $request['email']), '@num' => $data['email']['frequency']));
  113. }
  114. if (!empty($data['username']['appears'])) {
  115. $messages[] = t('This account\'s username matches @num times: !link', array('!link' => l($request['username'], 'http://www.stopforumspam.com/search?q=' . $request['username']), '@num' => $data['username']['frequency']));
  116. }
  117. }
  118. else {
  119. drupal_set_message(t('Error contacting service.'), 'warning');
  120. $service_down = TRUE;
  121. }
  122. // Check IP addresses
  123. if (!$service_down) {
  124. $ips = spambot_account_ip_addresses($account);
  125. foreach ($ips as $ip) {
  126. // Skip the loopback interface
  127. if ($ip == '127.0.0.1') {
  128. continue;
  129. }
  130. $request = array('ip' => $ip);
  131. $data = array();
  132. if (spambot_sfs_request($request, $data)) {
  133. if (!empty($data['ip']['appears'])) {
  134. $messages[] = t('An IP address !ip used by this account matches @num times.', array('!ip' => l($ip, 'http://www.stopforumspam.com/search?q=' . $ip), '@num' => $data['ip']['frequency']));
  135. }
  136. }
  137. else {
  138. drupal_set_message(t('Error contacting service.'), 'warning');
  139. $service_down = TRUE;
  140. break;
  141. }
  142. }
  143. }
  144. if (count($messages)) {
  145. foreach ($messages as $message) {
  146. drupal_set_message($message);
  147. }
  148. }
  149. else {
  150. drupal_set_message(t('No matches against known spammers found.'));
  151. }
  152. }
  153. else if ($form_state['values']['op'] == $form_state['values']['action']) {
  154. if ($account->uid == 1) {
  155. drupal_set_message(t('Sorry, taking action against uid 1 is not allowed.'));
  156. return;
  157. }
  158. // Block account
  159. if (!empty($form_state['values']['block_user'])) {
  160. if ($account->status) {
  161. user_save($account, array('status' => 0));
  162. drupal_set_message(t('Account blocked.'));
  163. }
  164. else {
  165. drupal_set_message(t('This account is already blocked.'));
  166. }
  167. }
  168. // Prepare some data
  169. $nodes = db_select('node')->fields('node', array('nid'))
  170. ->condition('uid', $account->uid, '=')->orderBy('nid')->execute()->fetchCol();
  171. $node_hostnames = array();
  172. $result = db_select('node_spambot')->fields('node_spambot', array('nid', 'hostname'))->condition('uid', $account->uid)->orderBy('nid', 'DESC')->execute();
  173. foreach ($result as $record) {
  174. $node_hostnames[$record->nid] = $record->hostname;
  175. }
  176. $comments = array();
  177. if (module_exists('comment')) {
  178. $comments = db_select('comment')->fields('comment', array('cid'))
  179. ->condition('uid', $account->uid, '=')->orderBy('cid')->execute()->fetchCol();
  180. }
  181. // Report posts to www.stopforumspam.com
  182. if (!empty($form_state['values']['report']['nids'])) {
  183. foreach (array_filter($form_state['values']['report']['nids']) as $nid => $unused) {
  184. $node = node_load($nid);
  185. if (!empty($node->nid)) {
  186. if (spambot_report_account($account, $node_hostnames[$nid], $node->title . "\n\n" . $node->body['und'][0]['summary'] . "\n\n" . $node->body['und'][0]['value'])) {
  187. drupal_set_message(t('Node %title has been reported.', array('%title' => $node->title)));
  188. }
  189. else {
  190. drupal_set_message(t('There was a problem reporting node %title.', array('%title' => $node->title)));
  191. }
  192. }
  193. }
  194. }
  195. if (module_exists('comment') && !empty($form_state['values']['report']['cids'])) {
  196. foreach (array_filter($form_state['values']['report']['cids']) as $cid => $unused) {
  197. $comment = comment_load($cid);
  198. if (!empty($comment->cid)) {
  199. if (spambot_report_account($account, $comment->hostname, $comment->subject . "\n\n" . $comment->comment_body['und'][0]['value'])) {
  200. drupal_set_message(t('Comment %title has been reported.', array('%title' => $comment->subject)));
  201. }
  202. else {
  203. drupal_set_message(t('There was a problem reporting comment %title.', array('%title' => $comment->subject)));
  204. }
  205. }
  206. }
  207. }
  208. // Delete nodes and content
  209. if (!empty($form_state['values']['delete_content'])) {
  210. node_delete_multiple($nodes);
  211. if (count($comments)) {
  212. comment_delete_multiple($comments);
  213. }
  214. drupal_set_message(t('Nodes and comments have been deleted.'));
  215. }
  216. else if (!empty($form_state['values']['unpublish_content'])) {
  217. // Unpublish nodes and content
  218. if (count($nodes)) {
  219. module_load_include('inc', 'node', 'node.admin');
  220. node_mass_update($nodes, array('status' => 0));
  221. }
  222. if (count($comments)) {
  223. db_update('comment')->fields(array('status' => COMMENT_NOT_PUBLISHED))
  224. ->condition('uid', $account->uid)->execute();
  225. cache_clear_all();
  226. }
  227. drupal_set_message(t('Nodes and comments have been unpublished.'));
  228. }
  229. // Delete user
  230. if (!empty($form_state['values']['delete_user'])) {
  231. // Redirect to user delete form
  232. $form_state['redirect'] = 'user/' . $account->uid . '/cancel';
  233. }
  234. }
  235. }