web/lib/Zend/Service/Akismet.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service
       
    17  * @subpackage Akismet
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Akismet.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * @see Zend_Version
       
    26  */
       
    27 require_once 'Zend/Version.php';
       
    28 
       
    29 /**
       
    30  * @see Zend_Service_Abstract
       
    31  */
       
    32 require_once 'Zend/Service/Abstract.php';
       
    33 
       
    34 
       
    35 /**
       
    36  * Akismet REST service implementation
       
    37  *
       
    38  * @uses       Zend_Service_Abstract
       
    39  * @category   Zend
       
    40  * @package    Zend_Service
       
    41  * @subpackage Akismet
       
    42  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    43  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    44  */
       
    45 class Zend_Service_Akismet extends Zend_Service_Abstract
       
    46 {
       
    47     /**
       
    48      * Akismet API key
       
    49      * @var string
       
    50      */
       
    51     protected $_apiKey;
       
    52 
       
    53     /**
       
    54      * Blog URL
       
    55      * @var string
       
    56      */
       
    57     protected $_blogUrl;
       
    58 
       
    59     /**
       
    60      * Charset used for encoding
       
    61      * @var string
       
    62      */
       
    63     protected $_charset = 'UTF-8';
       
    64 
       
    65     /**
       
    66      * TCP/IP port to use in requests
       
    67      * @var int
       
    68      */
       
    69     protected $_port = 80;
       
    70 
       
    71     /**
       
    72      * User Agent string to send in requests
       
    73      * @var string
       
    74      */
       
    75     protected $_userAgent;
       
    76 
       
    77     /**
       
    78      * Constructor
       
    79      *
       
    80      * @param string $apiKey Akismet API key
       
    81      * @param string $blog Blog URL
       
    82      * @return void
       
    83      */
       
    84     public function __construct($apiKey, $blog)
       
    85     {
       
    86         $this->setBlogUrl($blog)
       
    87              ->setApiKey($apiKey)
       
    88              ->setUserAgent('Zend Framework/' . Zend_Version::VERSION . ' | Akismet/1.11');
       
    89     }
       
    90 
       
    91     /**
       
    92      * Retrieve blog URL
       
    93      *
       
    94      * @return string
       
    95      */
       
    96     public function getBlogUrl()
       
    97     {
       
    98         return $this->_blogUrl;
       
    99     }
       
   100 
       
   101     /**
       
   102      * Set blog URL
       
   103      *
       
   104      * @param string $blogUrl
       
   105      * @return Zend_Service_Akismet
       
   106      * @throws Zend_Service_Exception if invalid URL provided
       
   107      */
       
   108     public function setBlogUrl($blogUrl)
       
   109     {
       
   110         require_once 'Zend/Uri.php';
       
   111         if (!Zend_Uri::check($blogUrl)) {
       
   112             require_once 'Zend/Service/Exception.php';
       
   113             throw new Zend_Service_Exception('Invalid url provided for blog');
       
   114         }
       
   115 
       
   116         $this->_blogUrl = $blogUrl;
       
   117         return $this;
       
   118     }
       
   119 
       
   120     /**
       
   121      * Retrieve API key
       
   122      *
       
   123      * @return string
       
   124      */
       
   125     public function getApiKey()
       
   126     {
       
   127         return $this->_apiKey;
       
   128     }
       
   129 
       
   130     /**
       
   131      * Set API key
       
   132      *
       
   133      * @param string $apiKey
       
   134      * @return Zend_Service_Akismet
       
   135      */
       
   136     public function setApiKey($apiKey)
       
   137     {
       
   138         $this->_apiKey = $apiKey;
       
   139         return $this;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Retrieve charset
       
   144      *
       
   145      * @return string
       
   146      */
       
   147     public function getCharset()
       
   148     {
       
   149         return $this->_charset;
       
   150     }
       
   151 
       
   152     /**
       
   153      * Set charset
       
   154      *
       
   155      * @param string $charset
       
   156      * @return Zend_Service_Akismet
       
   157      */
       
   158     public function setCharset($charset)
       
   159     {
       
   160         $this->_charset = $charset;
       
   161         return $this;
       
   162     }
       
   163 
       
   164     /**
       
   165      * Retrieve TCP/IP port
       
   166      *
       
   167      * @return int
       
   168      */
       
   169     public function getPort()
       
   170     {
       
   171         return $this->_port;
       
   172     }
       
   173 
       
   174     /**
       
   175      * Set TCP/IP port
       
   176      *
       
   177      * @param int $port
       
   178      * @return Zend_Service_Akismet
       
   179      * @throws Zend_Service_Exception if non-integer value provided
       
   180      */
       
   181     public function setPort($port)
       
   182     {
       
   183         if (!is_int($port)) {
       
   184             require_once 'Zend/Service/Exception.php';
       
   185             throw new Zend_Service_Exception('Invalid port');
       
   186         }
       
   187 
       
   188         $this->_port = $port;
       
   189         return $this;
       
   190     }
       
   191 
       
   192     /**
       
   193      * Retrieve User Agent string
       
   194      *
       
   195      * @return string
       
   196      */
       
   197     public function getUserAgent()
       
   198     {
       
   199         return $this->_userAgent;
       
   200     }
       
   201 
       
   202     /**
       
   203      * Set User Agent
       
   204      *
       
   205      * Should be of form "Some user agent/version | Akismet/version"
       
   206      *
       
   207      * @param string $userAgent
       
   208      * @return Zend_Service_Akismet
       
   209      * @throws Zend_Service_Exception with invalid user agent string
       
   210      */
       
   211     public function setUserAgent($userAgent)
       
   212     {
       
   213         if (!is_string($userAgent)
       
   214             || !preg_match(":^[^\n/]*/[^ ]* \| Akismet/[0-9\.]*$:i", $userAgent))
       
   215         {
       
   216             require_once 'Zend/Service/Exception.php';
       
   217             throw new Zend_Service_Exception('Invalid User Agent string; must be of format "Application name/version | Akismet/version"');
       
   218         }
       
   219 
       
   220         $this->_userAgent = $userAgent;
       
   221         return $this;
       
   222     }
       
   223 
       
   224     /**
       
   225      * Post a request
       
   226      *
       
   227      * @param string $host
       
   228      * @param string $path
       
   229      * @param array  $params
       
   230      * @return mixed
       
   231      */
       
   232     protected function _post($host, $path, array $params)
       
   233     {
       
   234         $uri    = 'http://' . $host . ':' . $this->getPort() . $path;
       
   235         $client = self::getHttpClient();
       
   236         $client->setUri($uri);
       
   237         $client->setConfig(array(
       
   238             'useragent'    => $this->getUserAgent(),
       
   239         ));
       
   240 
       
   241         $client->setHeaders(array(
       
   242             'Host'         => $host,
       
   243             'Content-Type' => 'application/x-www-form-urlencoded; charset=' . $this->getCharset()
       
   244         ));
       
   245         $client->setParameterPost($params);
       
   246 
       
   247         $client->setMethod(Zend_Http_Client::POST);
       
   248         return $client->request();
       
   249     }
       
   250 
       
   251     /**
       
   252      * Verify an API key
       
   253      *
       
   254      * @param string $key Optional; API key to verify
       
   255      * @param string $blog Optional; blog URL against which to verify key
       
   256      * @return boolean
       
   257      */
       
   258     public function verifyKey($key = null, $blog = null)
       
   259     {
       
   260         if (null === $key) {
       
   261             $key = $this->getApiKey();
       
   262         }
       
   263 
       
   264         if (null === $blog) {
       
   265             $blog = $this->getBlogUrl();
       
   266         }
       
   267 
       
   268         $response = $this->_post('rest.akismet.com', '/1.1/verify-key', array(
       
   269             'key'  => $key,
       
   270             'blog' => $blog
       
   271         ));
       
   272 
       
   273         return ('valid' == $response->getBody());
       
   274     }
       
   275 
       
   276     /**
       
   277      * Perform an API call
       
   278      *
       
   279      * @param string $path
       
   280      * @param array $params
       
   281      * @return Zend_Http_Response
       
   282      * @throws Zend_Service_Exception if missing user_ip or user_agent fields
       
   283      */
       
   284     protected function _makeApiCall($path, $params)
       
   285     {
       
   286         if (empty($params['user_ip']) || empty($params['user_agent'])) {
       
   287             require_once 'Zend/Service/Exception.php';
       
   288             throw new Zend_Service_Exception('Missing required Akismet fields (user_ip and user_agent are required)');
       
   289         }
       
   290 
       
   291         if (!isset($params['blog'])) {
       
   292             $params['blog'] = $this->getBlogUrl();
       
   293         }
       
   294 
       
   295         return $this->_post($this->getApiKey() . '.rest.akismet.com', $path, $params);
       
   296     }
       
   297 
       
   298     /**
       
   299      * Check a comment for spam
       
   300      *
       
   301      * Checks a comment to see if it is spam. $params should be an associative
       
   302      * array with one or more of the following keys (unless noted, all keys are
       
   303      * optional):
       
   304      * - blog: URL of the blog. If not provided, uses value returned by {@link getBlogUrl()}
       
   305      * - user_ip (required): IP address of comment submitter
       
   306      * - user_agent (required): User Agent used by comment submitter
       
   307      * - referrer: contents of HTTP_REFERER header
       
   308      * - permalink: location of the entry to which the comment was submitted
       
   309      * - comment_type: typically, one of 'blank', 'comment', 'trackback', or 'pingback', but may be any value
       
   310      * - comment_author: name submitted with the content
       
   311      * - comment_author_email: email submitted with the content
       
   312      * - comment_author_url: URL submitted with the content
       
   313      * - comment_content: actual content
       
   314      *
       
   315      * Additionally, Akismet suggests returning the key/value pairs in the
       
   316      * $_SERVER array, and these may be included in the $params.
       
   317      *
       
   318      * This method implements the Akismet comment-check REST method.
       
   319      *
       
   320      * @param array $params
       
   321      * @return boolean
       
   322      * @throws Zend_Service_Exception with invalid API key
       
   323      */
       
   324     public function isSpam($params)
       
   325     {
       
   326         $response = $this->_makeApiCall('/1.1/comment-check', $params);
       
   327 
       
   328         $return = trim($response->getBody());
       
   329 
       
   330         if ('invalid' == $return) {
       
   331             require_once 'Zend/Service/Exception.php';
       
   332             throw new Zend_Service_Exception('Invalid API key');
       
   333         }
       
   334 
       
   335         if ('true' == $return) {
       
   336             return true;
       
   337         }
       
   338 
       
   339         return false;
       
   340     }
       
   341 
       
   342     /**
       
   343      * Submit spam
       
   344      *
       
   345      * Takes the same arguments as {@link isSpam()}.
       
   346      *
       
   347      * Submits known spam content to Akismet to help train it.
       
   348      *
       
   349      * This method implements Akismet's submit-spam REST method.
       
   350      *
       
   351      * @param array $params
       
   352      * @return void
       
   353      * @throws Zend_Service_Exception with invalid API key
       
   354      */
       
   355     public function submitSpam($params)
       
   356     {
       
   357         $response = $this->_makeApiCall('/1.1/submit-spam', $params);
       
   358         $value    = trim($response->getBody());
       
   359         if ('invalid' == $value) {
       
   360             require_once 'Zend/Service/Exception.php';
       
   361             throw new Zend_Service_Exception('Invalid API key');
       
   362         }
       
   363     }
       
   364 
       
   365     /**
       
   366      * Submit ham
       
   367      *
       
   368      * Takes the same arguments as {@link isSpam()}.
       
   369      *
       
   370      * Submits a comment that has been falsely categorized as spam by Akismet
       
   371      * as a false positive, telling Akismet's filters not to filter such
       
   372      * comments as spam in the future.
       
   373      *
       
   374      * Unlike {@link submitSpam()} and {@link isSpam()}, a valid API key is
       
   375      * never necessary; as a result, this method never throws an exception
       
   376      * (unless an exception happens with the HTTP client layer).
       
   377      *
       
   378      * this method implements Akismet's submit-ham REST method.
       
   379      *
       
   380      * @param array $params
       
   381      * @return void
       
   382      */
       
   383     public function submitHam($params)
       
   384     {
       
   385         $response = $this->_makeApiCall('/1.1/submit-ham', $params);
       
   386     }
       
   387 }