Avatar.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. if (!defined('GNUSOCIAL')) { exit(1); }
  3. /**
  4. * Table Definition for avatar
  5. */
  6. class Avatar extends Managed_DataObject
  7. {
  8. public $__table = 'avatar'; // table name
  9. public $profile_id; // int(4) primary_key not_null
  10. public $original; // tinyint(1)
  11. public $width; // int(4) primary_key not_null
  12. public $height; // int(4) primary_key not_null
  13. public $mediatype; // varchar(32) not_null
  14. public $filename; // varchar(191) not 255 because utf8mb4 takes more space
  15. public $created; // datetime() not_null
  16. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  17. public static function schemaDef()
  18. {
  19. return array(
  20. 'fields' => array(
  21. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  22. 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
  23. 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
  24. 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
  25. 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
  26. 'filename' => array('type' => 'varchar', 'length' => 191, 'description' => 'local filename, if local'),
  27. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  28. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  29. ),
  30. 'primary key' => array('profile_id', 'width', 'height'),
  31. 'unique keys' => array(
  32. // 'avatar_filename_key' => array('filename'),
  33. ),
  34. 'foreign keys' => array(
  35. 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  36. ),
  37. 'indexes' => array(
  38. 'avatar_profile_id_idx' => array('profile_id'),
  39. ),
  40. );
  41. }
  42. // We clean up the file, too
  43. function delete($useWhere=false)
  44. {
  45. $filename = $this->filename;
  46. if (file_exists(Avatar::path($filename))) {
  47. @unlink(Avatar::path($filename));
  48. }
  49. return parent::delete($useWhere);
  50. }
  51. /*
  52. * Deletes all avatars (but may spare the original) from a profile.
  53. *
  54. * @param Profile $target The profile we're deleting avatars of.
  55. * @param boolean $original Whether original should be removed or not.
  56. */
  57. public static function deleteFromProfile(Profile $target, $original=true) {
  58. try {
  59. $avatars = self::getProfileAvatars($target);
  60. foreach ($avatars as $avatar) {
  61. if ($avatar->original && !$original) {
  62. continue;
  63. }
  64. $avatar->delete();
  65. }
  66. } catch (NoAvatarException $e) {
  67. // There are no avatars to delete, a sort of success.
  68. }
  69. return true;
  70. }
  71. static protected $_avatars = array();
  72. /*
  73. * Get an avatar by profile. Currently can't call newSize with $height
  74. */
  75. public static function byProfile(Profile $target, $width=null, $height=null)
  76. {
  77. $width = intval($width);
  78. $height = !is_null($height) ? intval($height) : null;
  79. if (is_null($height)) {
  80. $height = $width;
  81. }
  82. $size = "{$width}x{$height}";
  83. if (!isset(self::$_avatars[$target->id])) {
  84. self::$_avatars[$target->id] = array();
  85. } elseif (isset(self::$_avatars[$target->id][$size])){
  86. return self::$_avatars[$target->id][$size];
  87. }
  88. $avatar = null;
  89. if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
  90. $avatar = self::pkeyGet(
  91. array(
  92. 'profile_id' => $target->id,
  93. 'width' => $width,
  94. 'height' => $height,
  95. )
  96. );
  97. Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
  98. }
  99. if (is_null($avatar)) {
  100. // Obviously we can't find an avatar, so let's resize the original!
  101. $avatar = Avatar::newSize($target, $width);
  102. } elseif (!($avatar instanceof Avatar)) {
  103. throw new NoAvatarException($target, $avatar);
  104. }
  105. self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
  106. return $avatar;
  107. }
  108. public static function getUploaded(Profile $target)
  109. {
  110. $avatar = new Avatar();
  111. $avatar->profile_id = $target->id;
  112. $avatar->original = true;
  113. if (!$avatar->find(true)) {
  114. throw new NoAvatarException($target, $avatar);
  115. }
  116. if (!file_exists(Avatar::path($avatar->filename))) {
  117. // The delete call may be odd for, say, unmounted filesystems
  118. // that cause a file to currently not exist, but actually it does...
  119. $avatar->delete();
  120. throw new NoAvatarException($target, $avatar);
  121. }
  122. return $avatar;
  123. }
  124. public static function getProfileAvatars(Profile $target) {
  125. $avatar = new Avatar();
  126. $avatar->profile_id = $target->id;
  127. if (!$avatar->find()) {
  128. throw new NoAvatarException($target, $avatar);
  129. }
  130. return $avatar->fetchAll();
  131. }
  132. /**
  133. * Where should the avatar go for this user?
  134. */
  135. static function filename($id, $extension, $size=null, $extra=null)
  136. {
  137. if ($size) {
  138. return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
  139. } else {
  140. return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
  141. }
  142. }
  143. static function path($filename)
  144. {
  145. $dir = common_config('avatar', 'dir');
  146. if ($dir[strlen($dir)-1] != '/') {
  147. $dir .= '/';
  148. }
  149. return $dir . $filename;
  150. }
  151. static function url($filename)
  152. {
  153. $path = common_config('avatar', 'path');
  154. if ($path[strlen($path)-1] != '/') {
  155. $path .= '/';
  156. }
  157. if ($path[0] != '/') {
  158. $path = '/'.$path;
  159. }
  160. $server = common_config('avatar', 'server');
  161. if (empty($server)) {
  162. $server = common_config('site', 'server');
  163. }
  164. $ssl = (common_config('avatar', 'ssl') || GNUsocial::useHTTPS());
  165. $protocol = ($ssl) ? 'https' : 'http';
  166. return $protocol.'://'.$server.$path.$filename;
  167. }
  168. function displayUrl()
  169. {
  170. return Avatar::url($this->filename);
  171. }
  172. static function urlByProfile(Profile $target, $width=null, $height=null) {
  173. try {
  174. return self::byProfile($target, $width, $height)->displayUrl();
  175. } catch (Exception $e) {
  176. return self::defaultImage($width);
  177. }
  178. }
  179. static function defaultImage($size=null)
  180. {
  181. if (is_null($size)) {
  182. $size = AVATAR_PROFILE_SIZE;
  183. }
  184. static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
  185. AVATAR_STREAM_SIZE => 'stream',
  186. AVATAR_MINI_SIZE => 'mini');
  187. return Theme::path('default-avatar-'.$sizenames[$size].'.png');
  188. }
  189. static function newSize(Profile $target, $width) {
  190. $width = intval($width);
  191. if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
  192. // TRANS: An error message when avatar size is unreasonable
  193. throw new Exception(_m('Avatar size too large'));
  194. }
  195. // So far we only have square avatars and I don't have time to
  196. // rewrite support for non-square ones right now ;)
  197. $height = $width;
  198. $original = Avatar::getUploaded($target);
  199. $imagefile = new ImageFile(null, Avatar::path($original->filename));
  200. $filename = Avatar::filename($target->getID(), image_type_to_extension($imagefile->preferredType()),
  201. $width, common_timestamp());
  202. $imagefile->resizeTo(Avatar::path($filename), array('width'=>$width, 'height'=>$height));
  203. $scaled = clone($original);
  204. $scaled->original = false;
  205. $scaled->width = $width;
  206. $scaled->height = $height;
  207. $scaled->filename = $filename;
  208. $scaled->created = common_sql_now();
  209. if (!$scaled->insert()) {
  210. // TRANS: An error message when unable to insert avatar data into the db
  211. throw new Exception(_m('Could not insert new avatar data to database'));
  212. }
  213. // Return the new avatar object
  214. return $scaled;
  215. }
  216. }