Subversion Repositories oidplus

Rev

Rev 148 | 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusPagePublicFreeOID extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'public';
  25.         }
  26.  
  27.         public function priority() {
  28.                 return 200;
  29.         }
  30.  
  31.         private static function getFreeRootOid() {
  32.                 return OIDplusOID::parse('oid:'.OIDplus::config()->getValue('freeoid_root_oid'));
  33.         }
  34.  
  35.         public function action(&$handled) {
  36.                 if (isset($_POST["action"]) && ($_POST["action"] == "com.viathinksoft.freeoid.request_freeoid")) {
  37.                         $handled = true;
  38.                         $email = $_POST['email'];
  39.  
  40.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($email));
  41.                         if (OIDplus::db()->num_rows($res) > 0) {
  42.                                 die(json_encode(array("error" => 'This email address already exists.'))); // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID
  43.                         }
  44.  
  45.                         if (!oidplus_valid_email($email)) {
  46.                                 die(json_encode(array("error" => 'Invalid email address')));
  47.                         }
  48.  
  49.                         if (RECAPTCHA_ENABLED) {
  50.                                 $secret=RECAPTCHA_PRIVATE;
  51.                                 $response=$_POST["captcha"];
  52.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  53.                                 $captcha_success=json_decode($verify);
  54.                                 if ($captcha_success->success==false) {
  55.                                         die(json_encode(array("error" => 'Captcha wrong')));
  56.                                 }
  57.                         }
  58.  
  59.                         $root_oid = OIDplus::config()->getValue('freeoid_root_oid');
  60.                         OIDplus::logger()->log("OID(oid:$root_oid)+RA($email)!", "Requested a free OID for email '$email' to be placed into root '$root_oid'");
  61.  
  62.                         $timestamp = time();
  63.                         $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp));
  64.  
  65.                         $message = file_get_contents(__DIR__ . '/request_msg.tpl');
  66.                         $message = str_replace('{{SYSTEM_URL}}', OIDplus::system_url(), $message);
  67.                         $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message);
  68.                         $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  69.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  70.                         my_mail($email, OIDplus::config()->systemTitle().' - Free OID request', $message, OIDplus::config()->globalCC());
  71.  
  72.                         echo json_encode(array("status" => 0));
  73.                 }
  74.  
  75.                 if (isset($_POST["action"]) && ($_POST["action"] == "com.viathinksoft.freeoid.activate_freeoid")) {
  76.                         $handled = true;
  77.  
  78.                         $password1 = $_POST['password1'];
  79.                         $password2 = $_POST['password2'];
  80.                         $email = $_POST['email'];
  81.  
  82.                         $ra_name = $_POST['ra_name'];
  83.                         $url = $_POST['url'];
  84.                         $title = $_POST['title'];
  85.  
  86.                         $auth = $_POST['auth'];
  87.                         $timestamp = $_POST['timestamp'];
  88.  
  89.                         if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
  90.                                 die(json_encode(array("error" => 'Invalid auth key')));
  91.                         }
  92.  
  93.                         if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
  94.                                 die(json_encode(array("error" => 'Invitation expired!')));
  95.                         }
  96.  
  97.                         if ($password1 !== $password2) {
  98.                                 die(json_encode(array("error" => 'Passwords are not equal')));
  99.                         }
  100.  
  101.                         if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
  102.                                 die(json_encode(array("error" => 'Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength())));
  103.                         }
  104.  
  105.                         if (empty($ra_name)) {
  106.                                 die(json_encode(array("error" => 'Please enter your personal name or the name of your group.')));
  107.                         }
  108.  
  109.                         // 1. step: Add the RA to the database
  110.  
  111.                         $ra = new OIDplusRA($email);
  112.                         $ra->register_ra($password1);
  113.                         $ra->setRaName($ra_name);
  114.  
  115.                         // 2. step: Add the new OID to the database
  116.  
  117.                         $root_oid = OIDplus::config()->getValue('freeoid_root_oid');
  118.                         $new_oid = $root_oid.'.'.($this->freeoid_max_id()+1);
  119.  
  120.                         OIDplus::logger()->log("OID(oid:$root_oid)+OIDRA(oid:$root_oid)!", "Child OID '$new_oid' added automatically by '$email' (RA Name: '$ra_name')");
  121.                         OIDplus::logger()->log("OID(oid:$new_oid)+RA($email)!",            "Free OID '$new_oid' activated (RA Name: '$ra_name')");
  122.  
  123.                         if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
  124.  
  125.                         $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
  126.                         if (!empty($url)) {
  127.                                 $description .= '<p>More information at <a href="'.htmlentities($url).'">'.htmlentities($url).'</a></p>';
  128.                         }
  129.  
  130.                         if (empty($title)) $title = $ra_name;
  131.  
  132.                         if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."objects (id, ra_email, parent, title, description, confidential, created) values (?, ?, ?, ?, ?, 0, now())", array('oid:'.$new_oid, $email, 'oid:'.OIDplus::config()->getValue('freeoid_root_oid'), $title, $description))) {
  133.                                 $ra->delete();
  134.                                 die(json_encode(array("error" => OIDplus::db()->error())));
  135.                         }
  136.  
  137.                         // Send delegation report email to admin
  138.  
  139.                         $message  = "OID delegation report\n";
  140.                         $message .= "\n";
  141.                         $message .= "OID: ".$new_oid."\n";;
  142.                         $message .= "\n";
  143.                         $message .= "RA Name: $ra_name\n";
  144.                         $message .= "RA eMail: $email\n";
  145.                         $message .= "URL for more information: $url\n";
  146.                         $message .= "OID Name: $title\n";
  147.                         $message .= "\n";
  148.                         $message .= "More details: ".OIDplus::system_url()."?goto=oid:$new_oid\n";
  149.  
  150.                         my_mail($email, OIDplus::config()->systemTitle()." - OID $new_oid registered", $message, OIDplus::config()->globalCC());
  151.  
  152.                         // Send delegation information to user
  153.  
  154.                         $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
  155.                         $message = str_replace('{{SYSTEM_URL}}', OIDplus::system_url(), $message);
  156.                         $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message);
  157.                         $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  158.                         $message = str_replace('{{NEW_OID}}', $new_oid, $message);
  159.                         my_mail($email, OIDplus::config()->systemTitle().' - Free OID allocated', $message, OIDplus::config()->globalCC());
  160.  
  161.                         echo json_encode(array("status" => 0));
  162.                 }
  163.         }
  164.  
  165.         public function init($html=true) {
  166.                 OIDplus::config()->prepareConfigKey('freeoid_root_oid', 'Root-OID of free OID service', '', 0, 1);
  167.         }
  168.  
  169.         public function cfgSetValue($name, $value) {
  170.                 if ($name == 'freeoid_root_oid') {
  171.                         if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
  172.                                 throw new Exception("Please enter a valid OID in dot notation or nothing");
  173.                         }
  174.                 }
  175.         }
  176.  
  177.         public function gui($id, &$out, &$handled) {
  178.                 if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
  179.                         $handled = true;
  180.  
  181.                         $out['title'] = 'Register a free OID';
  182.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
  183.  
  184.                         $highest_id = $this->freeoid_max_id();
  185.  
  186.                         $out['text'] .= '<p>Currently <a '.oidplus_link('oid:'.OIDplus::config()->getValue('freeoid_root_oid')).'>'.$highest_id.' free OIDs have been</a> registered. Please enter your email below to receive a free OID.</p>';
  187.  
  188.                         try {
  189.                                 $out['text'] .= '
  190.                                   <form id="freeOIDForm" onsubmit="return freeOIDFormOnSubmit();">
  191.                                     E-Mail: <input type="text" id="email" value=""/><br><br>'.
  192.                                  (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'.
  193.                                                    '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.RECAPTCHA_PUBLIC.'"></div>' : '').
  194.                                 ' <br>
  195.                                     <input type="submit" value="Request free OID">
  196.                                   </form>';
  197.  
  198.                                 $tos = file_get_contents(__DIR__ . '/tos.html');
  199.                                 $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
  200.                                 $tos = str_replace('{{ROOT_OID}}', OIDplus::config()->getValue('freeoid_root_oid'), $tos);
  201.                                 $tos = str_replace('{{ROOT_OID_ASN1}}', self::getFreeRootOid()->getAsn1Notation(), $tos);
  202.                                 $tos = str_replace('{{ROOT_OID_IRI}}', self::getFreeRootOid()->getIriNotation(), $tos);
  203.                                 $out['text'] .= $tos;
  204.                         } catch (Exception $e) {
  205.                                 $out['text'] = "Error: ".$e->getMessage();
  206.                         }
  207.                 } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
  208.                         $handled = true;
  209.  
  210.                         $email = explode('$',$id)[1];
  211.                         $timestamp = explode('$',$id)[2];
  212.                         $auth = explode('$',$id)[3];
  213.  
  214.                         $out['title'] = 'Activate Free OID';
  215.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
  216.  
  217.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($email));
  218.                         if (OIDplus::db()->num_rows($res) > 0) {
  219.                                 $out['icon'] = 'img/error_big.png';
  220.                                 $out['text'] = 'This RA is already registered.'; // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID
  221.                         } else {
  222.                                 if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
  223.                                         $out['icon'] = 'img/error_big.png';
  224.                                         $out['text'] = 'Invalid authorization. Is the URL OK?';
  225.                                 } else {
  226.                                         $out['text'] = '<p>eMail-Address: <b>'.$email.'</b></p>
  227.  
  228.                                   <form id="activateFreeOIDForm" onsubmit="return activateFreeOIDFormOnSubmit();">
  229.                                     <input type="hidden" id="email" value="'.htmlentities($email).'"/>
  230.                                     <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  231.                                     <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  232.  
  233.                                     Your personal name or the name of your group:<br><input type="text" id="ra_name" value=""/><br><br><!-- TODO: disable autocomplete -->
  234.                                     Title of your OID (usually equal to your name, optional):<br><input type="text" id="title" value=""/><br><br>
  235.                                     URL for more information about your project(s) (optional):<br><input type="text" id="url" value=""/><br><br>
  236.  
  237.                                     <label class="padding_label">Password:</label><input type="password" id="password1" value=""/><br><br>
  238.                                     <label class="padding_label">Again:</label><input type="password" id="password2" value=""/><br><br>
  239.                                     <input type="submit" value="Register">
  240.                                   </form>';
  241.                                 }
  242.                         }
  243.                 }
  244.         }
  245.  
  246.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  247.                 if (file_exists(__DIR__.'/treeicon.png')) {
  248.                         $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
  249.                 } else {
  250.                         $tree_icon = null; // default icon (folder)
  251.                 }
  252.  
  253.                 $json[] = array(
  254.                         'id' => 'oidplus:com.viathinksoft.freeoid',
  255.                         'icon' => $tree_icon,
  256.                         'text' => 'Register a free OID'
  257.                 );
  258.  
  259.                 return true;
  260.         }
  261.  
  262.         # ---
  263.  
  264.         protected function freeoid_max_id() {
  265.                 $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like ? order by ".OIDplus::db()->natOrder('id'), array('oid:'.OIDplus::config()->getValue('freeoid_root_oid').'.%'));
  266.                 $highest_id = 0;
  267.                 while ($row = OIDplus::db()->fetch_array($res)) {
  268.                         $arc = substr_count(OIDplus::config()->getValue('freeoid_root_oid'), '.')+1;
  269.                         $highest_id = explode('.',$row['id'])[$arc];
  270.                 }
  271.                 return $highest_id;
  272.         }
  273.  
  274.         public function tree_search($request) {
  275.                 return false;
  276.         }
  277. }
  278.  
  279. if (OIDplus::config()->getValue('freeoid_root_oid') != '') {
  280.         OIDplus::registerPagePlugin(new OIDplusPagePublicFreeOID());
  281. }
  282.