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.

251 lines
8.6 KiB

14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Florian Pritz <bluewind@xinu.at>
  7. * @author Frank Karlitschek <frank@owncloud.org>
  8. * @author Individual IT Services <info@individual-it.net>
  9. * @author Joas Schilling <nickvergessen@owncloud.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@owncloud.com>
  12. * @author Luke Policinski <lpolicinski@gmail.com>
  13. * @author Robin Appelman <icewind@owncloud.com>
  14. * @author Roman Geber <rgeber@owncloudapps.com>
  15. * @author TheSFReader <TheSFReader@gmail.com>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <pvince81@owncloud.com>
  18. *
  19. * @copyright Copyright (c) 2015, ownCloud, Inc.
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. \OC::$server->getSession()->close();
  36. // Firefox and Konqueror tries to download application/json for me. --Arthur
  37. OCP\JSON::setContentTypeHeader('text/plain');
  38. // If a directory token is sent along check if public upload is permitted.
  39. // If not, check the login.
  40. // If no token is sent along, rely on login only
  41. $allowedPermissions = \OCP\Constants::PERMISSION_ALL;
  42. $errorCode = null;
  43. $l = \OC::$server->getL10N('files');
  44. if (empty($_POST['dirToken'])) {
  45. // The standard case, files are uploaded through logged in users :)
  46. OCP\JSON::checkLoggedIn();
  47. $dir = isset($_POST['dir']) ? (string)$_POST['dir'] : '';
  48. if (!$dir || empty($dir) || $dir === false) {
  49. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  50. die();
  51. }
  52. } else {
  53. // TODO: ideally this code should be in files_sharing/ajax/upload.php
  54. // and the upload/file transfer code needs to be refactored into a utility method
  55. // that could be used there
  56. \OC_User::setIncognitoMode(true);
  57. // return only read permissions for public upload
  58. $allowedPermissions = \OCP\Constants::PERMISSION_READ;
  59. $publicDirectory = !empty($_POST['subdir']) ? (string)$_POST['subdir'] : '/';
  60. $linkItem = OCP\Share::getShareByToken((string)$_POST['dirToken']);
  61. if ($linkItem === false) {
  62. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Invalid Token')))));
  63. die();
  64. }
  65. if (!($linkItem['permissions'] & \OCP\Constants::PERMISSION_CREATE)) {
  66. OCP\JSON::checkLoggedIn();
  67. } else {
  68. // resolve reshares
  69. $rootLinkItem = OCP\Share::resolveReShare($linkItem);
  70. OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
  71. // Setup FS with owner
  72. OC_Util::tearDownFS();
  73. OC_Util::setupFS($rootLinkItem['uid_owner']);
  74. // The token defines the target directory (security reasons)
  75. $path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
  76. if($path === null) {
  77. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  78. die();
  79. }
  80. $dir = sprintf(
  81. "/%s/%s",
  82. $path,
  83. $publicDirectory
  84. );
  85. if (!$dir || empty($dir) || $dir === false) {
  86. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
  87. die();
  88. }
  89. $dir = rtrim($dir, '/');
  90. }
  91. }
  92. OCP\JSON::callCheck();
  93. // get array with current storage stats (e.g. max file size)
  94. $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir);
  95. if (!isset($_FILES['files'])) {
  96. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('No file was uploaded. Unknown error')), $storageStats)));
  97. exit();
  98. }
  99. foreach ($_FILES['files']['error'] as $error) {
  100. if ($error != 0) {
  101. $errors = array(
  102. UPLOAD_ERR_OK => $l->t('There is no error, the file uploaded with success'),
  103. UPLOAD_ERR_INI_SIZE => $l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini: ')
  104. . ini_get('upload_max_filesize'),
  105. UPLOAD_ERR_FORM_SIZE => $l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
  106. UPLOAD_ERR_PARTIAL => $l->t('The uploaded file was only partially uploaded'),
  107. UPLOAD_ERR_NO_FILE => $l->t('No file was uploaded'),
  108. UPLOAD_ERR_NO_TMP_DIR => $l->t('Missing a temporary folder'),
  109. UPLOAD_ERR_CANT_WRITE => $l->t('Failed to write to disk'),
  110. );
  111. $errorMessage = $errors[$error];
  112. \OC::$server->getLogger()->alert("Upload error: $error - $errorMessage", array('app' => 'files'));
  113. OCP\JSON::error(array('data' => array_merge(array('message' => $errorMessage), $storageStats)));
  114. exit();
  115. }
  116. }
  117. $files = $_FILES['files'];
  118. $error = false;
  119. $maxUploadFileSize = $storageStats['uploadMaxFilesize'];
  120. $maxHumanFileSize = OCP\Util::humanFileSize($maxUploadFileSize);
  121. $totalSize = 0;
  122. foreach ($files['size'] as $size) {
  123. $totalSize += $size;
  124. }
  125. if ($maxUploadFileSize >= 0 and $totalSize > $maxUploadFileSize) {
  126. OCP\JSON::error(array('data' => array('message' => $l->t('Not enough storage available'),
  127. 'uploadMaxFilesize' => $maxUploadFileSize,
  128. 'maxHumanFilesize' => $maxHumanFileSize)));
  129. exit();
  130. }
  131. $result = array();
  132. if (\OC\Files\Filesystem::isValidPath($dir) === true) {
  133. $fileCount = count($files['name']);
  134. for ($i = 0; $i < $fileCount; $i++) {
  135. if (isset($_POST['resolution'])) {
  136. $resolution = $_POST['resolution'];
  137. } else {
  138. $resolution = null;
  139. }
  140. // target directory for when uploading folders
  141. $relativePath = '';
  142. if(!empty($_POST['file_directory'])) {
  143. $relativePath = '/'.$_POST['file_directory'];
  144. }
  145. // $path needs to be normalized - this failed within drag'n'drop upload to a sub-folder
  146. if ($resolution === 'autorename') {
  147. // append a number in brackets like 'filename (2).ext'
  148. $target = OCP\Files::buildNotExistingFileName($dir . $relativePath, $files['name'][$i]);
  149. } else {
  150. $target = \OC\Files\Filesystem::normalizePath($dir . $relativePath.'/'.$files['name'][$i]);
  151. }
  152. // relative dir to return to the client
  153. if (isset($publicDirectory)) {
  154. // path relative to the public root
  155. $returnedDir = $publicDirectory . $relativePath;
  156. } else {
  157. // full path
  158. $returnedDir = $dir . $relativePath;
  159. }
  160. $returnedDir = \OC\Files\Filesystem::normalizePath($returnedDir);
  161. $exists = \OC\Files\Filesystem::file_exists($target);
  162. if ($exists) {
  163. $updatable = \OC\Files\Filesystem::isUpdatable($target);
  164. }
  165. if ( ! $exists || ($updatable && $resolution === 'replace' ) ) {
  166. // upload and overwrite file
  167. try
  168. {
  169. if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) {
  170. // updated max file size after upload
  171. $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir);
  172. $meta = \OC\Files\Filesystem::getFileInfo($target);
  173. if ($meta === false) {
  174. $error = $l->t('The target folder has been moved or deleted.');
  175. $errorCode = 'targetnotfound';
  176. } else {
  177. $data = \OCA\Files\Helper::formatFileInfo($meta);
  178. $data['status'] = 'success';
  179. $data['originalname'] = $files['name'][$i];
  180. $data['uploadMaxFilesize'] = $maxUploadFileSize;
  181. $data['maxHumanFilesize'] = $maxHumanFileSize;
  182. $data['permissions'] = $meta['permissions'] & $allowedPermissions;
  183. $data['directory'] = $returnedDir;
  184. $result[] = $data;
  185. }
  186. } else {
  187. $error = $l->t('Upload failed. Could not find uploaded file');
  188. }
  189. } catch(Exception $ex) {
  190. $error = $ex->getMessage();
  191. }
  192. } else {
  193. // file already exists
  194. $meta = \OC\Files\Filesystem::getFileInfo($target);
  195. if ($meta === false) {
  196. $error = $l->t('Upload failed. Could not get file info.');
  197. } else {
  198. $data = \OCA\Files\Helper::formatFileInfo($meta);
  199. if ($updatable) {
  200. $data['status'] = 'existserror';
  201. } else {
  202. $data['status'] = 'readonly';
  203. }
  204. $data['originalname'] = $files['name'][$i];
  205. $data['uploadMaxFilesize'] = $maxUploadFileSize;
  206. $data['maxHumanFilesize'] = $maxHumanFileSize;
  207. $data['permissions'] = $meta['permissions'] & $allowedPermissions;
  208. $data['directory'] = $returnedDir;
  209. $result[] = $data;
  210. }
  211. }
  212. }
  213. } else {
  214. $error = $l->t('Invalid directory.');
  215. }
  216. if ($error === false) {
  217. OCP\JSON::encodedPrint($result);
  218. } else {
  219. OCP\JSON::error(array(array('data' => array_merge(array('message' => $error, 'code' => $errorCode), $storageStats))));
  220. }