newfile.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // Init owncloud
  3. global $eventSource;
  4. if(!OC_User::isLoggedIn()) {
  5. exit;
  6. }
  7. session_write_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. if(trim($filename) === '') {
  46. $result['data'] = array('message' => (string)$l10n->t('File name cannot be empty.'));
  47. OCP\JSON::error($result);
  48. exit();
  49. }
  50. if(strpos($filename, '/') !== false) {
  51. $result['data'] = array('message' => (string)$l10n->t('File name must not contain "/". Please choose a different name.'));
  52. OCP\JSON::error($result);
  53. exit();
  54. }
  55. if (!\OC\Files\Filesystem::file_exists($dir . '/')) {
  56. $result['data'] = array('message' => (string)$l10n->t(
  57. 'The target folder has been moved or deleted.'),
  58. 'code' => 'targetnotfound'
  59. );
  60. OCP\JSON::error($result);
  61. exit();
  62. }
  63. //TODO why is stripslashes used on foldername in newfolder.php but not here?
  64. $target = $dir.'/'.$filename;
  65. if (\OC\Files\Filesystem::file_exists($target)) {
  66. $result['data'] = array('message' => (string)$l10n->t(
  67. 'The name %s is already used in the folder %s. Please choose a different name.',
  68. array($filename, $dir))
  69. );
  70. OCP\JSON::error($result);
  71. exit();
  72. }
  73. if($source) {
  74. if(substr($source, 0, 8)!='https://' and substr($source, 0, 7)!='http://') {
  75. OCP\JSON::error(array('data' => array('message' => $l10n->t('Not a valid source'))));
  76. exit();
  77. }
  78. if (!ini_get('allow_url_fopen')) {
  79. $eventSource->send('error', array('message' => $l10n->t('Server is not allowed to open URLs, please check the server configuration')));
  80. $eventSource->close();
  81. exit();
  82. }
  83. $ctx = stream_context_create(null, array('notification' =>'progress'));
  84. $sourceStream=@fopen($source, 'rb', false, $ctx);
  85. $result = 0;
  86. if (is_resource($sourceStream)) {
  87. $result=\OC\Files\Filesystem::file_put_contents($target, $sourceStream);
  88. }
  89. if($result) {
  90. $meta = \OC\Files\Filesystem::getFileInfo($target);
  91. $mime=$meta['mimetype'];
  92. $id = $meta['fileid'];
  93. $eventSource->send('success', array('mime' => $mime, 'size' => \OC\Files\Filesystem::filesize($target), 'id' => $id, 'etag' => $meta['etag']));
  94. } else {
  95. $eventSource->send('error', array('message' => $l10n->t('Error while downloading %s to %s', array($source, $target))));
  96. }
  97. if (is_resource($sourceStream)) {
  98. fclose($sourceStream);
  99. }
  100. $eventSource->close();
  101. exit();
  102. } else {
  103. $success = false;
  104. if (!$content) {
  105. $templateManager = OC_Helper::getFileTemplateManager();
  106. $mimeType = OC_Helper::getMimetypeDetector()->detectPath($target);
  107. $content = $templateManager->getTemplate($mimeType);
  108. }
  109. if($content) {
  110. $success = \OC\Files\Filesystem::file_put_contents($target, $content);
  111. } else {
  112. $success = \OC\Files\Filesystem::touch($target);
  113. }
  114. if($success) {
  115. $meta = \OC\Files\Filesystem::getFileInfo($target);
  116. $id = $meta['fileid'];
  117. $mime = $meta['mimetype'];
  118. $size = $meta['size'];
  119. OCP\JSON::success(array('data' => array(
  120. 'id' => $id,
  121. 'mime' => $mime,
  122. 'size' => $size,
  123. 'content' => $content,
  124. 'etag' => $meta['etag'],
  125. )));
  126. exit();
  127. }
  128. }
  129. OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the file') )));