Subversion Repositories oidplus

Rev

Rev 2 | Rev 16 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
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
 
20
abstract class OIDplusObject {
21
        public static $registeredObjectTypes = array();
22
 
23
        public static function parse($node_id) { // please overwrite this function!
24
                // TODO: in case we are not calling this class directly, check if function is overwritten and throw exception otherwise
25
                foreach (self::$registeredObjectTypes as $ot) {
26
                        if ($obj = $ot::parse($node_id)) return $obj;
27
                }
28
                return null;
29
        }
30
 
31
        public abstract static function objectTypeTitle();
32
 
33
        public abstract static function objectTypeTitleShort();
34
 
35
        public abstract static function ns();
36
 
37
        public abstract static function root();
38
 
39
        public abstract function isRoot();
40
 
41
        public abstract function nodeId();
42
 
43
        public abstract function addString($str);
44
 
45
        public abstract function crudShowId(OIDplusObject $parent);
46
 
47
        public abstract function crudInsertPrefix();
48
 
49
        public abstract function jsTreeNodeName(OIDplusObject $parent = null);
50
 
51
        public abstract function defaultTitle();
52
 
53
        public abstract function getContentPage(&$title, &$content);
54
 
55
        private static function getRaRoots_rec($parent=null, $ra_email=null, &$out, $prev_owns) {
56
                if (is_null($parent)) {
57
                        $roots = array();
58
                        foreach (self::$registeredObjectTypes as $ot) {
59
                                $roots[] = "parent = '" . OIDplus::db()->real_escape_string($ot::root()) . "'";
60
                        }
61
                        $roots = implode(' or ', $roots);
62
                } else {
63
                        $roots = "parent = '" . OIDplus::db()->real_escape_string($parent) . "'";
64
                }
65
 
66
                $res = OIDplus::db()->query("select id, ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where $roots order by ".OIDplus::db()->natOrder('id'));
67
 
68
                $this_owns = array();
69
                $this_all = array();
70
                while ($row = OIDplus::db()->fetch_array($res)) {
71
                        if (is_null($ra_email)) {
72
                                // if $ra_email is null, we want to query only the roots of the currently logged in user
73
                                $owns = OIDplus::authUtils()::isRaLoggedIn($row['ra_email']);
74
                        } else {
75
                                $owns = $row['ra_email'] == $ra_email;
76
                        }
77
 
78
                        if ($owns) $this_owns[] = $row['id'];
79
                        $this_all[] = $row['id'];
80
                }
81
 
82
                foreach ($this_owns as $this_ra) {
83
                        $nogap = true;
84
                        if (!is_null($prev_owns)) {
85
                                // Check if we have any gaps. If there is a "gap" in the hierarchy, then we need to count that as a second root of that RA ("reintroduce ownership")
86
                                foreach ($prev_owns as $prev) {
87
                                        if (oid_up(explode(':',$this_ra)[1]) == explode(':',$prev)[1]) $nogap = false;
88
                                }
89
                        }
90
                        if ($nogap) $out[] = self::parse($this_ra);
91
                }
92
 
93
                foreach ($this_all as $this_ra) {
94
                        self::getRaRoots_rec($this_ra, $ra_email, $out, $this_owns);
95
                }
96
        }
97
 
98
        public static function getRaRoots($ra_email=null) {
99
                $out = array();
100
                self::getRaRoots_rec(null, $ra_email, $out, null);
101
                return $out;
102
        }
103
 
104
        private static function getAllNonConfidential_rec($parent=null, &$out) {
105
                if (is_null($parent)) {
106
                        $roots = array();
107
                        foreach (self::$registeredObjectTypes as $ot) {
108
                                $roots[] = "parent = '" . OIDplus::db()->real_escape_string($ot::root()) . "'";
109
                        }
110
                        $roots = implode(' or ', $roots);
111
                } else {
112
                        $roots = "parent = '" . OIDplus::db()->real_escape_string($parent) . "'";
113
                }
114
 
115
                $res = OIDplus::db()->query("select id, confidential from ".OIDPLUS_TABLENAME_PREFIX."objects where $roots order by ".OIDplus::db()->natOrder('id'));
116
 
117
                while ($row = OIDplus::db()->fetch_array($res)) {
118
                        if ($row['confidential'] == '1') {
119
                                // do nothing
120
                        } else {
121
                                $out[] = $row['id'];
122
                                self::getAllNonConfidential_rec($row['id'], $out);
123
                        }
124
                }
125
        }
126
 
127
        public static function getAllNonConfidential() {
128
                $out = array();
129
                self::getAllNonConfidential_rec(null, $out);
130
                return $out;
131
        }
132
 
133
        public function isConfidential() {
134
                $curid = $this->nodeId();
135
                // Recursively search for the confidential flag in the parents
136
                while (OIDplus::db()->num_rows($res = OIDplus::db()->query("select parent, confidential from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($curid)."'")) > 0) {
137
                        $row = OIDplus::db()->fetch_array($res);
138
                        if ($row['confidential']) return true;
139
                        $curid = $row['parent'];
140
                }
141
 
142
                return false;
143
        }
144
 
145
        public function isChildOf(OIDplusObject $obj) {
146
                $curid = $this->nodeId();
147
                while (OIDplus::db()->num_rows($res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($curid)."'")) > 0) {
148
                        $row = OIDplus::db()->fetch_array($res);
149
                        if ($curid == $obj->nodeId()) return true;
150
                        $curid = $row['parent'];
151
                }
152
 
153
                return false;
154
        }
155
 
156
        public function userHasReadRights($ra_email=null) {
157
                // Admin may do everything
158
                if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
159
 
160
                // If it is not confidential, everybody can read/see it.
161
                if (!$this->isConfidential()) return true;
162
 
163
                // If we own the object, we may see it
164
                if (is_null($ra_email)) {
165
                        if ($this->userHasWriteRights()) return true;
166
                } else {
167
                        $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($this->nodeId())."'");
168
                        $row = OIDplus::db()->fetch_array($res);
169
                        if ($row['ra_email'] == $ra_email) return true;
170
                }
171
 
172
                // If someone has rights to an object below our confidential node,
173
                // we let him see the confidential node,
174
                // Otherwise he could not browse through to his own node.
175
                $roots = $this->getRaRoots($ra_email);
176
                foreach ($roots as $root) {
177
                        if ($root->isChildOf($this)) return true;
178
                }
179
 
180
                return false;
181
        }
182
 
183
        public function getIcon($row=null) {
184
                $namespace = self::parse($this->nodeId())::ns(); // TODO: warum muss ich das machen??? $this::ns() gibt abstrakten fehler
185
 
186
                if (is_null($row)) {
187
                        $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($this->nodeId())."'");
188
                        $row = OIDplus::db()->fetch_array($res);
189
                }
190
                // TODO: have different icons for Leaf-Nodes
191
                if (OIDplus::authUtils()::isRaLoggedIn($row['ra_email'])) {
192
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_own.png';
193
                } else {
194
                        $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_general.png';
195
                }
196
                if (!file_exists($icon)) $icon = null; // default icon (folder)
197
                return $icon;
198
        }
199
 
12 daniel-mar 200
        public static function exists($id) {
201
                $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
202
                return OIDplus::db()->num_rows($res) > 0;
203
        }
204
 
2 daniel-mar 205
        public function getParent() {
206
                $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($this->nodeId())."'");
207
                $row = OIDplus::db()->fetch_array($res);
208
                $parent = $row['parent'];
209
                return OIDplusObject::parse($parent);
210
        }
211
 
212
        public function getRaMail() {
213
                $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($this->nodeId())."'");
214
                $row = OIDplus::db()->fetch_array($res);
215
                return $row['ra_email'];
216
        }
217
 
218
        public function userHasParentalWriteRights($ra_email=null) {
219
                if (is_null($ra_email)) {
220
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
221
                }
222
 
223
                $objParent = $this->getParent();
224
                if (is_null($objParent)) return false;
225
                return $objParent->userHasWriteRights($ra_email);
226
        }
227
 
228
        public function userHasWriteRights($ra_email=null) {
229
                if (is_null($ra_email)) {
230
                        if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
231
                        return OIDplus::authUtils()::isRaLoggedIn($this->getRaMail());
232
                } else {
233
                        return $this->getRaMail() == $ra_email;
234
                }
235
        }
12 daniel-mar 236
 
237
        public function distance($to) {
238
                return null; // not implemented
239
        }
2 daniel-mar 240
}