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

/plugins/Comment/src/Lib/Akismet.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 414 lines | 180 code | 43 blank | 191 comment | 16 complexity | dd07eb5d476f7881e9214cadb8ed0789 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Akismet anti-comment spam service
  4. *
  5. * The class in this package allows use of the {@link http://akismet.com Akismet} anti-comment spam service in any PHP5 application.
  6. *
  7. * This service performs a number of checks on submitted data and returns whether or not the data is likely to be spam.
  8. *
  9. * Please note that in order to use this class, you must have a vaild {@link http://wordpress.com/api-keys/ WordPress API key}. They are free for non/small-profit types and getting one will only take a couple of minutes.
  10. *
  11. * For commercial use, please {@link http://akismet.com/commercial/ visit the Akismet commercial licensing page}.
  12. *
  13. * Please be aware that this class is PHP5 only. Attempts to run it under PHP4 will most likely fail.
  14. *
  15. * See the Akismet class documentation page linked to below for usage information.
  16. *
  17. * @package akismet
  18. * @author Alex Potsides, {@link http://www.achingbrain.net http://www.achingbrain.net}
  19. * @version 0.4
  20. * @copyright Alex Potsides, {@link http://www.achingbrain.net http://www.achingbrain.net}
  21. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  22. */
  23. /**
  24. * The Akismet PHP5 Class
  25. *
  26. * This class takes the functionality from the Akismet WordPress plugin written by {@link http://photomatt.net/ Matt Mullenweg} and allows it to be integrated into any PHP5 application or website.
  27. *
  28. * The original plugin is {@link http://akismet.com/download/ available on the Akismet website}.
  29. *
  30. * <b>Usage:</b>
  31. * <code>
  32. * $akismet = new Akismet('http://www.example.com/blog/', 'aoeu1aoue');
  33. * $akismet->setCommentAuthor($name);
  34. * $akismet->setCommentAuthorEmail($email);
  35. * $akismet->setCommentAuthorURL($url);
  36. * $akismet->setCommentContent($comment);
  37. * $akismet->setPermalink('http://www.example.com/blog/alex/someurl/');
  38. * if($akismet->isCommentSpam())
  39. * // store the comment but mark it as spam (in case of a mis-diagnosis)
  40. * else
  41. * // store the comment normally
  42. * </code>
  43. *
  44. * Optionally you may wish to check if your WordPress API key is valid as in the example below.
  45. *
  46. * <code>
  47. * $akismet = new Akismet('http://www.example.com/blog/', 'aoeu1aoue');
  48. *
  49. * if($akismet->isKeyValid()) {
  50. * // api key is okay
  51. * } else {
  52. * // api key is invalid
  53. * }
  54. * </code>
  55. *
  56. * @package QuickApps.Plugin.Comment.Lib.Akismet
  57. * @name Akismet
  58. * @version 0.4
  59. * @author Alex Potsides
  60. * @link http://www.achingbrain.net/
  61. */
  62. class Akismet
  63. {
  64. private $version = '0.4';
  65. private $wordPressAPIKey;
  66. private $blogURL;
  67. private $comment;
  68. private $apiPort;
  69. private $akismetServer;
  70. private $akismetVersion;
  71. // This prevents some potentially sensitive information from being sent accross the wire.
  72. private $ignore = array('HTTP_COOKIE',
  73. 'HTTP_X_FORWARDED_FOR',
  74. 'HTTP_X_FORWARDED_HOST',
  75. 'HTTP_MAX_FORWARDS',
  76. 'HTTP_X_FORWARDED_SERVER',
  77. 'REDIRECT_STATUS',
  78. 'SERVER_PORT',
  79. 'PATH',
  80. 'DOCUMENT_ROOT',
  81. 'SERVER_ADMIN',
  82. 'QUERY_STRING',
  83. 'PHP_SELF' );
  84. /**
  85. * @param string $blogURL The URL of your blog.
  86. * @param string $wordPressAPIKey WordPress API key.
  87. */
  88. public function __construct($blogURL, $wordPressAPIKey)
  89. {
  90. $this->blogURL = $blogURL;
  91. $this->wordPressAPIKey = $wordPressAPIKey;
  92. // Set some default values
  93. $this->apiPort = 80;
  94. $this->akismetServer = 'rest.akismet.com';
  95. $this->akismetVersion = '1.1';
  96. // Start to populate the comment data
  97. $this->comment['blog'] = $blogURL;
  98. $this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  99. if (isset($_SERVER['HTTP_REFERER'])) {
  100. $this->comment['referrer'] = $_SERVER['HTTP_REFERER'];
  101. }
  102. /*
  103. * This is necessary if the server PHP5 is running on has been set up to run PHP4 and
  104. * PHP5 concurently and is actually running through a separate proxy al a these instructions:
  105. * http://www.schlitt.info/applications/blog/archives/83_How_to_run_PHP4_and_PHP_5_parallel.html
  106. * and http://wiki.coggeshall.org/37.html
  107. * Otherwise the user_ip appears as the IP address of the PHP4 server passing the requests to the
  108. * PHP5 one...
  109. */
  110. $this->comment['user_ip'] = $_SERVER['REMOTE_ADDR'] != getenv('SERVER_ADDR') ? $_SERVER['REMOTE_ADDR'] : getenv('HTTP_X_FORWARDED_FOR');
  111. }
  112. /**
  113. * Makes a request to the Akismet service to see if the API key passed to the constructor is valid.
  114. *
  115. * Use this method if you suspect your API key is invalid.
  116. *
  117. * @return bool True is if the key is valid, false if not.
  118. */
  119. public function isKeyValid()
  120. {
  121. // Check to see if the key is valid
  122. $response = $this->sendRequest('key=' . $this->wordPressAPIKey . '&blog=' . $this->blogURL, $this->akismetServer, '/' . $this->akismetVersion . '/verify-key');
  123. return $response[1] == 'valid';
  124. }
  125. // makes a request to the Akismet service
  126. private function sendRequest($request, $host, $path)
  127. {
  128. $http_request = "POST " . $path . " HTTP/1.0\r\n";
  129. $http_request .= "Host: " . $host . "\r\n";
  130. $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
  131. $http_request .= "Content-Length: " . strlen($request) . "\r\n";
  132. $http_request .= "User-Agent: Akismet PHP5 Class " . $this->version . " | Akismet/1.11\r\n";
  133. $http_request .= "\r\n";
  134. $http_request .= $request;
  135. $socketWriteRead = new SocketWriteRead($host, $this->apiPort, $http_request);
  136. $socketWriteRead->send();
  137. return explode("\r\n\r\n", $socketWriteRead->getResponse(), 2);
  138. }
  139. // Formats the data for transmission
  140. private function getQueryString()
  141. {
  142. foreach ($_SERVER as $key => $value) {
  143. if (!in_array($key, $this->ignore)) {
  144. if ($key == 'REMOTE_ADDR') {
  145. $this->comment[$key] = $this->comment['user_ip'];
  146. } else {
  147. $this->comment[$key] = $value;
  148. }
  149. }
  150. }
  151. $query_string = '';
  152. foreach ($this->comment as $key => $data) {
  153. if (!is_array($data)) {
  154. $query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
  155. }
  156. }
  157. return $query_string;
  158. }
  159. /**
  160. * Tests for spam.
  161. *
  162. * Uses the web service provided by {@link http://www.akismet.com Akismet} to see whether or not the submitted comment is spam. Returns a boolean value.
  163. *
  164. * @return bool True if the comment is spam, false if not
  165. * @throws Will throw an exception if the API key passed to the constructor is invalid.
  166. */
  167. public function isCommentSpam()
  168. {
  169. $response = $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.rest.akismet.com', '/' . $this->akismetVersion . '/comment-check');
  170. if ($response[1] == 'invalid' && !$this->isKeyValid()) {
  171. throw new exception('The Wordpress API key passed to the Akismet constructor is invalid. Please obtain a valid one from http://wordpress.com/api-keys/');
  172. }
  173. return ($response[1] == 'true');
  174. }
  175. /**
  176. * Submit spam that is incorrectly tagged as ham.
  177. *
  178. * Using this function will make you a good citizen as it helps Akismet to learn from its mistakes. This will improve the service for everybody.
  179. */
  180. public function submitSpam()
  181. {
  182. $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-spam');
  183. }
  184. /**
  185. * Submit ham that is incorrectly tagged as spam.
  186. *
  187. * Using this function will make you a good citizen as it helps Akismet to learn from its mistakes. This will improve the service for everybody.
  188. */
  189. public function submitHam()
  190. {
  191. $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-ham');
  192. }
  193. /**
  194. * To override the user IP address when submitting spam/ham later on
  195. *
  196. * @param string $userip An IP address. Optional.
  197. */
  198. public function setUserIP($userip)
  199. {
  200. $this->comment['user_ip'] = $userip;
  201. }
  202. /**
  203. * To override the referring page when submitting spam/ham later on
  204. *
  205. * @param string $referrer The referring page. Optional.
  206. */
  207. public function setReferrer($referrer)
  208. {
  209. $this->comment['referrer'] = $referrer;
  210. }
  211. /**
  212. * A permanent URL referencing the blog post the comment was submitted to.
  213. *
  214. * @param string $permalink The URL. Optional.
  215. */
  216. public function setPermalink($permalink)
  217. {
  218. $this->comment['permalink'] = $permalink;
  219. }
  220. /**
  221. * The type of comment being submitted.
  222. *
  223. * May be blank, comment, trackback, pingback, or a made up value like "registration" or "wiki".
  224. */
  225. public function setCommentType($commentType)
  226. {
  227. $this->comment['comment_type'] = $commentType;
  228. }
  229. /**
  230. * The name that the author submitted with the comment.
  231. */
  232. public function setCommentAuthor($commentAuthor)
  233. {
  234. $this->comment['comment_author'] = $commentAuthor;
  235. }
  236. /**
  237. * The email address that the author submitted with the comment.
  238. *
  239. * The address is assumed to be valid.
  240. */
  241. public function setCommentAuthorEmail($authorEmail)
  242. {
  243. $this->comment['comment_author_email'] = $authorEmail;
  244. }
  245. /**
  246. * The URL that the author submitted with the comment.
  247. */
  248. public function setCommentAuthorURL($authorURL)
  249. {
  250. $this->comment['comment_author_url'] = $authorURL;
  251. }
  252. /**
  253. * The comment's body text.
  254. */
  255. public function setCommentContent($commentBody)
  256. {
  257. $this->comment['comment_content'] = $commentBody;
  258. }
  259. /**
  260. * Defaults to 80
  261. */
  262. public function setAPIPort($apiPort)
  263. {
  264. $this->apiPort = $apiPort;
  265. }
  266. /**
  267. * Defaults to rest.akismet.com
  268. */
  269. public function setAkismetServer($akismetServer)
  270. {
  271. $this->akismetServer = $akismetServer;
  272. }
  273. /**
  274. * Defaults to '1.1'
  275. */
  276. public function setAkismetVersion($akismetVersion)
  277. {
  278. $this->akismetVersion = $akismetVersion;
  279. }
  280. }
  281. /**
  282. * Utility class used by Akismet
  283. *
  284. * This class is used by Akismet to do the actual sending and receiving of data. It opens a connection to a remote host, sends some data and the reads the response and makes it available to the calling program.
  285. *
  286. * The code that makes up this class originates in the Akismet WordPress plugin, which is {@link http://akismet.com/download/ available on the Akismet website}.
  287. *
  288. * N.B. It is not necessary to call this class directly to use the Akismet class. This is included here mainly out of a sense of completeness.
  289. *
  290. * @package QuickApps.Plugin.Comment.Lib.Akismet
  291. * @name SocketWriteRead
  292. * @version 0.1
  293. * @author Alex Potsides
  294. * @link http://www.achingbrain.net/
  295. */
  296. class SocketWriteRead
  297. {
  298. private $host;
  299. private $port;
  300. private $request;
  301. private $response;
  302. private $responseLength;
  303. private $errorNumber;
  304. private $errorString;
  305. /**
  306. * @param string $host The host to send/receive data.
  307. * @param int $port The port on the remote host.
  308. * @param string $request The data to send.
  309. * @param int $responseLength The amount of data to read. Defaults to 1160 bytes.
  310. */
  311. public function __construct($host, $port, $request, $responseLength = 1160)
  312. {
  313. $this->host = $host;
  314. $this->port = $port;
  315. $this->request = $request;
  316. $this->responseLength = $responseLength;
  317. $this->errorNumber = 0;
  318. $this->errorString = '';
  319. }
  320. /**
  321. * Sends the data to the remote host.
  322. *
  323. * @throws An exception is thrown if a connection cannot be made to the remote host.
  324. */
  325. public function send()
  326. {
  327. $this->response = '';
  328. $fs = fsockopen($this->host, $this->port, $this->errorNumber, $this->errorString, 3);
  329. if ($this->errorNumber != 0) {
  330. throw new Exception('Error connecting to host: ' . $this->host . ' Error number: ' . $this->errorNumber . ' Error message: ' . $this->errorString);
  331. }
  332. if ($fs !== false) {
  333. @fwrite($fs, $this->request);
  334. while (!feof($fs)) {
  335. $this->response .= fgets($fs, $this->responseLength);
  336. }
  337. fclose($fs);
  338. }
  339. }
  340. /**
  341. * Returns the server response text
  342. *
  343. * @return string
  344. */
  345. public function getResponse()
  346. {
  347. return $this->response;
  348. }
  349. /**
  350. * Returns the error number
  351. *
  352. * If there was no error, 0 will be returned.
  353. *
  354. * @return int
  355. */
  356. public function getErrorNumner()
  357. {
  358. return $this->errorNumber;
  359. }
  360. /**
  361. * Returns the error string
  362. *
  363. * If there was no error, an empty string will be returned.
  364. *
  365. * @return string
  366. */
  367. public function getErrorString()
  368. {
  369. return $this->errorString;
  370. }
  371. }