File indexing completed on 2024-05-12 06:02:11

0001 <?php
0002 
0003 /**
0004  *  Class Gravatar
0005  *
0006  * From Gravatar Help:
0007  *        "A gravatar is a dynamic image resource that is requested from our server. The request
0008  *        URL is presented here, broken into its segments."
0009  * Source:
0010  *    http://site.gravatar.com/site/implement
0011  *
0012  * Usage:
0013  * <code>
0014  *        $email = "youremail@yourhost.com";
0015  *        $default = "http://www.yourhost.com/default_image.jpg";    // Optional
0016  *        $gravatar = new Gravatar($email, $default);
0017  *        $gravatar->size = 80;
0018  *        $gravatar->rating = "G";
0019  *        $gravatar->border = "FF0000";
0020  *
0021  *        echo $gravatar; // Or echo $gravatar->toHTML();
0022  * </code>
0023  *
0024  *    Class Page: http://www.phpclasses.org/browse/package/4227.html
0025  *
0026  * @author Lucas Araújo <araujo.lucas@gmail.com>
0027  * @version 1.0
0028  * @package Gravatar
0029  */
0030 class Local_Tools_Gravatar
0031 {
0032     /**
0033      *    Gravatar's url
0034      */
0035     const GRAVATAR_URL = "http://www.gravatar.com/avatar.php";
0036     /**
0037      *    Query string. key/value
0038      */
0039     protected $_properties = array(
0040         "gravatar_id" => null,
0041         "default" => null,
0042         "size" => 80, // The default value
0043         "rating" => null,
0044         "border" => null,
0045     );
0046     /**
0047      *    E-mail. This will be converted to md5($email)
0048      */
0049     protected $email = "";
0050     /**
0051      *    Extra attributes to the IMG tag like ALT, CLASS, STYLE...
0052      */
0053     protected $extra = "";
0054     /**
0055      *    Ratings available
0056      */
0057     private $GRAVATAR_RATING = array("G", "PG", "R", "X");
0058 
0059     /**
0060      *
0061      */
0062     public function __construct($email = null, $default = null)
0063     {
0064         $this->setEmail($email);
0065         $this->setDefault($default);
0066     }
0067 
0068     /**
0069      *
0070      */
0071     public function setEmail($email)
0072     {
0073         if ($this->isValidEmail($email)) {
0074             $this->email = $email;
0075             $this->_properties['gravatar_id'] = md5(strtolower($this->email));
0076             return true;
0077         }
0078         return false;
0079     }
0080 
0081     /**
0082      *
0083      */
0084     public function isValidEmail($email)
0085     {
0086         // Source: http://www.zend.com/zend/spotlight/ev12apr.php
0087         return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
0088     }
0089 
0090     /**
0091      *
0092      */
0093     public function setDefault($default)
0094     {
0095         $this->_properties['default'] = $default;
0096     }
0097 
0098     /**
0099      *
0100      */
0101     public function setExtra($extra)
0102     {
0103         $this->extra = $extra;
0104     }
0105 
0106     /**
0107      *    Object property overloading
0108      */
0109     public function __get($var)
0110     {
0111         return @$this->_properties[$var];
0112     }
0113 
0114     /**
0115      *    Object property overloading
0116      */
0117     public function __set($var, $value)
0118     {
0119         switch ($var) {
0120             case "email":
0121                 return $this->setEmail($value);
0122             case "rating":
0123                 return $this->setRating($value);
0124             case "default":
0125                 return $this->setDefault($value);
0126             case "size":
0127                 return $this->setSize($value);
0128             // Cannot set gravatar_id
0129             case "gravatar_id":
0130                 return;
0131         }
0132         return @$this->_properties[$var] = $value;
0133     }
0134 
0135     /**
0136      *
0137      */
0138     public function setRating($rating)
0139     {
0140         if (in_array($rating, $this->GRAVATAR_RATING)) {
0141             $this->_properties['rating'] = $rating;
0142             return true;
0143         }
0144         return false;
0145     }
0146 
0147     /**
0148      *
0149      */
0150     public function setSize($size)
0151     {
0152         $size = (int)$size;
0153         if ($size <= 0) {
0154             $size = null;
0155         } // Use the default size
0156         $this->_properties['size'] = $size;
0157     }
0158 
0159     /**
0160      *    Object property overloading
0161      */
0162     public function __isset($var)
0163     {
0164         return isset($this->_properties[$var]);
0165     }
0166 
0167     /**
0168      *    Object property overloading
0169      */
0170     public function __unset($var)
0171     {
0172         return @$this->_properties[$var] == null;
0173     }
0174 
0175     /**
0176      *    toString
0177      */
0178     public function __toString()
0179     {
0180         return $this->toHTML();
0181     }
0182 
0183     /**
0184      *    toHTML
0185      */
0186     public function toHTML()
0187     {
0188         return '<img src="' . $this->getSrc() . '"'
0189         . (!isset($this->size) ? "" : ' width="' . $this->size . '" height="' . $this->size . '"')
0190         . $this->extra
0191         . ' />';
0192     }
0193 
0194     /**
0195      *    Get source
0196      */
0197     public function getSrc()
0198     {
0199         $url = self::GRAVATAR_URL . "?";
0200         $first = true;
0201         foreach ($this->_properties as $key => $value) {
0202             if (isset($value)) {
0203                 if (!$first) {
0204                     $url .= "&";
0205                 }
0206                 $url .= $key . "=" . urlencode($value);
0207                 $first = false;
0208             }
0209         }
0210         return $url;
0211     }
0212 }