PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/class.avatar.php

https://gitlab.com/billyprice1/osTicket
PHP | 239 lines | 220 code | 4 blank | 15 comment | 0 complexity | f5768002832f57837f4d30b8c4188ced MD5 | raw file
  1. <?php
  2. /*********************************************************************
  3. class.avatar.php
  4. Avatar sources for users and agents
  5. Jared Hancock <jared@osticket.com>
  6. Peter Rotich <peter@osticket.com>
  7. Copyright (c) 2006-2015 osTicket
  8. http://www.osticket.com
  9. Released under the GNU General Public License WITHOUT ANY WARRANTY.
  10. See LICENSE.TXT for details.
  11. vim: expandtab sw=4 ts=4 sts=4:
  12. **********************************************************************/
  13. abstract class Avatar {
  14. var $user;
  15. function __construct($user) {
  16. $this->user = $user;
  17. }
  18. abstract function getUrl($size);
  19. function getImageTag($size=null) {
  20. $style = ($size)
  21. ? sprintf('style="max-height:%spx"', $size)
  22. : '';
  23. return "<img {$style} class=\"avatar\" alt=\""
  24. .__('Avatar').'" src="'.$this->getUrl($size).'" />';
  25. }
  26. function __toString() {
  27. return $this->getImageTag();
  28. }
  29. function isChangeable() {
  30. return false;
  31. }
  32. function toggle() {}
  33. }
  34. abstract class AvatarSource {
  35. static $id;
  36. static $name;
  37. var $mode;
  38. function __construct($mode=null) {
  39. if (isset($mode))
  40. $this->mode = $mode;
  41. }
  42. function getName() {
  43. return __(static::$name);
  44. }
  45. abstract function getAvatar($user);
  46. static $registry = array();
  47. static function register($class) {
  48. if (!class_exists($class))
  49. throw new Exception($class.': Does not exist');
  50. if (!isset($class::$id))
  51. throw new Exception($class.': AvatarClass must specify $id');
  52. static::$registry[$class::$id] = $class;
  53. }
  54. static function lookup($id, $mode=null) {
  55. $class = static::$registry[$id];
  56. if (!isset($class))
  57. ; // TODO: Return built-in avatar source
  58. if (is_string($class))
  59. $class = static::$registry[$id] = new $class($mode);
  60. return $class;
  61. }
  62. static function allSources() {
  63. return static::$registry;
  64. }
  65. static function getModes() {
  66. return null;
  67. }
  68. }
  69. class LocalAvatarSource
  70. extends AvatarSource {
  71. static $id = 'local';
  72. static $name = /* @trans */ 'Built-In';
  73. var $mode = 'ateam';
  74. static function getModes() {
  75. return array(
  76. 'ateam' => __("Oscar's A-Team"),
  77. );
  78. }
  79. function getAvatar($user) {
  80. return new LocalAvatar($user, $this->mode);
  81. }
  82. }
  83. AvatarSource::register('LocalAvatarSource');
  84. class LocalAvatar
  85. extends Avatar {
  86. var $mode;
  87. var $code;
  88. function __construct($user, $mode) {
  89. parent::__construct($user);
  90. $this->mode = $mode;
  91. }
  92. function getUrl($size) {
  93. $code = $this->code;
  94. if (!$code && method_exists($this->user, 'getExtraAttr'))
  95. $code = $this->user->getExtraAttr('avatar');
  96. if ($code)
  97. $uid = md5($code);
  98. else
  99. // Generate a random string of 0-6 chars for the avatar signature
  100. $uid = md5(strtolower($this->user->getEmail()));
  101. return ROOT_PATH . 'avatar.php?'.Http::build_query(array('uid'=>$uid,
  102. 'mode' => $this->mode));
  103. }
  104. function toggle() {
  105. $this->code = Misc::randCode(21);
  106. return $this->code;
  107. }
  108. function isChangeable() {
  109. return true;
  110. }
  111. }
  112. class RandomAvatar {
  113. var $mode;
  114. static $sprites = array(
  115. 'ateam' => array(
  116. 'file' => 'images/avatar-sprite-ateam.png',
  117. 'grid' => 96,
  118. ),
  119. );
  120. function __construct($mode) {
  121. $this->mode = $mode;
  122. }
  123. function makeAvatar($uid) {
  124. $sprite = self::$sprites[$this->mode];
  125. if (!$sprite || !is_readable(ROOT_DIR . $sprite['file']) || !extension_loaded('gd'))
  126. Http::redirect(ROOT_PATH.'images/mystery-oscar.png');
  127. $source = imagecreatefrompng(ROOT_DIR . $sprite['file']);
  128. $grid = $sprite['grid'];
  129. $avatar = imagecreatetruecolor($grid, $grid);
  130. $width = imagesx($source) / $grid;
  131. $height = imagesy($source) / $grid;
  132. // Start with a white matte
  133. $white = imagecolorallocate($avatar, 255, 255, 255);
  134. imagefill($avatar, 0, 0, $white);
  135. for ($i=0, $k=$height; $i<$k; $i++) {
  136. $idx = hexdec($uid[$i]) % $width;
  137. imagecopy($avatar, $source, 0, 0, $idx*$grid, $i*$grid, $grid, $grid);
  138. }
  139. return $avatar;
  140. }
  141. }
  142. class AvatarsByGravatar
  143. extends AvatarSource {
  144. static $name = 'Gravatar';
  145. static $id = 'gravatar';
  146. var $mode;
  147. function __construct($mode=null) {
  148. $this->mode = $mode ?: 'retro';
  149. }
  150. static function getModes() {
  151. return array(
  152. 'mm' => __('Mystery Man'),
  153. 'identicon' => 'Identicon',
  154. 'monsterid' => 'Monster',
  155. 'wavatar' => 'Wavatar',
  156. 'retro' => 'Retro',
  157. );
  158. }
  159. function getAvatar($user) {
  160. return new Gravatar($user, $this->mode);
  161. }
  162. }
  163. AvatarSource::register('AvatarsByGravatar');
  164. class Gravatar
  165. extends Avatar {
  166. var $email;
  167. var $d;
  168. var $size;
  169. function __construct($user, $imageset) {
  170. $this->email = $user->getEmail();
  171. $this->d = $imageset;
  172. }
  173. function setSize($size) {
  174. $this->size = $size;
  175. }
  176. /**
  177. * Get either a Gravatar URL or complete image tag for a specified email address.
  178. *
  179. * @param string $email The email address
  180. * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
  181. * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
  182. * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
  183. * @param boole $img True to return a complete IMG tag False for just the URL
  184. * @param array $atts Optional, additional key/value attributes to include in the IMG tag
  185. * @return String containing either just a URL or a complete image tag
  186. * @source http://gravatar.com/site/implement/images/php/
  187. */
  188. function getUrl($size=null) {
  189. $size = $this->size ?: 80;
  190. $url = '//www.gravatar.com/avatar/';
  191. $url .= md5( strtolower( $this->email ) );
  192. $url .= "?s=$size&d={$this->d}";
  193. return $url;
  194. }
  195. }