Subversion Repositories oidplus

Rev

Rev 1012 | Rev 1050 | 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 - 2022 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. class OIDplusPagePublicObjects extends OIDplusPagePluginPublic {
  23.  
  24.         private function get_treeicon_root($ot) {
  25.                 $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/objectTypes/'.$ot::ns());
  26.  
  27.                 if (count($dirs) == 0) {
  28.                         $icon = null;
  29.                 } else {
  30.                         $dir = $dirs[0];
  31.                         $icon_name = $ot::treeIconFilename('root');
  32.                         if (!$icon_name) return null;
  33.                         $icon = $dir.'/'.$icon_name;
  34.                         if (!file_exists($icon)) return null;
  35.                         $icon = substr($icon, strlen(OIDplus::localpath()));
  36.                 }
  37.  
  38.                 return $icon;
  39.         }
  40.  
  41.         private function ra_change_rec($id, $old_ra, $new_ra) {
  42.                 if (is_null($old_ra)) $old_ra = '';
  43.                 OIDplus::db()->query("update ###objects set ra_email = ?, updated = ".OIDplus::db()->sqlDate()." where id = ? and ".OIDplus::db()->getSlang()->isNullFunction('ra_email',"''")." = ?", array($new_ra, $id, $old_ra));
  44.                 OIDplusObject::resetObjectInformationCache();
  45.  
  46.                 $res = OIDplus::db()->query("select id from ###objects where parent = ? and ".OIDplus::db()->getSlang()->isNullFunction('ra_email',"''")." = ?", array($id, $old_ra));
  47.                 while ($row = $res->fetch_array()) {
  48.                         $this->ra_change_rec($row['id'], $old_ra, $new_ra);
  49.                 }
  50.         }
  51.  
  52.         public function action($actionID, $params) {
  53.  
  54.                 // Action:     Delete
  55.                 // Method:     POST
  56.                 // Parameters: id
  57.                 // Outputs:    <0 Error, =0 Success
  58.                 if ($actionID == 'Delete') {
  59.                         _CheckParamExists($params, 'id');
  60.                         $id = $params['id'];
  61.                         $obj = OIDplusObject::parse($id);
  62.                         if ($obj === null) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','DELETE',$id));
  63.  
  64.                         if (!OIDplusObject::exists($id)) {
  65.                                 throw new OIDplusException(_L('Object %1 does not exist',$id));
  66.                         }
  67.  
  68.                         // Check if permitted
  69.                         if (!$obj->userHasParentalWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the superior RA to delete this OID.'));
  70.  
  71.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  72.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  73.                                         $plugin->beforeObjectDelete($id);
  74.                                 }
  75.                         }
  76.  
  77.                         OIDplus::logger()->log("[WARN]OID($id)+[?WARN/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "Object '$id' (recursively) deleted");
  78.                         OIDplus::logger()->log("[CRIT]OIDRA($id)!", "Lost ownership of object '$id' because it was deleted");
  79.  
  80.                         if ($parentObj = $obj->getParent()) {
  81.                                 $parent_oid = $parentObj->nodeId();
  82.                                 OIDplus::logger()->log("[WARN]OID($parent_oid)", "Object '$id' (recursively) deleted");
  83.                         }
  84.  
  85.                         // Delete object
  86.                         OIDplus::db()->query("delete from ###objects where id = ?", array($id));
  87.                         OIDplusObject::resetObjectInformationCache();
  88.  
  89.                         // Delete orphan stuff
  90.                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  91.                                 do {
  92.                                         $res = OIDplus::db()->query("select tchild.id from ###objects tchild " .
  93.                                                                     "left join ###objects tparent on tparent.id = tchild.parent " .
  94.                                                                     "where tchild.parent <> ? and tchild.id like ? and tparent.id is null;", array($ot::root(), $ot::root().'%'));
  95.                                         if (!$res->any()) break;
  96.  
  97.                                         while ($row = $res->fetch_array()) {
  98.                                                 $id_to_delete = $row['id'];
  99.                                                 OIDplus::logger()->log("[CRIT]OIDRA($id_to_delete)!", "Lost ownership of object '$id_to_delete' because one of the superior objects ('$id') was recursively deleted");
  100.                                                 OIDplus::db()->query("delete from ###objects where id = ?", array($id_to_delete));
  101.                                                 OIDplusObject::resetObjectInformationCache();
  102.                                         }
  103.                                 } while (true);
  104.                         }
  105.                         OIDplus::db()->query("delete from ###asn1id where well_known = ? and oid not in (select id from ###objects where id like 'oid:%')", array(false));
  106.                         OIDplus::db()->query("delete from ###iri    where well_known = ? and oid not in (select id from ###objects where id like 'oid:%')", array(false));
  107.  
  108.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  109.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  110.                                         $plugin->afterObjectDelete($id);
  111.                                 }
  112.                         }
  113.  
  114.                         return array("status" => 0);
  115.                 }
  116.  
  117.                 // Action:     Update
  118.                 // Method:     POST
  119.                 // Parameters: id, ra_email, comment, iris, asn1ids, confidential
  120.                 // Outputs:    <0 Error, =0 Success, with following bitfields for further information:
  121.                 //             1 = RA is not registered
  122.                 //             2 = RA is not registered, but it cannot be invited
  123.                 //             4 = OID is a well-known OID, so RA, ASN.1 and IRI identifiers were reset
  124.                 else if ($actionID == 'Update') {
  125.                         _CheckParamExists($params, 'id');
  126.                         $id = $params['id'];
  127.                         $obj = OIDplusObject::parse($id);
  128.                         if ($obj === null) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','UPDATE',$id));
  129.  
  130.                         if (!OIDplusObject::exists($id)) {
  131.                                 throw new OIDplusException(_L('Object %1 does not exist',$id));
  132.                         }
  133.  
  134.                         // Check if permitted
  135.                         if (!$obj->userHasParentalWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the superior RA to update this OID.'));
  136.  
  137.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  138.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  139.                                         $plugin->beforeObjectUpdateSuperior($id, $params);
  140.                                 }
  141.                         }
  142.  
  143.                         // First, do a simulation for ASN.1 IDs and IRIs to check if there are any problems (then an Exception will be thrown)
  144.                         if ($obj::ns() == 'oid') {
  145.                                 if (!$obj->isWellKnown()) {
  146.                                         if (isset($params['iris'])) {
  147.                                                 $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
  148.                                                 $ids = array_map('trim',$ids);
  149.                                                 $obj->replaceIris($ids, true);
  150.                                         }
  151.  
  152.                                         if (isset($params['asn1ids'])) {
  153.                                                 $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
  154.                                                 $ids = array_map('trim',$ids);
  155.                                                 $obj->replaceAsn1Ids($ids, true);
  156.                                         }
  157.                                 }
  158.                         }
  159.  
  160.                         // RA E-Mail change
  161.                         if (isset($params['ra_email'])) {
  162.                                 // Validate RA email address
  163.                                 $new_ra = $params['ra_email'];
  164.                                 if ($obj::ns() == 'oid') {
  165.                                         if ($obj->isWellKnown()) {
  166.                                                 $new_ra = '';
  167.                                         }
  168.                                 }
  169.                                 if (!empty($new_ra) && !OIDplus::mailUtils()->validMailAddress($new_ra)) {
  170.                                         throw new OIDplusException(_L('Invalid RA email address'));
  171.                                 }
  172.  
  173.                                 // Change RA recursively
  174.                                 $current_ra = $obj->getRaMail();
  175.                                 if ($new_ra != $current_ra) {
  176.                                         OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
  177.                                         OIDplus::logger()->log("[WARN]RA($current_ra)!",           "Lost ownership of object '$id' due to RA transfer of superior RA / admin.");
  178.                                         OIDplus::logger()->log("[INFO]RA($new_ra)!",               "Gained ownership of object '$id' due to RA transfer of superior RA / admin.");
  179.                                         if ($parentObj = $obj->getParent()) {
  180.                                                 $parent_oid = $parentObj->nodeId();
  181.                                                 OIDplus::logger()->log("[INFO]OID($parent_oid)", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
  182.                                         }
  183.                                         $this->ra_change_rec($id, $current_ra, $new_ra); // Recursively change inherited RAs
  184.                                 }
  185.                         }
  186.  
  187.                         // Log if confidentially flag was changed
  188.                         OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]SUPOIDRA($id)?/[?INFO/!OK]A?", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
  189.                         if ($parentObj = $obj->getParent()) {
  190.                                 $parent_oid = $parentObj->nodeId();
  191.                                 OIDplus::logger()->log("[INFO]OID($parent_oid)", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
  192.                         }
  193.  
  194.                         // Replace ASN.1 IDs und IRIs
  195.                         if ($obj::ns() == 'oid') {
  196.                                 if (!$obj->isWellKnown()) {
  197.                                         if (isset($params['iris'])) {
  198.                                                 $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
  199.                                                 $ids = array_map('trim',$ids);
  200.                                                 $obj->replaceIris($ids, false);
  201.                                         }
  202.  
  203.                                         if (isset($params['asn1ids'])) {
  204.                                                 $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
  205.                                                 $ids = array_map('trim',$ids);
  206.                                                 $obj->replaceAsn1Ids($ids, false);
  207.                                         }
  208.                                 }
  209.  
  210.                                 // TODO: Check if any identifiers have been actually changed,
  211.                                 // and log it to OID($id), OID($parent), ... (see above)
  212.                         }
  213.  
  214.                         if (isset($params['confidential'])) {
  215.                                 $confidential = $params['confidential'] == 'true';
  216.                                 OIDplus::db()->query("UPDATE ###objects SET confidential = ? WHERE id = ?", array($confidential, $id));
  217.                                 OIDplusObject::resetObjectInformationCache();
  218.                         }
  219.  
  220.                         if (isset($params['comment'])) {
  221.                                 $comment = $params['comment'];
  222.                                 OIDplus::db()->query("UPDATE ###objects SET comment = ? WHERE id = ?", array($comment, $id));
  223.                                 OIDplusObject::resetObjectInformationCache();
  224.                         }
  225.  
  226.                         OIDplus::db()->query("UPDATE ###objects SET updated = ".OIDplus::db()->sqlDate()." WHERE id = ?", array($id));
  227.                         OIDplusObject::resetObjectInformationCache();
  228.  
  229.                         $status = 0;
  230.  
  231.                         if (!empty($new_ra)) {
  232.                                 $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($new_ra));
  233.                                 $invitePlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.2.92'); // OIDplusPageRaInvite
  234.                                 if (!$res->any()) $status = !is_null($invitePlugin) && OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
  235.                         }
  236.  
  237.                         if ($obj::ns() == 'oid') {
  238.                                 if ($obj->isWellKnown()) {
  239.                                         $status += 4;
  240.                                 }
  241.                         }
  242.  
  243.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  244.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  245.                                         $plugin->afterObjectUpdateSuperior($id, $params);
  246.                                 }
  247.                         }
  248.  
  249.                         return array("status" => $status);
  250.                 }
  251.  
  252.                 // Action:     Update2
  253.                 // Method:     POST
  254.                 // Parameters: id, title, description
  255.                 // Outputs:    <0 Error, =0 Success
  256.                 else if ($actionID == 'Update2') {
  257.                         _CheckParamExists($params, 'id');
  258.                         $id = $params['id'];
  259.                         $obj = OIDplusObject::parse($id);
  260.                         if ($obj === null) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','UPDATE2',$id));
  261.  
  262.                         if (!OIDplusObject::exists($id)) {
  263.                                 throw new OIDplusException(_L('Object %1 does not exist',$id));
  264.                         }
  265.  
  266.                         // Check if allowed
  267.                         if (!$obj->userHasWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the RA to update this OID.'));
  268.  
  269.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  270.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  271.                                         $plugin->beforeObjectUpdateSelf($id, $params);
  272.                                 }
  273.                         }
  274.  
  275.                         OIDplus::logger()->log("[INFO]OID($id)+[?INFO/!OK]OIDRA($id)?/[?INFO/!OK]A?", "Title/Description of object '$id' updated");
  276.  
  277.                         if (isset($params['title'])) {
  278.                                 $title = $params['title'];
  279.                                 OIDplus::db()->query("UPDATE ###objects SET title = ? WHERE id = ?", array($title, $id));
  280.                                 OIDplusObject::resetObjectInformationCache();
  281.                         }
  282.  
  283.                         if (isset($params['description'])) {
  284.                                 $description = $params['description'];
  285.                                 OIDplus::db()->query("UPDATE ###objects SET description = ? WHERE id = ?", array($description, $id));
  286.                                 OIDplusObject::resetObjectInformationCache();
  287.                         }
  288.  
  289.                         OIDplus::db()->query("UPDATE ###objects SET updated = ".OIDplus::db()->sqlDate()." WHERE id = ?", array($id));
  290.                         OIDplusObject::resetObjectInformationCache();
  291.  
  292.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  293.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  294.                                         $plugin->afterObjectUpdateSelf($id, $params);
  295.                                 }
  296.                         }
  297.  
  298.                         return array("status" => 0);
  299.                 }
  300.  
  301.                 // Generate UUID
  302.                 else if ($actionID == 'generate_uuid') {
  303.                         $uuid = gen_uuid();
  304.                         if (!$uuid) return array("status" => 1);
  305.                         return array(
  306.                                 "status" => 0,
  307.                                 "uuid" => $uuid,
  308.                                 "intval" => substr(uuid_to_oid($uuid),strlen('2.25.'))
  309.                         );
  310.                 }
  311.  
  312.                 // Action:     Insert
  313.                 // Method:     POST
  314.                 // Parameters: parent, id, ra_email, confidential, iris, asn1ids
  315.                 // Outputs:    status=<0 Error, =0 Success, with following bitfields for further information:
  316.                 //             1 = RA is not registered
  317.                 //             2 = RA is not registered, but it cannot be invited
  318.                 //             4 = OID is a well-known OID, so RA, ASN.1 and IRI identifiers were reset
  319.                 else if ($actionID == 'Insert') {
  320.                         // Check if you have write rights on the parent (to create a new object)
  321.                         _CheckParamExists($params, 'parent');
  322.                         $objParent = OIDplusObject::parse($params['parent']);
  323.                         if ($objParent === null) throw new OIDplusException(_L('%1 action failed because parent object "%2" cannot be parsed!','INSERT',$params['parent']));
  324.  
  325.                         if (!$objParent->isRoot()) {
  326.                                 $idParent = $objParent->nodeId();
  327.                                 if (!OIDplusObject::exists($idParent)) {
  328.                                         throw new OIDplusException(_L('Parent object %1 does not exist',$idParent));
  329.                                 }
  330.                         }
  331.  
  332.                         if (!$objParent->userHasWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as the correct RA to insert an OID at this arc.'));
  333.  
  334.                         // Check if the ID is valid
  335.                         _CheckParamExists($params, 'id');
  336.                         if ($params['id'] == '') throw new OIDplusException(_L('ID may not be empty'));
  337.  
  338.                         // For the root objects, let the user also enter a WEID
  339.                         if ($objParent::ns() == 'oid') {
  340.                                 if (strtolower(substr(trim($params['id']),0,5)) === 'weid:') {
  341.                                         if ($objParent->isRoot()) {
  342.                                                 $params['id'] = WeidOidConverter::weid2oid($params['id']);
  343.                                                 if ($params['id'] === false) {
  344.                                                         throw new OIDplusException(_L('Invalid WEID'));
  345.                                                 }
  346.                                         } else {
  347.                                                 throw new OIDplusException(_L('You can use the WEID syntax only at your object tree root.'));
  348.                                         }
  349.                                 }
  350.                         }
  351.  
  352.                         // Determine absolute OID name
  353.                         // Note: At addString() and parse(), the syntax of the ID will be checked
  354.                         $id = $objParent->addString($params['id']);
  355.  
  356.                         // Check, if the OID exists
  357.                         if (OIDplusObject::exists($id)) {
  358.                                 throw new OIDplusException(_L('Object %1 already exists!',$id));
  359.                         }
  360.  
  361.                         $obj = OIDplusObject::parse($id);
  362.                         if ($obj === null) throw new OIDplusException(_L('%1 action failed because object "%2" cannot be parsed!','INSERT',$id));
  363.  
  364.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  365.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  366.                                         $plugin->beforeObjectInsert($id, $params);
  367.                                 }
  368.                         }
  369.  
  370.                         // First simulate if there are any problems of ASN.1 IDs und IRIs
  371.                         if ($obj::ns() == 'oid') {
  372.                                 if (!$obj->isWellKnown()) {
  373.                                         if (isset($params['iris'])) {
  374.                                                 $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
  375.                                                 $ids = array_map('trim',$ids);
  376.                                                 $obj->replaceIris($ids, true);
  377.                                         }
  378.  
  379.                                         if (isset($params['asn1ids'])) {
  380.                                                 $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
  381.                                                 $ids = array_map('trim',$ids);
  382.                                                 $obj->replaceAsn1Ids($ids, true);
  383.                                         }
  384.                                 }
  385.                         }
  386.  
  387.                         // Apply superior RA change
  388.                         $parent = $params['parent'];
  389.                         $ra_email = isset($params['ra_email']) ? $params['ra_email'] : '';
  390.                         if ($obj::ns() == 'oid') {
  391.                                 if ($obj->isWellKnown()) {
  392.                                         $ra_email = '';
  393.                                 }
  394.                         }
  395.                         if (!empty($ra_email) && !OIDplus::mailUtils()->validMailAddress($ra_email)) {
  396.                                 throw new OIDplusException(_L('Invalid RA email address'));
  397.                         }
  398.  
  399.                         OIDplus::logger()->log("[INFO]OID($parent)+[INFO]OID($id)+[?INFO/!OK]OIDRA($parent)?/[?INFO/!OK]A?", "Object '$id' created, ".(empty($ra_email) ? "without defined RA" : "given to RA '$ra_email'")).", superior object is '$parent'";
  400.                         if (!empty($ra_email)) {
  401.                                 OIDplus::logger()->log("[INFO]RA($ra_email)!", "Gained ownership of newly created object '$id'");
  402.                         }
  403.  
  404.                         $confidential = isset($params['confidential']) ? ($params['confidential'] == 'true') : false;
  405.                         $comment = isset($params['comment']) ? $params['comment'] : '';
  406.                         $title = '';
  407.                         $description = '';
  408.  
  409.                         if (strlen($id) > OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')) {
  410.                                 $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH');
  411.                                 throw new OIDplusException(_L('The identifier %1 is too long (max allowed length: %2)',$id,$maxlen));
  412.                         }
  413.  
  414.                         OIDplus::db()->query("INSERT INTO ###objects (id, parent, ra_email, confidential, comment, created, title, description) VALUES (?, ?, ?, ?, ?, ".OIDplus::db()->sqlDate().", ?, ?)", array($id, $parent, $ra_email, $confidential, $comment, $title, $description));
  415.                         OIDplusObject::resetObjectInformationCache();
  416.  
  417.                         // Set ASN.1 IDs und IRIs
  418.                         if ($obj::ns() == 'oid') {
  419.                                 if (!$obj->isWellKnown()) {
  420.                                         if (isset($params['iris'])) {
  421.                                                 $ids = ($params['iris'] == '') ? array() : explode(',',$params['iris']);
  422.                                                 $ids = array_map('trim',$ids);
  423.                                                 $obj->replaceIris($ids, false);
  424.                                         }
  425.  
  426.                                         if (isset($params['asn1ids'])) {
  427.                                                 $ids = ($params['asn1ids'] == '') ? array() : explode(',',$params['asn1ids']);
  428.                                                 $ids = array_map('trim',$ids);
  429.                                                 $obj->replaceAsn1Ids($ids, false);
  430.                                         }
  431.                                 }
  432.                         }
  433.  
  434.                         $status = 0;
  435.  
  436.                         if (!empty($ra_email)) {
  437.                                 // Do we need to notify that the RA does not exist?
  438.                                 $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($ra_email));
  439.                                 $invitePlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.2.92'); // OIDplusPageRaInvite
  440.                                 if (!$res->any()) $status = !is_null($invitePlugin) && OIDplus::config()->getValue('ra_invitation_enabled') ? 1 : 2;
  441.                         }
  442.  
  443.                         if ($obj::ns() == 'oid') {
  444.                                 if ($obj->isWellKnown()) {
  445.                                         $status += 4;
  446.                                 }
  447.                         }
  448.  
  449.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  450.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.3')) {
  451.                                         $plugin->afterObjectInsert($id, $params);
  452.                                 }
  453.                         }
  454.  
  455.                         return array(
  456.                                 "status" => $status,
  457.                                 "inserted_id" => $id
  458.                         );
  459.                 } else {
  460.                         throw new OIDplusException(_L('Unknown action ID'));
  461.                 }
  462.         }
  463.  
  464.         public function init($html=true) {
  465.                 OIDplus::config()->prepareConfigKey('oobe_objects_done', '"Out Of Box Experience" wizard for OIDplusPagePublicObjects done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {});
  466.                 OIDplus::config()->prepareConfigKey('oid_grid_show_weid', 'Show WEID/Base36 column in CRUD grid of OIDs?', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  467.                         if (!is_numeric($value) || ($value < 0) || ($value > 1)) {
  468.                                 throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
  469.                         }
  470.                 });
  471.         }
  472.  
  473.         private function tryObject($id, &$out) {
  474.                 $parent = null;
  475.                 $res = null;
  476.                 $row = null;
  477.                 $obj = OIDplusObject::parse($id);
  478.                 if (is_null($obj)) return false;
  479.                 if ($obj->isRoot()) {
  480.                         $obj->getContentPage($out['title'], $out['text'], $out['icon']);
  481.                         $objParent = null; // $obj->getParent();
  482.                 } else {
  483.                         $obj = OIDplusObject::findFitting($id); // this time, the object will be found, not just the object type
  484.                         if (!$obj) {
  485.                                 return false;
  486.                         } else {
  487.                                 $obj->getContentPage($out['title'], $out['text'], $out['icon']);
  488.                                 if (empty($out['title'])) $out['title'] = explode(':',$obj->nodeId(),2)[1];
  489.                                 $objParent = $obj->getParent();
  490.                         }
  491.                 }
  492.                 return array($id, $obj, $objParent);
  493.         }
  494.  
  495.         public static function getAlternativesForQuery($id) {
  496.                 // Attention: This is NOT an implementation of 1.3.6.1.4.1.37476.2.5.2.3.7 !
  497.                 //            This is the function that calls getAlternativesForQuery() of every plugin that implements 1.3.6.1.4.1.37476.2.5.2.3.7
  498.  
  499.                 // e.g. used for "Reverse Alt Id"
  500.                 $alternatives = array();
  501.                 foreach (OIDplus::getAllPlugins() as $plugin) {
  502.                         if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.7')) {
  503.                                 $tmp = $plugin->getAlternativesForQuery($id);
  504.                                 if (is_array($tmp)) {
  505.                                         $alternatives = array_merge($tmp, $alternatives);
  506.                                 }
  507.                         }
  508.                 }
  509.  
  510.                 // If something is more than one time, remove it
  511.                 $alternatives = array_unique($alternatives);
  512.  
  513.                 // If a plugin accidentally added the own ID, remove it. This function lists only alternatives, not the own ID
  514.                 $alternatives_tmp = array();
  515.                 foreach ($alternatives as $alt) {
  516.                         if ($alt !== $id) $alternatives_tmp[] = $alt;
  517.                 }
  518.                 $alternatives = $alternatives_tmp;
  519.  
  520.                 return $alternatives;
  521.         }
  522.  
  523.         public function gui($id, &$out, &$handled) {
  524.                 if ($id === 'oidplus:system') {
  525.                         $handled = true;
  526.  
  527.                         $out['title'] = OIDplus::config()->getValue('system_title');
  528.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  529.  
  530.                         if (file_exists(OIDplus::localpath() . 'userdata/welcome/welcome$'.OIDplus::getCurrentLang().'.html')) {
  531.                                 $cont = file_get_contents(OIDplus::localpath() . 'userdata/welcome/welcome$'.OIDplus::getCurrentLang().'.html');
  532.                         } else if (file_exists(OIDplus::localpath() . 'userdata/welcome/welcome.html')) {
  533.                                 $cont = file_get_contents(OIDplus::localpath() . 'userdata/welcome/welcome.html');
  534.                         } else if (file_exists(__DIR__ . '/welcome$'.OIDplus::getCurrentLang().'.html')) {
  535.                                 $cont = file_get_contents(__DIR__ . '/welcome$'.OIDplus::getCurrentLang().'.html');
  536.                         } else if (file_exists(__DIR__ . '/welcome.html')) {
  537.                                 $cont = file_get_contents(__DIR__ . '/welcome.html');
  538.                         } else {
  539.                                 $cont = '';
  540.                         }
  541.  
  542.                         list($html, $js, $css) = extractHtmlContents($cont);
  543.                         $cont = '';
  544.                         if (!empty($js))  $cont .= "<script>\n$js\n</script>";
  545.                         if (!empty($css)) $cont .= "<style>\n$css\n</style>";
  546.                         $cont .= stripHtmlComments($html);
  547.  
  548.                         $out['text'] = $cont;
  549.  
  550.                         if (strpos($out['text'], '%%OBJECT_TYPE_LIST%%') !== false) {
  551.                                 $tmp = '<ul>';
  552.                                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  553.                                         $tmp .= '<li><a '.OIDplus::gui()->link($ot::root()).'>'.htmlentities($ot::objectTypeTitle()).'</a></li>';
  554.                                 }
  555.                                 $tmp .= '</ul>';
  556.                                 $out['text'] = str_replace('%%OBJECT_TYPE_LIST%%', $tmp, $out['text']);
  557.                         }
  558.  
  559.                         return;
  560.                 }
  561.  
  562.                 // Never answer to an object type that is called 'oidplus:',
  563.                 // otherwise, an object type plugin could break the whole system!
  564.                 else if ((strpos($id,':') !== false) && (!str_starts_with($id,'oidplus:'))) {
  565.  
  566.                         // --- Try to find the object or an alternative
  567.  
  568.                         $test = $this->tryObject($id, $out);
  569.                         if ($test === false) {
  570.                                 // try to find an alternative
  571.                                 $alternatives = $this->getAlternativesForQuery($id);
  572.                                 foreach ($alternatives as $alternative) {
  573.                                         $test = $this->tryObject($alternative, $out);
  574.                                         if ($test !== false) break; // found something
  575.                                 }
  576.                         }
  577.                         if ($test !== false) {
  578.                                 list($id, $obj, $objParent) = $test;
  579.                         }
  580.  
  581.                         // --- If the object type is disabled or not an object at all (e.g. "oidplus:"), then $handled=false
  582.                         //     If the object type is enabled but object not found, $handled=true
  583.  
  584.                         $obj = OIDplusObject::parse($id);
  585.  
  586.                         if ($test === false) {
  587.                                 if (is_null($obj)) {
  588.                                         // Object type disabled or not known (e.g. ObjectType "oidplus:").
  589.                                         $handled = false;
  590.                                         return;
  591.                                 } else {
  592.                                         // Object type enabled but identifier not in database
  593.                                         $handled = true;
  594.                                         if (isset($_SERVER['SCRIPT_FILENAME']) && (strtolower(basename($_SERVER['SCRIPT_FILENAME'])) !== 'ajax.php')) { // don't send HTTP error codes in ajax.php, because we want a page and not a JavaScript alert box, when someone enters an invalid OID in the GoTo-Box
  595.                                                 http_response_code(404);
  596.                                         }
  597.                                         $out['title'] = _L('Object not found');
  598.                                         $out['icon'] = 'img/error.png';
  599.                                         $out['text'] = _L('The object %1 was not found in this database.','<code>'.htmlentities($id).'</code>');
  600.                                         return;
  601.                                 }
  602.                         } else {
  603.                                 $handled = true;
  604.                         }
  605.  
  606.                         unset($test);
  607.  
  608.                         // --- If found, do we have read rights?
  609.  
  610.                         if (!$obj->userHasReadRights()) {
  611.                                 if (isset($_SERVER['SCRIPT_FILENAME']) && (strtolower(basename($_SERVER['SCRIPT_FILENAME'])) !== 'ajax.php')) { // don't send HTTP error codes in ajax.php, because we want a page and not a JavaScript alert box, when someone enters an invalid OID in the GoTo-Box
  612.                                         http_response_code(403);
  613.                                 }
  614.                                 $out['title'] = _L('Access denied');
  615.                                 $out['icon'] = 'img/error.png';
  616.                                 $out['text'] = '<p>'._L('Please <a %1>log in</a> to receive information about this object.',OIDplus::gui()->link('oidplus:login')).'</p>';
  617.                                 return;
  618.                         }
  619.  
  620.                         // ---
  621.  
  622.                         if ($objParent) {
  623.                                 if ($objParent->isRoot()) {
  624.                                         $parent_link_text = $objParent->objectTypeTitle();
  625.                                         $out['text'] = '<p><a '.OIDplus::gui()->link($objParent->root()).'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Parent node: %1',htmlentities($parent_link_text)).'</a></p>' . $out['text'];
  626.                                 } else {
  627.                                         $parent_title = $objParent->getTitle();
  628.                                         if (empty($parent_title) && ($objParent->ns() == 'oid')) {
  629.                                                 // If not title is available, then use an ASN.1 identifier
  630.                                                 $res_asn = OIDplus::db()->query("select name from ###asn1id where oid = ?", array($objParent->nodeId()));
  631.                                                 if ($res_asn->any()) {
  632.                                                         $row_asn = $res_asn->fetch_array();
  633.                                                         $parent_title = $row_asn['name']; // TODO: multiple ASN1 ids?
  634.                                                 }
  635.                                         }
  636.  
  637.                                         $parent_link_text = empty($parent_title) ? explode(':',$objParent->nodeId())[1] : $parent_title.' ('.explode(':',$objParent->nodeId())[1].')';
  638.  
  639.                                         $out['text'] = '<p><a '.OIDplus::gui()->link($objParent->nodeId()).'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Parent node: %1',htmlentities($parent_link_text)).'</a></p>' . $out['text'];
  640.                                 }
  641.                         } else {
  642.                                 $parent_link_text = _L('Go back to front page');
  643.                                 $out['text'] = '<p><a '.OIDplus::gui()->link('oidplus:system').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
  644.                         }
  645.  
  646.                         // ---
  647.  
  648.                         if ($obj) {
  649.                                 $title = $obj->getTitle();
  650.                                 $description = $obj->getDescription();
  651.                                 if (empty(strip_tags($description)) && (stripos($description,'<img') === false)) {
  652.                                         if (empty($title)) {
  653.                                                 $desc = '<p><i>'._L('No description for this object available').'</i></p>';
  654.                                         } else {
  655.                                                 $desc = $title;
  656.                                         }
  657.                                 } else {
  658.                                         $desc = self::objDescription($description);
  659.                                 }
  660.  
  661.                                 if ($obj->userHasWriteRights()) {
  662.                                         $rand = ++self::$crudCounter;
  663.                                         $desc = '<noscript><p><b>'._L('You need to enable JavaScript to edit title or description of this object.').'</b></p>'.$desc.'</noscript>';
  664.                                         $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
  665.                                         $desc .= _L('Title').': <input type="text" name="title" id="titleedit" value="'.htmlentities($title).'"><br><br>'._L('Description').':<br>';
  666.                                         $desc .= self::showMCE('description', $description);
  667.                                         $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="OIDplusPagePublicObjects.updateDesc()">'._L('Update description').'</button>';
  668.                                         $desc .= '</div>';
  669.                                         $desc .= '<script>$("#descbox_'.$rand.'")[0].style.display = "block";</script>';
  670.                                 }
  671.                         } else {
  672.                                 $desc = '';
  673.                         }
  674.  
  675.                         // ---
  676.  
  677.                         if (strpos($out['text'], '%%DESC%%') !== false)
  678.                                 $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
  679.                         if (strpos($out['text'], '%%CRUD%%') !== false)
  680.                                 $out['text'] = str_replace('%%CRUD%%',    self::showCrud($obj->nodeId()),     $out['text']);
  681.                         if (strpos($out['text'], '%%RA_INFO%%') !== false)
  682.                                 $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($obj->getRaMail()), $out['text']);
  683.  
  684.                         $alt_ids = $obj->getAltIds();
  685.                         if (count($alt_ids) > 0) {
  686.                                 $out['text'] .= '<h2>'._L('Alternative Identifiers').'</h2>';
  687.                                 foreach ($alt_ids as $alt_id) {
  688.                                         $ns = $alt_id->getNamespace();
  689.                                         $aid = $alt_id->getId();
  690.                                         $aiddesc = $alt_id->getDescription();
  691.                                         $suffix = $alt_id->getSuffix();
  692.                                         $out['text'] .= "$aiddesc: <code>$ns:$aid</code>$suffix<br>";
  693.                                 }
  694.                         }
  695.  
  696.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  697.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.2')) {
  698.                                         $plugin->modifyContent($obj->nodeId(), $out['title'], $out['icon'], $out['text']);
  699.                                 }
  700.                         }
  701.                 }
  702.         }
  703.  
  704.         private function publicSitemap_rec($json, &$out) {
  705.                 foreach ($json as $x) {
  706.                         if (isset($x['id']) && $x['id']) {
  707.                                 $out[] = $x['id'];
  708.                         }
  709.                         if (isset($x['children'])) {
  710.                                 $this->publicSitemap_rec($x['children'], $out);
  711.                         }
  712.                 }
  713.         }
  714.  
  715.         public function publicSitemap(&$out) {
  716.                 $json = array();
  717.                 $this->tree($json, null/*RA EMail*/, false/*HTML tree algorithm*/, true/*display all*/);
  718.                 $this->publicSitemap_rec($json, $out);
  719.         }
  720.  
  721.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  722.                 if ($nonjs) {
  723.                         $json[] = array(
  724.                                 'id' => 'oidplus:system',
  725.                                 'icon' => OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png',
  726.                                 'text' => _L('System')
  727.                         );
  728.  
  729.                         $objGoto = OIDplusObject::findFitting($req_goto);
  730.                         $objGotoParent = $objGoto ? $objGoto->getParent() : null;
  731.                         $parent = $objGotoParent ? $objGotoParent->nodeId() : '';
  732.  
  733.                         $objTypesChildren = array();
  734.                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  735.                                 $icon = $this->get_treeicon_root($ot);
  736.  
  737.                                 $json[] = array(
  738.                                         'id' => $ot::root(),
  739.                                         'icon' => $icon,
  740.                                         'text' => $ot::objectTypeTitle()
  741.                                 );
  742.  
  743.                                 $tmp = OIDplusObject::parse($req_goto);
  744.                                 if (!is_null($tmp) && ($ot == get_class($tmp))) {
  745.                                         // TODO: Instead of just having 3 levels (parent, this and children), it would be better if we'd had a full tree of all parents
  746.                                         //       on the other hand, for giving search engines content, this is good enough
  747.                                         if (empty($parent)) {
  748.                                                 $res = OIDplus::db()->query("select * from ###objects where " .
  749.                                                                             "parent = ? or " .
  750.                                                                             "id = ? " .
  751.                                                                             "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto));
  752.                                         } else {
  753.                                                 $res = OIDplus::db()->query("select * from ###objects where " .
  754.                                                                             "parent = ? or " .
  755.                                                                             "id = ? or " .
  756.                                                                             "id = ? ".
  757.                                                                             "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto, $parent));
  758.                                         }
  759.  
  760.                                         $z_used = 0;
  761.                                         $y_used = 0;
  762.                                         $x_used = 0;
  763.                                         $stufe = 0;
  764.                                         $menu_entries = array();
  765.                                         $stufen = array();
  766.                                         while ($row = $res->fetch_object()) {
  767.                                                 $obj = OIDplusObject::parse($row->id);
  768.                                                 if (is_null($obj)) continue; // might happen if the objectType is not available/loaded
  769.                                                 if (!$obj->userHasReadRights()) continue;
  770.                                                 $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title);
  771.  
  772.                                                 if ($row->id == $parent) { $stufe=0; $z_used++; }
  773.                                                 if ($row->id == $req_goto) { $stufe=1; $y_used++; }
  774.                                                 if ($row->parent == $req_goto) { $stufe=2; $x_used++; }
  775.  
  776.                                                 $menu_entry = array('id' => $row->id, 'icon' => '', 'text' => $txt, 'indent' => 0);
  777.                                                 $menu_entries[] = $menu_entry;
  778.                                                 $stufen[] = $stufe;
  779.                                         }
  780.                                         if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry['indent'] += 1;
  781.                                         if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry['indent'] += 1;
  782.                                         if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry['indent'] += 1;
  783.                                         $json = array_merge($json, $menu_entries);
  784.                                 }
  785.                         }
  786.  
  787.                         return true;
  788.                 } else {
  789.                         if ($req_goto === true) {
  790.                                 $goto_path = true; // display everything recursively
  791.                         } else if (isset($req_goto)) {
  792.                                 $goto = $req_goto;
  793.                                 $path = array();
  794.                                 while (true) {
  795.                                         $path[] = $goto;
  796.                                         $objGoto = OIDplusObject::findFitting($goto);
  797.                                         if (!$objGoto) break;
  798.                                         $objGotoParent = $objGoto->getParent();
  799.                                         $goto = $objGotoParent ? $objGotoParent->nodeId() : '';
  800.                                         if ($goto == '') continue;
  801.                                 }
  802.  
  803.                                 $goto_path = array_reverse($path);
  804.                         } else {
  805.                                 $goto_path = null;
  806.                         }
  807.  
  808.                         $objTypesChildren = array();
  809.                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  810.                                 $icon = $this->get_treeicon_root($ot);
  811.  
  812.                                 $child = array('id' => $ot::root(),
  813.                                                'text' => $ot::objectTypeTitle(),
  814.                                                'state' => array("opened" => true),
  815.                                                'icon' => $icon,
  816.                                                'children' => OIDplus::menuUtils()->tree_populate($ot::root(), $goto_path)
  817.                                                );
  818.                                 if (!file_exists($child['icon'])) $child['icon'] = null; // default icon (folder)
  819.                                 $objTypesChildren[] = $child;
  820.                         }
  821.  
  822.                         $json[] = array(
  823.                                 'id' => "oidplus:system",
  824.                                 'text' => _L('Objects'),
  825.                                 'state' => array(
  826.                                         "opened" => true,
  827.                                         // "selected" => true)  // "selected" is buggy:
  828.                                         // 1) The select-event will not be triggered upon loading
  829.                                         // 2) The nodes directly blow cannot be opened (loading infinite time)
  830.                                 ),
  831.                                 'icon' => OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png',
  832.                                 'children' => $objTypesChildren
  833.                         );
  834.  
  835.                         return true;
  836.                 }
  837.         }
  838.  
  839.         public function tree_search($request) {
  840.                 $ary = array();
  841.                 $found_leaf = false;
  842.                 if ($obj = OIDplusObject::parse($request)) {
  843.                         $found_leaf = OIDplusObject::exists($request);
  844.                         do {
  845.                                 if ($obj->userHasReadRights()) {
  846.                                         $ary[] = $obj->nodeId();
  847.                                 }
  848.                         } while ($obj = $obj->getParent());
  849.                         $ary = array_reverse($ary);
  850.                 }
  851.                 if (!$found_leaf) {
  852.                         $alternatives = $this->getAlternativesForQuery($request);
  853.                         foreach ($alternatives as $alternative) {
  854.                                 $ary_ = array();
  855.                                 if ($obj = OIDplusObject::parse($alternative)) {
  856.                                         if ($obj->userHasReadRights() && OIDplusObject::exists($alternative)) {
  857.                                                 do {
  858.                                                         $ary_[] = $obj->nodeId();
  859.                                                 } while ($obj = $obj->getParent());
  860.                                                 $ary_ = array_reverse($ary_);
  861.                                         }
  862.                                 }
  863.                                 if (!empty($ary_)) {
  864.                                         $ary = $ary_;
  865.                                         break;
  866.                                 }
  867.                         }
  868.                 }
  869.                 return $ary;
  870.         }
  871.  
  872.         private static $crudCounter = 0;
  873.  
  874.         protected static function showCrud($parent='oid:') {
  875.                 $items_total = 0;
  876.                 $items_hidden = 0;
  877.  
  878.                 $objParent = OIDplusObject::parse($parent);
  879.                 $parentNS = $objParent::ns();
  880.  
  881.                 // http://www.oid-info.com/cgi-bin/display?a=list-by-category&category=Not%20allocating%20identifiers
  882.                 $no_asn1 = array(
  883.                         'oid:1.3.6.1.4.1',
  884.                         'oid:1.3.6.1.4.1.37476.9000',
  885.                         'oid:1.3.6.1.4.1.37553.8.8',
  886.                         'oid:2.16.276.1',
  887.                         //'oid:2.25', // according to Olivier, it is OK that UUID owners define their own ASN.1 ID, since the ASN.1 ID is not required to be unique
  888.                         //'oid:1.2.840.113556.1.8000.2554' // Adhoc (GUID/UUID-based) customer use. It is probably the same case as the UUID OIDs, after all, these are UUIDs, too.
  889.                 );
  890.  
  891.                 // http://www.oid-info.com/cgi-bin/display?a=list-by-category&category=Not%20allocating%20Unicode%20labels
  892.                 $no_iri = array(
  893.                         'oid:1.2.250.1',
  894.                         'oid:1.3.6.1.4.1',
  895.                         'oid:1.3.6.1.4.1.37476.9000',
  896.                         'oid:1.3.6.1.4.1.37553.8.8',
  897.                         'oid:2.16.276.1',
  898.                         'oid:2.25'
  899.                 );
  900.  
  901.                 $accepts_asn1 = ($parentNS == 'oid') && (!in_array($objParent->nodeId(), $no_asn1)) && (!is_uuid_oid($objParent->nodeId(),true));
  902.                 $accepts_iri  = ($parentNS == 'oid') && (!in_array($objParent->nodeId(), $no_iri)) && (!is_uuid_oid($objParent->nodeId(),true));
  903.  
  904.                 $result = OIDplus::db()->query("select o.*, r.ra_name " .
  905.                                                "from ###objects o " .
  906.                                                "left join ###ra r on r.email = o.ra_email " .
  907.                                                "where parent = ? " .
  908.                                                "order by ".OIDplus::db()->natOrder('id'), array($parent));
  909.  
  910.                 $rows = array();
  911.                 while ($row = $result->fetch_object()) {
  912.                         $obj = OIDplusObject::parse($row->id);
  913.                         $rows[] = array($obj,$row);
  914.                 }
  915.  
  916.                 $enable_weid_presentation = OIDplus::config()->getValue('oid_grid_show_weid');
  917.  
  918.                 $output = '';
  919.                 $output .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  920.                 $output .= '<table class="table table-bordered table-striped">';
  921.                 $output .= '    <tr>';
  922.                 $output .= '         <th>'._L('ID').(($parentNS == 'gs1') ? ' '._L('(without check digit)') : '').'</th>';
  923.                 if ($enable_weid_presentation && ($parentNS == 'oid') && !$objParent->isRoot()) {
  924.                         $output .= '         <th><abbr title="'._L('Binary-to-text encoding used for WEIDs').'">'._L('Base36').'</abbr></th>';
  925.                 }
  926.                 if ($parentNS == 'oid') {
  927.                         if ($accepts_asn1) $output .= '      <th>'._L('ASN.1 IDs (comma sep.)').'</th>';
  928.                         if ($accepts_iri)  $output .= '      <th>'._L('IRI IDs (comma sep.)').'</th>';
  929.                 }
  930.                 $output .= '         <th>'._L('RA').'</th>';
  931.                 $output .= '         <th>'._L('Comment').'</th>';
  932.                 if ($objParent->userHasWriteRights()) {
  933.                         $output .= '         <th>'._L('Hide').'</th>';
  934.                         $output .= '         <th>'._L('Update').'</th>';
  935.                         $output .= '         <th>'._L('Delete').'</th>';
  936.                 }
  937.                 $output .= '         <th>'._L('Created').'</th>';
  938.                 $output .= '         <th>'._L('Updated').'</th>';
  939.                 $output .= '    </tr>';
  940.  
  941.                 foreach ($rows as list($obj,$row)) {
  942.                         $items_total++;
  943.                         if (!$obj->userHasReadRights()) {
  944.                                 $items_hidden++;
  945.                                 continue;
  946.                         }
  947.  
  948.                         $show_id = $obj->crudShowId($objParent);
  949.  
  950.                         $asn1ids = array();
  951.                         $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ? order by lfd", array($row->id));
  952.                         while ($row2 = $res2->fetch_array()) {
  953.                                 $asn1ids[] = $row2['name'];
  954.                         }
  955.  
  956.                         $iris = array();
  957.                         $res2 = OIDplus::db()->query("select name from ###iri where oid = ? order by lfd", array($row->id));
  958.                         while ($row2 = $res2->fetch_array()) {
  959.                                 $iris[] = $row2['name'];
  960.                         }
  961.  
  962.                         $date_created = explode(' ', $row->created)[0] == '0000-00-00' ? '' : explode(' ', $row->created)[0];
  963.                         $date_updated = explode(' ', $row->updated)[0] == '0000-00-00' ? '' : explode(' ', $row->updated)[0];
  964.  
  965.                         $output .= '<tr>';
  966.                         $output .= '     <td><a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.htmlentities($show_id).'</a>';
  967.                         if ($enable_weid_presentation && ($parentNS == 'oid') && $objParent->isRoot()) {
  968.                                 // To save space horizontal space, the WEIDs were written below the OIDs
  969.                                 $output .= '<br>'.$obj->getWeidNotation(true);
  970.                         }
  971.                         $output .= '</td>';
  972.                         if ($enable_weid_presentation && ($parentNS == 'oid') && !$objParent->isRoot()) {
  973.                                 $output .= '    <td>'.htmlentities($obj->weidArc()).'</td>';
  974.                         }
  975.                         if ($objParent->userHasWriteRights()) {
  976.                                 if ($parentNS == 'oid') {
  977.                                         if ($accepts_asn1) $output .= '     <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>';
  978.                                         if ($accepts_iri)  $output .= '     <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>';
  979.                                 }
  980.                                 $output .= '     <td><input type="text" id="ra_email_'.$row->id.'" value="'.htmlentities($row->ra_email).'"></td>';
  981.                                 $output .= '     <td><input type="text" id="comment_'.$row->id.'" value="'.htmlentities($row->comment).'"></td>';
  982.                                 $output .= '     <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>';
  983.                                 $output .= '     <td><button type="button" name="update_'.$row->id.'" id="update_'.$row->id.'" class="btn btn-success btn-xs update" onclick="OIDplusPagePublicObjects.crudActionUpdate('.js_escape($row->id).', '.js_escape($parent).')">'._L('Update').'</button></td>';
  984.                                 $output .= '     <td><button type="button" name="delete_'.$row->id.'" id="delete_'.$row->id.'" class="btn btn-danger btn-xs delete" onclick="OIDplusPagePublicObjects.crudActionDelete('.js_escape($row->id).', '.js_escape($parent).')">'._L('Delete').'</button></td>';
  985.                                 $output .= '     <td>'.$date_created.'</td>';
  986.                                 $output .= '     <td>'.$date_updated.'</td>';
  987.                         } else {
  988.                                 if ($parentNS == 'oid') {
  989.                                         if ($asn1ids == '') $asn1ids = '<i>'._L('(none)').'</i>';
  990.                                         if ($iris == '') $iris = '<i>'._L('(none)').'</i>';
  991.                                         $asn1ids_ext = array();
  992.                                         foreach ($asn1ids as $asn1id) {
  993.                                                 $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>';
  994.                                         }
  995.                                         if ($accepts_asn1) $output .= '     <td>'.implode(', ', $asn1ids_ext).'</td>';
  996.                                         if ($accepts_iri)  $output .= '     <td>'.implode(', ', $iris).'</td>';
  997.                                 }
  998.                                 $output .= '     <td><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$row->ra_email)).'>'.htmlentities(empty($row->ra_name) ? str_replace('@','&',$row->ra_email) : $row->ra_name).'</a></td>';
  999.                                 $output .= '     <td>'.htmlentities($row->comment).'</td>';
  1000.                                 $output .= '     <td>'.$date_created.'</td>';
  1001.                                 $output .= '     <td>'.$date_updated.'</td>';
  1002.                         }
  1003.                         $output .= '</tr>';
  1004.                 }
  1005.  
  1006.                 $parent_ra_email = $objParent ? $objParent->getRaMail() : '';
  1007.  
  1008.                 // "Create OID" row
  1009.                 if ($objParent->userHasWriteRights()) {
  1010.                         $output .= '<tr>';
  1011.                         $prefix = is_null($objParent) ? '' : $objParent->crudInsertPrefix();
  1012.  
  1013.                         $suffix = is_null($objParent) ? '' : $objParent->crudInsertSuffix();
  1014.                         foreach (OIDplus::getObjectTypePlugins() as $plugin) {
  1015.                                 if (($plugin::getObjectTypeClassName()::ns() == $parentNS) && $plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.6')) {
  1016.                                         $suffix .= $plugin->gridGeneratorLinks($objParent);
  1017.                                 }
  1018.                         }
  1019.  
  1020.                         if ($parentNS == 'guid') {
  1021.                                 $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:275px">'.$suffix.'</td>';
  1022.                         } else if ($parentNS == 'oid') {
  1023.                                 // TODO: Idea: Give a class name, e.g. "OID" and then with a oid-specific CSS make the width individual. So, every plugin has more control over the appearance and widths of the input fields
  1024.                                 if ($objParent->nodeId() === 'oid:2.25') {
  1025.                                         $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:345px">'.$suffix.'</td>';
  1026.                                         if ($enable_weid_presentation) $output .= '     <td>&nbsp;</td>'; // For UUID-OIDs, you must generate a valid one. Don't be tempted to create one using the Base36 input!
  1027.                                 } else if ($objParent->isRoot()) {
  1028.                                         $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:345px">'.$suffix.'</td>';
  1029.                                         if ($enable_weid_presentation) $output .= ''; // WEID-editor not available for root nodes at the moment. For the moment you need to enter the OID (TODO: Create JavaScript WEID encoder/decoder)
  1030.                                 } else {
  1031.                                         if ($enable_weid_presentation) {
  1032.                                                 $output .= '     <td>'.$prefix.' <input oninput="OIDplusPagePublicObjects.frdl_oidid_change()" type="text" id="id" value="" style="width:100%;min-width:100px">'.$suffix.'</td>';
  1033.                                                 $output .= '     <td><input type="text" name="weid" id="weid" value="" oninput="OIDplusPagePublicObjects.frdl_weid_change()" style="width:100%;min-width:100px"></td>';
  1034.                                         } else {
  1035.                                                 $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:100px">'.$suffix.'</td>';
  1036.                                         }
  1037.                                 }
  1038.                         } else {
  1039.                                 $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:100px">'.$suffix.'</td>';
  1040.                         }
  1041.                         if ($accepts_asn1) $output .= '     <td><input type="text" id="asn1ids" value=""></td>';
  1042.                         if ($accepts_iri)  $output .= '     <td><input type="text" id="iris" value=""></td>';
  1043.                         $output .= '     <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>';
  1044.                         $output .= '     <td><input type="text" id="comment" value=""></td>';
  1045.                         $output .= '     <td><input type="checkbox" id="hide"></td>';
  1046.                         $output .= '     <td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs update" onclick="OIDplusPagePublicObjects.crudActionInsert('.js_escape($parent).')">'._L('Insert').'</button></td>';
  1047.                         $output .= '     <td></td>';
  1048.                         $output .= '     <td></td>';
  1049.                         $output .= '     <td></td>';
  1050.                         $output .= '</tr>';
  1051.                 } else {
  1052.                         if ($items_total-$items_hidden == 0) {
  1053.                                 $cols = ($parentNS == 'oid') ? 7 : 5;
  1054.                                 if ($enable_weid_presentation && ($parentNS == 'oid') && !$objParent->isRoot()) {
  1055.                                         $cols++;
  1056.                                 }
  1057.                                 $output .= '<tr><td colspan="'.$cols.'">'._L('No items available').'</td></tr>';
  1058.                         }
  1059.                 }
  1060.  
  1061.                 $output .= '</table>';
  1062.                 $output .= '</div></div>';
  1063.  
  1064.                 if ($items_hidden == 1) {
  1065.                         $output .= '<p>'._L('One item is hidden. Please <a %1>log in</a> to see it.',$items_hidden,OIDplus::gui()->link('oidplus:login')).'</p>';
  1066.                 } else if ($items_hidden > 1) {
  1067.                         $output .= '<p>'._L('%1 items are hidden. Please <a %2>log in</a> to see them.',$items_hidden,OIDplus::gui()->link('oidplus:login')).'</p>';
  1068.                 }
  1069.  
  1070.                 return $output;
  1071.         }
  1072.  
  1073.         protected static function objDescription($html) {
  1074.                 // We allow HTML, but no hacking
  1075.                 $html = anti_xss($html);
  1076.  
  1077.                 return trim_br($html);
  1078.         }
  1079.  
  1080.         // 'quickbars' added 11 July 2019: Disabled because of two problems:
  1081.         //                                 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus
  1082.         //                                 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit
  1083.         // 'colorpicker', 'textcolor' and 'contextmenu' added in 07 April 2020, because it is built in in the core.
  1084.         // 'importcss' added 17 September 2020, because it breaks the "Format/Style" dropdown box ("styleselect" toolbar)
  1085.         // 'legacyoutput' added 24 September 2021, because it is declared as deprecated
  1086.         // 'spellchecker' added 6 October 2021, because it is declared as deprecated and marked for removal in TinyMCE 6.0
  1087.         // 'imagetools' and 'toc' added 23 February 2022, because they are declared as deprecated and marked for removal in TinyMCE 6.0 ("moving to premium")
  1088.         public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars', 'colorpicker', 'textcolor', 'contextmenu', 'importcss', 'legacyoutput', 'spellchecker', 'imagetools', 'toc');
  1089.  
  1090.         protected static function showMCE($name, $content) {
  1091.                 $mce_plugins = array();
  1092.                 foreach (glob(OIDplus::localpath().'vendor/tinymce/tinymce/plugins/*') as $m) { // */
  1093.                         $mce_plugins[] = basename($m);
  1094.                 }
  1095.  
  1096.                 foreach (self::$exclude_tinymce_plugins as $exclude) {
  1097.                         $index = array_search($exclude, $mce_plugins);
  1098.                         if ($index !== false) unset($mce_plugins[$index]);
  1099.                 }
  1100.  
  1101.                 $oidplusLang = OIDplus::getCurrentLang();
  1102.  
  1103.                 $langCandidates = array(
  1104.                         strtolower(substr($oidplusLang,0,2)).'_'.strtoupper(substr($oidplusLang,2,2)), // de_DE
  1105.                         strtolower(substr($oidplusLang,0,2)) // de
  1106.                 );
  1107.                 $tinyMCELang = '';
  1108.                 foreach ($langCandidates as $candidate) {
  1109.                         if (file_exists(OIDplus::localpath().'vendor/tweeb/tinymce-i18n/langs/'.$candidate.'.js')) {
  1110.                                 $tinyMCELang = $candidate;
  1111.                                 break;
  1112.                         }
  1113.                 }
  1114.  
  1115.                 $out = '<script>
  1116.                                 tinymce.EditorManager.baseURL = "vendor/tinymce/tinymce";
  1117.                                 tinymce.init({
  1118.                                         document_base_url: "'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'",
  1119.                                         selector: "#'.$name.'",
  1120.                                         height: 200,
  1121.                                         statusbar: false,
  1122. //                                      menubar:false,
  1123. //                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
  1124.                                         toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
  1125.                                         plugins: "'.implode(' ', $mce_plugins).'",
  1126.                                         mobile: {
  1127.                                                 theme: "mobile",
  1128.                                                 toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
  1129.                                                 plugins: "'.implode(' ', $mce_plugins).'"
  1130.                                         }
  1131.                                         '.($tinyMCELang == '' ? '' : ', language : "'.$tinyMCELang.'"').'
  1132.                                         '.($tinyMCELang == '' ? '' : ', language_url : "'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'vendor/tweeb/tinymce-i18n/langs/'.$tinyMCELang.'.js"').'
  1133.                                 });
  1134.  
  1135.                                 pageChangeRequestCallbacks.push([OIDplusPagePublicObjects.cbQueryTinyMCE, "#'.$name.'"]);
  1136.                                 pageChangeCallbacks.push([OIDplusPagePublicObjects.cbRemoveTinyMCE, "#'.$name.'"]);
  1137.                         </script>';
  1138.  
  1139.                 $content = htmlentities($content); // For some reason, if we want to display the text "<xyz>" in TinyMCE, we need to double-encode things! &lt; will not be accepted, we need &amp;lt; ... why?
  1140.  
  1141.                 $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
  1142.  
  1143.                 return $out;
  1144.         }
  1145.  
  1146.         public function implementsFeature($id) {
  1147.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry, oobeRequested()
  1148.                 // Important: Do NOT 1.3.6.1.4.1.37476.2.5.2.3.7 because our getAlternativesForQuery() is the one that calls others!
  1149.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
  1150.                 return false;
  1151.         }
  1152.  
  1153.         public function oobeRequested(): bool {
  1154.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
  1155.  
  1156.                 return OIDplus::config()->getValue('oobe_objects_done') == '0';
  1157.         }
  1158.  
  1159.         public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
  1160.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
  1161.  
  1162.                 echo '<p><u>'._L('Step %1: Enable/Disable object type plugins',$step).'</u></p>';
  1163.                 echo '<p>'._L('Which object types do you want to manage using OIDplus?').'</p>';
  1164.  
  1165.                 $enabled_ary = array();
  1166.  
  1167.                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  1168.                         echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
  1169.                         if (isset($_POST['sent'])) {
  1170.                                 if (isset($_POST['enable_ot_'.$ot::ns()])) {
  1171.                                         echo ' checked';
  1172.                                         $enabled_ary[] = $ot::ns();
  1173.                                 }
  1174.                         } else {
  1175.                                 echo ' checked';
  1176.                         }
  1177.                         echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
  1178.                 }
  1179.  
  1180.                 foreach (OIDplus::getDisabledObjectTypes() as $ot) {
  1181.                         echo '<input type="checkbox" name="enable_ot_'.$ot::ns().'" id="enable_ot_'.$ot::ns().'"';
  1182.                         if (isset($_POST['sent'])) {
  1183.                                 if (isset($_POST['enable_ot_'.$ot::ns()])) {
  1184.                                         echo ' checked';
  1185.                                         $enabled_ary[] = $ot::ns();
  1186.                                 }
  1187.                         } else {
  1188.                                 echo ''; // <-- difference
  1189.                         }
  1190.                         echo '> <label for="enable_ot_'.$ot::ns().'">'.htmlentities($ot::objectTypeTitle()).'</label><br>';
  1191.                 }
  1192.  
  1193.                 $msg = '';
  1194.                 if ($do_edits) {
  1195.                         try {
  1196.                                 OIDplus::config()->setValue('objecttypes_enabled', implode(';', $enabled_ary));
  1197.                                 OIDplus::config()->setValue('oobe_objects_done', '1');
  1198.                         } catch (Exception $e) {
  1199.                                 $msg = $e->getMessage();
  1200.                                 $errors_happened = true;
  1201.                         }
  1202.                 }
  1203.  
  1204.                 echo ' <font color="red"><b>'.$msg.'</b></font>';
  1205.         }
  1206.  
  1207.         public function getNotifications($user=null): array {
  1208.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.8
  1209.                 $notifications = array();
  1210.                 $res = OIDplus::db()->query("select id, title from ###objects order by ".OIDplus::db()->natOrder('id'));
  1211.                 if ($res->any()) {
  1212.                         $is_admin_logged_in = OIDplus::authUtils()->isAdminLoggedIn(); // run just once, for performance
  1213.                         while ($row = $res->fetch_array()) {
  1214.                                 if (empty($row['title'])) {
  1215.                                         if ($user === 'admin') {
  1216.                                                 $accept = $is_admin_logged_in;
  1217.                                         } else {
  1218.                                                 $accept = false;
  1219.                                                 if ($obj = OIDplusObject::parse($row['id'])) {
  1220.                                                         if ($obj->userHasWriteRights($user)) {
  1221.                                                                 $accept = true;
  1222.                                                         }
  1223.                                                 }
  1224.                                         }
  1225.  
  1226.                                         if ($accept) {
  1227.                                                 $notifications[] = array('WARN', _L('Object %1 has no title.', '<a '.OIDplus::gui()->link($row['id']).'>'.$row['id'].'</a>'));
  1228.                                         }
  1229.                                 }
  1230.                         }
  1231.                 }
  1232.                 return $notifications;
  1233.         }
  1234.  
  1235. }
  1236.