Subversion Repositories oidplus

Rev

Go to most recent revision | Blame | 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:text/plain; charset=utf-8');
  28.  
  29. try {
  30.         if (isset($_POST["action"])) {
  31.  
  32.                 $handled = false;
  33.  
  34.                 // === Plugins ===
  35.  
  36.                 $ary = glob(__DIR__ . '/plugins/publicPages/'.'*'.'/action.inc.php');
  37.                 sort($ary);
  38.                 foreach ($ary as $a) include $a;
  39.  
  40.                 $ary = glob(__DIR__ . '/plugins/adminPages/'.'*'.'/action.inc.php');
  41.                 sort($ary);
  42.                 foreach ($ary as $a) include $a;
  43.  
  44.                 $ary = glob(__DIR__ . '/plugins/raPages/'.'*'.'/action.inc.php');
  45.                 sort($ary);
  46.                 foreach ($ary as $a) include $a;
  47.  
  48.                 // === INVITATION ===
  49.  
  50.                 if ($_POST["action"] == "invite_ra") {
  51.                         $handled = true;
  52.                         $email = $_POST['email'];
  53.  
  54.                         if (!oiddb_valid_email($email)) {
  55.                                 die('Invalid email address');
  56.                         }
  57.  
  58.                         if (RECAPTCHA_ENABLED) {
  59.                                 $secret=RECAPTCHA_PRIVATE;
  60.                                 $response=$_POST["captcha"];
  61.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  62.                                 $captcha_success=json_decode($verify);
  63.                                 if ($captcha_success->success==false) {
  64.                                         die('Captcha wrong');
  65.                                 }
  66.                         }
  67.  
  68.                         $timestamp = time();
  69.                         $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:activate_ra$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_ra;'.$email.';'.$timestamp));
  70.  
  71.                         $message = OIDplus::gui()::getInvitationText($_POST['email']);
  72.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  73.  
  74.                         my_mail($email, 'Invitation from OIDplus', $message, OIDplus::config()->globalCC());
  75.  
  76.                         die("OK");
  77.                 }
  78.  
  79.                 if ($_POST["action"] == "activate_ra") {
  80.                         $handled = true;
  81.  
  82.                         $password1 = $_POST['password1'];
  83.                         $password2 = $_POST['password2'];
  84.                         $email = $_POST['email'];
  85.                         $auth = $_POST['auth'];
  86.                         $timestamp = $_POST['timestamp'];
  87.  
  88.                         if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) {
  89.                                 die('Invalid auth key');
  90.                         }
  91.  
  92.                         if ((OIDplus::config()->maxInviteTime() > 0) && (time()-$timestamp > OIDplus::config()->maxInviteTime())) {
  93.                                 die('Invitation expired!');
  94.                         }
  95.  
  96.                         if ($password1 !== $password2) {
  97.                                 die('Passwords are not equal');
  98.                         }
  99.  
  100.                         if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
  101.                                 die('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength());
  102.                         }
  103.  
  104.                         $ra = new OIDplusRA($email);
  105.                         $ra->register_ra($password1);
  106.  
  107.                         die('OK');
  108.                 }
  109.  
  110.                 // === FORGOT PASSWORD ===
  111.  
  112.                 if ($_POST["action"] == "forgot_password") {
  113.                         $handled = true;
  114.  
  115.                         $email = $_POST['email'];
  116.  
  117.                         if (!oiddb_valid_email($email)) {
  118.                                 die('Invalid email address');
  119.                         }
  120.  
  121.                         if (RECAPTCHA_ENABLED) {
  122.                                 $secret=RECAPTCHA_PRIVATE;
  123.                                 $response=$_POST["captcha"];
  124.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  125.                                 $captcha_success=json_decode($verify);
  126.                                 if ($captcha_success->success==false) {
  127.                                         die('Captcha wrong');
  128.                                 }
  129.                         }
  130.  
  131.                         $timestamp = time();
  132.                         $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('reset_password;'.$email.';'.$timestamp));
  133.  
  134.                         $message = OIDplus::gui()::getForgotPasswordText($_POST['email']);
  135.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  136.  
  137.                         my_mail($email, 'OIDplus password reset', $message, OIDplus::config()->globalCC());
  138.  
  139.                         die("OK");
  140.                 }
  141.  
  142.                 if ($_POST["action"] == "reset_password") {
  143.                         $handled = true;
  144.  
  145.                         $password1 = $_POST['password1'];
  146.                         $password2 = $_POST['password2'];
  147.                         $email = $_POST['email'];
  148.                         $auth = $_POST['auth'];
  149.                         $timestamp = $_POST['timestamp'];
  150.  
  151.                         if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  152.                                 die('Invalid auth key');
  153.                         }
  154.  
  155.                         if ((OIDplus::config()->maxPasswordResetTime() > 0) && (time()-$timestamp > OIDplus::config()->maxPasswordResetTime())) {
  156.                                 die('Invitation expired!');
  157.                         }
  158.  
  159.                         if ($password1 !== $password2) {
  160.                                 die('Passwords are not equal');
  161.                         }
  162.  
  163.                         if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
  164.                                 die('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength());
  165.                         }
  166.  
  167.                         $ra = new OIDplusRA($email);
  168.                         $ra->change_password($password1);
  169.  
  170.                         die('OK');
  171.                 }
  172.  
  173.                 // === Admin / RA actions ===
  174.  
  175.                 if ($_POST["action"] == "delete_ra") {
  176.                         $handled = true;
  177.  
  178.                         $email = $_POST['email'];
  179.  
  180.                         $ra_logged_in = OIDplus::authUtils()::isRaLoggedIn($email);
  181.  
  182.                         if (!OIDplus::authUtils()::isAdminLoggedIn() && !$ra_logged_in) {
  183.                                 die('You need to log in as administrator');
  184.                         }
  185.  
  186.                         if ($ra_logged_in) OIDplus::authUtils()::raLogout($email);
  187.  
  188.                         $ra = new OIDplusRA($email);
  189.                         $ra->delete();
  190.  
  191.                         die('OK');
  192.                 }
  193.  
  194.                 // === OID CRUD ===
  195.  
  196.                 if ($_POST["action"] == "Delete") {
  197.                         $handled = true;
  198.  
  199.                         $id = $_POST['id'];
  200.                         $obj = OIDplusObject::parse($_POST['id']);
  201.  
  202.                         // Prüfen ob zugelassen
  203.                         if (!$obj->userHasParentalWriteRights()) die('Authentification error. Please log in as the superior RA to delete this OID.');
  204.  
  205.                         // Nun löschen
  206.                         OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
  207.                         if ($obj::ns() == 'oid') {
  208.                                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."objects where id like '".OIDplus::db()->real_escape_string($id).".%'"); // Unter-OIDs löschen
  209.  
  210.                                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = '".OIDplus::db()->real_escape_string($id)."'");
  211.                                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid like '".OIDplus::db()->real_escape_string($id).".%'"); // Unter-OIDs löschen
  212.  
  213.                                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = '".OIDplus::db()->real_escape_string($id)."'");
  214.                                 OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."iri where oid like '".OIDplus::db()->real_escape_string($id).".%'"); // Unter-OIDs löschen
  215.                         } else {
  216.                                 // TODO: Wir sollten nun aber auch hier rekurisv löschen, (evtl per Foreign Key Constraint?)
  217.                         }
  218.  
  219.                         echo "OK";
  220.                 }
  221.                 if ($_POST["action"] == "Update") {
  222.                         $handled = true;
  223.  
  224.                         // TODO: eingaben auf gültigkeit prüfen
  225.                         $id = $_POST['id'];
  226.                         $obj = OIDplusObject::parse($_POST['id']);
  227.                         $new_ra = $_POST['ra_email'];
  228.  
  229.                         // Prüfen ob zugelassen
  230.                         if (!$obj->userHasParentalWriteRights()) die('Authentification error. Please log in as the superior RA to update this OID.');
  231.  
  232.                         // RA ändern (rekursiv)
  233.                         $res = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
  234.                         $row = OIDplus::db()->fetch_array($res);
  235.                         $current_ra = $row['ra_email'];
  236.  
  237.                         if ($new_ra != $current_ra) _ra_change_rec($id, $current_ra, $new_ra); // Inherited RAs rekursiv mitändern
  238.  
  239.                         $error = false;
  240.  
  241.                         if ($obj::ns() == 'oid') {
  242.                                 $oid = $obj;
  243.  
  244.                                 try {
  245.                                         $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
  246.                                         $ids = array_map('trim',$ids);
  247.                                         $oid->replaceIris($ids);
  248.                                 } catch (Exception $e) {
  249.                                         echo $e->getMessage()."\n";
  250.                                         $error = true;
  251.                                 }
  252.  
  253.                                 try {
  254.                                         $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
  255.                                         $ids = array_map('trim',$ids);
  256.                                         $oid->replaceAsn1Ids($ids);
  257.                                 } catch (Exception $e) {
  258.                                         echo $e->getMessage()."\n";
  259.                                         $error = true;
  260.                                 }
  261.                         }
  262.  
  263.                         $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
  264.                         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)."'")) {
  265.                                 die('Error at setting confidential flag:' . OIDplus::db()->error());
  266.                         }
  267.  
  268.                         if (!$error) {
  269.                                 echo "OK";
  270.  
  271.                                 if (!empty($new_ra)) {
  272.                                         $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($new_ra)."'");
  273.                                         if (OIDplus::db()->num_rows($res) == 0) echo " (RaNotInDatabase)"; // do not change
  274.                                 }
  275.                         }
  276.                 }
  277.                 if ($_POST["action"] == "Update2") {
  278.                         $handled = true;
  279.  
  280.                         $error = false;
  281.                         $id = $_POST['id'];
  282.                         $obj = OIDplusObject::parse($_POST['id']);
  283.  
  284.                         // Prüfen ob zugelassen
  285.                         if (!$obj->userHasWriteRights()) die('Authentification error. Please log in as the RA to update this OID.');
  286.  
  287.                         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)."'")) {
  288.                                 die(OIDplus::db()->error());
  289.                         }
  290.  
  291.                         if (!$error) echo "OK";
  292.                 }
  293.                 if ($_POST["action"] == "Insert") {
  294.                         $handled = true;
  295.  
  296.                         // TODO: eingaben auf gültigkeit prüfen
  297.                         $objParent = OIDplusObject::parse($_POST['parent']);
  298.                         $error = false;
  299.  
  300.                         if (!$objParent->userHasWriteRights()) die('Authentification error. Please log in as the correct RA to insert an OID at this arc.');
  301.  
  302.                         if (is_null($_POST['id']) || ($_POST['id'] === '')) die('Invalid identifier. It may not be empty.');
  303.  
  304.                         // Absoluten OID namen bestimmen
  305.                         if ($parent = OIDplusObject::parse($_POST['parent'])) $id = $parent->addString($_POST['id']);
  306.                         if (is_null($parent)) throw new Exception("Type of ".$_POST['parent']." unknown");
  307.                         $obj = OIDplusObject::parse($id);
  308.  
  309.                         // Superior RA Änderung durchführen
  310.                         $parent = $_POST['parent'];
  311.                         $ra_email = $_POST['ra_email'];
  312.                         $confidential = $_POST['confidential'] == 'true' ? '1' : '0';
  313.                         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())")) {
  314.                                 die(OIDplus::db()->error());
  315.                         }
  316.  
  317.                         // nun ASN.1 und IRI IDs setzen
  318.                         if ($obj::ns() == 'oid') {
  319.                                 $oid = $obj;
  320.  
  321.                                 try {
  322.                                         $ids = ($_POST['iris'] == '') ? array() : explode(',',$_POST['iris']);
  323.                                         $ids = array_map('trim',$ids);
  324.                                         $oid->replaceIris($ids);
  325.                                 } catch (Exception $e) {
  326.                                         echo $e->getMessage()."\n";
  327.                                         $error = true;
  328.                                 }
  329.  
  330.                                 try {
  331.                                         $ids = ($_POST['asn1ids'] == '') ? array() : explode(',',$_POST['asn1ids']);
  332.                                         $ids = array_map('trim',$ids);
  333.                                         $oid->replaceAsn1Ids($ids);
  334.                                 } catch (Exception $e) {
  335.                                         echo $e->getMessage()."\n";
  336.                                         $error = true;
  337.                                 }
  338.                         }
  339.  
  340.                         if (!$error) {
  341.                                 echo "OK";
  342.  
  343.                                 if (!empty($ra_email)) {
  344.                                         $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($ra_email)."'");
  345.                                         if (OIDplus::db()->num_rows($res) == 0) echo " (RaNotInDatabase)"; // do not change
  346.                                 }
  347.                         }
  348.                 }
  349.  
  350.                 // === RA LOGIN/LOGOUT ===
  351.  
  352.                 if ($_POST["action"] == "ra_login") {
  353.                         $handled = true;
  354.  
  355.                         $ra = new OIDplusRA($_POST['email']);
  356.  
  357.                         if (RECAPTCHA_ENABLED) {
  358.                                 $secret=RECAPTCHA_PRIVATE;
  359.                                 $response=$_POST["captcha"];
  360.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  361.                                 $captcha_success=json_decode($verify);
  362.                                 if ($captcha_success->success==false) {
  363.                                         die('Captcha wrong');
  364.                                 }
  365.                         }
  366.  
  367.                         if ($ra->checkPassword($_POST['password'])) {
  368.                                 OIDplus::authUtils()::raLogin($_POST['email']);
  369.  
  370.                                 if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."ra set last_login = now() where email = '".OIDplus::db()->real_escape_string($_POST['email'])."'")) {
  371.                                         die(OIDplus::db()->error());
  372.                                 }
  373.  
  374.                                 echo "OK";
  375.                         } else {
  376.                                 echo "Wrong password";
  377.                         }
  378.                 }
  379.                 if ($_POST["action"] == "ra_logout") {
  380.                         $handled = true;
  381.                         OIDplus::authUtils()::raLogout($_POST['email']);
  382.                         echo "OK";
  383.                 }
  384.  
  385.                 // === ADMIN LOGIN/LOGOUT ===
  386.  
  387.                 if ($_POST["action"] == "admin_login") {
  388.                         $handled = true;
  389.  
  390.                         if (RECAPTCHA_ENABLED) {
  391.                                 $secret=RECAPTCHA_PRIVATE;
  392.                                 $response=$_POST["captcha"];
  393.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  394.                                 $captcha_success=json_decode($verify);
  395.                                 if ($captcha_success->success==false) {
  396.                                         die('Captcha wrong');
  397.                                 }
  398.                         }
  399.  
  400.                         if (OIDplus::authUtils()::adminCheckPassword($_POST['password'])) {
  401.                                 OIDplus::authUtils()::adminLogin();
  402.                                 echo "OK";
  403.                         } else {
  404.                                 echo "Wrong password";
  405.                         }
  406.                 }
  407.                 if ($_POST["action"] == "admin_logout") {
  408.                         $handled = true;
  409.                         OIDplus::authUtils()::adminLogout();
  410.                         echo "OK";
  411.                 }
  412.  
  413.                 // === Not found ===
  414.  
  415.                 if (!$handled) {
  416.                         die('Invalid action ID');
  417.                 }
  418.         }
  419. } catch (Exception $e) {
  420.         echo $e->getMessage();
  421. }
  422.  
  423. # ---
  424.  
  425. function _ra_change_rec($id, $old_ra, $new_ra) {
  426.         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)."'");
  427.  
  428.         $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)."'");
  429.         while ($row = OIDplus::db()->fetch_array($res)) {
  430.                 _ra_change_rec($row['id'], $old_ra, $new_ra);
  431.         }
  432. }
  433.