Avatar.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC;
  29. use OC\User\User;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\NotPermittedException;
  32. use OCP\Files\SimpleFS\ISimpleFile;
  33. use OCP\Files\SimpleFS\ISimpleFolder;
  34. use OCP\IAvatar;
  35. use OCP\IConfig;
  36. use OCP\IImage;
  37. use OCP\IL10N;
  38. use OC_Image;
  39. use OCP\ILogger;
  40. /**
  41. * This class gets and sets users avatars.
  42. */
  43. class Avatar implements IAvatar {
  44. /** @var ISimpleFolder */
  45. private $folder;
  46. /** @var IL10N */
  47. private $l;
  48. /** @var User */
  49. private $user;
  50. /** @var ILogger */
  51. private $logger;
  52. /** @var IConfig */
  53. private $config;
  54. /**
  55. * constructor
  56. *
  57. * @param ISimpleFolder $folder The folder where the avatars are
  58. * @param IL10N $l
  59. * @param User $user
  60. * @param ILogger $logger
  61. * @param IConfig $config
  62. */
  63. public function __construct(ISimpleFolder $folder,
  64. IL10N $l,
  65. $user,
  66. ILogger $logger,
  67. IConfig $config) {
  68. $this->folder = $folder;
  69. $this->l = $l;
  70. $this->user = $user;
  71. $this->logger = $logger;
  72. $this->config = $config;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function get ($size = 64) {
  78. try {
  79. $file = $this->getFile($size);
  80. } catch (NotFoundException $e) {
  81. return false;
  82. }
  83. $avatar = new OC_Image();
  84. $avatar->loadFromData($file->getContent());
  85. return $avatar;
  86. }
  87. /**
  88. * Check if an avatar exists for the user
  89. *
  90. * @return bool
  91. */
  92. public function exists() {
  93. return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png');
  94. }
  95. /**
  96. * sets the users avatar
  97. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  98. * @throws \Exception if the provided file is not a jpg or png image
  99. * @throws \Exception if the provided image is not valid
  100. * @throws NotSquareException if the image is not square
  101. * @return void
  102. */
  103. public function set ($data) {
  104. if($data instanceOf IImage) {
  105. $img = $data;
  106. $data = $img->data();
  107. } else {
  108. $img = new OC_Image($data);
  109. }
  110. $type = substr($img->mimeType(), -3);
  111. if ($type === 'peg') {
  112. $type = 'jpg';
  113. }
  114. if ($type !== 'jpg' && $type !== 'png') {
  115. throw new \Exception($this->l->t("Unknown filetype"));
  116. }
  117. if (!$img->valid()) {
  118. throw new \Exception($this->l->t("Invalid image"));
  119. }
  120. if (!($img->height() === $img->width())) {
  121. throw new NotSquareException($this->l->t("Avatar image is not square"));
  122. }
  123. $this->remove();
  124. $file = $this->folder->newFile('avatar.'.$type);
  125. $file->putContent($data);
  126. $this->user->triggerChange('avatar', $file);
  127. }
  128. /**
  129. * remove the users avatar
  130. * @return void
  131. */
  132. public function remove () {
  133. $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
  134. $avatars = $this->folder->getDirectoryListing();
  135. $this->config->setUserValue($this->user->getUID(), 'avatar', 'version',
  136. (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1);
  137. foreach ($avatars as $avatar) {
  138. if (preg_match($regex, $avatar->getName())) {
  139. $avatar->delete();
  140. }
  141. }
  142. $this->user->triggerChange('avatar', '');
  143. }
  144. /**
  145. * @inheritdoc
  146. */
  147. public function getFile($size) {
  148. $ext = $this->getExtension();
  149. if ($size === -1) {
  150. $path = 'avatar.' . $ext;
  151. } else {
  152. $path = 'avatar.' . $size . '.' . $ext;
  153. }
  154. try {
  155. $file = $this->folder->getFile($path);
  156. } catch (NotFoundException $e) {
  157. if ($size <= 0) {
  158. throw new NotFoundException;
  159. }
  160. $avatar = new OC_Image();
  161. /** @var ISimpleFile $file */
  162. $file = $this->folder->getFile('avatar.' . $ext);
  163. $avatar->loadFromData($file->getContent());
  164. if ($size !== -1) {
  165. $avatar->resize($size);
  166. }
  167. try {
  168. $file = $this->folder->newFile($path);
  169. $file->putContent($avatar->data());
  170. } catch (NotPermittedException $e) {
  171. $this->logger->error('Failed to save avatar for ' . $this->user->getUID());
  172. }
  173. }
  174. return $file;
  175. }
  176. /**
  177. * Get the extension of the avatar. If there is no avatar throw Exception
  178. *
  179. * @return string
  180. * @throws NotFoundException
  181. */
  182. private function getExtension() {
  183. if ($this->folder->fileExists('avatar.jpg')) {
  184. return 'jpg';
  185. } elseif ($this->folder->fileExists('avatar.png')) {
  186. return 'png';
  187. }
  188. throw new NotFoundException;
  189. }
  190. }