GravatarHelper.php 5.2 KB

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