controller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. $user = stripslashes($args['user']);
  14. $size = (int)$args['size'];
  15. if ($size > 2048) {
  16. $size = 2048;
  17. }
  18. // Undefined size
  19. elseif ($size === 0) {
  20. $size = 64;
  21. }
  22. $avatar = new \OC_Avatar($user);
  23. $image = $avatar->get($size);
  24. \OC_Response::disableCaching();
  25. \OC_Response::setLastModifiedHeader(time());
  26. if ($image instanceof \OC_Image) {
  27. \OC_Response::setETagHeader(crc32($image->data()));
  28. $image->show();
  29. } else {
  30. // Signalizes $.avatar() to display a defaultavatar
  31. \OC_JSON::success();
  32. }
  33. }
  34. public static function postAvatar($args) {
  35. \OC_JSON::checkLoggedIn();
  36. \OC_JSON::callCheck();
  37. $user = \OC_User::getUser();
  38. if (isset($_POST['path'])) {
  39. $path = stripslashes($_POST['path']);
  40. $view = new \OC\Files\View('/'.$user.'/files');
  41. $newAvatar = $view->file_get_contents($path);
  42. } elseif (!empty($_FILES)) {
  43. $files = $_FILES['files'];
  44. if (
  45. $files['error'][0] === 0 &&
  46. is_uploaded_file($files['tmp_name'][0]) &&
  47. !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
  48. ) {
  49. $newAvatar = file_get_contents($files['tmp_name'][0]);
  50. unlink($files['tmp_name'][0]);
  51. }
  52. } else {
  53. $l = new \OC_L10n('core');
  54. \OC_JSON::error(array("data" => array("message" => $l->t("No image or file provided")) ));
  55. return;
  56. }
  57. try {
  58. $avatar = new \OC_Avatar($user);
  59. $avatar->set($newAvatar);
  60. \OC_JSON::success();
  61. } catch (\OC\NotSquareException $e) {
  62. $image = new \OC_Image($newAvatar);
  63. if ($image->valid()) {
  64. \OC_Cache::set('tmpavatar', $image->data(), 7200);
  65. \OC_JSON::error(array("data" => "notsquare"));
  66. } else {
  67. $l = new \OC_L10n('core');
  68. $mimeType = $image->mimeType();
  69. if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
  70. \OC_JSON::error(array("data" => array("message" => $l->t("Unknown filetype")) ));
  71. }
  72. if (!$image->valid()) {
  73. \OC_JSON::error(array("data" => array("message" => $l->t("Invalid image")) ));
  74. }
  75. }
  76. } catch (\Exception $e) {
  77. \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  78. }
  79. }
  80. public static function deleteAvatar($args) {
  81. \OC_JSON::checkLoggedIn();
  82. \OC_JSON::callCheck();
  83. $user = \OC_User::getUser();
  84. try {
  85. $avatar = new \OC_Avatar($user);
  86. $avatar->remove();
  87. \OC_JSON::success();
  88. } catch (\Exception $e) {
  89. \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  90. }
  91. }
  92. public static function getTmpAvatar($args) {
  93. \OC_JSON::checkLoggedIn();
  94. \OC_JSON::callCheck();
  95. $tmpavatar = \OC_Cache::get('tmpavatar');
  96. if (is_null($tmpavatar)) {
  97. $l = new \OC_L10n('core');
  98. \OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
  99. return;
  100. }
  101. $image = new \OC_Image($tmpavatar);
  102. \OC_Response::disableCaching();
  103. \OC_Response::setLastModifiedHeader(time());
  104. \OC_Response::setETagHeader(crc32($image->data()));
  105. $image->show();
  106. }
  107. public static function postCroppedAvatar($args) {
  108. \OC_JSON::checkLoggedIn();
  109. \OC_JSON::callCheck();
  110. $user = \OC_User::getUser();
  111. if (isset($_POST['crop'])) {
  112. $crop = $_POST['crop'];
  113. } else {
  114. $l = new \OC_L10n('core');
  115. \OC_JSON::error(array("data" => array("message" => $l->t("No crop data provided")) ));
  116. return;
  117. }
  118. $tmpavatar = \OC_Cache::get('tmpavatar');
  119. if (is_null($tmpavatar)) {
  120. $l = new \OC_L10n('core');
  121. \OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
  122. return;
  123. }
  124. $image = new \OC_Image($tmpavatar);
  125. $image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
  126. try {
  127. $avatar = new \OC_Avatar($user);
  128. $avatar->set($image->data());
  129. // Clean up
  130. \OC_Cache::remove('tmpavatar');
  131. \OC_JSON::success();
  132. } catch (\Exception $e) {
  133. \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  134. }
  135. }
  136. }