Subversion Repositories oidplus

Rev

Rev 251 | Rev 277 | 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 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('IN_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, '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()), 'Namebased version 3 / MD5 UUID with namespace UUID_NAMEBASED_NS_OidPlusMisc');
  48.                         $ids[] = new OIDplusAltId('guid', gen_uuid_sha1_namebased(self::UUID_NAMEBASED_NS_OidPlusMisc, $this->nodeId()), 'Namebased version 5 / SHA1 UUID with namespace 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 (ifnull(oParent.ra_email,'') <> ? and ifnull(oChild.ra_email,'') = ?) or ".
  99.                                                             "      (oParent.ra_email is null and ifnull(oChild.ra_email,'') = ?) ".
  100.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email));
  101.                                 while ($row = $res->fetch_array()) {
  102.                                         $x = self::parse($row['id']); // can be FALSE if namespace was disabled
  103.                                         if ($x) $out[] = self::parse($row['id']);
  104.                                 }
  105.                         }
  106.                 } else {
  107.                         if (is_null($ra_email)) {
  108.                                 $ra_mails_to_check = OIDplusAuthUtils::loggedInRaList();
  109.                                 if (count($ra_mails_to_check) == 0) return $out;
  110.                         } else {
  111.                                 $ra_mails_to_check = array($ra_email);
  112.                         }
  113.  
  114.                         self::buildObjectInformationCache();
  115.  
  116.                         foreach ($ra_mails_to_check as $check_ra_mail) {
  117.                                 $out_part = array();
  118.  
  119.                                 foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  120.                                         // If the OID RA is the RA we are searching, then add the object to the choice list
  121.                                         if ($ra_email == $check_ra_mail) $out_part[] = $id;
  122.                                 }
  123.  
  124.                                 foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  125.                                         if (isset(self::$object_info_cache[$parent])) {
  126.                                                 if (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] == $ra_email) {
  127.                                                         // if the parent has the same RA, then this OID cannot be a root => remove the element from the choice list
  128.                                                         foreach (array_keys($out_part, $id) as $key) unset($out_part[$key]);
  129.                                                 }
  130.                                         }
  131.                                 }
  132.  
  133.                                 natsort($out_part);
  134.  
  135.                                 foreach ($out_part as $id) {
  136.                                         $obj = self::parse($id);
  137.                                         if ($obj) $out[] = $obj;
  138.                                 }
  139.                         }
  140.                 }
  141.  
  142.                 return $out;
  143.         }
  144.  
  145.         public static function getAllNonConfidential() {
  146.                 $out = array();
  147.  
  148.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  149.                         $res = OIDplus::db()->query("select id from ###objects where confidential = '0' order by ".OIDplus::db()->natOrder('id'));
  150.  
  151.                         while ($row = $res->fetch_array()) {
  152.                                 $obj = self::parse($row['id']); // will be NULL if the object type is not registered
  153.                                 if ($obj && (!$obj->isConfidential())) {
  154.                                         $out[] = $row['id'];
  155.                                 }
  156.                         }
  157.                 } else {
  158.                         self::buildObjectInformationCache();
  159.  
  160.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  161.                                 if (!$confidential) {
  162.                                         $obj = self::parse($id); // will be NULL if the object type is not registered
  163.                                         if ($obj && (!$obj->isConfidential())) {
  164.                                                 $out[] = $id;
  165.                                         }
  166.                                 }
  167.                         }
  168.                 }
  169.  
  170.                 return $out;
  171.         }
  172.  
  173.         public function isConfidential() {
  174.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  175.                         $curid = $this->nodeId();
  176.                         $orig_curid = $curid;
  177.                         if (isset(self::$object_info_cache[$curid])) return self::$object_info_cache[$curid];
  178.                         // Recursively search for the confidential flag in the parents
  179.                         while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->num_rows() > 0) {
  180.                                 $row = $res->fetch_array();
  181.                                 if ($row['confidential']) {
  182.                                         self::$object_info_cache[$curid] = true;
  183.                                         self::$object_info_cache[$orig_curid] = true;
  184.                                         return true;
  185.                                 } else {
  186.                                         self::$object_info_cache[$curid] = false;
  187.                                 }
  188.                                 $curid = $row['parent'];
  189.                                 if (isset(self::$object_info_cache[$curid])) {
  190.                                         self::$object_info_cache[$orig_curid] = self::$object_info_cache[$curid];
  191.                                         return self::$object_info_cache[$curid];
  192.                                 }
  193.                         }
  194.  
  195.                         self::$object_info_cache[$orig_curid] = false;
  196.                         return false;
  197.                 } else {
  198.                         self::buildObjectInformationCache();
  199.  
  200.                         $curid = $this->nodeId();
  201.                         // Recursively search for the confidential flag in the parents
  202.                         while (isset(self::$object_info_cache[$curid])) {
  203.                                 if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
  204.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  205.                         }
  206.                         return false;
  207.                 }
  208.         }
  209.  
  210.         public function isChildOf(OIDplusObject $obj) {
  211.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  212.                         $curid = $this->nodeId();
  213.                         while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->num_rows() > 0) {
  214.                                 $row = $res->fetch_array();
  215.                                 if ($curid == $obj->nodeId()) return true;
  216.                                 $curid = $row['parent'];
  217.                         }
  218.                         return false;
  219.                 } else {
  220.                         self::buildObjectInformationCache();
  221.  
  222.                         $curid = $this->nodeId();
  223.                         while (isset(self::$object_info_cache[$curid])) {
  224.                                 if ($curid == $obj->nodeId()) return true;
  225.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  226.                         }
  227.                         return false;
  228.                 }
  229.         }
  230.  
  231.         public function getChildren() {
  232.                 $out = array();
  233.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  234.                         $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId()));
  235.                         while ($row = $res->fetch_array()) {
  236.                                 $obj = self::parse($row['id']);
  237.                                 if (!$obj) continue;
  238.                                 $out[] = $obj;
  239.                         }
  240.                 } else {
  241.                         self::buildObjectInformationCache();
  242.  
  243.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  244.                                 if ($parent == $this->nodeId()) {
  245.                                         $obj = self::parse($id);
  246.                                         if (!$obj) continue;
  247.                                         $out[] = $obj;
  248.                                 }
  249.                         }
  250.                 }
  251.                 return $out;
  252.         }
  253.  
  254.         public function getRa() {
  255.                 return new OIDplusRA($this->getRaMail());
  256.         }
  257.  
  258.         public function userHasReadRights($ra_email=null) {
  259.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  260.  
  261.                 // Admin may do everything
  262.                 if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
  263.  
  264.                 // If it is not confidential, everybody can read/see it.
  265.                 if (!$this->isConfidential()) return true;
  266.  
  267.                 // If we own the object, we may see it
  268.                 if (is_null($ra_email)) {
  269.                         if ($this->userHasWriteRights()) return true;
  270.                 } else {
  271.                         if ($this->getRaMail() == $ra_email) return true;
  272.                 }
  273.  
  274.                 // If someone has rights to an object below our confidential node,
  275.                 // we let him see the confidential node,
  276.                 // Otherwise he could not browse through to his own node.
  277.                 $roots = $this->getRaRoots($ra_email);
  278.                 foreach ($roots as $root) {
  279.                         if ($root->isChildOf($this)) return true;
  280.                 }
  281.  
  282.                 return false;
  283.         }
  284.  
  285.         public function getIcon($row=null) {
  286.                 $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
  287.  
  288.                 if (is_null($row)) {
  289.                         $ra_email = $this->getRaMail();
  290.                 } else {
  291.                         $ra_email = $row['ra_email'];
  292.                 }
  293.                 // TODO: have different icons for Leaf-Nodes
  294.                 if (OIDplus::authUtils()::isRaLoggedIn($ra_email)) {
  295.                         $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_own.png';
  296.                 } else {
  297.                         $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_general.png';
  298.                 }
  299.                 if (!file_exists($icon)) $icon = null; // default icon (folder)
  300.                 return $icon;
  301.         }
  302.  
  303.         public static function exists($id) {
  304.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  305.                         $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
  306.                         return $res->num_rows() > 0;
  307.                 } else {
  308.                         self::buildObjectInformationCache();
  309.                         return isset(self::$object_info_cache[$id]);
  310.                 }
  311.         }
  312.  
  313.         public function getParent() {
  314.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  315.                         $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
  316.                         if ($res->num_rows() == 0) return null;
  317.                         $row = $res->fetch_array();
  318.                         $parent = $row['parent'];
  319.                         $obj = OIDplusObject::parse($parent);
  320.                         if ($obj) return $obj;
  321.                 } else {
  322.                         self::buildObjectInformationCache();
  323.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  324.                                 $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
  325.                                 $obj = OIDplusObject::parse($parent);
  326.                                 if ($obj) return $obj;
  327.                         }
  328.  
  329.                         // 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()
  330.                         $cur = $this->one_up();
  331.                         if (!$cur) return false;
  332.                         do {
  333.                                 if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
  334.  
  335.                                 $prev = $cur;
  336.                                 $cur = $cur->one_up();
  337.                                 if (!$cur) return false;
  338.                         } while ($prev != $cur);
  339.  
  340.                         return false;
  341.                 }
  342.         }
  343.  
  344.         public function getRaMail() {
  345.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  346.                         $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
  347.                         if ($res->num_rows() == 0) return null;
  348.                         $row = $res->fetch_array();
  349.                         return $row['ra_email'];
  350.                 } else {
  351.                         self::buildObjectInformationCache();
  352.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  353.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
  354.                         }
  355.                         return false;
  356.                 }
  357.         }
  358.  
  359.         public function getTitle() {
  360.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  361.                         $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
  362.                         if ($res->num_rows() == 0) return null;
  363.                         $row = $res->fetch_array();
  364.                         return $row['title'];
  365.                 } else {
  366.                         self::buildObjectInformationCache();
  367.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  368.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
  369.                         }
  370.                         return false;
  371.                 }
  372.         }
  373.  
  374.         public function userHasParentalWriteRights($ra_email=null) {
  375.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  376.  
  377.                 if (is_null($ra_email)) {
  378.                         if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
  379.                 }
  380.  
  381.                 $objParent = $this->getParent();
  382.                 if (is_null($objParent)) return false;
  383.                 return $objParent->userHasWriteRights($ra_email);
  384.         }
  385.  
  386.         public function userHasWriteRights($ra_email=null) {
  387.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  388.  
  389.                 if (is_null($ra_email)) {
  390.                         if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
  391.                         return OIDplus::authUtils()::isRaLoggedIn($this->getRaMail());
  392.                 } else {
  393.                         return $this->getRaMail() == $ra_email;
  394.                 }
  395.         }
  396.  
  397.         public function distance($to) {
  398.                 return null; // not implemented
  399.         }
  400.  
  401.         public function equals($obj) {
  402.                 if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
  403.                 if (!($obj instanceof $this)) return false;
  404.  
  405.                 $distance = $this->distance($obj);
  406.                 if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
  407.  
  408.                 return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
  409.         }
  410.  
  411.         public static function findFitting($id) {
  412.                 $obj = OIDplusObject::parse($id);
  413.                 if (!$obj) throw new OIDplusException("findFitting: Parse failed\n");
  414.  
  415.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  416.                         $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
  417.                         while ($row = $res->fetch_object()) {
  418.                                 $test = OIDplusObject::parse($row->id);
  419.                                 if ($obj->equals($test)) return $test;
  420.                         }
  421.                         return false;
  422.                 } else {
  423.                         self::buildObjectInformationCache();
  424.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  425.                                 if (strpos($id, $obj->ns().':') === 0) {
  426.                                         $test = OIDplusObject::parse($id);
  427.                                         if ($obj->equals($test)) return $test;
  428.                                 }
  429.                         }
  430.                         return false;
  431.                 }
  432.         }
  433.  
  434.         public function one_up() {
  435.                 return null; // not implemented
  436.         }
  437.  
  438.         // Caching stuff
  439.  
  440.         protected static $object_info_cache = null;
  441.  
  442.         public static function resetObjectInformationCache() {
  443.                 self::$object_info_cache = null;
  444.         }
  445.  
  446.         const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc.
  447.         const CACHE_PARENT = 1;
  448.         const CACHE_RA_EMAIL = 2;
  449.         const CACHE_TITLE = 3;
  450.  
  451.         private static function buildObjectInformationCache() {
  452.                 if (is_null(self::$object_info_cache)) {
  453.                         self::$object_info_cache = array();
  454.                         $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects");
  455.                         while ($row = $res->fetch_array()) {
  456.                                 self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
  457.                         }
  458.                 }
  459.         }
  460. }
  461.