Subversion Repositories oidplus

Rev

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