Subversion Repositories oidplus

Rev

Rev 1127 | Rev 1137 | 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 - 2023 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. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. abstract class OIDplusObject extends OIDplusBaseClass {
  27.  
  28.         /**
  29.          *
  30.          */
  31.         const UUID_NAMEBASED_NS_OidPlusMisc = 'ad1654e6-7e15-11e4-9ef6-78e3b5fc7f22';
  32.  
  33.         /**
  34.          * Please overwrite this function!
  35.          * @param string $node_id
  36.          * @return OIDplusObject|null
  37.          */
  38.         public static function parse(string $node_id)/*: ?OIDplusObject*/ {
  39.                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  40.                         try {
  41.                                 $good = false;
  42.                                 if (get_parent_class($ot) == OIDplusObject::class) {
  43.                                         $reflector = new \ReflectionMethod($ot, 'parse');
  44.                                         $isImplemented = ($reflector->getDeclaringClass()->getName() === $ot);
  45.                                         if ($isImplemented) { // avoid endless loop if parse is not overriden
  46.                                                 $good = true;
  47.                                         }
  48.                                 }
  49.                                 // We need to do the workaround with "$good", otherwise PHPstan shows
  50.                                 // "Call to an undefined static method object::parse()"
  51.                                 if ($good && $obj = $ot::parse($node_id)) return $obj;
  52.                         } catch (\Exception $e) {}
  53.                 }
  54.                 return null;
  55.         }
  56.  
  57.         /**
  58.          * @return OIDplusAltId[]
  59.          * @throws OIDplusException
  60.          */
  61.         public function getAltIds(): array {
  62.                 if ($this->isRoot()) return array();
  63.  
  64.                 $ids = array();
  65.  
  66.                 // Creates an OIDplus-Hash-OID
  67.                 if ($this->ns() != 'oid') {
  68.                         $sid = OIDplus::getSystemId(true);
  69.                         if (!empty($sid)) {
  70.                                 $ns_oid = $this->getPlugin()->getManifest()->getOid();
  71.                                 if (str_starts_with($ns_oid, '1.3.6.1.4.1.37476.2.5.2.')) {
  72.                                         // Official ViaThinkSoft object type plugins
  73.                                         // For backwards compatibility with existing IDs,
  74.                                         // set the hash_payload as '<namespace>:<id>'
  75.                                         $hash_payload = $this->nodeId(true);
  76.                                 } else {
  77.                                         // Third-party object type plugins
  78.                                         // Set the hash_payload as '<plugin oid>:<id>'
  79.                                         $hash_payload = $ns_oid.':'.$this->nodeId(false);
  80.                                 }
  81.                                 $oid = $sid . '.' . smallhash($hash_payload);
  82.                                 $ids[] = new OIDplusAltId('oid', $oid, _L('OIDplus Information Object OID'));
  83.                         }
  84.                 }
  85.  
  86.                 // Make a namebased UUID, but...
  87.                 // ... exclude GUID, because a GUID is already a GUID
  88.                 // ... exclude OID, because an OID already has a record UUID_NAMEBASED_NS_OID (defined by IETF) set by class OIDplusOid
  89.                 if (($this->ns() != 'guid') && ($this->ns() != 'oid')) {
  90.                         $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'));
  91.                         $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'));
  92.                 }
  93.  
  94.                 // Make a AID based on ViaThinkSoft schema
  95.                 // ... 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)
  96.                 if (($this->ns() != 'aid') && !str_starts_with($this->nodeId(true), 'oid:1.3.6.1.4.1.37476.30.9.')) {
  97.                         $sid = OIDplus::getSystemId(false);
  98.                         if ($sid !== false) {
  99.                                 $ns_oid = $this->getPlugin()->getManifest()->getOid();
  100.                                 if (str_starts_with($ns_oid, '1.3.6.1.4.1.37476.2.5.2.')) {
  101.                                         // Official ViaThinkSoft object type plugins
  102.                                         // For backwards compatibility with existing IDs,
  103.                                         // set the hash_payload as '<namespace>:<id>'
  104.                                         $hash_payload = $this->nodeId(true);
  105.                                 } else {
  106.                                         // Third-party object type plugins
  107.                                         // Set the hash_payload as '<plugin oid>:<id>'
  108.                                         $hash_payload = $ns_oid.':'.$this->nodeId(false);
  109.                                 }
  110.  
  111.                                 $sid_hex = strtoupper(str_pad(dechex((int)$sid),8,'0',STR_PAD_LEFT));
  112.                                 $obj_hex = strtoupper(str_pad(dechex(smallhash($hash_payload)),8,'0',STR_PAD_LEFT));
  113.                                 $aid = 'D276000186B20005'.$sid_hex.$obj_hex;
  114.                                 $ids[] = new OIDplusAltId('aid', $aid, _L('OIDplus Information Object Application Identifier (ISO/IEC 7816)'), ' ('._L('No PIX allowed').')');
  115.                         }
  116.                 }
  117.  
  118.                 return $ids;
  119.         }
  120.  
  121.         /**
  122.          * @return string
  123.          */
  124.         public abstract static function objectTypeTitle(): string;
  125.  
  126.         /**
  127.          * @return string
  128.          */
  129.         public abstract static function objectTypeTitleShort(): string;
  130.  
  131.         /**
  132.          * @return OIDplusObjectTypePlugin|null
  133.          */
  134.         public function getPlugin()/*: ?OIDplusObjectTypePlugin */ {
  135.                 $plugins = OIDplus::getObjectTypePlugins();
  136.                 foreach ($plugins as $plugin) {
  137.                         if (get_class($this) == $plugin::getObjectTypeClassName()) {
  138.                                 return $plugin;
  139.                         }
  140.                 }
  141.                 return null;
  142.         }
  143.  
  144.         /**
  145.          * @return string
  146.          */
  147.         public abstract static function ns(): string;
  148.  
  149.         /**
  150.          * @return string
  151.          */
  152.         public abstract static function root(): string;
  153.  
  154.         /**
  155.          * @return bool
  156.          */
  157.         public abstract function isRoot(): bool;
  158.  
  159.         /**
  160.          * @param bool $with_ns
  161.          * @return string
  162.          */
  163.         public abstract function nodeId(bool $with_ns=true): string;
  164.  
  165.         /**
  166.          * @param string $str
  167.          * @return string mixed
  168.          * @throws OIDplusException
  169.          */
  170.         public abstract function addString(string $str): string;
  171.  
  172.         /**
  173.          * @param OIDplusObject $parent
  174.          * @return string
  175.          */
  176.         public abstract function crudShowId(OIDplusObject $parent): string;
  177.  
  178.         /**
  179.          * @return string
  180.          */
  181.         public function crudInsertPrefix(): string {
  182.                 return '';
  183.         }
  184.  
  185.         /**
  186.          * @return string
  187.          */
  188.         public function crudInsertSuffix(): string {
  189.                 return '';
  190.         }
  191.  
  192.         /**
  193.          * @param OIDplusObject|null $parent
  194.          * @return string
  195.          */
  196.         public abstract function jsTreeNodeName(OIDplusObject $parent = null): string;
  197.  
  198.         /**
  199.          * @return string
  200.          */
  201.         public abstract function defaultTitle(): string;
  202.  
  203.         /**
  204.          * @return bool
  205.          */
  206.         public abstract function isLeafNode(): bool;
  207.  
  208.         /**
  209.          * @param string $title
  210.          * @param string $content
  211.          * @param string $icon
  212.          * @return void
  213.          */
  214.         public abstract function getContentPage(string &$title, string &$content, string &$icon);
  215.  
  216.         /**
  217.          * @param OIDplusRA|string|null $ra
  218.          * @return array
  219.          * @throws OIDplusConfigInitializationException
  220.          * @throws OIDplusException
  221.          */
  222.         public static function getRaRoots($ra=null) : array{
  223.                 if ($ra instanceof OIDplusRA) $ra = $ra->raEmail();
  224.  
  225.                 $out = array();
  226.  
  227.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  228.                         if (!$ra) {
  229.                                 $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 ".
  230.                                                             "left join ###objects as oParent on oChild.parent = oParent.id ".
  231.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'));
  232.                                 while ($row = $res->fetch_array()) {
  233.                                         if (!OIDplus::authUtils()->isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()->isRaLoggedIn($row['child_mail'])) {
  234.                                                 $x = self::parse($row['id']); // can be NULL if namespace was disabled
  235.                                                 if ($x) $out[] = $x;
  236.                                         }
  237.                                 }
  238.                         } else {
  239.                                 $res = OIDplus::db()->query("select oChild.id as id from ###objects as oChild ".
  240.                                                             "left join ###objects as oParent on oChild.parent = oParent.id ".
  241.                                                             "where (".OIDplus::db()->getSlang()->isNullFunction('oParent.ra_email',"''")." <> ? and ".
  242.                                                             OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) or ".
  243.                                                             "      (oParent.ra_email is null and ".OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) ".
  244.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra, $ra, $ra));
  245.                                 while ($row = $res->fetch_array()) {
  246.                                         $x = self::parse($row['id']); // can be NULL if namespace was disabled
  247.                                         if ($x) $out[] = $x;
  248.                                 }
  249.                         }
  250.                 } else {
  251.                         if (!$ra) {
  252.                                 $ra_mails_to_check = OIDplus::authUtils()->loggedInRaList();
  253.                                 if (count($ra_mails_to_check) == 0) return $out;
  254.                         } else {
  255.                                 $ra_mails_to_check = array($ra);
  256.                         }
  257.  
  258.                         self::buildObjectInformationCache();
  259.  
  260.                         foreach ($ra_mails_to_check as $check_ra_mail) {
  261.                                 $out_part = array();
  262.  
  263.                                 foreach (self::$object_info_cache as $id => $cacheitem) {
  264.                                         if ($cacheitem[self::CACHE_RA_EMAIL] == $check_ra_mail) {
  265.                                                 $parent = $cacheitem[self::CACHE_PARENT];
  266.                                                 if (!isset(self::$object_info_cache[$parent]) || (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] != $check_ra_mail)) {
  267.                                                         $out_part[] = $id;
  268.                                                 }
  269.                                         }
  270.                                 }
  271.  
  272.                                 natsort($out_part);
  273.  
  274.                                 foreach ($out_part as $id) {
  275.                                         $obj = self::parse($id);
  276.                                         if ($obj) $out[] = $obj;
  277.                                 }
  278.                         }
  279.                 }
  280.  
  281.                 return $out;
  282.         }
  283.  
  284.         /**
  285.          * @return array
  286.          * @throws OIDplusException
  287.          */
  288.         public static function getAllNonConfidential(): array {
  289.                 $out = array();
  290.  
  291.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  292.                         $res = OIDplus::db()->query("select id from ###objects where confidential = ? order by ".OIDplus::db()->natOrder('id'), array(false));
  293.  
  294.                         while ($row = $res->fetch_array()) {
  295.                                 $obj = self::parse($row['id']); // will be NULL if the object type is not registered
  296.                                 if ($obj && (!$obj->isConfidential())) {
  297.                                         $out[] = $row['id'];
  298.                                 }
  299.                         }
  300.                 } else {
  301.                         self::buildObjectInformationCache();
  302.  
  303.                         foreach (self::$object_info_cache as $id => $cacheitem) {
  304.                                 $confidential = $cacheitem[self::CACHE_CONFIDENTIAL];
  305.                                 if (!$confidential) {
  306.                                         $obj = self::parse($id); // will be NULL if the object type is not registered
  307.                                         if ($obj && (!$obj->isConfidential())) {
  308.                                                 $out[] = $id;
  309.                                         }
  310.                                 }
  311.                         }
  312.                 }
  313.  
  314.                 return $out;
  315.         }
  316.  
  317.         /**
  318.          * @return bool
  319.          * @throws OIDplusException
  320.          */
  321.         public function isConfidential(): bool {
  322.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  323.                         //static $confidential_cache = array();
  324.                         $curid = $this->nodeId();
  325.                         //$orig_curid = $curid;
  326.                         //if (isset($confidential_cache[$curid])) return $confidential_cache[$curid];
  327.                         // Recursively search for the confidential flag in the parents
  328.                         while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->any()) {
  329.                                 $row = $res->fetch_array();
  330.                                 if ($row['confidential']) {
  331.                                         //$confidential_cache[$curid] = true;
  332.                                         //$confidential_cache[$orig_curid] = true;
  333.                                         return true;
  334.                                 } else {
  335.                                         //$confidential_cache[$curid] = false;
  336.                                 }
  337.                                 $curid = $row['parent'];
  338.                                 //if (isset($confidential_cache[$curid])) {
  339.                                         //$confidential_cache[$orig_curid] = $confidential_cache[$curid];
  340.                                         //return $confidential_cache[$curid];
  341.                                 //}
  342.                         }
  343.  
  344.                         //$confidential_cache[$orig_curid] = false;
  345.                         return false;
  346.                 } else {
  347.                         self::buildObjectInformationCache();
  348.  
  349.                         $curid = $this->nodeId();
  350.                         // Recursively search for the confidential flag in the parents
  351.                         while (isset(self::$object_info_cache[$curid])) {
  352.                                 if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
  353.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  354.                         }
  355.                         return false;
  356.                 }
  357.         }
  358.  
  359.         /**
  360.          * @param OIDplusObject $obj
  361.          * @return bool
  362.          * @throws OIDplusException
  363.          */
  364.         public function isChildOf(OIDplusObject $obj): bool {
  365.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  366.                         $curid = $this->nodeId();
  367.                         while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->any()) {
  368.                                 $row = $res->fetch_array();
  369.                                 if ($curid == $obj->nodeId()) return true;
  370.                                 $curid = $row['parent'];
  371.                         }
  372.                         return false;
  373.                 } else {
  374.                         self::buildObjectInformationCache();
  375.  
  376.                         $curid = $this->nodeId();
  377.                         while (isset(self::$object_info_cache[$curid])) {
  378.                                 if ($curid == $obj->nodeId()) return true;
  379.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  380.                         }
  381.                         return false;
  382.                 }
  383.         }
  384.  
  385.         /**
  386.          * @return array
  387.          * @throws OIDplusException
  388.          */
  389.         public function getChildren(): array {
  390.                 $out = array();
  391.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  392.                         $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId()));
  393.                         while ($row = $res->fetch_array()) {
  394.                                 $obj = self::parse($row['id']);
  395.                                 if (!$obj) continue;
  396.                                 $out[] = $obj;
  397.                         }
  398.                 } else {
  399.                         self::buildObjectInformationCache();
  400.  
  401.                         foreach (self::$object_info_cache as $id => $cacheitem) {
  402.                                 $parent = $cacheitem[self::CACHE_PARENT];
  403.                                 if ($parent == $this->nodeId()) {
  404.                                         $obj = self::parse($id);
  405.                                         if (!$obj) continue;
  406.                                         $out[] = $obj;
  407.                                 }
  408.                         }
  409.                 }
  410.                 return $out;
  411.         }
  412.  
  413.         /**
  414.          * @return OIDplusRA
  415.          * @throws OIDplusException
  416.          */
  417.         public function getRa(): OIDplusRA {
  418.                 return new OIDplusRA($this->getRaMail());
  419.         }
  420.  
  421.         /**
  422.          * @param OIDplusRA|string|null $ra
  423.          * @return bool
  424.          * @throws OIDplusConfigInitializationException
  425.          * @throws OIDplusException
  426.          */
  427.         public function userHasReadRights($ra=null): bool {
  428.                 if ($ra instanceof OIDplusRA) $ra = $ra->raEmail();
  429.  
  430.                 // If it is not confidential, everybody can read/see it.
  431.                 // Note: This also checks if superior OIDs are confidential.
  432.                 if (!$this->isConfidential()) return true;
  433.  
  434.                 if (!$ra) {
  435.                         // Admin may do everything
  436.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  437.  
  438.                         // If the RA is logged in, then they can see the OID.
  439.                         if (OIDplus::authUtils()->isRaLoggedIn($this->getRaMail())) return true;
  440.                 } else {
  441.                         // If this OID belongs to the requested RA, then they may see it.
  442.                         if ($this->getRaMail() == $ra) return true;
  443.                 }
  444.  
  445.                 // If someone has rights to an object below our confidential node,
  446.                 // we let him see the confidential node,
  447.                 // Otherwise he could not browse through to his own node.
  448.                 $roots = $this->getRaRoots($ra);
  449.                 foreach ($roots as $root) {
  450.                         if ($root->isChildOf($this)) return true;
  451.                 }
  452.  
  453.                 return false;
  454.         }
  455.  
  456.         /**
  457.          * @param array|null $row
  458.          * @return string|null
  459.          * @throws OIDplusException
  460.          */
  461.         public function getIcon(array $row=null) {
  462.                 $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
  463.  
  464.                 if (is_null($row)) {
  465.                         $ra_email = $this->getRaMail();
  466.                 } else {
  467.                         $ra_email = $row['ra_email'];
  468.                 }
  469.  
  470.                 // $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/objectTypes/'.$namespace.'/');
  471.                 // if (count($dirs) == 0) return null; // default icon (folder)
  472.                 // $dir = substr($dirs[0], strlen(OIDplus::localpath()));
  473.                 $reflection = new \ReflectionClass($this);
  474.                 $dir = dirname($reflection->getFilename());
  475.                 $dir = substr($dir, strlen(OIDplus::localpath()));
  476.                 $dir = str_replace('\\', '/', $dir);
  477.  
  478.                 if ($this->isRoot()) {
  479.                         $icon = $dir . '/' . $this::treeIconFilename('root');
  480.                 } else {
  481.                         // We use $this:: instead of self:: , because we want to call the overridden methods
  482.                         if ($ra_email && OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
  483.                                 if ($this->isLeafNode()) {
  484.                                         $icon = $dir . '/' . $this::treeIconFilename('own_leaf');
  485.                                         if (!file_exists($icon)) $icon = $dir . '/' . $this::treeIconFilename('own');
  486.                                 } else {
  487.                                         $icon = $dir . '/' . $this::treeIconFilename('own');
  488.                                 }
  489.                         } else {
  490.                                 if ($this->isLeafNode()) {
  491.                                         $icon = $dir . '/' . $this::treeIconFilename('general_leaf');
  492.                                         if (!file_exists($icon)) $icon = $dir . '/' . $this::treeIconFilename('general');
  493.                                 } else {
  494.                                         $icon = $dir . '/' . $this::treeIconFilename('general');
  495.                                 }
  496.                         }
  497.                 }
  498.  
  499.                 if (!file_exists($icon)) return null; // default icon (folder)
  500.  
  501.                 return $icon;
  502.         }
  503.  
  504.         /**
  505.          * @param string $id
  506.          * @return bool
  507.          * @throws OIDplusException
  508.          */
  509.         public static function exists(string $id): bool {
  510.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  511.                         $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
  512.                         return $res->any();
  513.                 } else {
  514.                         self::buildObjectInformationCache();
  515.                         return isset(self::$object_info_cache[$id]);
  516.                 }
  517.         }
  518.  
  519.         /**
  520.          * Get parent gives the next possible parent which is EXISTING in OIDplus
  521.          * It does not give the immediate parent
  522.          * @return OIDplusObject|null
  523.          * @throws OIDplusException
  524.          */
  525.         public function getParent()/*: ?OIDplusObject*/ {
  526.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  527.                         $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
  528.                         if (!$res->any()) return null;
  529.                         $row = $res->fetch_array();
  530.                         $parent = $row['parent'];
  531.                         $obj = OIDplusObject::parse($parent);
  532.                         if ($obj) return $obj;
  533.                         // TODO: Also implement one_up() like below
  534.                 } else {
  535.                         self::buildObjectInformationCache();
  536.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  537.                                 $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
  538.                                 $obj = OIDplusObject::parse($parent);
  539.                                 if ($obj) return $obj;
  540.                         }
  541.  
  542.                         // 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()
  543.                         $cur = $this->one_up();
  544.                         if (!$cur) return null;
  545.                         do {
  546.                                 // findFitting() checks if that OID exists
  547.                                 if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
  548.  
  549.                                 $prev = $cur;
  550.                                 $cur = $cur->one_up();
  551.                                 if (!$cur) return null;
  552.                         } while ($prev !== $cur);
  553.                 }
  554.                 return null;
  555.         }
  556.  
  557.         /**
  558.          * @return false|string|null
  559.          * @throws OIDplusException
  560.          */
  561.         public function getRaMail() {
  562.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  563.                         $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
  564.                         if (!$res->any()) return null;
  565.                         $row = $res->fetch_array();
  566.                         return $row['ra_email'];
  567.                 } else {
  568.                         self::buildObjectInformationCache();
  569.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  570.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
  571.                         }
  572.                         return false;
  573.                 }
  574.         }
  575.  
  576.         /**
  577.          * @return false|string|null
  578.          * @throws OIDplusException
  579.          */
  580.         public function getTitle() {
  581.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  582.                         $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
  583.                         if (!$res->any()) return null;
  584.                         $row = $res->fetch_array();
  585.                         return $row['title'];
  586.                 } else {
  587.                         self::buildObjectInformationCache();
  588.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  589.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
  590.                         }
  591.                         return false;
  592.                 }
  593.         }
  594.  
  595.         /**
  596.          * @return false|string|null
  597.          * @throws OIDplusException
  598.          */
  599.         public function getDescription() {
  600.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  601.                         $res = OIDplus::db()->query("select description from ###objects where id = ?", array($this->nodeId()));
  602.                         if (!$res->any()) return null;
  603.                         $row = $res->fetch_array();
  604.                         return $row['description'];
  605.                 } else {
  606.                         self::buildObjectInformationCache();
  607.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  608.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_DESCRIPTION];
  609.                         }
  610.                         return false;
  611.                 }
  612.         }
  613.  
  614.         /**
  615.          * @return false|string|null
  616.          * @throws OIDplusException
  617.          */
  618.         public function getComment() {
  619.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  620.                         $res = OIDplus::db()->query("select comment from ###objects where id = ?", array($this->nodeId()));
  621.                         if (!$res->any()) return null;
  622.                         $row = $res->fetch_array();
  623.                         return $row['comment'];
  624.                 } else {
  625.                         self::buildObjectInformationCache();
  626.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  627.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_COMMENT];
  628.                         }
  629.                         return false;
  630.                 }
  631.         }
  632.  
  633.         /**
  634.          * @return false|string|null
  635.          * @throws OIDplusException
  636.          */
  637.         public function getCreatedTime() {
  638.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  639.                         $res = OIDplus::db()->query("select created from ###objects where id = ?", array($this->nodeId()));
  640.                         if (!$res->any()) return null;
  641.                         $row = $res->fetch_array();
  642.                         return $row['created'];
  643.                 } else {
  644.                         self::buildObjectInformationCache();
  645.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  646.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_CREATED];
  647.                         }
  648.                         return false;
  649.                 }
  650.         }
  651.  
  652.         /**
  653.          * @return false|string|null
  654.          * @throws OIDplusException
  655.          */
  656.         public function getUpdatedTime() {
  657.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  658.                         $res = OIDplus::db()->query("select updated from ###objects where id = ?", array($this->nodeId()));
  659.                         if (!$res->any()) return null;
  660.                         $row = $res->fetch_array();
  661.                         return $row['updated'];
  662.                 } else {
  663.                         self::buildObjectInformationCache();
  664.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  665.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_UPDATED];
  666.                         }
  667.                         return false;
  668.                 }
  669.         }
  670.  
  671.         /**
  672.          * @param OIDplusRA|string|null $ra
  673.          * @return bool
  674.          * @throws OIDplusException
  675.          */
  676.         public function userHasParentalWriteRights($ra=null): bool {
  677.                 if ($ra instanceof OIDplusRA) $ra = $ra->raEmail();
  678.  
  679.                 if (!$ra) {
  680.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  681.                 }
  682.  
  683.                 $objParent = $this->getParent();
  684.                 if (!$objParent) return false;
  685.                 return $objParent->userHasWriteRights($ra);
  686.         }
  687.  
  688.         /**
  689.          * @param OIDplusRA|string|null $ra
  690.          * @return bool
  691.          * @throws OIDplusException
  692.          */
  693.         public function userHasWriteRights($ra=null): bool {
  694.                 if ($ra instanceof OIDplusRA) $ra = $ra->raEmail();
  695.  
  696.                 if (!$ra) {
  697.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  698.                         return OIDplus::authUtils()->isRaLoggedIn($this->getRaMail());
  699.                 } else {
  700.                         return $this->getRaMail() == $ra;
  701.                 }
  702.         }
  703.  
  704.         /**
  705.          * @param string|OIDplusObject $to
  706.          * @return int|null
  707.          */
  708.         public function distance($to)/*: ?int*/ {
  709.                 return null; // not implemented
  710.         }
  711.  
  712.         /**
  713.          * @param OIDplusObject|string $obj
  714.          * @return bool
  715.          */
  716.         public function equals($obj): bool {
  717.                 if (!$obj) return false;
  718.                 if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
  719.                 if (!$obj) return false;
  720.                 if (!($obj instanceof $this)) return false;
  721.  
  722.                 $distance = $this->distance($obj);
  723.                 if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
  724.  
  725.                 return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
  726.         }
  727.  
  728.         /**
  729.          * @param string $id
  730.          * @return OIDplusObject|false
  731.          * @throws OIDplusException
  732.          */
  733.         public static function findFitting(string $id) {
  734.                 $obj = OIDplusObject::parse($id);
  735.                 if (!$obj) return false; // e.g. if ObjectType plugin is disabled
  736.  
  737.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  738.                         $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
  739.                         while ($row = $res->fetch_object()) {
  740.                                 $test = OIDplusObject::parse($row->id);
  741.                                 if ($obj->equals($test)) return $test;
  742.                         }
  743.                         return false;
  744.                 } else {
  745.                         self::buildObjectInformationCache();
  746.                         foreach (self::$object_info_cache as $id => $cacheitem) {
  747.                                 if (strpos($id, $obj->ns().':') === 0) {
  748.                                         $test = OIDplusObject::parse($id);
  749.                                         if ($obj->equals($test)) return $test;
  750.                                 }
  751.                         }
  752.                         return false;
  753.                 }
  754.         }
  755.  
  756.         /**
  757.          * @return OIDplusObject|null
  758.          */
  759.         public function one_up()/*: ?OIDplusObject*/ {
  760.                 return null; // not implemented
  761.         }
  762.  
  763.         // Caching stuff
  764.  
  765.         protected static $object_info_cache = null;
  766.  
  767.         /**
  768.          * @return void
  769.          */
  770.         public static function resetObjectInformationCache() {
  771.                 self::$object_info_cache = null;
  772.         }
  773.  
  774.         const CACHE_ID = 'id';
  775.         const CACHE_PARENT = 'parent';
  776.         const CACHE_TITLE = 'title';
  777.         const CACHE_DESCRIPTION = 'description';
  778.         const CACHE_RA_EMAIL = 'ra_email';
  779.         const CACHE_CONFIDENTIAL = 'confidential';
  780.         const CACHE_CREATED = 'created';
  781.         const CACHE_UPDATED = 'updated';
  782.         const CACHE_COMMENT = 'comment';
  783.  
  784.         /**
  785.          * @return void
  786.          * @throws OIDplusException
  787.          */
  788.         private static function buildObjectInformationCache() {
  789.                 if (is_null(self::$object_info_cache)) {
  790.                         self::$object_info_cache = array();
  791.                         $res = OIDplus::db()->query("select * from ###objects");
  792.                         while ($row = $res->fetch_array()) {
  793.                                 self::$object_info_cache[$row['id']] = $row;
  794.                         }
  795.                 }
  796.         }
  797.  
  798.         /**
  799.          * override this function if you want your object type to save
  800.          * attachments in directories with easy names.
  801.          * Take care that your custom directory name will not allow jailbreaks (../) !
  802.          * @return string
  803.          * @throws OIDplusException
  804.          */
  805.         public function getDirectoryName(): string {
  806.                 if ($this->isRoot()) return $this->ns();
  807.                 return $this->getLegacyDirectoryName();
  808.         }
  809.  
  810.         /**
  811.          * @return string
  812.          * @throws OIDplusException
  813.          */
  814.         public final function getLegacyDirectoryName(): string {
  815.                 if ($this::ns() == 'oid') {
  816.                         $oid = $this->nodeId(false);
  817.                 } else {
  818.                         $oid = null;
  819.                         $alt_ids = $this->getAltIds();
  820.                         foreach ($alt_ids as $alt_id) {
  821.                                 if ($alt_id->getNamespace() == 'oid') {
  822.                                         $oid = $alt_id->getId();
  823.                                         break; // we prefer the first OID (for GUIDs, the first OID is the OIDplus-OID, and the second OID is the UUID OID)
  824.                                 }
  825.                         }
  826.                 }
  827.  
  828.                 if (!is_null($oid) && ($oid != '')) {
  829.                         // For OIDs, it is the OID, for other identifiers
  830.                         // it it the OID alt ID (generated using the SystemID)
  831.                         return str_replace('.', '_', $oid);
  832.                 } else {
  833.                         // Can happen if you don't have a system ID (due to missing OpenSSL plugin)
  834.                         return md5($this->nodeId(true)); // we don't use $id, because $this->nodeId(true) is possibly more canonical than $id
  835.                 }
  836.         }
  837.  
  838.         /**
  839.          * @param string $mode
  840.          * @return string
  841.          */
  842.         public static function treeIconFilename(string $mode): string {
  843.                 // for backwards-compatibility with older plugins
  844.                 return 'img/treeicon_'.$mode.'.png';
  845.         }
  846.  
  847. }
  848.