Subversion Repositories oidplus

Rev

Rev 116 | Rev 150 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. require_once __DIR__ . '/includes/oidplus.inc.php';
  21.  
  22. OIDplus::init(false);
  23.  
  24. OIDplus::db()->set_charset("UTF8");
  25. OIDplus::db()->query("SET NAMES 'utf8'");
  26.  
  27. header('Content-Type:application/json; charset=utf-8');
  28.  
  29. try {
  30.         $handled = false;
  31.  
  32.         // Action:     (actions defined by plugins)
  33.         // Method:     GET / POST
  34.         // Parameters: ...
  35.         // Outputs:    ...
  36.         foreach (OIDplus::getPagePlugins('*') as $plugin) {
  37.                 $plugin->action($handled);
  38.         }
  39.  
  40.         // Action:     get_description
  41.         // Method:     GET / POST
  42.         // Parameters: id
  43.         // Outputs:    JSON
  44.         if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'get_description')) {
  45.                 $handled = true;
  46.                 if (!isset($_REQUEST['id'])) throw new Exception("Invalid args");
  47.                 try {
  48.                         $out = OIDplus::gui()::generateContentPage($_REQUEST['id']);
  49.                 } catch(Exception $e) {
  50.                         $out = array();
  51.                         $out['title'] = 'Error';
  52.                         $out['icon'] = 'img/error_big.png';
  53.                         $out['text'] = $e->getMessage();
  54.                 }
  55.                 echo json_encode($out);
  56.         }
  57.  
  58.         // === jsTree ===
  59.  
  60.         // Action:     tree_search
  61.         // Method:     GET / POST
  62.         // Parameters: search
  63.         // Outputs:    JSON
  64.         if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'tree_search')) {
  65.                 $handled = true;
  66.                 if (!isset($_REQUEST['search'])) throw new Exception("Invalid args");
  67.  
  68.                 $found = false;
  69.                 foreach (OIDplus::getPagePlugins('*') as $plugin) {
  70.                         $res = $plugin->tree_search($_REQUEST['search']);
  71.                         if ($res) {
  72.                                 echo json_encode($res);
  73.                                 $found = true;
  74.                                 break;
  75.                         }
  76.                 }
  77.  
  78.                 if (!$found) {
  79.                         echo json_encode(array());
  80.                 }
  81.         }
  82.  
  83.         // Action:     tree_load
  84.         // Method:     GET / POST
  85.         // Parameters: id; goto (optional)
  86.         // Outputs:    JSON
  87.         if (isset($_REQUEST["action"]) && ($_REQUEST['action'] == 'tree_load')) {
  88.                 $handled = true;
  89.                 if (!isset($_REQUEST['id'])) throw new Exception("Invalid args");
  90.                 $json = OIDplusTree::json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : '');
  91.                 echo $json;
  92.         }
  93.  
  94.         // === Admin / RA actions ===
  95.  
  96.         // Action:     delete_ra
  97.         // Method:     POST
  98.         // Parameters: email
  99.         // Outputs:    Text
  100.         if (isset($_POST["action"]) && ($_POST["action"] == "delete_ra")) {
  101.                 $handled = true;
  102.  
  103.                 $email = $_POST['email'];
  104.  
  105.                 $ra_logged_in = OIDplus::authUtils()->isRaLoggedIn($email);
  106.  
  107.                 if (!OIDplus::authUtils()->isAdminLoggedIn() && !$ra_logged_in) {
  108.                         throw new Exception('Authentification error. Please log in.');
  109.                 }
  110.  
  111.                 if ($ra_logged_in) OIDplus::authUtils()->raLogout($email);
  112.  
  113.                 $ra = new OIDplusRA($email);
  114.                 $ra->delete();
  115.  
  116.                 OIDplus::logger()->log("RA($email)?/A?", "RA '$email' deleted");
  117.  
  118.                 echo json_encode(array("status" => 0));
  119.         }
  120.  
  121.         // === OID CRUD ===
  122.  
  123.         // Action:     Delete
  124.         // Method:     POST
  125.         // Parameters: id
  126.         // Outputs:    Text
  127.         if (isset($_POST["action"]) && ($_POST["action"] == "Delete")) {
  128.                 $handled = true;
  129.  
  130.                 $id = $_POST['id'];
  131.                 $obj = OIDplusObject::parse($id);
  132.  
  133.                 // Prüfen ob zugelassen
  134.                 if (!$obj->userHasParentalWriteRights()) throw new Exception('Authentification error. Please log in as the superior RA to delete this OID.');
  135.                
  136.                 OIDplus::logger()->log("OID($id)+SUPOIDRA($id)?/A?", "Object '$id' (recursively) deleted");
  137.                 OIDplus::logger()->log("OIDRA($id)!", "Lost ownership of object '$id' because it was deleted");
  138.  
  139.                 // Delete object
  140.                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
  141.  
  142.                 // Delete orphan stuff
  143.                 foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  144.                         $where = "where parent <> '".OIDplus::db()->real_escape_string($ot::root())."' and " .
  145.                                  "      parent like '".OIDplus::db()->real_escape_string($ot::root().'%')."' and " .
  146.                                  "      parent not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like '".OIDplus::db()->real_escape_string($ot::root().'%')."')";
  147.                         do {
  148.                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects $where");
  149.                                 while ($row = OIDplus::db()->fetch_array($res)) {
  150.                                         $id_to_delete = $row['id'];
  151.                                         OIDplus::logger()->log("OIDRA($id_to_delete)!", "Lost ownership of object '$id_to_delete' because one of the superior objects ('$id') was recursively deleted");
  152.                                         if (!OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id_to_delete)."'")) {
  153.                                                 throw new Exception(OIDplus::db()->error());
  154.                                         }
  155.                                 }
  156.                         } while (OIDplus::db()->num_rows($res) > 0);
  157.                 }
  158.                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."asn1id where well_known <> 1 and oid not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like 'oid:%');");
  159.                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."iri    where well_known <> 1 and oid not in (select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like 'oid:%');");
  160.  
  161.                 echo json_encode(array("status" => 0));
  162.         }
  163.  
  164.         // Action:     Update
  165.         // Method:     POST
  166.         // Parameters: id, ra_email, iris, asn1ids, confidential
  167.         // Outputs:    Text
  168.         if (isset($_POST["action"]) && ($_POST["action"] == "Update")) {
  169.                 $handled = true;
  170.  
  171.                 $id = $_POST['id'];
  172.                 $obj = OIDplusObject::parse($id);
  173.  
  174.                 // Prüfen ob zugelassen
  175.                 if (!$obj->userHasParentalWriteRights()) throw new Exception('Authentification error. Please log in as the superior RA to update this OID.');
  176.  
  177.                 // Validate RA email address
  178.                 $new_ra = $_POST['ra_email'];
  179.                 if (!empty($new_ra) && !oidplus_valid_email($new_ra)) {
  180.                         throw new Exception('Invalid RA email address');
  181.                 }
  182.                
  183.                 // RA ändern (rekursiv)
  184.                 $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
  185.                 $row = OIDplus::db()->fetch_array($res);
  186.                 $current_ra = $row['ra_email'];
  187.  
  188.                 if ($new_ra != $current_ra) {
  189.                         OIDplus::logger()->log("OID($id)+SUPOIDRA($id)?/A?", "RA of object '$id' changed from '$current_ra' to '$new_ra'");
  190.                         OIDplus::logger()->log("RA($current_ra)!",           "Lost ownership of object '$id' due to RA transfer of superior RA / admin.");
  191.                         OIDplus::logger()->log("RA($new_ra)!",               "Gained ownership of object '$id' due to RA transfer of superior RA / admin.");
  192.                         _ra_change_rec($id, $current_ra, $new_ra); // Inherited RAs rekursiv mitändern
  193.                 }
  194.  
  195.                 OIDplus::logger()->log("OID($id)+SUPOIDRA($id)?/A?", "Identifiers/Confidential flag of object '$id' updated"); // TODO: Check if they were ACTUALLY updated!
  196.  
  197.                 // Replace ASN.1 und IRI IDs
  198.                 if ($obj::ns() == 'oid') {
  199.                         $oid = $obj;
  200.  
  201.                         $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
  202.                         $ids = array_map('trim',$ids);
  203.                         $oid->replaceIris($ids);
  204.  
  205.                         $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
  206.                         $ids = array_map('trim',$ids);
  207.                         $oid->replaceAsn1Ids($ids);
  208.                 }
  209.  
  210.                 $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
  211.                 if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."objects SET confidential = ".OIDplus::db()->real_escape_string($confidential).", updated = now() WHERE id = '".OIDplus::db()->real_escape_string($id)."'")) {
  212.                         throw new Exception('Error at setting confidential flag:' . OIDplus::db()->error());
  213.                 }
  214.  
  215.                 $status = 0;
  216.  
  217.                 if (!empty($new_ra)) {
  218.                         $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($new_ra)."'");
  219.                         if (OIDplus::db()->num_rows($res) == 0) $status = 1;
  220.                 }
  221.  
  222.                 echo json_encode(array("status" => $status));
  223.         }
  224.  
  225.         // Action:     Update2
  226.         // Method:     POST
  227.         // Parameters: id, title, description
  228.         // Outputs:    Text
  229.         if (isset($_POST["action"]) && ($_POST["action"] == "Update2")) {
  230.                 $handled = true;
  231.  
  232.                 $id = $_POST['id'];
  233.                 $obj = OIDplusObject::parse($id);
  234.  
  235.                 // Prüfen ob zugelassen
  236.                 if (!$obj->userHasWriteRights()) throw new Exception('Authentification error. Please log in as the RA to update this OID.');
  237.                
  238.                 OIDplus::logger()->log("OID($id)+OIDRA($id)?/A?", "Title/Description of object '$id' updated");
  239.  
  240.                 if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."objects SET title = '".OIDplus::db()->real_escape_string($_POST['title'])."', description = '".OIDplus::db()->real_escape_string($_POST['description'])."', updated = now() WHERE id = '".OIDplus::db()->real_escape_string($id)."'")) {
  241.                         throw new Exception(OIDplus::db()->error());
  242.                 }
  243.  
  244.                 echo json_encode(array("status" => 0));
  245.         }
  246.  
  247.         // Action:     Insert
  248.         // Method:     POST
  249.         // Parameters: parent, id, ra_email, confidential, iris, asn1ids
  250.         // Outputs:    Text
  251.         if (isset($_POST["action"]) && ($_POST["action"] == "Insert")) {
  252.                 $handled = true;
  253.  
  254.                 // Es wird validiert: ID, ra email, asn1 ids, iri ids
  255.  
  256.                 // Check if you have write rights on the parent (to create a new object)
  257.                 $objParent = OIDplusObject::parse($_POST['parent']);
  258.                 if (!$objParent->userHasWriteRights()) throw new Exception('Authentification error. Please log in as the correct RA to insert an OID at this arc.');
  259.  
  260.                 // Check if the ID is valid
  261.                 if ($_POST['id'] == '') throw new Exception('ID may not be empty');
  262.                
  263.                 // Absoluten OID namen bestimmen
  264.                 // Note: At addString() and parse(), the syntax of the ID will be checked
  265.                 $id = $objParent->addString($_POST['id']);
  266.                 $obj = OIDplusObject::parse($id);
  267.  
  268.                 // Superior RA Änderung durchführen
  269.                 $parent = $_POST['parent'];
  270.                 $ra_email = $_POST['ra_email'];
  271.                 if (!empty($ra_email) && !oidplus_valid_email($ra_email)) {
  272.                         throw new Exception('Invalid RA email address');
  273.                 }
  274.                 $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
  275.  
  276.                 OIDplus::logger()->log("OID($parent)+OID($id)+OIDRA($parent)?/A?", "Object '$id' created, ".(empty($ra_email) ? "without defined RA" : "given to RA '$ra_email'")).", superior object is '$parent'";
  277.                 if (!empty($ra_email)) {
  278.                         OIDplus::logger()->log("RA($ra_email)!", "Gained ownership of newly created object '$id'");
  279.                 }
  280.  
  281.                 if (!OIDplus::db()->query("INSERT INTO ".OIDPLUS_TABLENAME_PREFIX."objects (id, parent, ra_email, confidential, created) VALUES ('".OIDplus::db()->real_escape_string($id)."', '".OIDplus::db()->real_escape_string($parent)."', '".OIDplus::db()->real_escape_string($ra_email)."', ".OIDplus::db()->real_escape_string($confidential).", now())")) {
  282.                         throw new Exception(OIDplus::db()->error());
  283.                 }
  284.  
  285.                 // Set ASN.1 und IRI IDs
  286.                 if ($obj::ns() == 'oid') {
  287.                         $oid = $obj;
  288.  
  289.                         $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
  290.                         $ids = array_map('trim',$ids);
  291.                         $oid->replaceIris($ids);
  292.  
  293.                         $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
  294.                         $ids = array_map('trim',$ids);
  295.                         $oid->replaceAsn1Ids($ids);
  296.                 }
  297.  
  298.                 $status = 0;
  299.  
  300.                 if (!empty($ra_email)) {
  301.                         // Do we need to notify that the RA does not exist?
  302.                         $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($ra_email)."'");
  303.                         if (OIDplus::db()->num_rows($res) == 0) $status = 1;
  304.                 }
  305.  
  306.                 echo json_encode(array("status" => $status));
  307.         }
  308.  
  309.         if (!$handled) {
  310.                 throw new Exception('Invalid action ID');
  311.         }
  312. } catch (Exception $e) {
  313.         $ary = array();
  314.         $ary['error'] = $e->getMessage();
  315.         echo json_encode($ary);
  316. }
  317.  
  318. # ---
  319.  
  320. function _ra_change_rec($id, $old_ra, $new_ra) {
  321.         OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."objects set ra_email = '".OIDplus::db()->real_escape_string($new_ra)."', updated = now() where id = '".OIDplus::db()->real_escape_string($id)."' and ifnull(ra_email,'') = '".OIDplus::db()->real_escape_string($old_ra)."'");
  322.  
  323.         $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string($id)."' and ifnull(ra_email,'') = '".OIDplus::db()->real_escape_string($old_ra)."'");
  324.         while ($row = OIDplus::db()->fetch_array($res)) {
  325.                 _ra_change_rec($row['id'], $old_ra, $new_ra);
  326.         }
  327. }
  328.