GravatarHelper.php 4.9 KB

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