upload.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // Firefox and Konqueror tries to download application/json for me. --Arthur
  3. OCP\JSON::setContentTypeHeader('text/plain');
  4. // If a directory token is sent along check if public upload is permitted.
  5. // If not, check the login.
  6. // If no token is sent along, rely on login only
  7. $l = OC_L10N::get('files');
  8. if (empty($_POST['dirToken'])) {
  9. // The standard case, files are uploaded through logged in users :)
  10. OCP\JSON::checkLoggedIn();
  11. $dir = isset($_POST['dir']) ? $_POST['dir'] : "";
  12. if (!$dir || empty($dir) || $dir === false) {
  13. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  14. die();
  15. }
  16. } else {
  17. $linkItem = OCP\Share::getShareByToken($_POST['dirToken']);
  18. if ($linkItem === false) {
  19. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Invalid Token')))));
  20. die();
  21. }
  22. if (!($linkItem['permissions'] & OCP\PERMISSION_CREATE)) {
  23. OCP\JSON::checkLoggedIn();
  24. } else {
  25. // resolve reshares
  26. $rootLinkItem = OCP\Share::resolveReShare($linkItem);
  27. // Setup FS with owner
  28. OC_Util::tearDownFS();
  29. OC_Util::setupFS($rootLinkItem['uid_owner']);
  30. // The token defines the target directory (security reasons)
  31. $path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  32. $dir = sprintf(
  33. "/%s/%s",
  34. $path,
  35. isset($_POST['subdir']) ? $_POST['subdir'] : ''
  36. );
  37. if (!$dir || empty($dir) || $dir === false) {
  38. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  39. die();
  40. }
  41. }
  42. }
  43. OCP\JSON::callCheck();
  44. // get array with current storage stats (e.g. max file size)
  45. $storageStats = \OCA\files\lib\Helper::buildFileStorageStatistics($dir);
  46. if (!isset($_FILES['files'])) {
  47. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('No file was uploaded. Unknown error')), $storageStats)));
  48. exit();
  49. }
  50. foreach ($_FILES['files']['error'] as $error) {
  51. if ($error != 0) {
  52. $errors = array(
  53. UPLOAD_ERR_OK => $l->t('There is no error, the file uploaded with success'),
  54. UPLOAD_ERR_INI_SIZE => $l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini: ')
  55. . ini_get('upload_max_filesize'),
  56. UPLOAD_ERR_FORM_SIZE => $l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
  57. UPLOAD_ERR_PARTIAL => $l->t('The uploaded file was only partially uploaded'),
  58. UPLOAD_ERR_NO_FILE => $l->t('No file was uploaded'),
  59. UPLOAD_ERR_NO_TMP_DIR => $l->t('Missing a temporary folder'),
  60. UPLOAD_ERR_CANT_WRITE => $l->t('Failed to write to disk'),
  61. );
  62. OCP\JSON::error(array('data' => array_merge(array('message' => $errors[$error]), $storageStats)));
  63. exit();
  64. }
  65. }
  66. $files = $_FILES['files'];
  67. $error = '';
  68. $maxUploadFileSize = $storageStats['uploadMaxFilesize'];
  69. $maxHumanFileSize = OCP\Util::humanFileSize($maxUploadFileSize);
  70. $totalSize = 0;
  71. foreach ($files['size'] as $size) {
  72. $totalSize += $size;
  73. }
  74. if ($maxUploadFileSize >= 0 and $totalSize > $maxUploadFileSize) {
  75. OCP\JSON::error(array('data' => array('message' => $l->t('Not enough storage available'),
  76. 'uploadMaxFilesize' => $maxUploadFileSize,
  77. 'maxHumanFilesize' => $maxHumanFileSize)));
  78. exit();
  79. }
  80. $result = array();
  81. if (strpos($dir, '..') === false) {
  82. $fileCount = count($files['name']);
  83. for ($i = 0; $i < $fileCount; $i++) {
  84. $target = OCP\Files::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
  85. // $path needs to be normalized - this failed within drag'n'drop upload to a sub-folder
  86. $target = \OC\Files\Filesystem::normalizePath($target);
  87. if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) {
  88. $meta = \OC\Files\Filesystem::getFileInfo($target);
  89. // updated max file size after upload
  90. $storageStats = \OCA\files\lib\Helper::buildFileStorageStatistics($dir);
  91. $result[] = array('status' => 'success',
  92. 'mime' => $meta['mimetype'],
  93. 'size' => $meta['size'],
  94. 'id' => $meta['fileid'],
  95. 'name' => basename($target),
  96. 'originalname' => $files['name'][$i],
  97. 'uploadMaxFilesize' => $maxUploadFileSize,
  98. 'maxHumanFilesize' => $maxHumanFileSize
  99. );
  100. }
  101. }
  102. OCP\JSON::encodedPrint($result);
  103. exit();
  104. } else {
  105. $error = $l->t('Invalid directory.');
  106. }
  107. OCP\JSON::error(array('data' => array_merge(array('message' => $error), $storageStats)));