PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/View/Helper/Gravatar.php

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