GravatarHelper.php 4.8 KB

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