PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_zoo/classes/commentauthor.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 268 lines | 95 code | 36 blank | 137 comment | 15 complexity | b73c98586dafeba15c4f26263345ba52 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * Class that represents an Author of a comment
  10. *
  11. * @package Component.Classes
  12. */
  13. class CommentAuthor {
  14. /**
  15. * A reference to the global App object
  16. *
  17. * @var App
  18. * @since 2.0
  19. */
  20. public $app;
  21. /**
  22. *
  23. * The referenced application
  24. *
  25. * @var Application
  26. */
  27. public $application;
  28. /**
  29. * The name of the Author
  30. *
  31. * @var string
  32. * @since 2.0
  33. */
  34. public $name;
  35. /**
  36. * The email of the author
  37. *
  38. * @var string
  39. * @since 2.0
  40. */
  41. public $email;
  42. /**
  43. * The url of the author
  44. *
  45. * @var string
  46. * @since 2.0
  47. */
  48. public $url;
  49. /**
  50. * The id of the user that made the comment
  51. *
  52. * @var int
  53. * @since 2.0
  54. */
  55. public $user_id;
  56. /**
  57. * Class Constructor
  58. *
  59. * @param string $name The name of the author
  60. * @param string $email The email of the author
  61. * @param string $url The url of the author
  62. * @param string $user_id The id of the user linked to the author
  63. */
  64. public function __construct($name = '', $email = '', $url = '', $user_id = '', $application = null) {
  65. // set vars
  66. $this->name = $name;
  67. $this->email = $email;
  68. $this->url = $url;
  69. $this->user_id = $user_id;
  70. $this->application = $application;
  71. }
  72. /**
  73. * Get a user avatar from Gravatar or use the default avatar
  74. *
  75. * @param int $size Size in pixels of the avatar (squared). Default: 32
  76. *
  77. * @return string The html tag img showing the avatar+
  78. *
  79. * @since 2.0
  80. */
  81. public function getAvatar($size = 32) {
  82. $default = JURI::root().'media/zoo/assets/images/avatar.png';
  83. if ($this->email) {
  84. return '<img title="'.$this->name.'" src="http://www.gravatar.com/avatar/'.md5($this->app->string->strtolower($this->email)).'?s='.$size.'&amp;d='.$default.'" height="'.$size.'" width="'.$size.'" alt="'.$this->name.'" />';
  85. } else {
  86. return '<img title="'.$this->name.'" src="'.$default.'" height="'.$size.'" width="'.$size.'" alt="'.$this->name.'" />';
  87. }
  88. }
  89. /**
  90. * Check the if the author is a guest
  91. *
  92. * @return boolean True if the user is not logged in
  93. *
  94. * @since 2.0
  95. */
  96. public function isGuest() {
  97. return empty($this->user_id);
  98. }
  99. /**
  100. * Check if the author is a joomla admin
  101. *
  102. * @return boolean True if the user is an admin
  103. *
  104. * @since 2.0
  105. */
  106. public function isJoomlaAdmin() {
  107. return false;
  108. }
  109. /**
  110. * Get the user type
  111. *
  112. * @return string The type of the user
  113. *
  114. * @since 2.0
  115. */
  116. public function getUserType() {
  117. return strtolower(str_replace('CommentAuthor', '', get_class($this)));
  118. }
  119. }
  120. /**
  121. * Class representing an auhtor which is a Joomla User
  122. *
  123. * @package Component.Classes
  124. */
  125. class CommentAuthorJoomla extends CommentAuthor {
  126. /**
  127. * Get the related JUser object
  128. *
  129. * @return JUser The joomla user
  130. *
  131. * @since 2.0
  132. */
  133. public function getJoomlaUser() {
  134. return $this->app->user->get($this->user_id);
  135. }
  136. /**
  137. * Check if the joomla user is an admin
  138. *
  139. * @return boolean If the user is an admin
  140. *
  141. * @since 2.0
  142. */
  143. public function isJoomlaAdmin() {
  144. if ($user = $this->getJoomlaUser()) {
  145. return $this->app->user->isJoomlaAdmin($user);
  146. }
  147. return false;
  148. }
  149. }
  150. /**
  151. * Class that represents an author that has logged in through facebook
  152. *
  153. * @package Component.Classes
  154. */
  155. class CommentAuthorFacebook extends CommentAuthor {
  156. /**
  157. * Get the avatar from facebook
  158. *
  159. * @param integer $size The size in pixels (squared). Default: 32
  160. *
  161. * @return string The html img tag with the avatar
  162. *
  163. * @since 2.0
  164. */
  165. public function getAvatar($size = 32) {
  166. if ($this->user_id) {
  167. $cache = $this->app->cache->create($this->app->path->path('cache:') . '/author_cache', true, 604800);
  168. $cache_check = ($cache) ? $cache->check() : false;
  169. $url = '';
  170. // try to get avatar url from cache
  171. if ($cache_check) {
  172. $url = $cache->get($this->user_id);
  173. }
  174. // if url is empty, try to get avatar url from twitter
  175. if (empty($url)) {
  176. $info = $this->app->facebook->fields($this->user_id, array('picture'), $this->application);
  177. if (isset($info['picture'])) {
  178. $url = $info['picture'];
  179. }
  180. if ($cache_check) {
  181. $cache->set($this->user_id, $url);
  182. $cache->save();
  183. }
  184. }
  185. if (!empty($url)) {
  186. return '<img alt="'.$this->name.'" title="'.$this->name.'" src="'.$url.'" height="'.$size.'" width="'.$size.'" />';
  187. }
  188. }
  189. return parent::getAvatar($size);
  190. }
  191. }
  192. /**
  193. * Class that represents an author that has logged in using Twitter
  194. *
  195. * @package Component.Classes
  196. */
  197. class CommentAuthorTwitter extends CommentAuthor {
  198. /**
  199. * Get the avatar from twitter
  200. *
  201. * @param integer $size The size of the avatar (squared). Default: 32
  202. *
  203. * @return string The html img tag
  204. *
  205. * @since 2.0
  206. */
  207. public function getAvatar($size = 32) {
  208. if ($this->user_id) {
  209. $cache = $this->app->cache->create($this->app->path->path('cache:') . '/author_cache', true, 604800);
  210. $cache_check = ($cache) ? $cache->check() : false;
  211. $url = '';
  212. // try to get avatar url from cache
  213. if ($cache_check) {
  214. $url = $cache->get($this->user_id);
  215. }
  216. // if url is empty, try to get avatar url from twitter
  217. if (empty($url)) {
  218. $info = $this->app->twitter->fields($this->user_id, array('profile_image_url'), $this->application);
  219. if (isset($info['profile_image_url'])) {
  220. $url = $info['profile_image_url'];
  221. }
  222. if ($cache_check) {
  223. $cache->set($this->user_id, $url)->save();
  224. }
  225. }
  226. if (!empty($url)) {
  227. return '<img alt="'.$this->name.'" title="'.$this->name.'" src="'.$url.'" height="'.$size.'" width="'.$size.'" />';
  228. }
  229. }
  230. return parent::getAvatar($size);
  231. }
  232. }