-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.php
244 lines (230 loc) · 5.82 KB
/
auth.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
<?php
/**
Authentication plugin for the PHP Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2009-2012 F3::Factory
Bong Cosca <[email protected]>
@package Auth
@version 2.0.12
**/
//! Plugin for various user authentication methods
class Auth extends Base {
//@{ Locale-specific error/exception messages
const
TEXT_AuthSetup='Invalid AUTH variable configuration',
TEXT_IMAPConnect='Unable to connect to IMAP server %s',
TEXT_LDAPConnect='Unable to connect to LDAP server %s',
TEXT_LDAPBind='LDAP bind failure';
//@}
/**
Authenticate against SQL database;
AUTH global array elements:
db:<database-id> (default:'DB'),
table:<table-name>,
id:<userID-field>,
pw:<password-field>
@return mixed
@param $id string
@param $pw string
@public
**/
static function sql($id,$pw) {
$auth=&self::$vars['AUTH'];
foreach (array('table','id','pw') as $param)
if (!isset($auth[$param])) {
trigger_error(self::TEXT_AuthSetup);
return FALSE;
}
if (!isset($auth['db']))
$auth['db']=self::ref('DB');
$axon=new Axon($auth['table'],self::ref('AUTH.db'));
$axon->load(
array(
self::ref('AUTH.id').'=:id AND '.
self::ref('AUTH.pw').'=:pw',
array(':id'=>$id,':pw'=>$pw)
)
);
return $axon->dry()?FALSE:$axon;
}
/**
Authenticate against NoSQL database (MongoDB);
AUTH global array elements:
db:<database-id> (default:'DB'),
collection:<collection-name>,
id:<userID-field>,
pw:<password-field>
@return mixed
@param $id string
@param $pw string
@public
**/
static function nosql($id,$pw) {
$auth=&self::$vars['AUTH'];
foreach (array('collection','id','pw') as $param)
if (!isset($auth[$param])) {
trigger_error(self::TEXT_AuthSetup);
return FALSE;
}
if (!isset($auth['db']))
$auth['db']=self::ref('DB');
$m2=new M2($auth['collection'],self::ref('AUTH.db'));
$m2->load(
array(
self::ref('AUTH.id')=>$id,
self::ref('AUTH.pw')=>$pw
)
);
return $m2->dry()?FALSE:$m2;
}
/**
Authenticate against Jig-mapped flat-file database;
AUTH global array elements:
db:<database-id> (default:'DB'),
table:<table-name>,
id:<userID-field>,
pw:<password-field>
@return mixed
@param $id string
@param $pw string
@public
**/
static function jig($id,$pw) {
$auth=&self::$vars['AUTH'];
foreach (array('table','id','pw') as $param)
if (!isset($auth[$param])) {
trigger_error(self::TEXT_AuthSetup);
return FALSE;
}
if (!isset($auth['db']))
$auth['db']=self::ref('DB');
$jig=new Jig($auth['table'],self::ref('AUTH.db'));
$jig->load(
array(
self::ref('AUTH.id')=>$id,
self::ref('AUTH.pw')=>$pw
)
);
return $jig->dry()?FALSE:$jig;
}
/**
Authenticate against IMAP server;
AUTH global array elements:
server:<IMAP-server>,
port:<TCP-port> (default:143)
@return boolean
@param $id string
@param $pw string
@public
**/
static function imap($id,$pw) {
// IMAP extension required
if (!extension_loaded('imap')) {
// Unable to continue
trigger_error(sprintf(self::TEXT_PHPExt,'imap'));
return;
}
$auth=self::$vars['AUTH'];
if (!isset($auth['server'])) {
trigger_error(self::TEXT_AuthSetup);
return FALSE;
}
if (!isset($auth['port']))
$auth['port']=143;
$ic=@fsockopen($auth['server'],$auth['port']);
if (!is_resource($ic)) {
// Connection failed
trigger_error(sprintf(self::TEXT_IMAPConnect,$auth['server']));
return FALSE;
}
$ibox='{'.$auth['server'].':'.$auth['port'].'}INBOX';
$mbox=@imap_open($ibox,$id,$pw);
$ok=is_resource($mbox);
if (!$ok) {
$mbox=@imap_open($ibox,$id.'@'.$auth['server'],$pw);
$ok=is_resource($mbox);
}
imap_close($mbox);
return $ok;
}
/**
Authenticate via LDAP;
AUTH global array elements:
dc:<domain-controller>,
rdn:<connection-DN>,
pw:<connection-password>
@return boolean
@param $id string
@param $pw string
@public
**/
static function ldap($id,$pw) {
// LDAP extension required
if (!extension_loaded('ldap')) {
// Unable to continue
trigger_error(sprintf(self::TEXT_PHPExt,'ldap'));
return;
}
$auth=self::$vars['AUTH'];
if (!isset($auth['dc'])) {
trigger_error(self::TEXT_AuthSetup);
return FALSE;
}
$dc=@ldap_connect($auth['dc']);
if (!$dc) {
// Connection failed
trigger_error(sprintf(self::TEXT_LDAPConnect,$auth['dc']));
return FALSE;
}
ldap_set_option($dc,LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($dc,LDAP_OPT_REFERRALS,0);
if (!@ldap_bind($dc,$auth['rdn'],$auth['pw'])) {
// Bind failed
trigger_error(self::TEXT_LDAPBind);
return FALSE;
}
$result=ldap_search($dc,$auth['base_dn'],'uid='.$id);
if (ldap_count_entries($dc,$result)!=1)
// Didn't return a single record
return FALSE;
// Bind using credentials
$info=ldap_get_entries($dc,$result);
if (!@ldap_bind($dc,$info[0]['dn'],$pw))
// Bind failed
return FALSE;
@ldap_unbind($dc);
// Verify user ID
return $info[0]['uid'][0]==$id;
}
/**
Basic HTTP authentication
@return boolean
@param $auth mixed
@param $realm string
@public
**/
static function basic($auth,$realm=NULL) {
if (is_null($realm))
$realm=$_SERVER['REQUEST_URI'];
if (isset($_SERVER['PHP_AUTH_USER']))
return call_user_func(
array('self',$auth),
$_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']
);
if (PHP_SAPI!='cli')
header(self::HTTP_WebAuth.': Basic realm="'.$realm.'"',TRUE,401);
return FALSE;
}
/**
Class initializer
@public
**/
static function onload() {
if (!isset(self::$vars['AUTH']))
// Authentication setup options
self::$vars['AUTH']=NULL;
}
}