Subversion Repositories oidplus

Rev

Rev 277 | Rev 415 | 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 = OIDplusAuthUtils::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.                         $curid = $this->nodeId();
  174.                         $orig_curid = $curid;
  175.                         if (isset(self::$object_info_cache[$curid])) return self::$object_info_cache[$curid];
  176.                         // Recursively search for the confidential flag in the parents
  177.                         while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->num_rows() > 0) {
  178.                                 $row = $res->fetch_array();
  179.                                 if ($row['confidential']) {
  180.                                         self::$object_info_cache[$curid] = true;
  181.                                         self::$object_info_cache[$orig_curid] = true;
  182.                                         return true;
  183.                                 } else {
  184.                                         self::$object_info_cache[$curid] = false;
  185.                                 }
  186.                                 $curid = $row['parent'];
  187.                                 if (isset(self::$object_info_cache[$curid])) {
  188.                                         self::$object_info_cache[$orig_curid] = self::$object_info_cache[$curid];
  189.                                         return self::$object_info_cache[$curid];
  190.                                 }
  191.                         }
  192.  
  193.                         self::$object_info_cache[$orig_curid] = false;
  194.                         return false;
  195.                 } else {
  196.                         self::buildObjectInformationCache();
  197.  
  198.                         $curid = $this->nodeId();
  199.                         // Recursively search for the confidential flag in the parents
  200.                         while (isset(self::$object_info_cache[$curid])) {
  201.                                 if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
  202.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  203.                         }
  204.                         return false;
  205.                 }
  206.         }
  207.  
  208.         public function isChildOf(OIDplusObject $obj) {
  209.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  210.                         $curid = $this->nodeId();
  211.                         while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->num_rows() > 0) {
  212.                                 $row = $res->fetch_array();
  213.                                 if ($curid == $obj->nodeId()) return true;
  214.                                 $curid = $row['parent'];
  215.                         }
  216.                         return false;
  217.                 } else {
  218.                         self::buildObjectInformationCache();
  219.  
  220.                         $curid = $this->nodeId();
  221.                         while (isset(self::$object_info_cache[$curid])) {
  222.                                 if ($curid == $obj->nodeId()) return true;
  223.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  224.                         }
  225.                         return false;
  226.                 }
  227.         }
  228.  
  229.         public function getChildren() {
  230.                 $out = array();
  231.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  232.                         $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId()));
  233.                         while ($row = $res->fetch_array()) {
  234.                                 $obj = self::parse($row['id']);
  235.                                 if (!$obj) continue;
  236.                                 $out[] = $obj;
  237.                         }
  238.                 } else {
  239.                         self::buildObjectInformationCache();
  240.  
  241.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  242.                                 if ($parent == $this->nodeId()) {
  243.                                         $obj = self::parse($id);
  244.                                         if (!$obj) continue;
  245.                                         $out[] = $obj;
  246.                                 }
  247.                         }
  248.                 }
  249.                 return $out;
  250.         }
  251.  
  252.         public function getRa() {
  253.                 return new OIDplusRA($this->getRaMail());
  254.         }
  255.  
  256.         public function userHasReadRights($ra_email=null) {
  257.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  258.  
  259.                 // Admin may do everything
  260.                 if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
  261.  
  262.                 // If it is not confidential, everybody can read/see it.
  263.                 if (!$this->isConfidential()) return true;
  264.  
  265.                 // If we own the object, we may see it
  266.                 if (is_null($ra_email)) {
  267.                         if ($this->userHasWriteRights()) return true;
  268.                 } else {
  269.                         if ($this->getRaMail() == $ra_email) return true;
  270.                 }
  271.  
  272.                 // If someone has rights to an object below our confidential node,
  273.                 // we let him see the confidential node,
  274.                 // Otherwise he could not browse through to his own node.
  275.                 $roots = $this->getRaRoots($ra_email);
  276.                 foreach ($roots as $root) {
  277.                         if ($root->isChildOf($this)) return true;
  278.                 }
  279.  
  280.                 return false;
  281.         }
  282.  
  283.         public function getIcon($row=null) {
  284.                 $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
  285.  
  286.                 if (is_null($row)) {
  287.                         $ra_email = $this->getRaMail();
  288.                 } else {
  289.                         $ra_email = $row['ra_email'];
  290.                 }
  291.                 // TODO: have different icons for Leaf-Nodes
  292.                 if (OIDplus::authUtils()::isRaLoggedIn($ra_email)) {
  293.                         $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_own.png';
  294.                 } else {
  295.                         $icon = 'plugins/objectTypes/'.$namespace.'/img/treeicon_general.png';
  296.                 }
  297.                 if (!file_exists($icon)) $icon = null; // default icon (folder)
  298.                 return $icon;
  299.         }
  300.  
  301.         public static function exists($id) {
  302.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  303.                         $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
  304.                         return $res->num_rows() > 0;
  305.                 } else {
  306.                         self::buildObjectInformationCache();
  307.                         return isset(self::$object_info_cache[$id]);
  308.                 }
  309.         }
  310.  
  311.         public function getParent() {
  312.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  313.                         $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
  314.                         if ($res->num_rows() == 0) return null;
  315.                         $row = $res->fetch_array();
  316.                         $parent = $row['parent'];
  317.                         $obj = OIDplusObject::parse($parent);
  318.                         if ($obj) return $obj;
  319.                 } else {
  320.                         self::buildObjectInformationCache();
  321.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  322.                                 $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
  323.                                 $obj = OIDplusObject::parse($parent);
  324.                                 if ($obj) return $obj;
  325.                         }
  326.  
  327.                         // 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()
  328.                         $cur = $this->one_up();
  329.                         if (!$cur) return false;
  330.                         do {
  331.                                 if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
  332.  
  333.                                 $prev = $cur;
  334.                                 $cur = $cur->one_up();
  335.                                 if (!$cur) return false;
  336.                         } while ($prev != $cur);
  337.  
  338.                         return false;
  339.                 }
  340.         }
  341.  
  342.         public function getRaMail() {
  343.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  344.                         $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
  345.                         if ($res->num_rows() == 0) return null;
  346.                         $row = $res->fetch_array();
  347.                         return $row['ra_email'];
  348.                 } else {
  349.                         self::buildObjectInformationCache();
  350.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  351.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
  352.                         }
  353.                         return false;
  354.                 }
  355.         }
  356.  
  357.         public function getTitle() {
  358.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  359.                         $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
  360.                         if ($res->num_rows() == 0) return null;
  361.                         $row = $res->fetch_array();
  362.                         return $row['title'];
  363.                 } else {
  364.                         self::buildObjectInformationCache();
  365.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  366.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
  367.                         }
  368.                         return false;
  369.                 }
  370.         }
  371.  
  372.         public function userHasParentalWriteRights($ra_email=null) {
  373.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  374.  
  375.                 if (is_null($ra_email)) {
  376.                         if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
  377.                 }
  378.  
  379.                 $objParent = $this->getParent();
  380.                 if (is_null($objParent)) return false;
  381.                 return $objParent->userHasWriteRights($ra_email);
  382.         }
  383.  
  384.         public function userHasWriteRights($ra_email=null) {
  385.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  386.  
  387.                 if (is_null($ra_email)) {
  388.                         if (OIDplus::authUtils()::isAdminLoggedIn()) return true;
  389.                         return OIDplus::authUtils()::isRaLoggedIn($this->getRaMail());
  390.                 } else {
  391.                         return $this->getRaMail() == $ra_email;
  392.                 }
  393.         }
  394.  
  395.         public function distance($to) {
  396.                 return null; // not implemented
  397.         }
  398.  
  399.         public function equals($obj) {
  400.                 if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
  401.                 if (!($obj instanceof $this)) return false;
  402.  
  403.                 $distance = $this->distance($obj);
  404.                 if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
  405.  
  406.                 return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
  407.         }
  408.  
  409.         public static function findFitting($id) {
  410.                 $obj = OIDplusObject::parse($id);
  411.                 if (!$obj) throw new OIDplusException(_L('findFitting: Parse failed'));
  412.  
  413.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  414.                         $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
  415.                         while ($row = $res->fetch_object()) {
  416.                                 $test = OIDplusObject::parse($row->id);
  417.                                 if ($obj->equals($test)) return $test;
  418.                         }
  419.                         return false;
  420.                 } else {
  421.                         self::buildObjectInformationCache();
  422.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  423.                                 if (strpos($id, $obj->ns().':') === 0) {
  424.                                         $test = OIDplusObject::parse($id);
  425.                                         if ($obj->equals($test)) return $test;
  426.                                 }
  427.                         }
  428.                         return false;
  429.                 }
  430.         }
  431.  
  432.         public function one_up() {
  433.                 return null; // not implemented
  434.         }
  435.  
  436.         // Caching stuff
  437.  
  438.         protected static $object_info_cache = null;
  439.  
  440.         public static function resetObjectInformationCache() {
  441.                 self::$object_info_cache = null;
  442.         }
  443.  
  444.         const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc.
  445.         const CACHE_PARENT = 1;
  446.         const CACHE_RA_EMAIL = 2;
  447.         const CACHE_TITLE = 3;
  448.  
  449.         private static function buildObjectInformationCache() {
  450.                 if (is_null(self::$object_info_cache)) {
  451.                         self::$object_info_cache = array();
  452.                         $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects");
  453.                         while ($row = $res->fetch_array()) {
  454.                                 self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
  455.                         }
  456.                 }
  457.         }
  458. }