/ee2/system/third_party/spamfreeform/ext.spamfreeform.php

https://github.com/amphibian/spamfreeform.ee_addon · PHP · 170 lines · 143 code · 27 blank · 0 comment · 21 complexity · dec00c5d8171bfdff13696fb36761e40 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Spamfreeform_ext
  3. {
  4. var $settings = array();
  5. var $name = 'Spam-Freeform';
  6. var $version = '1.0';
  7. var $description = 'Runs Freeform submissions through the Akismet anti-spam service.';
  8. var $settings_exist = 'y';
  9. var $docs_url = 'http://github.com/amphibian/spamfreeform.ee_addon';
  10. function __construct($settings='')
  11. {
  12. $this->EE =& get_instance();
  13. $this->settings = $settings;
  14. $this->site = urlencode('http://'.$_SERVER['SERVER_NAME']);
  15. }
  16. function settings()
  17. {
  18. $settings = array();
  19. $settings['spamfreeform_api_key'] = '';
  20. $settings['spamfreeform_is_spam'] = array('t', '', 'Your submission appears to be spam. Please adjust your submission and try again.');
  21. return $settings;
  22. }
  23. function freeform_module_validate_end($errors)
  24. {
  25. $content = '';
  26. if($this->EE->input->post('spamfreeform_fields') && $this->EE->input->post('spamfreeform_fields') != '')
  27. {
  28. $fields = explode('|', $this->EE->input->post('spamfreeform_fields'));
  29. foreach($fields as $field)
  30. {
  31. $content .= ($this->EE->input->post($field)) ? $this->EE->input->post($field).' ' : '';
  32. }
  33. }
  34. if($this->_verify_key() == TRUE && !empty($content))
  35. {
  36. $query = array(
  37. 'blog' => $this->site,
  38. 'comment_content' => $content,
  39. 'referrer' => $_SERVER['HTTP_REFERER'],
  40. 'user_agent' => $_SERVER['HTTP_USER_AGENT'],
  41. 'user_ip' => $this->EE->input->ip_address()
  42. );
  43. if($this->EE->input->post('spamfreeform_name') && $this->EE->input->post('spamfreeform_name') != '')
  44. {
  45. $name = '';
  46. $fields = explode('|', $this->EE->input->post('spamfreeform_name'));
  47. foreach($fields as $field)
  48. {
  49. $name .= ($this->EE->input->post($field)) ? $this->EE->input->post($field).' ' : '';
  50. }
  51. $query['comment_author'] = $name;
  52. }
  53. if($this->EE->input->post('spamfreeform_email') && $this->EE->input->post('spamfreeform_email') != '')
  54. {
  55. $query['comment_author_email'] = $this->EE->input->post($this->EE->input->post('spamfreeform_email'));
  56. }
  57. $response = $this->_request('https://'.trim($this->settings['spamfreeform_api_key']).'.rest.akismet.com/1.1/comment-check', $query);
  58. $response = explode("\r\n\r\n", $response, 2);
  59. if($response[0] == 'true')
  60. {
  61. $errors[] = $this->settings['spamfreeform_is_spam'];
  62. }
  63. }
  64. return $errors;
  65. }
  66. function _verify_key()
  67. {
  68. if(isset($this->settings['spamfreeform_api_key']))
  69. {
  70. $query = array(
  71. 'blog' => $this->site,
  72. 'key' => trim($this->settings['spamfreeform_api_key'])
  73. );
  74. $response = $this->_request('https://rest.akismet.com/1.1/verify-key', $query);
  75. if(trim($response) == 'valid')
  76. {
  77. return TRUE;
  78. }
  79. else
  80. {
  81. return FALSE;
  82. }
  83. }
  84. else
  85. {
  86. return FALSE;
  87. }
  88. }
  89. function _request($server, $query)
  90. {
  91. $args = '';
  92. foreach ($query as $key => $value)
  93. {
  94. $args .= trim($key).'='.trim($value).'&';
  95. }
  96. $args = rtrim($args, '&');
  97. $ch = curl_init($server);
  98. curl_setopt($ch, CURLOPT_HEADER, 0);
  99. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  100. curl_setopt($ch, CURLOPT_POST, 1);
  101. curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
  102. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  103. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  104. $response = curl_exec($ch);
  105. curl_close($ch);
  106. return $response;
  107. }
  108. function activate_extension()
  109. {
  110. $hooks = array(
  111. 'freeform_module_validate_end' => 'freeform_module_validate_end'
  112. );
  113. foreach($hooks as $hook => $method)
  114. {
  115. $this->EE->db->query($this->EE->db->insert_string('exp_extensions',
  116. array(
  117. 'class' => ucfirst(get_class($this)),
  118. 'method' => $method,
  119. 'hook' => $hook,
  120. 'settings' => '',
  121. 'priority' => 99,
  122. 'version' => $this->version,
  123. 'enabled' => "y"
  124. )
  125. )
  126. );
  127. }
  128. }
  129. function update_extension($current='')
  130. {
  131. if ($current == '' OR $current == $this->version)
  132. {
  133. return FALSE;
  134. }
  135. $this->EE->db->query("UPDATE exp_extensions
  136. SET version = '". $this->EE->db->escape_str($this->version)."'
  137. WHERE class = '".ucfirst(get_class($this))."'");
  138. }
  139. function disable_extension()
  140. {
  141. $this->EE->db->query("DELETE FROM exp_extensions WHERE class = '".ucfirst(get_class($this))."'");
  142. }
  143. }