newfile.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // Init owncloud
  3. global $eventSource;
  4. if(!OC_User::isLoggedIn()) {
  5. exit;
  6. }
  7. \OC::$session->close();
  8. // Get the params
  9. $dir = isset( $_REQUEST['dir'] ) ? '/'.trim($_REQUEST['dir'], '/\\') : '';
  10. $filename = isset( $_REQUEST['filename'] ) ? trim($_REQUEST['filename'], '/\\') : '';
  11. $content = isset( $_REQUEST['content'] ) ? $_REQUEST['content'] : '';
  12. $source = isset( $_REQUEST['source'] ) ? trim($_REQUEST['source'], '/\\') : '';
  13. if($source) {
  14. $eventSource=new OC_EventSource();
  15. } else {
  16. OC_JSON::callCheck();
  17. }
  18. function progress($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
  19. static $filesize = 0;
  20. static $lastsize = 0;
  21. global $eventSource;
  22. switch($notification_code) {
  23. case STREAM_NOTIFY_FILE_SIZE_IS:
  24. $filesize = $bytes_max;
  25. break;
  26. case STREAM_NOTIFY_PROGRESS:
  27. if ($bytes_transferred > 0) {
  28. if (!isset($filesize)) {
  29. } else {
  30. $progress = (int)(($bytes_transferred/$filesize)*100);
  31. if($progress>$lastsize) { //limit the number or messages send
  32. $eventSource->send('progress', $progress);
  33. }
  34. $lastsize=$progress;
  35. }
  36. }
  37. break;
  38. }
  39. }
  40. $l10n = \OC_L10n::get('files');
  41. $result = array(
  42. 'success' => false,
  43. 'data' => NULL
  44. );
  45. $trimmedFileName = trim($filename);
  46. if($trimmedFileName === '') {
  47. $result['data'] = array('message' => (string)$l10n->t('File name cannot be empty.'));
  48. OCP\JSON::error($result);
  49. exit();
  50. }
  51. if($trimmedFileName === '.' || $trimmedFileName === '..') {
  52. $result['data'] = array('message' => (string)$l10n->t('"%s" is an invalid file name.', $trimmedFileName));
  53. OCP\JSON::error($result);
  54. exit();
  55. }
  56. if(!OCP\Util::isValidFileName($filename)) {
  57. $result['data'] = array('message' => (string)$l10n->t("Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed."));
  58. OCP\JSON::error($result);
  59. exit();
  60. }
  61. if (!\OC\Files\Filesystem::file_exists($dir . '/')) {
  62. $result['data'] = array('message' => (string)$l10n->t(
  63. 'The target folder has been moved or deleted.'),
  64. 'code' => 'targetnotfound'
  65. );
  66. OCP\JSON::error($result);
  67. exit();
  68. }
  69. //TODO why is stripslashes used on foldername in newfolder.php but not here?
  70. $target = $dir.'/'.$filename;
  71. if (\OC\Files\Filesystem::file_exists($target)) {
  72. $result['data'] = array('message' => (string)$l10n->t(
  73. 'The name %s is already used in the folder %s. Please choose a different name.',
  74. array($filename, $dir))
  75. );
  76. OCP\JSON::error($result);
  77. exit();
  78. }
  79. if($source) {
  80. if(substr($source, 0, 8)!='https://' and substr($source, 0, 7)!='http://') {
  81. OCP\JSON::error(array('data' => array('message' => $l10n->t('Not a valid source'))));
  82. exit();
  83. }
  84. if (!ini_get('allow_url_fopen')) {
  85. $eventSource->send('error', array('message' => $l10n->t('Server is not allowed to open URLs, please check the server configuration')));
  86. $eventSource->close();
  87. exit();
  88. }
  89. $ctx = stream_context_create(null, array('notification' =>'progress'));
  90. $sourceStream=@fopen($source, 'rb', false, $ctx);
  91. $result = 0;
  92. if (is_resource($sourceStream)) {
  93. $result=\OC\Files\Filesystem::file_put_contents($target, $sourceStream);
  94. }
  95. if($result) {
  96. $meta = \OC\Files\Filesystem::getFileInfo($target);
  97. $data = \OCA\Files\Helper::formatFileInfo($meta);
  98. $eventSource->send('success', $data);
  99. } else {
  100. $eventSource->send('error', array('message' => $l10n->t('Error while downloading %s to %s', array($source, $target))));
  101. }
  102. if (is_resource($sourceStream)) {
  103. fclose($sourceStream);
  104. }
  105. $eventSource->close();
  106. exit();
  107. } else {
  108. $success = false;
  109. if (!$content) {
  110. $templateManager = OC_Helper::getFileTemplateManager();
  111. $mimeType = OC_Helper::getMimetypeDetector()->detectPath($target);
  112. $content = $templateManager->getTemplate($mimeType);
  113. }
  114. if($content) {
  115. $success = \OC\Files\Filesystem::file_put_contents($target, $content);
  116. } else {
  117. $success = \OC\Files\Filesystem::touch($target);
  118. }
  119. if($success) {
  120. $meta = \OC\Files\Filesystem::getFileInfo($target);
  121. OCP\JSON::success(array('data' => \OCA\Files\Helper::formatFileInfo($meta)));
  122. exit();
  123. }
  124. }
  125. OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the file') )));