/classes/kohana/gravatar.php

https://github.com/kemo/kohana-gravatar · PHP · 289 lines · 152 code · 30 blank · 107 comment · 13 complexity · a0b901cf0bd0e4a56997e56e851c1018 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->load('gravatar.default');
  85. }
  86. elseif (is_array($config))
  87. {
  88. // Setup the configuration
  89. $config += Kohana::$config->load('gravatar.default');
  90. $this->_config = $config;
  91. }
  92. elseif (is_string($config))
  93. {
  94. if ($config = Kohana::$config->load('gravatar.'.$config) === NULL)
  95. {
  96. throw new Gravatar_Exception('Gravatar.__construct() , Invalid configuration group name : :config',
  97. array(':config' => $config));
  98. }
  99. $this->_config = $config + Kohana::$config->load('gravatar.default');
  100. }
  101. }
  102. /**
  103. * Handles this object being cast to string
  104. *
  105. * @return string the resulting Gravatar
  106. * @access public
  107. * @author Sam Clark
  108. */
  109. public function __toString()
  110. {
  111. return (string) $this->render();
  112. }
  113. /**
  114. * Accessor method for setting size of gravatar
  115. *
  116. * @param int size the size of the gravatar image in pixels
  117. * @return self
  118. */
  119. public function size($size = NULL)
  120. {
  121. if ($size === NULL)
  122. {
  123. return $this->_config['size'];
  124. }
  125. else
  126. {
  127. $this->_config['size'] = (int) $size;
  128. return $this;
  129. }
  130. }
  131. /**
  132. * Accessor method for the rating of the gravatar
  133. *
  134. * @param string rating the rating of the gravatar
  135. * @return self
  136. * @throws Kohana_Gravatar_Exception
  137. */
  138. public function rating($rating = NULL)
  139. {
  140. $rating = strtoupper($rating);
  141. if ($rating === NULL)
  142. {
  143. return $this->_config['rating'];
  144. }
  145. else
  146. {
  147. if (in_array($rating, array(Gravatar::GRAVATAR_G, Gravatar::GRAVATAR_PG, Gravatar::GRAVATAR_R, Gravatar::GRAVATAR_X)))
  148. {
  149. $this->_config['rating'] = $rating;
  150. }
  151. else
  152. {
  153. throw new Gravatar_Exception('The rating value :rating is not valid. Please use G, PG, R or X. Also available through Class constants',
  154. array(':rating' => $rating));
  155. }
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Accessor method for setting the default image if the supplied email address or rating return an empty result
  161. *
  162. * @param string url the url of the image to use instead of the Gravatar
  163. * @return self
  164. */
  165. public function default_image($url = NULL)
  166. {
  167. if ($url === NULL)
  168. {
  169. return $this->_config['default'];
  170. }
  171. else
  172. {
  173. if (Valid::url($url))
  174. {
  175. $this->_config['default'] = $url;
  176. }
  177. else
  178. {
  179. throw new Gravatar_Exception('The url : :url is improperly formatted',
  180. array(':url' => $url));
  181. }
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Renders the Gravatar using supplied configuration and attributes. Can use custom view.
  187. *
  188. * @param string view [Optional] a kohana PHP
  189. * @param string email [Optional] the valid email of a Gravatar user
  190. * @return string the rendered Gravatar output
  191. * @access public
  192. */
  193. public function render($view = FALSE, $email = NULL)
  194. {
  195. if ($email !== NULL)
  196. {
  197. $this->email = $email;
  198. }
  199. $data = array('attr' => array(), 'src' => $this->url());
  200. if ($this->attributes)
  201. {
  202. $data['attr'] += $this->attributes;
  203. }
  204. $data['attr']['alt'] = $this->_process_alt();
  205. if ( ! $view)
  206. {
  207. return new View($this->_config['view'], $data);
  208. }
  209. else
  210. {
  211. return new View($view, $data);
  212. }
  213. }
  214. /**
  215. * Process the alt attribute output
  216. *
  217. * @return string
  218. * @access protected
  219. */
  220. protected function _process_alt()
  221. {
  222. $keys = array
  223. (
  224. '{$email}' => $this->email,
  225. '{$size}' => $this->_config['size'],
  226. '{$rating}' => $this->_config['rating'],
  227. );
  228. if ($this->_config['alt'])
  229. {
  230. $alt = strtr($this->_config['alt'], $keys);
  231. }
  232. else
  233. {
  234. $alt = FALSE;
  235. }
  236. return $alt;
  237. }
  238. /**
  239. * Creates the Gravatar URL based on the configuration and email
  240. *
  241. * @return string the resulting Gravatar URL
  242. * @access public
  243. */
  244. public function url()
  245. {
  246. $params = array(
  247. 'gravatar_id' => md5($this->email),
  248. 's' => $this->_config['size'],
  249. 'r' => $this->_config['rating'],
  250. );
  251. // Add the default avatar if it exists
  252. if ( ! empty($this->_config['default']))
  253. {
  254. $params['d'] = $this->_config['default'];
  255. }
  256. $url = $this->_config['service'].'?'.http_build_query($params,'','&');
  257. return $url;
  258. }
  259. }