Advertisement
Guest User

Untitled

a guest
Jan 25th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.18 KB | None | 0 0
  1. <?php
  2.  
  3. App::uses('AppHelper','View/Helper');
  4.  
  5. App::import(array('Security', 'Validation'));
  6.  
  7. /**
  8.  * CakePHP Gravatar Helper
  9.  *
  10.  * A CakePHP View Helper for the display of Gravatar images (http://www.gravatar.com)
  11.  *
  12.  * @copyright Copyright 2010, Graham Weldon
  13.  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14.  * @package goodies
  15.  * @subpackage goodies.tests.cases.helpers
  16.  *
  17.  */
  18. class GravatarHelper extends AppHelper {
  19.  
  20. /**
  21.  * Gravatar avatar image base URL
  22.  *
  23.  * @var string
  24.  * @access private
  25.  */
  26.     private $__url = array(
  27.         'http' => 'http://www.gravatar.com/avatar/',
  28.         'https' => 'https://secure.gravatar.com/avatar/'
  29.     );
  30.  
  31. /**
  32.  * Hash type to use for email addresses
  33.  *
  34.  * @var string
  35.  * @access private
  36.  */
  37.     private $__hashType = 'md5';
  38.  
  39. /**
  40.  * Collection of allowed ratings
  41.  *
  42.  * @var array
  43.  * @access private
  44.  */
  45.     private $__allowedRatings = array('g', 'pg', 'r', 'x');
  46.  
  47. /**
  48.  * Default Icon sets
  49.  *
  50.  * @var array
  51.  * @access private
  52.  */
  53.     private $__defaultIcons = array('none', 'identicon', 'mm', 'monsterid', 'wavatar', '404');
  54.  
  55. /**
  56.  * Default settings
  57.  *
  58.  * @var array
  59.  * @access private
  60.  */
  61.     private $__default = array(
  62.         'default' => null,
  63.         'size' => null,
  64.         'rating' => null,
  65.         'ext' => false);
  66.  
  67. /**
  68.  * Helpers used by this helper
  69.  *
  70.  * @var array
  71.  * @access public
  72.  */
  73.     public $helpers = array('Html');
  74.  
  75. /**
  76.  * Constructor
  77.  *
  78.  * @access public
  79.  */
  80.     public function __construct() {
  81.         // Default the secure option to match the current URL.
  82.         $this->__default['secure'] = env('HTTPS');
  83.     }
  84.  
  85. /**
  86.  * Show gravatar for the supplied email address
  87.  *
  88.  * @param string $email Email address
  89.  * @param array $options Array of options, keyed from default settings
  90.  * @return string Gravatar image string
  91.  * @access public
  92.  */
  93.     public function image($email, $options = array()) {
  94.         $imageUrl = $this->url($email, $options);
  95.         unset($options['default'], $options['size'], $options['rating'], $options['ext']);
  96.        
  97.         return $this->Html->image($imageUrl, $options);
  98.     }
  99.  
  100. /**
  101.  * Generate image URL
  102.  *
  103.  * @param string $email Email address
  104.  * @param string $options Array of options, keyed from default settings
  105.  * @return string Gravatar Image URL
  106.  * @access public
  107.  */
  108.     public function url($email, $options = array()) {
  109.         $options = $this->__cleanOptions(array_merge($this->__default, $options));
  110.         $ext = $options['ext'];
  111.         $secure = $options['secure'];
  112.         unset($options['ext'], $options['secure']);
  113.         $protocol = $secure === true ? 'https' : 'http';
  114.  
  115.         $imageUrl = $this->__url[$protocol] . $this->__emailHash($email, $this->__hashType);
  116.         if ($ext === true) {
  117.             // If 'ext' option is supplied and true, append an extension to the generated image URL.
  118.             // This helps systems that don't display images unless they have a specific image extension on the URL.
  119.             $imageUrl .= '.jpg';
  120.         }
  121.         $imageUrl .= $this->__buildOptions($options);
  122.         return $imageUrl;
  123.     }
  124.  
  125. /**
  126.  * Sanitize the options array
  127.  *
  128.  * @param array $options Array of options, keyed from default settings
  129.  * @return array Clean options array
  130.  * @access private
  131.  */
  132.     private function __cleanOptions($options) {
  133.         if (!isset($options['size']) || empty($options['size']) || !is_numeric($options['size'])) {
  134.             unset($options['size']);
  135.         } else {
  136.             $options['size'] = min(max($options['size'], 1), 512);
  137.         }
  138.  
  139.         if (!$options['rating'] || !in_array(mb_strtolower($options['rating']), $this->__allowedRatings)) {
  140.             unset($options['rating']);
  141.         }
  142.  
  143.         if (!$options['default']) {
  144.             unset($options['default']);
  145.         } else {
  146.             if (!in_array($options['default'], $this->__defaultIcons) && !Validation::url($options['default'])) {
  147.                 unset($options['default']);
  148.             }
  149.         }
  150.         return $options;
  151.     }
  152.  
  153. /**
  154.  * Generate email address hash
  155.  *
  156.  * @param string $email Email address
  157.  * @param string $type Hash type to employ
  158.  * @return string Email address hash
  159.  * @access private
  160.  */
  161.     private function __emailHash($email, $type) {
  162.         return Security::hash(mb_strtolower($email), $type);
  163.     }
  164.  
  165. /**
  166.  * Build Options URL string
  167.  *
  168.  * @param array $options Array of options, keyed from default settings
  169.  * @return string URL string of options
  170.  * @access private
  171.  */
  172.     private function __buildOptions($options = array()) {
  173.         $gravatarOptions = array_intersect(array_keys($options), array_keys($this->__default));
  174.         if (!empty($gravatarOptions)) {
  175.             $optionArray = array();
  176.             foreach ($gravatarOptions as $key) {
  177.                 $value = $options[$key];
  178.                 $optionArray[] = $key . '=' . mb_strtolower($value);
  179.             }
  180.             return '?' . implode('&amp;', $optionArray);
  181.         }
  182.         return '';
  183.     }
  184. }
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement