Test Failed
Push — master ( 72fd13...fd475f )
by Justin
09:44 queued 05:39
created

PageType::checkPermissions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 13
rs 9.2
c 2
b 0
f 2
cc 4
eloc 8
nc 4
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
		if (!$this->getPage()->hasCustomPermissions()) {
94
			return true;
95
		} else {
96
			$permissions = $this->getPage()->listCustomPermissions();
97
98
			foreach ($permissions as $permission) {
99
				if ($permission_checker->hasRight($permission)) {
100
					return true;
101
				}
102
			}
103
104
			return false;
105
		}
106
	}
107
108
	public function exitAfterOutput () {
109
		return false;
110
	}
111
112
	public static function reloadCache () {
113
		Cache::clear("pagetypes");
114
	}
115
116
	public static function listPageTypes (bool $advanced = false) : array {
117
		$rows = array();
118
119
		$advanced_str = $advanced ? "advanced" : "normal";
120
121
		if (Cache::contains("pagetypes", "list-" . $advanced_str)) {
122
			$rows = Cache::get("pagetypes", "list-" . $advanced_str);
123
		} else {
124
			if ($advanced) {
125
				//show all page types
126
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}page_types` WHERE `activated` = '1' ORDER BY `order`; ");
127
			} else {
128
				//show only not-expert page types
129
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}page_types` WHERE `advanced` = '0' AND `activated` = '1' ORDER BY `order`; ");
130
			}
131
132
			//put into cache
133
			Cache::put("pagetypes", "list-" . $advanced_str, $rows);
134
		}
135
136
		return $rows;
137
	}
138
139
	public static function createPageType (string $class_name, string $title, bool $advanced = false,int $order = 10, array $permissions = array("none")) {
140
		//validate values
141
		$class_name = Validator_String::get($class_name);
142
		$title = Validator_String::get($title);
143
		$order = Validator_Int::get($order);
144
145
		Events::throwEvent("before_add_pagetype", array(
146
			'class_name' => &$class_name,
147
			'title' => &$title,
148
			'create_permissions' => &$permissions,
149
			'advanced' => &$advanced,
150
			'order' => &$order
151
		));
152
153
		//validate and convert array to string
154
		$permissions = implode("|", $permissions);
155
		$permissions = Validator_String::get($permissions);
156
157
		Database::getInstance()->execute("INSERT INTO `{praefix}page_types` (
158
			`page_type`, `title`, `create_permissions`, `advanced`, `order`, `activated`
159
		) VALUES (
160
			:pagetype, :title, :permissions, :advanced, :order, '1'
161
		) ON DUPLICATE KEY UPDATE `title` = :title, `advanced` = :advanced, `activated` = '1'; ", array(
162
			'pagetype' => $class_name,
163
			'title' => $title,
164
			'permissions' => $permissions,
165
			'advanced' => ($advanced ? 1 : 0),
166
			'order' => $order
167
		));
168
169
		Events::throwEvent("after_add_pagetype", array(
170
			'class_name' => $class_name,
171
			'title' => $title,
172
			'create_permissions' => $permissions,
173
			'advanced' => $advanced,
174
			'order' => $order
175
		));
176
177
		//clear cache
178
		Cache::clear("pagetypes");
179
	}
180
181
	public static function removePageType (string $class_name) {
182
		//validate value
183
		$class_name = Validator_String::get($class_name);
184
185
		$delete = true;
186
187
		//throw event, so plugins can interact
188
		Events::throwEvent("before_remove_pagetype", array(
189
			'class_name' => &$class_name,
190
			'delete' => &$delete
191
		));
192
193
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
194
			Database::getInstance()->execute("DELETE FROM `{praefix}page_types` WHERE `page_type` = :pagetype; ", array('pagetype' => $class_name));
195
196
			//throw event, so plugins can interact
197
			Events::throwEvent("after_remove_pagetype", array(
198
				'class_name' => $class_name
199
			));
200
201
			//clear cache
202
			Cache::clear("pagetypes");
203
		}
204
	}
205
206
}
207
208
?>
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...
209