AvatarController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <rullzer@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Controller;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Http\DataDisplayResponse;
  32. use OCP\Files\NotFoundException;
  33. use OCP\IAvatarManager;
  34. use OCP\ILogger;
  35. use OCP\IL10N;
  36. use OCP\IRequest;
  37. use OCP\IUserManager;
  38. use OCP\IUserSession;
  39. use OCP\Files\Folder;
  40. /**
  41. * Class AvatarController
  42. *
  43. * @package OC\Core\Controller
  44. */
  45. class AvatarController extends Controller {
  46. /** @var IAvatarManager */
  47. protected $avatarManager;
  48. /** @var \OC\Cache\File */
  49. protected $cache;
  50. /** @var IL10N */
  51. protected $l;
  52. /** @var IUserManager */
  53. protected $userManager;
  54. /** @var IUserSession */
  55. protected $userSession;
  56. /** @var Folder */
  57. protected $userFolder;
  58. /** @var ILogger */
  59. protected $logger;
  60. /**
  61. * @param string $appName
  62. * @param IRequest $request
  63. * @param IAvatarManager $avatarManager
  64. * @param \OC\Cache\File $cache
  65. * @param IL10N $l10n
  66. * @param IUserManager $userManager
  67. * @param IUserSession $userSession
  68. * @param Folder $userFolder
  69. * @param ILogger $logger
  70. */
  71. public function __construct($appName,
  72. IRequest $request,
  73. IAvatarManager $avatarManager,
  74. \OC\Cache\File $cache,
  75. IL10N $l10n,
  76. IUserManager $userManager,
  77. IUserSession $userSession,
  78. Folder $userFolder,
  79. ILogger $logger) {
  80. parent::__construct($appName, $request);
  81. $this->avatarManager = $avatarManager;
  82. $this->cache = $cache;
  83. $this->l = $l10n;
  84. $this->userManager = $userManager;
  85. $this->userSession = $userSession;
  86. $this->userFolder = $userFolder;
  87. $this->logger = $logger;
  88. }
  89. /**
  90. * @NoAdminRequired
  91. * @NoCSRFRequired
  92. *
  93. * @param string $userId
  94. * @param int $size
  95. * @return DataResponse|DataDisplayResponse
  96. */
  97. public function getAvatar($userId, $size) {
  98. if ($size > 2048) {
  99. $size = 2048;
  100. } elseif ($size <= 0) {
  101. $size = 64;
  102. }
  103. try {
  104. $avatar = $this->avatarManager->getAvatar($userId)->getFile($size);
  105. $resp = new DataDisplayResponse($avatar->getContent(),
  106. Http::STATUS_OK,
  107. ['Content-Type' => $avatar->getMimeType()]);
  108. $resp->setETag($avatar->getEtag());
  109. } catch (NotFoundException $e) {
  110. $user = $this->userManager->get($userId);
  111. $resp = new DataResponse([
  112. 'data' => [
  113. 'displayname' => $user->getDisplayName(),
  114. ],
  115. ]);
  116. } catch (\Exception $e) {
  117. $resp = new DataResponse([
  118. 'data' => [
  119. 'displayname' => '',
  120. ],
  121. ]);
  122. }
  123. $resp->addHeader('Pragma', 'public');
  124. $resp->cacheFor(0);
  125. $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
  126. return $resp;
  127. }
  128. /**
  129. * @NoAdminRequired
  130. *
  131. * @param string $path
  132. * @return DataResponse
  133. */
  134. public function postAvatar($path) {
  135. $userId = $this->userSession->getUser()->getUID();
  136. $files = $this->request->getUploadedFile('files');
  137. $headers = [];
  138. if ($this->request->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE_8])) {
  139. // due to upload iframe workaround, need to set content-type to text/plain
  140. $headers['Content-Type'] = 'text/plain';
  141. }
  142. if (isset($path)) {
  143. $path = stripslashes($path);
  144. $node = $this->userFolder->get($path);
  145. if (!($node instanceof \OCP\Files\File)) {
  146. return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers);
  147. }
  148. if ($node->getSize() > 20*1024*1024) {
  149. return new DataResponse(
  150. ['data' => ['message' => $this->l->t('File is too big')]],
  151. Http::STATUS_BAD_REQUEST,
  152. $headers
  153. );
  154. }
  155. $content = $node->getContent();
  156. } elseif (!is_null($files)) {
  157. if (
  158. $files['error'][0] === 0 &&
  159. is_uploaded_file($files['tmp_name'][0]) &&
  160. !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
  161. ) {
  162. if ($files['size'][0] > 20*1024*1024) {
  163. return new DataResponse(
  164. ['data' => ['message' => $this->l->t('File is too big')]],
  165. Http::STATUS_BAD_REQUEST,
  166. $headers
  167. );
  168. }
  169. $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
  170. $content = $this->cache->get('avatar_upload');
  171. unlink($files['tmp_name'][0]);
  172. } else {
  173. return new DataResponse(
  174. ['data' => ['message' => $this->l->t('Invalid file provided')]],
  175. Http::STATUS_BAD_REQUEST,
  176. $headers
  177. );
  178. }
  179. } else {
  180. //Add imgfile
  181. return new DataResponse(
  182. ['data' => ['message' => $this->l->t('No image or file provided')]],
  183. Http::STATUS_BAD_REQUEST,
  184. $headers
  185. );
  186. }
  187. try {
  188. $image = new \OC_Image();
  189. $image->loadFromData($content);
  190. $image->fixOrientation();
  191. if ($image->valid()) {
  192. $mimeType = $image->mimeType();
  193. if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
  194. return new DataResponse(
  195. ['data' => ['message' => $this->l->t('Unknown filetype')]],
  196. Http::STATUS_OK,
  197. $headers
  198. );
  199. }
  200. $this->cache->set('tmpAvatar', $image->data(), 7200);
  201. return new DataResponse(
  202. ['data' => 'notsquare'],
  203. Http::STATUS_OK,
  204. $headers
  205. );
  206. } else {
  207. return new DataResponse(
  208. ['data' => ['message' => $this->l->t('Invalid image')]],
  209. Http::STATUS_OK,
  210. $headers
  211. );
  212. }
  213. } catch (\Exception $e) {
  214. $this->logger->logException($e, ['app' => 'core']);
  215. return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK, $headers);
  216. }
  217. }
  218. /**
  219. * @NoAdminRequired
  220. *
  221. * @return DataResponse
  222. */
  223. public function deleteAvatar() {
  224. $userId = $this->userSession->getUser()->getUID();
  225. try {
  226. $avatar = $this->avatarManager->getAvatar($userId);
  227. $avatar->remove();
  228. return new DataResponse();
  229. } catch (\Exception $e) {
  230. $this->logger->logException($e, ['app' => 'core']);
  231. return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
  232. }
  233. }
  234. /**
  235. * @NoAdminRequired
  236. *
  237. * @return DataResponse|DataDisplayResponse
  238. */
  239. public function getTmpAvatar() {
  240. $tmpAvatar = $this->cache->get('tmpAvatar');
  241. if (is_null($tmpAvatar)) {
  242. return new DataResponse(['data' => [
  243. 'message' => $this->l->t("No temporary profile picture available, try again")
  244. ]],
  245. Http::STATUS_NOT_FOUND);
  246. }
  247. $image = new \OC_Image($tmpAvatar);
  248. $resp = new DataDisplayResponse($image->data(),
  249. Http::STATUS_OK,
  250. ['Content-Type' => $image->mimeType()]);
  251. $resp->setETag(crc32($image->data()));
  252. $resp->cacheFor(0);
  253. $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
  254. return $resp;
  255. }
  256. /**
  257. * @NoAdminRequired
  258. *
  259. * @param array $crop
  260. * @return DataResponse
  261. */
  262. public function postCroppedAvatar($crop) {
  263. $userId = $this->userSession->getUser()->getUID();
  264. if (is_null($crop)) {
  265. return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
  266. Http::STATUS_BAD_REQUEST);
  267. }
  268. if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
  269. return new DataResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
  270. Http::STATUS_BAD_REQUEST);
  271. }
  272. $tmpAvatar = $this->cache->get('tmpAvatar');
  273. if (is_null($tmpAvatar)) {
  274. return new DataResponse(['data' => [
  275. 'message' => $this->l->t("No temporary profile picture available, try again")
  276. ]],
  277. Http::STATUS_BAD_REQUEST);
  278. }
  279. $image = new \OC_Image($tmpAvatar);
  280. $image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h']));
  281. try {
  282. $avatar = $this->avatarManager->getAvatar($userId);
  283. $avatar->set($image);
  284. // Clean up
  285. $this->cache->remove('tmpAvatar');
  286. return new DataResponse(['status' => 'success']);
  287. } catch (\OC\NotSquareException $e) {
  288. return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
  289. Http::STATUS_BAD_REQUEST);
  290. } catch (\Exception $e) {
  291. $this->logger->logException($e, ['app' => 'core']);
  292. return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
  293. }
  294. }
  295. }