Subversion Repositories oidplus

Rev

Rev 115 | Rev 220 | 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
        }
32
 
2 daniel-mar 33
        public function raName() {
150 daniel-mar 34
                $res = OIDplus::db()->query("select ra_name from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($this->email));
2 daniel-mar 35
                if (OIDplus::db()->num_rows($res) == 0) return "(RA not in database)";
36
                $row = OIDplus::db()->fetch_array($res);
37
                return $row['ra_name'];
38
        }
39
 
115 daniel-mar 40
        public static function getAllRAs() {
41
                $out = array();
42
                $res = OIDplus::db()->query("select email from ".OIDPLUS_TABLENAME_PREFIX."ra");
43
                while ($row = OIDplus::db()->fetch_array($res)) {
44
                        $out[] = new OIDplusRA($row['email']);
45
                }
46
                return $out;
47
        }
48
 
49
        public function change_password($new_password) {
2 daniel-mar 50
                $s_salt = substr(md5(rand()), 0, 7);
51
                $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));
150 daniel-mar 52
                if (!OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."ra set salt=?, authkey=? where email = ?", array($s_salt, $calc_authkey, $this->email))) {
2 daniel-mar 53
                        throw new Exception(OIDplus::db()->error());
54
                }
55
        }
56
 
115 daniel-mar 57
        public function change_email($new_email) {
150 daniel-mar 58
                if (!OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."ra set email = ? where email = ?", array($new_email, $this->email))) {
44 daniel-mar 59
                        throw new Exception(OIDplus::db()->error());
60
                }
61
        }
62
 
115 daniel-mar 63
        public function register_ra($new_password) {
2 daniel-mar 64
                $s_salt = substr(md5(rand()), 0, 7);
65
                $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));
150 daniel-mar 66
                if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."ra (salt, authkey, email, registered) values (?, ?, ?, now())", array($s_salt, $calc_authkey, $this->email))) {
2 daniel-mar 67
                        throw new Exception(OIDplus::db()->error());
68
                }
69
        }
70
 
115 daniel-mar 71
        public function checkPassword($password) {
150 daniel-mar 72
                $ra_res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($this->email));
2 daniel-mar 73
                $ra_row = OIDplus::db()->fetch_array($ra_res);
74
                $s_salt = $ra_row['salt'];
75
                @list($s_authmethod, $s_authkey) = explode('#', $ra_row['authkey'], 2);
76
 
77
                if ($s_authmethod == 'A1') {
78
                        // Downwards compatibility for ViaThinkSoft FreeOID
79
                        $calc_authkey = sha1('asdlkgfdklgnklsdlkans'.$s_salt.$password);
80
                } else if ($s_authmethod == 'A2') {
81
                        $calc_authkey = base64_encode(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $s_salt.$password, true) : bb\Sha3\Sha3::hash($s_salt.$password, 512, true));
82
                } else {
83
                        // Invalid auth code
84
                        return false;
85
                }
86
 
87
                return hash_equals($calc_authkey, $s_authkey);
88
        }
89
 
115 daniel-mar 90
        public function delete() {
150 daniel-mar 91
                if (!OIDplus::db()->query("delete from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($this->email))) {
2 daniel-mar 92
                        throw new Exception(OIDplus::db()->error());
93
                }
94
        }
95
 
115 daniel-mar 96
        public function setRaName($ra_name) {
150 daniel-mar 97
                if (!OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."ra set ra_name = ? where email = ?", array($ra_name, $this->email))) {
2 daniel-mar 98
                        throw new Exception(OIDplus::db()->error());
99
                }
100
        }
101
}