nyroFwk  0.2
akismet.class.php
Go to the documentation of this file.
1 <?php
12 class helper_akismet extends object {
13 
20  public function isSpam(array $vars = array()) {
21  foreach ($vars as $k=>$v)
22  $this->setCommentVar($k, $v);
23 
24  $response = $this->sendRequest(
25  $this->cfg->apiKey.'.'.$this->cfg->apiServer,
26  '/'.$this->cfg->apiVersion.'/comment-check',
27  $this->getQueryString());
28 
29  if ($response[1] == 'invalid' && !$this->verifyKey())
30  throw new nException('The API key passed to the Akismet helper is invalid. Please obtain a valid one from https://akismet.com/signup/');
31 
32  return ($response[1] == 'true');
33  }
34 
40  public function submitSpam(array $vars = array()) {
41  foreach ($vars as $k=>$v)
42  $this->setCommentVar($k, $v);
43 
44  $this->sendRequest(
45  $this->cfg->apiKey.'.'.$this->cfg->apiServer,
46  '/'.$this->cfg->apiVersion.'/submit-spam',
47  $this->getQueryString());
48  }
49 
55  public function submitHam(array $vars = array()) {
56  foreach($vars as $k=>$v)
57  $this->setCommentVar($k, $v);
58 
59  $this->sendRequest(
60  $this->cfg->apiKey.'.'.$this->cfg->apiServer,
61  '/'.$this->cfg->apiVersion.'/submit-ham',
62  $this->getQueryString());
63  }
64 
72  public function setCommentVar($key, $val) {
73  $this->cfg->setInArray('comment', $key, $val);
74  }
75 
81  public function setAuthor($author) {
82  $this->setCommentVar('comment_author', $author);
83  }
84 
90  public function setAuthorEmail($email) {
91  $this->setCommentVar('comment_author_email', $email);
92  }
93 
99  public function setAuthorUrl($url) {
100  $this->setCommentVar('comment_author_url', $url);
101  }
102 
108  public function setContent($content) {
109  $this->setCommentVar('comment_content', $content);
110  }
111 
117  public function verifyKey() {
118  $response = $this->sendRequest(
119  $this->cfg->apiServer,
120  '/'.$this->cfg->apiVersion.'/verify-key',
121  'key='.$this->cfg->apiKey.'&blog='.$this->cfg->url);
122  return $response[1] == 'valid';
123  }
124 
133  protected function sendRequest($host, $path, $request) {
134  $httpRequest = "POST ".$path." HTTP/1.0\r\n";
135  $httpRequest.= "Host: ".$host."\r\n";
136  $httpRequest.= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
137  $httpRequest.= "Content-Length: ".strlen($request)."\r\n";
138  $httpRequest.= "User-Agent: ".$this->cfg->userAgent."\r\n";
139  $httpRequest.= "\r\n";
140  $httpRequest.= $request;
141 
142  $response = '';
143  if (false !== ($fs = @fsockopen($host, $this->cfg->apiPort, $errno, $errstr, 3))) {
144  fwrite($fs, $httpRequest);
145  while (!feof($fs))
146  $response.= fgets($fs, 1160); // One TCP-IP packet
147  fclose($fs);
148  $response = explode("\r\n\r\n", $response, 2);
149  }
150  return $response;
151  }
152 
158  protected function getQueryString() {
159  $queryString = 'blog='.urlencode(stripslashes($this->cfg->url)).'&';
160  $queryString.= 'user_agent='.urlencode(stripslashes($this->cfg->userAgent)).'&';
161  foreach ($this->cfg->comment as $k=>$v)
162  $queryString.= $k.'='.urlencode(stripslashes($v)).'&';
163 
164  $vars = array_diff_key($_SERVER, array_flip($this->cfg->ignoreServerVars));
165  foreach ($vars as $k=>$v)
166  $queryString.= $k.'='.urlencode(stripslashes($v)).'&';
167 
168  return $queryString;
169  }
170 }
submitSpam(array $vars=array())
setAuthor($author)
setAuthorEmail($email)
isSpam(array $vars=array())
setContent($content)
setCommentVar($key, $val)
sendRequest($host, $path, $request)
submitHam(array $vars=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13