newfolder.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // Init owncloud
  3. OCP\JSON::checkLoggedIn();
  4. OCP\JSON::callCheck();
  5. \OC::$session->close();
  6. // Get the params
  7. $dir = isset( $_POST['dir'] ) ? stripslashes($_POST['dir']) : '';
  8. $foldername = isset( $_POST['foldername'] ) ? stripslashes($_POST['foldername']) : '';
  9. $l10n = \OC_L10n::get('files');
  10. $result = array(
  11. 'success' => false,
  12. 'data' => NULL
  13. );
  14. if(trim($foldername) === '') {
  15. $result['data'] = array('message' => $l10n->t('Folder name cannot be empty.'));
  16. OCP\JSON::error($result);
  17. exit();
  18. }
  19. if(!OCP\Util::isValidFileName($foldername)) {
  20. $result['data'] = array('message' => (string)$l10n->t("Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed."));
  21. OCP\JSON::error($result);
  22. exit();
  23. }
  24. if (!\OC\Files\Filesystem::file_exists($dir . '/')) {
  25. $result['data'] = array('message' => (string)$l10n->t(
  26. 'The target folder has been moved or deleted.'),
  27. 'code' => 'targetnotfound'
  28. );
  29. OCP\JSON::error($result);
  30. exit();
  31. }
  32. //TODO why is stripslashes used on foldername here but not in newfile.php?
  33. $target = $dir . '/' . stripslashes($foldername);
  34. if (\OC\Files\Filesystem::file_exists($target)) {
  35. $result['data'] = array('message' => $l10n->t(
  36. 'The name %s is already used in the folder %s. Please choose a different name.',
  37. array($foldername, $dir))
  38. );
  39. OCP\JSON::error($result);
  40. exit();
  41. }
  42. if(\OC\Files\Filesystem::mkdir($target)) {
  43. if ( $dir !== '/') {
  44. $path = $dir.'/'.$foldername;
  45. } else {
  46. $path = '/'.$foldername;
  47. }
  48. $meta = \OC\Files\Filesystem::getFileInfo($path);
  49. $meta['type'] = 'dir'; // missing ?!
  50. OCP\JSON::success(array('data' => \OCA\Files\Helper::formatFileInfo($meta)));
  51. exit();
  52. }
  53. OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the folder') )));