Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

newfile.php 2.9KB

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