upload.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $allowedPermissions = OCP\PERMISSION_ALL;
  8. $l = OC_L10N::get('files');
  9. if (empty($_POST['dirToken'])) {
  10. // The standard case, files are uploaded through logged in users :)
  11. OCP\JSON::checkLoggedIn();
  12. $dir = isset($_POST['dir']) ? $_POST['dir'] : "";
  13. if (!$dir || empty($dir) || $dir === false) {
  14. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  15. die();
  16. }
  17. } else {
  18. // return only read permissions for public upload
  19. $allowedPermissions = OCP\PERMISSION_READ;
  20. $linkItem = OCP\Share::getShareByToken($_POST['dirToken']);
  21. if ($linkItem === false) {
  22. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Invalid Token')))));
  23. die();
  24. }
  25. if (!($linkItem['permissions'] & OCP\PERMISSION_CREATE)) {
  26. OCP\JSON::checkLoggedIn();
  27. } else {
  28. // resolve reshares
  29. $rootLinkItem = OCP\Share::resolveReShare($linkItem);
  30. // Setup FS with owner
  31. OC_Util::tearDownFS();
  32. OC_Util::setupFS($rootLinkItem['uid_owner']);
  33. // The token defines the target directory (security reasons)
  34. $path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  35. $dir = sprintf(
  36. "/%s/%s",
  37. $path,
  38. isset($_POST['subdir']) ? $_POST['subdir'] : ''
  39. );
  40. if (!$dir || empty($dir) || $dir === false) {
  41. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  42. die();
  43. }
  44. }
  45. }
  46. OCP\JSON::callCheck();
  47. // get array with current storage stats (e.g. max file size)
  48. $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir);
  49. if (!isset($_FILES['files'])) {
  50. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('No file was uploaded. Unknown error')), $storageStats)));
  51. exit();
  52. }
  53. foreach ($_FILES['files']['error'] as $error) {
  54. if ($error != 0) {
  55. $errors = array(
  56. UPLOAD_ERR_OK => $l->t('There is no error, the file uploaded with success'),
  57. UPLOAD_ERR_INI_SIZE => $l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini: ')
  58. . ini_get('upload_max_filesize'),
  59. UPLOAD_ERR_FORM_SIZE => $l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
  60. UPLOAD_ERR_PARTIAL => $l->t('The uploaded file was only partially uploaded'),
  61. UPLOAD_ERR_NO_FILE => $l->t('No file was uploaded'),
  62. UPLOAD_ERR_NO_TMP_DIR => $l->t('Missing a temporary folder'),
  63. UPLOAD_ERR_CANT_WRITE => $l->t('Failed to write to disk'),
  64. );
  65. OCP\JSON::error(array('data' => array_merge(array('message' => $errors[$error]), $storageStats)));
  66. exit();
  67. }
  68. }
  69. $files = $_FILES['files'];
  70. $error = false;
  71. $maxUploadFileSize = $storageStats['uploadMaxFilesize'];
  72. $maxHumanFileSize = OCP\Util::humanFileSize($maxUploadFileSize);
  73. $totalSize = 0;
  74. foreach ($files['size'] as $size) {
  75. $totalSize += $size;
  76. }
  77. if ($maxUploadFileSize >= 0 and $totalSize > $maxUploadFileSize) {
  78. OCP\JSON::error(array('data' => array('message' => $l->t('Not enough storage available'),
  79. 'uploadMaxFilesize' => $maxUploadFileSize,
  80. 'maxHumanFilesize' => $maxHumanFileSize)));
  81. exit();
  82. }
  83. $result = array();
  84. if (strpos($dir, '..') === false) {
  85. $fileCount = count($files['name']);
  86. for ($i = 0; $i < $fileCount; $i++) {
  87. // $path needs to be normalized - this failed within drag'n'drop upload to a sub-folder
  88. if (isset($_POST['resolution']) && $_POST['resolution']==='autorename') {
  89. // append a number in brackets like 'filename (2).ext'
  90. $target = OCP\Files::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
  91. } else {
  92. $target = \OC\Files\Filesystem::normalizePath(stripslashes($dir).'/'.$files['name'][$i]);
  93. }
  94. if ( ! \OC\Files\Filesystem::file_exists($target)
  95. || (isset($_POST['resolution']) && $_POST['resolution']==='replace')
  96. ) {
  97. // upload and overwrite file
  98. try
  99. {
  100. if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) {
  101. // updated max file size after upload
  102. $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir);
  103. $meta = \OC\Files\Filesystem::getFileInfo($target);
  104. if ($meta === false) {
  105. $error = $l->t('Upload failed. Could not get file info.');
  106. } else {
  107. $result[] = array('status' => 'success',
  108. 'mime' => $meta['mimetype'],
  109. 'mtime' => $meta['mtime'],
  110. 'size' => $meta['size'],
  111. 'id' => $meta['fileid'],
  112. 'name' => basename($target),
  113. 'originalname' => $files['tmp_name'][$i],
  114. 'uploadMaxFilesize' => $maxUploadFileSize,
  115. 'maxHumanFilesize' => $maxHumanFileSize,
  116. 'permissions' => $meta['permissions'] & $allowedPermissions
  117. );
  118. }
  119. } else {
  120. $error = $l->t('Upload failed. Could not find uploaded file');
  121. }
  122. } catch(Exception $ex) {
  123. $error = $ex->getMessage();
  124. }
  125. } else {
  126. // file already exists
  127. $meta = \OC\Files\Filesystem::getFileInfo($target);
  128. if ($meta === false) {
  129. $error = $l->t('Upload failed. Could not get file info.');
  130. } else {
  131. $result[] = array('status' => 'existserror',
  132. 'mime' => $meta['mimetype'],
  133. 'mtime' => $meta['mtime'],
  134. 'size' => $meta['size'],
  135. 'id' => $meta['fileid'],
  136. 'name' => basename($target),
  137. 'originalname' => $files['tmp_name'][$i],
  138. 'uploadMaxFilesize' => $maxUploadFileSize,
  139. 'maxHumanFilesize' => $maxHumanFileSize,
  140. 'permissions' => $meta['permissions'] & $allowedPermissions
  141. );
  142. }
  143. }
  144. }
  145. } else {
  146. $error = $l->t('Invalid directory.');
  147. }
  148. if ($error === false) {
  149. OCP\JSON::encodedPrint($result);
  150. exit();
  151. } else {
  152. OCP\JSON::error(array(array('data' => array_merge(array('message' => $error), $storageStats))));
  153. }