PageRenderTime 67ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/root/antispam/index.php

https://github.com/EXreaction/Anti-Spam-ACP
PHP | 449 lines | 358 code | 65 blank | 26 comment | 64 complexity | 5584aec4994af38d2f6b56ec7c80fd5e MD5 | raw file
Possible License(s): AGPL-1.0
  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. user_ban('user', $user_row['username'], 0, '', false, utf8_normalize_nfc(request_var('ban_reason', '', true)), utf8_normalize_nfc(request_var('ban_reason_shown', '', true)));
  147. // Remove the flag on the user's account if they are banned
  148. $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_flagged = 0 WHERE user_id = ' . $user_id);
  149. }
  150. // Deactivate the user
  151. if ($config['asacp_ocban_deactivate'])
  152. {
  153. user_active_flip('deactivate', $user_id, INACTIVE_MANUAL);
  154. }
  155. // Move the user to a certain group
  156. if ($config['asacp_ocban_move_to_group'])
  157. {
  158. $sql = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user_id;
  159. $result = $db->sql_query($sql);
  160. while ($row = $db->sql_fetchrow($result))
  161. {
  162. group_user_del($row['group_id'], array($user_id), array($username));
  163. }
  164. $db->sql_freeresult($result);
  165. group_user_add($config['asacp_ocban_move_to_group'], array($user_id), array($username), false, true);
  166. }
  167. // Delete the user's posts
  168. if ($config['asacp_ocban_delete_posts'])
  169. {
  170. delete_posts('poster_id', $user_id);
  171. }
  172. // Delete the user's avatar
  173. if ($config['asacp_ocban_delete_avatar'] && $user_row['user_avatar'])
  174. {
  175. avatar_delete('user', $user_row, true);
  176. }
  177. // Delete the user's signature
  178. if ($config['asacp_ocban_delete_signature'])
  179. {
  180. $sql = 'UPDATE ' . USERS_TABLE . '
  181. SET ' . $db->sql_build_array('UPDATE', array('user_sig' => '', 'user_sig_bbcode_uid' => '', 'user_sig_bbcode_bitfield' => '')) . '
  182. WHERE user_id = ' . $user_id;
  183. $db->sql_query($sql);
  184. }
  185. // Delete the user's blog
  186. if ($config['asacp_ocban_blog'] && file_exists($phpbb_root_path . 'blog/includes/functions_admin.' . $phpEx))
  187. {
  188. if (!function_exists('blog_delete_user'))
  189. {
  190. include($phpbb_root_path . 'blog/includes/functions_admin.' . $phpEx);
  191. }
  192. blog_delete_user($user_id);
  193. }
  194. // Clear the user's outbox
  195. if ($config['asacp_ocban_clear_outbox'])
  196. {
  197. $msg_ids = array();
  198. $sql = 'SELECT msg_id
  199. FROM ' . PRIVMSGS_TO_TABLE . "
  200. WHERE author_id = $user_id
  201. AND folder_id = " . PRIVMSGS_OUTBOX;
  202. $result = $db->sql_query($sql);
  203. if ($row = $db->sql_fetchrow($result))
  204. {
  205. if (!function_exists('delete_pm'))
  206. {
  207. include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
  208. }
  209. do
  210. {
  211. $msg_ids[] = (int) $row['msg_id'];
  212. }
  213. while ($row = $db->sql_fetchrow($result));
  214. $db->sql_freeresult($result);
  215. delete_pm($user_id, $msg_ids, PRIVMSGS_OUTBOX);
  216. add_log('admin', 'LOG_USER_DEL_OUTBOX', $user_row['username']);
  217. }
  218. $db->sql_freeresult($result);
  219. }
  220. // Empty the user's profile fields
  221. if ($config['asacp_ocban_delete_profile_fields'])
  222. {
  223. $sql_ary = array(
  224. 'user_birthday' => '',
  225. 'user_from' => '',
  226. 'user_icq' => '',
  227. 'user_aim' => '',
  228. 'user_yim' => '',
  229. 'user_msnm' => '',
  230. 'user_jabber' => '',
  231. 'user_website' => '',
  232. 'user_occ' => '',
  233. 'user_interests' => '',
  234. );
  235. $sql = 'UPDATE ' . USERS_TABLE . '
  236. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  237. WHERE user_id = ' . $user_id;
  238. $db->sql_query($sql);
  239. }
  240. // Submit the information to Stop Forum Spam
  241. if (isset($_POST['sfs_submit']) && $config['asacp_sfs_key'])
  242. {
  243. $data = array(
  244. 'username' => $user_row['username'],
  245. 'email' => $user_row['user_email'],
  246. 'ip_addr' => $user_row['user_ip'],
  247. 'evidence' => substr(utf8_normalize_nfc(request_var('sfs_evidence', '', true)), 0, 7999), // Evidence is limited to 8,000 characters
  248. 'api_key' => $config['asacp_sfs_key'],
  249. );
  250. $errno = $errstr = '';
  251. $domain = 'www.stopforumspam.com';
  252. $fp = @fsockopen($domain, 80, $errno, $errstr, 5);
  253. if ($fp)
  254. {
  255. $post = http_build_query($data);
  256. $out = "POST /add HTTP/1.0\r\n";
  257. $out .= "Host: $domain\r\n";
  258. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  259. $out .= 'Content-Length: ' . strlen($post) . "\r\n\r\n";
  260. $out .= "$post\r\n";
  261. $out .= "Connection: close\r\n";
  262. fwrite($fp, $out);
  263. fclose($fp);
  264. }
  265. }
  266. // Submit the spam to Akismet
  267. if (isset($_POST['akismet_submit']) && $config['asacp_akismet_enable'] && $config['asacp_akismet_key'] && ($post_id = request_var('p', 0)))
  268. {
  269. $sql = 'SELECT * FROM ' . POSTS_TABLE . '
  270. WHERE post_id = ' . $post_id;
  271. $result = $db->sql_query($sql);
  272. $post = $db->sql_fetchrow($result);
  273. $db->sql_freeresult($result);
  274. if ($post)
  275. {
  276. if (!class_exists('Akismet'))
  277. {
  278. global $phpbb_root_path, $phpEx;
  279. include($phpbb_root_path . 'antispam/Akismet.class.' . $phpEx);
  280. }
  281. $post['decoded_text'] = $post['post_text'];
  282. decode_message($post['decoded_text'], $post['bbcode_uid']);
  283. $akismet = new Akismet($config['asacp_akismet_domain'], $config['asacp_akismet_key']);
  284. $akismet->setUserIP($post['poster_ip']);
  285. $akismet->setReferrer('');
  286. $akismet->setCommentUserAgent('');
  287. $akismet->setCommentType('comment');
  288. $akismet->setCommentAuthor($user_row['username']);
  289. $akismet->setCommentAuthorEmail($user_row['user_email']);
  290. $akismet->setCommentContent($post['decoded_text']);
  291. $akismet->submitSpam();
  292. }
  293. }
  294. trigger_error(sprintf($user->lang['ASACP_BAN_COMPLETE'], append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$user_id")));
  295. }
  296. else
  297. {
  298. if (isset($_REQUEST['confirm_key']) && $error)
  299. {
  300. // Hack to fix the confirm_box if we need to come back to it because of an error
  301. unset($_REQUEST['confirm_key']);
  302. }
  303. // Build the ban actions string
  304. $user->add_lang('mods/acp_asacp');
  305. $ban_actions = array();
  306. if ($config['asacp_ocban_username'])
  307. {
  308. $ban_actions[] = $user->lang['ASACP_BAN_USERNAME'];
  309. }
  310. if ($config['asacp_ocban_deactivate'])
  311. {
  312. $ban_actions[] = $user->lang['ASACP_BAN_DEACTIVATE_USER'];
  313. }
  314. if ($config['asacp_ocban_move_to_group'])
  315. {
  316. $sql = 'SELECT group_name FROM ' . GROUPS_TABLE . ' WHERE group_id = ' . $config['asacp_ocban_move_to_group'];
  317. $result = $db->sql_query($sql);
  318. $group_name = $db->sql_fetchfield('group_name');
  319. $db->sql_freeresult($result);
  320. $group_name = (isset($user->lang['G_' . $group_name])) ? $user->lang['G_' . $group_name] : $group_name;
  321. $ban_actions[] = $user->lang['ASACP_BAN_MOVE_TO_GROUP'] . ': ' . $group_name;
  322. }
  323. if ($config['asacp_ocban_delete_posts'])
  324. {
  325. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_POSTS'];
  326. }
  327. if ($config['asacp_ocban_delete_avatar'])
  328. {
  329. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_AVATAR'];
  330. }
  331. if ($config['asacp_ocban_delete_signature'])
  332. {
  333. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_SIGNATURE'];
  334. }
  335. if ($config['asacp_ocban_clear_outbox'])
  336. {
  337. $ban_actions[] = $user->lang['ASACP_BAN_CLEAR_OUTBOX'];
  338. }
  339. if ($config['asacp_ocban_delete_profile_fields'])
  340. {
  341. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_PROFILE_FIELDS'];
  342. }
  343. if ($config['asacp_ocban_blog'] && file_exists($phpbb_root_path . 'blog/includes/functions_admin.' . $phpEx))
  344. {
  345. $ban_actions[] = $user->lang['ASACP_BAN_DELETE_BLOG'];
  346. }
  347. $post = false;
  348. if (($post_id = request_var('p', 0)))
  349. {
  350. $sql = 'SELECT * FROM ' . POSTS_TABLE . '
  351. WHERE post_id = ' . $post_id;
  352. $result = $db->sql_query($sql);
  353. $post = $db->sql_fetchrow($result);
  354. $db->sql_freeresult($result);
  355. if ($post)
  356. {
  357. $post['decoded_text'] = $post['post_text'];
  358. decode_message($post['decoded_text'], $post['bbcode_uid']);
  359. }
  360. }
  361. $template->assign_vars(array(
  362. 'POST_TEXT' => (is_array($post)) ? $post['post_text'] : false,
  363. 'S_BAN_USER' => $config['asacp_ocban_username'],
  364. 'S_AKISMET_SUBMIT' => ($config['asacp_akismet_enable'] && $config['asacp_akismet_key'] && is_array($post)) ? true : false,
  365. 'S_SFS_SUBMIT' => ($config['asacp_sfs_key']) ? true : false,
  366. 'BAN_REASON' => utf8_normalize_nfc(request_var('ban_reason', '', true)),
  367. 'AKISMET_SUBMIT' => (isset($_POST['akismet_submit'])) ? true : false,
  368. 'AKISMET_TEXT' => (is_array($post)) ? $post['decoded_text'] : '',
  369. 'SFS_SUBMIT' => (isset($_POST['sfs_submit'])) ? true : false,
  370. 'SFS_EVIDENCE' => (!isset($_POST['confirm']) && !request_var('sfs_evidence', '', true) && is_array($post)) ? $post['decoded_text'] : utf8_normalize_nfc(request_var('sfs_evidence', '', true)),
  371. 'SFS_EVIDENCE_ERROR' => ($error) ? true : false,
  372. 'L_ASACP_BAN_ACTIONS' => sprintf($user->lang['ASACP_BAN_ACTIONS'], implode(', ', $ban_actions)),
  373. ));
  374. $user->lang['ASACP_BAN_CONFIRM'] = sprintf($user->lang['ASACP_BAN_CONFIRM'], $username);
  375. confirm_box(false, 'ASACP_BAN', '', 'antispam/oc_ban.html', "antispam/index.{$phpEx}?mode=ocban&amp;u=$user_id&amp;p=$post_id");
  376. }
  377. break;
  378. default :
  379. trigger_error('NO_MODE');
  380. break;
  381. }
  382. // Should not get here (unless No selected for the confirm_box)
  383. redirect($return_url);
  384. ?>