PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Service/Akismet.php

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