mollom.pages.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @file
  4. * Various non-administration page callbacks for the mollom module.
  5. */
  6. /**
  7. * AJAX callback to retrieve a CAPTCHA.
  8. *
  9. * @param $type
  10. * The new CAPTCHA type to retrieve, e.g. 'image' or 'audio'.
  11. * @param $form_build_id
  12. * The internal form build id of the form to update the CAPTCHA for.
  13. * @param $contentId
  14. * (optional) The associated content ID in the form.
  15. *
  16. * @return
  17. * A JSON array containing:
  18. * - content: The HTML markup for the new CAPTCHA.
  19. * - captchaId: The ID for the new CAPTCHA.
  20. *
  21. * @todo Add error handling.
  22. */
  23. function mollom_captcha_js($type, $form_build_id, $contentId = NULL) {
  24. // Deny GET requests to make automated security audit tools not complain
  25. // about a JSON Hijacking possibility.
  26. // @see http://capec.mitre.org/data/definitions/111.html
  27. // @see http://haacked.com/archive/2009/06/24/json-hijacking.aspx
  28. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  29. header($_SERVER['SERVER_PROTOCOL'] . ' 405 Method Not Allowed');
  30. // A HTTP 405 response MUST specify allowed methods.
  31. header('Allow: POST');
  32. drupal_exit();
  33. }
  34. // Load $form_state from cache or create a dummy state.
  35. $cid = 'form_state_' . $form_build_id;
  36. if ($cache = cache_get($cid, 'cache_form')) {
  37. $form_state = $cache->data;
  38. }
  39. else {
  40. $form_state['mollom'] = array();
  41. if (isset($contentId)) {
  42. $form_state['values']['mollom']['contentId'] = $contentId;
  43. }
  44. }
  45. $form_state['mollom']['captcha_type'] = $type;
  46. $captcha = mollom_get_captcha($form_state);
  47. if (!empty($form_state['mollom']['response']['captcha']['id'])) {
  48. // Update the CAPTCHA ID in the cached $form_state, since it might have
  49. // changed.
  50. // @todo Replace the entire CAPTCHA switch/refresh with new AJAX framework
  51. // functionality.
  52. if ($cache) {
  53. cache_set($cid, $form_state, 'cache_form', REQUEST_TIME + 21600);
  54. }
  55. // Return new content and CAPTCHA ID via JSON.
  56. $data = array(
  57. 'content' => $captcha,
  58. 'captchaId' => $form_state['mollom']['response']['captcha']['id'],
  59. );
  60. drupal_json_output($data);
  61. }
  62. drupal_exit();
  63. }
  64. /**
  65. * Form builder for report to Mollom form.
  66. *
  67. * @param $entity
  68. * The entity type of the data to report, e.g. 'node' or 'comment'.
  69. * @param $id
  70. * The entity id the data belongs to.
  71. *
  72. * @see mollom_report_access()
  73. */
  74. function mollom_report_form($form, &$form_state, $entity, $id) {
  75. $form['entity'] = array(
  76. '#type' => 'value',
  77. '#value' => $entity,
  78. );
  79. $form['id'] = array(
  80. '#type' => 'value',
  81. '#value' => $id,
  82. );
  83. // @todo "Delete" does not work for reporting mails to Mollom. In D7+, this
  84. // form should be used solely for mails, as other entities are reported
  85. // through existing delete confirmation forms instead. Perhaps there should
  86. // be a dedicated form for reporting mails, as they are not really
  87. // compatible with any of the standard processes either way.
  88. $form = confirm_form($form,
  89. t('Are you sure you want to delete and report the content as inappropriate?'),
  90. '<front>',
  91. t('This action cannot be undone.'),
  92. t('Delete'), t('Cancel')
  93. );
  94. mollom_data_delete_form_alter($form, $form_state);
  95. return $form;
  96. }
  97. /**
  98. * Form submit handler for mollom_report_form().
  99. */
  100. function mollom_report_form_submit($form, &$form_state) {
  101. if ($form_state['values']['confirm']) {
  102. $entity = $form_state['values']['entity'];
  103. $id = $form_state['values']['id'];
  104. // Load the Mollom session data.
  105. $data = mollom_data_load($entity, $id);
  106. // Send feedback to Mollom, if we have session data.
  107. if ((!empty($data->contentId) || !empty($data->captchaId)) && !empty($form_state['values']['mollom']['feedback'])) {
  108. if (_mollom_send_feedback($data, $form_state['values']['mollom']['feedback'], 'moderate', 'mollom_report_form_submit')) {
  109. drupal_set_message(t('The content was successfully reported as inappropriate.'));
  110. }
  111. }
  112. // Delete the content. The callback should take care of proper deletion and
  113. // cache clearing on its own.
  114. foreach (mollom_form_list() as $form_id => $info) {
  115. if (!isset($info['entity']) || $info['entity'] != $entity) {
  116. continue;
  117. }
  118. // If there is a 'report delete callback', invoke it.
  119. if (isset($info['report delete callback']) && function_exists($info['report delete callback'])) {
  120. $function = $info['report delete callback'];
  121. $function($entity, $id);
  122. break;
  123. }
  124. }
  125. $form_state['redirect'] = '<front>';
  126. }
  127. }