-
Notifications
You must be signed in to change notification settings - Fork 4
/
controller.php
260 lines (179 loc) · 6.6 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/*
* Copyright (c) Codiad, Rafasashi & beli3ver, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
error_reporting(0);
require_once('../../common.php');
checkSession();
require_once('./functions.php');
//need for rar filelist check
$error = false;
switch($_GET['action']) {
case 'extract':
if(isset($_GET['path'])){
$source = getWorkspacePath($_GET['path']);
$source_info=pathinfo($source);
if(!isset($source_info['extension'])||empty($source_info['extension'])){
echo '{"status":"error","message":"Not an archive"}';
}
else{
$des = dirname($source);
if($source_info['extension']=='zip') {
if(class_exists('ZipArchive') && $zip = new ZipArchive) {
if($res = $zip->open($source)){
$epath = '';
if(isset($_GET['epath'])){
$epath = trim($_GET['epath']);
}
if($epath==''||$epath=='/'||$epath==$source_info['basename']){
// extract all archive to the path we determined above
if($zip->extractTo($des)){
echo '{"status":"success","message":"Archive extracted"}';
}
else{
echo '{"status":"error","message":"Failed to extract contents"}';
}
}
else{
// extract epath to the path we determined above
if(substr($epath, -1)!=='/'){
//extract single file
if(copy("zip://".$source."#".$epath, $des.'/'.basename($epath)) === TRUE){
echo '{"status":"success","message":"Sub contents extracted"}';
}
else{
echo '{"status":"error","message":"Failed to extract sub contents"}';
}
}
else{
//extract sub directory recursively
$response='{"status":"success","message":"Sub contents extracted"}';
for($i = 0; $i < $zip->numFiles; $i++){
$info = $zip->statIndex($i);
$entry = $info['name'];
$is_dir=false;
if($info['crc'] == 0 && substr($entry, -1)=='/'){
$is_dir=true;
}
if(strpos($entry,$epath)===0){
//get branche path from targeted directory
$branche=explode($epath,$entry,2);
$branche=$branche[1];
if($is_dir){
if($branche!=''&&!is_dir($des.'/'.$branche)&&!mkdir($des.'/'.$branche,0755,true)){
$response= '{"status":"error","message":"Failed to create sub directory"}';
break;
}
}
elseif(!copy("zip://".$source."#".$entry, $des.'/'.$branche)){
$response= '{"status":"error","message":"Failed to copy sub file"}';
break;
}
}
}
echo $response;
}
}
$zip->close();
}
else {
echo '{"status":"error","message":"Could not open zip archive"}';
}
}
}
elseif($source_info['extension']=='tar') {
if(class_exists('PharData') && $tar = new PharData($source)) {
if($tar->extractTo($des)){
echo '{"status":"success","message":"File extracted"}';
}
else{
echo '{"status":"error","message":"Failed to extract contents"}';
}
}
else {
echo '{"status":"error","message":"PharData extension missing or cloud not open tar archive"}';
}
}
elseif($source_info['extension']=='gz') {
if(class_exists('PharData') && $gz = new PharData($source)) {
if($tar_gzip_files = $gz->decompress()) {
$tar_gzip = new PharData($tar_gzip_files);
if($$tar_gzip->extractTo($des)){
echo '{"status":"success","message":"File extracted"}';
}
else {
echo '{"status":"error","message":"Failed to extract the files"}';
}
}
else {
echo '{"status":"error","message":"Failed to extract the .gzip file"}';
}
}
else {
echo '{"status":"error","message":"PharData extension missing or cloud not open tar archive"}';
}
}
elseif($source_info['extension']=='bz2') {
if(class_exists('PharData') && $bz = new PharData($source)) {
if($tar_bzip2_files = $bz->decompress()) {
$tar_bzip2 = new PharData($tar_bzip2_files);
if($$tar_bzip2->extractTo($des)){
echo '{"status":"success","message":"File extracted"}';
}
else {
echo '{"status":"error","message":"Failed to extract the files"}';
}
}
else {
echo '{"status":"error","message":"Failed to extract the .bzip2 file"}';
}
}
else {
echo '{"status":"error","message":"PharData extension missing or cloud not open tar archive"}';
}
}
elseif($source_info['extension']=='rar') {
if(class_exists('rar_open') && $rar = new rar_open) {
if($res = $rar->open($source)){
$entries = rar_list($res);
try {
foreach ($entries as $entry) {
$entry->extract($des);
}
} catch (Exception $e) {
$error = true;
}
// extract it to the path we determined above
if($error === false){
echo '{"status":"success","message":"File extracted"}';
}
else{
echo '{"status":"error","message":"Failed to extract contents"}';
}
$rar->close();
}
else {
echo '{"status":"error","message":"Could not open rar archive"}';
}
}
else {
echo '{"status":"error","message":"Cloud not open rar archive"}';
}
}
else {
echo '{"status":"error","message":"Looks like a .'.$source_info['extension'].'"}';
}
}
}
else {
echo '{"status":"error","message":"Missing Parameter"}';
}
break;
default:
echo '{"status":"error","message":"No Type"}';
break;
}
?>