File indexing completed on 2024-12-22 05:37:06
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service 0017 * @subpackage Akismet 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 0024 /** 0025 * @see Zend_Version 0026 */ 0027 // require_once 'Zend/Version.php'; 0028 0029 /** 0030 * @see Zend_Service_Abstract 0031 */ 0032 // require_once 'Zend/Service/Abstract.php'; 0033 0034 0035 /** 0036 * Akismet REST service implementation 0037 * 0038 * @uses Zend_Service_Abstract 0039 * @category Zend 0040 * @package Zend_Service 0041 * @subpackage Akismet 0042 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0043 * @license http://framework.zend.com/license/new-bsd New BSD License 0044 */ 0045 class Zend_Service_Akismet extends Zend_Service_Abstract 0046 { 0047 /** 0048 * Akismet API key 0049 * @var string 0050 */ 0051 protected $_apiKey; 0052 0053 /** 0054 * Blog URL 0055 * @var string 0056 */ 0057 protected $_blogUrl; 0058 0059 /** 0060 * Charset used for encoding 0061 * @var string 0062 */ 0063 protected $_charset = 'UTF-8'; 0064 0065 /** 0066 * TCP/IP port to use in requests 0067 * @var int 0068 */ 0069 protected $_port = 80; 0070 0071 /** 0072 * User Agent string to send in requests 0073 * @var string 0074 */ 0075 protected $_userAgent; 0076 0077 /** 0078 * Constructor 0079 * 0080 * @param string $apiKey Akismet API key 0081 * @param string $blog Blog URL 0082 * @return void 0083 */ 0084 public function __construct($apiKey, $blog) 0085 { 0086 $this->setBlogUrl($blog) 0087 ->setApiKey($apiKey) 0088 ->setUserAgent('Zend Framework/' . Zend_Version::VERSION . ' | Akismet/1.11'); 0089 } 0090 0091 /** 0092 * Retrieve blog URL 0093 * 0094 * @return string 0095 */ 0096 public function getBlogUrl() 0097 { 0098 return $this->_blogUrl; 0099 } 0100 0101 /** 0102 * Set blog URL 0103 * 0104 * @param string $blogUrl 0105 * @return Zend_Service_Akismet 0106 * @throws Zend_Service_Exception if invalid URL provided 0107 */ 0108 public function setBlogUrl($blogUrl) 0109 { 0110 // require_once 'Zend/Uri.php'; 0111 if (!Zend_Uri::check($blogUrl)) { 0112 // require_once 'Zend/Service/Exception.php'; 0113 throw new Zend_Service_Exception('Invalid url provided for blog'); 0114 } 0115 0116 $this->_blogUrl = $blogUrl; 0117 return $this; 0118 } 0119 0120 /** 0121 * Retrieve API key 0122 * 0123 * @return string 0124 */ 0125 public function getApiKey() 0126 { 0127 return $this->_apiKey; 0128 } 0129 0130 /** 0131 * Set API key 0132 * 0133 * @param string $apiKey 0134 * @return Zend_Service_Akismet 0135 */ 0136 public function setApiKey($apiKey) 0137 { 0138 $this->_apiKey = $apiKey; 0139 return $this; 0140 } 0141 0142 /** 0143 * Retrieve charset 0144 * 0145 * @return string 0146 */ 0147 public function getCharset() 0148 { 0149 return $this->_charset; 0150 } 0151 0152 /** 0153 * Set charset 0154 * 0155 * @param string $charset 0156 * @return Zend_Service_Akismet 0157 */ 0158 public function setCharset($charset) 0159 { 0160 $this->_charset = $charset; 0161 return $this; 0162 } 0163 0164 /** 0165 * Retrieve TCP/IP port 0166 * 0167 * @return int 0168 */ 0169 public function getPort() 0170 { 0171 return $this->_port; 0172 } 0173 0174 /** 0175 * Set TCP/IP port 0176 * 0177 * @param int $port 0178 * @return Zend_Service_Akismet 0179 * @throws Zend_Service_Exception if non-integer value provided 0180 */ 0181 public function setPort($port) 0182 { 0183 if (!is_int($port)) { 0184 // require_once 'Zend/Service/Exception.php'; 0185 throw new Zend_Service_Exception('Invalid port'); 0186 } 0187 0188 $this->_port = $port; 0189 return $this; 0190 } 0191 0192 /** 0193 * Retrieve User Agent string 0194 * 0195 * @return string 0196 */ 0197 public function getUserAgent() 0198 { 0199 return $this->_userAgent; 0200 } 0201 0202 /** 0203 * Set User Agent 0204 * 0205 * Should be of form "Some user agent/version | Akismet/version" 0206 * 0207 * @param string $userAgent 0208 * @return Zend_Service_Akismet 0209 * @throws Zend_Service_Exception with invalid user agent string 0210 */ 0211 public function setUserAgent($userAgent) 0212 { 0213 if (!is_string($userAgent) 0214 || !preg_match(":^[^\n/]*/[^ ]* \| Akismet/[0-9\.]*$:i", $userAgent)) 0215 { 0216 // require_once 'Zend/Service/Exception.php'; 0217 throw new Zend_Service_Exception('Invalid User Agent string; must be of format "Application name/version | Akismet/version"'); 0218 } 0219 0220 $this->_userAgent = $userAgent; 0221 return $this; 0222 } 0223 0224 /** 0225 * Post a request 0226 * 0227 * @param string $host 0228 * @param string $path 0229 * @param array $params 0230 * @return mixed 0231 */ 0232 protected function _post($host, $path, array $params) 0233 { 0234 $uri = 'http://' . $host . ':' . $this->getPort() . $path; 0235 $client = self::getHttpClient(); 0236 $client->setUri($uri); 0237 $client->setConfig(array( 0238 'useragent' => $this->getUserAgent(), 0239 )); 0240 0241 $client->setHeaders(array( 0242 'Host' => $host, 0243 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . $this->getCharset() 0244 )); 0245 $client->setParameterPost($params); 0246 0247 $client->setMethod(Zend_Http_Client::POST); 0248 return $client->request(); 0249 } 0250 0251 /** 0252 * Verify an API key 0253 * 0254 * @param string $key Optional; API key to verify 0255 * @param string $blog Optional; blog URL against which to verify key 0256 * @return boolean 0257 */ 0258 public function verifyKey($key = null, $blog = null) 0259 { 0260 if (null === $key) { 0261 $key = $this->getApiKey(); 0262 } 0263 0264 if (null === $blog) { 0265 $blog = $this->getBlogUrl(); 0266 } 0267 0268 $response = $this->_post('rest.akismet.com', '/1.1/verify-key', array( 0269 'key' => $key, 0270 'blog' => $blog 0271 )); 0272 0273 return ('valid' == $response->getBody()); 0274 } 0275 0276 /** 0277 * Perform an API call 0278 * 0279 * @param string $path 0280 * @param array $params 0281 * @return Zend_Http_Response 0282 * @throws Zend_Service_Exception if missing user_ip or user_agent fields 0283 */ 0284 protected function _makeApiCall($path, $params) 0285 { 0286 if (empty($params['user_ip']) || empty($params['user_agent'])) { 0287 // require_once 'Zend/Service/Exception.php'; 0288 throw new Zend_Service_Exception('Missing required Akismet fields (user_ip and user_agent are required)'); 0289 } 0290 0291 if (!isset($params['blog'])) { 0292 $params['blog'] = $this->getBlogUrl(); 0293 } 0294 0295 return $this->_post($this->getApiKey() . '.rest.akismet.com', $path, $params); 0296 } 0297 0298 /** 0299 * Check a comment for spam 0300 * 0301 * Checks a comment to see if it is spam. $params should be an associative 0302 * array with one or more of the following keys (unless noted, all keys are 0303 * optional): 0304 * - blog: URL of the blog. If not provided, uses value returned by {@link getBlogUrl()} 0305 * - user_ip (required): IP address of comment submitter 0306 * - user_agent (required): User Agent used by comment submitter 0307 * - referrer: contents of HTTP_REFERER header 0308 * - permalink: location of the entry to which the comment was submitted 0309 * - comment_type: typically, one of 'blank', 'comment', 'trackback', or 'pingback', but may be any value 0310 * - comment_author: name submitted with the content 0311 * - comment_author_email: email submitted with the content 0312 * - comment_author_url: URL submitted with the content 0313 * - comment_content: actual content 0314 * 0315 * Additionally, Akismet suggests returning the key/value pairs in the 0316 * $_SERVER array, and these may be included in the $params. 0317 * 0318 * This method implements the Akismet comment-check REST method. 0319 * 0320 * @param array $params 0321 * @return boolean 0322 * @throws Zend_Service_Exception with invalid API key 0323 */ 0324 public function isSpam($params) 0325 { 0326 $response = $this->_makeApiCall('/1.1/comment-check', $params); 0327 0328 $return = trim($response->getBody()); 0329 0330 if ('invalid' == $return) { 0331 // require_once 'Zend/Service/Exception.php'; 0332 throw new Zend_Service_Exception('Invalid API key'); 0333 } 0334 0335 if ('true' == $return) { 0336 return true; 0337 } 0338 0339 return false; 0340 } 0341 0342 /** 0343 * Submit spam 0344 * 0345 * Takes the same arguments as {@link isSpam()}. 0346 * 0347 * Submits known spam content to Akismet to help train it. 0348 * 0349 * This method implements Akismet's submit-spam REST method. 0350 * 0351 * @param array $params 0352 * @return void 0353 * @throws Zend_Service_Exception with invalid API key 0354 */ 0355 public function submitSpam($params) 0356 { 0357 $response = $this->_makeApiCall('/1.1/submit-spam', $params); 0358 $value = trim($response->getBody()); 0359 if ('invalid' == $value) { 0360 // require_once 'Zend/Service/Exception.php'; 0361 throw new Zend_Service_Exception('Invalid API key'); 0362 } 0363 } 0364 0365 /** 0366 * Submit ham 0367 * 0368 * Takes the same arguments as {@link isSpam()}. 0369 * 0370 * Submits a comment that has been falsely categorized as spam by Akismet 0371 * as a false positive, telling Akismet's filters not to filter such 0372 * comments as spam in the future. 0373 * 0374 * Unlike {@link submitSpam()} and {@link isSpam()}, a valid API key is 0375 * never necessary; as a result, this method never throws an exception 0376 * (unless an exception happens with the HTTP client layer). 0377 * 0378 * this method implements Akismet's submit-ham REST method. 0379 * 0380 * @param array $params 0381 * @return void 0382 */ 0383 public function submitHam($params) 0384 { 0385 $response = $this->_makeApiCall('/1.1/submit-ham', $params); 0386 } 0387 }