Subversion Repositories oidplus

Rev

Rev 1028 | Rev 1078 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2022 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. abstract class OIDplusObject extends OIDplusBaseClass {
  23.         const UUID_NAMEBASED_NS_OidPlusMisc = 'ad1654e6-7e15-11e4-9ef6-78e3b5fc7f22';
  24.  
  25.         public static function parse($node_id) { // please overwrite this function!
  26.                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  27.                         try {
  28.                                 $good = false;
  29.                                 if (get_parent_class($ot) == OIDplusObject::class) {
  30.                                         $reflector = new \ReflectionMethod($ot, 'parse');
  31.                                         $isImplemented = ($reflector->getDeclaringClass()->getName() === $ot);
  32.                                         if ($isImplemented) { // avoid endless loop if parse is not overriden
  33.                                                 $good = true;
  34.                                         }
  35.                                 }
  36.                                 // We need to do the workaround with "$good", otherwise PHPstan shows
  37.                                 // "Call to an undefined static method object::parse()"
  38.                                 if ($good && $obj = $ot::parse($node_id)) return $obj;
  39.                         } catch (\Exception $e) {}
  40.                 }
  41.                 return null;
  42.         }
  43.  
  44.         public function /*OIDplusAltId[]*/ getAltIds() {
  45.                 if ($this->isRoot()) return array();
  46.  
  47.                 $ids = array();
  48.                 if ($this->ns() != 'oid') {
  49.                         // Creates an OIDplus-Hash-OID
  50.                         $sid = OIDplus::getSystemId(true);
  51.                         if (!empty($sid)) {
  52.                                 $ns_oid = $this->getPlugin()->getManifest()->getOid();
  53.                                 if (str_starts_with($ns_oid, '1.3.6.1.4.1.37476.2.5.2.')) {
  54.                                         // Official ViaThinkSoft object type plugins
  55.                                         // For backwards compatibility with existing IDs,
  56.                                         // set the hash_payload as '<namespace>:<id>'
  57.                                         $hash_payload = $this->nodeId(true);
  58.                                 } else {
  59.                                         // Third-party object type plugins
  60.                                         // Set the hash_payload as '<plugin oid>:<id>'
  61.                                         $hash_payload = $ns_oid.':'.$this->nodeId(false);
  62.                                 }
  63.                                 $oid = $sid . '.' . smallhash($hash_payload);
  64.                                 $ids[] = new OIDplusAltId('oid', $oid, _L('OIDplus Information Object ID'));
  65.                         }
  66.  
  67.                         // Make a namebased UUID, but...
  68.                         // ... exclude GUID, because a GUID is already a GUID
  69.                         // ... exclude OID, because an OID already has a record UUID_NAMEBASED_NS_OID set  by class OIDplusOid
  70.                         if ($this->ns() != 'guid') {
  71.                                 $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'));
  72.                                 $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'));
  73.                         }
  74.                 }
  75.                 return $ids;
  76.         }
  77.  
  78.         public abstract static function objectTypeTitle();
  79.  
  80.         public abstract static function objectTypeTitleShort();
  81.  
  82.         public function getPlugin()/*: ?OIDplusObjectTypePlugin */ {
  83.                 $res = null;
  84.                 $plugins = OIDplus::getObjectTypePlugins();
  85.                 foreach ($plugins as $plugin) {
  86.                         if (get_class($this) == $plugin::getObjectTypeClassName($this)) {
  87.                                 return $plugin;
  88.                         }
  89.                 }
  90.                 return $res;
  91.         }
  92.  
  93.         public abstract static function ns();
  94.  
  95.         public abstract static function root();
  96.  
  97.         public abstract function isRoot();
  98.  
  99.         public abstract function nodeId($with_ns=true);
  100.  
  101.         public abstract function addString($str);
  102.  
  103.         public abstract function crudShowId(OIDplusObject $parent);
  104.  
  105.         public function crudInsertPrefix() {
  106.                 return '';
  107.         }
  108.  
  109.         public function crudInsertSuffix() {
  110.                 return '';
  111.         }
  112.  
  113.         public abstract function jsTreeNodeName(OIDplusObject $parent = null);
  114.  
  115.         public abstract function defaultTitle();
  116.  
  117.         public abstract function isLeafNode();
  118.  
  119.         public abstract function getContentPage(&$title, &$content, &$icon);
  120.  
  121.         public static function getRaRoots($ra_email=null) {
  122.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  123.  
  124.                 $out = array();
  125.  
  126.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  127.                         if (!$ra_email) {
  128.                                 $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 ".
  129.                                                             "left join ###objects as oParent on oChild.parent = oParent.id ".
  130.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'));
  131.                                 while ($row = $res->fetch_array()) {
  132.                                         if (!OIDplus::authUtils()->isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()->isRaLoggedIn($row['child_mail'])) {
  133.                                                 $x = self::parse($row['id']); // can be FALSE if namespace was disabled
  134.                                                 if ($x) $out[] = $x;
  135.                                         }
  136.                                 }
  137.                         } else {
  138.                                 $res = OIDplus::db()->query("select oChild.id as id from ###objects as oChild ".
  139.                                                             "left join ###objects as oParent on oChild.parent = oParent.id ".
  140.                                                             "where (".OIDplus::db()->getSlang()->isNullFunction('oParent.ra_email',"''")." <> ? and ".
  141.                                                             OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) or ".
  142.                                                             "      (oParent.ra_email is null and ".OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) ".
  143.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email));
  144.                                 while ($row = $res->fetch_array()) {
  145.                                         $x = self::parse($row['id']); // can be FALSE if namespace was disabled
  146.                                         if ($x) $out[] = $x;
  147.                                 }
  148.                         }
  149.                 } else {
  150.                         if (!$ra_email) {
  151.                                 $ra_mails_to_check = OIDplus::authUtils()->loggedInRaList();
  152.                                 if (count($ra_mails_to_check) == 0) return $out;
  153.                         } else {
  154.                                 $ra_mails_to_check = array($ra_email);
  155.                         }
  156.  
  157.                         self::buildObjectInformationCache();
  158.  
  159.                         foreach ($ra_mails_to_check as $check_ra_mail) {
  160.                                 $out_part = array();
  161.  
  162.                                 foreach (self::$object_info_cache as $id => $cacheitem) {
  163.                                         if ($cacheitem[self::CACHE_RA_EMAIL] == $check_ra_mail) {
  164.                                                 $parent = $cacheitem[self::CACHE_PARENT];
  165.                                                 if (!isset(self::$object_info_cache[$parent]) || (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] != $check_ra_mail)) {
  166.                                                         $out_part[] = $id;
  167.                                                 }
  168.                                         }
  169.                                 }
  170.  
  171.                                 natsort($out_part);
  172.  
  173.                                 foreach ($out_part as $id) {
  174.                                         $obj = self::parse($id);
  175.                                         if ($obj) $out[] = $obj;
  176.                                 }
  177.                         }
  178.                 }
  179.  
  180.                 return $out;
  181.         }
  182.  
  183.         public static function getAllNonConfidential() {
  184.                 $out = array();
  185.  
  186.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  187.                         $res = OIDplus::db()->query("select id from ###objects where confidential = ? order by ".OIDplus::db()->natOrder('id'), array(false));
  188.  
  189.                         while ($row = $res->fetch_array()) {
  190.                                 $obj = self::parse($row['id']); // will be NULL if the object type is not registered
  191.                                 if ($obj && (!$obj->isConfidential())) {
  192.                                         $out[] = $row['id'];
  193.                                 }
  194.                         }
  195.                 } else {
  196.                         self::buildObjectInformationCache();
  197.  
  198.                         foreach (self::$object_info_cache as $id => $cacheitem) {
  199.                                 $confidential = $cacheitem[self::CACHE_CONFIDENTIAL];
  200.                                 if (!$confidential) {
  201.                                         $obj = self::parse($id); // will be NULL if the object type is not registered
  202.                                         if ($obj && (!$obj->isConfidential())) {
  203.                                                 $out[] = $id;
  204.                                         }
  205.                                 }
  206.                         }
  207.                 }
  208.  
  209.                 return $out;
  210.         }
  211.  
  212.         public function isConfidential() {
  213.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  214.                         //static $confidential_cache = array();
  215.                         $curid = $this->nodeId();
  216.                         //$orig_curid = $curid;
  217.                         //if (isset($confidential_cache[$curid])) return $confidential_cache[$curid];
  218.                         // Recursively search for the confidential flag in the parents
  219.                         while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->any()) {
  220.                                 $row = $res->fetch_array();
  221.                                 if ($row['confidential']) {
  222.                                         //$confidential_cache[$curid] = true;
  223.                                         //$confidential_cache[$orig_curid] = true;
  224.                                         return true;
  225.                                 } else {
  226.                                         //$confidential_cache[$curid] = false;
  227.                                 }
  228.                                 $curid = $row['parent'];
  229.                                 //if (isset($confidential_cache[$curid])) {
  230.                                         //$confidential_cache[$orig_curid] = $confidential_cache[$curid];
  231.                                         //return $confidential_cache[$curid];
  232.                                 //}
  233.                         }
  234.  
  235.                         //$confidential_cache[$orig_curid] = false;
  236.                         return false;
  237.                 } else {
  238.                         self::buildObjectInformationCache();
  239.  
  240.                         $curid = $this->nodeId();
  241.                         // Recursively search for the confidential flag in the parents
  242.                         while (isset(self::$object_info_cache[$curid])) {
  243.                                 if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
  244.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  245.                         }
  246.                         return false;
  247.                 }
  248.         }
  249.  
  250.         public function isChildOf(OIDplusObject $obj) {
  251.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  252.                         $curid = $this->nodeId();
  253.                         while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->any()) {
  254.                                 $row = $res->fetch_array();
  255.                                 if ($curid == $obj->nodeId()) return true;
  256.                                 $curid = $row['parent'];
  257.                         }
  258.                         return false;
  259.                 } else {
  260.                         self::buildObjectInformationCache();
  261.  
  262.                         $curid = $this->nodeId();
  263.                         while (isset(self::$object_info_cache[$curid])) {
  264.                                 if ($curid == $obj->nodeId()) return true;
  265.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  266.                         }
  267.                         return false;
  268.                 }
  269.         }
  270.  
  271.         public function getChildren() {
  272.                 $out = array();
  273.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  274.                         $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId()));
  275.                         while ($row = $res->fetch_array()) {
  276.                                 $obj = self::parse($row['id']);
  277.                                 if (!$obj) continue;
  278.                                 $out[] = $obj;
  279.                         }
  280.                 } else {
  281.                         self::buildObjectInformationCache();
  282.  
  283.                         foreach (self::$object_info_cache as $id => $cacheitem) {
  284.                                 $parent = $cacheitem[self::CACHE_PARENT];
  285.                                 if ($parent == $this->nodeId()) {
  286.                                         $obj = self::parse($id);
  287.                                         if (!$obj) continue;
  288.                                         $out[] = $obj;
  289.                                 }
  290.                         }
  291.                 }
  292.                 return $out;
  293.         }
  294.  
  295.         public function getRa() {
  296.                 return new OIDplusRA($this->getRaMail());
  297.         }
  298.  
  299.         public function userHasReadRights($ra_email=null) {
  300.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  301.  
  302.                 // If it is not confidential, everybody can read/see it.
  303.                 // Note: This also checks if superior OIDs are confidential.
  304.                 if (!$this->isConfidential()) return true;
  305.  
  306.                 if (!$ra_email) {
  307.                         // Admin may do everything
  308.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  309.  
  310.                         // If the RA is logged in, then they can see the OID.
  311.                         if (OIDplus::authUtils()->isRaLoggedIn($this->getRaMail())) return true;
  312.                 } else {
  313.                         // If this OID belongs to the requested RA, then they may see it.
  314.                         if ($this->getRaMail() == $ra_email) return true;
  315.                 }
  316.  
  317.                 // If someone has rights to an object below our confidential node,
  318.                 // we let him see the confidential node,
  319.                 // Otherwise he could not browse through to his own node.
  320.                 $roots = $this->getRaRoots($ra_email);
  321.                 foreach ($roots as $root) {
  322.                         if ($root->isChildOf($this)) return true;
  323.                 }
  324.  
  325.                 return false;
  326.         }
  327.  
  328.         public function getIcon($row=null) {
  329.                 $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
  330.  
  331.                 if (is_null($row)) {
  332.                         $ra_email = $this->getRaMail();
  333.                 } else {
  334.                         $ra_email = $row['ra_email'];
  335.                 }
  336.                 // TODO: have different icons for Leaf-Nodes
  337.  
  338.                 $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/objectTypes/'.$namespace.'/');
  339.  
  340.                 if (count($dirs) == 0) return null; // default icon (folder)
  341.  
  342.                 $dir = substr($dirs[0], strlen(OIDplus::localpath()));
  343.  
  344.                 // We use $this:: instead of self:: , because we want to call the overridden methods
  345.                 if (OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
  346.                         $icon = $dir.'/'.$this::treeIconFilename('own');
  347.                 } else {
  348.                         $icon = $dir.'/'.$this::treeIconFilename('general');
  349.                 }
  350.  
  351.                 if (!file_exists($icon)) return null; // default icon (folder)
  352.  
  353.                 return $icon;
  354.         }
  355.  
  356.         public static function exists(string $id) {
  357.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  358.                         $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
  359.                         return $res->any();
  360.                 } else {
  361.                         self::buildObjectInformationCache();
  362.                         return isset(self::$object_info_cache[$id]);
  363.                 }
  364.         }
  365.  
  366.         // Get parent gives the next possible parent which is EXISTING in OIDplus
  367.         // It does not give the immediate parent
  368.         public function getParent()/*: ?OIDplusObject*/ {
  369.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  370.                         $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
  371.                         if (!$res->any()) return null;
  372.                         $row = $res->fetch_array();
  373.                         $parent = $row['parent'];
  374.                         $obj = OIDplusObject::parse($parent);
  375.                         if ($obj) return $obj;
  376.                         // TODO: Also implement one_up() like below
  377.                 } else {
  378.                         self::buildObjectInformationCache();
  379.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  380.                                 $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
  381.                                 $obj = OIDplusObject::parse($parent);
  382.                                 if ($obj) return $obj;
  383.                         }
  384.  
  385.                         // 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()
  386.                         $cur = $this->one_up();
  387.                         if (!$cur) return null;
  388.                         do {
  389.                                 // findFitting() checks if that OID exists
  390.                                 if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
  391.  
  392.                                 $prev = $cur;
  393.                                 $cur = $cur->one_up();
  394.                                 if (!$cur) return null;
  395.                         } while ($prev != $cur);
  396.                 }
  397.                 return null;
  398.         }
  399.  
  400.         public function getRaMail() {
  401.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  402.                         $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
  403.                         if (!$res->any()) return null;
  404.                         $row = $res->fetch_array();
  405.                         return $row['ra_email'];
  406.                 } else {
  407.                         self::buildObjectInformationCache();
  408.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  409.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
  410.                         }
  411.                         return false;
  412.                 }
  413.         }
  414.  
  415.         public function getTitle() {
  416.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  417.                         $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
  418.                         if (!$res->any()) return null;
  419.                         $row = $res->fetch_array();
  420.                         return $row['title'];
  421.                 } else {
  422.                         self::buildObjectInformationCache();
  423.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  424.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
  425.                         }
  426.                         return false;
  427.                 }
  428.         }
  429.  
  430.         public function getDescription() {
  431.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  432.                         $res = OIDplus::db()->query("select description from ###objects where id = ?", array($this->nodeId()));
  433.                         if (!$res->any()) return null;
  434.                         $row = $res->fetch_array();
  435.                         return $row['description'];
  436.                 } else {
  437.                         self::buildObjectInformationCache();
  438.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  439.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_DESCRIPTION];
  440.                         }
  441.                         return false;
  442.                 }
  443.         }
  444.  
  445.         public function getComment() {
  446.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  447.                         $res = OIDplus::db()->query("select comment from ###objects where id = ?", array($this->nodeId()));
  448.                         if (!$res->any()) return null;
  449.                         $row = $res->fetch_array();
  450.                         return $row['comment'];
  451.                 } else {
  452.                         self::buildObjectInformationCache();
  453.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  454.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_COMMENT];
  455.                         }
  456.                         return false;
  457.                 }
  458.         }
  459.  
  460.         public function getCreatedTime() {
  461.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  462.                         $res = OIDplus::db()->query("select created from ###objects where id = ?", array($this->nodeId()));
  463.                         if (!$res->any()) return null;
  464.                         $row = $res->fetch_array();
  465.                         return $row['created'];
  466.                 } else {
  467.                         self::buildObjectInformationCache();
  468.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  469.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_CREATED];
  470.                         }
  471.                         return false;
  472.                 }
  473.         }
  474.  
  475.         public function getUpdatedTime() {
  476.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  477.                         $res = OIDplus::db()->query("select updated from ###objects where id = ?", array($this->nodeId()));
  478.                         if (!$res->any()) return null;
  479.                         $row = $res->fetch_array();
  480.                         return $row['updated'];
  481.                 } else {
  482.                         self::buildObjectInformationCache();
  483.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  484.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_UPDATED];
  485.                         }
  486.                         return false;
  487.                 }
  488.         }
  489.  
  490.         public function userHasParentalWriteRights($ra_email=null) {
  491.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  492.  
  493.                 if (!$ra_email) {
  494.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  495.                 }
  496.  
  497.                 $objParent = $this->getParent();
  498.                 if (!$objParent) return false;
  499.                 return $objParent->userHasWriteRights($ra_email);
  500.         }
  501.  
  502.         public function userHasWriteRights($ra_email=null) {
  503.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  504.  
  505.                 if (!$ra_email) {
  506.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  507.                         return OIDplus::authUtils()->isRaLoggedIn($this->getRaMail());
  508.                 } else {
  509.                         return $this->getRaMail() == $ra_email;
  510.                 }
  511.         }
  512.  
  513.         public function distance($to) {
  514.                 return null; // not implemented
  515.         }
  516.  
  517.         public function equals($obj) {
  518.                 if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
  519.                 if (!($obj instanceof $this)) return false;
  520.  
  521.                 $distance = $this->distance($obj);
  522.                 if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
  523.  
  524.                 return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
  525.         }
  526.  
  527.         public static function findFitting(string $id) {
  528.                 $obj = OIDplusObject::parse($id);
  529.                 if (!$obj) return false; // e.g. if ObjectType plugin is disabled
  530.  
  531.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  532.                         $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
  533.                         while ($row = $res->fetch_object()) {
  534.                                 $test = OIDplusObject::parse($row->id);
  535.                                 if ($obj->equals($test)) return $test;
  536.                         }
  537.                         return false;
  538.                 } else {
  539.                         self::buildObjectInformationCache();
  540.                         foreach (self::$object_info_cache as $id => $cacheitem) {
  541.                                 if (strpos($id, $obj->ns().':') === 0) {
  542.                                         $test = OIDplusObject::parse($id);
  543.                                         if ($obj->equals($test)) return $test;
  544.                                 }
  545.                         }
  546.                         return false;
  547.                 }
  548.         }
  549.  
  550.         public function one_up() {
  551.                 return null; // not implemented
  552.         }
  553.  
  554.         // Caching stuff
  555.  
  556.         protected static $object_info_cache = null;
  557.  
  558.         public static function resetObjectInformationCache() {
  559.                 self::$object_info_cache = null;
  560.         }
  561.  
  562.         const CACHE_ID = 'id';
  563.         const CACHE_PARENT = 'parent';
  564.         const CACHE_TITLE = 'title';
  565.         const CACHE_DESCRIPTION = 'description';
  566.         const CACHE_RA_EMAIL = 'ra_email';
  567.         const CACHE_CONFIDENTIAL = 'confidential';
  568.         const CACHE_CREATED = 'created';
  569.         const CACHE_UPDATED = 'updated';
  570.         const CACHE_COMMENT = 'comment';
  571.  
  572.         private static function buildObjectInformationCache() {
  573.                 if (is_null(self::$object_info_cache)) {
  574.                         self::$object_info_cache = array();
  575.                         $res = OIDplus::db()->query("select * from ###objects");
  576.                         while ($row = $res->fetch_array()) {
  577.                                 self::$object_info_cache[$row['id']] = $row;
  578.                         }
  579.                 }
  580.         }
  581.  
  582.         // override this function if you want your object type to save
  583.         // attachments in directories with easy names.
  584.         // Take care that your custom directory name will not allow jailbreaks (../) !
  585.         public function getDirectoryName() {
  586.                 if ($this->isRoot()) return $this->ns();
  587.                 return $this->getLegacyDirectoryName();
  588.         }
  589.  
  590.         public final function getLegacyDirectoryName() {
  591.                 if ($this::ns() == 'oid') {
  592.                         $oid = $this->nodeId(false);
  593.                 } else {
  594.                         $oid = null;
  595.                         $alt_ids = $this->getAltIds();
  596.                         foreach ($alt_ids as $alt_id) {
  597.                                 if ($alt_id->getNamespace() == 'oid') {
  598.                                         $oid = $alt_id->getId();
  599.                                         break; // we prefer the first OID (for GUIDs, the first OID is the OIDplus-OID, and the second OID is the UUID OID)
  600.                                 }
  601.                         }
  602.                 }
  603.  
  604.                 if (!is_null($oid) && ($oid != '')) {
  605.                         // For OIDs, it is the OID, for other identifiers
  606.                         // it it the OID alt ID (generated using the SystemID)
  607.                         return str_replace('.', '_', $oid);
  608.                 } else {
  609.                         // Can happen if you don't have a system ID (due to missing OpenSSL plugin)
  610.                         return md5($this->nodeId(true)); // we don't use $id, because $this->nodeId(true) is possibly more canonical than $id
  611.                 }
  612.         }
  613.  
  614.         public static function treeIconFilename($mode) {
  615.                 // for backwards-compatibility with older plugins
  616.                 return 'img/treeicon_'.$mode.'.png';
  617.         }
  618.  
  619. }
  620.