1
0

newfile.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Georg Ehrke <georg@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. // Init owncloud
  29. global $eventSource;
  30. \OCP\JSON::checkLoggedIn();
  31. \OCP\JSON::callCheck();
  32. \OC::$server->getSession()->close();
  33. // Get the params
  34. $dir = isset( $_REQUEST['dir'] ) ? '/'.trim((string)$_REQUEST['dir'], '/\\') : '';
  35. $fileName = isset( $_REQUEST['filename'] ) ? trim((string)$_REQUEST['filename'], '/\\') : '';
  36. $l10n = \OC::$server->getL10N('files');
  37. $result = array(
  38. 'success' => false,
  39. 'data' => NULL
  40. );
  41. try {
  42. \OC\Files\Filesystem::getView()->verifyPath($dir, $fileName);
  43. } catch (\OCP\Files\InvalidPathException $ex) {
  44. $result['data'] = [
  45. 'message' => $ex->getMessage()];
  46. OCP\JSON::error($result);
  47. return;
  48. }
  49. if (!\OC\Files\Filesystem::file_exists($dir . '/')) {
  50. $result['data'] = array('message' => (string)$l10n->t(
  51. 'The target folder has been moved or deleted.'),
  52. 'code' => 'targetnotfound'
  53. );
  54. OCP\JSON::error($result);
  55. exit();
  56. }
  57. $target = $dir.'/'.$fileName;
  58. if (\OC\Files\Filesystem::file_exists($target)) {
  59. $result['data'] = array('message' => (string)$l10n->t(
  60. 'The name %s is already used in the folder %s. Please choose a different name.',
  61. array($fileName, $dir))
  62. );
  63. OCP\JSON::error($result);
  64. exit();
  65. }
  66. $success = false;
  67. $templateManager = OC_Helper::getFileTemplateManager();
  68. $mimeType = OC_Helper::getMimetypeDetector()->detectPath($target);
  69. $content = $templateManager->getTemplate($mimeType);
  70. if($content) {
  71. $success = \OC\Files\Filesystem::file_put_contents($target, $content);
  72. } else {
  73. $success = \OC\Files\Filesystem::touch($target);
  74. }
  75. if($success) {
  76. $meta = \OC\Files\Filesystem::getFileInfo($target);
  77. OCP\JSON::success(array('data' => \OCA\Files\Helper::formatFileInfo($meta)));
  78. return;
  79. }
  80. OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the file') )));