upload   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
ccs 0
cts 43
cp 0
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
C admin_upload_post() 0 52 12
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);
0 ignored issues
show
Bug Best Practice introduced by
The property file_too_large does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
32
			case UPLOAD_ERR_NO_TMP_DIR:
33
				throw new ExitException($L->temporary_folder_is_missing, 400);
0 ignored issues
show
Bug Best Practice introduced by
The property temporary_folder_is_missing does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
34
			case UPLOAD_ERR_CANT_WRITE:
35
				throw new ExitException($L->cant_write_file_to_disk, 500);
0 ignored issues
show
Bug Best Practice introduced by
The property cant_write_file_to_disk does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
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';
0 ignored issues
show
Bug introduced by
Are you sure cs\Session::instance()->get_id() of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
		$tmp_filename = /** @scrutinizer ignore-type */ Session::instance()->get_id().'.phar';
Loading history...
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