forked from BitweaverCMS/users
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseAuth.php
250 lines (228 loc) · 6.57 KB
/
BaseAuth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* $Header$
*
* @package users
*/
/**
* required setup
*/
require_once(UTIL_PKG_PATH . "PHP_Compat/Compat/Function/scandir.php");
/**
* Class that manages the base autentication method
*
* @package users
* @subpackage auth
*/
class BaseAuth {
var $mLogin;
var $mConfig;
var $mInfo;
var $mCfg;
var $mErrors =array();
function __construct($pAuthId) {
global $gBitSystem;
$this->mCfg = BaseAuth::getAuthMethod($pAuthId);
$this->mCfg['auth_id'] = $pAuthId;
foreach ($this->getSettings() as $op_id => $op) {
$var_id = substr($op_id,strrpos($op_id,"_")+1);
$var = $gBitSystem->getConfig($op_id, $op['default']);
if ($op['type']=="checkbox") {
$var = ($var== "y");
}
$this->mConfig[$var_id]=$var;
}
}
public static function &getAuthMethods() {
static $authMethod = array();
static $scaned = false;
if (!$scaned) {
$scaned = true;
BaseAuth::scanAuthPlugins();
}
return $authMethod;
}
function getAuthMethod( $pAuthId ) {
$authMethod =& BaseAuth::getAuthMethods();
if (empty($authMethod[$pAuthId])) return null;
return $authMethod[$pAuthId];
}
function setAuthMethod($pAuthId,&$method) {
$authMethod =& BaseAuth::getAuthMethods();
$authMethod[$pAuthId]=$method;
}
function scanAuthPlugins() {
global $gBitSystem;
$authDir = $gBitSystem->getConfig( 'users_auth_plugins_dir', USERS_PKG_PATH.'auth/' );
if( is_dir( $authDir ) && $authScan = scandir( $authDir ) ) {
foreach( $authScan as $plugDir ) {
if( $plugDir != 'CVS' && substr($plugDir,0,1)!='.' && is_dir( $authDir.$plugDir ) ) {
BaseAuth::register( $plugDir,array(
'name' => strtoupper( $plugDir ).' Auth',
'file' => $authDir.$plugDir.'/auth.php',
'class' => ucfirst( $plugDir ).'Auth',
) );
}
}
}
}
function register($id,$hash) {
global $gBitSystem;
$err = false;
$method = BaseAuth::getAuthMethod($id);
if (! empty($method)) {
BaseAuth::authError("Auth Registration Failed: $id already registered");
$err = true;
}
if (empty($hash['name'])) {
BaseAuth::authError("Auth Registration Failed: $id: No Name given");
$err = true;
}
if (empty($hash['file'])) {
BaseAuth::authError("Auth Registration Failed: $id: No file given");
$err = true;
} elseif(!file_exists($hash['file'])) {
BaseAuth::authError("Auth Registration Failed: $id: File (".basename($hash['file']).") doesn't exist");
$err = true;
}
if (empty($hash['class'])) {
BaseAuth::authError("Auth Registration Failed: $id: No class given");
$err = true;
}
if (!$err) {
BaseAuth::setAuthMethod($id,$hash);
}
}
function authError($str) {
$warning = '<div class="error">'.$str.'</div>';
print( $warning );
}
public static function getAuthMethodCount() {
$methods = BaseAuth::getAuthMethods();
if (empty($methods)) return 0;
return count($methods);
}
function validate($user,$pass,$challenge,$response) {
if (!$this->isSupported()) return false;
$this->mLogin = $user;
$this->mInfo['login']=$user;
$this->mInfo['password']=$pass;
}
function getUserData() {
return $this->mInfo;
}
function isSupported() {
$this->mErrors[] = "BaseAuth is not an authentcation method";
return false;
}
function createUser(&$userattr) {
$this->mErrors[] = "BaseAuth is not an authentcation method";
return false;
}
function getSettings() {
return array();
}
function canManageAuth() {
$this->mErrors[] = "BaseAuth is not an authentcation method";
return false;
}
function getRegistrationFields() {
return array();
}
function isActive($package = '') {
global $gBitSystem;
if (empty($package) && !empty($this->mCfg['auth_id'])) {
$package = $this->mCfg['auth_id'];
}
for ($i=0;$i<BaseAuth::getAuthMethodCount();$i++) {
$default="";
if ( $i==0 ) {
$default="bit";
}
if ($gBitSystem->getConfig("users_auth_method_$i",$default)== $package) {
return true;
}
}
return false;
}
function init( $pAuthMixed ) {
global $gBitSystem;
if( is_numeric( $pAuthMixed ) ) {
$default="";
if ($pAuthMixed==0) {
$default="bit";
}
$authPlugin = $gBitSystem->getConfig("users_auth_method_$pAuthMixed",$default);
if (!empty( $authPlugin ) ) {
return BaseAuth::init( $authPlugin );
}
} elseif (!empty($pAuthMixed)) {
$authPlugin=BaseAuth::getAuthMethod( $pAuthMixed );
if (file_exists( $authPlugin['file'] )) {
require_once( $authPlugin['file'] );
$cl = $authPlugin['class'];
$instance = new $cl();
if( $instance->isSupported() ) {
return $instance;
}
}
}
return false;
}
function getConfig() {
global $gBitSystem;
$authSettings = array();
foreach( BaseAuth::getAuthMethods() as $meth_name => $method ) {
$instance = BaseAuth::init($meth_name) ;
if ($instance) {
foreach ($instance->getSettings() as $op_id => $op) {
if (!empty($_REQUEST[$op_id])) {
if( $op['type'] == 'checkbox' ) {
simple_set_toggle( $op_id, USERS_PKG_NAME );
} else {
simple_set_value( $op_id, USERS_PKG_NAME );
}
}
$value = $gBitSystem->getConfig($op_id, $op['default']);
$op['value']=$value;
$method['options'][$op_id] = $op;
}
$method['canManageAuth'] = $instance->canManageAuth();
$authSettings['avail'][$meth_name]=$method;
} elseif( is_object( $instance ) ) {
$authSettings['err'][$meth_name]=implode("<br />",$instance->mErrors);
}
}
if (!empty($_REQUEST["loginprefs"])) {
$used =array();
for ($i=0,$j=0;$i<count($authSettings['avail']);$i++,$j++) {
$gBitSystem->storeConfig( "users_auth_method_$i",null, USERS_PKG_NAME );
if (empty($_REQUEST["users_auth_method_$i"])) {
$j--;
} elseif(!empty($used[$_REQUEST["users_auth_method_$i"]])) {
$j--;
} else {
$used[$_REQUEST["users_auth_method_$i"]]="stored_$j";
$gBitSystem->storeConfig( "users_auth_method_$j", $_REQUEST["users_auth_method_$i"], USERS_PKG_NAME );
}
}
}
$canManageAuth = false;
for ($i=0;$i<count($authSettings['avail']);$i++) {
$default="";
if ($i==0) {
$default="bit";
}
$authSettings['avail_method'][$i]['value']=$gBitSystem->getConfig("users_auth_method_$i",$default);
if (!$canManageAuth&&!empty($authSettings['avail_method'][$i]['value'])) {
$canManageAuth = $authSettings['avail'][$authSettings['avail_method'][$i]['value']]['canManageAuth'];
}
}
if (($gBitSystem->getConfig('users_allow_register','y')=='y')&&!$canManageAuth) {
$authSettings['err']['bit_reg']="Registration is enabled but there are no Auth Methods that support this, Registration won't work!";
}
$method['active']=BaseAuth::isActive($meth_name);
return $authSettings;
}
}
?>