/mods/spamhurdles/spamhurdles.php

https://github.com/mysnip/Core · PHP · 353 lines · 174 code · 62 blank · 117 comment · 35 complexity · 954b942a870dd84fcd0c40f77a9f46b8 MD5 · raw file

  1. <?php
  2. if (!defined("PHORUM")) return;
  3. require_once("./mods/spamhurdles/api.php");
  4. $GLOBALS['PHORUM']['DATA']['SPAMHURDLES_BEFORE_FOOTER'] = '';
  5. // ----------------------------------------------------------------------
  6. // General purpose hooks
  7. // ----------------------------------------------------------------------
  8. // {{{ Function: phorum_mod_spamhurdles_css_register
  9. /**
  10. * Register the additional CSS code for this module.
  11. */
  12. function phorum_mod_spamhurdles_css_register($data)
  13. {
  14. if ($data['css'] != 'css') return $data;
  15. $data['register'][] = array(
  16. "module" => "spamhurdles",
  17. "where" => "after",
  18. "source" => "template(spamhurdles::css)"
  19. );
  20. return $data;
  21. }
  22. // }}}
  23. // {{{ Function: phorum_mod_spamhurdles_javascript_register
  24. /**
  25. * Register the javascript code for this module.
  26. */
  27. function phorum_mod_spamhurdles_javascript_register($data)
  28. {
  29. // A function that can be used to evaluate blocks of JavaScript code
  30. // inside a block of page data. This is used by the modified
  31. // iScramble code that is in use by this module.
  32. $data[] = array(
  33. "module" => "spamhurdles",
  34. "source" => "file(mods/spamhurdles/spamhurdles.js)"
  35. );
  36. // Allow spam hurdle implementations to add javascript code.
  37. $data = spamhurdles_api_javascript_register($data);
  38. return $data;
  39. }
  40. // }}}
  41. // {{{ Function: phorum_mod_spamhurdles_before_footer
  42. /**
  43. * Add data that needs to go to the end of the page.
  44. * The form specific hooks will fill the template variable
  45. * {SPAMHURDLES_BEFORE_FOOTER}. This hook will take care
  46. * of putting that data in the page.
  47. */
  48. function phorum_mod_spamhurdles_before_footer()
  49. {
  50. global $PHORUM;
  51. if (!empty($PHORUM['DATA']['SPAMHURDLES_BEFORE_FOOTER'])) {
  52. print $PHORUM['DATA']['SPAMHURDLES_BEFORE_FOOTER'];
  53. }
  54. }
  55. // }}}
  56. // {{{ Function: phorum_mod_spamhurdles_addon
  57. function phorum_mod_spamhurdles_addon()
  58. {
  59. global $PHORUM;
  60. $args = $PHORUM['args'];
  61. if (isset($args['hurdle'])) {
  62. spamhurdles_hurdle_call('addon', $args, array($args['hurdle']));
  63. }
  64. }
  65. // }}}
  66. // ----------------------------------------------------------------------
  67. // Implement protection for posting messages
  68. // ----------------------------------------------------------------------
  69. // {{{ Function: phorum_mod_spamhurdles_tpl_editor_before_textarea
  70. /**
  71. * Extend the message posting form with spam hurdles data.
  72. */
  73. function phorum_mod_spamhurdles_tpl_editor_before_textarea()
  74. {
  75. global $PHORUM;
  76. if (phorum_page != "post" && phorum_page != "read") return;
  77. // Only run the spamhurdle checks when writing a new message.
  78. // We do not need the checks for editing existing messages.
  79. if (isset($PHORUM["DATA"]["POSTING"]["message_id"])) {
  80. if (!empty($PHORUM["DATA"]["POSTING"]["message_id"])) return;
  81. } else trigger_error(
  82. "phorum_mod_spamhurdles_tpl_editor_before_textarea(): " .
  83. "Can't determine whether we're editing a new message",
  84. E_USER_ERROR
  85. );
  86. // Initialize the form, unless we have spamhurdles data in the
  87. // request already.
  88. if (!isset($_POST['spamhurdles_posting']))
  89. {
  90. $data = spamhurdles_api_init(
  91. 'posting',
  92. spamhurdles_get_hurdles_for_form('posting')
  93. );
  94. $data['use_editor_block'] = 'posting';
  95. }
  96. // We are not initializing the form.
  97. // Valid Spam Hurdles data must be available.
  98. else
  99. {
  100. $data = spamhurdles_api_get_formdata('posting');
  101. if ($data === NULL) trigger_error(
  102. 'No "spamhurdles_posting" data field was found in the POST ' .
  103. 'request. This should not happen.',
  104. E_USER_ERROR
  105. );
  106. }
  107. // Output the required form data.
  108. print spamhurdles_api_build_form($data);
  109. // Prepare the data that needs to be added after the form.
  110. // We will display it from the before_footer hook, but since
  111. // we have the spam hurdles data at hand here, it's easiest
  112. // to format the after form data here.
  113. $PHORUM['DATA']['SPAMHURDLES_BEFORE_FOOTER'] .=
  114. spamhurdles_api_build_after_form($data);
  115. }
  116. // }}}
  117. // {{{ Function: phorum_mod_spamhurdles_check_post
  118. /**
  119. * Check posted editor data.
  120. */
  121. function phorum_mod_spamhurdles_check_post($args)
  122. {
  123. global $PHORUM;
  124. list ($message, $error) = $args;
  125. // Return if another module already set an error.
  126. if (!empty($error)) return $args;
  127. // Our checks are only needed when finishing a post.
  128. if (!isset($_POST["finish"])) return $args;
  129. // Only run the checks when we are editing a new message.
  130. if (!empty($message["message_id"])) return $args;
  131. // Run the spam hurdle checks. If the checks fail, then check what
  132. // the block action is. For "unapprove", we will still post the
  133. // message, only in an unapproved state. Otherwise, we display
  134. // an error to the user.
  135. list($hurdle_status, $hurdle_error) = spamhurdles_api_check_form('posting');
  136. if ($hurdle_status == SPAMHURDLES_FATAL) {
  137. $block_action = $PHORUM['mod_spamhurdles']['posting']['block_action'];
  138. if ($block_action == 'unapprove') {
  139. // The pre_post hook will use this to make the message moderated.
  140. $PHORUM['mod_spamhurdles_unapprove'] = TRUE;
  141. } else {
  142. $error = $hurdle_error;
  143. }
  144. }
  145. elseif ($hurdle_status == SPAMHURDLES_WARNING) {
  146. $error = $hurdle_error;
  147. }
  148. return array($message, $error);
  149. }
  150. // }}}
  151. // {{{ Function: phorum_mod_spamhurdles_pre_post
  152. /**
  153. * Handle marking the message unapproved if requested by this mod.
  154. */
  155. function phorum_mod_spamhurdles_pre_post($message)
  156. {
  157. global $PHORUM;
  158. if (!empty($PHORUM['mod_spamhurdles_unapprove'])) {
  159. $message['status'] = PHORUM_STATUS_HOLD;
  160. }
  161. return $message;
  162. }
  163. // }}}
  164. // {{{ Function: phorum_mod_spamhurdles_after_post
  165. /**
  166. * Let hurdles run tasks after posting a message.
  167. */
  168. function phorum_mod_spamhurdles_after_post($message)
  169. {
  170. spamhurdles_api_after_post('posting');
  171. return $message;
  172. }
  173. // }}}
  174. // ----------------------------------------------------------------------
  175. // Implement protection for account registration
  176. // ----------------------------------------------------------------------
  177. // {{{ Function: phorum_mod_spamhurdles_tpl_register_form
  178. /**
  179. * Extend the account registration form with spam hurdles data.
  180. */
  181. function phorum_mod_spamhurdles_tpl_register_form()
  182. {
  183. global $PHORUM;
  184. // Initialize the form, unless we have spamhurdles data in the
  185. // request already.
  186. if (!isset($_POST['spamhurdles_register']))
  187. {
  188. $data = spamhurdles_api_init(
  189. 'register',
  190. spamhurdles_get_hurdles_for_form('register')
  191. );
  192. }
  193. // We are not initializing the form.
  194. // Valid Spam Hurdles data must be available.
  195. else
  196. {
  197. $data = spamhurdles_api_get_formdata('register');
  198. if ($data === NULL) trigger_error(
  199. 'No "spamhurdles_register" data field was found in the POST ' .
  200. 'request. This should not happen.',
  201. E_USER_ERROR
  202. );
  203. }
  204. // Output the required form data.
  205. print spamhurdles_api_build_form($data);
  206. // Prepare the data that needs to be added after the form.
  207. // We will display it from the before_footer hook, but since
  208. // we have the spam hurdles data at hand here, it's easiest
  209. // to format the after form data here.
  210. $PHORUM['DATA']['SPAMHURDLES_BEFORE_FOOTER'] .=
  211. spamhurdles_api_build_after_form($data);
  212. }
  213. // }}}
  214. // {{{ Function: phorum_mod_spamhurdles_before_register
  215. /**
  216. * Check posted account registration data.
  217. */
  218. function phorum_mod_spamhurdles_before_register($user)
  219. {
  220. // Return if another module already set an error.
  221. if (isset($user["error"]) && !empty($user["error"])) {
  222. return $user;
  223. }
  224. // Run the spam hurdle checks.
  225. list ($status, $error) = spamhurdles_api_check_form('register');
  226. if ($status != SPAMHURDLES_OK) {
  227. $user["error"] = $error;
  228. }
  229. return $user;
  230. }
  231. // }}}
  232. // ----------------------------------------------------------------------
  233. // Implement protection for PM posting
  234. // ----------------------------------------------------------------------
  235. // {{{ Function: phorum_mod_spamhurdles_tpl_pm_editor_before_textarea
  236. /**
  237. * Extend the PM sending form with spam hurdles data.
  238. */
  239. function phorum_mod_spamhurdles_tpl_pm_editor_before_textarea()
  240. {
  241. global $PHORUM;
  242. // Initialize the form, unless we have spamhurdles data in the
  243. // request already.
  244. if (!isset($_POST['spamhurdles_pm']))
  245. {
  246. $data = spamhurdles_api_init(
  247. 'pm',
  248. spamhurdles_get_hurdles_for_form('pm')
  249. );
  250. $data['use_editor_block'] = 'pm';
  251. }
  252. // We are not initializing the form.
  253. // Valid Spam Hurdles data must be available.
  254. else
  255. {
  256. $data = spamhurdles_api_get_formdata('pm');
  257. if ($data === NULL) trigger_error(
  258. 'No "spamhurdles_pm" data field was found in the POST request. ' .
  259. 'This should not happen.',
  260. E_USER_ERROR
  261. );
  262. }
  263. // Output the required form data.
  264. print spamhurdles_api_build_form($data);
  265. // Prepare the data that needs to be added after the form.
  266. // We will display it from the before_footer hook, but since
  267. // we have the spam hurdles data at hand here, it's easiest
  268. // to format the after form data here.
  269. $PHORUM['DATA']['SPAMHURDLES_BEFORE_FOOTER'] .=
  270. spamhurdles_api_build_after_form($data);
  271. }
  272. // }}}
  273. // {{{ Function: phorum_mod_spamhurdles_pm_before_send
  274. /**
  275. * Check posted account registration data.
  276. */
  277. function phorum_mod_spamhurdles_pm_before_send($message)
  278. {
  279. // Return if another module already set an error.
  280. if ($message['error'] !== NULL) {
  281. return $message;
  282. }
  283. // Run the spam hurdle checks.
  284. list ($status, $error) = spamhurdles_api_check_form('pm');
  285. if ($status != SPAMHURDLES_OK) {
  286. $message["error"] = $error;
  287. }
  288. return $message;
  289. }
  290. // }}}
  291. ?>