| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- App::uses('AppHelper', 'View/Helper');
- /**
- * CakePHP Gravatar Helper
- *
- * A CakePHP View Helper for the display of Gravatar images (http://www.gravatar.com)
- *
- * @copyright Copyright 2009-2010, Graham Weldon (http://grahamweldon.com)
- * @license http://opensource.org/licenses/mit-license.php MIT
- *
- * hashtype now always md5
- */
- class GravatarHelper extends AppHelper {
- /**
- * Gravatar avatar image base URL
- *
- * @var string
- */
- protected $_url = [
- 'http' => 'http://www.gravatar.com/avatar/',
- 'https' => 'https://secure.gravatar.com/avatar/'
- ];
- /**
- * Collection of allowed ratings
- *
- * @var array
- */
- protected $_allowedRatings = [
- 'g', 'pg', 'r', 'x'];
- /**
- * Default Icon sets
- *
- * @var array
- */
- protected $_defaultIcons = [
- 'none', 'mm', 'identicon', 'monsterid', 'retro', 'wavatar', '404'];
- /**
- * Default settings
- *
- * @var array
- */
- protected $_defaultConfig = [
- 'default' => null,
- 'size' => null,
- 'rating' => null,
- 'ext' => false];
- /**
- * Helpers used by this helper
- *
- * @var array
- */
- public $helpers = ['Html'];
- /**
- * Constructor
- *
- */
- public function __construct($View = null, $config = []) {
- $config += $this->_defaultConfig;
- // Default the secure option to match the current URL.
- $config['secure'] = env('HTTPS');
- parent::__construct($View, $config);
- }
- /**
- * Shows gravatar for the supplied email address
- *
- * @param string $email Email address
- * @param array $options Array of options, keyed from default settings
- * @return string Gravatar image string
- */
- public function image($email, $options = []) {
- $imageUrl = $this->imageUrl($email, $options);
- unset($options['default'], $options['size'], $options['rating'], $options['ext']);
- return $this->Html->image($imageUrl, $options);
- }
- /**
- * Generates image URL
- *
- * @param string $email Email address
- * @param string $options Array of options, keyed from default settings
- * @return string Gravatar Image URL
- */
- public function imageUrl($email, $options = []) {
- $options = $this->_cleanOptions($options + $this->settings);
- $ext = $options['ext'];
- $secure = $options['secure'];
- unset($options['ext'], $options['secure']);
- $protocol = $secure === true ? 'https' : 'http';
- $imageUrl = $this->_url[$protocol] . md5($email);
- if ($ext === true) {
- // If 'ext' option is supplied and true, append an extension to the generated image URL.
- // This helps systems that don't display images unless they have a specific image extension on the URL.
- $imageUrl .= '.jpg';
- }
- $imageUrl .= $this->_buildOptions($options);
- return $imageUrl;
- }
- /**
- * Generate an array of default images for preview purposes
- *
- * @param array $options Array of options, keyed from default settings
- * @return array Default images array
- */
- public function defaultImages($options = []) {
- $options = $this->_cleanOptions($options + $this->settings);
- $images = [];
- foreach ($this->_defaultIcons as $defaultIcon) {
- $options['default'] = $defaultIcon;
- $images[$defaultIcon] = $this->image(null, $options);
- }
- return $images;
- }
- /**
- * Sanitize the options array
- *
- * @param array $options Array of options, keyed from default settings
- * @return array Clean options array
- */
- protected function _cleanOptions($options) {
- if (!isset($options['size']) || empty($options['size']) || !is_numeric($options['size'])) {
- unset($options['size']);
- } else {
- $options['size'] = min(max($options['size'], 1), 512);
- }
- if (!$options['rating'] || !in_array(mb_strtolower($options['rating']), $this->_allowedRatings)) {
- unset($options['rating']);
- }
- if (!$options['default']) {
- unset($options['default']);
- } else {
- App::uses('Validation', 'Utility');
- if (!in_array($options['default'], $this->_defaultIcons) && !Validation::url($options['default'])) {
- unset($options['default']);
- }
- }
- return $options;
- }
- /**
- * Generate email address hash
- *
- * @param string $email Email address
- * @param string $type Hash type to employ
- * @return string Email address hash
- */
- protected function _emailHash($email, $type) {
- return md5(mb_strtolower($email), $type);
- }
- /**
- * Build Options URL string
- *
- * @param array $options Array of options, keyed from default settings
- * @return string URL string of options
- */
- protected function _buildOptions($options = []) {
- $gravatarOptions = array_intersect(array_keys($options), array_keys($this->_defaultConfig));
- if (!empty($gravatarOptions)) {
- $optionArray = [];
- foreach ($gravatarOptions as $key) {
- $value = $options[$key];
- $optionArray[] = $key . '=' . mb_strtolower($value);
- }
- return '?' . implode('&', $optionArray);
- }
- return '';
- }
- }
|