Subversion Repositories oidplus

Rev

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