GravatarHelper.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * GravatarHelper::url()
  78. *
  79. * @param mixed $email
  80. * @param boolean $options
  81. * @return void
  82. * @deprecated Use imageUrl() instead.
  83. */
  84. public function url($email = null, $options = false) {
  85. if ($options === false) {
  86. $options = array();
  87. }
  88. $this->imageUrl($email, $options);
  89. }
  90. /**
  91. * Generate image URL
  92. * TODO: rename to avoid E_STRICT errors here
  93. *
  94. * @param string $email Email address
  95. * @param string $options Array of options, keyed from default settings
  96. * @return string Gravatar Image URL
  97. */
  98. public function imageUrl($email, $options = array()) {
  99. $options = $this->_cleanOptions(array_merge($this->_default, $options));
  100. $ext = $options['ext'];
  101. $secure = $options['secure'];
  102. unset($options['ext'], $options['secure']);
  103. $protocol = $secure === true ? 'https' : 'http';
  104. $imageUrl = $this->_url[$protocol] . md5($email);
  105. if ($ext === true) {
  106. // If 'ext' option is supplied and true, append an extension to the generated image URL.
  107. // This helps systems that don't display images unless they have a specific image extension on the URL.
  108. $imageUrl .= '.jpg';
  109. }
  110. $imageUrl .= $this->_buildOptions($options);
  111. return $imageUrl;
  112. }
  113. /**
  114. * Generate an array of default images for preview purposes
  115. *
  116. * @param array $options Array of options, keyed from default settings
  117. * @return array Default images array
  118. */
  119. public function defaultImages($options = array()) {
  120. $options = $this->_cleanOptions(array_merge($this->_default, $options));
  121. $images = array();
  122. foreach ($this->_defaultIcons as $defaultIcon) {
  123. $options['default'] = $defaultIcon;
  124. $images[$defaultIcon] = $this->image(null, $options);
  125. }
  126. return $images;
  127. }
  128. /**
  129. * Sanitize the options array
  130. *
  131. * @param array $options Array of options, keyed from default settings
  132. * @return array Clean options array
  133. */
  134. protected function _cleanOptions($options) {
  135. if (!isset($options['size']) || empty($options['size']) || !is_numeric($options['size'])) {
  136. unset($options['size']);
  137. } else {
  138. $options['size'] = min(max($options['size'], 1), 512);
  139. }
  140. if (!$options['rating'] || !in_array(mb_strtolower($options['rating']), $this->_allowedRatings)) {
  141. unset($options['rating']);
  142. }
  143. if (!$options['default']) {
  144. unset($options['default']);
  145. } else {
  146. App::uses('Validation', 'Utility');
  147. if (!in_array($options['default'], $this->_defaultIcons) && !Validation::url($options['default'])) {
  148. unset($options['default']);
  149. }
  150. }
  151. return $options;
  152. }
  153. /**
  154. * Generate email address hash
  155. *
  156. * @param string $email Email address
  157. * @param string $type Hash type to employ
  158. * @return string Email address hash
  159. */
  160. protected function _emailHash($email, $type) {
  161. return md5(mb_strtolower($email), $type);
  162. }
  163. /**
  164. * Build Options URL string
  165. *
  166. * @param array $options Array of options, keyed from default settings
  167. * @return string URL string of options
  168. */
  169. protected function _buildOptions($options = array()) {
  170. $gravatarOptions = array_intersect(array_keys($options), array_keys($this->_default));
  171. if (!empty($gravatarOptions)) {
  172. $optionArray = array();
  173. foreach ($gravatarOptions as $key) {
  174. $value = $options[$key];
  175. $optionArray[] = $key . '=' . mb_strtolower($value);
  176. }
  177. return '?' . implode('&amp;', $optionArray);
  178. }
  179. return '';
  180. }
  181. }