PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/akismet/Akismet.class.php

https://github.com/luthercollege/reason_package
PHP | 476 lines | 174 code | 54 blank | 248 comment | 19 complexity | ebb50adc20b69735a469cb81e252471d MD5 | raw file
  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.5
  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. * <code>
  31. * $akismet = new Akismet('http://www.example.com/blog/', 'aoeu1aoue');
  32. * $akismet->setCommentAuthor($name);
  33. * $akismet->setCommentAuthorEmail($email);
  34. * $akismet->setCommentAuthorURL($url);
  35. * $akismet->setCommentContent($comment);
  36. * $akismet->setPermalink('http://www.example.com/blog/alex/someurl/');
  37. *
  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 akismet
  57. * @name Akismet
  58. * @version 0.5
  59. * @author Alex Potsides
  60. * @link http://www.achingbrain.net/
  61. */
  62. class Akismet {
  63. private $version = '0.5';
  64. private $wordPressAPIKey;
  65. private $blogURL;
  66. private $comment;
  67. private $apiPort;
  68. private $akismetServer;
  69. private $akismetVersion;
  70. private $requestFactory;
  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. $this->blogURL = $blogURL;
  90. $this->wordPressAPIKey = $wordPressAPIKey;
  91. // Set some default values
  92. $this->apiPort = 80;
  93. $this->akismetServer = 'rest.akismet.com';
  94. $this->akismetVersion = '1.1';
  95. $this->requestFactory = new SocketWriteReadFactory();
  96. // Start to populate the comment data
  97. $this->comment['blog'] = $blogURL;
  98. if(isset($_SERVER['HTTP_USER_AGENT'])) {
  99. $this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  100. }
  101. if(isset($_SERVER['HTTP_REFERER'])) {
  102. $this->comment['referrer'] = $_SERVER['HTTP_REFERER'];
  103. }
  104. /*
  105. * This is necessary if the server PHP5 is running on has been set up to run PHP4 and
  106. * PHP5 concurently and is actually running through a separate proxy al a these instructions:
  107. * http://www.schlitt.info/applications/blog/archives/83_How_to_run_PHP4_and_PHP_5_parallel.html
  108. * and http://wiki.coggeshall.org/37.html
  109. * Otherwise the user_ip appears as the IP address of the PHP4 server passing the requests to the
  110. * PHP5 one...
  111. */
  112. if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != getenv('SERVER_ADDR')) {
  113. $this->comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
  114. } else {
  115. $this->comment['user_ip'] = getenv('HTTP_X_FORWARDED_FOR');
  116. }
  117. }
  118. /**
  119. * Makes a request to the Akismet service to see if the API key passed to the constructor is valid.
  120. *
  121. * Use this method if you suspect your API key is invalid.
  122. *
  123. * @return bool True is if the key is valid, false if not.
  124. */
  125. public function isKeyValid() {
  126. // Check to see if the key is valid
  127. $response = $this->sendRequest('key=' . $this->wordPressAPIKey . '&blog=' . $this->blogURL, $this->akismetServer, '/' . $this->akismetVersion . '/verify-key');
  128. return $response[1] == 'valid';
  129. }
  130. // makes a request to the Akismet service
  131. private function sendRequest($request, $host, $path) {
  132. $http_request = "POST " . $path . " HTTP/1.0\r\n";
  133. $http_request .= "Host: " . $host . "\r\n";
  134. $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
  135. $http_request .= "Content-Length: " . strlen($request) . "\r\n";
  136. $http_request .= "User-Agent: Akismet PHP5 Class " . $this->version . " | Akismet/1.11\r\n";
  137. $http_request .= "\r\n";
  138. $http_request .= $request;
  139. $requestSender = $this->requestFactory->createRequestSender();
  140. $response = $requestSender->send($host, $this->apiPort, $http_request);
  141. return explode("\r\n\r\n", $response, 2);
  142. }
  143. // Formats the data for transmission
  144. private function getQueryString() {
  145. foreach($_SERVER as $key => $value) {
  146. if(!in_array($key, $this->ignore)) {
  147. if($key == 'REMOTE_ADDR') {
  148. $this->comment[$key] = $this->comment['user_ip'];
  149. } else {
  150. $this->comment[$key] = $value;
  151. }
  152. }
  153. }
  154. $query_string = '';
  155. foreach($this->comment as $key => $data) {
  156. if(!is_array($data)) {
  157. $query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
  158. }
  159. }
  160. return $query_string;
  161. }
  162. /**
  163. * Tests for spam.
  164. *
  165. * 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.
  166. *
  167. * @return bool True if the comment is spam, false if not
  168. * @throws Will throw an exception if the API key passed to the constructor is invalid.
  169. */
  170. public function isCommentSpam() {
  171. $response = $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.rest.akismet.com', '/' . $this->akismetVersion . '/comment-check');
  172. if($response[1] == 'invalid' && !$this->isKeyValid()) {
  173. 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/');
  174. }
  175. return ($response[1] == 'true');
  176. }
  177. /**
  178. * Submit spam that is incorrectly tagged as ham.
  179. *
  180. * 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.
  181. */
  182. public function submitSpam() {
  183. $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-spam');
  184. }
  185. /**
  186. * Submit ham that is incorrectly tagged as spam.
  187. *
  188. * 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.
  189. */
  190. public function submitHam() {
  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. $this->comment['user_ip'] = $userip;
  200. }
  201. /**
  202. * To override the referring page when submitting spam/ham later on
  203. *
  204. * @param string $referrer The referring page. Optional.
  205. */
  206. public function setReferrer($referrer) {
  207. $this->comment['referrer'] = $referrer;
  208. }
  209. /**
  210. * A permanent URL referencing the blog post the comment was submitted to.
  211. *
  212. * @param string $permalink The URL. Optional.
  213. */
  214. public function setPermalink($permalink) {
  215. $this->comment['permalink'] = $permalink;
  216. }
  217. /**
  218. * The type of comment being submitted.
  219. *
  220. * May be blank, comment, trackback, pingback, or a made up value like "registration" or "wiki".
  221. */
  222. public function setCommentType($commentType) {
  223. $this->comment['comment_type'] = $commentType;
  224. }
  225. /**
  226. * The name that the author submitted with the comment.
  227. */
  228. public function setCommentAuthor($commentAuthor) {
  229. $this->comment['comment_author'] = $commentAuthor;
  230. }
  231. /**
  232. * The email address that the author submitted with the comment.
  233. *
  234. * The address is assumed to be valid.
  235. */
  236. public function setCommentAuthorEmail($authorEmail) {
  237. $this->comment['comment_author_email'] = $authorEmail;
  238. }
  239. /**
  240. * The URL that the author submitted with the comment.
  241. */
  242. public function setCommentAuthorURL($authorURL) {
  243. $this->comment['comment_author_url'] = $authorURL;
  244. }
  245. /**
  246. * The comment's body text.
  247. */
  248. public function setCommentContent($commentBody) {
  249. $this->comment['comment_content'] = $commentBody;
  250. }
  251. /**
  252. * Lets you override the user agent used to submit the comment.
  253. * you may wish to do this when submitting ham/spam.
  254. * Defaults to $_SERVER['HTTP_USER_AGENT']
  255. */
  256. public function setCommentUserAgent($userAgent) {
  257. $this->comment['user_agent'] = $userAgent;
  258. }
  259. /**
  260. * Defaults to 80
  261. */
  262. public function setAPIPort($apiPort) {
  263. $this->apiPort = $apiPort;
  264. }
  265. /**
  266. * Defaults to rest.akismet.com
  267. */
  268. public function setAkismetServer($akismetServer) {
  269. $this->akismetServer = $akismetServer;
  270. }
  271. /**
  272. * Defaults to '1.1'
  273. *
  274. * @param string $akismetVersion
  275. */
  276. public function setAkismetVersion($akismetVersion) {
  277. $this->akismetVersion = $akismetVersion;
  278. }
  279. /**
  280. * Used by unit tests to mock transport layer
  281. *
  282. * @param AkismetRequestFactory $requestFactory
  283. */
  284. public function setRequestFactory($requestFactory) {
  285. $this->requestFactory = $requestFactory;
  286. }
  287. }
  288. /**
  289. * Used internally by Akismet
  290. *
  291. * 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.
  292. *
  293. * 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}.
  294. *
  295. * N.B. It is not necessary to call this class directly to use the Akismet class.
  296. *
  297. * @package akismet
  298. * @name SocketWriteRead
  299. * @version 0.5
  300. * @author Alex Potsides
  301. * @link http://www.achingbrain.net/
  302. */
  303. class SocketWriteRead implements AkismetRequestSender {
  304. private $response;
  305. private $errorNumber;
  306. private $errorString;
  307. public function __construct() {
  308. $this->errorNumber = 0;
  309. $this->errorString = '';
  310. }
  311. /**
  312. * Sends the data to the remote host.
  313. *
  314. * @param string $host The host to send/receive data.
  315. * @param int $port The port on the remote host.
  316. * @param string $request The data to send.
  317. * @param int $responseLength The amount of data to read. Defaults to 1160 bytes.
  318. * @throws An exception is thrown if a connection cannot be made to the remote host.
  319. * @returns The server response
  320. */
  321. public function send($host, $port, $request, $responseLength = 1160) {
  322. $response = '';
  323. $fs = fsockopen($host, $port, $this->errorNumber, $this->errorString, 3);
  324. if($this->errorNumber != 0) {
  325. throw new Exception('Error connecting to host: ' . $host . ' Error number: ' . $this->errorNumber . ' Error message: ' . $this->errorString);
  326. }
  327. if($fs !== false) {
  328. @fwrite($fs, $request);
  329. while(!feof($fs)) {
  330. $response .= fgets($fs, $responseLength);
  331. }
  332. fclose($fs);
  333. }
  334. return $response;
  335. }
  336. /**
  337. * Returns the server response text
  338. *
  339. * @return string
  340. */
  341. public function getResponse() {
  342. return $this->response;
  343. }
  344. /**
  345. * Returns the error number
  346. *
  347. * If there was no error, 0 will be returned.
  348. *
  349. * @return int
  350. */
  351. public function getErrorNumner() {
  352. return $this->errorNumber;
  353. }
  354. /**
  355. * Returns the error string
  356. *
  357. * If there was no error, an empty string will be returned.
  358. *
  359. * @return string
  360. */
  361. public function getErrorString() {
  362. return $this->errorString;
  363. }
  364. }
  365. /**
  366. * Used internally by the Akismet class and to mock the Akismet anti spam service in
  367. * the unit tests.
  368. *
  369. * N.B. It is not necessary to call this class directly to use the Akismet class.
  370. *
  371. * @package akismet
  372. * @name SocketWriteReadFactory
  373. * @version 0.5
  374. * @author Alex Potsides
  375. * @link http://www.achingbrain.net/
  376. */
  377. class SocketWriteReadFactory implements AkismetRequestFactory {
  378. public function createRequestSender() {
  379. return new SocketWriteRead();
  380. }
  381. }
  382. /**
  383. * Used internally by the Akismet class and to mock the Akismet anti spam service in
  384. * the unit tests.
  385. *
  386. * N.B. It is not necessary to implement this class to use the Akismet class.
  387. *
  388. * @package akismet
  389. * @name AkismetRequestSender
  390. * @version 0.5
  391. * @author Alex Potsides
  392. * @link http://www.achingbrain.net/
  393. */
  394. interface AkismetRequestSender {
  395. /**
  396. * Sends the data to the remote host.
  397. *
  398. * @param string $host The host to send/receive data.
  399. * @param int $port The port on the remote host.
  400. * @param string $request The data to send.
  401. * @param int $responseLength The amount of data to read. Defaults to 1160 bytes.
  402. * @throws An exception is thrown if a connection cannot be made to the remote host.
  403. * @returns The server response
  404. */
  405. public function send($host, $port, $request, $responseLength = 1160);
  406. }
  407. /**
  408. * Used internally by the Akismet class and to mock the Akismet anti spam service in
  409. * the unit tests.
  410. *
  411. * N.B. It is not necessary to implement this class to use the Akismet class.
  412. *
  413. * @package akismet
  414. * @name AkismetRequestFactory
  415. * @version 0.5
  416. * @author Alex Potsides
  417. * @link http://www.achingbrain.net/
  418. */
  419. interface AkismetRequestFactory {
  420. public function createRequestSender();
  421. }
  422. ?>