Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
566 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 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('INSIDE_OIDPLUS')) die();
21
 
22
abstract class OIDplusAuthContentStore {
23
 
24
        protected $content = array();
25
 
26
        // Getter / Setter
27
 
567 daniel-mar 28
        protected abstract function getValue($name);
566 daniel-mar 29
 
567 daniel-mar 30
        protected abstract function setValue($name, $value);
566 daniel-mar 31
 
32
        protected abstract function destroySession();
33
 
34
        // RA authentication functions
35
 
36
        public function raLogin($email) {
37
                if (strpos($email, '|') !== false) return;
38
 
39
                $list = $this->getValue('oidplus_logged_in');
40
                if (is_null($list)) $list = '';
41
 
42
                $ary = ($list == '') ? array() : explode('|', $list);
43
                if (!in_array($email, $ary)) $ary[] = $email;
44
                $list = implode('|', $ary);
45
 
46
                $this->setValue('oidplus_logged_in', $list);
47
        }
48
 
49
        public function raLogout($email) {
50
                $list = $this->getValue('oidplus_logged_in');
51
                if (is_null($list)) $list = '';
52
 
53
                $ary = ($list == '') ? array() : explode('|', $list);
54
                $key = array_search($email, $ary);
55
                if ($key !== false) unset($ary[$key]);
56
                $list = implode('|', $ary);
57
 
58
                $this->setValue('oidplus_logged_in', $list);
59
 
60
                if (($list == '') && (!self::isAdminLoggedIn())) {
61
                        // Nobody logged in anymore. Destroy session cookie to make GDPR people happy
62
                        $this->destroySession();
63
                }
64
        }
65
 
66
        public function raNumLoggedIn() {
67
                return count(self::loggedInRaList());
68
        }
69
 
70
        public function raLogoutAll() {
71
                $this->setValue('oidplus_logged_in', '');
72
        }
73
 
74
        public function loggedInRaList() {
75
                $list = $this->getValue('oidplus_logged_in');
76
                if (is_null($list)) $list = '';
77
 
78
                $res = array();
79
                foreach (array_unique(explode('|',$list)) as $ra_email) {
80
                        if ($ra_email == '') continue;
81
                        $res[] = new OIDplusRA($ra_email);
82
                }
83
                return $res;
84
        }
85
 
86
        public function isRaLoggedIn($email) {
87
                foreach (self::loggedInRaList() as $ra) {
88
                        if ($email == $ra->raEmail()) return true;
89
                }
90
                return false;
91
        }
92
 
93
        // Admin authentication functions
94
 
95
        public function adminLogin() {
96
                $this->setValue('oidplus_admin_logged_in', '1');
97
        }
98
 
99
        public function adminLogout() {
100
                $this->setValue('oidplus_admin_logged_in', '0');
101
 
102
                if (self::raNumLoggedIn() == 0) {
103
                        // Nobody logged in anymore. Destroy session cookie to make GDPR people happy
104
                        $this->destroySession();
105
                }
106
        }
107
 
108
        public function isAdminLoggedIn() {
109
                return $this->getValue('oidplus_admin_logged_in') == '1';
110
        }
111
 
112
}