|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// Init owncloud |
|
4
|
|
|
global $eventSource; |
|
5
|
|
|
|
|
6
|
|
|
if(!OC_User::isLoggedIn()) { |
|
7
|
|
|
exit; |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
\OC::$server->getSession()->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 = \OC::$server->createEventSource(); |
|
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) || $filesize === 0) { |
|
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
|
|
|
|
|
50
|
|
|
$l10n = \OC::$server->getL10N('files'); |
|
51
|
|
|
|
|
52
|
|
|
$result = array( |
|
53
|
|
|
'success' => false, |
|
54
|
|
|
'data' => NULL |
|
55
|
|
|
); |
|
56
|
|
|
$trimmedFileName = trim($filename); |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
if($trimmedFileName === '') { |
|
|
|
|
|
|
59
|
|
|
$result['data'] = array('message' => (string)$l10n->t('File name cannot be empty.')); |
|
60
|
|
|
OCP\JSON::error($result); |
|
61
|
|
|
exit(); |
|
62
|
|
|
} |
|
63
|
|
|
if($trimmedFileName === '.' || $trimmedFileName === '..') { |
|
64
|
|
|
$result['data'] = array('message' => (string)$l10n->t('"%s" is an invalid file name.', $trimmedFileName)); |
|
|
|
|
|
|
65
|
|
|
OCP\JSON::error($result); |
|
66
|
|
|
exit(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
if(!OCP\Util::isValidFileName($filename)) { |
|
|
|
|
|
|
70
|
|
|
$result['data'] = array('message' => (string)$l10n->t("Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.")); |
|
71
|
|
|
OCP\JSON::error($result); |
|
72
|
|
|
exit(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
if (!\OC\Files\Filesystem::file_exists($dir . '/')) { |
|
|
|
|
|
|
76
|
|
|
$result['data'] = array('message' => (string)$l10n->t( |
|
77
|
|
|
'The target folder has been moved or deleted.'), |
|
78
|
|
|
'code' => 'targetnotfound' |
|
79
|
|
|
); |
|
80
|
|
|
OCP\JSON::error($result); |
|
81
|
|
|
exit(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.