-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternal.php
More file actions
405 lines (365 loc) · 12 KB
/
Copy pathExternal.php
File metadata and controls
405 lines (365 loc) · 12 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* @brief External Database Login Handler
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 18 Mar 2013
* @version SVN_VERSION_NUMBER
*/
namespace IPS\Login;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* External Database Login Handler
*/
class _External extends LoginAbstract
{
/**
* @brief Authentication types
*/
public $authTypes;
/**
* Initiate
*
* @return void
*/
public function init()
{
$this->authTypes = $this->settings['auth_types'] ?: \IPS\Login::AUTH_TYPE_USERNAME;
}
/**
* Authenticate
*
* @param array $values Values from from
* @return \IPS\Member
* @throws \IPS\Login\Exception
*/
public function authenticate( $values )
{
/* Build where clause */
switch ( $this->authTypes )
{
case \IPS\Login::AUTH_TYPE_USERNAME:
$where = array( "{$this->settings['db_col_user']}=?", $values['auth'] );
break;
case \IPS\Login::AUTH_TYPE_EMAIL:
$where = array( "{$this->settings['db_col_email']}=?", $values['auth'] );
break;
case \IPS\Login::AUTH_TYPE_USERNAME + \IPS\Login::AUTH_TYPE_EMAIL:
$where = array( "{$this->settings['db_col_user']}=? OR {$this->settings['db_col_email']}=?", $values['auth'], $values['auth'] );
break;
}
if ( $this->settings['db_extra'] )
{
$where[] = array( $this->settings['db_extra'] );
}
/* Fetch result */
try
{
$result = $this->externalDb()->select( '*', $this->settings['db_table'], $where )->first();
}
catch ( \IPS\Db\Exception $e )
{
throw new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::INTERNAL_ERROR );
}
catch ( \UnderflowException $e )
{
switch ( $this->authTypes )
{
case \IPS\Login::AUTH_TYPE_USERNAME + \IPS\Login::AUTH_TYPE_EMAIL:
$type = 'username_or_email';
break;
case \IPS\Login::AUTH_TYPE_USERNAME:
$type = 'username';
break;
case \IPS\Login::AUTH_TYPE_EMAIL:
$type = 'email_address';
break;
}
throw new \IPS\Login\Exception( \IPS\Member::loggedIn()->language()->addToStack( 'login_err_no_account', FALSE, array( 'sprintf' => array( \IPS\Member::loggedIn()->language()->addToStack( $type ) ) ) ), \IPS\Login\Exception::NO_ACCOUNT );
}
/* Get a local account if one exists */
$member = NULL;
if ( $this->settings['db_col_user'] )
{
$_member = \IPS\Member::load( $result[ $this->settings['db_col_user'] ], 'name' );
if ( $_member->member_id )
{
$member = $_member;
}
}
if ( $this->settings['db_col_email'] )
{
$_member = \IPS\Member::load( $result[ $this->settings['db_col_email'] ], 'email' );
if ( $_member->member_id )
{
$member = $_member;
}
}
/* If the password doesn't match, throw an exception */
if(!password_verify($values['password'], $result[ $this->settings['db_col_pass'] ] ) ) {
throw new \IPS\Login\Exception( 'login_err_bad_password', \IPS\Login\Exception::BAD_PASSWORD, NULL, $member );
}
/* Or, create a member */
if ( $member === NULL )
{
$member = $this->createOrUpdateAccount( NULL, array(), $this->settings['db_col_user'] ? $result[ $this->settings['db_col_user'] ] : NULL, $this->settings['db_col_email'] ? $result[ $this->settings['db_col_email'] ] : NULL );
}
/* Return */
return $member;
}
/**
* Encrypt Password
*
* @param string $password The password
* @return bool
*/
protected function encryptedPassword( $password )
{
switch ( $this->settings['db_encryption'] )
{
case 'md5':
return md5(md5( $password ));
case 'sha1':
return sha1( $password );
case 'sha512':
return hash("sha512", $password );
case 'CRYPT_BLOWFISH':
return password_hash($password, PASSWORD_DEFAULT);
# return crypt($password, '$2y$10$');
default:
return $password;
}
}
/**
* ACP Settings Form
*
* @param string $url URL to redirect user to after successful submission
* @return array List of settings to save - settings will be stored to core_login_handlers.login_settings DB field
* @code
return array( 'savekey' => new \IPS\Helpers\Form\[Type]( ... ), ... );
* @endcode
*/
public function acpForm()
{
return array(
'login_external_conn',
'sql_host' => new \IPS\Helpers\Form\Text( 'login_external_host', $this->settings['sql_host'] ?: 'localhost', TRUE ),
'sql_user' => new \IPS\Helpers\Form\Text( 'login_external_user', $this->settings['sql_user'], TRUE ),
'sql_pass' => new \IPS\Helpers\Form\Text( 'login_external_pass', $this->settings['sql_pass'], TRUE ),
'sql_database' => new \IPS\Helpers\Form\Text( 'login_external_database', $this->settings['sql_database'], TRUE ),
'sql_port' => new \IPS\Helpers\Form\Number( 'login_external_port', $this->settings['sql_port'], FALSE ),
'sql_socket' => new \IPS\Helpers\Form\Text( 'login_external_socket', $this->settings['sql_socket'], FALSE ),
'login_external_schema',
'db_table' => new \IPS\Helpers\Form\Text( 'login_external_table', $this->settings['db_table'], TRUE ),
'db_col_user' => new \IPS\Helpers\Form\Text( 'login_external_username', $this->settings['db_col_user'], FALSE, array(), function( $val )
{
if ( !$val and \IPS\Request::i()->login_auth_types & \IPS\Login::AUTH_TYPE_USERNAME )
{
throw new \DomainException('login_external_username_err');
}
} ),
'db_col_email' => new \IPS\Helpers\Form\Text( 'login_external_email', $this->settings['db_col_email'], FALSE, array(), function( $val )
{
if ( !$val and \IPS\Request::i()->login_auth_types & \IPS\Login::AUTH_TYPE_EMAIL )
{
throw new \DomainException('login_external_email_err');
}
} ),
'db_col_pass' => new \IPS\Helpers\Form\Text( 'login_external_password', $this->settings['db_col_pass'], TRUE ),
'db_encryption' => new \IPS\Helpers\Form\Select( 'login_external_encryption', $this->settings['db_encryption'], TRUE, array( 'options' => array(
'md5' => 'MD5',
'sha1' => 'SHA1',
'sha512' => 'SHA512',
'CRYPT_BLOWFISH' => 'CRYPT_BLOWFISH',
'plaintext' => 'login_external_encryption_plain',
) ) ),
'db_extra' => new \IPS\Helpers\Form\Text( 'login_external_extra', isset( $this->settings['db_extra'] ) ? $this->settings['db_extra'] : '' ),
'login_settings',
'auth_types' => new \IPS\Helpers\Form\Select( 'login_auth_types', $this->settings['auth_types'], TRUE, array( 'options' => array(
\IPS\Login::AUTH_TYPE_USERNAME => 'username',
\IPS\Login::AUTH_TYPE_EMAIL => 'email_address',
\IPS\Login::AUTH_TYPE_USERNAME + \IPS\Login::AUTH_TYPE_EMAIL => 'username_or_email',
) ) ),
);
}
/**
* Test Settings
*
* @return bool
* @throws \IPS\Db\Exception
*/
public function testSettings()
{
$select = array( $this->settings['db_col_pass'] );
if ( $this->settings['db_col_user'] )
{
$select[] = $this->settings['db_col_user'];
}
if ( $this->settings['db_col_email'] )
{
$select[] = $this->settings['db_col_email'];
}
try
{
$result = $this->externalDb()->select( implode( ',', $select ), $this->settings['db_table'], ( isset( $this->settings['db_extra'] ) AND $this->settings['db_extra'] != '' ) ? array( $this->settings['db_extra'] ) : NULL )->first();
}
catch ( \UnderflowException $e )
{
// It's possible that no users exist, which is fine
}
return TRUE;
}
/**
* Get DB Connection
*
* @return bool
* @throws \IPS\Db\Exception
*/
protected function externalDb()
{
return \IPS\Db::i( 'external_login', $this->settings );
}
/**
* Can a member sign in with this login handler?
* Used to ensure when a user disassociates a social login that they have some other way of logging in
*
* @param \IPS\Member $member The member
* @return bool
*/
public function canProcess( \IPS\Member $member )
{
if ( $this->authTypes & \IPS\Login::AUTH_TYPE_USERNAME and $member->name and $this->usernameIsInUse( $member->name ) )
{
return TRUE;
}
if ( $this->authTypes & \IPS\Login::AUTH_TYPE_EMAIL and $member->email and $this->emailIsInUse( $member->email ) )
{
return TRUE;
}
return FALSE;
}
/**
* Can a member change their email/password with this login handler?
*
* @param string $type 'username' or 'email' or 'password'
* @param \IPS\Member $member The member
* @return bool
*/
public function canChange( $type, \IPS\Member $member )
{
return $this->canProcess( $member );
}
/**
* Email is in use?
* Used when registering or changing an email address to check the new one is available
*
* @param string $email Email Address
* @param \IPS\Member|NULL $exclude Member to exclude
* @return bool|NULL Boolean indicates if email is in use (TRUE means is in use and thus not registerable) or NULL if this handler does not support such an API
*/
public function emailIsInUse( $email, \IPS\Member $exclude=NULL )
{
if ( $exclude )
{
return NULL;
}
try
{
$this->externalDb()->select( $this->settings['db_col_email'], $this->settings['db_table'], array( "{$this->settings['db_col_email']}=?", $email ) )->first();
return TRUE;
}
catch ( \UnderflowException $e )
{
return FALSE;
}
catch ( \IPS\Db\Exception $e )
{
return NULL;
}
}
/**
* Username is in use?
* Used when registering or changing an username to check the new one is available
*
* @param string $username Username
* @return bool|NULL Boolean indicates if username is in use (TRUE means is in use and thus not registerable) or NULL if this handler does not support such an API
*/
public function usernameIsInUse( $username )
{
try
{
$result = $this->externalDb()->select( $this->settings['db_col_user'], $this->settings['db_table'], array( "{$this->settings['db_col_user']}=?", $username ) )->first();
return TRUE;
}
catch ( \UnderflowException $e )
{
return FALSE;
}
catch ( \IPS\Db\Exception $e )
{
return NULL;
}
}
/**
* Change Email Address
*
* @param \IPS\Member $member The member
* @param string $oldEmail Old Email Address
* @param string $newEmail New Email Address
* @return void
* @throws \IPS\Db\Exception
*/
public function changeEmail( \IPS\Member $member, $oldEmail, $newEmail )
{
if ( $this->settings['db_col_email'] )
{
$this->externalDb()->update( $this->settings['db_table'], array( $this->settings['db_col_email'] => $newEmail ), array( $this->settings['db_col_email'] . '=?', $oldEmail ) );
}
}
/**
* Change Password
*
* @param \IPS\Member $member The member
* @param string $newPassword New Password
* @return void
* @throws \IPS\Db\Exception
*/
public function changePassword( \IPS\Member $member, $newPassword )
{
$where = '1=0';
switch ( $this->authTypes )
{
case \IPS\Login::AUTH_TYPE_USERNAME:
$where = array( "{$this->settings['db_col_user']}=?", $member->name );
break;
case \IPS\Login::AUTH_TYPE_EMAIL:
case \IPS\Login::AUTH_TYPE_USERNAME + \IPS\Login::AUTH_TYPE_EMAIL:
$where = array( "{$this->settings['db_col_email']}=?", $member->email );
break;
}
$this->externalDb()->update( $this->settings['db_table'], array( $this->settings['db_col_pass'] => $this->encryptedPassword( $newPassword ) ), $where );
}
/**
* Change Username
*
* @param \IPS\Member $member The member
* @param string $oldUsername Old Username
* @param string $newUsername New Username
* @return void
* @throws \IPS\Db\Exception
*/
public function changeUsername( \IPS\Member $member, $oldUsername, $newUsername )
{
if ( $this->settings['db_col_user'] )
{
$this->externalDb()->update( $this->settings['db_table'], array( $this->settings['db_col_user'] => $newUsername ), array( $this->settings['db_col_user'] . '=?', $oldUsername ) );
}
}
}