Test Failed
Push — master ( a791ff...76f79c )
by Justin
04:31
created

PageType::checkPermissions()   C

Complexity

Conditions 8
Paths 21

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 29
rs 5.3846
cc 8
eloc 16
nc 21
nop 1
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
class PageType {
20
21
	protected $page = null;
22
23
	public function __construct() {
24
		//
25
	}
26
27
	public function setPage (Page &$page) {
28
		$this->page = $page;
29
	}
30
31
	protected function getPage () : Page {
32
		return $this->page;
33
	}
34
35
	public function showDesign () {
36
		return true;
37
	}
38
39
	public function getContentType () : string {
40
		return "" . $this->page->getContentType() . "; charset=" . $this->getCharset();
41
	}
42
43
	public function getCharset () : string {
44
		return "utf-8";
45
	}
46
47
	public function setCustomHeader () {
48
		//
49
	}
50
51
	public function showFooter () : bool {
52
		return true;
53
	}
54
55
	public function showHTMLComments () : bool {
56
		return true;
57
	}
58
59
	public function getContent () : string {
60
		$content = $this->getPage()->getContent();
61
62
		//check, if page has custom template
63
		if ($this->getPage()->hasCustomTemplate()) {
64
			//get custom template
65
			$template = Validator_String::get($this->getPage()->getCustomTemplate());
66
67
			$current_style = Registry::singleton()->getSetting("current_style_name");
68
69
			//check, if custom template exists
70
			if (file_exists(STYLE_PATH . $current_style . "/" . $template)) {
71
				$template = new Template($template);
72
73
				$template->assign("TITLE", $this->getPage()->getTitle());
74
				$template->assign("CONTENT", $content);
75
76
				$template->parse("main");
77
				$content = $template->getCode();
78
			} else {
79
				throw new IllegalStateException("Custom template '" . $template . "' doesnt exists.");
80
			}
81
		}
82
83
		Events::throwEvent("get_content", array(
84
			'content' => &$content,
85
			'page' => &$this->page,
86
			'page_type' => &$this
87
		));
88
89
		return $content;
90
	}
91
92
	public function checkPermissions (PermissionChecker $permission_checker) {
93
		//first, check required permissions
94
		if (count($this->listRequiredPermissions()) > 0) {
95
			$bool = false;
96
97
			foreach ($this->listRequiredPermissions() as $permission) {
98
				if ($permission_checker->hasRight($permission)) {
99
					$bool = true;
100
					break;
101
				}
102
			}
103
104
			if (!$bool) {
105
				return false;
106
			}
107
		}
108
109
		if (!$this->getPage()->hasCustomPermissions()) {
110
			return true;
111
		} else {
112
			$permissions = $this->getPage()->listCustomPermissions();
113
114
			foreach ($permissions as $permission) {
115
				if ($permission_checker->hasRight($permission)) {
116
					return true;
117
				}
118
			}
119
120
			return false;
121
		}
122
	}
123
124
	protected function listRequiredPermissions () : array {
125
		return array();
126
	}
127
128
	public function exitAfterOutput () {
129
		return false;
130
	}
131
132
	public static function reloadCache () {
133
		Cache::clear("pagetypes");
134
	}
135
136
	public static function listPageTypes (bool $advanced = false) : array {
137
		$rows = array();
138
139
		$advanced_str = $advanced ? "advanced" : "normal";
140
141
		if (Cache::contains("pagetypes", "list-" . $advanced_str)) {
142
			$rows = Cache::get("pagetypes", "list-" . $advanced_str);
143
		} else {
144
			if ($advanced) {
145
				//show all page types
146
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}page_types` WHERE `activated` = '1' ORDER BY `order`; ");
147
			} else {
148
				//show only not-expert page types
149
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}page_types` WHERE `advanced` = '0' AND `activated` = '1' ORDER BY `order`; ");
150
			}
151
152
			//put into cache
153
			Cache::put("pagetypes", "list-" . $advanced_str, $rows);
154
		}
155
156
		return $rows;
157
	}
158
159
	public static function createPageType (string $class_name, string $title, bool $advanced = false,int $order = 10, array $permissions = array("none")) {
160
		//validate values
161
		$class_name = Validator_String::get($class_name);
162
		$title = Validator_String::get($title);
163
		$order = Validator_Int::get($order);
164
165
		Events::throwEvent("before_add_pagetype", array(
166
			'class_name' => &$class_name,
167
			'title' => &$title,
168
			'create_permissions' => &$permissions,
169
			'advanced' => &$advanced,
170
			'order' => &$order
171
		));
172
173
		//validate and convert array to string
174
		$permissions = implode("|", $permissions);
175
		$permissions = Validator_String::get($permissions);
176
177
		Database::getInstance()->execute("INSERT INTO `{praefix}page_types` (
178
			`page_type`, `title`, `create_permissions`, `advanced`, `order`, `activated`
179
		) VALUES (
180
			:pagetype, :title, :permissions, :advanced, :order, '1'
181
		) ON DUPLICATE KEY UPDATE `title` = :title, `advanced` = :advanced, `activated` = '1'; ", array(
182
			'pagetype' => $class_name,
183
			'title' => $title,
184
			'permissions' => $permissions,
185
			'advanced' => ($advanced ? 1 : 0),
186
			'order' => $order
187
		));
188
189
		Events::throwEvent("after_add_pagetype", array(
190
			'class_name' => $class_name,
191
			'title' => $title,
192
			'create_permissions' => $permissions,
193
			'advanced' => $advanced,
194
			'order' => $order
195
		));
196
197
		//clear cache
198
		Cache::clear("pagetypes");
199
	}
200
201
	public static function removePageType (string $class_name) {
202
		//validate value
203
		$class_name = Validator_String::get($class_name);
204
205
		$delete = true;
206
207
		//throw event, so plugins can interact
208
		Events::throwEvent("before_remove_pagetype", array(
209
			'class_name' => &$class_name,
210
			'delete' => &$delete
211
		));
212
213
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
214
			Database::getInstance()->execute("DELETE FROM `{praefix}page_types` WHERE `page_type` = :pagetype; ", array('pagetype' => $class_name));
215
216
			//throw event, so plugins can interact
217
			Events::throwEvent("after_remove_pagetype", array(
218
				'class_name' => $class_name
219
			));
220
221
			//clear cache
222
			Cache::clear("pagetypes");
223
		}
224
	}
225
226
}
227
228
?>
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...
229