PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/kohana/gravatar.php

https://github.com/mintbridge/kohana-gravatar
PHP | 282 lines | 147 code | 29 blank | 106 comment | 13 complexity | a8522c4aee2b5cfb66d5e6654ed1d648 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * [Gravatar's](http://en.gravatar.com) are universal avatars available to all web sites and services.
  4. * Users must register their email addresses with Gravatar before their avatars will be
  5. * usable with this module. Users with gravatars can have a default image of your selection.
  6. *
  7. * @see http://en.gravatar.com
  8. * @package Kohana
  9. * @category Gravatar
  10. * @version 3.1.0
  11. * @author Kohana Team
  12. * @copyright (c) 2009-2010 Kohana Team
  13. * @license http://kohanaphp.com/license
  14. */
  15. class Kohana_Gravatar {
  16. const GRAVATAR_G = 'G';
  17. const GRAVATAR_PG = 'PG';
  18. const GRAVATAR_R = 'R';
  19. const GRAVATAR_X = 'X';
  20. /**
  21. * Static instances
  22. *
  23. * @var array
  24. * @static
  25. * @access protected
  26. */
  27. static protected $_instances = array();
  28. /**
  29. * Instance constructor pattern
  30. *
  31. * @param string email the Gravatar to fetch for email address
  32. * @param string config the name of the configuration grouping
  33. * @param array config array of key value configuration pairs
  34. * @return Gravatar
  35. * @access public
  36. * @static
  37. */
  38. public static function instance($email, $config = NULL)
  39. {
  40. // Create an instance checksum
  41. $config_checksum = sha1(serialize($config));
  42. // Load the Gravatar instance for email and configuration
  43. if ( ! isset(self::$_instances[$email][$config_checksum]))
  44. {
  45. self::$_instances[$email][$config_checksum] = new Gravatar($email, $config);
  46. }
  47. // Return a the instance
  48. return self::$_instances[$email][$config_checksum];
  49. }
  50. /**
  51. * Configuration for this library, merged with the static config
  52. *
  53. * @var array
  54. * @access protected
  55. */
  56. protected $_config;
  57. /**
  58. * Additional attributes to add to the image
  59. *
  60. * @var array
  61. */
  62. public $attributes = array();
  63. /**
  64. * The email address of the user
  65. *
  66. * @var string
  67. */
  68. public $email;
  69. /**
  70. * Gravatar constructor
  71. *
  72. * @param string email the Gravatar to fetch for email address
  73. * @param string config the name of the configuration grouping
  74. * @param array config array of key value configuration pairs
  75. * @access public
  76. * @throws Kohana_Gravatar_Exception
  77. */
  78. protected function __construct($email, $config = NULL)
  79. {
  80. // Set the email address
  81. $this->email = $email;
  82. if (empty($config))
  83. {
  84. $this->_config = Kohana::config('gravatar.default');
  85. }
  86. elseif (is_array($config))
  87. {
  88. // Setup the configuration
  89. $config += Kohana::config('gravatar.default');
  90. $this->_config = $config;
  91. }
  92. elseif (is_string($config))
  93. {
  94. if ($config = Kohana::config('gravatar.'.$config) === NULL)
  95. {
  96. throw new Kohana_Gravatar_Exception('Gravatar.__construct() , Invalid configuration group name : :config', array(':config' => $config));
  97. }
  98. $this->_config = $config + Kohana::config('gravatar.default');
  99. }
  100. }
  101. /**
  102. * Handles this object being cast to string
  103. *
  104. * @return string the resulting Gravatar
  105. * @access public
  106. * @author Sam Clark
  107. */
  108. public function __toString()
  109. {
  110. return (string) $this->render();
  111. }
  112. /**
  113. * Accessor method for setting size of gravatar
  114. *
  115. * @param int size the size of the gravatar image in pixels
  116. * @return self
  117. */
  118. public function size($size = NULL)
  119. {
  120. if ($size === NULL)
  121. {
  122. return $this->_config['size'];
  123. }
  124. else
  125. {
  126. $this->_config['size'] = (int) $size;
  127. return $this;
  128. }
  129. }
  130. /**
  131. * Accessor method for the rating of the gravatar
  132. *
  133. * @param string rating the rating of the gravatar
  134. * @return self
  135. * @throws Kohana_Gravatar_Exception
  136. */
  137. public function rating($rating = NULL)
  138. {
  139. $rating = strtoupper($rating);
  140. if ($rating === NULL)
  141. {
  142. return $this->_config['rating'];
  143. }
  144. else
  145. {
  146. if (in_array($rating, array(Gravatar::GRAVATAR_G, Gravatar::GRAVATAR_PG, Gravatar::GRAVATAR_R, Gravatar::GRAVATAR_X)))
  147. {
  148. $this->_config['rating'] = $rating;
  149. }
  150. else
  151. {
  152. throw new Kohana_Gravatar_Exception('The rating value :rating is not valid. Please use G, PG, R or X. Also available through Class constants', array(':rating' => $rating));
  153. }
  154. }
  155. return $this;
  156. }
  157. /**
  158. * Accessor method for setting the default image if the supplied email address or rating return an empty result
  159. *
  160. * @param string url the url of the image to use instead of the Gravatar
  161. * @return self
  162. */
  163. public function default_image($url = NULL)
  164. {
  165. if ($url === NULL)
  166. {
  167. return $this->_config['default'];
  168. }
  169. else
  170. {
  171. if (validate::url($url))
  172. {
  173. $this->_config['default'] = $url;
  174. }
  175. else
  176. {
  177. throw new Gravatar('The url : :url is improperly formatted', array(':url' => $url));
  178. }
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Renders the Gravatar using supplied configuration and attributes. Can use custom view.
  184. *
  185. * @param string view [Optional] a kohana PHP
  186. * @param string email [Optional] the valid email of a Gravatar user
  187. * @return string the rendered Gravatar output
  188. * @access public
  189. */
  190. public function render($view = FALSE, $email = NULL)
  191. {
  192. if ($email !== NULL)
  193. {
  194. $this->email = $email;
  195. }
  196. $data = array('attr' => array(), 'src' => $this->_generate_url());
  197. if ($this->attributes)
  198. {
  199. $data['attr'] += $this->attributes;
  200. }
  201. $data['attr']['alt'] = $this->_process_alt();
  202. if ( ! $view)
  203. {
  204. return new View($this->_config['view'], $data);
  205. }
  206. else
  207. {
  208. return new View($view, $data);
  209. }
  210. }
  211. /**
  212. * Process the alt attribute output
  213. *
  214. * @return string
  215. * @access protected
  216. */
  217. protected function _process_alt()
  218. {
  219. $keys = array
  220. (
  221. '{$email}' => $this->email,
  222. '{$size}' => $this->_config['size'],
  223. '{$rating}' => $this->_config['rating'],
  224. );
  225. if ($this->_config['alt'])
  226. {
  227. $alt = strtr($this->_config['alt'], $keys);
  228. }
  229. else
  230. {
  231. $alt = FALSE;
  232. }
  233. return $alt;
  234. }
  235. /**
  236. * Creates the Gravatar URL based on the configuration and email
  237. *
  238. * @return string the resulting Gravatar URL
  239. * @access protected
  240. */
  241. protected function _generate_url()
  242. {
  243. $string = $this->_config['service'].
  244. '?gravatar_id='.md5($this->email).
  245. '&s='.$this->_config['size'].
  246. '&r='.$this->_config['rating'];
  247. if ( ! empty($this->_config['default']))
  248. {
  249. $string .= '&d='.$this->_config['default'];
  250. }
  251. return $string;
  252. }
  253. }