You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

controller.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Core\Avatar;
  9. class Controller {
  10. public static function getAvatar($args) {
  11. \OC_JSON::checkLoggedIn();
  12. \OC_JSON::callCheck();
  13. \OC::$server->getSession()->close();
  14. $user = stripslashes($args['user']);
  15. $size = (int)$args['size'];
  16. if ($size > 2048) {
  17. $size = 2048;
  18. }
  19. // Undefined size
  20. elseif ($size === 0) {
  21. $size = 64;
  22. }
  23. $avatar = new \OC_Avatar($user);
  24. $image = $avatar->get($size);
  25. \OC_Response::disableCaching();
  26. \OC_Response::setLastModifiedHeader(time());
  27. if ($image instanceof \OC_Image) {
  28. \OC_Response::setETagHeader(crc32($image->data()));
  29. $image->show();
  30. } else {
  31. // Signalizes $.avatar() to display a defaultavatar
  32. \OC_JSON::success(array("data"=> array("displayname"=> \OC_User::getDisplayName($user)) ));
  33. }
  34. }
  35. public static function postAvatar($args) {
  36. \OC_JSON::checkLoggedIn();
  37. \OC_JSON::callCheck();
  38. $user = \OC_User::getUser();
  39. if (isset($_POST['path'])) {
  40. $path = stripslashes($_POST['path']);
  41. $view = new \OC\Files\View('/'.$user.'/files');
  42. $fileInfo = $view->getFileInfo($path);
  43. if($fileInfo['encrypted'] === true) {
  44. $fileName = $view->toTmpFile($path);
  45. } else {
  46. $fileName = $view->getLocalFile($path);
  47. }
  48. } elseif (!empty($_FILES)) {
  49. $files = $_FILES['files'];
  50. if (
  51. $files['error'][0] === 0 &&
  52. is_uploaded_file($files['tmp_name'][0]) &&
  53. !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
  54. ) {
  55. \OC\Cache::set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
  56. $view = new \OC\Files\View('/'.$user.'/cache');
  57. $fileName = $view->getLocalFile('avatar_upload');
  58. unlink($files['tmp_name'][0]);
  59. }
  60. } else {
  61. $l = new \OC_L10n('core');
  62. \OC_JSON::error(array("data" => array("message" => $l->t("No image or file provided")) ));
  63. return;
  64. }
  65. try {
  66. $image = new \OC_Image();
  67. $image->loadFromFile($fileName);
  68. $image->fixOrientation();
  69. if ($image->valid()) {
  70. \OC\Cache::set('tmpavatar', $image->data(), 7200);
  71. \OC_JSON::error(array("data" => "notsquare"));
  72. } else {
  73. $l = new \OC_L10n('core');
  74. $mimeType = $image->mimeType();
  75. if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
  76. \OC_JSON::error(array("data" => array("message" => $l->t("Unknown filetype")) ));
  77. }
  78. if (!$image->valid()) {
  79. \OC_JSON::error(array("data" => array("message" => $l->t("Invalid image")) ));
  80. }
  81. }
  82. } catch (\Exception $e) {
  83. \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  84. }
  85. }
  86. public static function deleteAvatar($args) {
  87. \OC_JSON::checkLoggedIn();
  88. \OC_JSON::callCheck();
  89. $user = \OC_User::getUser();
  90. try {
  91. $avatar = new \OC_Avatar($user);
  92. $avatar->remove();
  93. \OC_JSON::success();
  94. } catch (\Exception $e) {
  95. \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  96. }
  97. }
  98. public static function getTmpAvatar($args) {
  99. \OC_JSON::checkLoggedIn();
  100. \OC_JSON::callCheck();
  101. $tmpavatar = \OC\Cache::get('tmpavatar');
  102. if (is_null($tmpavatar)) {
  103. $l = new \OC_L10n('core');
  104. \OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
  105. return;
  106. }
  107. $image = new \OC_Image($tmpavatar);
  108. \OC_Response::disableCaching();
  109. \OC_Response::setLastModifiedHeader(time());
  110. \OC_Response::setETagHeader(crc32($image->data()));
  111. $image->show();
  112. }
  113. public static function postCroppedAvatar($args) {
  114. \OC_JSON::checkLoggedIn();
  115. \OC_JSON::callCheck();
  116. $user = \OC_User::getUser();
  117. if (isset($_POST['crop'])) {
  118. $crop = $_POST['crop'];
  119. } else {
  120. $l = new \OC_L10n('core');
  121. \OC_JSON::error(array("data" => array("message" => $l->t("No crop data provided")) ));
  122. return;
  123. }
  124. $tmpavatar = \OC\Cache::get('tmpavatar');
  125. if (is_null($tmpavatar)) {
  126. $l = new \OC_L10n('core');
  127. \OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
  128. return;
  129. }
  130. $image = new \OC_Image($tmpavatar);
  131. $image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
  132. try {
  133. $avatar = new \OC_Avatar($user);
  134. $avatar->set($image->data());
  135. // Clean up
  136. \OC\Cache::remove('tmpavatar');
  137. \OC_JSON::success();
  138. } catch (\Exception $e) {
  139. \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  140. }
  141. }
  142. }