Rev 1121 | Rev 1417 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
707 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
707 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
707 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
707 | daniel-mar | 26 | class OIDplusDomain extends OIDplusObject { |
1130 | daniel-mar | 27 | /** |
28 | * @var string |
||
29 | */ |
||
707 | daniel-mar | 30 | private $domain; |
31 | |||
1116 | daniel-mar | 32 | /** |
33 | * @param string $domain |
||
34 | */ |
||
35 | public function __construct(string $domain) { |
||
707 | daniel-mar | 36 | // TODO: syntax checks |
37 | $this->domain = $domain; |
||
38 | } |
||
39 | |||
1116 | daniel-mar | 40 | /** |
41 | * @param string $node_id |
||
42 | * @return OIDplusDomain|null |
||
43 | */ |
||
44 | public static function parse(string $node_id)/*: ?OIDplusDomain*/ { |
||
707 | daniel-mar | 45 | @list($namespace, $domain) = explode(':', $node_id, 2); |
1116 | daniel-mar | 46 | if ($namespace !== self::ns()) return null; |
707 | daniel-mar | 47 | return new self($domain); |
48 | } |
||
49 | |||
1116 | daniel-mar | 50 | /** |
51 | * @return string |
||
52 | */ |
||
53 | public static function objectTypeTitle(): string { |
||
707 | daniel-mar | 54 | return _L('Domain Names'); |
55 | } |
||
56 | |||
1116 | daniel-mar | 57 | /** |
58 | * @return string |
||
59 | */ |
||
60 | public static function objectTypeTitleShort(): string { |
||
707 | daniel-mar | 61 | return _L('Domain'); |
62 | } |
||
63 | |||
1116 | daniel-mar | 64 | /** |
65 | * @return string |
||
66 | */ |
||
67 | public static function ns(): string { |
||
707 | daniel-mar | 68 | return 'domain'; |
69 | } |
||
70 | |||
1116 | daniel-mar | 71 | /** |
72 | * @return string |
||
73 | */ |
||
74 | public static function root(): string { |
||
860 | daniel-mar | 75 | return self::ns().':'; |
707 | daniel-mar | 76 | } |
77 | |||
1116 | daniel-mar | 78 | /** |
79 | * @return bool |
||
80 | */ |
||
81 | public function isRoot(): bool { |
||
707 | daniel-mar | 82 | return $this->domain == ''; |
83 | } |
||
84 | |||
1116 | daniel-mar | 85 | /** |
86 | * @param bool $with_ns |
||
87 | * @return string |
||
88 | */ |
||
89 | public function nodeId(bool $with_ns=true): string { |
||
859 | daniel-mar | 90 | return $with_ns ? self::root().$this->domain : $this->domain; |
707 | daniel-mar | 91 | } |
92 | |||
1116 | daniel-mar | 93 | /** |
94 | * @param string $str |
||
95 | * @return string |
||
96 | * @throws OIDplusException |
||
97 | */ |
||
98 | public function addString(string $str): string { |
||
707 | daniel-mar | 99 | if ($this->isRoot()) { |
859 | daniel-mar | 100 | return self::root().$str; |
707 | daniel-mar | 101 | } else { |
102 | if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.')); |
||
859 | daniel-mar | 103 | return self::root().$str.'.'.$this->nodeId(false); |
707 | daniel-mar | 104 | } |
105 | } |
||
106 | |||
1116 | daniel-mar | 107 | /** |
108 | * @param OIDplusObject $parent |
||
109 | * @return string |
||
110 | */ |
||
111 | public function crudShowId(OIDplusObject $parent): string { |
||
707 | daniel-mar | 112 | return $this->domain; |
113 | } |
||
114 | |||
1116 | daniel-mar | 115 | /** |
116 | * @return string |
||
117 | * @throws OIDplusException |
||
118 | */ |
||
119 | public function crudInsertSuffix(): string { |
||
707 | daniel-mar | 120 | return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1); |
121 | } |
||
122 | |||
1116 | daniel-mar | 123 | /** |
124 | * @param OIDplusObject|null $parent |
||
125 | * @return string |
||
126 | */ |
||
127 | public function jsTreeNodeName(OIDplusObject $parent = null): string { |
||
707 | daniel-mar | 128 | if ($parent == null) return $this->objectTypeTitle(); |
129 | return $this->domain; |
||
130 | } |
||
131 | |||
1116 | daniel-mar | 132 | /** |
133 | * @return string |
||
134 | */ |
||
135 | public function defaultTitle(): string { |
||
707 | daniel-mar | 136 | return $this->domain; |
137 | } |
||
138 | |||
1116 | daniel-mar | 139 | /** |
140 | * @return bool |
||
141 | */ |
||
142 | public function isLeafNode(): bool { |
||
707 | daniel-mar | 143 | return false; |
144 | } |
||
145 | |||
1116 | daniel-mar | 146 | /** |
147 | * @param string $title |
||
148 | * @param string $content |
||
149 | * @param string $icon |
||
150 | * @return void |
||
151 | * @throws OIDplusException |
||
152 | */ |
||
153 | public function getContentPage(string &$title, string &$content, string &$icon) { |
||
801 | daniel-mar | 154 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
707 | daniel-mar | 155 | |
156 | if ($this->isRoot()) { |
||
157 | $title = OIDplusDomain::objectTypeTitle(); |
||
158 | |||
159 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 160 | if ($res->any()) { |
962 | daniel-mar | 161 | $content = '<p>'._L('Please select a Domain Name in the tree view at the left to show its contents.').'</p>'; |
707 | daniel-mar | 162 | } else { |
962 | daniel-mar | 163 | $content = '<p>'._L('Currently, no Domain Name is registered in the system.').'</p>'; |
707 | daniel-mar | 164 | } |
165 | |||
166 | if (!$this->isLeafNode()) { |
||
167 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
168 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
169 | } else { |
||
170 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
171 | } |
||
172 | $content .= '%%CRUD%%'; |
||
173 | } |
||
174 | } else { |
||
175 | $title = $this->getTitle(); |
||
176 | |||
177 | $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>'; |
||
178 | |||
179 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
||
180 | |||
181 | if (!$this->isLeafNode()) { |
||
182 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 183 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
707 | daniel-mar | 184 | } else { |
928 | daniel-mar | 185 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
707 | daniel-mar | 186 | } |
187 | $content .= '%%CRUD%%'; |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | |||
1116 | daniel-mar | 192 | /** |
193 | * @return OIDplusDomain|null |
||
194 | */ |
||
195 | public function one_up()/*: ?OIDplusDomain*/ { |
||
707 | daniel-mar | 196 | $oid = $this->domain; |
197 | |||
198 | $p = strpos($oid, '.'); |
||
713 | daniel-mar | 199 | if ($p === false) return self::parse(''); |
707 | daniel-mar | 200 | |
201 | $oid_up = substr($oid, $p+1); |
||
202 | |||
203 | return self::parse(self::ns().':'.$oid_up); |
||
204 | } |
||
205 | |||
1116 | daniel-mar | 206 | /** |
1130 | daniel-mar | 207 | * @param OIDplusObject|string $to |
1116 | daniel-mar | 208 | * @return int|null |
209 | */ |
||
707 | daniel-mar | 210 | public function distance($to) { |
211 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
1121 | daniel-mar | 212 | if (!$to) return null; |
1116 | daniel-mar | 213 | if (!($to instanceof $this)) return null; |
707 | daniel-mar | 214 | |
215 | $a = $to->domain; |
||
216 | $b = $this->domain; |
||
217 | |||
218 | if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1); |
||
219 | if (substr($b,-1) == '.') $b = substr($b,0,strlen($b)-1); |
||
220 | |||
221 | $ary = explode('.', $a); |
||
222 | $bry = explode('.', $b); |
||
223 | |||
224 | $ary = array_reverse($ary); |
||
225 | $bry = array_reverse($bry); |
||
226 | |||
227 | $min_len = min(count($ary), count($bry)); |
||
228 | |||
229 | for ($i=0; $i<$min_len; $i++) { |
||
1116 | daniel-mar | 230 | if ($ary[$i] != $bry[$i]) return null; |
707 | daniel-mar | 231 | } |
232 | |||
233 | return count($ary) - count($bry); |
||
234 | } |
||
235 | |||
1116 | daniel-mar | 236 | /** |
237 | * @return string |
||
238 | */ |
||
239 | public function getDirectoryName(): string { |
||
707 | daniel-mar | 240 | if ($this->isRoot()) return $this->ns(); |
241 | return $this->ns().'_'.md5($this->nodeId(false)); |
||
242 | } |
||
800 | daniel-mar | 243 | |
1116 | daniel-mar | 244 | /** |
245 | * @param string $mode |
||
246 | * @return string |
||
247 | */ |
||
248 | public static function treeIconFilename(string $mode): string { |
||
800 | daniel-mar | 249 | return 'img/'.$mode.'_icon16.png'; |
250 | } |
||
707 | daniel-mar | 251 | } |