Completed
Push — master ( 8c2e26...e49b46 )
by Justin
03:16
created

Page::hasCustomPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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 Page {
20
21
	protected $pageID = -1;
22
	protected $alias = null;
23
	protected $row = null;
24
	protected $pagetype = "";
25
26
	protected $author = null;
27
28
	public function __construct() {
29
		//
30
	}
31
32
	public function load ($alias = null) {
33
		if ($alias == null) {
34
			if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) {
35
				$alias = Validator_String::get($_REQUEST['page']);
36
			} else {
37
				$alias = $this->getDomain()->getHomePage();
38
			}
39
		} else {
40
			//$alias = Database::getInstance()->escape($alias);
41
		}
42
43
		Events::throwEvent("get_alias", array(
44
			'alias' => &$alias,
45
			'page' => &$this,
46
			'domain' => $this->getDomain()
47
		));
48
49
		$this->alias = $alias;
50
51
		if (Cache::contains("pages", "page_" . $alias)) {
52
			$this->row = Cache::get("pages", "page_" . $alias);
53
		} else {
54
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias AND `activated` = '1'; ", array('alias' => $alias));
55
56
			if (!$row) {
57
				if (PHPUtils::strEqs("error404", $alias)) {
58
					throw new IllegalStateException("No page with alias 'error404' exists (requested alias: " . $alias . ").");
59
				}
60
61
				//page not found
62
				$new_alias = "error404";
63
64
				Events::throwEvent("load_page_error404", array(
65
					'alias' => &$new_alias,
66
					'original_alias' => $alias,
67
					'page' => &$this,
68
					'domain' => $this->getDomain()
69
				));
70
71
				$this->load($new_alias);
72
				return null;
73
			}
74
75
			$this->row = $row;
76
77
			//cache result
78
			Cache::put("pages", "page_" . $alias, $row);
79
		}
80
81
		//get pageID
82
		$this->pageID = $this->row['id'];
83
84
		//get name of page type (class name)
85
		$this->pagetype = $this->row['page_type'];
86
	}
87
88
	public function loadByID (int $pageID) {
89
		if (Cache::contains("pages", "pageID_" . $pageID)) {
90
			$this->row = Cache::get("pages", "pageID_" . $pageID);
91
		} else {
92
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `id` = :pageID; ", array('pageID' => $pageID));
93
94
			if (!$row) {
95
				throw new IllegalStateException("Page with pageID " . $pageID . " doesnt exists!");
96
			}
97
98
			$this->row = $row;
99
100
			//cache result
101
			Cache::put("pages", "pageID_" . $pageID, $row);
102
		}
103
104
		$this->pageID = $this->row['id'];
105
		$this->alias = $this->row['alias'];
106
107
		//get name of page type (class name)
108
		$this->pagetype = $this->row['page_type'];
109
	}
110
111
	protected function getDomain () : Domain {
112
		return Registry::singleton()->getObject("domain");
113
	}
114
115
	public function reloadCache () {
116
		Cache::clear("pages");
117
	}
118
119
	public function getPageID () : int {
120
		return $this->row['id'];
121
	}
122
123
	public function getAlias () : string {
124
		return $this->alias;
125
	}
126
127
	public function getPageType () : string {
128
		return $this->pagetype;
129
	}
130
131
	public function getTitle () : string {
132
		return $this->row['title'];
133
	}
134
135
	public function getContent () : string {
136
		return $this->row['content'];
137
	}
138
139
	public function getGlobalMenuID () : int {
140
		return $this->row['global_menu'];
141
	}
142
143
	public function getLocalMenuID () : int {
144
		return $this->row['local_menu'];
145
	}
146
147
	public function getStyle () : string {
148
		return $this->row['design'];
149
	}
150
151
	public function getFolder () : string {
152
		return $this->row['folder'];
153
	}
154
155
	public function getLastEdit () {
156
		return $this->row['lastUpdate'];
157
	}
158
159
	public function hasCustomTemplate () : bool {
160
		return $this->row['template'] !== "none";
161
	}
162
163
	public function getCustomTemplate () : string {
164
		return $this->row['template'];
165
	}
166
167
	public function hasCustomPermissions () : bool {
168
		return $this->row['can_see_permissions'] !== "none";
169
	}
170
171
	public function listCustomPermissions () : array {
172
		return explode("|", $this->row['can_see_permissions']);
173
	}
174
175
	public function isPublished () : bool {
176
		return $this->row['published'] == 1;
177
	}
178
179
	public function getContentType () : string {
180
		return $this->row['content_type'];
181
	}
182
183
	public function getLeftSidebarID () : int {
184
		return $this->row['sidebar_left'];
185
	}
186
187
	public function getRightSidebarID () : int {
188
		return $this->row['sidebar_right'];
189
	}
190
191
	public function getMetaDescription () : string {
192
		return $this->row['meta_description'];
193
	}
194
195
	public function getMetaKeywords () : string {
196
		return $this->row['meta_keywords'];
197
	}
198
199
	public function getMetaRobotsOptions () : string {
200
		return $this->row['meta_robots'];
201
	}
202
203
	public function getMetaCanonicals () : string {
204
		return $this->row['meta_canonicals'];
205
	}
206
207
	public function getAuthorID () : int {
208
		return $this->row['author'];
209
	}
210
211
	public function getAuthor () : User {
212
		if ($this->author == null) {
213
			//load author
214
			$this->author = new User();
215
216
			if ($this->getAuthorID() <= 0) {
217
				throw new IllegalArgumentException("authorID has to be > 0.");
218
			}
219
220
			$this->author->load($this->getAuthorID());
221
		}
222
223
		return $this->author;
224
	}
225
226
	public function activate (bool $bool = true) {
227
		$this->row['activated'] = ($bool ? 1 : 0);
228
	}
229
230
	public function isTrash () : bool {
231
		return $this->row['activated'] == 2;
232
	}
233
234
	public function isEditable () : bool {
235
		return $this->row['editable'] == 1;
236
	}
237
238
	public function isDeletable () : bool {
239
		return $this->row['deletable'] == 1;
240
	}
241
242
	public function isActivated () : bool {
243
		return $this->row['activated'] == 1;
244
	}
245
246
	public function moveToTrash () {
247
		self::movePageToTrash($this->pageID);
248
249
		//clear cache
250
		Cache::clear("pages", "pageID_" . $this->pageID);
251
		Cache::clear("pages", "page_" . $this->alias);
252
	}
253
254
	/**
255
	 * restore page from trash
256
	 */
257
	public function restore () {
258
		self::restorePage($this->pageID);
259
260
		//clear cache
261
		Cache::clear("pages", "pageID_" . $this->pageID);
262
		Cache::clear("pages", "page_" . $this->alias);
263
	}
264
265
	/**
266
	 * save changes into database
267
	 */
268
	public function save () {
269
		//TODO: add code here
270
	}
271
272
	public static function createIfAbsent (string $alias, string $title, string $page_type, string $content = "", string $folder = "/", int $globalMenu = -1, int $localMenu = -1, int $parentID = -1, bool $sitemap = true, bool $published = true, bool $editable = true, bool $deletable = true, string $author = "system") : int {
273
		//throw event
274
		Events::throwEvent("create_page", array(
275
			'alias' => &$alias,
276
			'title' => &$title,
277
			'page_type' => &$page_type,
278
			'content' => &$content,
279
			'folder' => &$folder,
280
			'global_menu' => &$globalMenu,
281
			'local_menu' => &$localMenu,
282
			'parentID' => &$parentID,
283
			'sitemap' => &$sitemap,
284
			'published' => &$published,
285
			'editable' => &$editable,
286
			'author' => &$author
287
		));
288
289
		if (!is_int($author)) {
0 ignored issues
show
introduced by
The condition is_int($author) is always false.
Loading history...
290
			//get userID of author
291
			$author = User::getIDByUsernameFromDB($author);
292
293
			if ($author == -1) {
294
				//username doesnt exists, so choose first user
295
				$author = 1;
296
			}
297
		} else {
298
			$author = (int) $author;
299
		}
300
301
		Database::getInstance()->execute("INSERT INTO `{praefix}pages` (
302
			`id`, `alias`, `title`, `content`, `parent`, `folder`, `global_menu`, `local_menu`, `page_type`, `design`, `sitemap`, `published`, `version`, `last_update`, `created`, `editable`, `deletable`, `author`, `activated`
303
		) VALUES (
304
			NULL, :alias, :title, :content, :parent, :folder, :globalMenu, :localMenu, :pageType, 'none', :sitemap, :published, '1', '0000-00-00 00:00:00', CURRENT_TIMESTAMP, :editable, :deletable, :author, '1'
305
		) ON DUPLICATE KEY UPDATE `alias` = :alias, `editable` = :editable, `deletable` = :deletable; ", array(
306
			'alias' => $alias,
307
			'title' => $title,
308
			'content' => $content,
309
			'parent' => $parentID,
310
			'folder' => $folder,
311
			'globalMenu' => $globalMenu,
312
			'localMenu' => $localMenu,
313
			'pageType' => $page_type,
314
			'sitemap' => ($sitemap ? 1 : 0),
315
			'published' => ($published ? 1 : 0),
316
			'editable' => ($editable ? 1 : 0),
317
			'deletable' => ($deletable ? 1 : 0),
318
			'author' => $author
319
		));
320
321
		Cache::clear("pages");
322
323
		//return page id
324
		$insertID = Database::getInstance()->lastInsertId();
325
326
		//throw event
327
		Events::throwEvent("created_page", array(
328
			'alias' => $alias,
329
			'title' => $title,
330
			'insertID' => $insertID
331
		));
332
333
		//get pageID by alias
334
		$pageID = Page::getPageIDByAlias($alias);
335
336
		//set default rights, allow page for administrators, registered users, guests and bots
337
		PageRights::setDefaultAllowedGroups($pageID, array(1, 2, 3, 4));
338
339
		return $pageID;
340
	}
341
342
	public static function delete (string $alias) {
343
		$delete = true;
344
345
		//plugins can avoid deletion or change alias
346
		Events::throwEvent("delete_page_alias", array(
347
			'alias' => &$alias,
348
			'delete' => &$delete
349
		));
350
351
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
352
			//remove page from database
353
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
354
355
			Cache::clear("pages");
356
		}
357
	}
358
359
	public static function deleteByID (int $id) {
360
		$delete = true;
361
362
		//plugins can avoid deletion or change alias
363
		Events::throwEvent("delete_page_id", array(
364
			'alias' => &$id,
365
			'delete' => &$delete
366
		));
367
368
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
369
			//remove page from database
370
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id));
371
372
			Cache::clear("pages");
373
		}
374
	}
375
376
	public static function get (string $alias) : Page {
377
		$page = new Page();
378
		$page->load($alias);
379
380
		return $page;
381
	}
382
383
	public static function setPageType (string $alias, string $page_type) {
384
		Events::throwEvent("set_pagetype", array(
385
			'alias' => &$alias,
386
			'page_type' => &$page_type
387
		));
388
389
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array(
390
			'alias' => $alias,
391
			'page_type' => $page_type
392
		));
393
394
		Cache::clear("pages");
395
	}
396
397
	/**
398
	 * get id of page by alias
399
	 *
400
	 * only use this method for database upgrade, because their is no caching for this method!
401
	 *
402
	 * @param string $alias alias of page
403
	 *
404
	 * @throws IllegalStateException if alias doesnt exists
405
	 *
406
	 * @return int pageID
407
	 */
408
	public static function getPageIDByAlias (string $alias) : int {
409
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
410
411
		if (!$row) {
412
			throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists.");
413
		}
414
415
		return $row['id'];
416
	}
417
418
	public static function lockPage (int $pageID, int $userID) {
419
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` = :userID, `locked_timestamp` = CURRENT_TIMESTAMP WHERE `id` = :pageID; ", array(
420
			'userID' => $userID,
421
			'pageID' => $pageID
422
		));
423
	}
424
425
	public static function unlockPage (int $pageID) {
426
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` WHERE `id` = :pageID; ", array(
427
			'pageID' => $pageID
428
		));
429
	}
430
431
	protected static function movePageToTrash (int $pageID) {
432
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 2 WHERE `id` = :pageID; ", array(
433
			'pageID' => $pageID
434
		));
435
436
		//clear cache
437
		Cache::clear("pages", "pageID_" . $pageID);
438
	}
439
440
	protected static function restorePage (int $pageID) {
441
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 1 WHERE `id` = :pageID; ", array(
442
			'pageID' => $pageID
443
		));
444
445
		//clear cache
446
		Cache::clear("pages", "pageID_" . $pageID);
447
	}
448
449
}
450
451
?>
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...
452