Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Service/Akismet.php
Go to the documentation of this file.
00001 <?php
00027 require_once 'Zend/Version.php';
00028 
00032 require_once 'Zend/Service/Abstract.php';
00033 
00034 
00045 class Zend_Service_Akismet extends Zend_Service_Abstract
00046 {
00051     protected $_apiKey;
00052 
00057     protected $_blogUrl;
00058 
00063     protected $_charset = 'UTF-8';
00064 
00069     protected $_port = 80;
00070 
00075     protected $_userAgent;
00076 
00084     public function __construct($apiKey, $blog)
00085     {
00086         $this->setBlogUrl($blog)
00087              ->setApiKey($apiKey)
00088              ->setUserAgent('Zend Framework/' . Zend_Version::VERSION . ' | Akismet/1.11');
00089     }
00090 
00096     public function getBlogUrl()
00097     {
00098         return $this->_blogUrl;
00099     }
00100 
00108     public function setBlogUrl($blogUrl)
00109     {
00110         require_once 'Zend/Uri.php';
00111         if (!Zend_Uri::check($blogUrl)) {
00112             require_once 'Zend/Service/Exception.php';
00113             throw new Zend_Service_Exception('Invalid url provided for blog');
00114         }
00115 
00116         $this->_blogUrl = $blogUrl;
00117         return $this;
00118     }
00119 
00125     public function getApiKey()
00126     {
00127         return $this->_apiKey;
00128     }
00129 
00136     public function setApiKey($apiKey)
00137     {
00138         $this->_apiKey = $apiKey;
00139         return $this;
00140     }
00141 
00147     public function getCharset()
00148     {
00149         return $this->_charset;
00150     }
00151 
00158     public function setCharset($charset)
00159     {
00160         $this->_charset = $charset;
00161         return $this;
00162     }
00163 
00169     public function getPort()
00170     {
00171         return $this->_port;
00172     }
00173 
00181     public function setPort($port)
00182     {
00183         if (!is_int($port)) {
00184             require_once 'Zend/Service/Exception.php';
00185             throw new Zend_Service_Exception('Invalid port');
00186         }
00187 
00188         $this->_port = $port;
00189         return $this;
00190     }
00191 
00197     public function getUserAgent()
00198     {
00199         return $this->_userAgent;
00200     }
00201 
00211     public function setUserAgent($userAgent)
00212     {
00213         if (!is_string($userAgent)
00214             || !preg_match(":^[^\n/]*/[^ ]* \| Akismet/[0-9\.]*$:i", $userAgent))
00215         {
00216             require_once 'Zend/Service/Exception.php';
00217             throw new Zend_Service_Exception('Invalid User Agent string; must be of format "Application name/version | Akismet/version"');
00218         }
00219 
00220         $this->_userAgent = $userAgent;
00221         return $this;
00222     }
00223 
00232     protected function _post($host, $path, array $params)
00233     {
00234         $uri    = 'http://' . $host . ':' . $this->getPort() . $path;
00235         $client = self::getHttpClient();
00236         $client->setUri($uri);
00237         $client->setConfig(array(
00238             'useragent'    => $this->getUserAgent(),
00239         ));
00240 
00241         $client->setHeaders(array(
00242             'Host'         => $host,
00243             'Content-Type' => 'application/x-www-form-urlencoded; charset=' . $this->getCharset()
00244         ));
00245         $client->setParameterPost($params);
00246 
00247         $client->setMethod(Zend_Http_Client::POST);
00248         return $client->request();
00249     }
00250 
00258     public function verifyKey($key = null, $blog = null)
00259     {
00260         if (null === $key) {
00261             $key = $this->getApiKey();
00262         }
00263 
00264         if (null === $blog) {
00265             $blog = $this->getBlogUrl();
00266         }
00267 
00268         $response = $this->_post('rest.akismet.com', '/1.1/verify-key', array(
00269             'key'  => $key,
00270             'blog' => $blog
00271         ));
00272 
00273         return ('valid' == $response->getBody());
00274     }
00275 
00284     protected function _makeApiCall($path, $params)
00285     {
00286         if (empty($params['user_ip']) || empty($params['user_agent'])) {
00287             require_once 'Zend/Service/Exception.php';
00288             throw new Zend_Service_Exception('Missing required Akismet fields (user_ip and user_agent are required)');
00289         }
00290 
00291         if (!isset($params['blog'])) {
00292             $params['blog'] = $this->getBlogUrl();
00293         }
00294 
00295         return $this->_post($this->getApiKey() . '.rest.akismet.com', $path, $params);
00296     }
00297 
00324     public function isSpam($params)
00325     {
00326         $response = $this->_makeApiCall('/1.1/comment-check', $params);
00327 
00328         $return = trim($response->getBody());
00329 
00330         if ('invalid' == $return) {
00331             require_once 'Zend/Service/Exception.php';
00332             throw new Zend_Service_Exception('Invalid API key');
00333         }
00334 
00335         if ('true' == $return) {
00336             return true;
00337         }
00338 
00339         return false;
00340     }
00341 
00355     public function submitSpam($params)
00356     {
00357         $response = $this->_makeApiCall('/1.1/submit-spam', $params);
00358         $value    = trim($response->getBody());
00359         if ('invalid' == $value) {
00360             require_once 'Zend/Service/Exception.php';
00361             throw new Zend_Service_Exception('Invalid API key');
00362         }
00363     }
00364 
00383     public function submitHam($params)
00384     {
00385         $response = $this->_makeApiCall('/1.1/submit-ham', $params);
00386     }
00387 }
 All Data Structures Namespaces Files Functions Variables Enumerations