Rev 549 | Rev 635 | 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 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
2 | 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 | |||
511 | daniel-mar | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | |||
261 | daniel-mar | 22 | abstract class OIDplusObject { |
23 | const UUID_NAMEBASED_NS_OidPlusMisc = 'ad1654e6-7e15-11e4-9ef6-78e3b5fc7f22'; |
||
150 | daniel-mar | 24 | |
2 | daniel-mar | 25 | public static function parse($node_id) { // please overwrite this function! |
26 | // TODO: in case we are not calling this class directly, check if function is overwritten and throw exception otherwise |
||
227 | daniel-mar | 27 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
2 | daniel-mar | 28 | if ($obj = $ot::parse($node_id)) return $obj; |
29 | } |
||
30 | return null; |
||
31 | } |
||
32 | |||
228 | daniel-mar | 33 | public function /*OIDplusAltId[]*/ getAltIds() { |
193 | daniel-mar | 34 | if ($this->isRoot()) return array(); |
35 | |||
36 | $ids = array(); |
||
37 | if ($this->ns() != 'oid') { |
||
38 | // Creates an OIDplus-Hash-OID |
||
39 | // If the object type has a better way of defining an OID, please override this method! |
||
227 | daniel-mar | 40 | $sid = OIDplus::getSystemId(true); |
193 | daniel-mar | 41 | if (!empty($sid)) { |
42 | $oid = $sid . '.' . smallhash($this->nodeId()); |
||
360 | daniel-mar | 43 | $ids[] = new OIDplusAltId('oid', $oid, _L('OIDplus Information Object ID')); |
193 | daniel-mar | 44 | } |
45 | } |
||
46 | if ($this->ns() != 'guid') { |
||
360 | daniel-mar | 47 | $ids[] = new OIDplusAltId('guid', gen_uuid_md5_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), _L('Name based version 3 / MD5 UUID with namespace %1','UUID_NAMEBASED_NS_OidPlusMisc')); |
48 | $ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), _L('Name based version 5 / SHA1 UUID with namespace %1','UUID_NAMEBASED_NS_OidPlusMisc')); |
||
193 | daniel-mar | 49 | } |
50 | return $ids; |
||
83 | daniel-mar | 51 | } |
52 | |||
2 | daniel-mar | 53 | public abstract static function objectTypeTitle(); |
54 | |||
55 | public abstract static function objectTypeTitleShort(); |
||
56 | |||
57 | public abstract static function ns(); |
||
58 | |||
59 | public abstract static function root(); |
||
60 | |||
61 | public abstract function isRoot(); |
||
62 | |||
247 | daniel-mar | 63 | public abstract function nodeId($with_ns=true); |
2 | daniel-mar | 64 | |
65 | public abstract function addString($str); |
||
66 | |||
67 | public abstract function crudShowId(OIDplusObject $parent); |
||
68 | |||
69 | public abstract function crudInsertPrefix(); |
||
70 | |||
71 | public abstract function jsTreeNodeName(OIDplusObject $parent = null); |
||
72 | |||
73 | public abstract function defaultTitle(); |
||
74 | |||
16 | daniel-mar | 75 | public abstract function isLeafNode(); |
76 | |||
34 | daniel-mar | 77 | public abstract function getContentPage(&$title, &$content, &$icon); |
2 | daniel-mar | 78 | |
27 | daniel-mar | 79 | public static function getRaRoots($ra_email=null) { |
115 | daniel-mar | 80 | if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail(); |
81 | |||
27 | daniel-mar | 82 | $out = array(); |
150 | daniel-mar | 83 | |
261 | daniel-mar | 84 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
150 | daniel-mar | 85 | if (is_null($ra_email)) { |
261 | daniel-mar | 86 | $res = OIDplus::db()->query("select oChild.id as id, oChild.ra_email as child_mail, oParent.ra_email as parent_mail from ###objects as oChild ". |
87 | "left join ###objects as oParent on oChild.parent = oParent.id ". |
||
150 | daniel-mar | 88 | "order by ".OIDplus::db()->natOrder('oChild.id')); |
236 | daniel-mar | 89 | while ($row = $res->fetch_array()) { |
549 | daniel-mar | 90 | if (!OIDplus::authUtils()->isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()->isRaLoggedIn($row['child_mail'])) { |
150 | daniel-mar | 91 | $x = self::parse($row['id']); // can be FALSE if namespace was disabled |
92 | if ($x) $out[] = $x; |
||
93 | } |
||
94 | } |
||
95 | } else { |
||
261 | daniel-mar | 96 | $res = OIDplus::db()->query("select oChild.id as id from ###objects as oChild ". |
97 | "left join ###objects as oParent on oChild.parent = oParent.id ". |
||
433 | daniel-mar | 98 | "where (".OIDplus::db()->getSlang()->isNullFunction('oParent.ra_email',"''")." <> ? and ". |
99 | OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) or ". |
||
100 | " (oParent.ra_email is null and ".OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) ". |
||
150 | daniel-mar | 101 | "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email)); |
236 | daniel-mar | 102 | while ($row = $res->fetch_array()) { |
68 | daniel-mar | 103 | $x = self::parse($row['id']); // can be FALSE if namespace was disabled |
150 | daniel-mar | 104 | if ($x) $out[] = self::parse($row['id']); |
27 | daniel-mar | 105 | } |
2 | daniel-mar | 106 | } |
107 | } else { |
||
150 | daniel-mar | 108 | if (is_null($ra_email)) { |
415 | daniel-mar | 109 | $ra_mails_to_check = OIDplus::authUtils()->loggedInRaList(); |
150 | daniel-mar | 110 | if (count($ra_mails_to_check) == 0) return $out; |
111 | } else { |
||
112 | $ra_mails_to_check = array($ra_email); |
||
2 | daniel-mar | 113 | } |
150 | daniel-mar | 114 | |
115 | self::buildObjectInformationCache(); |
||
116 | |||
117 | foreach ($ra_mails_to_check as $check_ra_mail) { |
||
193 | daniel-mar | 118 | $out_part = array(); |
150 | daniel-mar | 119 | |
193 | daniel-mar | 120 | foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) { |
121 | // If the OID RA is the RA we are searching, then add the object to the choice list |
||
122 | if ($ra_email == $check_ra_mail) $out_part[] = $id; |
||
150 | daniel-mar | 123 | } |
124 | |||
193 | daniel-mar | 125 | foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) { |
126 | if (isset(self::$object_info_cache[$parent])) { |
||
127 | if (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] == $ra_email) { |
||
128 | // if the parent has the same RA, then this OID cannot be a root => remove the element from the choice list |
||
129 | foreach (array_keys($out_part, $id) as $key) unset($out_part[$key]); |
||
150 | daniel-mar | 130 | } |
131 | } |
||
132 | } |
||
133 | |||
134 | natsort($out_part); |
||
135 | |||
136 | foreach ($out_part as $id) { |
||
137 | $obj = self::parse($id); |
||
138 | if ($obj) $out[] = $obj; |
||
139 | } |
||
140 | } |
||
2 | daniel-mar | 141 | } |
150 | daniel-mar | 142 | |
2 | daniel-mar | 143 | return $out; |
144 | } |
||
145 | |||
150 | daniel-mar | 146 | public static function getAllNonConfidential() { |
147 | $out = array(); |
||
148 | |||
261 | daniel-mar | 149 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
443 | daniel-mar | 150 | $res = OIDplus::db()->query("select id from ###objects where confidential = ? order by ".OIDplus::db()->natOrder('id'), array(false)); |
150 | daniel-mar | 151 | |
236 | daniel-mar | 152 | while ($row = $res->fetch_array()) { |
150 | daniel-mar | 153 | $obj = self::parse($row['id']); // will be NULL if the object type is not registered |
169 | daniel-mar | 154 | if ($obj && (!$obj->isConfidential())) { |
150 | daniel-mar | 155 | $out[] = $row['id']; |
156 | } |
||
2 | daniel-mar | 157 | } |
158 | } else { |
||
150 | daniel-mar | 159 | self::buildObjectInformationCache(); |
2 | daniel-mar | 160 | |
193 | daniel-mar | 161 | foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) { |
150 | daniel-mar | 162 | if (!$confidential) { |
163 | $obj = self::parse($id); // will be NULL if the object type is not registered |
||
169 | daniel-mar | 164 | if ($obj && (!$obj->isConfidential())) { |
150 | daniel-mar | 165 | $out[] = $id; |
166 | } |
||
167 | } |
||
2 | daniel-mar | 168 | } |
169 | } |
||
170 | |||
171 | return $out; |
||
172 | } |
||
173 | |||
174 | public function isConfidential() { |
||
261 | daniel-mar | 175 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
426 | daniel-mar | 176 | //static $confidential_cache = array(); |
150 | daniel-mar | 177 | $curid = $this->nodeId(); |
426 | daniel-mar | 178 | //$orig_curid = $curid; |
179 | //if (isset($confidential_cache[$curid])) return $confidential_cache[$curid]; |
||
150 | daniel-mar | 180 | // Recursively search for the confidential flag in the parents |
261 | daniel-mar | 181 | while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->num_rows() > 0) { |
236 | daniel-mar | 182 | $row = $res->fetch_array(); |
150 | daniel-mar | 183 | if ($row['confidential']) { |
426 | daniel-mar | 184 | //$confidential_cache[$curid] = true; |
185 | //$confidential_cache[$orig_curid] = true; |
||
150 | daniel-mar | 186 | return true; |
187 | } else { |
||
426 | daniel-mar | 188 | //$confidential_cache[$curid] = false; |
150 | daniel-mar | 189 | } |
190 | $curid = $row['parent']; |
||
426 | daniel-mar | 191 | //if (isset($confidential_cache[$curid])) { |
192 | //$confidential_cache[$orig_curid] = $confidential_cache[$curid]; |
||
193 | //return $confidential_cache[$curid]; |
||
194 | //} |
||
150 | daniel-mar | 195 | } |
196 | |||
426 | daniel-mar | 197 | //$confidential_cache[$orig_curid] = false; |
150 | daniel-mar | 198 | return false; |
199 | } else { |
||
200 | self::buildObjectInformationCache(); |
||
201 | |||
202 | $curid = $this->nodeId(); |
||
203 | // Recursively search for the confidential flag in the parents |
||
169 | daniel-mar | 204 | while (isset(self::$object_info_cache[$curid])) { |
150 | daniel-mar | 205 | if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true; |
206 | $curid = self::$object_info_cache[$curid][self::CACHE_PARENT]; |
||
207 | } |
||
208 | return false; |
||
2 | daniel-mar | 209 | } |
210 | } |
||
211 | |||
212 | public function isChildOf(OIDplusObject $obj) { |
||
261 | daniel-mar | 213 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
150 | daniel-mar | 214 | $curid = $this->nodeId(); |
261 | daniel-mar | 215 | while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->num_rows() > 0) { |
236 | daniel-mar | 216 | $row = $res->fetch_array(); |
150 | daniel-mar | 217 | if ($curid == $obj->nodeId()) return true; |
218 | $curid = $row['parent']; |
||
219 | } |
||
220 | return false; |
||
221 | } else { |
||
222 | self::buildObjectInformationCache(); |
||
223 | |||
224 | $curid = $this->nodeId(); |
||
169 | daniel-mar | 225 | while (isset(self::$object_info_cache[$curid])) { |
150 | daniel-mar | 226 | if ($curid == $obj->nodeId()) return true; |
227 | $curid = self::$object_info_cache[$curid][self::CACHE_PARENT]; |
||
228 | } |
||
229 | return false; |
||
2 | daniel-mar | 230 | } |
150 | daniel-mar | 231 | } |
2 | daniel-mar | 232 | |
150 | daniel-mar | 233 | public function getChildren() { |
234 | $out = array(); |
||
261 | daniel-mar | 235 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
236 | $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId())); |
||
236 | daniel-mar | 237 | while ($row = $res->fetch_array()) { |
150 | daniel-mar | 238 | $obj = self::parse($row['id']); |
239 | if (!$obj) continue; |
||
240 | $out[] = $obj; |
||
241 | } |
||
242 | } else { |
||
243 | self::buildObjectInformationCache(); |
||
244 | |||
193 | daniel-mar | 245 | foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) { |
150 | daniel-mar | 246 | if ($parent == $this->nodeId()) { |
247 | $obj = self::parse($id); |
||
248 | if (!$obj) continue; |
||
249 | $out[] = $obj; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | return $out; |
||
2 | daniel-mar | 254 | } |
255 | |||
115 | daniel-mar | 256 | public function getRa() { |
150 | daniel-mar | 257 | return new OIDplusRA($this->getRaMail()); |
115 | daniel-mar | 258 | } |
259 | |||
2 | daniel-mar | 260 | public function userHasReadRights($ra_email=null) { |
115 | daniel-mar | 261 | if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail(); |
262 | |||
2 | daniel-mar | 263 | // If it is not confidential, everybody can read/see it. |
416 | daniel-mar | 264 | // Note: This also checks if superior OIDs are confidential. |
2 | daniel-mar | 265 | if (!$this->isConfidential()) return true; |
266 | |||
267 | if (is_null($ra_email)) { |
||
416 | daniel-mar | 268 | // Admin may do everything |
549 | daniel-mar | 269 | if (OIDplus::authUtils()->isAdminLoggedIn()) return true; |
416 | daniel-mar | 270 | |
271 | // If the RA is logged in, then they can see the OID. |
||
549 | daniel-mar | 272 | if (OIDplus::authUtils()->isRaLoggedIn($this->getRaMail())) return true; |
2 | daniel-mar | 273 | } else { |
416 | daniel-mar | 274 | // If this OID belongs to the requested RA, then they may see it. |
150 | daniel-mar | 275 | if ($this->getRaMail() == $ra_email) return true; |
2 | daniel-mar | 276 | } |
277 | |||
278 | // If someone has rights to an object below our confidential node, |
||
279 | // we let him see the confidential node, |
||
280 | // Otherwise he could not browse through to his own node. |
||
281 | $roots = $this->getRaRoots($ra_email); |
||
282 | foreach ($roots as $root) { |
||
283 | if ($root->isChildOf($this)) return true; |
||
284 | } |
||
285 | |||
286 | return false; |
||
287 | } |
||
288 | |||
289 | public function getIcon($row=null) { |
||
20 | daniel-mar | 290 | $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called |
2 | daniel-mar | 291 | |
292 | if (is_null($row)) { |
||
150 | daniel-mar | 293 | $ra_email = $this->getRaMail(); |
294 | } else { |
||
295 | $ra_email = $row['ra_email']; |
||
2 | daniel-mar | 296 | } |
297 | // TODO: have different icons for Leaf-Nodes |
||
632 | daniel-mar | 298 | |
299 | $dirs = array_merge( |
||
300 | glob(OIDplus::localpath().'plugins/objectTypes/'.$namespace.'/'), |
||
301 | glob(OIDplus::localpath().'plugins/_thirdParty/'.'*'.'/objectTypes/'.$namespace.'/') |
||
302 | ); |
||
303 | |||
304 | if (count($dirs) == 0) return null; // default icon (folder) |
||
305 | |||
306 | $dir = substr($dirs[0], strlen(OIDplus::localpath())); |
||
307 | |||
549 | daniel-mar | 308 | if (OIDplus::authUtils()->isRaLoggedIn($ra_email)) { |
632 | daniel-mar | 309 | $icon = $dir.'/img/treeicon_own.png'; |
2 | daniel-mar | 310 | } else { |
632 | daniel-mar | 311 | $icon = $dir.'/img/treeicon_general.png'; |
2 | daniel-mar | 312 | } |
632 | daniel-mar | 313 | |
314 | if (!file_exists($icon)) return null; // default icon (folder) |
||
315 | |||
2 | daniel-mar | 316 | return $icon; |
317 | } |
||
318 | |||
12 | daniel-mar | 319 | public static function exists($id) { |
261 | daniel-mar | 320 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
321 | $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id)); |
||
236 | daniel-mar | 322 | return $res->num_rows() > 0; |
150 | daniel-mar | 323 | } else { |
324 | self::buildObjectInformationCache(); |
||
325 | return isset(self::$object_info_cache[$id]); |
||
326 | } |
||
12 | daniel-mar | 327 | } |
328 | |||
415 | daniel-mar | 329 | // Get parent gives the next possible parent which is EXISTING in OIDplus |
330 | // It does not give the immediate parent |
||
2 | daniel-mar | 331 | public function getParent() { |
261 | daniel-mar | 332 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
333 | $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId())); |
||
251 | daniel-mar | 334 | if ($res->num_rows() == 0) return null; |
236 | daniel-mar | 335 | $row = $res->fetch_array(); |
150 | daniel-mar | 336 | $parent = $row['parent']; |
337 | $obj = OIDplusObject::parse($parent); |
||
338 | if ($obj) return $obj; |
||
415 | daniel-mar | 339 | // TODO: Also implement one_up() like below |
150 | daniel-mar | 340 | } else { |
341 | self::buildObjectInformationCache(); |
||
342 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
343 | $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT]; |
||
344 | $obj = OIDplusObject::parse($parent); |
||
345 | if ($obj) return $obj; |
||
346 | } |
||
20 | daniel-mar | 347 | |
150 | daniel-mar | 348 | // If this OID does not exist, the SQL query "select parent from ..." does not work. So we try to find the next possible parent using one_up() |
349 | $cur = $this->one_up(); |
||
418 | daniel-mar | 350 | if (!$cur) return null; |
150 | daniel-mar | 351 | do { |
415 | daniel-mar | 352 | // findFitting() checks if that OID exists |
150 | daniel-mar | 353 | if ($fitting = self::findFitting($cur->nodeId())) return $fitting; |
20 | daniel-mar | 354 | |
150 | daniel-mar | 355 | $prev = $cur; |
356 | $cur = $cur->one_up(); |
||
418 | daniel-mar | 357 | if (!$cur) return null; |
150 | daniel-mar | 358 | } while ($prev != $cur); |
359 | |||
418 | daniel-mar | 360 | return null; |
150 | daniel-mar | 361 | } |
2 | daniel-mar | 362 | } |
363 | |||
364 | public function getRaMail() { |
||
261 | daniel-mar | 365 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
366 | $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId())); |
||
251 | daniel-mar | 367 | if ($res->num_rows() == 0) return null; |
236 | daniel-mar | 368 | $row = $res->fetch_array(); |
150 | daniel-mar | 369 | return $row['ra_email']; |
370 | } else { |
||
371 | self::buildObjectInformationCache(); |
||
372 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
373 | return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL]; |
||
374 | } |
||
375 | return false; |
||
376 | } |
||
2 | daniel-mar | 377 | } |
378 | |||
192 | daniel-mar | 379 | public function getTitle() { |
261 | daniel-mar | 380 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
381 | $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId())); |
||
251 | daniel-mar | 382 | if ($res->num_rows() == 0) return null; |
236 | daniel-mar | 383 | $row = $res->fetch_array(); |
192 | daniel-mar | 384 | return $row['title']; |
385 | } else { |
||
386 | self::buildObjectInformationCache(); |
||
387 | if (isset(self::$object_info_cache[$this->nodeId()])) { |
||
388 | return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE]; |
||
389 | } |
||
390 | return false; |
||
391 | } |
||
392 | } |
||
393 | |||
2 | daniel-mar | 394 | public function userHasParentalWriteRights($ra_email=null) { |
115 | daniel-mar | 395 | if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail(); |
396 | |||
2 | daniel-mar | 397 | if (is_null($ra_email)) { |
549 | daniel-mar | 398 | if (OIDplus::authUtils()->isAdminLoggedIn()) return true; |
2 | daniel-mar | 399 | } |
400 | |||
401 | $objParent = $this->getParent(); |
||
419 | daniel-mar | 402 | if (!$objParent) return false; |
2 | daniel-mar | 403 | return $objParent->userHasWriteRights($ra_email); |
404 | } |
||
405 | |||
406 | public function userHasWriteRights($ra_email=null) { |
||
115 | daniel-mar | 407 | if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail(); |
408 | |||
2 | daniel-mar | 409 | if (is_null($ra_email)) { |
549 | daniel-mar | 410 | if (OIDplus::authUtils()->isAdminLoggedIn()) return true; |
411 | return OIDplus::authUtils()->isRaLoggedIn($this->getRaMail()); |
||
2 | daniel-mar | 412 | } else { |
413 | return $this->getRaMail() == $ra_email; |
||
414 | } |
||
415 | } |
||
12 | daniel-mar | 416 | |
417 | public function distance($to) { |
||
418 | return null; // not implemented |
||
419 | } |
||
20 | daniel-mar | 420 | |
421 | public function equals($obj) { |
||
422 | if (!is_object($obj)) $obj = OIDplusObject::parse($obj); |
||
28 | daniel-mar | 423 | if (!($obj instanceof $this)) return false; |
424 | |||
20 | daniel-mar | 425 | $distance = $this->distance($obj); |
426 | if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it |
||
427 | |||
428 | return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive |
||
429 | } |
||
430 | |||
431 | public static function findFitting($id) { |
||
432 | $obj = OIDplusObject::parse($id); |
||
360 | daniel-mar | 433 | if (!$obj) throw new OIDplusException(_L('findFitting: Parse failed')); |
20 | daniel-mar | 434 | |
261 | daniel-mar | 435 | if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) { |
436 | $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%')); |
||
236 | daniel-mar | 437 | while ($row = $res->fetch_object()) { |
150 | daniel-mar | 438 | $test = OIDplusObject::parse($row->id); |
439 | if ($obj->equals($test)) return $test; |
||
440 | } |
||
441 | return false; |
||
442 | } else { |
||
443 | self::buildObjectInformationCache(); |
||
193 | daniel-mar | 444 | foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) { |
150 | daniel-mar | 445 | if (strpos($id, $obj->ns().':') === 0) { |
446 | $test = OIDplusObject::parse($id); |
||
447 | if ($obj->equals($test)) return $test; |
||
448 | } |
||
449 | } |
||
450 | return false; |
||
20 | daniel-mar | 451 | } |
452 | } |
||
453 | |||
454 | public function one_up() { |
||
455 | return null; // not implemented |
||
456 | } |
||
150 | daniel-mar | 457 | |
458 | // Caching stuff |
||
459 | |||
460 | protected static $object_info_cache = null; |
||
461 | |||
462 | public static function resetObjectInformationCache() { |
||
463 | self::$object_info_cache = null; |
||
464 | } |
||
465 | |||
193 | daniel-mar | 466 | const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc. |
150 | daniel-mar | 467 | const CACHE_PARENT = 1; |
468 | const CACHE_RA_EMAIL = 2; |
||
192 | daniel-mar | 469 | const CACHE_TITLE = 3; |
150 | daniel-mar | 470 | |
471 | private static function buildObjectInformationCache() { |
||
472 | if (is_null(self::$object_info_cache)) { |
||
473 | self::$object_info_cache = array(); |
||
261 | daniel-mar | 474 | $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects"); |
236 | daniel-mar | 475 | while ($row = $res->fetch_array()) { |
192 | daniel-mar | 476 | self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']); |
150 | daniel-mar | 477 | } |
478 | } |
||
479 | } |
||
513 | daniel-mar | 480 | |
481 | // override this function if you want your object type to save |
||
482 | // attachments in directories with easy names. |
||
483 | // Take care that your custom directory name will not allow jailbreaks (../) ! |
||
484 | public function getDirectoryName() { |
||
514 | daniel-mar | 485 | if ($this->isRoot()) return $this->ns(); |
486 | return $this->getLegacyDirectoryName(); |
||
513 | daniel-mar | 487 | } |
488 | |||
514 | daniel-mar | 489 | public final function getLegacyDirectoryName() { |
513 | daniel-mar | 490 | if ($this::ns() == 'oid') { |
491 | $oid = $this->nodeId(false); |
||
492 | } else { |
||
493 | $oid = null; |
||
494 | $alt_ids = $this->getAltIds(); |
||
495 | foreach ($alt_ids as $alt_id) { |
||
496 | if ($alt_id->getNamespace() == 'oid') { |
||
497 | $oid = $alt_id->getId(); |
||
498 | break; // we prefer the first OID (for GUIDs, the first OID is the OIDplus-OID, and the second OID is the UUID OID) |
||
499 | } |
||
500 | } |
||
501 | } |
||
502 | |||
503 | if (!is_null($oid) && ($oid != '')) { |
||
504 | // For OIDs, it is the OID, for other identifiers |
||
505 | // it it the OID alt ID (generated using the SystemID) |
||
506 | return str_replace('.', '_', $oid); |
||
507 | } else { |
||
508 | // Can happen if you don't have a system ID (due to missing OpenSSL plugin) |
||
509 | return md5($this->nodeId(true)); // we don't use $id, because $this->nodeId(true) is possibly more canonical than $id |
||
510 | } |
||
511 | } |
||
415 | daniel-mar | 512 | } |