avatarcontroller.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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) 2015, 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\Avatar;
  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\IAvatarManager;
  33. use OCP\ILogger;
  34. use OCP\IL10N;
  35. use OCP\IRequest;
  36. use OCP\IUserManager;
  37. use OCP\IUserSession;
  38. use OCP\Files\Folder;
  39. /**
  40. * Class AvatarController
  41. *
  42. * @package OC\Core\Avatar
  43. */
  44. class AvatarController extends Controller {
  45. /** @var IAvatarManager */
  46. protected $avatarManager;
  47. /** @var \OC\Cache\File */
  48. protected $cache;
  49. /** @var IL10N */
  50. protected $l;
  51. /** @var IUserManager */
  52. protected $userManager;
  53. /** @var IUserSession */
  54. protected $userSession;
  55. /** @var Folder */
  56. protected $userFolder;
  57. /** @var ILogger */
  58. protected $logger;
  59. /**
  60. * @param string $appName
  61. * @param IRequest $request
  62. * @param IAvatarManager $avatarManager
  63. * @param \OC\Cache\File $cache
  64. * @param IL10N $l10n
  65. * @param IUserManager $userManager
  66. * @param IUserSession $userSession
  67. * @param Folder $userFolder
  68. * @param ILogger $logger
  69. */
  70. public function __construct($appName,
  71. IRequest $request,
  72. IAvatarManager $avatarManager,
  73. \OC\Cache\File $cache,
  74. IL10N $l10n,
  75. IUserManager $userManager,
  76. IUserSession $userSession,
  77. Folder $userFolder,
  78. ILogger $logger) {
  79. parent::__construct($appName, $request);
  80. $this->avatarManager = $avatarManager;
  81. $this->cache = $cache;
  82. $this->l = $l10n;
  83. $this->userManager = $userManager;
  84. $this->userSession = $userSession;
  85. $this->userFolder = $userFolder;
  86. $this->logger = $logger;
  87. }
  88. /**
  89. * @NoAdminRequired
  90. * @NoCSRFRequired
  91. *
  92. * @param string $userId
  93. * @param int $size
  94. * @return DataResponse|DataDisplayResponse
  95. */
  96. public function getAvatar($userId, $size) {
  97. if ($size > 2048) {
  98. $size = 2048;
  99. } elseif ($size <= 0) {
  100. $size = 64;
  101. }
  102. $avatar = $this->avatarManager->getAvatar($userId);
  103. $image = $avatar->get($size);
  104. if ($image instanceof \OCP\IImage) {
  105. $resp = new DataDisplayResponse($image->data(),
  106. Http::STATUS_OK,
  107. ['Content-Type' => $image->mimeType()]);
  108. $resp->setETag(crc32($image->data()));
  109. } else {
  110. $user = $this->userManager->get($userId);
  111. $userName = $user ? $user->getDisplayName() : '';
  112. $resp = new DataResponse([
  113. 'data' => [
  114. 'displayname' => $userName,
  115. ],
  116. ]);
  117. }
  118. $resp->addHeader('Pragma', 'public');
  119. $resp->cacheFor(0);
  120. $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
  121. return $resp;
  122. }
  123. /**
  124. * @NoAdminRequired
  125. *
  126. * @param string $path
  127. * @return DataResponse
  128. */
  129. public function postAvatar($path) {
  130. $userId = $this->userSession->getUser()->getUID();
  131. $files = $this->request->getUploadedFile('files');
  132. $headers = [];
  133. if ($this->request->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE_8])) {
  134. // due to upload iframe workaround, need to set content-type to text/plain
  135. $headers['Content-Type'] = 'text/plain';
  136. }
  137. if (isset($path)) {
  138. $path = stripslashes($path);
  139. $node = $this->userFolder->get($path);
  140. if ($node->getSize() > 20*1024*1024) {
  141. return new DataResponse(
  142. ['data' => ['message' => $this->l->t('File is too big')]],
  143. Http::STATUS_BAD_REQUEST,
  144. $headers
  145. );
  146. }
  147. $content = $node->getContent();
  148. } elseif (!is_null($files)) {
  149. if (
  150. $files['error'][0] === 0 &&
  151. is_uploaded_file($files['tmp_name'][0]) &&
  152. !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
  153. ) {
  154. if ($files['size'][0] > 20*1024*1024) {
  155. return new DataResponse(
  156. ['data' => ['message' => $this->l->t('File is too big')]],
  157. Http::STATUS_BAD_REQUEST,
  158. $headers
  159. );
  160. }
  161. $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
  162. $content = $this->cache->get('avatar_upload');
  163. unlink($files['tmp_name'][0]);
  164. } else {
  165. return new DataResponse(
  166. ['data' => ['message' => $this->l->t('Invalid file provided')]],
  167. Http::STATUS_BAD_REQUEST,
  168. $headers
  169. );
  170. }
  171. } else {
  172. //Add imgfile
  173. return new DataResponse(
  174. ['data' => ['message' => $this->l->t('No image or file provided')]],
  175. Http::STATUS_BAD_REQUEST,
  176. $headers
  177. );
  178. }
  179. try {
  180. $image = new \OC_Image();
  181. $image->loadFromData($content);
  182. $image->fixOrientation();
  183. if ($image->valid()) {
  184. $mimeType = $image->mimeType();
  185. if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
  186. return new DataResponse(
  187. ['data' => ['message' => $this->l->t('Unknown filetype')]],
  188. Http::STATUS_OK,
  189. $headers
  190. );
  191. }
  192. $this->cache->set('tmpAvatar', $image->data(), 7200);
  193. return new DataResponse(
  194. ['data' => 'notsquare'],
  195. Http::STATUS_OK,
  196. $headers
  197. );
  198. } else {
  199. return new DataResponse(
  200. ['data' => ['message' => $this->l->t('Invalid image')]],
  201. Http::STATUS_OK,
  202. $headers
  203. );
  204. }
  205. } catch (\Exception $e) {
  206. $this->logger->logException($e, ['app' => 'core']);
  207. return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK, $headers);
  208. }
  209. }
  210. /**
  211. * @NoAdminRequired
  212. *
  213. * @return DataResponse
  214. */
  215. public function deleteAvatar() {
  216. $userId = $this->userSession->getUser()->getUID();
  217. try {
  218. $avatar = $this->avatarManager->getAvatar($userId);
  219. $avatar->remove();
  220. return new DataResponse();
  221. } catch (\Exception $e) {
  222. $this->logger->logException($e, ['app' => 'core']);
  223. return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
  224. }
  225. }
  226. /**
  227. * @NoAdminRequired
  228. *
  229. * @return DataResponse|DataDisplayResponse
  230. */
  231. public function getTmpAvatar() {
  232. $tmpAvatar = $this->cache->get('tmpAvatar');
  233. if (is_null($tmpAvatar)) {
  234. return new DataResponse(['data' => [
  235. 'message' => $this->l->t("No temporary profile picture available, try again")
  236. ]],
  237. Http::STATUS_NOT_FOUND);
  238. }
  239. $image = new \OC_Image($tmpAvatar);
  240. $resp = new DataDisplayResponse($image->data(),
  241. Http::STATUS_OK,
  242. ['Content-Type' => $image->mimeType()]);
  243. $resp->setETag(crc32($image->data()));
  244. $resp->cacheFor(0);
  245. $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
  246. return $resp;
  247. }
  248. /**
  249. * @NoAdminRequired
  250. *
  251. * @param array $crop
  252. * @return DataResponse
  253. */
  254. public function postCroppedAvatar($crop) {
  255. $userId = $this->userSession->getUser()->getUID();
  256. if (is_null($crop)) {
  257. return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
  258. Http::STATUS_BAD_REQUEST);
  259. }
  260. if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
  261. return new DataResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
  262. Http::STATUS_BAD_REQUEST);
  263. }
  264. $tmpAvatar = $this->cache->get('tmpAvatar');
  265. if (is_null($tmpAvatar)) {
  266. return new DataResponse(['data' => [
  267. 'message' => $this->l->t("No temporary profile picture available, try again")
  268. ]],
  269. Http::STATUS_BAD_REQUEST);
  270. }
  271. $image = new \OC_Image($tmpAvatar);
  272. $image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h']));
  273. try {
  274. $avatar = $this->avatarManager->getAvatar($userId);
  275. $avatar->set($image);
  276. // Clean up
  277. $this->cache->remove('tmpAvatar');
  278. return new DataResponse(['status' => 'success']);
  279. } catch (\OC\NotSquareException $e) {
  280. return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
  281. Http::STATUS_BAD_REQUEST);
  282. } catch (\Exception $e) {
  283. $this->logger->logException($e, ['app' => 'core']);
  284. return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
  285. }
  286. }
  287. }