summaryrefslogtreecommitdiff
path: root/BaseAuth.php
blob: e8e12afa1f23b89ed458817aca327e39604b6d6e (plain)
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
<?php
global $gBitUser;

class BaseAuth {
	var $mLogin;
	var $mConfig;
	var $mInfo;
	var $mCfg;
	var $mErrors =array();

	static $mAuthMethod;

	function BaseAuth($authId) {
		global $gBitSystem;
		global $gBitUser;
		$this->mCfg = BaseAuth::$mAuthMethod[$authId];
		$this->mCfg['auth_id'] = $authId;
		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;
		}
	}

	function register($id,$hash) {
		if (!function_exists('preFlightWarning')) {
			function preFlightWarning($str) {
				?><div style="background: white; z-index: 50000; margin: 0em; padding: 1px; color: red; text-align: center;"">
				<h1>
					<img src="<?php echo LIBERTY_PKG_URL; ?>/icons/warning.png" alt="Warning" />
					<?php echo $str; ?>
					<img src="<?php echo LIBERTY_PKG_URL; ?>/icons/warning.png" alt="Warning" />
				</h1>
				</div><?php
			}
		}
		global $gBitSystem;
		$err = false;
		if (! empty(BaseAuth::$mAuthMethod[$id])) {
			preFlightWarning("Auth Registration Failed: $id already registered");
			$err = true;
		}
		if (empty($hash['name'])) {
			preFlightWarning("Auth Registration Failed: $id: No Name given");
			$err = true;
		}
		if (empty($hash['file'])) {
			preFlightWarning("Auth Registration Failed: $id: No file given");
			$err = true;
		}elseif(!file_exists($hash['file'])) {
			preFlightWarning("Auth Registration Failed: $id: File (".basename($hash['file']).") doesn't exist");
			$err = true;
		}
		if (empty($hash['class'])) {
			preFlightWarning("Auth Registration Failed: $id: No class given");
			$err = true;
		}
		if (!$err) {
			BaseAuth::$mAuthMethod[$id]=$hash;
		}
	}

	function getAuthMethodCount() {
		return count(BaseAuth::$mAuthMethod);
	}

	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;
		global $gBitUser;
		if (empty($package) && !empty($this->mCfg['auth_id'])) {
			$package = $this->mCfg['auth_id'];
		}
		for ($i=0;$i<count($gBitUser->mAuthMethod);$i++) {
			$default="";
			if ($i==0) {
				$default="bit";
			}
			if ($gBitSystem->getConfig("users_auth_method_$i",$default)== $package) {
				return true;
			}
		}
		return false;
	}

	function init($authId) {
		global $gBitUser;
		global $gBitSystem;
		if (is_numeric($authId)) {
			$default="";
			if ($authId==0) {
				$default="tiki";
			}
			$method_name=$gBitSystem->getConfig("users_auth_method_$authId",$default);
			if (!empty($method_name)) {
				return BaseAuth::init($method_name);
			}
		} elseif (!empty($authId)) {
			$method=BaseAuth::$mAuthMethod[$authId];
			require_once($method['file']);
			$cl = $method['class'];
			$instance = new $cl();
			if ($instance->isSupported()) {
				return $instance;
			}
		}
		return false;
	}

	function settings() {
		global $gBitSystem;
		global $gBitUser;
		global $gBitSmarty;
		$authSettings = array();
		foreach( BaseAuth::$mAuthMethod 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;
			} else {
				$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);
		$gBitSmarty->assign_by_ref( 'authSettings',  $authSettings);
	}
}
?>