GravatarHelper.php 5.1 KB

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