This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
upload_addons.php
474 lines (367 loc) · 13.5 KB
/
upload_addons.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
//prohibit unauthorized access
require 'core/access.php';
require_once 'core/pclzip.lib.php';
$danger_zone_lifetime = 300;
if($_POST['confirm_danger_zone'] == $_SESSION['user_nick']) {
$_SESSION['confirmed_danger_zone'] = 'confirmed';
$_SESSION['confirmed_danger_zone_time'] = time();
}
if((time() - $_SESSION['confirmed_danger_zone_time']) > $danger_zone_lifetime) {
$_SESSION['confirmed_danger_zone'] = '';
}
if($_SESSION['confirmed_danger_zone'] !== 'confirmed') {
/* confirm that you know it is dangerous here */
echo '<form action="?tn=moduls&sub=u" method="POST">';
echo '<div class="alert alert-danger mb-4">';
echo '<p>'.$lang['section_is_beta'].'</p>';
echo '<p>'.$lang['section_is_danger_zone'].'</p>';
echo '<button type="submit" class="btn btn-danger" name="confirm_danger_zone" value="'.$_SESSION['user_nick'].'">OKAY, I GET IT</button>';
echo '</div>';
echo '</form><hr>';
} else {
echo '<fieldset class="mt-4">';
echo '<legend>Upload</legend>';
echo '<div class="row">';
echo '<div class="col-md-4">';
echo '<div class="well well-sm">';
echo '<form action="core/files.upload-script.php" id="dropAddons" class="dropzone dropzone-plugin dropzone-sm">';
echo '<input type="hidden" name="upload_type" value="plugin">';
echo '<input type="hidden" name="csrf_token" value="'.$_SESSION['token'].'">';
echo '<div class="fallback"><input name="file" type="file"></div>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '<div class="col-md-4">';
echo '<div class="well well-sm">';
echo '<form action="core/files.upload-script.php" id="dropAddons" class="dropzone dropzone-module dropzone-sm">';
echo '<input type="hidden" name="upload_type" value="module">';
echo '<input type="hidden" name="csrf_token" value="'.$_SESSION['token'].'">';
echo '<div class="fallback"><input name="file" type="file"></div>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '<div class="col-md-4">';
echo '<div class="well well-sm">';
echo '<form action="core/files.upload-script.php" id="dropAddons" class="dropzone dropzone-theme dropzone-sm">';
echo '<input type="hidden" name="upload_type" value="theme">';
echo '<input type="hidden" name="csrf_token" value="'.$_SESSION['token'].'">';
echo '<div class="fallback"><input name="file" type="file"></div>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</fieldset>';
}
/* delete uploaded zip files */
if(isset($_POST['delete_uploaded_zip'])) {
$file = basename($_POST['delete_uploaded_zip']);
if($_POST['dir'] == 't') {
$dir = 'themes';
} else if($_POST['dir'] == 'm') {
$dir = 'modules';
} else {
$dir = 'plugins';
}
if(is_file("../upload/$dir/$file")) {
unlink("../upload/$dir/$file");
}
}
/* check if we can write in /styles/, /modules/ and /content/plugins/ /*/
if(!is_writable('../styles/')) {
echo '<p class="alert alert-danger">'.sprintf($lang['dir_must_be_writable'],'/styles/').'</p>';
}
if(!is_writable('../modules/')) {
echo '<p class="alert alert-danger">'.sprintf($lang['dir_must_be_writable'],'/modules/').'</p>';
}
if(!is_writable('../content/plugins/')) {
echo '<p class="alert alert-danger">'.sprintf($lang['dir_must_be_writable'],'/content/plugins/').'</p>';
}
/**
* plugins, themes and modules must be contained in a zip archive
* the zip archive for themes must contain a file 'contents.php'
* in this file you can define which files have to be copied
* the root folder of your sources must be specified (relative path)
*
* example:
* $instRootDir = 'myThemefolder';
* $instFiles = 'all';
*
*/
/**
* install plugins
* 1. extract zip file
* 2. get files from extracted directory
* copy the files from ../upload/plugins/ to content/plugins/)
*
*/
if(isset($_POST['install_uploaded_plg'])) {
if(!is_dir("../upload/plugins/extract")) {
mkdir("../upload/plugins/extract", 0777);
}
unset($all_files);
$plugin = basename($_POST['install_uploaded_plg']);
$archive = new PclZip("../upload/plugins/$plugin");
$list = $archive->extract(
PCLZIP_OPT_PATH, '../upload/plugins/extract',
PCLZIP_OPT_STOP_ON_ERROR,
PCLZIP_OPT_SET_CHMOD, 0777
);
if($list == 0) {
echo "ERROR : ".$archive->errorInfo(true);
}
$extracted = basename("$plugin",".zip");
if(is_dir("../upload/plugins/extract/$extracted")) {
$all_files = fc_scandir_rec("../upload/plugins/extract/$extracted");
} else {
echo '<div class="alert alert-danger">No Source found: '. $extracted .'</div>';
}
echo '<div class="scroll-container">';
if(is_array($all_files)) {
foreach($all_files as $f) {
$i++;
$target = "../content/plugins/" . substr($f, strlen("../upload/plugins/extract/$extracted/"));
$status = copy_recursive("$f","$target");
if($status == 'success') {
$show_status = "<span class='badge badge-success'>ok</span>";
} else {
$show_status = '<span class="badge bg-danger">'.$status.'</span>';
$cnt_errors++;
}
echo '<dl class="row">';
echo '<dt class="col-sm-1 text-right">'.$i.'</dt>';
echo '<dd class="col-sm-11">from: <code>extract/(..)'. basename($f) .'</code> to: <code>'. $target .' '. $show_status .'</code></dd>';
echo '</dl>';
}
if($cnt_errors < 1) {
rmdir_recursive("../upload/plugins/extract");
echo '<div class="alert alert-success">Plugin installed</div>';
}
}
echo '</div>';
}
/**
* install modules
* 1. extract zip file
* 2. find xyz.mod directory
* 3. copy xyz.mod and it's contents to /modules/
*/
if(isset($_POST['install_uploaded_mod'])) {
if(!is_dir("../upload/modules/extract")) {
mkdir("../upload/modules/extract", 0777);
}
$mod = basename($_POST['install_uploaded_mod']);
$archive = new PclZip("../upload/modules/$mod");
$list = $archive->extract(
PCLZIP_OPT_PATH, '../upload/modules/extract',
PCLZIP_OPT_STOP_ON_ERROR,
PCLZIP_OPT_SET_CHMOD, 0777
);
if($list == 0) {
echo "ERROR : ".$archive->errorInfo(true);
}
$extracted = basename("$mod",".zip");
if(is_dir("../upload/modules/extract/$extracted")) {
$all_files = fc_scandir_rec("../upload/modules/extract/$extracted");
} else {
echo '<div class="alert alert-danger">No Source found: '. $extracted .'</div>';
}
echo '<div class="scroll-container">';
if(is_array($all_files)) {
foreach($all_files as $f) {
$i++;
$target = "../modules/" . substr($f, strlen("../upload/modules/extract/$extracted/"));
$status = copy_recursive("$f","$target");
if($status == 'success') {
$show_status = "<span class='badge badge-success'>ok</span>";
} else {
$show_status = '<span class="badge bg-danger">'.$status.'</span>';
$cnt_errors++;
}
echo '<dl class="row">';
echo '<dt class="col-sm-1 text-right">'.$i.'</dt>';
echo '<dd class="col-sm-11">from: <code>extract/(..)'. basename($f) .'</code> to: <code>'. $target .' '. $show_status .'</code></dd>';
echo '</dl>';
}
if($cnt_errors < 1) {
rmdir_recursive("../upload/modules/extract");
echo '<div class="alert alert-success">Module installed</div>';
}
}
echo '</div>';
}
/**
* install themes
* 1. extract zip file
* 2. find theme folder from contents.php
* 3. copy theme folder and it's contents to /styles/
*/
if(isset($_POST['install_uploaded_tpl'])) {
if(!is_dir("../upload/themes/extract")) {
mkdir("../upload/themes/extract", 0777);
}
unset($all_files);
$theme = basename($_POST['install_uploaded_tpl']);
$archive = new PclZip("../upload/themes/$theme");
$list = $archive->extract(
PCLZIP_OPT_PATH, '../upload/themes/extract',
PCLZIP_OPT_STOP_ON_ERROR,
PCLZIP_OPT_SET_CHMOD, 0777
);
if($list == 0) {
echo "ERROR : ".$archive->errorInfo(true);
}
$extracted = basename("$theme",".zip");
if(is_dir("../upload/themes/extract/$extracted")) {
if(is_file("../upload/themes/extract/$extracted/contents.php")) {
include '../upload/themes/extract/'.$extracted.'/contents.php';
// themes root folder ($instRootDir) must be defined in contents.php
$all_files = fc_scandir_rec("../upload/themes/extract/$extracted/$instRootDir");
} else {
echo '<div class="alert alert-danger">This is not a compatible Theme</div>';
}
} else {
echo '<div class="alert alert-danger">No Source found: '. $extracted .'</div>';
}
echo '<div class="scroll-container">';
if(is_array($all_files)) {
foreach($all_files as $f) {
$i++;
$target = "../styles/" . substr($f, strlen("../upload/themes/extract/$extracted/"));
$status = copy_recursive("$f","$target");
if($status == 'success') {
$show_status = "<span class='badge badge-success'>ok</span>";
} else {
$show_status = '<span class="badge bg-danger">'.$status.'</span>';
$cnt_errors++;
}
echo '<dl class="row">';
echo '<dt class="col-sm-1 text-right">'.$i.'</dt>';
echo '<dd class="col-sm-11">from: <code>extract/(..)'. basename($f) .'</code> to: <code>'. $target .' '. $show_status .'</code></dd>';
echo '</dl>';
}
if($cnt_errors < 1) {
rmdir_recursive("../upload/themes/extract");
echo '<div class="alert alert-success">Theme installed</div>';
}
}
echo '</div>';
}
/* list uploaded files */
if($_SESSION['confirmed_danger_zone'] == 'confirmed') {
if(is_dir('../upload')) {
$all_uploads = fc_scandir_rec('../upload');
echo '<fieldset class="mt-3">';
echo '<legend>'.$lang['label_ready_to_install'].'</legend>';
if(count($all_uploads) < 1) {
echo '<p class="text-muted">'.$lang['msg_nothing_to_install'].' <a href="?tn=moduls&sub=u" class="btn btn-link">'.$icon['sync_alt'].' reload</a></p>';
} else {
echo '<table class="table table-condensed">';
foreach($all_uploads as $upload) {
$this_pathinfo = pathinfo($upload);
$filemtime = date("Y-m-d H:i:s",filemtime($upload));
$pathinfo = print_r($this_pathinfo,true);
if($this_pathinfo['dirname'] == '../upload/modules') {
echo '<tr>';
echo '<td>Module:</td><td><strong>'.$this_pathinfo['basename'].'</strong> <small>Upload time: '.$filemtime.'</small></td>';
echo '<td>';
echo '<div class="btn-group float-end">';
echo '<form action="?tn=moduls&sub=u" method="POST">';
echo '<button class="btn btn-fc text-success" type="submit" name="install_uploaded_mod" value="'.$this_pathinfo['basename'].'">Install</button>';
echo '<button class="btn btn-fc text-danger" type="submit" name="delete_uploaded_zip" value="'.$this_pathinfo['basename'].'">Remove</button>';
echo '<input type="hidden" name="dir" value="m">';
echo '<input type="hidden" name="csrf_token" value="'.$_SESSION['token'].'">';
echo '</form>';
echo '</div>';
echo '</td>';
echo '</tr>';
} else if($this_pathinfo['dirname'] == '../upload/plugins') {
echo '<tr>';
echo '<td>Plugin:</td><td><strong>'.$this_pathinfo['basename'].'</strong> <small>Upload time: '.$filemtime.'</small></td>';
echo '<td>';
echo '<div class="btn-group float-end">';
echo '<form action="?tn=moduls&sub=u" method="POST">';
echo '<button class="btn btn-fc text-success" type="submit" name="install_uploaded_plg" value="'.$this_pathinfo['basename'].'">Install</button>';
echo '<button class="btn btn-fc text-danger" type="submit" name="delete_uploaded_zip" value="'.$this_pathinfo['basename'].'">Remove</button>';
echo '<input type="hidden" name="dir" value="p">';
echo '<input type="hidden" name="csrf_token" value="'.$_SESSION['token'].'">';
echo '</form>';
echo '</div>';
echo '</td>';
echo '</tr>';
} else if($this_pathinfo['dirname'] == '../upload/themes') {
echo '<tr>';
echo '<td>Theme:</td><td><strong>'.$this_pathinfo['basename'].'</strong> <small>Upload time: '.$filemtime.'</small></td>';
echo '<td>';
echo '<div class="btn-group float-end">';
echo '<form action="?tn=moduls&sub=u" method="POST">';
echo '<button class="btn btn-fc text-success" type="submit" name="install_uploaded_tpl" value="'.$this_pathinfo['basename'].'">Install</button>';
echo '<button class="btn btn-fc text-danger" type="submit" name="delete_uploaded_zip" value="'.$this_pathinfo['basename'].'">Remove</button>';
echo '<input type="hidden" name="dir" value="t">';
echo '<input type="hidden" name="csrf_token" value="'.$_SESSION['token'].'">';
echo '</form>';
echo '</div>';
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
}
echo '<fieldset>';
}
}
/**
* copy/move directory with its including contents
*/
function copy_recursive($source, $target) {
$path_parts = pathinfo($target);
if(!is_dir($path_parts['dirname'])) {
mkdir($path_parts['dirname'], 0777, true);
}
if(is_dir($source)) {
if(!is_dir("$target")) {
mkdir("$target", 0777, true);
}
$dir = dir($source);
while(FALSE !== ($entry = $dir->read())) {
if($entry == '.' || $entry == '..') { continue; }
$sub = $source . '/' . $entry;
if(is_dir($sub)) {
@chmod("$sub", 0777);
copy_recursive($sub, $target . '/' . $entry);
continue;
}
copy($sub, $target . '/' . $entry);
}
$dir->close();
} else {
@chmod("$target", 0777);
@unlink("$target");
if(copy($source, $target)) {
return 'success';
} else {
$errors = error_get_last();
return $errors['message'];
}
}
}
/**
* delete directory (recursive)
*/
function rmdir_recursive($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if(filetype($dir."/".$object) == "dir") {
rmdir_recursive($dir."/".$object);
} else {
unlink($dir."/".$object);
}
}
}
reset($objects);
rmdir($dir);
}
}
?>