PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

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