GravatarHelper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Show 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. * Generate image URL
  77. * TODO: rename to avoid E_STRICT errors here
  78. *
  79. * @param string $email Email address
  80. * @param string $options Array of options, keyed from default settings
  81. * @return string Gravatar Image URL
  82. */
  83. public function imageUrl($email, $options = []) {
  84. $options = $this->_cleanOptions($options + $this->settings);
  85. $ext = $options['ext'];
  86. $secure = $options['secure'];
  87. unset($options['ext'], $options['secure']);
  88. $protocol = $secure === true ? 'https' : 'http';
  89. $imageUrl = $this->_url[$protocol] . md5($email);
  90. if ($ext === true) {
  91. // If 'ext' option is supplied and true, append an extension to the generated image URL.
  92. // This helps systems that don't display images unless they have a specific image extension on the URL.
  93. $imageUrl .= '.jpg';
  94. }
  95. $imageUrl .= $this->_buildOptions($options);
  96. return $imageUrl;
  97. }
  98. /**
  99. * Generate an array of default images for preview purposes
  100. *
  101. * @param array $options Array of options, keyed from default settings
  102. * @return array Default images array
  103. */
  104. public function defaultImages($options = []) {
  105. $options = $this->_cleanOptions($options + $this->settings);
  106. $images = [];
  107. foreach ($this->_defaultIcons as $defaultIcon) {
  108. $options['default'] = $defaultIcon;
  109. $images[$defaultIcon] = $this->image(null, $options);
  110. }
  111. return $images;
  112. }
  113. /**
  114. * Sanitize the options array
  115. *
  116. * @param array $options Array of options, keyed from default settings
  117. * @return array Clean options array
  118. */
  119. protected function _cleanOptions($options) {
  120. if (!isset($options['size']) || empty($options['size']) || !is_numeric($options['size'])) {
  121. unset($options['size']);
  122. } else {
  123. $options['size'] = min(max($options['size'], 1), 512);
  124. }
  125. if (!$options['rating'] || !in_array(mb_strtolower($options['rating']), $this->_allowedRatings)) {
  126. unset($options['rating']);
  127. }
  128. if (!$options['default']) {
  129. unset($options['default']);
  130. } else {
  131. App::uses('Validation', 'Utility');
  132. if (!in_array($options['default'], $this->_defaultIcons) && !Validation::url($options['default'])) {
  133. unset($options['default']);
  134. }
  135. }
  136. return $options;
  137. }
  138. /**
  139. * Generate email address hash
  140. *
  141. * @param string $email Email address
  142. * @param string $type Hash type to employ
  143. * @return string Email address hash
  144. */
  145. protected function _emailHash($email, $type) {
  146. return md5(mb_strtolower($email), $type);
  147. }
  148. /**
  149. * Build Options URL string
  150. *
  151. * @param array $options Array of options, keyed from default settings
  152. * @return string URL string of options
  153. */
  154. protected function _buildOptions($options = []) {
  155. $gravatarOptions = array_intersect(array_keys($options), array_keys($this->_defaultConfig));
  156. if (!empty($gravatarOptions)) {
  157. $optionArray = [];
  158. foreach ($gravatarOptions as $key) {
  159. $value = $options[$key];
  160. $optionArray[] = $key . '=' . mb_strtolower($value);
  161. }
  162. return '?' . implode('&amp;', $optionArray);
  163. }
  164. return '';
  165. }
  166. }