|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @subpackage System module |
|
5
|
|
|
* @category modules |
|
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
7
|
|
|
* @license 0BSD |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace cs\modules\System\api\Controller\admin; |
|
10
|
|
|
use |
|
11
|
|
|
cs\ExitException, |
|
12
|
|
|
cs\Language, |
|
13
|
|
|
cs\Request, |
|
14
|
|
|
cs\Session; |
|
15
|
|
|
|
|
16
|
|
|
trait upload { |
|
17
|
|
|
/** |
|
18
|
|
|
* @return array |
|
19
|
|
|
* |
|
20
|
|
|
* @throws ExitException |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function admin_upload_post () { |
|
23
|
|
|
$file = Request::instance()->files('file'); |
|
24
|
|
|
if (!$file) { |
|
25
|
|
|
throw new ExitException(400); |
|
26
|
|
|
} |
|
27
|
|
|
$L = Language::prefix('system_admin_'); |
|
28
|
|
|
switch ($file['error']) { |
|
29
|
|
|
case UPLOAD_ERR_INI_SIZE: |
|
30
|
|
|
case UPLOAD_ERR_FORM_SIZE: |
|
31
|
|
|
throw new ExitException($L->file_too_large, 400); |
|
|
|
|
|
|
32
|
|
|
case UPLOAD_ERR_NO_TMP_DIR: |
|
33
|
|
|
throw new ExitException($L->temporary_folder_is_missing, 400); |
|
|
|
|
|
|
34
|
|
|
case UPLOAD_ERR_CANT_WRITE: |
|
35
|
|
|
throw new ExitException($L->cant_write_file_to_disk, 500); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
if ($file['error'] != UPLOAD_ERR_OK) { |
|
38
|
|
|
throw new ExitException(400); |
|
39
|
|
|
} |
|
40
|
|
|
$target_directory = TEMP.'/System/admin'; |
|
41
|
|
|
if (!@mkdir($target_directory, 0770, true) && !is_dir($target_directory)) { |
|
42
|
|
|
throw new ExitException(500); |
|
43
|
|
|
} |
|
44
|
|
|
$tmp_filename = Session::instance()->get_id().'.phar'; |
|
|
|
|
|
|
45
|
|
|
$tmp_location = "$target_directory/$tmp_filename"; |
|
46
|
|
|
// Cleanup |
|
47
|
|
|
get_files_list( |
|
48
|
|
|
$target_directory, |
|
49
|
|
|
'/.*\.phar$/', |
|
50
|
|
|
'f', |
|
51
|
|
|
true, |
|
52
|
|
|
false, |
|
53
|
|
|
false, |
|
54
|
|
|
false, |
|
55
|
|
|
false, |
|
56
|
|
|
function ($file) { |
|
57
|
|
|
unlink($file); |
|
58
|
|
|
} |
|
59
|
|
|
); |
|
60
|
|
|
if (!copy($file['tmp_name'], $tmp_location)) { |
|
61
|
|
|
throw new ExitException(500); |
|
62
|
|
|
} |
|
63
|
|
|
$tmp_dir = "phar://$tmp_location"; |
|
64
|
|
|
if (!file_exists("$tmp_dir/meta.json")) { |
|
65
|
|
|
unlink($tmp_location); |
|
66
|
|
|
throw new ExitException(400); |
|
67
|
|
|
} |
|
68
|
|
|
$meta = file_get_json("$tmp_dir/meta.json"); |
|
69
|
|
|
if (!isset($meta['category'], $meta['package'], $meta['version'])) { |
|
70
|
|
|
unlink($tmp_location); |
|
71
|
|
|
throw new ExitException(400); |
|
72
|
|
|
} |
|
73
|
|
|
return $meta; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|