Test Failed
Pull Request — master (#236)
by
unknown
04:56
created

Folder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 07.03.2018
25
 * Time: 15:46
26
 */
27
28
class Folder {
29
30
	protected $folder = "";
31
	protected $row = array();
32
33
	protected static $load_count = 0;
34
35
	public function __construct($folder) {
36
		$this->folder = $folder;
37
	}
38
39
	public function load ($folder) {
40
		self::$load_count++;
41
42
		if (self::$load_count > 50) {
43
			throw new IllegalStateException("reached max allowed number of Folder::load() calls (to prevent recursion).");
44
		}
45
46
		//escape string
47
		//$folder = Database::getInstance()->escape($folder);
48
49
		if (empty($folder)) {
50
			$folder = "/";
51
		}
52
53
		if (Cache::contains("folder", "folder-" . $folder)) {
54
			$this->row = Cache::get("folder", "folder-" . $folder);
55
		} else {
56
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}folder` WHERE `folder` = :folder; ", array('folder' => $folder));
57
58
			if (!$row) {
59
				//load upper folder
60
				if (strcmp($folder, "/") !== 0) {
61
					//load upper dir
62
					$folder = self::getUpperDir($folder);
63
64
					$this->load($folder);
65
				} else {
66
					throw new IllegalStateException("no main folder / is defined. Please insert folder '/'.");
67
				}
68
			} else {
69
				$this->row = $row;
70
			}
71
72
			Cache::put("folder", "folder-" . $folder, $this->row);
73
		}
74
	}
75
76
	public function getFolder () : string {
77
		return $this->row['folder'];
78
	}
79
80
	public function listRequiredPermissions () : array {
81
		return explode("|", $this->row['permissions']);
82
	}
83
84
	public function checkPermissions (PermissionChecker $permission_checker) : bool {
85
		foreach ($this->listRequiredPermissions() as $permission) {
86
			if ($permission_checker->hasRight($permission)) {
87
				return true;
88
			} else if ($permission === "none") {
89
				return true;
90
			}
91
		}
92
93
		return false;
94
	}
95
96
	public function isHidden () : bool {
97
		return $this->row['hidden'] == 1;
98
	}
99
100
	public function hasCustomMainMenu () : bool {
101
		return $this->row['main_menu'] != -1;
102
	}
103
104
	public function getMainMenu () : int {
105
		return $this->row['main_menu'];
106
	}
107
108
	public function hasCustomLocalMenu () : bool {
109
		return $this->row['local_menu'] != -1;
110
	}
111
112
	public function getLocalMenu () : int {
113
		return $this->row['local_menu'];
114
	}
115
116
	public function isTitleTranslationEnabled () {
117
		return $this->row['title_translation_support'] == 1;
118
	}
119
120
	public function isActivated () : bool {
121
		return $this->row['activated'] == 1;
122
	}
123
124
	public static function getFolderByPage (string $page) : string {
125
		$array = explode("/", $page);
126
127
		if (sizeof($array) <= 2 && empty($array[0])) {
128
			return "/";
129
		}
130
131
		//remove last element
132
		array_pop($array);
133
134
		return implode("/", $array) . "/";
135
	}
136
137
	public static function getUpperDir ($dir) {
138
		if (PHPUtils::endsWith($dir, "/")) {
139
			//remove last slash
140
			$dir = substr($dir, 0, -1);
141
		}
142
143
		$array = explode("/", $dir);
144
145
		if (sizeof($array) <= 2 && empty($array[0])) {
146
			return "/";
147
		}
148
149
		//remove last element
150
		array_pop($array);
151
152
		return implode("/", $array) . "/";
153
	}
154
155
	public static function createFolder (string $folder, bool $hidden = false, array $permissions = array(), int $main_menu = -1, int $local_menu = -1, string $force_template = "none", bool $title_translation = true) {
156
		//escape string
157
		//$folder = Database::getInstance()->escape($folder);
158
159
		$permissions_str = implode("|", $permissions);
160
161
		if (sizeof($permissions) == 0) {
162
			$permissions_str = "none";
163
		}
164
165
		Database::getInstance()->execute("INSERT INTO `{praefix}folder` (
166
			`folder`, `force_template`, `main_menu`, `local_menu`, `permissions`, `title_translation_support`, `hidden`, `activated`
167
		) VALUES (
168
			:folder, :templatename, :main_menu, :local_menu, :permissions, :title_translation_support, :hidden, '1'
169
		); ", array(
170
			'folder' => $folder,
171
			'templatename' => $force_template,
172
			'main_menu' => $main_menu,
173
			'local_menu' => $local_menu,
174
			'permissions' => $permissions_str,
175
			'title_translation_support' => ($title_translation ? 1 : 0),
176
			'hidden' => $hidden ? 1 : 0
177
		));
178
179
		//clear cache
180
		Cache::clear("folder");
181
	}
182
183
	public static function createFolderIfAbsent (string $folder, bool $hidden = false, array $permissions = array(), int $main_menu = -1, int $local_menu = -1, string $force_template = "none", bool $title_translation = true) {
184
		//escape string
185
		//$folder = Database::getInstance()->escape($folder);
186
187
		$permissions_str = implode("|", $permissions);
188
189
		if (sizeof($permissions) == 0) {
190
			$permissions_str = "none";
191
		}
192
193
		Database::getInstance()->execute("INSERT INTO `{praefix}folder` (
194
			`folder`, `force_template`, `main_menu`, `local_menu`, `permissions`, `title_translation_support`, `hidden`, `activated`
195
		) VALUES (
196
			:folder, :templatename, :main_menu, :local_menu, :permissions, :title_translation_support, :hidden, '1'
197
		) ON DUPLICATE KEY UPDATE `hidden` = :hidden, `permissions` = :permissions, `force_template` = :templatename; ", array(
198
			'folder' => $folder,
199
			'templatename' => $force_template,
200
			'main_menu' => $main_menu,
201
			'local_menu' => $local_menu,
202
			'permissions' => $permissions_str,
203
			'title_translation_support' => ($title_translation ? 1 : 0),
204
			'hidden' => $hidden ? 1 : 0
205
		));
206
207
		//clear cache
208
		Cache::clear("folder");
209
	}
210
211
	public static function deleteFolder ($folder) {
212
		//escape string
213
		//$folder = Database::getInstance()->escape($folder);
214
215
		Database::getInstance()->execute("DELETE FROM `{praefix}folder` WHERE `folder` = :folder; ", array('folder' => $folder));
216
	}
217
218
	public static function listFolders (bool $include_hidden_folders = false) {
219
		if (Cache::contains("folder", "list_" . ($include_hidden_folders ? "hidden" : "not_hidden"))) {
220
			return Cache::get("folder", "list_" . ($include_hidden_folders ? "hidden" : "not_hidden"));
221
		} else {
222
			$rows = array();
223
224
			if ($include_hidden_folders) {
225
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}folder` WHERE `activated` = 1; ");
226
			} else {
227
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}folder` WHERE `hidden` = 0 AND `activated` = 1; ");
228
			}
229
230
			Cache::put("folder", "list_" . ($include_hidden_folders ? "hidden" : "not_hidden"), $rows);
231
232
			return $rows;
233
		}
234
	}
235
236
	public static function exists (string $folder) : bool {
237
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}folder` WHERE `folder` = :folder; ", array(
238
			'folder' => $folder
239
		));
240
241
		return $row !== false;
242
	}
243
244
}
245
246
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
247