PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/View/Helper/Gravatar.php

https://bitbucket.org/Ppito/kawaiviewmodel2
PHP | 351 lines | 148 code | 28 blank | 175 comment | 7 complexity | 89bf3d97d6cd326dc0e024fe15674ce9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_View
  9. */
  10. namespace Zend\View\Helper;
  11. use Zend\View\Exception;
  12. /**
  13. * Helper for retrieving avatars from gravatar.com
  14. *
  15. * @package Zend\View
  16. * @subpackage Helper
  17. */
  18. class Gravatar extends AbstractHtmlElement
  19. {
  20. /**
  21. * URL to gravatar service
  22. */
  23. const GRAVATAR_URL = 'http://www.gravatar.com/avatar';
  24. /**
  25. * Secure URL to gravatar service
  26. */
  27. const GRAVATAR_URL_SECURE = 'https://secure.gravatar.com/avatar';
  28. /**
  29. * Gravatar rating
  30. */
  31. const RATING_G = 'g';
  32. const RATING_PG = 'pg';
  33. const RATING_R = 'r';
  34. const RATING_X = 'x';
  35. /**
  36. * Default gravatar image value constants
  37. */
  38. const DEFAULT_404 = '404';
  39. const DEFAULT_MM = 'mm';
  40. const DEFAULT_IDENTICON = 'identicon';
  41. const DEFAULT_MONSTERID = 'monsterid';
  42. const DEFAULT_WAVATAR = 'wavatar';
  43. /**
  44. * Options
  45. *
  46. * @var array
  47. */
  48. protected $options = array(
  49. 'img_size' => 80,
  50. 'default_img' => self::DEFAULT_MM,
  51. 'rating' => self::RATING_G,
  52. 'secure' => null,
  53. );
  54. /**
  55. * Email Address
  56. *
  57. * @var string
  58. */
  59. protected $email;
  60. /**
  61. * Attributes for HTML image tag
  62. *
  63. * @var array
  64. */
  65. protected $attribs;
  66. /**
  67. * Returns an avatar from gravatar's service.
  68. *
  69. * $options may include the following:
  70. * - 'img_size' int height of img to return
  71. * - 'default_img' string img to return if email address has not found
  72. * - 'rating' string rating parameter for avatar
  73. * - 'secure' bool load from the SSL or Non-SSL location
  74. *
  75. * @see http://pl.gravatar.com/site/implement/url
  76. * @see http://pl.gravatar.com/site/implement/url More information about gravatar's service.
  77. * @param string|null $email Email address.
  78. * @param null|array $options Options
  79. * @param array $attribs Attributes for image tag (title, alt etc.)
  80. * @return Gravatar
  81. */
  82. public function __invoke($email = "", $options = array(), $attribs = array())
  83. {
  84. if (!empty($email)) {
  85. $this->setEmail($email);
  86. }
  87. if (!empty($options)) {
  88. $this->setOptions($options);
  89. }
  90. if (!empty($attribs)) {
  91. $this->setAttribs($attribs);
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Configure state
  97. *
  98. * @param array $options
  99. * @return Gravatar
  100. */
  101. public function setOptions(array $options)
  102. {
  103. foreach ($options as $key => $value) {
  104. $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
  105. if (method_exists($this, $method)) {
  106. $this->{$method}($value);
  107. }
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Get img size
  113. *
  114. * @return int The img size
  115. */
  116. public function getImgSize()
  117. {
  118. return $this->options['img_size'];
  119. }
  120. /**
  121. * Set img size in pixels
  122. *
  123. * @param int $imgSize Size of img must be between 1 and 512
  124. * @return Gravatar
  125. */
  126. public function setImgSize($imgSize)
  127. {
  128. $this->options['img_size'] = (int) $imgSize;
  129. return $this;
  130. }
  131. /**
  132. * Get default img
  133. *
  134. * @return string
  135. */
  136. public function getDefaultImg()
  137. {
  138. return $this->options['default_img'];
  139. }
  140. /**
  141. * Set default img
  142. *
  143. * Can be either an absolute URL to an image, or one of the DEFAULT_* constants
  144. *
  145. * @link http://pl.gravatar.com/site/implement/url More information about default image.
  146. * @param string $defaultImg
  147. * @return Gravatar
  148. */
  149. public function setDefaultImg($defaultImg)
  150. {
  151. $this->options['default_img'] = urlencode($defaultImg);
  152. return $this;
  153. }
  154. /**
  155. * Set rating value
  156. *
  157. * Must be one of the RATING_* constants
  158. *
  159. * @link http://pl.gravatar.com/site/implement/url More information about rating.
  160. * @param string $rating Value for rating. Allowed values are: g, px, r,x
  161. * @return Gravatar
  162. * @throws Exception\DomainException
  163. */
  164. public function setRating($rating)
  165. {
  166. switch ($rating) {
  167. case self::RATING_G:
  168. case self::RATING_PG:
  169. case self::RATING_R:
  170. case self::RATING_X:
  171. $this->options['rating'] = $rating;
  172. break;
  173. default:
  174. throw new Exception\DomainException(sprintf(
  175. 'The rating value "%s" is not allowed',
  176. $rating
  177. ));
  178. }
  179. return $this;
  180. }
  181. /**
  182. * Get rating value
  183. *
  184. * @return string
  185. */
  186. public function getRating()
  187. {
  188. return $this->options['rating'];
  189. }
  190. /**
  191. * Set email address
  192. *
  193. * @param string $email
  194. * @return Gravatar
  195. */
  196. public function setEmail( $email )
  197. {
  198. $this->email = $email;
  199. return $this;
  200. }
  201. /**
  202. * Get email address
  203. *
  204. * @return string
  205. */
  206. public function getEmail()
  207. {
  208. return $this->email;
  209. }
  210. /**
  211. * Load from an SSL or No-SSL location?
  212. *
  213. * @param bool $flag
  214. * @return Gravatar
  215. */
  216. public function setSecure($flag)
  217. {
  218. $this->options['secure'] = ($flag === null) ? null : (bool) $flag;
  219. return $this;
  220. }
  221. /**
  222. * Get an SSL or a No-SSL location
  223. *
  224. * @return bool
  225. */
  226. public function getSecure()
  227. {
  228. if ($this->options['secure'] === null) {
  229. return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  230. }
  231. return $this->options['secure'];
  232. }
  233. /**
  234. * Get attribs of image
  235. *
  236. * Warning!
  237. * If you set src attrib, you get it, but this value will be overwritten in
  238. * protected method setSrcAttribForImg(). And finally your get other src
  239. * value!
  240. *
  241. * @return array
  242. */
  243. public function getAttribs()
  244. {
  245. return $this->attribs;
  246. }
  247. /**
  248. * Set attribs for image tag
  249. *
  250. * Warning! You shouldn't set src attrib for image tag.
  251. * This attrib is overwritten in protected method setSrcAttribForImg().
  252. * This method(_setSrcAttribForImg) is called in public method getImgTag().
  253. *
  254. * @param array $attribs
  255. * @return Gravatar
  256. */
  257. public function setAttribs(array $attribs)
  258. {
  259. $this->attribs = $attribs;
  260. return $this;
  261. }
  262. /**
  263. * Get URL to gravatar's service.
  264. *
  265. * @return string URL
  266. */
  267. protected function getGravatarUrl()
  268. {
  269. return ($this->getSecure() === false) ? self::GRAVATAR_URL : self::GRAVATAR_URL_SECURE;
  270. }
  271. /**
  272. * Get avatar url (including size, rating and default image options)
  273. *
  274. * @return string
  275. */
  276. protected function getAvatarUrl()
  277. {
  278. $src = $this->getGravatarUrl()
  279. . '/' . md5($this->getEmail())
  280. . '?s=' . $this->getImgSize()
  281. . '&d=' . $this->getDefaultImg()
  282. . '&r=' . $this->getRating();
  283. return $src;
  284. }
  285. /**
  286. * Set src attrib for image.
  287. *
  288. * You shouldn't set a own url value!
  289. * It sets value, uses protected method getAvatarUrl.
  290. *
  291. * If already exists, it will be overwritten.
  292. *
  293. * @return void
  294. */
  295. protected function setSrcAttribForImg()
  296. {
  297. $attribs = $this->getAttribs();
  298. $attribs['src'] = $this->getAvatarUrl();
  299. $this->setAttribs($attribs);
  300. }
  301. /**
  302. * Return valid image tag
  303. *
  304. * @return string
  305. */
  306. public function getImgTag()
  307. {
  308. $this->setSrcAttribForImg();
  309. $html = '<img'
  310. . $this->htmlAttribs($this->getAttribs())
  311. . $this->getClosingBracket();
  312. return $html;
  313. }
  314. /**
  315. * Return valid image tag
  316. *
  317. * @return string
  318. */
  319. public function __toString()
  320. {
  321. return $this->getImgTag();
  322. }
  323. }