spambot.pages.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * @file
  4. * User available pages from Spambot module.
  5. */
  6. /**
  7. * Page callback for 'user/%user/spambot' path.
  8. */
  9. function spambot_user_spam($account) {
  10. // Check if current user isn't anonymous user.
  11. if (!$account->uid) {
  12. drupal_set_message(t("The Anonymous user account can't be reported for spam. If you intended to block a user account verify that the URL is /user/XXXX/spambot where XXXX is a valid UID"), 'warning');
  13. return MENU_NOT_FOUND;
  14. }
  15. return drupal_get_form('spambot_user_spam_admin_form', $account);
  16. }
  17. /**
  18. * Form builder for spambot_user_spam_admin_form form.
  19. */
  20. function spambot_user_spam_admin_form($form, &$form_state, $account) {
  21. $key = variable_get('spambot_sfs_api_key', FALSE);
  22. $comments_enabled = module_exists('comment');
  23. $node_count = db_select('node', 'n')
  24. ->fields('n', array('nid'))
  25. ->condition('uid', $account->uid)
  26. ->countQuery()
  27. ->execute()
  28. ->fetchField();
  29. $status = t('This account has @n nodes.', array('@n' => $node_count));
  30. if ($comments_enabled) {
  31. $comment_count = db_select('comment', 'c')
  32. ->fields('c', array('cid'))
  33. ->condition('uid', $account->uid)
  34. ->countQuery()
  35. ->execute()
  36. ->fetchField();
  37. $status = t('This account has @n nodes and @c comments.', array('@n' => $node_count, '@c' => $comment_count));
  38. }
  39. $form['check'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Check if this account matches a known spammer'),
  42. );
  43. $form['action'] = array(
  44. '#type' => 'fieldset',
  45. '#title' => t('Take action against this account'),
  46. '#collapsible' => TRUE,
  47. '#description' => $status,
  48. );
  49. $form['action']['unpublish_content'] = array(
  50. '#type' => 'checkbox',
  51. '#title' => t('Unpublish nodes and comments by this account'),
  52. '#default_value' => TRUE,
  53. );
  54. $form['action']['delete_content'] = array(
  55. '#type' => 'checkbox',
  56. '#title' => t('Delete nodes and comments by this account'),
  57. '#default_value' => FALSE,
  58. );
  59. $form['action']['report'] = array(
  60. '#type' => 'fieldset',
  61. '#title' => t('Report this account to www.stopforumspam.com'),
  62. '#tree' => TRUE,
  63. '#collapsible' => TRUE,
  64. );
  65. // Fetch a list of reportable nodes.
  66. $form['action']['report']['nids'] = array();
  67. $result = db_select('node_spambot', 'ns')
  68. ->fields('ns', array('nid', 'hostname'))
  69. ->condition('ns.uid', $account->uid)
  70. ->orderBy('ns.nid', 'DESC')
  71. ->range(0, 20)
  72. ->execute();
  73. $nid_hostnames = array();
  74. foreach ($result as $record) {
  75. $nid_hostnames[$record->nid] = $record->hostname;
  76. }
  77. foreach ($nid_hostnames as $nid => $hostname) {
  78. if ($node = node_load($nid)) {
  79. $title = truncate_utf8(check_plain($node->title), 128, TRUE, TRUE);
  80. $form['action']['report']['nids'][$nid] = array(
  81. '#type' => 'checkbox',
  82. '#title' => l(
  83. $title,
  84. "node/$nid",
  85. array(
  86. 'attributes' => array(
  87. 'title' => $title,
  88. ),
  89. )
  90. ) . ' ' . t('(node, ip=@ip)', array('@ip' => $hostname)),
  91. '#disabled' => !$key,
  92. );
  93. }
  94. }
  95. // Fetch a list of reportable comments.
  96. if ($comments_enabled) {
  97. $form['action']['report']['cids'] = array();
  98. $result = db_select('comment')
  99. ->fields('comment', array('cid'))
  100. ->condition('uid', $account->uid)
  101. ->orderBy('cid', 'DESC')
  102. ->range(0, 20)
  103. ->execute();
  104. $cids = array();
  105. foreach ($result as $record) {
  106. $cids[$record->cid] = $record->cid;
  107. }
  108. foreach ($cids as $cid) {
  109. if ($comment = comment_load($cid)) {
  110. $subject = truncate_utf8(check_plain($comment->subject), 128, TRUE, TRUE);
  111. $form['action']['report']['cids'][$cid] = array(
  112. '#type' => 'checkbox',
  113. '#title' => l(
  114. $subject,
  115. "node/$comment->nid",
  116. array(
  117. 'fragment' => "comment-$comment->cid",
  118. 'attributes' => array(
  119. 'title' => $subject,
  120. ),
  121. )
  122. ) . ' ' . t('(comment, ip=@ip)', array('@ip' => $comment->hostname)),
  123. '#disabled' => !$key,
  124. );
  125. }
  126. }
  127. }
  128. if ($key) {
  129. $comment_cids = $comments_enabled ? count($form['action']['report']['cids']) : 0;
  130. $evidence_count = count($form['action']['report']['nids']) + $comment_cids;
  131. $form['action']['report']['#description'] = $evidence_count ? t('Select one or more posts below to report them to www.stopforumspam.com.') : t('This account cannot be reported because no evidence or IP address is available.');
  132. }
  133. else {
  134. $form['action']['report']['#description'] = t('An API key from <a href="http://www.stopforumspam.com">www.stopforumspam.com</a> must <a href="!admin-url">be configured</a> to report spammers.', array('!admin-url' => url('admin/config/system/spambot')));
  135. }
  136. $form['action']['block_user'] = array(
  137. '#type' => 'checkbox',
  138. '#title' => t('Block this account'),
  139. '#default_value' => TRUE,
  140. );
  141. $form['action']['delete_user'] = array(
  142. '#type' => 'checkbox',
  143. '#title' => t('Delete this account'),
  144. '#default_value' => FALSE,
  145. );
  146. $form['action']['action'] = array(
  147. '#type' => 'submit',
  148. '#value' => t('Take action'),
  149. );
  150. $form['uid'] = array(
  151. '#type' => 'value',
  152. '#value' => $account->uid,
  153. );
  154. $form['#validate'][] = 'spambot_user_spam_admin_form_validate';
  155. $form['#submit'][] = 'spambot_user_spam_admin_form_submit';
  156. return $form;
  157. }
  158. /**
  159. * Validate handler for spambot_user_spam_admin_form() form.
  160. */
  161. function spambot_user_spam_admin_form_validate(&$form, &$form_state) {
  162. $key_required = (!empty($form_state['values']['report']['nids']) && count(array_filter($form_state['values']['report']['nids']))) ? TRUE : FALSE;
  163. if (module_exists('comment')) {
  164. $key_required = (!empty($form_state['values']['report']['cids']) && count(array_filter($form_state['values']['report']['cids']))) || $key_required;
  165. }
  166. if ($key_required && !variable_get('spambot_sfs_api_key', FALSE)) {
  167. 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(
  168. '!page' => l(t('spambot settings'), 'admin/config/system/spambot'),
  169. )));
  170. }
  171. }
  172. /**
  173. * Submit handler for spambot_user_spam_admin_form() form.
  174. */
  175. function spambot_user_spam_admin_form_submit(&$form, &$form_state) {
  176. $account = user_load($form_state['values']['uid']);
  177. if ($form_state['values']['op'] == $form_state['values']['check']) {
  178. _spambot_user_spam_admin_form_submit_check($form, $form_state, $account);
  179. }
  180. elseif ($form_state['values']['op'] == $form_state['values']['action']) {
  181. _spambot_user_spam_admin_form_submit_action($form, $form_state, $account);
  182. }
  183. }
  184. /**
  185. * Do complex checking at this user account.
  186. */
  187. function _spambot_user_spam_admin_form_submit_check(&$form, &$form_state, $account) {
  188. $messages = array();
  189. $service_down = FALSE;
  190. // Check email and username.
  191. $data = array();
  192. $request = array(
  193. 'email' => $account->mail,
  194. 'username' => $account->name,
  195. );
  196. if (spambot_sfs_request($request, $data)) {
  197. if (!empty($data['email']['appears'])) {
  198. $messages[] = array(
  199. 'text' => t("This account's email address matches %num times: !link", array(
  200. '!link' => l($request['email'], 'http://www.stopforumspam.com/search?q=' . $request['email']),
  201. '%num' => $data['email']['frequency'],
  202. )),
  203. 'type' => 'warning',
  204. );
  205. }
  206. if (!empty($data['username']['appears'])) {
  207. $messages[] = array(
  208. 'text' => t("This account's username matches %num times: !link", array(
  209. '!link' => l($request['username'], 'http://www.stopforumspam.com/search?q=' . $request['username']),
  210. '%num' => $data['username']['frequency'],
  211. )),
  212. 'type' => 'warning',
  213. );
  214. }
  215. // Check data at whitelist.
  216. if (spambot_check_whitelist('email', $account->mail)) {
  217. $messages[] = array(
  218. 'text' => t("This account's email address placed at your whitelist."),
  219. 'type' => 'status',
  220. );
  221. }
  222. if (spambot_check_whitelist('username', $account->name)) {
  223. $messages[] = array(
  224. 'text' => t("This account's username placed at your whitelist."),
  225. 'type' => 'status',
  226. );
  227. }
  228. }
  229. else {
  230. drupal_set_message(t('Error contacting service.'), 'warning');
  231. $service_down = TRUE;
  232. }
  233. // Check IP addresses.
  234. if (!$service_down) {
  235. $ips = spambot_account_ip_addresses($account);
  236. foreach ($ips as $ip) {
  237. // Skip the loopback interface.
  238. if ($ip == '127.0.0.1') {
  239. continue;
  240. }
  241. elseif (spambot_check_whitelist('ip', $ip)) {
  242. $whitelist_ips[] = $ip;
  243. continue;
  244. }
  245. // Make sure we have a valid IPv4 address
  246. // (the API doesn't support IPv6 yet).
  247. elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
  248. $messages[] = array(
  249. 'text' => t('Invalid IP address: @ip. Spambot will not rely on it.', array('@ip' => $ip)),
  250. 'type' => 'warning',
  251. );
  252. continue;
  253. }
  254. $request = array('ip' => $ip);
  255. $data = array();
  256. if (spambot_sfs_request($request, $data)) {
  257. if (!empty($data['ip']['appears'])) {
  258. $messages[] = array(
  259. 'text' => t('An IP address !ip used by this account matches %num times.', array(
  260. '!ip' => l($ip, 'http://www.stopforumspam.com/search?q=' . $ip),
  261. '%num' => $data['ip']['frequency'],
  262. )),
  263. 'type' => 'warning',
  264. );
  265. }
  266. }
  267. else {
  268. drupal_set_message(t('Error contacting service.'), 'warning');
  269. break;
  270. }
  271. }
  272. if (!empty($whitelist_ips)) {
  273. $messages[] = array(
  274. 'text' => t('These IP addresses placed at your whitelist: %ips', array('%ips' => implode(', ', $whitelist_ips))),
  275. 'type' => 'status',
  276. );
  277. }
  278. }
  279. if ($messages) {
  280. foreach ($messages as $message) {
  281. drupal_set_message($message['text'], $message['type']);
  282. }
  283. }
  284. else {
  285. drupal_set_message(t('No matches against known spammers found.'));
  286. }
  287. }
  288. /**
  289. * Take action under this user account.
  290. */
  291. function _spambot_user_spam_admin_form_submit_action(&$form, &$form_state, $account) {
  292. $comments_enabled = module_exists('comment');
  293. if ($account->uid == 1) {
  294. drupal_set_message(t('Sorry, taking action against uid 1 is not allowed.'), 'warning');
  295. return;
  296. }
  297. // Block account.
  298. if (!empty($form_state['values']['block_user'])) {
  299. if ($account->status) {
  300. user_save($account, array('status' => 0));
  301. drupal_set_message(t('Account blocked.'));
  302. }
  303. else {
  304. drupal_set_message(t('This account is already blocked.'));
  305. }
  306. }
  307. // Prepare some data.
  308. $nodes = db_select('node')
  309. ->fields('node', array('nid'))
  310. ->condition('uid', $account->uid, '=')
  311. ->orderBy('nid')
  312. ->execute()
  313. ->fetchCol();
  314. $node_hostnames = array();
  315. $result = db_select('node_spambot')
  316. ->fields('node_spambot', array('nid', 'hostname'))
  317. ->condition('uid', $account->uid)
  318. ->orderBy('nid', 'DESC')
  319. ->execute();
  320. foreach ($result as $record) {
  321. $node_hostnames[$record->nid] = $record->hostname;
  322. }
  323. $comments = array();
  324. if ($comments_enabled) {
  325. $comments = db_select('comment')
  326. ->fields('comment', array('cid'))
  327. ->condition('uid', $account->uid, '=')
  328. ->orderBy('cid')
  329. ->execute()
  330. ->fetchCol();
  331. }
  332. // Report posts to www.stopforumspam.com.
  333. if (!empty($form_state['values']['report']['nids'])) {
  334. foreach (array_filter($form_state['values']['report']['nids']) as $nid => $unused) {
  335. $node = node_load($nid);
  336. if (!empty($node->nid)) {
  337. if (spambot_report_account($account, $node_hostnames[$nid], $node->title . "\n\n" . $node->body[LANGUAGE_NONE][0]['summary'] . "\n\n" . $node->body[LANGUAGE_NONE][0]['value'])) {
  338. drupal_set_message(t('Node %title has been reported.', array('%title' => $node->title)));
  339. }
  340. else {
  341. drupal_set_message(t('There was a problem reporting node %title.', array('%title' => $node->title)));
  342. }
  343. }
  344. }
  345. }
  346. if ($comments_enabled && !empty($form_state['values']['report']['cids'])) {
  347. foreach (array_filter($form_state['values']['report']['cids']) as $cid => $unused) {
  348. $comment = comment_load($cid);
  349. if (!empty($comment->cid)) {
  350. if (spambot_report_account($account, $comment->hostname, $comment->subject . "\n\n" . $comment->comment_body[LANGUAGE_NONE][0]['value'])) {
  351. drupal_set_message(t('Comment %title has been reported.', array('%title' => $comment->subject)));
  352. }
  353. else {
  354. drupal_set_message(t('There was a problem reporting comment %title.', array('%title' => $comment->subject)));
  355. }
  356. }
  357. }
  358. }
  359. // Delete nodes and content.
  360. if (!empty($form_state['values']['delete_content'])) {
  361. node_delete_multiple($nodes);
  362. if ($comments) {
  363. comment_delete_multiple($comments);
  364. }
  365. drupal_set_message(t('Nodes and comments have been deleted.'));
  366. }
  367. elseif (!empty($form_state['values']['unpublish_content'])) {
  368. // Unpublish nodes and content.
  369. if ($nodes) {
  370. module_load_include('inc', 'node', 'node.admin');
  371. node_mass_update($nodes, array('status' => 0));
  372. }
  373. if ($comments) {
  374. db_update('comment')
  375. ->fields(array('status' => COMMENT_NOT_PUBLISHED))
  376. ->condition('uid', $account->uid)
  377. ->execute();
  378. }
  379. drupal_set_message(t('Nodes and comments have been unpublished.'));
  380. }
  381. // Delete user.
  382. if (!empty($form_state['values']['delete_user'])) {
  383. // Redirect to user delete form.
  384. $form_state['redirect'] = "user/$account->uid/cancel";
  385. }
  386. }