Subversion Repositories oidplus

Rev

Rev 804 | Rev 817 | 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.                 // We use $this:: instead of self:: , because we want to call the overridden methods
  312.                 if (OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
  313.                         $icon = $dir.'/'.$this::treeIconFilename('own');
  314.                 } else {
  315.                         $icon = $dir.'/'.$this::treeIconFilename('general');
  316.                 }
  317.  
  318.                 if (!file_exists($icon)) return null; // default icon (folder)
  319.  
  320.                 return $icon;
  321.         }
  322.  
  323.         public static function exists($id) {
  324.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  325.                         $res = OIDplus::db()->query("select id from ###objects where id = ?", array($id));
  326.                         return $res->any();
  327.                 } else {
  328.                         self::buildObjectInformationCache();
  329.                         return isset(self::$object_info_cache[$id]);
  330.                 }
  331.         }
  332.  
  333.         // Get parent gives the next possible parent which is EXISTING in OIDplus
  334.         // It does not give the immediate parent
  335.         public function getParent() {
  336.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  337.                         $res = OIDplus::db()->query("select parent from ###objects where id = ?", array($this->nodeId()));
  338.                         if (!$res->any()) return null;
  339.                         $row = $res->fetch_array();
  340.                         $parent = $row['parent'];
  341.                         $obj = OIDplusObject::parse($parent);
  342.                         if ($obj) return $obj;
  343.                         // TODO: Also implement one_up() like below
  344.                 } else {
  345.                         self::buildObjectInformationCache();
  346.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  347.                                 $parent = self::$object_info_cache[$this->nodeId()][self::CACHE_PARENT];
  348.                                 $obj = OIDplusObject::parse($parent);
  349.                                 if ($obj) return $obj;
  350.                         }
  351.  
  352.                         // 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()
  353.                         $cur = $this->one_up();
  354.                         if (!$cur) return null;
  355.                         do {
  356.                                 // findFitting() checks if that OID exists
  357.                                 if ($fitting = self::findFitting($cur->nodeId())) return $fitting;
  358.  
  359.                                 $prev = $cur;
  360.                                 $cur = $cur->one_up();
  361.                                 if (!$cur) return null;
  362.                         } while ($prev != $cur);
  363.  
  364.                         return null;
  365.                 }
  366.         }
  367.  
  368.         public function getRaMail() {
  369.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  370.                         $res = OIDplus::db()->query("select ra_email from ###objects where id = ?", array($this->nodeId()));
  371.                         if (!$res->any()) return null;
  372.                         $row = $res->fetch_array();
  373.                         return $row['ra_email'];
  374.                 } else {
  375.                         self::buildObjectInformationCache();
  376.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  377.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_RA_EMAIL];
  378.                         }
  379.                         return false;
  380.                 }
  381.         }
  382.  
  383.         public function getTitle() {
  384.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  385.                         $res = OIDplus::db()->query("select title from ###objects where id = ?", array($this->nodeId()));
  386.                         if (!$res->any()) return null;
  387.                         $row = $res->fetch_array();
  388.                         return $row['title'];
  389.                 } else {
  390.                         self::buildObjectInformationCache();
  391.                         if (isset(self::$object_info_cache[$this->nodeId()])) {
  392.                                 return self::$object_info_cache[$this->nodeId()][self::CACHE_TITLE];
  393.                         }
  394.                         return false;
  395.                 }
  396.         }
  397.  
  398.         public function userHasParentalWriteRights($ra_email=null) {
  399.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  400.  
  401.                 if (is_null($ra_email)) {
  402.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  403.                 }
  404.  
  405.                 $objParent = $this->getParent();
  406.                 if (!$objParent) return false;
  407.                 return $objParent->userHasWriteRights($ra_email);
  408.         }
  409.  
  410.         public function userHasWriteRights($ra_email=null) {
  411.                 if ($ra_email instanceof OIDplusRA) $ra_email = $ra_email->raEmail();
  412.  
  413.                 if (is_null($ra_email)) {
  414.                         if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
  415.                         return OIDplus::authUtils()->isRaLoggedIn($this->getRaMail());
  416.                 } else {
  417.                         return $this->getRaMail() == $ra_email;
  418.                 }
  419.         }
  420.  
  421.         public function distance($to) {
  422.                 return null; // not implemented
  423.         }
  424.  
  425.         public function equals($obj) {
  426.                 if (!is_object($obj)) $obj = OIDplusObject::parse($obj);
  427.                 if (!($obj instanceof $this)) return false;
  428.  
  429.                 $distance = $this->distance($obj);
  430.                 if (is_numeric($distance)) return $distance === 0; // if the distance function is implemented, use it
  431.  
  432.                 return $this->nodeId() == $obj->nodeId(); // otherwise compare the node id case-sensitive
  433.         }
  434.  
  435.         public static function findFitting($id) {
  436.                 $obj = OIDplusObject::parse($id);
  437.                 if (!$obj) throw new OIDplusException(_L('findFitting: Parse failed'));
  438.  
  439.                 if (!OIDplus::baseConfig()->getValue('OBJECT_CACHING', true)) {
  440.                         $res = OIDplus::db()->query("select id from ###objects where id like ?", array($obj->ns().':%'));
  441.                         while ($row = $res->fetch_object()) {
  442.                                 $test = OIDplusObject::parse($row->id);
  443.                                 if ($obj->equals($test)) return $test;
  444.                         }
  445.                         return false;
  446.                 } else {
  447.                         self::buildObjectInformationCache();
  448.                         foreach (self::$object_info_cache as $id => list($confidential, $parent, $ra_email, $title)) {
  449.                                 if (strpos($id, $obj->ns().':') === 0) {
  450.                                         $test = OIDplusObject::parse($id);
  451.                                         if ($obj->equals($test)) return $test;
  452.                                 }
  453.                         }
  454.                         return false;
  455.                 }
  456.         }
  457.  
  458.         public function one_up() {
  459.                 return null; // not implemented
  460.         }
  461.  
  462.         // Caching stuff
  463.  
  464.         protected static $object_info_cache = null;
  465.  
  466.         public static function resetObjectInformationCache() {
  467.                 self::$object_info_cache = null;
  468.         }
  469.  
  470.         const CACHE_CONFIDENTIAL = 0; // TODO: An object would be better so you can use $cacheitem->isConfidential() etc.
  471.         const CACHE_PARENT = 1;
  472.         const CACHE_RA_EMAIL = 2;
  473.         const CACHE_TITLE = 3;
  474.  
  475.         private static function buildObjectInformationCache() {
  476.                 if (is_null(self::$object_info_cache)) {
  477.                         self::$object_info_cache = array();
  478.                         $res = OIDplus::db()->query("select id, parent, confidential, ra_email, title from ###objects");
  479.                         while ($row = $res->fetch_array()) {
  480.                                 self::$object_info_cache[$row['id']] = array($row['confidential'], $row['parent'], $row['ra_email'], $row['title']);
  481.                         }
  482.                 }
  483.         }
  484.  
  485.         // override this function if you want your object type to save
  486.         // attachments in directories with easy names.
  487.         // Take care that your custom directory name will not allow jailbreaks (../) !
  488.         public function getDirectoryName() {
  489.                 if ($this->isRoot()) return $this->ns();
  490.                 return $this->getLegacyDirectoryName();
  491.         }
  492.  
  493.         public final function getLegacyDirectoryName() {
  494.                 if ($this::ns() == 'oid') {
  495.                         $oid = $this->nodeId(false);
  496.                 } else {
  497.                         $oid = null;
  498.                         $alt_ids = $this->getAltIds();
  499.                         foreach ($alt_ids as $alt_id) {
  500.                                 if ($alt_id->getNamespace() == 'oid') {
  501.                                         $oid = $alt_id->getId();
  502.                                         break; // we prefer the first OID (for GUIDs, the first OID is the OIDplus-OID, and the second OID is the UUID OID)
  503.                                 }
  504.                         }
  505.                 }
  506.  
  507.                 if (!is_null($oid) && ($oid != '')) {
  508.                         // For OIDs, it is the OID, for other identifiers
  509.                         // it it the OID alt ID (generated using the SystemID)
  510.                         return str_replace('.', '_', $oid);
  511.                 } else {
  512.                         // Can happen if you don't have a system ID (due to missing OpenSSL plugin)
  513.                         return md5($this->nodeId(true)); // we don't use $id, because $this->nodeId(true) is possibly more canonical than $id
  514.                 }
  515.         }
  516.  
  517.         public static function treeIconFilename($mode) {
  518.                 // for backwards-compatibility with older plugins
  519.                 return 'img/treeicon_'.$mode.'.png';
  520.         }
  521.  
  522. }
  523.