Subversion Repositories oidplus

Rev

Rev 328 | 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. class OIDplusPagePublicLogin extends OIDplusPagePluginPublic {
  21.  
  22.         public function action($actionID, $params) {
  23.                 // === RA LOGIN/LOGOUT ===
  24.  
  25.                 if ($actionID == 'ra_login') {
  26.                         $email = $params['email'];
  27.                         $ra = new OIDplusRA($email);
  28.  
  29.                         if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
  30.                                 $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
  31.                                 $response=$params["captcha"];
  32.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  33.                                 $captcha_success=json_decode($verify);
  34.                                 if ($captcha_success->success==false) {
  35.                                         throw new OIDplusException(_L('Captcha wrong'));
  36.                                 }
  37.                         }
  38.  
  39.                         if ($ra->checkPassword($params['password'])) {
  40.                                 OIDplus::logger()->log("[OK]RA($email)!", "RA '$email' logged in");
  41.                                 OIDplus::authUtils()::raLogin($email);
  42.  
  43.                                 OIDplus::db()->query("UPDATE ###ra set last_login = ".OIDplus::db()->sqlDate()." where email = ?", array($email));
  44.  
  45.                                 return array("status" => 0);
  46.                         } else {
  47.                                 if (OIDplus::config()->getValue('log_failed_ra_logins', false)) {
  48.                                         if ($ra->existing()) {
  49.                                                 OIDplus::logger()->log("[WARN]A!", "Failed login to RA account '$email' (wrong password)");
  50.                                         } else {
  51.                                                 OIDplus::logger()->log("[WARN]A!", "Failed login to RA account '$email' (RA not existing)");
  52.                                         }
  53.                                 }
  54.                                 throw new OIDplusException(_L('Wrong password or user not registered'));
  55.                         }
  56.  
  57.                 } else if ($actionID == 'ra_logout') {
  58.  
  59.                         $email = $params['email'];
  60.  
  61.                         OIDplus::logger()->log("[OK]RA($email)!", "RA '$email' logged out");
  62.                         OIDplus::authUtils()::raLogout($email);
  63.                         return array("status" => 0);
  64.                 }
  65.  
  66.                 // === ADMIN LOGIN/LOGOUT ===
  67.  
  68.                 else if ($actionID == 'admin_login') {
  69.                         if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
  70.                                 $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
  71.                                 $response=$params["captcha"];
  72.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  73.                                 $captcha_success=json_decode($verify);
  74.                                 if ($captcha_success->success==false) {
  75.                                         throw new OIDplusException('Captcha wrong');
  76.                                 }
  77.                         }
  78.  
  79.                         if (OIDplus::authUtils()::adminCheckPassword($params['password'])) {
  80.                                 OIDplus::logger()->log("[OK]A!", "Admin logged in");
  81.                                 OIDplus::authUtils()::adminLogin();
  82.                                 return array("status" => 0);
  83.                         } else {
  84.                                 if (OIDplus::config()->getValue('log_failed_admin_logins', false)) {
  85.                                         OIDplus::logger()->log("[WARN]A!", "Failed login to admin account");
  86.                                 }
  87.                                 throw new OIDplusException('Wrong password');
  88.                         }
  89.                 }
  90.                 else if ($actionID == 'admin_logout') {
  91.                         OIDplus::logger()->log("[OK]A!", "Admin logged out");
  92.                         OIDplus::authUtils()::adminLogout();
  93.                         return array("status" => 0);
  94.                 }
  95.                 else {
  96.                         throw new OIDplusException(_L("Unknown action ID"));
  97.                 }
  98.         }
  99.  
  100.         public function init($html=true) {
  101.                 OIDplus::config()->prepareConfigKey('log_failed_ra_logins', 'Log failed RA logins', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  102.                         if (!is_numeric($value) || (($value != 0) && (($value != 1)))) {
  103.                                 throw new OIDplusException("Valid values: 0 (off) or 1 (on).");
  104.                         }
  105.                 });
  106.                 OIDplus::config()->prepareConfigKey('log_failed_admin_logins', 'Log failed Admin logins', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  107.                         if (!is_numeric($value) || (($value != 0) && (($value != 1)))) {
  108.                                 throw new OIDplusException("Valid values: 0 (off) or 1 (on).");
  109.                         }
  110.                 });
  111.         }
  112.  
  113.         public function gui($id, &$out, &$handled) {
  114.                 if ($id === 'oidplus:login') {
  115.                         $handled = true;
  116.                         $out['title'] = 'Login';
  117.                         $out['icon']  = OIDplus::webpath(__DIR__).'login_big.png';
  118.  
  119.                         $out['text'] .= '<noscript>';
  120.                         $out['text'] .= '<p>'._L('You need to enable JavaScript to use the login area.').'</p>';
  121.                         $out['text'] .= '</noscript>';
  122.  
  123.                         $out['text'] .= '<div id="loginArea" style="visibility: hidden"><div id="loginTab" class="container" style="width:100%;">';
  124.                         $out['text'] .= (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
  125.                                         '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
  126.                                         '<p>Before logging in, please solve the following CAPTCHA</p><div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : '');
  127.                         $out['text'] .= '<br>';
  128.                         $out['text'] .= '<ul class="nav nav-pills">';
  129.                         $out['text'] .= '                       <li class="active">';
  130.                         $out['text'] .= '                       <a href="#1a" data-toggle="tab">Login as RA</a>';
  131.                         $out['text'] .= '                       </li>';
  132.                         $out['text'] .= '                       <li><a href="#2a" data-toggle="tab">Login as administrator</a>';
  133.                         $out['text'] .= '                       </li>';
  134.                         $out['text'] .= '               </ul>';
  135.                         $out['text'] .= '                       <div class="tab-content clearfix">';
  136.                         $out['text'] .= '                         <div class="tab-pane active" id="1a">';
  137.  
  138.                         $out['text'] .= '<h2>Login as RA</h2>';
  139.  
  140.                         $login_list = OIDplus::authUtils()->loggedInRaList();
  141.                         if (count($login_list) > 0) {
  142.                                 foreach ($login_list as $x) {
  143.                                         $out['text'] .= '<p>You are logged in as <b>'.$x->raEmail().'</b> (<a href="#" onclick="return raLogout('.js_escape($x->raEmail()).');">Logout</a>)</p>';
  144.                                 }
  145.                                 $out['text'] .= '<p>'._L('If you have more accounts, you can log in with a new account:').'</p>';
  146.                         } else {
  147.                                 $out['text'] .= '<p>'._L('Enter your email address and your password to log in as Registration Authority.').'</p>';
  148.                         }
  149.                         $out['text'] .= '<form onsubmit="return raLoginOnSubmit(this);">';
  150.                         $out['text'] .= '<div><label class="padding_label">E-Mail:</label><input type="text" name="email" value="" id="raLoginEMail"></div>';
  151.                         $out['text'] .= '<div><label class="padding_label">Password:</label><input type="password" name="password" value="" id="raLoginPassword"></div>';
  152.                         $out['text'] .= '<br><input type="submit" value="Login"><br><br>';
  153.                         $out['text'] .= '</form>';
  154.                         $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:forgot_password').'>Forgot password?</a><br>';
  155.  
  156.                         if (class_exists('OIDplusPageRaInvite') && OIDplus::config()->getValue('ra_invitation_enabled')) {
  157.                                 $out['text'] .= '<abbr title="'._L('To receive login data, the superior RA needs to send you an invitation. After creating or updating your OID, the system will ask them if they want to send you an invitation. If they accept, you will receive an email with an activation link. Alternatively, the system admin can create your account manually in the administrator control panel.').'">'._L('How to register?').'</abbr></p>';
  158.                         } else {
  159.                                 $out['text'] .= '<abbr title="'._L('Since invitations are disabled at this OIDplus installation, the system administrator needs to create your account manually in the administrator control panel.').'">'._L('How to register?').'</abbr></p>';
  160.                         }
  161.  
  162.                         $out['text'] .= '                               </div>';
  163.                         $out['text'] .= '                               <div class="tab-pane" id="2a">';
  164.  
  165.                         if (OIDplus::authUtils()::isAdminLoggedIn()) {
  166.                                 $out['text'] .= '<h2>Admin login</h2>';
  167.                                 $out['text'] .= '<p>You are logged in as administrator.</p>';
  168.                                 $out['text'] .= '<a href="#" onclick="return adminLogout();">Logout</a>';
  169.                         } else {
  170.                                 $out['text'] .= '<h2>Login as administrator</h2>';
  171.                                 $out['text'] .= '<form onsubmit="return adminLoginOnSubmit(this);">';
  172.                                 $out['text'] .= '<div><label class="padding_label">Password:</label><input type="password" name="password" value="" id="adminLoginPassword"></div>';
  173.                                 $out['text'] .= '<br><input type="submit" value="Login"><br><br>';
  174.                                 $out['text'] .= '</form>';
  175.                                 $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:forgot_password_admin').'>Forgot password?</a><br>';
  176.                         }
  177.  
  178.                         $out['text'] .= '                               </div>';
  179.                         $out['text'] .= '                       </div>';
  180.                         $out['text'] .= '  </div><br>';
  181.                         $out['text'] .= '<p><font size="-1">'._L('<i>Privacy information</i>: By using the login functionality, you are accepting that a "session cookie" is temporarily stored in your browser. '.
  182.                                         'The session cookie is a small text file that is sent to this website every time you visit it, to identify you as an already logged in user. '.
  183.                                         'It does not track any of your online activities outside OIDplus. The cookie will be destroyed when you log out or after an inactivity of %d minutes.', ceil(OIDplus::baseConfig()->getValue('SESSION_LIFETIME', 30*60)/60));
  184.                         $privacy_document_file = 'OIDplus/privacy_documentation.html';
  185.                         if (class_exists('OIDplusPagePublicResources') && file_exists(OIDplus::basePath().'/res/'.$privacy_document_file)) {
  186.                                 $out['text'] .= ' <a '.OIDplus::gui()->link('oidplus:resources$'.$privacy_document_file.'$'.OIDplus::authUtils()::makeAuthKey("resources;".$privacy_document_file).'#cookies').'>More information about the cookies used</a>';
  187.                         }
  188.                         $out['text'] .= '</font></p></div>';
  189.  
  190.                         $out['text'] .= '<script>document.getElementById("loginArea").style.visibility = "visible";</script>';
  191.                 }
  192.         }
  193.  
  194.         public function publicSitemap(&$out) {
  195.                 $out[] = OIDplus::getSystemUrl().'?goto='.urlencode('oidplus:login');
  196.         }
  197.  
  198.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  199.                 $loginChildren = array();
  200.  
  201.                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  202.                         $ra_roots = array();
  203.  
  204.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  205.                                 if (is_subclass_of($plugin, OIDplusPagePluginAdmin::class)) {
  206.                                         $plugin->tree($ra_roots);
  207.                                 }
  208.                         }
  209.  
  210.                         $ra_roots[] = array(
  211.                                 'id'       => 'oidplus:logout$admin',
  212.                                 'icon'     => OIDplus::webpath(__DIR__).'treeicon_logout.png',
  213.                                 'conditionalselect' => 'adminLogout()', // defined in oidplus_base.js
  214.                                 'text'     => _L('Log out')
  215.                         );
  216.                         $loginChildren[] = array(
  217.                                 'id'       => 'oidplus:dummy$'.md5(rand()),
  218.                                 'text'     => _L("Logged in as admin"),
  219.                                 'icon'     => OIDplus::webpath(__DIR__).'treeicon_admin.png',
  220.                                 'conditionalselect' => 'false', // dummy node that can't be selected
  221.                                 'state'    => array("opened" => true),
  222.                                 'children' => $ra_roots
  223.                         );
  224.                 }
  225.  
  226.                 foreach (OIDplus::authUtils()::loggedInRaList() as $ra) {
  227.                         $ra_email = $ra->raEmail();
  228.                         $ra_roots = array();
  229.  
  230.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  231.                                 if (is_subclass_of($plugin, OIDplusPagePluginRa::class)) {
  232.                                         $plugin->tree($ra_roots, $ra_email);
  233.                                 }
  234.                         }
  235.  
  236.                         $ra_roots[] = array(
  237.                                 'id'       => 'oidplus:logout$'.$ra_email,
  238.                                 'conditionalselect' => 'raLogout('.js_escape($ra_email).')', // defined in oidplus_base.js
  239.                                 'icon'     => OIDplus::webpath(__DIR__).'treeicon_logout.png',
  240.                                 'text'     => _L('Log out')
  241.                         );
  242.                         foreach (OIDplusObject::getRaRoots($ra_email) as $loc_root) {
  243.                                 $ico = $loc_root->getIcon();
  244.                                 $ra_roots[] = array(
  245.                                         'id' => 'oidplus:raroot$'.$loc_root->nodeId(),
  246.                                         'text' => 'Jump to RA root '.$loc_root->objectTypeTitleShort().' '.$loc_root->crudShowId(OIDplusObject::parse($loc_root::root())),
  247.                                         'conditionalselect' => 'openOidInPanel('.js_escape($loc_root->nodeId()).', true);',
  248.                                         'icon' => !is_null($ico) ? $ico : OIDplus::webpath(__DIR__).'treeicon_link.png'
  249.                                 );
  250.                         }
  251.                         $ra_email_or_name = (new OIDplusRA($ra_email))->raName();
  252.                         if ($ra_email_or_name == '') $ra_email_or_name = $ra_email;
  253.                         $loginChildren[] = array(
  254.                                 'id'       => 'oidplus:dummy$'.md5(rand()),
  255.                                 'text'     => "Logged in as ".htmlentities($ra_email_or_name),
  256.                                 'icon'     => OIDplus::webpath(__DIR__).'treeicon_ra.png',
  257.                                 'conditionalselect' => 'false', // dummy node that can't be selected
  258.                                 'state'    => array("opened" => true),
  259.                                 'children' => $ra_roots
  260.                         );
  261.                 }
  262.  
  263.                 $json[] = array(
  264.                         'id'       => 'oidplus:login',
  265.                         'icon'     => OIDplus::webpath(__DIR__).'treeicon_login.png',
  266.                         'text'     => _L('Login'),
  267.                         'state'    => array("opened" => count($loginChildren)>0),
  268.                         'children' => $loginChildren
  269.                 );
  270.  
  271.                 return true;
  272.         }
  273.  
  274.         public function tree_search($request) {
  275.                 return false;
  276.         }
  277. }
  278.