avatarcontroller.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Avatar;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\Http\DataDisplayResponse;
  30. use OCP\IAvatarManager;
  31. use OCP\ICache;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  36. use OCP\Files\Folder;
  37. /**
  38. * Class AvatarController
  39. *
  40. * @package OC\Core\Avatar
  41. */
  42. class AvatarController extends Controller {
  43. /** @var IAvatarManager */
  44. protected $avatarManager;
  45. /** @var \OC\Cache\File */
  46. protected $cache;
  47. /** @var IL10N */
  48. protected $l;
  49. /** @var IUserManager */
  50. protected $userManager;
  51. /** @var IUserSession */
  52. protected $userSession;
  53. /** @var Folder */
  54. protected $userFolder;
  55. /**
  56. * @param string $appName
  57. * @param IRequest $request
  58. * @param IAvatarManager $avatarManager
  59. * @param \OC\Cache\File $cache
  60. * @param IL10N $l10n
  61. * @param IUserManager $userManager
  62. * @param IUserSession $userSession
  63. * @param Folder $userFolder
  64. */
  65. public function __construct($appName,
  66. IRequest $request,
  67. IAvatarManager $avatarManager,
  68. \OC\Cache\File $cache,
  69. IL10N $l10n,
  70. IUserManager $userManager,
  71. IUserSession $userSession,
  72. Folder $userFolder) {
  73. parent::__construct($appName, $request);
  74. $this->avatarManager = $avatarManager;
  75. $this->cache = $cache;
  76. $this->l = $l10n;
  77. $this->userManager = $userManager;
  78. $this->userSession = $userSession;
  79. $this->userFolder = $userFolder;
  80. }
  81. /**
  82. * @NoAdminRequired
  83. * @NoCSRFRequired
  84. *
  85. * @param string $userId
  86. * @param int $size
  87. * @return DataResponse|DataDisplayResponse
  88. */
  89. public function getAvatar($userId, $size) {
  90. if ($size > 2048) {
  91. $size = 2048;
  92. } elseif ($size <= 0) {
  93. $size = 64;
  94. }
  95. $avatar = $this->avatarManager->getAvatar($userId);
  96. $image = $avatar->get($size);
  97. if ($image instanceof \OCP\IImage) {
  98. $resp = new DataDisplayResponse($image->data(),
  99. Http::STATUS_OK,
  100. ['Content-Type' => $image->mimeType()]);
  101. $resp->setETag(crc32($image->data()));
  102. } else {
  103. $user = $this->userManager->get($userId);
  104. $userName = $user ? $user->getDisplayName() : '';
  105. $resp = new DataResponse([
  106. 'data' => [
  107. 'displayname' => $userName,
  108. ],
  109. ]);
  110. }
  111. $resp->addHeader('Pragma', 'public');
  112. $resp->cacheFor(0);
  113. $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
  114. return $resp;
  115. }
  116. /**
  117. * @NoAdminRequired
  118. *
  119. * @param string $path
  120. * @return DataResponse
  121. */
  122. public function postAvatar($path) {
  123. $userId = $this->userSession->getUser()->getUID();
  124. $files = $this->request->getUploadedFile('files');
  125. if (isset($path)) {
  126. $path = stripslashes($path);
  127. $node = $this->userFolder->get($path);
  128. if ($node->getSize() > 20*1024*1024) {
  129. return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]],
  130. Http::STATUS_BAD_REQUEST);
  131. }
  132. $content = $node->getContent();
  133. } elseif (!is_null($files)) {
  134. if (
  135. $files['error'][0] === 0 &&
  136. is_uploaded_file($files['tmp_name'][0]) &&
  137. !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
  138. ) {
  139. if ($files['size'][0] > 20*1024*1024) {
  140. return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]],
  141. Http::STATUS_BAD_REQUEST);
  142. }
  143. $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
  144. $content = $this->cache->get('avatar_upload');
  145. unlink($files['tmp_name'][0]);
  146. } else {
  147. return new DataResponse(['data' => ['message' => $this->l->t('Invalid file provided')]],
  148. Http::STATUS_BAD_REQUEST);
  149. }
  150. } else {
  151. //Add imgfile
  152. return new DataResponse(['data' => ['message' => $this->l->t('No image or file provided')]],
  153. Http::STATUS_BAD_REQUEST);
  154. }
  155. try {
  156. $image = new \OC_Image();
  157. $image->loadFromData($content);
  158. $image->fixOrientation();
  159. if ($image->valid()) {
  160. $mimeType = $image->mimeType();
  161. if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
  162. return new DataResponse(['data' => ['message' => $this->l->t('Unknown filetype')]]);
  163. }
  164. $this->cache->set('tmpAvatar', $image->data(), 7200);
  165. return new DataResponse(['data' => 'notsquare']);
  166. } else {
  167. return new DataResponse(['data' => ['message' => $this->l->t('Invalid image')]]);
  168. }
  169. } catch (\Exception $e) {
  170. return new DataResponse(['data' => ['message' => $e->getMessage()]]);
  171. }
  172. }
  173. /**
  174. * @NoAdminRequired
  175. *
  176. * @return DataResponse
  177. */
  178. public function deleteAvatar() {
  179. $userId = $this->userSession->getUser()->getUID();
  180. try {
  181. $avatar = $this->avatarManager->getAvatar($userId);
  182. $avatar->remove();
  183. return new DataResponse();
  184. } catch (\Exception $e) {
  185. return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_BAD_REQUEST);
  186. }
  187. }
  188. /**
  189. * @NoAdminRequired
  190. *
  191. * @return DataResponse|DataDisplayResponse
  192. */
  193. public function getTmpAvatar() {
  194. $tmpAvatar = $this->cache->get('tmpAvatar');
  195. if (is_null($tmpAvatar)) {
  196. return new DataResponse(['data' => [
  197. 'message' => $this->l->t("No temporary profile picture available, try again")
  198. ]],
  199. Http::STATUS_NOT_FOUND);
  200. }
  201. $image = new \OC_Image($tmpAvatar);
  202. $resp = new DataDisplayResponse($image->data(),
  203. Http::STATUS_OK,
  204. ['Content-Type' => $image->mimeType()]);
  205. $resp->setETag(crc32($image->data()));
  206. $resp->cacheFor(0);
  207. $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
  208. return $resp;
  209. }
  210. /**
  211. * @NoAdminRequired
  212. *
  213. * @param array $crop
  214. * @return DataResponse
  215. */
  216. public function postCroppedAvatar($crop) {
  217. $userId = $this->userSession->getUser()->getUID();
  218. if (is_null($crop)) {
  219. return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
  220. Http::STATUS_BAD_REQUEST);
  221. }
  222. if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
  223. return new DataResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
  224. Http::STATUS_BAD_REQUEST);
  225. }
  226. $tmpAvatar = $this->cache->get('tmpAvatar');
  227. if (is_null($tmpAvatar)) {
  228. return new DataResponse(['data' => [
  229. 'message' => $this->l->t("No temporary profile picture available, try again")
  230. ]],
  231. Http::STATUS_BAD_REQUEST);
  232. }
  233. $image = new \OC_Image($tmpAvatar);
  234. $image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h']));
  235. try {
  236. $avatar = $this->avatarManager->getAvatar($userId);
  237. $avatar->set($image);
  238. // Clean up
  239. $this->cache->remove('tmpAvatar');
  240. return new DataResponse(['status' => 'success']);
  241. } catch (\OC\NotSquareException $e) {
  242. return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
  243. Http::STATUS_BAD_REQUEST);
  244. }catch (\Exception $e) {
  245. return new DataResponse(['data' => ['message' => $e->getMessage()]],
  246. Http::STATUS_BAD_REQUEST);
  247. }
  248. }
  249. }