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