GravatarHelper.php 4.9 KB

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