1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Init owncloud |
4
|
|
|
global $eventSource; |
5
|
|
|
|
6
|
|
|
if(!OC_User::isLoggedIn()) { |
|
|
|
|
7
|
|
|
exit; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
\OC::$session->close(); |
11
|
|
|
|
12
|
|
|
// Get the params |
13
|
|
|
$dir = isset( $_REQUEST['dir'] ) ? '/'.trim($_REQUEST['dir'], '/\\') : ''; |
14
|
|
|
$filename = isset( $_REQUEST['filename'] ) ? trim($_REQUEST['filename'], '/\\') : ''; |
15
|
|
|
$content = isset( $_REQUEST['content'] ) ? $_REQUEST['content'] : ''; |
16
|
|
|
$source = isset( $_REQUEST['source'] ) ? trim($_REQUEST['source'], '/\\') : ''; |
17
|
|
|
|
18
|
|
|
if($source) { |
19
|
|
|
$eventSource=new OC_EventSource(); |
20
|
|
|
} else { |
21
|
|
|
OC_JSON::callCheck(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function progress($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { |
|
|
|
|
25
|
|
|
static $filesize = 0; |
26
|
|
|
static $lastsize = 0; |
27
|
|
|
global $eventSource; |
28
|
|
|
|
29
|
|
|
switch($notification_code) { |
30
|
|
|
case STREAM_NOTIFY_FILE_SIZE_IS: |
31
|
|
|
$filesize = $bytes_max; |
32
|
|
|
break; |
33
|
|
|
|
34
|
|
|
case STREAM_NOTIFY_PROGRESS: |
35
|
|
|
if ($bytes_transferred > 0) { |
36
|
|
|
if (!isset($filesize)) { |
37
|
|
|
} else { |
38
|
|
|
$progress = (int)(($bytes_transferred/$filesize)*100); |
39
|
|
|
if($progress>$lastsize) { //limit the number or messages send |
40
|
|
|
$eventSource->send('progress', $progress); |
41
|
|
|
} |
42
|
|
|
$lastsize=$progress; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
break; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$l10n = \OC_L10n::get('files'); |
50
|
|
|
|
51
|
|
|
$result = array( |
52
|
|
|
'success' => false, |
53
|
|
|
'data' => NULL |
54
|
|
|
); |
55
|
|
|
$trimmedFileName = trim($filename); |
56
|
|
|
|
57
|
|
View Code Duplication |
if($trimmedFileName === '') { |
|
|
|
|
58
|
|
|
$result['data'] = array('message' => (string)$l10n->t('File name cannot be empty.')); |
59
|
|
|
OCP\JSON::error($result); |
60
|
|
|
exit(); |
61
|
|
|
} |
62
|
|
|
if($trimmedFileName === '.' || $trimmedFileName === '..') { |
63
|
|
|
$result['data'] = array('message' => (string)$l10n->t('"%s" is an invalid file name.', $trimmedFileName)); |
|
|
|
|
64
|
|
|
OCP\JSON::error($result); |
65
|
|
|
exit(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
if(!OCP\Util::isValidFileName($filename)) { |
|
|
|
|
69
|
|
|
$result['data'] = array('message' => (string)$l10n->t("Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.")); |
70
|
|
|
OCP\JSON::error($result); |
71
|
|
|
exit(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
if (!\OC\Files\Filesystem::file_exists($dir . '/')) { |
|
|
|
|
75
|
|
|
$result['data'] = array('message' => (string)$l10n->t( |
76
|
|
|
'The target folder has been moved or deleted.'), |
77
|
|
|
'code' => 'targetnotfound' |
78
|
|
|
); |
79
|
|
|
OCP\JSON::error($result); |
80
|
|
|
exit(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
//TODO why is stripslashes used on foldername in newfolder.php but not here? |
84
|
|
|
$target = $dir.'/'.$filename; |
85
|
|
|
|
86
|
|
View Code Duplication |
if (\OC\Files\Filesystem::file_exists($target)) { |
|
|
|
|
87
|
|
|
$result['data'] = array('message' => (string)$l10n->t( |
88
|
|
|
'The name %s is already used in the folder %s. Please choose a different name.', |
89
|
|
|
array($filename, $dir)) |
90
|
|
|
); |
91
|
|
|
OCP\JSON::error($result); |
92
|
|
|
exit(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if($source) { |
96
|
|
|
$httpHelper = \OC::$server->getHTTPHelper(); |
97
|
|
|
if(!$httpHelper->isHTTPURL($source)) { |
98
|
|
|
OCP\JSON::error(array('data' => array('message' => $l10n->t('Not a valid source')))); |
99
|
|
|
exit(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!ini_get('allow_url_fopen')) { |
103
|
|
|
$eventSource->send('error', array('message' => $l10n->t('Server is not allowed to open URLs, please check the server configuration'))); |
104
|
|
|
$eventSource->close(); |
105
|
|
|
exit(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$source = $httpHelper->getFinalLocationOfURL($source); |
109
|
|
|
|
110
|
|
|
$ctx = stream_context_create(\OC::$server->getHTTPHelper()->getDefaultContextArray(), array('notification' =>'progress')); |
111
|
|
|
|
112
|
|
|
$sourceStream=@fopen($source, 'rb', false, $ctx); |
113
|
|
|
$result = 0; |
114
|
|
|
if (is_resource($sourceStream)) { |
115
|
|
|
$meta = stream_get_meta_data($sourceStream); |
116
|
|
|
if (isset($meta['wrapper_data']) && is_array($meta['wrapper_data'])) { |
117
|
|
|
//check stream size |
118
|
|
|
$storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); |
119
|
|
|
$freeSpace = $storageStats['freeSpace']; |
120
|
|
|
|
121
|
|
|
foreach($meta['wrapper_data'] as $header) { |
122
|
|
|
if (strpos($header, ':') === false){ |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
list($name, $value) = explode(':', $header); |
126
|
|
|
if ('content-length' === strtolower(trim($name))) { |
127
|
|
|
$length = (int) trim($value); |
128
|
|
|
|
129
|
|
|
if ($length > $freeSpace) { |
130
|
|
|
$delta = $length - $freeSpace; |
131
|
|
|
$humanDelta = OCP\Util::humanFileSize($delta); |
132
|
|
|
|
133
|
|
|
$eventSource->send('error', array('message' => (string)$l10n->t('The file exceeds your quota by %s', array($humanDelta)))); |
134
|
|
|
$eventSource->close(); |
135
|
|
|
fclose($sourceStream); |
136
|
|
|
exit(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
$result=\OC\Files\Filesystem::file_put_contents($target, $sourceStream); |
142
|
|
|
} |
143
|
|
|
if($result) { |
144
|
|
|
$meta = \OC\Files\Filesystem::getFileInfo($target); |
145
|
|
|
$data = \OCA\Files\Helper::formatFileInfo($meta); |
146
|
|
|
$eventSource->send('success', $data); |
147
|
|
|
} else { |
148
|
|
|
$eventSource->send('error', array('message' => $l10n->t('Error while downloading %s to %s', array($source, $target)))); |
149
|
|
|
} |
150
|
|
|
if (is_resource($sourceStream)) { |
151
|
|
|
fclose($sourceStream); |
152
|
|
|
} |
153
|
|
|
$eventSource->close(); |
154
|
|
|
exit(); |
155
|
|
|
} else { |
156
|
|
|
$success = false; |
157
|
|
|
if (!$content) { |
158
|
|
|
$templateManager = OC_Helper::getFileTemplateManager(); |
159
|
|
|
$mimeType = OC_Helper::getMimetypeDetector()->detectPath($target); |
160
|
|
|
$content = $templateManager->getTemplate($mimeType); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if($content) { |
164
|
|
|
$success = \OC\Files\Filesystem::file_put_contents($target, $content); |
165
|
|
|
} else { |
166
|
|
|
$success = \OC\Files\Filesystem::touch($target); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if($success) { |
170
|
|
|
$meta = \OC\Files\Filesystem::getFileInfo($target); |
171
|
|
|
OCP\JSON::success(array('data' => \OCA\Files\Helper::formatFileInfo($meta))); |
172
|
|
|
exit(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the file') ))); |
177
|
|
|
|
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.