PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/View/Helper/GravatarHelper.php

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