Go to the documentation of this file.00001 <?php
00012 class helper_akismet extends object {
00013
00020 public function isSpam(array $vars = array()) {
00021 foreach ($vars as $k=>$v)
00022 $this->setCommentVar($k, $v);
00023
00024 $response = $this->sendRequest(
00025 $this->cfg->apiKey.'.'.$this->cfg->apiServer,
00026 '/'.$this->cfg->apiVersion.'/comment-check',
00027 $this->getQueryString());
00028
00029 if ($response[1] == 'invalid' && !$this->verifyKey())
00030 throw new nException('The API key passed to the Akismet helper is invalid. Please obtain a valid one from https://akismet.com/signup/');
00031
00032 return ($response[1] == 'true');
00033 }
00034
00040 public function submitSpam(array $vars = array()) {
00041 foreach ($vars as $k=>$v)
00042 $this->setCommentVar($k, $v);
00043
00044 $this->sendRequest(
00045 $this->cfg->apiKey.'.'.$this->cfg->apiServer,
00046 '/'.$this->cfg->apiVersion.'/submit-spam',
00047 $this->getQueryString());
00048 }
00049
00055 public function submitHam(array $vars = array()) {
00056 foreach($vars as $k=>$v)
00057 $this->setCommentVar($k, $v);
00058
00059 $this->sendRequest(
00060 $this->cfg->apiKey.'.'.$this->cfg->apiServer,
00061 '/'.$this->cfg->apiVersion.'/submit-ham',
00062 $this->getQueryString());
00063 }
00064
00072 public function setCommentVar($key, $val) {
00073 $this->cfg->setInArray('comment', $key, $val);
00074 }
00075
00081 public function setAuthor($author) {
00082 $this->setCommentVar('comment_author', $author);
00083 }
00084
00090 public function setAuthorEmail($email) {
00091 $this->setCommentVar('comment_author_email', $email);
00092 }
00093
00099 public function setAuthorUrl($url) {
00100 $this->setCommentVar('comment_author_url', $url);
00101 }
00102
00108 public function setContent($content) {
00109 $this->setCommentVar('comment_content', $content);
00110 }
00111
00117 public function verifyKey() {
00118 $response = $this->sendRequest(
00119 $this->cfg->apiServer,
00120 '/'.$this->cfg->apiVersion.'/verify-key',
00121 'key='.$this->cfg->apiKey.'&blog='.$this->cfg->url);
00122 return $response[1] == 'valid';
00123 }
00124
00133 protected function sendRequest($host, $path, $request) {
00134 $httpRequest = "POST ".$path." HTTP/1.0\r\n";
00135 $httpRequest.= "Host: ".$host."\r\n";
00136 $httpRequest.= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
00137 $httpRequest.= "Content-Length: ".strlen($request)."\r\n";
00138 $httpRequest.= "User-Agent: ".$this->cfg->userAgent."\r\n";
00139 $httpRequest.= "\r\n";
00140 $httpRequest.= $request;
00141
00142 $response = '';
00143 if (false !== ($fs = @fsockopen($host, $this->cfg->apiPort, $errno, $errstr, 3))) {
00144 fwrite($fs, $httpRequest);
00145 while (!feof($fs))
00146 $response.= fgets($fs, 1160);
00147 fclose($fs);
00148 $response = explode("\r\n\r\n", $response, 2);
00149 }
00150 return $response;
00151 }
00152
00158 protected function getQueryString() {
00159 $queryString = 'blog='.urlencode(stripslashes($this->cfg->url)).'&';
00160 $queryString.= 'user_agent='.urlencode(stripslashes($this->cfg->userAgent)).'&';
00161 foreach ($this->cfg->comment as $k=>$v)
00162 $queryString.= $k.'='.urlencode(stripslashes($v)).'&';
00163
00164 $vars = array_diff_key($_SERVER, array_flip($this->cfg->ignoreServerVars));
00165 foreach ($vars as $k=>$v)
00166 $queryString.= $k.'='.urlencode(stripslashes($v)).'&';
00167
00168 return $queryString;
00169 }
00170 }