Subversion Repositories oidplus

Rev

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