avatarcontroller.php 7.2 KB

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