Subversion Repositories oidplus

Rev

Rev 962 | Rev 1086 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 962 Rev 1050
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with 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
9
 * You may obtain a copy of the License at
10
 *
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
if (!defined('INSIDE_OIDPLUS')) die();
20
namespace ViaThinkSoft\OIDplus;
21
 
21
 
22
class OIDplusDomain extends OIDplusObject {
22
class OIDplusDomain extends OIDplusObject {
23
        private $domain;
23
        private $domain;
24
 
24
 
25
        public function __construct($domain) {
25
        public function __construct($domain) {
26
                // TODO: syntax checks
26
                // TODO: syntax checks
27
                $this->domain = $domain;
27
                $this->domain = $domain;
28
        }
28
        }
29
 
29
 
30
        public static function parse($node_id) {
30
        public static function parse($node_id) {
31
                @list($namespace, $domain) = explode(':', $node_id, 2);
31
                @list($namespace, $domain) = explode(':', $node_id, 2);
32
                if ($namespace !== self::ns()) return false;
32
                if ($namespace !== self::ns()) return false;
33
                return new self($domain);
33
                return new self($domain);
34
        }
34
        }
35
 
35
 
36
        public static function objectTypeTitle() {
36
        public static function objectTypeTitle() {
37
                return _L('Domain Names');
37
                return _L('Domain Names');
38
        }
38
        }
39
 
39
 
40
        public static function objectTypeTitleShort() {
40
        public static function objectTypeTitleShort() {
41
                return _L('Domain');
41
                return _L('Domain');
42
        }
42
        }
43
 
43
 
44
        public static function ns() {
44
        public static function ns() {
45
                return 'domain';
45
                return 'domain';
46
        }
46
        }
47
 
47
 
48
        public static function root() {
48
        public static function root() {
49
                return self::ns().':';
49
                return self::ns().':';
50
        }
50
        }
51
 
51
 
52
        public function isRoot() {
52
        public function isRoot() {
53
                return $this->domain == '';
53
                return $this->domain == '';
54
        }
54
        }
55
 
55
 
56
        public function nodeId($with_ns=true) {
56
        public function nodeId($with_ns=true) {
57
                return $with_ns ? self::root().$this->domain : $this->domain;
57
                return $with_ns ? self::root().$this->domain : $this->domain;
58
        }
58
        }
59
 
59
 
60
        public function addString($str) {
60
        public function addString($str) {
61
                if ($this->isRoot()) {
61
                if ($this->isRoot()) {
62
                        return self::root().$str;
62
                        return self::root().$str;
63
                } else {
63
                } else {
64
                        if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
64
                        if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
65
                        return self::root().$str.'.'.$this->nodeId(false);
65
                        return self::root().$str.'.'.$this->nodeId(false);
66
                }
66
                }
67
        }
67
        }
68
 
68
 
69
        public function crudShowId(OIDplusObject $parent) {
69
        public function crudShowId(OIDplusObject $parent) {
70
                return $this->domain;
70
                return $this->domain;
71
        }
71
        }
72
 
72
 
73
        public function crudInsertSuffix() {
73
        public function crudInsertSuffix() {
74
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
74
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
75
        }
75
        }
76
 
76
 
77
        public function jsTreeNodeName(OIDplusObject $parent = null) {
77
        public function jsTreeNodeName(OIDplusObject $parent = null) {
78
                if ($parent == null) return $this->objectTypeTitle();
78
                if ($parent == null) return $this->objectTypeTitle();
79
                return $this->domain;
79
                return $this->domain;
80
        }
80
        }
81
 
81
 
82
        public function defaultTitle() {
82
        public function defaultTitle() {
83
                return $this->domain;
83
                return $this->domain;
84
        }
84
        }
85
 
85
 
86
        public function isLeafNode() {
86
        public function isLeafNode() {
87
                return false;
87
                return false;
88
        }
88
        }
89
 
89
 
90
        public function getContentPage(&$title, &$content, &$icon) {
90
        public function getContentPage(&$title, &$content, &$icon) {
91
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
91
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
92
 
92
 
93
                if ($this->isRoot()) {
93
                if ($this->isRoot()) {
94
                        $title = OIDplusDomain::objectTypeTitle();
94
                        $title = OIDplusDomain::objectTypeTitle();
95
 
95
 
96
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
96
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
97
                        if ($res->any()) {
97
                        if ($res->any()) {
98
                                $content  = '<p>'._L('Please select a Domain Name in the tree view at the left to show its contents.').'</p>';
98
                                $content  = '<p>'._L('Please select a Domain Name in the tree view at the left to show its contents.').'</p>';
99
                        } else {
99
                        } else {
100
                                $content  = '<p>'._L('Currently, no Domain Name is registered in the system.').'</p>';
100
                                $content  = '<p>'._L('Currently, no Domain Name is registered in the system.').'</p>';
101
                        }
101
                        }
102
 
102
 
103
                        if (!$this->isLeafNode()) {
103
                        if (!$this->isLeafNode()) {
104
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
104
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
105
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
105
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
106
                                } else {
106
                                } else {
107
                                        $content .= '<h2>'._L('Available objects').'</h2>';
107
                                        $content .= '<h2>'._L('Available objects').'</h2>';
108
                                }
108
                                }
109
                                $content .= '%%CRUD%%';
109
                                $content .= '%%CRUD%%';
110
                        }
110
                        }
111
                } else {
111
                } else {
112
                        $title = $this->getTitle();
112
                        $title = $this->getTitle();
113
 
113
 
114
                        $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
114
                        $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
115
 
115
 
116
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
116
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
117
 
117
 
118
                        if (!$this->isLeafNode()) {
118
                        if (!$this->isLeafNode()) {
119
                                if ($this->userHasWriteRights()) {
119
                                if ($this->userHasWriteRights()) {
120
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
120
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
121
                                } else {
121
                                } else {
122
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
122
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
123
                                }
123
                                }
124
                                $content .= '%%CRUD%%';
124
                                $content .= '%%CRUD%%';
125
                        }
125
                        }
126
                }
126
                }
127
        }
127
        }
128
 
128
 
129
        public function one_up() {
129
        public function one_up() {
130
                $oid = $this->domain;
130
                $oid = $this->domain;
131
 
131
 
132
                $p = strpos($oid, '.');
132
                $p = strpos($oid, '.');
133
                if ($p === false) return self::parse('');
133
                if ($p === false) return self::parse('');
134
 
134
 
135
                $oid_up = substr($oid, $p+1);
135
                $oid_up = substr($oid, $p+1);
136
 
136
 
137
                return self::parse(self::ns().':'.$oid_up);
137
                return self::parse(self::ns().':'.$oid_up);
138
        }
138
        }
139
 
139
 
140
        public function distance($to) {
140
        public function distance($to) {
141
                if (!is_object($to)) $to = OIDplusObject::parse($to);
141
                if (!is_object($to)) $to = OIDplusObject::parse($to);
142
                if (!($to instanceof $this)) return false;
142
                if (!($to instanceof $this)) return false;
143
 
143
 
144
                $a = $to->domain;
144
                $a = $to->domain;
145
                $b = $this->domain;
145
                $b = $this->domain;
146
 
146
 
147
                if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1);
147
                if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1);
148
                if (substr($b,-1) == '.') $b = substr($b,0,strlen($b)-1);
148
                if (substr($b,-1) == '.') $b = substr($b,0,strlen($b)-1);
149
 
149
 
150
                $ary = explode('.', $a);
150
                $ary = explode('.', $a);
151
                $bry = explode('.', $b);
151
                $bry = explode('.', $b);
152
 
152
 
153
                $ary = array_reverse($ary);
153
                $ary = array_reverse($ary);
154
                $bry = array_reverse($bry);
154
                $bry = array_reverse($bry);
155
 
155
 
156
                $min_len = min(count($ary), count($bry));
156
                $min_len = min(count($ary), count($bry));
157
 
157
 
158
                for ($i=0; $i<$min_len; $i++) {
158
                for ($i=0; $i<$min_len; $i++) {
159
                        if ($ary[$i] != $bry[$i]) return false;
159
                        if ($ary[$i] != $bry[$i]) return false;
160
                }
160
                }
161
 
161
 
162
                return count($ary) - count($bry);
162
                return count($ary) - count($bry);
163
        }
163
        }
164
 
164
 
165
        public function getDirectoryName() {
165
        public function getDirectoryName() {
166
                if ($this->isRoot()) return $this->ns();
166
                if ($this->isRoot()) return $this->ns();
167
                return $this->ns().'_'.md5($this->nodeId(false));
167
                return $this->ns().'_'.md5($this->nodeId(false));
168
        }
168
        }
169
 
169
 
170
        public static function treeIconFilename($mode) {
170
        public static function treeIconFilename($mode) {
171
                return 'img/'.$mode.'_icon16.png';
171
                return 'img/'.$mode.'_icon16.png';
172
        }
172
        }
173
}
173
}
174
 
174