Gravatar.php

Go to the documentation of this file.
00001 <?php
00002 
00032 class Robo47_Service_Gravatar extends Zend_Service_Abstract
00033 {
00037     const API_URL = 'http://www.gravatar.com/avatar/';
00038 
00042     const API_SSL_URL = 'https://secure.gravatar.com/avatar/';
00043 
00047     const RATING_G = 'g';
00048 
00052     const RATING_PG = 'pg';
00053 
00057     const RATING_R = 'r';
00058 
00062     const RATING_X = 'x';
00063 
00068     const DEFAULT_IDENTICON = 'identicon';
00069 
00074     const DEFAULT_MONSTERID = 'monsterid';
00075 
00080     const DEFAULT_WAVATAR = 'wavatar';
00081 
00085     const DEFAULT_404 = '404';
00086 
00098     protected $_rating = 'g';
00104     protected $_size = 80;
00118     protected $_default = '';
00124     protected $_cache = null;
00130     protected $_cachePrefix = 'gravatar_';
00136     protected $_useSSL = false;
00137 
00142     public function __construct(array $options = array())
00143     {
00144         $this->setOptions($options);
00145     }
00146 
00153     public function setOptions(array $options = array())
00154     {
00155         foreach ($options as $key => $value) {
00156             switch ($key) {
00157                 case 'rating':
00158                     $this->setRating($value);
00159                     break;
00160                 case 'default':
00161                     $this->setDefault($value);
00162                     break;
00163                 case 'cache':
00164                     $this->setCache($value);
00165                     break;
00166                 case 'cachePrefix':
00167                     $this->setCachePrefix($value);
00168                     break;
00169                 case 'size':
00170                     $this->setSize($value);
00171                     break;
00172                 case 'useSSL':
00173                     $this->useSSL($value);
00174                     break;
00175                 default:
00176                     break;
00177             }
00178         }
00179         return $this;
00180     }
00181 
00188     public function useSSL($flag)
00189     {
00190         $this->_useSSL = (bool) $flag;
00191         return $this;
00192     }
00193 
00199     public function usesSSL()
00200     {
00201         return $this->_useSSL;
00202     }
00203 
00210     public function setCache($cache = null)
00211     {
00212         if (is_string($cache)) {
00213             $cache = $this->_cacheFromRegistry($cache);
00214         }
00215         if (!$cache instanceof Zend_Cache_Core &&
00216             null !== $cache) {
00217             $message = 'cache is not instance of Zend_Cache_Core';
00218             throw new Robo47_Service_Gravatar_Exception($message);
00219         }
00220         $this->_cache = $cache;
00221         return $this;
00222     }
00223 
00231     protected function _cacheFromRegistry($key)
00232     {
00233         if (Zend_Registry::isRegistered($key)) {
00234             return Zend_Registry::get($key);
00235         } else {
00236             $message = 'Registry key "' . $key .
00237                 '" for Cache is not registered.';
00238             throw new Robo47_Service_Gravatar_Exception($message);
00239         }
00240     }
00241 
00247     public function getCache()
00248     {
00249         return $this->_cache;
00250     }
00251 
00258     public function setCachePrefix($cachePrefix)
00259     {
00260         $this->_cachePrefix = $cachePrefix;
00261         return $this;
00262     }
00263 
00269     public function getCachePrefix()
00270     {
00271         return $this->_cachePrefix;
00272     }
00273 
00280     public function setRating($rating)
00281     {
00282         $rating = strtolower($rating);
00283         switch ($rating) {
00284             case self::RATING_G:
00285             case self::RATING_PG:
00286             case self::RATING_R:
00287             case self::RATING_X:
00288                 $this->_rating = $rating;
00289                 break;
00290             default:
00291                 $message = 'Invalid rating: ' . $rating;
00292                 throw new Robo47_Service_Gravatar_Exception($message);
00293         }
00294         return $this;
00295     }
00296 
00302     public function getRating()
00303     {
00304         return $this->_rating;
00305     }
00306 
00313     public function setSize($size)
00314     {
00315         $size = (int) $size;
00316         if ($size < 1) {
00317             $message = 'size is smaller than 1';
00318             throw new Robo47_Service_Gravatar_Exception($message);
00319         }
00320         if ($size > 512) {
00321             $message = 'size is greater than 512';
00322             throw new Robo47_Service_Gravatar_Exception($message);
00323         }
00324         $this->_size = $size;
00325         return $this;
00326     }
00327 
00333     public function getSize()
00334     {
00335         return $this->_size;
00336     }
00337 
00349     public function setDefault($default)
00350     {
00351         $this->_default = $default;
00352         return $this;
00353     }
00354 
00360     public function getDefault()
00361     {
00362         return $this->_default;
00363     }
00364 
00373     protected function _filterSize($size)
00374     {
00375         if (null === $size) {
00376             return $this->getSize();
00377         } else {
00378             return (int) $size;
00379         }
00380     }
00381 
00390     protected function _filterRating($rating)
00391     {
00392         $rating = strtolower($rating);
00393         switch ($rating) {
00394             case self::RATING_G:
00395             case self::RATING_PG:
00396             case self::RATING_R:
00397             case self::RATING_X:
00398                 return $rating;
00399                 break;
00400             default:
00401                 return $this->getRating();
00402         }
00403     }
00404 
00413     protected function _filterDefault($default)
00414     {
00415         if (null === $default) {
00416             return $this->getDefault();
00417         } else {
00418             return (string) $default;
00419         }
00420     }
00421 
00430     protected function _filterSsl($ssl)
00431     {
00432         if (null === $ssl) {
00433             return $this->usesSSL();
00434         } else {
00435             return (bool) $ssl;
00436         }
00437     }
00438 
00447     protected function _filterCachePrefix($cachePrefix)
00448     {
00449         if (null === $cachePrefix) {
00450             return $this->getCachePrefix();
00451         } else {
00452             return (string) $cachePrefix;
00453         }
00454     }
00455 
00467     public function getUri($email, $size = null, $rating = null,
00468         $default = null, $ssl = null, $separator = '&amp;')
00469     {
00470         $size = $this->_filterSize($size);
00471         $rating = $this->_filterRating($rating);
00472         $default = $this->_filterDefault($default);
00473         $ssl = $this->_filterSsl($ssl);
00474 
00475         return $this->_getUri(
00476             $email,
00477             $size,
00478             $rating,
00479             $default,
00480             $ssl,
00481             $separator
00482         );
00483     }
00484 
00495     protected function _getUri($email, $size, $rating, $default, $ssl,
00496         $separator)
00497     {
00498         if ($ssl) {
00499             $url = Robo47_Service_Gravatar::API_SSL_URL;
00500         } else {
00501             $url = Robo47_Service_Gravatar::API_URL;
00502         }
00503 
00504         $url .= $this->getGravatarHash($email);
00505 
00506         $params = array(
00507             's' => $size,
00508             'r' => $rating,
00509             'd' => $default,
00510         );
00511         return $url . '?' . http_build_query($params, '', $separator);
00512     }
00513 
00520     public function getGravatarHash($email)
00521     {
00522         return md5(strtolower(trim($email)));
00523     }
00524 
00535     public function getCacheId($email, $cachePrefix = null, $rating = null,
00536         $size = null, $default = null)
00537     {
00538         $cachePrefix = $this->_filterCachePrefix($cachePrefix);
00539         $size = $this->_filterSize($size);
00540         $rating = $this->_filterRating($rating);
00541         $default = $this->_filterDefault($default);
00542 
00543         $id = $cachePrefix;
00544         $id .= $this->getGravatarHash($email);
00545         $id .= '_' . $rating;
00546         $id .= '_' . $size;
00547         $id .= '_' . md5($default);
00548         return $id;
00549     }
00550 
00557     public function _getCacheId($email)
00558     {
00559         return $this->getCacheId(
00560             $email,
00561             $this->getCachePrefix(),
00562             $this->getRating(),
00563             $this->getSize(),
00564             $this->getDefault()
00565         );
00566     }
00567 
00575     public function gravatarExists($email, $cached = true)
00576     {
00577         $cacheId = $this->_getCacheId($email);
00578         if (null !== $this->_cache && $cached) {
00579             $successful = $this->_cache->load($cacheId);
00580             if ('true' === $successful) {
00581                 return true;
00582             } elseif ('false' === $successful) {
00583                 return false;
00584             }
00585         }
00586         $httpClient = self::getHttpClient();
00587 
00588         $uri = $this->getUri($email, null, null, null, null, '&');
00589         $httpClient->setUri($uri);
00590 
00591         $response = $httpClient->request(Zend_Http_Client::GET);
00592         $success = false;
00593         if (200 == $response->getStatus()) {
00594             $success = true;
00595         }
00596         if (null !== $this->_cache) {
00597             if ($success) {
00598                 $this->_cache->save('true', $cacheId);
00599             } else {
00600                 $this->_cache->save('false', $cacheId);
00601             }
00602         }
00603         return $success;
00604     }
00605 }