/usr/plugins/Akismet/Plugin.php
PHP | 213 lines | 128 code | 21 blank | 64 comment | 22 complexity | 9279375e223ef0baeda38568b8fee7bd MD5 | raw file
Possible License(s): AGPL-1.0
- <?php
- /**
- * Akismet ??????? for Typecho
- *
- * @package Akismet
- * @author qining
- * @version 1.1.4
- * @link http://typecho.org
- */
- class Akismet_Plugin implements Typecho_Plugin_Interface
- {
- /**
- * ??????,??????,??????
- *
- * @access public
- * @return void
- * @throws Typecho_Plugin_Exception
- */
- public static function activate()
- {
- if (false == Typecho_Http_Client::get()) {
- throw new Typecho_Plugin_Exception(_t('???, ??????? php-curl ???????? allow_url_fopen ??, ?????????'));
- }
-
- Typecho_Plugin::factory('Widget_Feedback')->comment = array('Akismet_Plugin', 'filter');
- Typecho_Plugin::factory('Widget_Feedback')->trackback = array('Akismet_Plugin', 'filter');
- Typecho_Plugin::factory('Widget_XmlRpc')->pingback = array('Akismet_Plugin', 'filter');
- Typecho_Plugin::factory('Widget_Comments_Edit')->mark = array('Akismet_Plugin', 'mark');
-
- return _t('???????API KEY, ???????????');
- }
-
- /**
- * ??????,??????,??????
- *
- * @static
- * @access public
- * @return void
- * @throws Typecho_Plugin_Exception
- */
- public static function deactivate(){}
-
- /**
- * ????????
- *
- * @access public
- * @param Typecho_Widget_Helper_Form $form ????
- * @return void
- */
- public static function config(Typecho_Widget_Helper_Form $form)
- {
- $key = new Typecho_Widget_Helper_Form_Element_Textarea('key', NULL, NULL, _t('????'), _t('?????????????<br />
- ???????????????????'));
- $form->addInput($key->addRule('required', _t('???????????'))
- ->addRule(array('Akismet_Plugin', 'validate'), _t('??????????')));
-
- $url = new Typecho_Widget_Helper_Form_Element_Text('url', NULL, 'http://rest.akismet.com',
- _t('????'), _t('??????????????????<br />
- ??????? <a href="http://akismet.com">Akismet</a> ?? <a href="http://antispam.typepad.com">Typepad</a> ??????'));
- $form->addInput($url->addRule('url', _t('??????????')));
- }
-
- /**
- * ?????????
- *
- * @access public
- * @param Typecho_Widget_Helper_Form $form
- * @return void
- */
- public static function personalConfig(Typecho_Widget_Helper_Form $form){}
-
- /**
- * ??api?key?
- *
- * @access public
- * @param string $key ????
- * @return boolean
- */
- public static function validate($key)
- {
- $options = Typecho_Widget::widget('Widget_Options');
- $url = Typecho_Request::getInstance()->url;
-
- $data = array(
- 'key' => $key,
- 'blog' => $options->siteUrl
- );
-
- $client = Typecho_Http_Client::get('Curl', 'Socket');
- if (false != $client) {
- $client->setData($data)
- ->setHeader('User-Agent', $options->generator . ' | Akismet/1.1')
- ->send(Typecho_Common::url('/1.1/verify-key', $url));
-
- if ('valid' == $client->getResponseBody()) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * ????????????
- *
- * @access public
- * @param array $comment ????????
- * @param Typecho_Widget $commentWidget ????
- * @param string $status ????
- * @return void
- */
- public static function mark($comment, $commentWidget, $status)
- {
- if ('spam' == $comment['status'] && $status != 'spam') {
- self::filter($comment, $commentWidget, NULL, 'submit-ham');
- } else if ('spam' != $comment['status'] && $status == 'spam') {
- self::filter($comment, $commentWidget, NULL, 'submit-spam');
- }
- }
-
- /**
- * ?????
- *
- * @access public
- * @param array $comment ????
- * @param Typecho_Widget $post ??????
- * @param array $result ????????
- * @param string $api api??
- * @return void
- */
- public static function filter($comment, $post, $result, $api = 'comment-check')
- {
- $comment = empty($result) ? $comment : $result;
-
- $options = Typecho_Widget::widget('Widget_Options');
- $url = $options->plugin('Akismet')->url;
- $key = $options->plugin('Akismet')->key;
-
- $allowedServerVars = 'comment-check' == $api ? array(
- 'SCRIPT_URI',
- 'HTTP_HOST',
- 'HTTP_USER_AGENT',
- 'HTTP_ACCEPT',
- 'HTTP_ACCEPT_LANGUAGE',
- 'HTTP_ACCEPT_ENCODING',
- 'HTTP_ACCEPT_CHARSET',
- 'HTTP_KEEP_ALIVE',
- 'HTTP_CONNECTION',
- 'HTTP_CACHE_CONTROL',
- 'HTTP_PRAGMA',
- 'HTTP_DATE',
- 'HTTP_EXPECT',
- 'HTTP_MAX_FORWARDS',
- 'HTTP_RANGE',
- 'CONTENT_TYPE',
- 'CONTENT_LENGTH',
- 'SERVER_SIGNATURE',
- 'SERVER_SOFTWARE',
- 'SERVER_NAME',
- 'SERVER_ADDR',
- 'SERVER_PORT',
- 'REMOTE_PORT',
- 'GATEWAY_INTERFACE',
- 'SERVER_PROTOCOL',
- 'REQUEST_METHOD',
- 'QUERY_STRING',
- 'REQUEST_URI',
- 'SCRIPT_NAME',
- 'REQUEST_TIME'
- ) : array();
-
- $data = array(
- 'blog' => $options->siteUrl,
- 'user_ip' => $comment['ip'],
- 'user_agent' => $comment['agent'],
- 'referrer' => Typecho_Request::getInstance()->getReferer(),
- 'permalink' => $post->permalink,
- 'comment_type' => $comment['type'],
- 'comment_author' => $comment['author'],
- 'comment_author_email' => $comment['mail'],
- 'comment_author_url' => $comment['url'],
- 'comment_content' => $comment['text']
- );
-
- foreach ($allowedServerVars as $val) {
- if (array_key_exists($val, $_SERVER)) {
- $data[$val] = $_SERVER[$val];
- }
- }
- try {
- $client = Typecho_Http_Client::get();
- if (false != $client && $key) {
- $params = parse_url($url);
- $url = $params['scheme'] . '://' . $key . '.' . $params['host'] . (isset($params['path']) ? $params['path'] : NULL);
- $client->setHeader('User-Agent', $options->generator . ' | Akismet/1.1')
- ->setTimeout(5)
- ->setData($data)
- ->send(Typecho_Common::url('/1.1/' . $api, $url));
- if ('true' == $client->getResponseBody()) {
- $comment['status'] = 'spam';
- }
- }
- } catch (Typecho_Http_Client_Exception $e) {
- //do nothing
- error_log($e->getMessage());
- }
-
- return $comment;
- }
- }