File indexing completed on 2024-12-22 05:37:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id: Doctype.php 16971 2009-07-22 18:05:45Z mikaelkael $
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_View_Helper_HtmlElement */
0024 // require_once 'Zend/View/Helper/HtmlElement.php';
0025 
0026 /**
0027  * Helper for retrieving avatars from gravatar.com
0028  *
0029  * @package    Zend_View
0030  * @subpackage Helper
0031  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  * @link http://pl.gravatar.com/site/implement/url
0034  */
0035 class Zend_View_Helper_Gravatar extends Zend_View_Helper_HtmlElement
0036 {
0037 
0038     /**
0039      * URL to gravatar service
0040      */
0041     const GRAVATAR_URL = 'http://www.gravatar.com/avatar';
0042     /**
0043      * Secure URL to gravatar service
0044      */
0045     const GRAVATAR_URL_SECURE = 'https://secure.gravatar.com/avatar';
0046 
0047     /**
0048      * Gravatar rating
0049      */
0050     const RATING_G  = 'g';
0051     const RATING_PG = 'pg';
0052     const RATING_R  = 'r';
0053     const RATING_X  = 'x';
0054 
0055     /**
0056      * Default gravatar image value constants
0057      */
0058     const DEFAULT_404       = '404';
0059     const DEFAULT_MM        = 'mm';
0060     const DEFAULT_IDENTICON = 'identicon';
0061     const DEFAULT_MONSTERID = 'monsterid';
0062     const DEFAULT_WAVATAR   = 'wavatar';
0063 
0064     /**
0065      * Options
0066      *
0067      * @var array
0068      */
0069     protected $_options = array(
0070         'img_size'    => 80,
0071         'default_img' => self::DEFAULT_MM,
0072         'rating'      => self::RATING_G,
0073         'secure'      => null,
0074     );
0075 
0076     /**
0077      * Email Adress
0078      *
0079      * @var string
0080      */
0081     protected $_email;
0082 
0083     /**
0084      * Attributes for HTML image tag
0085      *
0086      * @var array
0087      */
0088     protected $_attribs;
0089 
0090     /**
0091      * Returns an avatar from gravatar's service.
0092      *
0093      * $options may include the following:
0094      * - 'img_size' int height of img to return
0095      * - 'default_img' string img to return if email adress has not found
0096      * - 'rating' string rating parameter for avatar
0097      * - 'secure' bool load from the SSL or Non-SSL location
0098      *
0099      * @see    http://pl.gravatar.com/site/implement/url
0100      * @see    http://pl.gravatar.com/site/implement/url More information about gravatar's service.
0101      * @param  string|null $email Email adress.
0102      * @param  null|array $options Options
0103      * @param  array $attribs Attributes for image tag (title, alt etc.)
0104      * @return Zend_View_Helper_Gravatar
0105      */
0106     public function gravatar($email = "", $options = array(), $attribs = array())
0107     {
0108         $this->setEmail($email);
0109         $this->setOptions($options);
0110         $this->setAttribs($attribs);
0111         return $this;
0112     }
0113 
0114     /**
0115      * Configure state
0116      *
0117      * @param  array $options
0118      * @return Zend_View_Helper_Gravatar
0119      */
0120     public function setOptions(array $options)
0121     {
0122         foreach ($options as $key => $value) {
0123             $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
0124             if (method_exists($this, $method)) {
0125                 $this->{$method}($value);
0126             }
0127         }
0128         return $this;
0129     }
0130 
0131     /**
0132      * Get img size
0133      *
0134      * @return int The img size
0135      */
0136     public function getImgSize()
0137     {
0138         return $this->_options['img_size'];
0139     }
0140 
0141     /**
0142      * Set img size in pixels
0143      *
0144      * @param int $imgSize Size of img must be between 1 and 512
0145      * @return Zend_View_Helper_Gravatar
0146      */
0147     public function setImgSize($imgSize)
0148     {
0149         $this->_options['img_size'] = (int) $imgSize;
0150         return $this;
0151     }
0152 
0153     /**
0154      * Get default img
0155      *
0156      * @return string
0157      */
0158     public function getDefaultImg()
0159     {
0160         return $this->_options['default_img'];
0161     }
0162 
0163     /**
0164      * Set default img
0165      *
0166      * Can be either an absolute URL to an image, or one of the DEFAULT_* constants
0167      *
0168      * @param string $defaultImg
0169      * @link http://pl.gravatar.com/site/implement/url More information about default image.
0170      * @return Zend_View_Helper_Gravatar
0171      */
0172     public function setDefaultImg($defaultImg)
0173     {
0174         $this->_options['default_img'] = urlencode($defaultImg);
0175         return $this;
0176     }
0177 
0178     /**
0179      *  Set rating value
0180      *
0181      * Must be one of the RATING_* constants
0182      *
0183      * @param string $rating Value for rating. Allowed values are: g, px, r,x
0184      * @link http://pl.gravatar.com/site/implement/url More information about rating.
0185      * @throws Zend_View_Exception
0186      */
0187     public function setRating($rating)
0188     {
0189         switch ($rating) {
0190             case self::RATING_G:
0191             case self::RATING_PG:
0192             case self::RATING_R:
0193             case self::RATING_X:
0194                 $this->_options['rating'] = $rating;
0195                 break;
0196             default:
0197                 // require_once 'Zend/View/Exception.php';
0198                 throw new Zend_View_Exception(sprintf(
0199                     'The rating value "%s" is not allowed',
0200                     $rating
0201                 ));
0202         }
0203         return $this;
0204     }
0205 
0206     /**
0207      * Get rating value
0208      *
0209      * @return string
0210      */
0211     public function getRating()
0212     {
0213         return $this->_options['rating'];
0214     }
0215 
0216     /**
0217      * Set email adress
0218      *
0219      * @param string $email
0220      * @return Zend_View_Helper_Gravatar
0221      */
0222     public function setEmail( $email )
0223     {
0224         $this->_email = $email;
0225         return $this;
0226     }
0227 
0228     /**
0229      * Get email adress
0230      *
0231      * @return string
0232      */
0233     public function getEmail()
0234     {
0235         return $this->_email;
0236     }
0237 
0238     /**
0239      * Load from an SSL or No-SSL location?
0240      *
0241      * @param bool $flag
0242      * @return Zend_View_Helper_Gravatar
0243      */
0244     public function setSecure($flag)
0245     {
0246         $this->_options['secure'] = ($flag === null) ? null : (bool) $flag;
0247         return $this;
0248     }
0249 
0250     /**
0251      * Get an SSL or a No-SSL location
0252      *
0253      * @return bool
0254      */
0255     public function getSecure()
0256     {
0257         if ($this->_options['secure'] === null) {
0258             return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
0259         }
0260         return $this->_options['secure'];
0261     }
0262 
0263     /**
0264      * Get attribs of image
0265      *
0266      * Warning!
0267      * If you set src attrib, you get it, but this value will be overwritten in
0268      * protected method _setSrcAttribForImg(). And finally your get other src
0269      * value!
0270      *
0271      * @return array
0272      */
0273     public function getAttribs()
0274     {
0275         return $this->_attribs;
0276     }
0277 
0278     /**
0279      * Set attribs for image tag
0280      *
0281      * Warning! You shouldn't set src attrib for image tag.
0282      * This attrib is overwritten in protected method _setSrcAttribForImg().
0283      * This method(_setSrcAttribForImg) is called in public method getImgTag().
0284 
0285      * @param array $attribs
0286      * @return Zend_View_Helper_Gravatar
0287      */
0288     public function setAttribs(array $attribs)
0289     {
0290         $this->_attribs = $attribs;
0291         return $this;
0292     }
0293 
0294     /**
0295      * Get URL to gravatar's service.
0296      *
0297      * @return string URL
0298      */
0299     protected function _getGravatarUrl()
0300     {
0301         return ($this->getSecure() === false) ? self::GRAVATAR_URL : self::GRAVATAR_URL_SECURE;
0302     }
0303 
0304     /**
0305      * Get avatar url (including size, rating and default image oprions)
0306      *
0307      * @return string
0308      */
0309     protected function _getAvatarUrl()
0310     {
0311         $src = $this->_getGravatarUrl()
0312              . '/'
0313              . md5(strtolower(trim($this->getEmail())))
0314              . '?s='
0315              . $this->getImgSize()
0316              . '&d='
0317              . $this->getDefaultImg()
0318              . '&r='
0319              . $this->getRating();
0320         return $src;
0321     }
0322 
0323     /**
0324      * Set src attrib for image.
0325      *
0326      * You shouldn't set a own url value!
0327      * It sets value, uses protected method _getAvatarUrl.
0328      *
0329      * If already exsist overwritten.
0330      */
0331     protected function _setSrcAttribForImg()
0332     {
0333         $attribs        = $this->getAttribs();
0334         $attribs['src'] = $this->_getAvatarUrl();
0335         $this->setAttribs($attribs);
0336     }
0337 
0338     /**
0339      * Return valid image tag
0340      *
0341      * @return string
0342      */
0343     public function getImgTag()
0344     {
0345         $this->_setSrcAttribForImg();
0346         $html = '<img'
0347               . $this->_htmlAttribs($this->getAttribs())
0348               . $this->getClosingBracket();
0349 
0350         return $html;
0351     }
0352 
0353     /**
0354      * Return valid image tag
0355      *
0356      * @return string
0357      */
0358     public function  __toString()
0359     {
0360         return $this->getImgTag();
0361 
0362     }
0363 }