Subversion Repositories oidplus

Rev

Rev 253 | Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 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
 
112 daniel-mar 20
if (!defined('IN_OIDPLUS')) die();
21
 
2 daniel-mar 22
class OIDplusRA {
23
        private $email = null;
24
 
115 daniel-mar 25
        public function __construct($email) {
2 daniel-mar 26
                $this->email = $email;
27
        }
28
 
115 daniel-mar 29
        public function raEmail() {
30
                return $this->email;
31
        }
253 daniel-mar 32
 
33
        public function existing() {
261 daniel-mar 34
                $res = OIDplus::db()->query("select email from ###ra where email = ?", array($this->email));
253 daniel-mar 35
                return ($res->num_rows() > 0);
36
        }
115 daniel-mar 37
 
2 daniel-mar 38
        public function raName() {
261 daniel-mar 39
                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($this->email));
236 daniel-mar 40
                if ($res->num_rows() == 0) return "(RA not in database)";
41
                $row = $res->fetch_array();
2 daniel-mar 42
                return $row['ra_name'];
43
        }
44
 
115 daniel-mar 45
        public static function getAllRAs() {
46
                $out = array();
261 daniel-mar 47
                $res = OIDplus::db()->query("select email from ###ra");
236 daniel-mar 48
                while ($row = $res->fetch_array()) {
115 daniel-mar 49
                        $out[] = new OIDplusRA($row['email']);
50
                }
51
                return $out;
52
        }
53
 
54
        public function change_password($new_password) {
2 daniel-mar 55
                $s_salt = substr(md5(rand()), 0, 7);
56
                $calc_authkey = 'A2#'.base64_encode(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $s_salt.$new_password, true) : bb\Sha3\Sha3::hash($s_salt.$new_password, 512, true));
261 daniel-mar 57
                OIDplus::db()->query("update ###ra set salt=?, authkey=? where email = ?", array($s_salt, $calc_authkey, $this->email));
2 daniel-mar 58
        }
59
 
115 daniel-mar 60
        public function change_email($new_email) {
261 daniel-mar 61
                OIDplus::db()->query("update ###ra set email = ? where email = ?", array($new_email, $this->email));
44 daniel-mar 62
        }
63
 
115 daniel-mar 64
        public function register_ra($new_password) {
2 daniel-mar 65
                $s_salt = substr(md5(rand()), 0, 7);
66
                $calc_authkey = 'A2#'.base64_encode(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $s_salt.$new_password, true) : bb\Sha3\Sha3::hash($s_salt.$new_password, 512, true));
239 daniel-mar 67
                if (OIDplus::db()->slang() == 'mssql') {
261 daniel-mar 68
                        OIDplus::db()->query("insert into ###ra (salt, authkey, email, registered) values (?, ?, ?, getdate())", array($s_salt, $calc_authkey, $this->email));
239 daniel-mar 69
                } else {
70
                        // MySQL + PgSQL
261 daniel-mar 71
                        OIDplus::db()->query("insert into ###ra (salt, authkey, email, registered) values (?, ?, ?, now())", array($s_salt, $calc_authkey, $this->email));
239 daniel-mar 72
                }
2 daniel-mar 73
        }
74
 
115 daniel-mar 75
        public function checkPassword($password) {
261 daniel-mar 76
                $ra_res = OIDplus::db()->query("select authkey, salt from ###ra where email = ?", array($this->email));
251 daniel-mar 77
                if ($ra_res->num_rows() == 0) return false; // User not found
236 daniel-mar 78
                $ra_row = $ra_res->fetch_array();
2 daniel-mar 79
 
222 daniel-mar 80
                $plugins = OIDplus::getAuthPlugins();
81
                if (count($plugins) == 0) {
250 daniel-mar 82
                        throw new OIDplusException("No RA authentication plugins found");
222 daniel-mar 83
                }
84
                foreach ($plugins as $plugin) {
221 daniel-mar 85
                        if ($plugin->verify($ra_row['authkey'], $ra_row['salt'], $password)) return true;
2 daniel-mar 86
                }
87
 
221 daniel-mar 88
                return false;
2 daniel-mar 89
        }
90
 
115 daniel-mar 91
        public function delete() {
261 daniel-mar 92
                OIDplus::db()->query("delete from ###ra where email = ?", array($this->email));
2 daniel-mar 93
        }
94
 
115 daniel-mar 95
        public function setRaName($ra_name) {
261 daniel-mar 96
                OIDplus::db()->query("update ###ra set ra_name = ? where email = ?", array($ra_name, $this->email));
2 daniel-mar 97
        }
98
}