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

Domain::isAlias()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 1
nc 2
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
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 05.03.2018
25
 * Time: 01:11
26
 */
27
28
class Domain {
29
30
	protected static $instance = null;
31
32
	protected $id = 0;
33
	protected $row = null;
34
35
	/**
36
	 * constructor
37
	 */
38
	public function __construct() {
39
		//
40
	}
41
42
	public function load ($id = null) {
43
		if ($id == null) {
44
			//get current domain
45
			$id = self::getIDByDomain(DomainUtils::getCurrentDomain());
46
		}
47
48
		if (!is_int($id)) {
49
			throw new IllegalArgumentException("domain id has to be an integer.");
50
		}
51
52
		$this->id = $id;
53
54
		if (Cache::getCache()->contains("domain", "domain_" . $id)) {
55
			$this->row = Cache::getCache()->get("domain", "domain_" . $id);
56
		} else {
57
			//load domain from database
58
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `id` = :id; ", array('id' => $id));
59
60
			if (!$row) {
61
				throw new DomainNotFoundException("Couldnt find domainID " . $id . " in database.");
62
			}
63
64
			$this->row = $row;
65
66
			//put row into cache
67
			Cache::getCache()->put("domain", "domain_" . $id, $this->row);
68
		}
69
70
		//throw event, so plugin can execute code here
71
		Events::throwEvent("load_domain", array(
72
			'id' => &$this->id,
73
			'row' => &$this->row
74
		));
75
	}
76
77
	public function isAlias () : bool {
78
		return $this->row['alias'] != -1 && $this->row['alias'] != 0;
79
	}
80
81
	public function getDomain () : string {
82
		return $this->row['domain'];
83
	}
84
85
	public function getStyleID () : int {
86
		return $this->row['styleID'];
87
	}
88
89
	public function getHomePage () : string {
90
		return $this->row['home_page'];
91
	}
92
93
	public function isRedirectUrl () : bool {
94
		return $this->row['redirect_url'] !== "none";
95
	}
96
97
	public function getRedirectUrl () : string {
98
		return $this->row['redirect_url'];
99
	}
100
101
	public function getRedirectCode () : int {
102
		return $this->row['redirect_code'];
103
	}
104
105
	public static function getIDByDomain (string $domain) : int {
106
		if (is_int($domain)) {
0 ignored issues
show
introduced by
The condition is_int($domain) is always false.
Loading history...
107
			throw new IllegalArgumentException("domain cannot be an integer, because a domain string is requested.");
108
		}
109
110
		if (Cache::getCache()->contains("domain", "id_" . $domain)) {
111
			return (int) Cache::getCache()->contains("domain", "id_" . $domain);
112
		} else {
113
			//get id from database
114
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `domain` = :domain AND `activated` = '1'; ", array('domain' => $domain));
115
116
			$id = -1;
117
118
			if ($row) {
119
				//get id from database row
120
				$id = $row['id'];
121
			}
122
123
			if ($id == -1) {
124
				//domain doesnt exists, so get wildcard domain
125
				$wildcard_domain_row = self::getWildcardDomainRow();
126
127
				//check, if id belongs to wildcard domain
128
				if ($wildcard_domain_row['domain'] != $domain) {
129
					//get id of wildcard domain
130
					$id = self::getIDByDomain(self::getWildcardDomain());
131
				} else {
132
					//throw exception
133
					throw new DomainNotFoundException("Couldnt find domain " . htmlspecialchars($domain) . " in database.");
134
				}
135
			}
136
137
			//add id to cache
138
			Cache::getCache()->put("domain", "id_" . $domain, (int) $id);
139
140
			return $id;
141
		}
142
	}
143
144
	public static function getWildcardDomainID () : int {
145
		$row = self::getWildcardDomainRow();
146
147
		if (!$row) {
148
			//repair the missing wildcard domain
149
			self::createWildcardDomain();
150
151
			//select domain again
152
			return self::getWildcardDomainID();
153
154
			//throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database.");
155
		}
156
157
		return $row['id'];
158
	}
159
160
	public static function getWildcardDomain () : string {
161
		$row = self::getWildcardDomainRow();
162
163
		if (!$row) {
164
			//repair the missing wildcard domain
165
			self::createWildcardDomain();
166
167
			//select domain again
168
			return self::getWildcardDomainID();
169
170
			//throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database.");
171
		}
172
173
		return $row['domain'];
174
	}
175
176
	public static function getWildcardDomainRow () {
177
		if (Cache::getCache()->contains("domain", "wildcard_domain_row")) {
178
			return Cache::getCache()->get("domain", "wildcard_domain_row");
179
		} else {
180
			//get domain id from database
181
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `wildcard` = 'YES' AND `activated` = '1'; ");
182
183
			if (!$row) {
184
				self::createWildcardDomain();
185
186
				return self::getWildcardDomainRow();
187
188
				//throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database.");
189
			}
190
191
			//put id into cache
192
			Cache::getCache()->put("domain", "wildcard_domain_row", $row);
193
194
			//return id
195
			return $row;
196
		}
197
	}
198
199
	public static function createWildcardDomain () {
200
		Database::getInstance()->execute("INSERT INTO `{praefix}domain` (
201
			`id`, `domain`, `alias`, `home_page`, `wildcard`, `styleID`, `redirect_url`, `redirect_code`, `lastUpdate`, `activated`
202
		) VALUES (
203
			NULL, '*', '-1', 'home', 'YES', '-1', 'none', '302', CURRENT_TIMESTAMP, '1'
204
		) ON DUPLICATE KEY UPDATE `activated` = '1'; ");
205
	}
206
207
	public static function getCurrent () : Domain {
208
		//check, if instance exists
209
		if (self::$instance == null) {
210
			//create new instance of domain
211
			self::$instance = new Domain();
212
213
			//get id of domain
214
			$domainID = self::getIDByDomain(DomainUtils::getDomain());
215
216
			//load data of domain with id
217
			self::$instance->load($domainID);
218
		}
219
220
		return self::$instance;
221
	}
222
223
}
224
225
?>
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...
226