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 OIDplusPageRaChangePassword extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'ra';
  25.         }
  26.  
  27.         public function priority() {
  28.                 return 101;
  29.         }
  30.  
  31.         public function action(&$handled) {
  32.                 if (isset($_POST["action"]) && ($_POST["action"] == "change_ra_password")) {
  33.                         $handled = true;
  34.  
  35.                         $email = $_POST['email'];
  36.  
  37.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($email));
  38.                         if (OIDplus::db()->num_rows($res) == 0) {
  39.                                 die(json_encode(array("error" => 'RA does not exist')));
  40.                         }
  41.  
  42.                         if (!OIDplus::authUtils()::isRaLoggedIn($email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  43.                                 die(json_encode(array("error" => 'Authentification error. Please log in as the RA to update its data.')));
  44.                         }
  45.  
  46.                         $old_password = $_POST['old_password'];
  47.                         $password1 = $_POST['new_password1'];
  48.                         $password2 = $_POST['new_password2'];
  49.  
  50.                         if ($password1 !== $password2) {
  51.                                 die(json_encode(array("error" => 'Passwords are not equal')));
  52.                         }
  53.  
  54.                         if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
  55.                                 die(json_encode(array("error" => 'New password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength())));
  56.                         }
  57.  
  58.                         $ra = new OIDplusRA($email);
  59.                         if (!$ra->checkPassword($old_password)) {
  60.                                 die(json_encode(array("error" => 'Old password incorrect')));
  61.                         }
  62.                         OIDplus::logger()->log("RA($email)?/A?", "Password of RA '$email' changed");
  63.                         $ra->change_password($password1);
  64.  
  65.                         echo json_encode(array("status" => 0));
  66.                 }
  67.         }
  68.  
  69.         public function init($html=true) {
  70.                 // Nothing
  71.         }
  72.  
  73.         public function cfgSetValue($name, $value) {
  74.                 // Nothing
  75.         }
  76.  
  77.         public function gui($id, &$out, &$handled) {
  78.                 if (explode('$',$id)[0] == 'oidplus:change_ra_password') {
  79.                         $handled = true;
  80.                         $out['title'] = 'Change RA password';
  81.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
  82.  
  83.                         $ra_email = explode('$',$id)[1];
  84.  
  85.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($ra_email));
  86.                         if (OIDplus::db()->num_rows($res) == 0) {
  87.                                 $out['icon'] = 'img/error_big.png';
  88.                                 $out['text'] = 'RA <b>'.htmlentities($ra_email).'</b> does not exist';
  89.                                 return $out;
  90.                         }
  91.  
  92.                         if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  93.                                 $out['icon'] = 'img/error_big.png';
  94.                                 $out['text'] .= '<p>You need to <a '.oidplus_link('oidplus:login').'>log in</a> as the requested RA <b>'.htmlentities($ra_email).'</b>.</p>';
  95.                         } else {
  96.                                 $out['text'] .= '<form id="raChangePasswordForm" onsubmit="return raChangePasswordFormOnSubmit();">';
  97.                                 $out['text'] .= '<input type="hidden" id="email" value="'.htmlentities($ra_email).'"/><br>';
  98.                                 $out['text'] .= '<label class="padding_label">Old password:</label><input type="password" id="old_password" value=""/><br>';
  99.                                 $out['text'] .= '<label class="padding_label">New password:</label><input type="password" id="new_password1" value=""/><br>';
  100.                                 $out['text'] .= '<label class="padding_label">Again:</label><input type="password" id="new_password2" value=""/><br><br>';
  101.                                 $out['text'] .= '<input type="submit" value="Change password"></form>';
  102.                         }
  103.                 }
  104.         }
  105.  
  106.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  107.                 if (file_exists(__DIR__.'/treeicon.png')) {
  108.                         $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
  109.                 } else {
  110.                         $tree_icon = null; // default icon (folder)
  111.                 }
  112.  
  113.                 $json[] = array(
  114.                         'id' => 'oidplus:change_ra_password$'.$ra_email,
  115.                         'icon' => $tree_icon,
  116.                         'text' => 'Change password'
  117.                 );
  118.  
  119.                 return true;
  120.         }
  121.  
  122.         public function tree_search($request) {
  123.                 return false;
  124.         }
  125. }
  126.  
  127. OIDplus::registerPagePlugin(new OIDplusPageRaChangePassword());
  128.