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.

avatar.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. require_once 'lib/base.php';
  3. if (!\OC_User::isLoggedIn()) {
  4. header("HTTP/1.0 403 Forbidden");
  5. \OC_Template::printErrorPage("Permission denied");
  6. }
  7. if ($_SERVER['REQUEST_METHOD'] === "GET") {
  8. if (isset($_GET['user'])) {
  9. //SECURITY TODO does this fully eliminate directory traversals?
  10. $user = stripslashes($_GET['user']);
  11. } else {
  12. exit();
  13. }
  14. if (isset($_GET['size']) && ((int)$_GET['size'] > 0)) {
  15. $size = (int)$_GET['size'];
  16. if ($size > 2048) {
  17. $size = 2048;
  18. }
  19. } else {
  20. $size = 64;
  21. }
  22. $image = \OC_Avatar::get($user, $size);
  23. if ($image instanceof \OC_Image) {
  24. $image->show();
  25. } elseif ($image === false) {
  26. OC_JSON::success(array('user' => $user, 'size' => $size));
  27. }
  28. } elseif ($_SERVER['REQUEST_METHOD'] === "POST") {
  29. $user = OC_User::getUser();
  30. // Select an image from own files
  31. if (isset($_POST['path'])) {
  32. $path = stripslashes($_POST['path']);
  33. $avatar = OC::$SERVERROOT.'/data/'.$user.'/files'.$path;
  34. }
  35. if (isset($_POST['crop'])) {
  36. $crop = json_decode($_POST['crop'], true);
  37. if (!isset($path)) {
  38. // TODO get path to temporarily saved uploaded-avatar
  39. }
  40. $image = new \OC_Image($avatar);
  41. $image->crop($x, $y, $w, $h);
  42. $avatar = $image->data();
  43. }
  44. // Upload a new image
  45. if (!empty($_FILES)) {
  46. $files = $_FILES['files'];
  47. if ($files['error'][0] === 0) {
  48. $avatar = file_get_contents($files['tmp_name'][0]);
  49. unlink($files['tmp_name'][0]);
  50. // TODO make the tmp_name reusable, if the uploaded avatar is not square
  51. }
  52. }
  53. try {
  54. \OC_Avatar::set($user, $avatar);
  55. OC_JSON::success();
  56. } catch (\OC\NotSquareException $e) {
  57. $tmpname = \OC_Util::generate_random_bytes(10);
  58. // TODO Save the image temporarily here
  59. // TODO add a cronjob that cleans up stale tmpimages
  60. OC_JSON::error(array("data" => array("message" => "notsquare", "tmpname" => $tmpname) ));
  61. } catch (\Exception $e) {
  62. OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
  63. }
  64. } elseif ($_SERVER['REQUEST_METHOD'] === "DELETE") {
  65. $user = OC_User::getUser();
  66. try {
  67. \OC_Avatar::remove($user);
  68. OC_JSON::success();
  69. } catch (\Exception $e) {
  70. OC_JSON::error(array("data" => array ("message" => $e->getMessage()) ));
  71. }
  72. }