Subversion Repositories oidplus

Rev

Rev 790 | Rev 803 | 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 extends OIDplusBaseClass {
  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 function crudInsertPrefix() {
  70.                 return '';
  71.         }
  72.  
  73.         public function crudInsertSuffix() {
  74.                 return '';
  75.         }
  76.  
  77.         public abstract function jsTreeNodeName(OIDplusObject $parent = null);
  78.  
  79.         public abstract function defaultTitle();
  80.  
  81.         public abstract function isLeafNode();
  82.  
  83.         public abstract function getContentPage(&$title, &$content, &$icon);
  84.  
  85.         public static function getRaRoots($ra_email=null) {
  86.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  87.  
  88.                 $out = array();
  89.  
  90.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  91.                         if (is_null($ra_email)) {
  92.                                 $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 ".
  93.                                                             "left join ###objects as oParent on oChild.parent = oParent.id ".
  94.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'));
  95.                                 while ($row = $res->fetch_array()) {
  96.                                         if (!OIDplus::authUtils()->isRaLoggedIn($row['parent_mail']) && OIDplus::authUtils()->isRaLoggedIn($row['child_mail'])) {
  97.                                                 $x = self::parse($row['id']); // can be FALSE if namespace was disabled
  98.                                                 if ($x) $out[] = $x;
  99.                                         }
  100.                                 }
  101.                         } else {
  102.                                 $res = OIDplus::db()->query("select oChild.id as id from ###objects as oChild ".
  103.                                                             "left join ###objects as oParent on oChild.parent = oParent.id ".
  104.                                                             "where (".OIDplus::db()->getSlang()->isNullFunction('oParent.ra_email',"''")." <> ? and ".
  105.                                                             OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) or ".
  106.                                                             "      (oParent.ra_email is null and ".OIDplus::db()->getSlang()->isNullFunction('oChild.ra_email',"''")." = ?) ".
  107.                                                             "order by ".OIDplus::db()->natOrder('oChild.id'), array($ra_email, $ra_email, $ra_email));
  108.                                 while ($row = $res->fetch_array()) {
  109.                                         $x = self::parse($row['id']); // can be FALSE if namespace was disabled
  110.                                         if ($x) $out[] = self::parse($row['id']);
  111.                                 }
  112.                         }
  113.                 } else {
  114.                         if (is_null($ra_email)) {
  115.                                 $ra_mails_to_check = OIDplus::authUtils()->loggedInRaList();
  116.                                 if (count($ra_mails_to_check) == 0) return $out;
  117.                         } else {
  118.                                 $ra_mails_to_check = array($ra_email);
  119.                         }
  120.  
  121.                         self::buildObjectInformationCache();
  122.  
  123.                         foreach ($ra_mails_to_check as $check_ra_mail) {
  124.                                 $out_part = array();
  125.  
  126.                                 foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  127.                                         // If the OID RA is the RA we are searching, then add the object to the choice list
  128.                                         if ($ra_email == $check_ra_mail) $out_part[] = $id;
  129.                                 }
  130.  
  131.                                 foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  132.                                         if (isset(self::$object_info_cache[$parent])) {
  133.                                                 if (self::$object_info_cache[$parent][self::CACHE_RA_EMAIL] == $ra_email) {
  134.                                                         // if the parent has the same RA, then this OID cannot be a root => remove the element from the choice list
  135.                                                         foreach (array_keys($out_part, $id) as $key) unset($out_part[$key]);
  136.                                                 }
  137.                                         }
  138.                                 }
  139.  
  140.                                 natsort($out_part);
  141.  
  142.                                 foreach ($out_part as $id) {
  143.                                         $obj = self::parse($id);
  144.                                         if ($obj) $out[] = $obj;
  145.                                 }
  146.                         }
  147.                 }
  148.  
  149.                 return $out;
  150.         }
  151.  
  152.         public static function getAllNonConfidential() {
  153.                 $out = array();
  154.  
  155.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  156.                         $res = OIDplus::db()->query("select id from ###objects where confidential = ? order by ".OIDplus::db()->natOrder('id'), array(false));
  157.  
  158.                         while ($row = $res->fetch_array()) {
  159.                                 $obj = self::parse($row['id']); // will be NULL if the object type is not registered
  160.                                 if ($obj && (!$obj->isConfidential())) {
  161.                                         $out[] = $row['id'];
  162.                                 }
  163.                         }
  164.                 } else {
  165.                         self::buildObjectInformationCache();
  166.  
  167.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  168.                                 if (!$confidential) {
  169.                                         $obj = self::parse($id); // will be NULL if the object type is not registered
  170.                                         if ($obj && (!$obj->isConfidential())) {
  171.                                                 $out[] = $id;
  172.                                         }
  173.                                 }
  174.                         }
  175.                 }
  176.  
  177.                 return $out;
  178.         }
  179.  
  180.         public function isConfidential() {
  181.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  182.                         //static $confidential_cache = array();
  183.                         $curid = $this->nodeId();
  184.                         //$orig_curid = $curid;
  185.                         //if (isset($confidential_cache[$curid])) return $confidential_cache[$curid];
  186.                         // Recursively search for the confidential flag in the parents
  187.                         while (($res = OIDplus::db()->query("select parent, confidential from ###objects where id = ?", array($curid)))->any()) {
  188.                                 $row = $res->fetch_array();
  189.                                 if ($row['confidential']) {
  190.                                         //$confidential_cache[$curid] = true;
  191.                                         //$confidential_cache[$orig_curid] = true;
  192.                                         return true;
  193.                                 } else {
  194.                                         //$confidential_cache[$curid] = false;
  195.                                 }
  196.                                 $curid = $row['parent'];
  197.                                 //if (isset($confidential_cache[$curid])) {
  198.                                         //$confidential_cache[$orig_curid] = $confidential_cache[$curid];
  199.                                         //return $confidential_cache[$curid];
  200.                                 //}
  201.                         }
  202.  
  203.                         //$confidential_cache[$orig_curid] = false;
  204.                         return false;
  205.                 } else {
  206.                         self::buildObjectInformationCache();
  207.  
  208.                         $curid = $this->nodeId();
  209.                         // Recursively search for the confidential flag in the parents
  210.                         while (isset(self::$object_info_cache[$curid])) {
  211.                                 if (self::$object_info_cache[$curid][self::CACHE_CONFIDENTIAL]) return true;
  212.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  213.                         }
  214.                         return false;
  215.                 }
  216.         }
  217.  
  218.         public function isChildOf(OIDplusObject $obj) {
  219.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  220.                         $curid = $this->nodeId();
  221.                         while (($res = OIDplus::db()->query("select parent from ###objects where id = ?", array($curid)))->any()) {
  222.                                 $row = $res->fetch_array();
  223.                                 if ($curid == $obj->nodeId()) return true;
  224.                                 $curid = $row['parent'];
  225.                         }
  226.                         return false;
  227.                 } else {
  228.                         self::buildObjectInformationCache();
  229.  
  230.                         $curid = $this->nodeId();
  231.                         while (isset(self::$object_info_cache[$curid])) {
  232.                                 if ($curid == $obj->nodeId()) return true;
  233.                                 $curid = self::$object_info_cache[$curid][self::CACHE_PARENT];
  234.                         }
  235.                         return false;
  236.                 }
  237.         }
  238.  
  239.         public function getChildren() {
  240.                 $out = array();
  241.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  242.                         $res = OIDplus::db()->query("select id from ###objects where parent = ?", array($this->nodeId()));
  243.                         while ($row = $res->fetch_array()) {
  244.                                 $obj = self::parse($row['id']);
  245.                                 if (!$obj) continue;
  246.                                 $out[] = $obj;
  247.                         }
  248.                 } else {
  249.                         self::buildObjectInformationCache();
  250.  
  251.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  252.                                 if ($parent == $this->nodeId()) {
  253.                                         $obj = self::parse($id);
  254.                                         if (!$obj) continue;
  255.                                         $out[] = $obj;
  256.                                 }
  257.                         }
  258.                 }
  259.                 return $out;
  260.         }
  261.  
  262.         public function getRa() {
  263.                 return new OIDplusRA($this->getRaMail());
  264.         }
  265.  
  266.         public function userHasReadRights($ra_email=null) {
  267.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  268.  
  269.                 // If it is not confidential, everybody can read/see it.
  270.                 // Note: This also checks if superior OIDs are confidential.
  271.                 if (!$this->isConfidential()) return true;
  272.  
  273.                 if (is_null($ra_email)) {
  274.                         // Admin may do everything
  275.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  276.  
  277.                         // If the RA is logged in, then they can see the OID.
  278.                         if (OIDplus::authUtils()->isRaLoggedIn($this->getRaMail())) return true;
  279.                 } else {
  280.                         // If this OID belongs to the requested RA, then they may see it.
  281.                         if ($this->getRaMail() == $ra_email) return true;
  282.                 }
  283.  
  284.                 // If someone has rights to an object below our confidential node,
  285.                 // we let him see the confidential node,
  286.                 // Otherwise he could not browse through to his own node.
  287.                 $roots = $this->getRaRoots($ra_email);
  288.                 foreach ($roots as $root) {
  289.                         if ($root->isChildOf($this)) return true;
  290.                 }
  291.  
  292.                 return false;
  293.         }
  294.  
  295.         public function getIcon($row=null) {
  296.                 $namespace = $this->ns(); // must use $this, not self::, otherwise the virtual method will not be called
  297.  
  298.                 if (is_null($row)) {
  299.                         $ra_email = $this->getRaMail();
  300.                 } else {
  301.                         $ra_email = $row['ra_email'];
  302.                 }
  303.                 // TODO: have different icons for Leaf-Nodes
  304.  
  305.                 $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/objectTypes/'.$namespace.'/');
  306.  
  307.                 if (count($dirs) == 0) return null; // default icon (folder)
  308.  
  309.                 $dir = substr($dirs[0], strlen(OIDplus::localpath()));
  310.  
  311.                 if (OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
  312.                         $icon = $dir.'/'.$this->rootIconname('own');
  313.                 } else {
  314.                         $icon = $dir.'/'.$this->rootIconname('general');
  315.                 }
  316.  
  317.                 if (!file_exists($icon)) return null; // default icon (folder)
  318.  
  319.                 return $icon;
  320.         }
  321.  
  322.         public static function exists($id) {
  323.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  324.                         $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
  325.                         return $res->any();
  326.                 } else {
  327.                         self::buildObjectInformationCache();
  328.                         return isset(self::$object_info_cache[$id]);
  329.                 }
  330.         }
  331.  
  332.         // Get parent gives the next possible parent which is EXISTING in OIDplus
  333.         // It does not give the immediate parent
  334.         public function getParent() {
  335.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  336.                         $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
  337.                         if (!$res->any()) return null;
  338.                         $row = $res->fetch_array();
  339.                         $parent = $row['parent'];
  340.                         $obj = OIDplusObject::parse($parent);
  341.                         if ($obj) return $obj;
  342.                         // TODO: Also implement one_up() like below
  343.                 } else {
  344.                         self::buildObjectInformationCache();
  345.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  346.                                 $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
  347.                                 $obj = OIDplusObject::parse($parent);
  348.                                 if ($obj) return $obj;
  349.                         }
  350.  
  351.                         // 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()
  352.                         $cur = $this->one_up();
  353.                         if (!$cur) return null;
  354.                         do {
  355.                                 // findFitting() checks if that OID exists
  356.                                 if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
  357.  
  358.                                 $prev = $cur;
  359.                                 $cur = $cur->one_up();
  360.                                 if (!$cur) return null;
  361.                         } while ($prev != $cur);
  362.  
  363.                         return null;
  364.                 }
  365.         }
  366.  
  367.         public function getRaMail() {
  368.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  369.                         $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
  370.                         if (!$res->any()) return null;
  371.                         $row = $res->fetch_array();
  372.                         return $row['ra_email'];
  373.                 } else {
  374.                         self::buildObjectInformationCache();
  375.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  376.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
  377.                         }
  378.                         return false;
  379.                 }
  380.         }
  381.  
  382.         public function getTitle() {
  383.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  384.                         $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
  385.                         if (!$res->any()) return null;
  386.                         $row = $res->fetch_array();
  387.                         return $row['title'];
  388.                 } else {
  389.                         self::buildObjectInformationCache();
  390.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  391.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
  392.                         }
  393.                         return false;
  394.                 }
  395.         }
  396.  
  397.         public function userHasParentalWriteRights($ra_email=null) {
  398.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  399.  
  400.                 if (is_null($ra_email)) {
  401.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  402.                 }
  403.  
  404.                 $objParent = $this->getParent();
  405.                 if (!$objParent) return false;
  406.                 return $objParent->userHasWriteRights($ra_email);
  407.         }
  408.  
  409.         public function userHasWriteRights($ra_email=null) {
  410.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  411.  
  412.                 if (is_null($ra_email)) {
  413.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  414.                         return OIDplus::authUtils()->isRaLoggedIn($this->getRaMail());
  415.                 } else {
  416.                         return $this->getRaMail() == $ra_email;
  417.                 }
  418.         }
  419.  
  420.         public function distance($to) {
  421.                 return null; // not implemented
  422.         }
  423.  
  424.         public function equals($obj) {
  425.                 if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
  426.                 if (!($obj instanceof $this)) return false;
  427.  
  428.                 $distance = $this->distance($obj);
  429.                 if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
  430.  
  431.                 return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
  432.         }
  433.  
  434.         public static function findFitting($id) {
  435.                 $obj = OIDplusObject::parse($id);
  436.                 if (!$obj) throw new OIDplusException(_L('findFitting: Parse failed'));
  437.  
  438.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  439.                         $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
  440.                         while ($row = $res->fetch_object()) {
  441.                                 $test = OIDplusObject::parse($row->id);
  442.                                 if ($obj->equals($test)) return $test;
  443.                         }
  444.                         return false;
  445.                 } else {
  446.                         self::buildObjectInformationCache();
  447.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  448.                                 if (strpos($id, $obj->ns().':') === 0) {
  449.                                         $test = OIDplusObject::parse($id);
  450.                                         if ($obj->equals($test)) return $test;
  451.                                 }
  452.                         }
  453.                         return false;
  454.                 }
  455.         }
  456.  
  457.         public function one_up() {
  458.                 return null; // not implemented
  459.         }
  460.  
  461.         // Caching stuff
  462.  
  463.         protected static $object_info_cache = null;
  464.  
  465.         public static function resetObjectInformationCache() {
  466.                 self::$object_info_cache = null;
  467.         }
  468.  
  469.         const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc.
  470.         const CACHE_PARENT = 1;
  471.         const CACHE_RA_EMAIL = 2;
  472.         const CACHE_TITLE = 3;
  473.  
  474.         private static function buildObjectInformationCache() {
  475.                 if (is_null(self::$object_info_cache)) {
  476.                         self::$object_info_cache = array();
  477.                         $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects");
  478.                         while ($row = $res->fetch_array()) {
  479.                                 self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
  480.                         }
  481.                 }
  482.         }
  483.  
  484.         // override this function if you want your object type to save
  485.         // attachments in directories with easy names.
  486.         // Take care that your custom directory name will not allow jailbreaks (../) !
  487.         public function getDirectoryName() {
  488.                 if ($this->isRoot()) return $this->ns();
  489.                 return $this->getLegacyDirectoryName();
  490.         }
  491.  
  492.         public final function getLegacyDirectoryName() {
  493.                 if ($this::ns() == 'oid') {
  494.                         $oid = $this->nodeId(false);
  495.                 } else {
  496.                         $oid = null;
  497.                         $alt_ids = $this->getAltIds();
  498.                         foreach ($alt_ids as $alt_id) {
  499.                                 if ($alt_id->getNamespace() == 'oid') {
  500.                                         $oid = $alt_id->getId();
  501.                                         break; // we prefer the first OID (for GUIDs, the first OID is the OIDplus-OID, and the second OID is the UUID OID)
  502.                                 }
  503.                         }
  504.                 }
  505.  
  506.                 if (!is_null($oid) && ($oid != '')) {
  507.                         // For OIDs, it is the OID, for other identifiers
  508.                         // it it the OID alt ID (generated using the SystemID)
  509.                         return str_replace('.', '_', $oid);
  510.                 } else {
  511.                         // Can happen if you don't have a system ID (due to missing OpenSSL plugin)
  512.                         return md5($this->nodeId(true)); // we don't use $id, because $this->nodeId(true) is possibly more canonical than $id
  513.                 }
  514.         }
  515.  
  516.         public function rootIconname($mode) {
  517.                 // for backwards-compatibility with older plugins
  518.                 return 'img/treeicon_'.$mode.'.png';
  519.         }
  520.  
  521. }
  522.