PageRenderTime 79ms CodeModel.GetById 34ms RepoModel.GetById 2ms app.codeStats 0ms

/antispam/index.php

https://github.com/shikadilord/weldyn
PHP | 467 lines | 369 code | 71 blank | 27 comment | 64 complexity | 0e9911a813770bdfe3415580b11ddb0c MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package Anti-Spam ACP
  5. * @copyright (c) 2008 EXreaction
  6. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  7. *
  8. */
  9. /**
  10. * @ignore
  11. */
  12. define('IN_PHPBB', true);
  13. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
  14. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  15. include($phpbb_root_path . 'common.' . $phpEx);
  16. // Start session management
  17. $user->session_begin();
  18. $auth->acl($user->data);
  19. $user->setup('mods/asacp');
  20. $mode = request_var('mode', '');
  21. $user_id = request_var('u', 0);
  22. $post_id = request_var('p', 0);
  23. $return_url = append_sid("{$phpbb_root_path}index.$phpEx");
  24. if ($post_id)
  25. {
  26. $return_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=$post_id#p$post_id");
  27. }
  28. else if ($user_id)
  29. {
  30. $return_url = append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$user_id");
  31. }
  32. $return = '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $return_url . '">', '</a>');
  33. switch ($mode)
  34. {
  35. case 'display_ips' :
  36. if (!$auth->acl_get('m_asacp_ip_search'))
  37. {
  38. trigger_error('NOT_AUTHORISED');
  39. }
  40. $sql = 'SELECT user_ip FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
  41. $result = $db->sql_query($sql);
  42. $user_row = $db->sql_fetchrow($result);
  43. $db->sql_freeresult($result);
  44. if (!$user_row)
  45. {
  46. trigger_error('NO_USER');
  47. }
  48. $ip_search = array();
  49. $u_ip_search = '<a href="' . append_sid("{$phpbb_root_path}adm/index.$phpEx", 'i=asacp&amp;mode=ip_search&amp;ip={IP}', true, $user->session_id) . '">{IP}</a>';
  50. if ($user_row['user_ip'])
  51. {
  52. $ip_search[] = str_replace('{IP}', $user_row['user_ip'], $u_ip_search);
  53. }
  54. $sql = 'SELECT DISTINCT(poster_ip) FROM ' . POSTS_TABLE . '
  55. WHERE poster_id = ' . $user_id . "
  56. AND poster_ip <> '" . $user_row['user_ip'] . "'
  57. ORDER BY post_id DESC";
  58. $result = $db->sql_query($sql);
  59. while ($row = $db->sql_fetchrow($result))
  60. {
  61. $ip_search[] = str_replace('{IP}', $row['poster_ip'], $u_ip_search);
  62. }
  63. $db->sql_freeresult($result);
  64. trigger_error(implode('<br />', $ip_search) . $return);
  65. break;
  66. case 'user_flag' :
  67. if (!$auth->acl_get('m_asacp_user_flag'))
  68. {
  69. trigger_error('NOT_AUTHORISED');
  70. }
  71. $sql = 'SELECT username, user_colour FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
  72. $result = $db->sql_query($sql);
  73. $row = $db->sql_fetchrow($result);
  74. $db->sql_freeresult($result);
  75. if (!$row)
  76. {
  77. trigger_error('NO_USER');
  78. }
  79. $username = get_username_string('full', $user_id, $row['username'], $row['user_colour']);
  80. if (confirm_box(true))
  81. {
  82. $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_flagged = 1 WHERE user_id = ' . $user_id);
  83. add_log('admin', 'LOG_USER_FLAGGED', $username);
  84. trigger_error($user->lang['USER_FLAG_SUCCESS'] . $return);
  85. }
  86. else
  87. {
  88. $user->lang['USER_FLAG_CONFIRM'] = sprintf($user->lang['USER_FLAG_CONFIRM'], $username);
  89. confirm_box(false, 'USER_FLAG');
  90. }
  91. break;
  92. case 'user_unflag' :
  93. if (!$auth->acl_get('m_asacp_user_flag'))
  94. {
  95. trigger_error('NOT_AUTHORISED');
  96. }
  97. $sql = 'SELECT username, user_colour FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
  98. $result = $db->sql_query($sql);
  99. $row = $db->sql_fetchrow($result);
  100. $db->sql_freeresult($result);
  101. if (!$row)
  102. {
  103. trigger_error('NO_USER');
  104. }
  105. $username = get_username_string('full', $user_id, $row['username'], $row['user_colour']);
  106. if (confirm_box(true))
  107. {
  108. $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_flagged = 0 WHERE user_id = ' . $user_id);
  109. add_log('admin', 'LOG_USER_UNFLAGGED', $username);
  110. trigger_error($user->lang['USER_UNFLAG_SUCCESS'] . $return);
  111. }
  112. else
  113. {
  114. $user->lang['USER_UNFLAG_CONFIRM'] = sprintf($user->lang['USER_UNFLAG_CONFIRM'], $username);
  115. confirm_box(false, 'USER_UNFLAG');
  116. }
  117. break;
  118. case 'ocban' :
  119. if (!$auth->acl_get('m_asacp_ban'))
  120. {
  121. trigger_error('NOT_AUTHORISED');
  122. }
  123. $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
  124. $result = $db->sql_query($sql);
  125. $user_row = $db->sql_fetchrow($result);
  126. $db->sql_freeresult($result);
  127. if (!$user_row)
  128. {
  129. trigger_error('NO_USER');
  130. }
  131. $username = get_username_string('full', $user_id, $user_row['username'], $user_row['user_colour']);
  132. $error = (isset($_POST['sfs_submit']) && !request_var('sfs_evidence', '')) ? true : false;
  133. if (confirm_box(true) && !$error)
  134. {
  135. if (!function_exists('user_ban'))
  136. {
  137. include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  138. }
  139. if (!function_exists('delete_posts'))
  140. {
  141. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  142. }
  143. // Ban the user
  144. if ($config['asacp_ocban_username'])
  145. {
  146. $ban_reason_prefix = '[OCBAN: ' . $user_row['username'] . '] ';
  147. $ban_reason = utf8_normalize_nfc(request_var('ban_reason', '', true));
  148. $ban_reason_shown = utf8_normalize_nfc(request_var('ban_reason_shown', '', true));
  149. $effective_ban_reason = ($ban_reason !== '' ? $ban_reason : 'Username used for spamming');
  150. $effective_ban_reason_shown = ($ban_reason_shown !== '' ? $ban_reason_shown : 'Username used for spamming');
  151. user_ban('user', $user_row['username'], 0, '', false, $ban_reason_prefix . $effective_ban_reason, $effective_ban_reason_shown);
  152. $effective_ban_reason = ($ban_reason !== '' ? $ban_reason : 'IP address used for spamming');
  153. $effective_ban_reason_shown = ($ban_reason_shown !== '' ? $ban_reason_shown : 'IP address used for spamming');
  154. // 10080 = 7 days ban
  155. user_ban('ip', $user_row['user_ip'], 10080, '', false, $ban_reason_prefix . $effective_ban_reason, $effective_ban_reason_shown);
  156. $effective_ban_reason = ($ban_reason !== '' ? $ban_reason : 'Email address used for spamming');
  157. $effective_ban_reason_shown = ($ban_reason_shown !== '' ? $ban_reason_shown : 'Email address used for spamming');
  158. user_ban('email', $user_row['user_email'], 0, '', false, $ban_reason_prefix . $effective_ban_reason, $effective_ban_reason_shown);
  159. // Remove the flag on the user's account if they are banned
  160. $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_flagged = 0 WHERE user_id = ' . $user_id);
  161. }
  162. // Deactivate the user
  163. if ($config['asacp_ocban_deactivate'])
  164. {
  165. user_active_flip('deactivate', $user_id, INACTIVE_MANUAL);
  166. }
  167. // Move the user to a certain group
  168. if ($config['asacp_ocban_move_to_group'])
  169. {
  170. $sql = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user_id;
  171. $result = $db->sql_query($sql);
  172. while ($row = $db->sql_fetchrow($result))
  173. {
  174. group_user_del($row['group_id'], array($user_id), array($username));
  175. }
  176. $db->sql_freeresult($result);
  177. group_user_add($config['asacp_ocban_move_to_group'], array($user_id), array($username), false, true);
  178. }
  179. // Delete the user's posts
  180. if ($config['asacp_ocban_delete_posts'])
  181. {
  182. delete_posts('poster_id', $user_id);
  183. }
  184. // Delete the user's avatar
  185. if ($config['asacp_ocban_delete_avatar'] && $user_row['user_avatar'])
  186. {
  187. avatar_delete('user', $user_row, true);
  188. }
  189. // Delete the user's signature
  190. if ($config['asacp_ocban_delete_signature'])
  191. {
  192. $sql = 'UPDATE ' . USERS_TABLE . '
  193. SET ' . $db->sql_build_array('UPDATE', array('user_sig' => '', 'user_sig_bbcode_uid' => '', 'user_sig_bbcode_bitfield' => '')) . '
  194. WHERE user_id = ' . $user_id;
  195. $db->sql_query($sql);
  196. }
  197. // Delete the user's blog
  198. if ($config['asacp_ocban_blog'] && file_exists($phpbb_root_path . 'blog/includes/functions_admin.' . $phpEx))
  199. {
  200. if (!function_exists('blog_delete_user'))
  201. {
  202. include($phpbb_root_path . 'blog/includes/functions_admin.' . $phpEx);
  203. }
  204. blog_delete_user($user_id);
  205. }
  206. // Clear the user's outbox
  207. if ($config['asacp_ocban_clear_outbox'])
  208. {
  209. $msg_ids = array();
  210. $sql = 'SELECT msg_id
  211. FROM ' . PRIVMSGS_TO_TABLE . "
  212. WHERE author_id = $user_id
  213. AND folder_id = " . PRIVMSGS_OUTBOX;
  214. $result = $db->sql_query($sql);
  215. if ($row = $db->sql_fetchrow($result))
  216. {
  217. if (!function_exists('delete_pm'))
  218. {
  219. include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
  220. }
  221. do
  222. {
  223. $msg_ids[] = (int) $row['msg_id'];
  224. }
  225. while ($row = $db->sql_fetchrow($result));
  226. $db->sql_freeresult($result);
  227. delete_pm($user_id, $msg_ids, PRIVMSGS_OUTBOX);
  228. add_log('admin', 'LOG_USER_DEL_OUTBOX', $user_row['username']);
  229. }
  230. $db->sql_freeresult($result);
  231. }
  232. // Empty the user's profile fields
  233. if ($config['asacp_ocban_delete_profile_fields'])
  234. {
  235. $sql_ary = array(
  236. 'user_birthday' => '',
  237. 'user_from' => '',
  238. 'user_icq' => '',
  239. 'user_aim' => '',
  240. 'user_yim' => '',
  241. 'user_msnm' => '',
  242. 'user_jabber' => '',
  243. 'user_website' => '',
  244. 'user_occ' => '',
  245. 'user_interests' => '',
  246. );
  247. $sql = 'UPDATE ' . USERS_TABLE . '
  248. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  249. WHERE user_id = ' . $user_id;
  250. $db->sql_query($sql);
  251. }
  252. // Submit the information to Stop Forum Spam
  253. if (isset($_POST['sfs_submit']) && $config['asacp_sfs_key'])
  254. {
  255. $data = array(
  256. 'username' => $user_row['username'],
  257. 'email' => $user_row['user_email'],
  258. 'ip_addr' => $user_row['user_ip'],
  259. 'evidence' => substr(utf8_normalize_nfc(request_var('sfs_evidence', '', true)), 0, 7999), // Evidence is limited to 8,000 characters
  260. 'api_key' => $config['asacp_sfs_key'],
  261. );
  262. $errno = $errstr = '';
  263. $domain = 'www.stopforumspam.com';
  264. $fp = @fsockopen($domain, 80, $errno, $errstr, 5);
  265. if ($fp)
  266. {
  267. $post = http_build_query($data);
  268. $out = "POST /add HTTP/1.0\r\n";
  269. $out .= "Host: $domain\r\n";
  270. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  271. $out .= 'Content-Length: ' . strlen($post) . "\r\n\r\n";
  272. $out .= "$post\r\n";
  273. $out .= "Connection: close\r\n";
  274. fwrite($fp, $out);
  275. fclose($fp);
  276. }
  277. }
  278. // Submit the spam to Akismet
  279. if (isset($_POST['akismet_submit']) && $config['asacp_akismet_enable'] && $config['asacp_akismet_key'] && ($post_id = request_var('p', 0)))
  280. {
  281. $sql = 'SELECT * FROM ' . POSTS_TABLE . '
  282. WHERE post_id = ' . $post_id;
  283. $result = $db->sql_query($sql);
  284. $post = $db->sql_fetchrow($result);
  285. $db->sql_freeresult($result);
  286. if ($post)
  287. {
  288. if (!class_exists('Akismet'))
  289. {
  290. global $phpbb_root_path, $phpEx;
  291. include($phpbb_root_path . 'antispam/Akismet.class.' . $phpEx);
  292. }
  293. $post['decoded_text'] = $post['post_text'];
  294. decode_message($post['decoded_text'], $post['bbcode_uid']);
  295. $akismet = new Akismet($config['asacp_akismet_domain'], $config['asacp_akismet_key']);
  296. $akismet->setUserIP($post['poster_ip']);
  297. $akismet->setReferrer('');
  298. $akismet->setCommentUserAgent('');
  299. $akismet->setCommentType('comment');
  300. $akismet->setCommentAuthor($user_row['username']);
  301. $akismet->setCommentAuthorEmail($user_row['user_email']);
  302. $akismet->setCommentContent($post['decoded_text']);
  303. $akismet->submitSpam();
  304. }
  305. }
  306. trigger_error(sprintf($user->lang['ASACP_BAN_COMPLETE'], append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$user_id")));
  307. }
  308. else
  309. {
  310. if (isset($_REQUEST['confirm_key']) && $error)
  311. {
  312. // Hack to fix the confirm_box if we need to come back to it because of an error
  313. unset($_REQUEST['confirm_key']);
  314. }
  315. // Build the ban actions string
  316. $user->add_lang('mods/acp_asacp');
  317. $ban_actions = array();
  318. if ($config['asacp_ocban_username'])
  319. {
  320. $ban_actions[] = $user->lang['ASACP_BAN_USERNAME'];
  321. }
  322. if ($config['asacp_ocban_deactivate'])
  323. {
  324. $ban_actions[] = $user->lang['ASACP_BAN_DEACTIVATE_USER'];
  325. }
  326. if ($config['asacp_ocban_move_to_group'])
  327. {
  328. $sql = 'SELECT group_name FROM ' . GROUPS_TABLE . ' WHERE group_id = ' . $config['asacp_ocban_move_to_group'];
  329. $result = $db->sql_query($sql);
  330. $group_name = $db->sql_fetchfield('group_name');
  331. $db->sql_freeresult($result);
  332. $group_name = (isset($user->lang['G_' . $group_name])) ? $user->lang['G_' . $group_name] : $group_name;
  333. $ban_actions[] = $user->lang['ASACP_BAN_MOVE_TO_GROUP'] . ': ' . $group_name;
  334. }
  335. if ($config['asacp_ocban_delete_posts'])
  336. {
  337. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_POSTS'];
  338. }
  339. if ($config['asacp_ocban_delete_avatar'])
  340. {
  341. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_AVATAR'];
  342. }
  343. if ($config['asacp_ocban_delete_signature'])
  344. {
  345. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_SIGNATURE'];
  346. }
  347. if ($config['asacp_ocban_clear_outbox'])
  348. {
  349. $ban_actions[] = $user->lang['ASACP_BAN_CLEAR_OUTBOX'];
  350. }
  351. if ($config['asacp_ocban_delete_profile_fields'])
  352. {
  353. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_PROFILE_FIELDS'];
  354. }
  355. if ($config['asacp_ocban_blog'] && file_exists($phpbb_root_path . 'blog/includes/functions_admin.' . $phpEx))
  356. {
  357. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_BLOG'];
  358. }
  359. $post = false;
  360. if (($post_id = request_var('p', 0)))
  361. {
  362. $sql = 'SELECT * FROM ' . POSTS_TABLE . '
  363. WHERE post_id = ' . $post_id;
  364. $result = $db->sql_query($sql);
  365. $post = $db->sql_fetchrow($result);
  366. $db->sql_freeresult($result);
  367. if ($post)
  368. {
  369. $post['decoded_text'] = $post['post_text'];
  370. decode_message($post['decoded_text'], $post['bbcode_uid']);
  371. }
  372. }
  373. $template->assign_vars(array(
  374. 'POST_TEXT' => (is_array($post)) ? $post['post_text'] : false,
  375. 'S_BAN_USER' => $config['asacp_ocban_username'],
  376. 'S_AKISMET_SUBMIT' => ($config['asacp_akismet_enable'] && $config['asacp_akismet_key'] && is_array($post)) ? true : false,
  377. 'S_SFS_SUBMIT' => ($config['asacp_sfs_key']) ? true : false,
  378. 'BAN_REASON' => utf8_normalize_nfc(request_var('ban_reason', '', true)),
  379. 'AKISMET_SUBMIT' => (isset($_POST['akismet_submit'])) ? true : false,
  380. 'AKISMET_TEXT' => (is_array($post)) ? $post['decoded_text'] : '',
  381. 'SFS_SUBMIT' => (isset($_POST['sfs_submit'])) ? true : false,
  382. 'SFS_EVIDENCE' => (!isset($_POST['confirm']) && !request_var('sfs_evidence', '', true) && is_array($post)) ? $post['decoded_text'] : utf8_normalize_nfc(request_var('sfs_evidence', '', true)),
  383. 'SFS_EVIDENCE_ERROR' => ($error) ? true : false,
  384. 'L_ASACP_BAN_ACTIONS' => sprintf($user->lang['ASACP_BAN_ACTIONS'], implode(', ', $ban_actions)),
  385. ));
  386. $user->lang['ASACP_BAN_CONFIRM'] = sprintf($user->lang['ASACP_BAN_CONFIRM'], $username);
  387. confirm_box(false, 'ASACP_BAN', '', 'antispam/oc_ban.html', "antispam/index.{$phpEx}?mode=ocban&amp;u=$user_id&amp;p=$post_id");
  388. }
  389. break;
  390. default :
  391. trigger_error('NO_MODE');
  392. break;
  393. }
  394. // Should not get here (unless No selected for the confirm_box)
  395. redirect($return_url);
  396. ?>