Subversion Repositories oidplus

Rev

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