Issues (1358)

modules/Uploader/events.php (13 issues)

1
<?php
2
/**
3
 * @package  Uploader
4
 * @category modules
5
 * @author   Nazar Mokrynskyi <[email protected]>
6
 * @license  0BSD
7
 */
8
namespace cs;
9
10
Event::instance()
11
	->on(
12
		'System/Page/render/before',
13
		function () {
14
			if (!Config::instance()->module('Uploader')->enabled()) {
15
				return;
16
			}
17
			$Config        = Config::instance();
18
			$Page          = Page::instance();
19
			$module_data   = $Config->module('Uploader');
20
			$max_file_size = (int)$module_data->max_file_size;
0 ignored issues
show
Bug Best Practice introduced by
The property max_file_size does not exist on cs\Config\Module_Properties. Since you implemented __get, consider adding a @property annotation.
Loading history...
21
			switch (substr(strtolower($module_data->max_file_size), -2)) {
0 ignored issues
show
It seems like $module_data->max_file_size can also be of type false; however, parameter $str of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

21
			switch (substr(strtolower(/** @scrutinizer ignore-type */ $module_data->max_file_size), -2)) {
Loading history...
22
				case 'gb':
23
					$max_file_size *= 1024;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
24
				case 'mb':
25
					$max_file_size *= 1024;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
26
				case 'kb':
27
					$max_file_size *= 1024;
28
			}
29
			$Page->config(
30
				[
31
					'max_file_size' => $max_file_size
32
				],
33
				'cs.uploader'
34
			);
35
		}
36
	)
37
	->on(
38
		'System/upload_files/add_tag',
39
		function ($data) {
40
			if (!Config::instance()->module('Uploader')->enabled()) {
41
				return true;
42
			}
43
			if (!isset($data['url'], $data['tag'])) {
44
				return false;
45
			}
46
			$module_data = Config::instance()->module('Uploader');
47
			$storage     = Storage::instance()->storage($module_data->storage('files'));
48
			if (mb_strpos($data['url'], $storage->base_url()) !== 0) {
0 ignored issues
show
The method base_url() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

48
			if (mb_strpos($data['url'], $storage->/** @scrutinizer ignore-call */ base_url()) !== 0) {
Loading history...
49
				return false;
50
			}
51
			$cdb = DB::instance()->db_prime($module_data->db('files'));
52
			$id  = $cdb->qfs(
0 ignored issues
show
The method qfs() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

52
			/** @scrutinizer ignore-call */ 
53
   $id  = $cdb->qfs(
Loading history...
53
				"SELECT `id`
54
				FROM `[prefix]uploader_files`
55
				WHERE `url` = '%s'
56
				LIMIT 1",
57
				$data['url']
58
			);
59
			if (!$id) {
60
				return false;
61
			}
62
			return $cdb->q(
0 ignored issues
show
The method q() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

62
			return $cdb->/** @scrutinizer ignore-call */ q(
Loading history...
63
				"INSERT IGNORE INTO `[prefix]uploader_files_tags`
64
					(`id`, `tag`)
65
				VALUES
66
					('%s', '%s')",
67
				$id,
68
				$data['tag']
69
			);
70
		}
71
	)
72
	->on(
73
		'admin/System/modules/uninstall/before',
74
		function ($data) {
75
			if ($data['name'] != 'Uploader') {
76
				return;
77
			}
78
			$module_data = Config::instance()->module('Uploader');
79
			$storage     = Storage::instance()->storage($module_data->storage('files'));
80
			$cdb         = DB::instance()->db($module_data->db('files'));
81
			unset($module_data);
82
			if (!$storage || !$cdb) {
83
				return;
84
			}
85
			$files = $cdb->q(
86
				'SELECT `source`
87
				FROM `[prefix]uploader_files`'
88
			);
89
			while ($f = $cdb->f($files, true)) {
0 ignored issues
show
The method f() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

89
			while ($f = $cdb->/** @scrutinizer ignore-call */ f($files, true)) {
Loading history...
90
				$storage->unlink($f);
0 ignored issues
show
The method unlink() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

90
				$storage->/** @scrutinizer ignore-call */ 
91
              unlink($f);
Loading history...
91
			}
92
			if ($storage->is_dir('Uploader')) {
0 ignored issues
show
The method is_dir() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

92
			if ($storage->/** @scrutinizer ignore-call */ is_dir('Uploader')) {
Loading history...
93
				$storage->rmdir('Uploader');
0 ignored issues
show
The method rmdir() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

93
				$storage->/** @scrutinizer ignore-call */ 
94
              rmdir('Uploader');
Loading history...
94
			}
95
		}
96
	)
97
	->on(
98
		'System/upload_files/del_tag',
99
		function ($data) {
100
			if (!isset($data['url']) && !isset($data['tag'])) {
101
				return;
102
			}
103
			$module_data = Config::instance()->module('Uploader');
104
			$storage     = Storage::instance()->storage($module_data->storage('files'));
105
			if (isset($data['url']) && mb_strpos($data['url'], $storage->base_url()) !== 0) {
106
				return;
107
			}
108
			$cdb = DB::instance()->db_prime($module_data->db('files'));
109
			if (isset($data['url']) && !isset($data['tag'])) {
110
				$cdb->q(
111
					"DELETE FROM `[prefix]uploader_files_tags`
112
					WHERE `id` IN(
113
						SELECT `id`
114
						FROM `[prefix]uploader_files`
115
						WHERE `url` = '%s'
116
					)",
117
					$data['url']
118
				);
119
			} elseif (isset($data['url'], $data['tag'])) {
120
				$cdb->q(
121
					"DELETE FROM `[prefix]uploader_files_tags`
122
					WHERE
123
						`id`	IN(
124
							SELECT `id`
125
							FROM `[prefix]uploader_files`
126
							WHERE `url` = '%s'
127
						) AND
128
						`tag`	= '%s'",
129
					$data['url'],
130
					$data['tag']
131
				);
132
			} else {
133
				$cdb->q(
134
					"DELETE FROM `[prefix]uploader_files_tags`
135
					WHERE `tag` LIKE '%s'",
136
					$data['tag']
137
				);
138
			}
139
		}
140
	)
141
	->on(
142
		'admin/System/modules/install/after',
143
		function ($data) {
144
			if ($data['name'] == 'Uploader') {
145
				$Config                                        = Config::instance();
146
				$Config->module('Uploader')->max_file_size     = '5mb';
0 ignored issues
show
Bug Best Practice introduced by
The property max_file_size does not exist on cs\Config\Module_Properties. Since you implemented __set, consider adding a @property annotation.
Loading history...
147
				$Config->module('Uploader')->confirmation_time = '900';
0 ignored issues
show
Bug Best Practice introduced by
The property confirmation_time does not exist on cs\Config\Module_Properties. Since you implemented __set, consider adding a @property annotation.
Loading history...
148
			}
149
		}
150
	);
151