PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Service/Akismet.php

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