PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/plugins/Akismet/Plugin.php

http://typecho.googlecode.com/
PHP | 213 lines | 128 code | 21 blank | 64 comment | 22 complexity | 9279375e223ef0baeda38568b8fee7bd MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Akismet ??????? for Typecho
  4. *
  5. * @package Akismet
  6. * @author qining
  7. * @version 1.1.4
  8. * @link http://typecho.org
  9. */
  10. class Akismet_Plugin implements Typecho_Plugin_Interface
  11. {
  12. /**
  13. * ??????,??????,??????
  14. *
  15. * @access public
  16. * @return void
  17. * @throws Typecho_Plugin_Exception
  18. */
  19. public static function activate()
  20. {
  21. if (false == Typecho_Http_Client::get()) {
  22. throw new Typecho_Plugin_Exception(_t('???, ??????? php-curl ???????? allow_url_fopen ??, ?????????'));
  23. }
  24. Typecho_Plugin::factory('Widget_Feedback')->comment = array('Akismet_Plugin', 'filter');
  25. Typecho_Plugin::factory('Widget_Feedback')->trackback = array('Akismet_Plugin', 'filter');
  26. Typecho_Plugin::factory('Widget_XmlRpc')->pingback = array('Akismet_Plugin', 'filter');
  27. Typecho_Plugin::factory('Widget_Comments_Edit')->mark = array('Akismet_Plugin', 'mark');
  28. return _t('???????API KEY, ???????????');
  29. }
  30. /**
  31. * ??????,??????,??????
  32. *
  33. * @static
  34. * @access public
  35. * @return void
  36. * @throws Typecho_Plugin_Exception
  37. */
  38. public static function deactivate(){}
  39. /**
  40. * ????????
  41. *
  42. * @access public
  43. * @param Typecho_Widget_Helper_Form $form ????
  44. * @return void
  45. */
  46. public static function config(Typecho_Widget_Helper_Form $form)
  47. {
  48. $key = new Typecho_Widget_Helper_Form_Element_Textarea('key', NULL, NULL, _t('????'), _t('?????????????<br />
  49. ???????????????????'));
  50. $form->addInput($key->addRule('required', _t('???????????'))
  51. ->addRule(array('Akismet_Plugin', 'validate'), _t('??????????')));
  52. $url = new Typecho_Widget_Helper_Form_Element_Text('url', NULL, 'http://rest.akismet.com',
  53. _t('????'), _t('??????????????????<br />
  54. ??????? <a href="http://akismet.com">Akismet</a> ?? <a href="http://antispam.typepad.com">Typepad</a> ??????'));
  55. $form->addInput($url->addRule('url', _t('??????????')));
  56. }
  57. /**
  58. * ?????????
  59. *
  60. * @access public
  61. * @param Typecho_Widget_Helper_Form $form
  62. * @return void
  63. */
  64. public static function personalConfig(Typecho_Widget_Helper_Form $form){}
  65. /**
  66. * ??api?key?
  67. *
  68. * @access public
  69. * @param string $key ????
  70. * @return boolean
  71. */
  72. public static function validate($key)
  73. {
  74. $options = Typecho_Widget::widget('Widget_Options');
  75. $url = Typecho_Request::getInstance()->url;
  76. $data = array(
  77. 'key' => $key,
  78. 'blog' => $options->siteUrl
  79. );
  80. $client = Typecho_Http_Client::get('Curl', 'Socket');
  81. if (false != $client) {
  82. $client->setData($data)
  83. ->setHeader('User-Agent', $options->generator . ' | Akismet/1.1')
  84. ->send(Typecho_Common::url('/1.1/verify-key', $url));
  85. if ('valid' == $client->getResponseBody()) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. /**
  92. * ????????????
  93. *
  94. * @access public
  95. * @param array $comment ????????
  96. * @param Typecho_Widget $commentWidget ????
  97. * @param string $status ????
  98. * @return void
  99. */
  100. public static function mark($comment, $commentWidget, $status)
  101. {
  102. if ('spam' == $comment['status'] && $status != 'spam') {
  103. self::filter($comment, $commentWidget, NULL, 'submit-ham');
  104. } else if ('spam' != $comment['status'] && $status == 'spam') {
  105. self::filter($comment, $commentWidget, NULL, 'submit-spam');
  106. }
  107. }
  108. /**
  109. * ?????
  110. *
  111. * @access public
  112. * @param array $comment ????
  113. * @param Typecho_Widget $post ??????
  114. * @param array $result ????????
  115. * @param string $api api??
  116. * @return void
  117. */
  118. public static function filter($comment, $post, $result, $api = 'comment-check')
  119. {
  120. $comment = empty($result) ? $comment : $result;
  121. $options = Typecho_Widget::widget('Widget_Options');
  122. $url = $options->plugin('Akismet')->url;
  123. $key = $options->plugin('Akismet')->key;
  124. $allowedServerVars = 'comment-check' == $api ? array(
  125. 'SCRIPT_URI',
  126. 'HTTP_HOST',
  127. 'HTTP_USER_AGENT',
  128. 'HTTP_ACCEPT',
  129. 'HTTP_ACCEPT_LANGUAGE',
  130. 'HTTP_ACCEPT_ENCODING',
  131. 'HTTP_ACCEPT_CHARSET',
  132. 'HTTP_KEEP_ALIVE',
  133. 'HTTP_CONNECTION',
  134. 'HTTP_CACHE_CONTROL',
  135. 'HTTP_PRAGMA',
  136. 'HTTP_DATE',
  137. 'HTTP_EXPECT',
  138. 'HTTP_MAX_FORWARDS',
  139. 'HTTP_RANGE',
  140. 'CONTENT_TYPE',
  141. 'CONTENT_LENGTH',
  142. 'SERVER_SIGNATURE',
  143. 'SERVER_SOFTWARE',
  144. 'SERVER_NAME',
  145. 'SERVER_ADDR',
  146. 'SERVER_PORT',
  147. 'REMOTE_PORT',
  148. 'GATEWAY_INTERFACE',
  149. 'SERVER_PROTOCOL',
  150. 'REQUEST_METHOD',
  151. 'QUERY_STRING',
  152. 'REQUEST_URI',
  153. 'SCRIPT_NAME',
  154. 'REQUEST_TIME'
  155. ) : array();
  156. $data = array(
  157. 'blog' => $options->siteUrl,
  158. 'user_ip' => $comment['ip'],
  159. 'user_agent' => $comment['agent'],
  160. 'referrer' => Typecho_Request::getInstance()->getReferer(),
  161. 'permalink' => $post->permalink,
  162. 'comment_type' => $comment['type'],
  163. 'comment_author' => $comment['author'],
  164. 'comment_author_email' => $comment['mail'],
  165. 'comment_author_url' => $comment['url'],
  166. 'comment_content' => $comment['text']
  167. );
  168. foreach ($allowedServerVars as $val) {
  169. if (array_key_exists($val, $_SERVER)) {
  170. $data[$val] = $_SERVER[$val];
  171. }
  172. }
  173. try {
  174. $client = Typecho_Http_Client::get();
  175. if (false != $client && $key) {
  176. $params = parse_url($url);
  177. $url = $params['scheme'] . '://' . $key . '.' . $params['host'] . (isset($params['path']) ? $params['path'] : NULL);
  178. $client->setHeader('User-Agent', $options->generator . ' | Akismet/1.1')
  179. ->setTimeout(5)
  180. ->setData($data)
  181. ->send(Typecho_Common::url('/1.1/' . $api, $url));
  182. if ('true' == $client->getResponseBody()) {
  183. $comment['status'] = 'spam';
  184. }
  185. }
  186. } catch (Typecho_Http_Client_Exception $e) {
  187. //do nothing
  188. error_log($e->getMessage());
  189. }
  190. return $comment;
  191. }
  192. }