forked from PHPAuth/PHPAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
395 lines (309 loc) · 15.4 KB
/
Config.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
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
<?php
namespace PHPAuth;
/**
* PHPAuth Config class
*/
class Config
{
protected $dbh;
public $config;
public $config_table = 'phpauth_config';
/**
* Config::__construct()
*
* Create config class for PHPAuth\Auth.
* Examples:
*
* new Config($dbh) -- Defaults will be used: load config from SQL table phpauth_config, language is 'en_GB'
* new Config($dbh, 'config_table', '') -- 3rd argument is 'sql' or '' => 2rd argument determines config table in DB, default 'phpauth_config'
* new Config($dbh, '$/config/phpauth.ini', 'ini') -- configuration will be loaded from INI file, '$' means Application basedir
* new Config($dbh, $CONFIG_ARRAY, 'array') -- configuration must be defined in $CONFIG_ARRAY value
*
* in any case, 4th argument defines site language as locale code
*
* @param \PDO $dbh
* @param string $config_source -- declare source of config - table name, filepath or data-array
* @param string $config_type -- default empty (means config in SQL table phpauth_config), possible values: 'sql', 'ini', 'array'
* @param string $config_site_language -- declare site language, empty value means 'en_GB'
*/
public function __construct(\PDO $dbh, $config_source = NULL, $config_type = '', $config_site_language = '')
{
$config_type = strtolower($config_type);
if (version_compare(phpversion(), '5.6.0', '<')) {
die('PHPAuth: PHP 5.6.0+ required for PHPAuth engine!');
}
$this->config = array();
$this->dbh = $dbh;
switch ($config_type) {
case 'ini': {
// check valid keys
if (empty($config_source)) die('PHPAuth: config type is FILE, but no source file declared!'); //@todo: \Exception
// replace beginner '$' in filepath to application root directory
$source = preg_replace('/^\$/', getcwd(), $config_source);
// check ini-config is readable
if (!is_readable($source)) die("PHPAuth: config type is FILE, declared as {$source}, but file not readable or not exist"); //@todo: \Exception
// load configuration
$this->config = parse_ini_file($source);
break;
}
case 'array': {
// check data is valid
if (empty($config_source)) die('PHPAuth: config type is ARRAY, but source config is EMPTY'); //@todo: \Exception
// get configuration from given array
$this->config = $config_source;
break;
}
case 'json': {
break;
}
case 'yml': {
break;
}
case 'xml': {
break;
}
default: {
// is 'SQL' or EMPTY value
//
// determine config table
$this->config_table = (empty($config_source)) ? 'phpauth_config' : $config_source;
// check config table exists
if (! $this->dbh->query("SHOW TABLES LIKE '{$this->config_table}'")->fetchAll() ) {
die("PHPAuth: Config table `{$this->config_table}` NOT PRESENT in given database" . PHP_EOL);
};
// load configuration
$this->config = $this->dbh->query("SELECT `setting`, `value` FROM {$this->config_table}")->fetchAll(\PDO::FETCH_KEY_PAIR);
break;
}
} // end switch
$this->setForgottenDefaults(); // Danger foreseen is half avoided.
// Check required tables exists
// check table_attempts
if (! $this->dbh->query("SHOW TABLES LIKE '{$this->config['table_attempts']}'")->fetchAll() ) {
die("PHPAuth: Table `{$this->config['table_attempts']}` NOT PRESENT in given database" . PHP_EOL);
};
// check table requests
if (! $this->dbh->query("SHOW TABLES LIKE '{$this->config['table_requests']}'")->fetchAll() ) {
die("PHPAuth: Table `{$this->config['table_requests']}` NOT PRESENT in given database" . PHP_EOL);
};
// check table sessions
if (! $this->dbh->query("SHOW TABLES LIKE '{$this->config['table_sessions']}'")->fetchAll() ) {
die("PHPAuth: Table `{$this->config['table_sessions']}` NOT PRESENT in given database" . PHP_EOL);
};
// check table users
if (! $this->dbh->query("SHOW TABLES LIKE '{$this->config['table_users']}'")->fetchAll() ) {
die("PHPAuth: Table `{$this->config['table_users']}` NOT PRESENT in given database" . PHP_EOL);
};
// Determine site language
$site_language = (empty($config_site_language))
? isset($this->config['site_language']) ? $this->config['site_language'] : 'en_GB'
: $config_site_language;
$dictionary = [];
if (isset($this->config['translation_source'])) {
switch ($this->config['translation_source']) {
case 'php': {
$lang_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR . "{$site_language}.php";
if (is_readable($lang_file)) {
$dictionary = include $lang_file;
} else {
$dictionary = $this->setForgottenDictionary();
}
break;
}
case 'ini': {
$lang_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR . "{$site_language}.ini";
if (is_readable($lang_file)) {
$dictionary = parse_ini_file($lang_file);
} else {
$dictionary = $this->setForgottenDictionary();
}
break;
}
case 'sql': {
// check field `table_translations` present
if (empty($this->config['table_translations'])) {
$dictionary = $this->setForgottenDictionary();
break;
}
// check table exists in database
if (! $this->dbh->query("SHOW TABLES LIKE '{$this->config['table_translations']}'")->fetchAll() ) {
$dictionary = $this->setForgottenDictionary();
break;
};
$query = "SELECT `translation_key`, `{$site_language}` as `lang` FROM {$this->config['table_translations']} ";
$dictionary = $this->dbh->query($query)->fetchAll(\PDO::FETCH_KEY_PAIR);
break;
}
case 'xml': {
break;
}
case 'json': {
break;
}
default: {
$dictionary = $this->setForgottenDictionary();
}
} // end switch
} else {
$dictionary = $this->setForgottenDictionary();
}
// set dictionary
$this->config['dictionary'] = $dictionary;
// set reCaptcha config
$config_recaptcha = [];
if (array_key_exists('recaptcha_enabled', $this->config)) {
$config_recaptcha['recaptcha_enabled'] = true;
$config_recaptcha['recaptcha_site_key'] = $this->config['recaptcha_site_key'];
$config_recaptcha['recaptcha_secret_key'] = $this->config['recaptcha_secret_key'];
}
$this->config['recaptcha'] = $config_recaptcha;
}
/**
* Config::__get()
*
* @param mixed $setting
* @return string
*/
public function __get($setting)
{
return array_key_exists($setting, $this->config) ? $this->config[$setting] : NULL;
}
/**
* @return array
*/
public function getAll()
{
return $this->config;
}
/**
* Config::__set()
*
* @param mixed $setting
* @param mixed $value
* @return bool
*/
public function __set($setting, $value)
{
$query_prepared = $this->dbh->prepare("UPDATE {$this->config_table} SET value = :value WHERE setting = :setting");
if ($query_prepared->execute(['value' => $value, 'setting' => $setting])) {
$this->config[$setting] = $value;
return true;
}
return false;
}
/**
* Config::override()
*
* @param mixed $setting
* @param mixed $value
* @return bool
*/
public function override($setting, $value)
{
$this->config[$setting] = $value;
return true;
}
/**
* Danger foreseen is half avoided.
*
* Set default values.
* REQUIRED FOR USERS THAT DOES NOT UPDATE THEIR `config` TABLES.
*/
protected function setForgottenDefaults()
{
// ==== unchecked values ====
$this->repairConfigValue('bcrypt_cost', 10);
// cookies* values
$this->repairConfigValue('cookie_name', 'phpauth_session_cookie');
// verify* values
$this->repairConfigValue('verify_password_min_length', 3);
$this->repairConfigValue('verify_email_min_length', 5);
$this->repairConfigValue('verify_email_max_length', 100);
$this->repairConfigValue('verify_email_use_banlist', 1);
// emailmessage* values
$this->repairConfigValue('emailmessage_suppress_activation', 0);
$this->repairConfigValue('emailmessage_suppress_reset', 0);
$this->repairConfigValue('mail_charset', "UTF-8");
// others
$this->repairConfigValue('allow_concurrent_sessions', false);
}
/**
* Set configuration value if it is not present.
* @param $setting
* @param $default_value
*/
protected function repairConfigValue($setting, $default_value)
{
if (!isset($this->config[$setting]))
$this->config[$setting] = $default_value;
}
/**
* Returns forgotten translation dictionary
*
* @return array
*/
private function setForgottenDictionary()
{
$lang = array();
$lang['user_blocked'] = "You are currently locked out of the system.";
$lang['user_verify_failed'] = "Captcha Code was invalid.";
//new
$lang['account_email_invalid'] = "Email address is incorrect or banned";
//new
$lang['account_password_invalid'] = "Password is invalid";
//new
$lang['account_not_found'] = "Account with given email not found.";
$lang['login_remember_me_invalid'] = "The remember me field is invalid.";
$lang['email_password_invalid'] = "Email address / password are invalid.";
$lang['email_password_incorrect'] = "Password are incorrect for given EMail.";
$lang['remember_me_invalid'] = "The remember me field is invalid.";
$lang['password_short'] = "Password is too short.";
$lang['password_weak'] = "Password is too weak.";
$lang['password_nomatch'] = "Passwords do not match.";
$lang['password_changed'] = "Password changed successfully.";
$lang['password_incorrect'] = "Current password is incorrect.";
$lang['password_notvalid'] = "Password is invalid.";
$lang['newpassword_short'] = "New password is too short.";
$lang['newpassword_long'] = "New password is too long.";
$lang['newpassword_invalid'] = "New password must contain at least one uppercase and lowercase character, and at least one digit.";
$lang['newpassword_nomatch'] = "New passwords do not match.";
$lang['newpassword_match'] = "New password is the same as the old password.";
$lang['email_short'] = "Email address is too short.";
$lang['email_long'] = "Email address is too long.";
$lang['email_invalid'] = "It is not a correct Email address.";
$lang['email_incorrect'] = "Email address is incorrect.";
$lang['email_banned'] = "This email address is not allowed.";
$lang['email_changed'] = "Email address changed successfully.";
$lang['newemail_match'] = "New email matches previous email.";
$lang['account_inactive'] = "Account has not yet been activated.";
$lang['account_activated'] = "Account activated.";
$lang['logged_in'] = "You are now logged in.";
$lang['logged_out'] = "You are now logged out.";
$lang['system_error'] = "A system error has been encountered. Please try again.";
$lang['register_success'] = "Account created. Activation email sent to email.";
$lang['register_success_emailmessage_suppressed'] = "Account created.";
$lang['email_taken'] = "The email address is already in use.";
$lang['resetkey_invalid'] = "Reset key is invalid.";
$lang['resetkey_incorrect'] = "Reset key is incorrect.";
$lang['resetkey_expired'] = "Reset key has expired.";
$lang['password_reset'] = "Password reset successfully.";
$lang['activationkey_invalid'] = "Activation key is invalid.";
$lang['activationkey_incorrect'] = "Activation key is incorrect.";
$lang['activationkey_expired'] = "Activation key has expired.";
$lang['reset_requested'] = "Password reset request sent to email address.";
$lang['reset_requested_emailmessage_suppressed'] = "Password reset request is created.";
$lang['reset_exists'] = "A reset request already exists.";
$lang['already_activated'] = "Account is already activated.";
$lang['activation_sent'] = "Activation email has been sent.";
$lang['activation_exists'] = "An activation email has already been sent.";
$lang['email_activation_subject'] = '%s - Activate account';
$lang['email_activation_body'] = 'Hello,<br/><br/> To be able to log in to your account you first need to activate your account by clicking on the following link : <strong><a href="%1$s/%2$s">%1$s/%2$s</a></strong><br/><br/> You then need to use the following activation key: <strong>%3$s</strong><br/><br/> If you did not sign up on %1$s recently then this message was sent in error, please ignore it.';
$lang['email_activation_altbody'] = 'Hello, ' . "\n\n" . 'To be able to log in to your account you first need to activate your account by visiting the following link :' . "\n" . '%1$s/%2$s' . "\n\n" . 'You then need to use the following activation key: %3$s' . "\n\n" . 'If you did not sign up on %1$s recently then this message was sent in error, please ignore it.';
$lang['email_reset_subject'] = '%s - Password reset request';
$lang['email_reset_body'] = 'Hello,<br/><br/>To reset your password click the following link :<br/><br/><strong><a href="%1$s/%2$s">%1$s/%2$s</a></strong><br/><br/>You then need to use the following password reset key: <strong>%3$s</strong><br/><br/>If you did not request a password reset key on %1$s recently then this message was sent in error, please ignore it.';
$lang['email_reset_altbody'] = 'Hello, ' . "\n\n" . 'To reset your password please visiting the following link :' . "\n" . '%1$s/%2$s' . "\n\n" . 'You then need to use the following password reset key: %3$s' . "\n\n" . 'If you did not request a password reset key on %1$s recently then this message was sent in error, please ignore it.';
$lang['account_deleted'] = "Account deleted successfully.";
$lang['function_disabled'] = "This function has been disabled.";
return $lang;
}
}