Rev 1050 | Rev 1116 | 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 { |
27 | private $domain; |
||
28 | |||
29 | public function __construct($domain) { |
||
30 | // TODO: syntax checks |
||
31 | $this->domain = $domain; |
||
32 | } |
||
33 | |||
34 | public static function parse($node_id) { |
||
35 | @list($namespace, $domain) = explode(':', $node_id, 2); |
||
860 | daniel-mar | 36 | if ($namespace !== self::ns()) return false; |
707 | daniel-mar | 37 | return new self($domain); |
38 | } |
||
39 | |||
40 | public static function objectTypeTitle() { |
||
41 | return _L('Domain Names'); |
||
42 | } |
||
43 | |||
44 | public static function objectTypeTitleShort() { |
||
45 | return _L('Domain'); |
||
46 | } |
||
47 | |||
48 | public static function ns() { |
||
49 | return 'domain'; |
||
50 | } |
||
51 | |||
52 | public static function root() { |
||
860 | daniel-mar | 53 | return self::ns().':'; |
707 | daniel-mar | 54 | } |
55 | |||
56 | public function isRoot() { |
||
57 | return $this->domain == ''; |
||
58 | } |
||
59 | |||
60 | public function nodeId($with_ns=true) { |
||
859 | daniel-mar | 61 | return $with_ns ? self::root().$this->domain : $this->domain; |
707 | daniel-mar | 62 | } |
63 | |||
64 | public function addString($str) { |
||
65 | if ($this->isRoot()) { |
||
859 | daniel-mar | 66 | return self::root().$str; |
707 | daniel-mar | 67 | } else { |
68 | if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.')); |
||
859 | daniel-mar | 69 | return self::root().$str.'.'.$this->nodeId(false); |
707 | daniel-mar | 70 | } |
71 | } |
||
72 | |||
73 | public function crudShowId(OIDplusObject $parent) { |
||
74 | return $this->domain; |
||
75 | } |
||
76 | |||
77 | public function crudInsertSuffix() { |
||
78 | return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1); |
||
79 | } |
||
80 | |||
81 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
||
82 | if ($parent == null) return $this->objectTypeTitle(); |
||
83 | return $this->domain; |
||
84 | } |
||
85 | |||
86 | public function defaultTitle() { |
||
87 | return $this->domain; |
||
88 | } |
||
89 | |||
90 | public function isLeafNode() { |
||
91 | return false; |
||
92 | } |
||
93 | |||
94 | public function getContentPage(&$title, &$content, &$icon) { |
||
801 | daniel-mar | 95 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
707 | daniel-mar | 96 | |
97 | if ($this->isRoot()) { |
||
98 | $title = OIDplusDomain::objectTypeTitle(); |
||
99 | |||
100 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 101 | if ($res->any()) { |
962 | daniel-mar | 102 | $content = '<p>'._L('Please select a Domain Name in the tree view at the left to show its contents.').'</p>'; |
707 | daniel-mar | 103 | } else { |
962 | daniel-mar | 104 | $content = '<p>'._L('Currently, no Domain Name is registered in the system.').'</p>'; |
707 | daniel-mar | 105 | } |
106 | |||
107 | if (!$this->isLeafNode()) { |
||
108 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
109 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
110 | } else { |
||
111 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
112 | } |
||
113 | $content .= '%%CRUD%%'; |
||
114 | } |
||
115 | } else { |
||
116 | $title = $this->getTitle(); |
||
117 | |||
118 | $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>'; |
||
119 | |||
120 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
||
121 | |||
122 | if (!$this->isLeafNode()) { |
||
123 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 124 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
707 | daniel-mar | 125 | } else { |
928 | daniel-mar | 126 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
707 | daniel-mar | 127 | } |
128 | $content .= '%%CRUD%%'; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | public function one_up() { |
||
134 | $oid = $this->domain; |
||
135 | |||
136 | $p = strpos($oid, '.'); |
||
713 | daniel-mar | 137 | if ($p === false) return self::parse(''); |
707 | daniel-mar | 138 | |
139 | $oid_up = substr($oid, $p+1); |
||
140 | |||
141 | return self::parse(self::ns().':'.$oid_up); |
||
142 | } |
||
143 | |||
144 | public function distance($to) { |
||
145 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
146 | if (!($to instanceof $this)) return false; |
||
147 | |||
148 | $a = $to->domain; |
||
149 | $b = $this->domain; |
||
150 | |||
151 | if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1); |
||
152 | if (substr($b,-1) == '.') $b = substr($b,0,strlen($b)-1); |
||
153 | |||
154 | $ary = explode('.', $a); |
||
155 | $bry = explode('.', $b); |
||
156 | |||
157 | $ary = array_reverse($ary); |
||
158 | $bry = array_reverse($bry); |
||
159 | |||
160 | $min_len = min(count($ary), count($bry)); |
||
161 | |||
162 | for ($i=0; $i<$min_len; $i++) { |
||
163 | if ($ary[$i] != $bry[$i]) return false; |
||
164 | } |
||
165 | |||
166 | return count($ary) - count($bry); |
||
167 | } |
||
168 | |||
169 | public function getDirectoryName() { |
||
170 | if ($this->isRoot()) return $this->ns(); |
||
171 | return $this->ns().'_'.md5($this->nodeId(false)); |
||
172 | } |
||
800 | daniel-mar | 173 | |
805 | daniel-mar | 174 | public static function treeIconFilename($mode) { |
800 | daniel-mar | 175 | return 'img/'.$mode.'_icon16.png'; |
176 | } |
||
707 | daniel-mar | 177 | } |