GravatarHelper.php 4.6 KB

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