forked from Eole-dev/authphpbb3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
830 lines (802 loc) · 32.3 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
<?php
/**
* Authentication Plugin for authphpbb3.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Eole <[email protected]>
*/
if (!defined('DOKU_INC')) {
die();
}
/**
* phpBB 3.x Authentication class.
*/
class auth_plugin_authphpbb3 extends DokuWiki_Auth_Plugin {
// @var object phpBB database connection.
protected $_phpbb_db_link = null;
// @var array phpBB configuration (cached).
protected $_phpbb_conf = array(
// @var string phpBB root path.
'root_path' => '',
// @var string phpBB URL.
'url' => '',
// @var string php extension.
'phpEx' => '',
// @var string phpBB database's driver to use.
'dbms' => '',
// @var string phpBB database's host.
'dbhost' => '',
// @var string phpBB database's port.
'dbport' => '',
// @var string phpBB database's name.
'dbname' => '',
// @var string phpBB database's user.
'dbuser' => '',
// @var string phpBB database's password.
'dbpasswd' => '',
// @var string phpBB database's table prefix.
'table_prefix' => '',
// @var string phpBB cookie's name.
'cookie_name' => ''
);
// @var int phpBB user ID.
protected $_phpbb_user_id = 0;
// @var int phpBB user session ID.
protected $_phpbb_user_session_id = '';
// @var int phpBB user type (0 = normal, 1 = inactive, 2 = bot, 3 = founder).
protected $_phpbb_user_type = 0;
// @var string phpBB user name.
protected $_phpbb_username = '';
// @var string phpBB user mail.
protected $_phpbb_user_email = '';
// @var array phpBB user's groups.
protected $_phpbb_groups = array();
// @var long phpBB user session time.
protected $_phpbb_sessiontime = 0;
// @var cache DokuWiki cache object.
protected $_cache = null;
// @var int Cache duration.
protected $_cache_duration = 0;
// @var int Cache extension file name.
protected $_cache_ext_name = '.phpbb3cache';
// @var int Cache unit constant (in seconds).
CONST CACHE_DURATION_UNIT = 86400; /* 60 * 60 * 24 = 1 day */
/**
* Constructor.
*/
public function __construct() {
if (method_exists(get_parent_class($this), '__construct')) {
parent::__construct();
}
// Set capabilities accordingly.
$this->cando['addUser'] = false; // Can Users be created?
$this->cando['delUser'] = false; // Can Users be deleted?
$this->cando['modLogin'] = false; // Can login names be changed?
$this->cando['modPass'] = false; // Can passwords be changed?
$this->cando['modName'] = false; // Can real names be changed?
$this->cando['modMail'] = false; // Can emails be changed?
$this->cando['modGroups'] = false; // Can groups be changed?
$this->cando['getUsers'] = true; // Can a (filtered) list of users be retrieved?
$this->cando['getUserCount']= true; // Can the number of users be retrieved?
$this->cando['getGroups'] = true; // Can a list of available groups be retrieved?
$this->cando['external'] = true; // Does the module do external auth checking?
$this->cando['logout'] = true; // Can the user logout again?
// Check database connection requirement.
if (!class_exists('PDO')) {
$this->dbglog('PDO extension for PHP not found.');
$this->success = false;
} else {
// Load plugin configuration.
$this->success = $this->load_configuration();
}
if (!$this->success) {
msg($this->getLang('config_error'), -1);
}
}
/**
* Destructor.
*/
public function __destruct() {
$this->phpbb_disconnect();
$this->_cache = null;
if (method_exists(get_parent_class($this), '__destruct')) {
parent::__destruct();
}
}
/**
* Writes debug informations.
*
* @param string $msg Message to write.
*/
public function dbglog($msg) {
$class_name = @get_class($this);
if ($class_name !== false) {
$msg = $class_name . ': ' . $msg;
}
dbglog($msg);
}
/**
* Sanitizes a given username.
*
* @param string $username Username to clean.
* @return string Clean username.
*/
public function clean_username($username) {
$username = preg_replace('#(?:[\x00-\x1F\x7F]+|(?:\xC2[\x80-\x9F])+)#', '', $username);
$username = preg_replace('# {2,}#', ' ', $username);
$username = trim($username);
return strtolower($username);
}
/**
* Gets phpBB URL.
*
* @return string|false phpBB URL if success, false otherwise.
*/
public function get_phpbb_url() {
if (!empty($this->_phpbb_conf['url'])) {
return $this->_phpbb_conf['url'];
}
if ($this->use_phpbb_cache()) {
$result = unserialize($this->_cache->retrieveCache(false));
if (is_array($result) && array_key_exists('url', $result)) {
$this->_phpbb_conf['url'] = $result['url'];
}
}
if (!empty($this->_phpbb_conf['url'])) {
return $this->_phpbb_conf['url'];
}
if (!$this->phpbb_connect()) {
return false;
}
$query = "SELECT config_name, config_value
FROM {$this->_phpbb_conf['table_prefix']}config
WHERE config_name IN ('server_protocol', 'server_name', 'script_path', 'server_port')";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for phpBB URL');
return false;
}
if ($result->execute() === false) {
$this->dbglog('error while executing query for phpBB URL');
return false;
}
$server_protocol = '';
$server_name = '';
$script_path = '';
$server_port = '';
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
switch ($row['config_name']) {
case 'server_protocol':
$server_protocol = strtolower(trim($row['config_value']));
break;
case 'server_name':
$server_name = rtrim(trim($row['config_value']), '/');
break;
case 'script_path':
$script_path = trim($row['config_value']);
break;
case 'server_port':
$server_port = intval($row['config_value']);
break;
default:
break;
}
}
if (empty($server_port)) {
$server_port = '80';
if ($server_protocol === 'https://') {
$server_port = '443';
}
}
$server_name = rtrim($server_protocol . $server_name . ':' . $server_port . $script_path, '/');
$this->_phpbb_conf['url'] = $server_name;
$result->closeCursor();
$result = null;
return $this->_phpbb_conf['url'];
}
/**
* Authenticates the user. Called on every page loaded.
*
* @param string $user Case sensitive user name.
* @param string $pass Plain text password for the user.
* @param boolean $sticky Remember login?
* @return boolean True for match, false for everything else.
*/
public function trustExternal($user, $pass, $sticky = false) {
global $USERINFO;
$b = false;
$this->_phpbb_username = '';
$this->_phpbb_user_email = '';
$this->_phpbb_user_session_id = '';
if (empty($user)) {
$b = $this->do_login_cookie();
}
if (!$b ||
empty($this->_phpbb_username) ||
empty($this->_phpbb_user_email)) {
return false;
}
$USERINFO['name'] = utf8_encode($this->_phpbb_username);
$USERINFO['mail'] = $this->_phpbb_user_email;
$USERINFO['grps'] = $this->_phpbb_groups;
$_SERVER['REMOTE_USER'] = $USERINFO['name'];
$_SESSION[DOKU_COOKIE]['auth']['user'] = $USERINFO['name'];
$_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
return true;
}
/**
* Fetchs user details from phpBB.
*
* @param string $user Case sensitive username.
* @param boolean $requireGroups Whether or not the returned data must include groups.
* @return array/boolean False for error conditions and an array for success.
* array['name'] string User's name.
* array['username'] string User's name.
* array['mail'] string User's email address.
* array['phpbb_user_id'] string User's ID.
* array['phpbb_profile'] string User's link to profile.
* array['grps'] array Group names the user belongs to.
*/
public function getUserData($user, $requireGroups = true) {
if (empty($user)) {
return false;
}
$this->_cache_duration = intval($this->getConf('phpbb_cache'));
$depends = array('age' => self::CACHE_DURATION_UNIT * $this->_cache_duration);
$cache = new cache('authphpbb3_getUserData_' . $user, $this->_cache_ext_name);
$user_data = false;
if (($this->_cache_duration > 0) && $cache->useCache($depends)) {
$user_data = unserialize($cache->retrieveCache(false));
} else {
$cache->removeCache();
if (!$this->phpbb_connect()) {
return false;
}
$query = "SELECT user_id, username, username_clean, user_email, user_type
FROM {$this->_phpbb_conf['table_prefix']}users
WHERE username_clean = ?";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for user data');
return false;
}
if (!$result->execute(array($this->clean_username($user)))) {
$this->dbglog('error while executing query for user data');
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
$this->_phpbb_user_type = (int)$row['user_type'];
$this->_phpbb_user_id = (int)$row['user_id'];
$this->_phpbb_username = $row['username'];
$this->_phpbb_user_email = $row['user_email'];
$result->closeCursor();
$result = null;
$this->get_phpbb_user_groups();
$this->get_phpbb_url();
$user_data = array(
'name' => $this->_phpbb_username,
'username' => $this->_phpbb_username,
'mail' => $this->_phpbb_user_email,
'phpbb_user_id' => $this->_phpbb_user_id,
'phpbb_profile' => $this->_phpbb_conf['url'] . '/memberlist.php?mode=viewprofile&u=' .
$this->_phpbb_user_id,
'grps' => $this->_phpbb_groups
);
$cache->storeCache(serialize($user_data));
}
$cache = null;
return $user_data;
}
/**
* Bulk retrieval of users' data (does not use cache system).
*
* @param int $start Index of first user to be returned.
* @param int $limit Maximum number of users to be returned.
* @param array $filter Array of field/pattern pairs.
* @return array/boolean List of arrays returned by getUserData function.
*/
public function retrieveUsers($start = 0, $limit = 0, $filter = array()) {
if (!$this->phpbb_connect()) {
return false;
}
$start = intval($start);
if ($start < 0) {
$start = 0;
}
$limit = intval($limit);
if ($limit <= 0) {
// Arbitrary limit.
$limit = 10000;
}
if (is_null($filter)) {
$filter = array();
}
if (isset($filter['grps'])) {
$filter['group'] = $filter['grps'];
}
foreach (array('user', 'name', 'mail') as $key) {
$tmp = '%';
if (isset($filter[$key])) {
$tmp = str_replace('%', '', $filter[$key]);
if (!is_string($tmp)) {
$tmp = '';
}
if (empty($tmp)) {
$tmp = '%';
} else {
$tmp = '%' . $tmp . '%';
}
}
$filter[$key] = $tmp;
}
$filter['start'] = (int)$start;
$filter['end'] = (int)($start + $limit);
$filter['limit'] = (int)$limit;
// Gets users.
$query = "SELECT user_id, username, username_clean, user_email
FROM {$this->_phpbb_conf['table_prefix']}users
WHERE (username like :name OR username like :user OR
username_clean like :nameclean OR username_clean like :userclean) AND
user_email like :mail
LIMIT :limit OFFSET :start";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for users data');
return false;
}
$result->bindValue(':name', $filter['name'], PDO::PARAM_STR);
$result->bindValue(':user', $filter['user'], PDO::PARAM_STR);
$result->bindValue(':nameclean', $filter['name'], PDO::PARAM_STR);
$result->bindValue(':userclean', $filter['user'], PDO::PARAM_STR);
$result->bindValue(':mail', $filter['mail'], PDO::PARAM_STR);
$result->bindValue(':limit', (int)$filter['limit'], PDO::PARAM_INT);
$result->bindValue(':start', (int)$filter['start'], PDO::PARAM_INT);
if (!$result->execute()) {
$this->dbglog('error while executing query for users data');
return false;
}
$users = array();
// Gets users' groups.
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$user_data = array(
'name' => $row['username'],
'username' => $row['username_clean'],
'mail' => $row['user_email'],
'phpbb_user_id' => $row['user_id'],
'phpbb_profile' => $this->_phpbb_conf['url'] . '/memberlist.php?mode=viewprofile&u=' .
$row['user_id'],
'grps' => array()
);
$query = "SELECT *
FROM {$this->_phpbb_conf['table_prefix']}groups g,
{$this->_phpbb_conf['table_prefix']}users u,
{$this->_phpbb_conf['table_prefix']}user_group ug
WHERE u.user_id = ug.user_id AND g.group_id = ug.group_id AND u.user_id = ?";
$resgrp = $this->_phpbb_sql_link->prepare($query);
if (($resgrp === false) || !$resgrp->execute(array($row['user_id']))) {
$this->dbglog('error while executing query for ' . $row['user_id'] . ' groups');
// If no group filter, we still add the user to the list.
if (!isset($filter['group'])) {
$users[$user_data['username']] = $user_data;
}
$resgrp->closeCursor();
$resgrp = null;
continue;
}
$ingroup = false;
while ($rowgrp = $resgrp->fetch(PDO::FETCH_ASSOC)) {
$user_data['grps'][] = $rowgrp['group_name'];
if (!$ingroup &&
isset($filter['group']) &&
(strpos($rowgrp['group_name'], $filter['group']) !== false)) {
$ingroup = true;
}
}
// Apply group's filter.
if (!isset($filter['group']) || (isset($filter['group']) && $ingroup)) {
$users[$user_data['username']] = $user_data;
}
$resgrp->closeCursor();
$resgrp = null;
}
$result->closeCursor();
$result = null;
return $users;
}
/**
* Bulk retrieval of groups (does not use cache system).
*
* @param int $start Index of first group to be returned.
* @param int $limit Maximum number of groups to be returned.
* @return array/boolean List of groups.
*/
public function retrieveGroups($start = 0, $limit = 0) {
if (!$this->phpbb_connect()) {
return false;
}
$start = intval($start);
if ($start < 0) {
$start = 0;
}
$limit = intval($limit);
if ($limit <= 0) {
// Arbitrary limit.
$limit = 10000;
}
$query = "SELECT *
FROM {$this->_phpbb_conf['table_prefix']}groups
LIMIT :limit OFFSET :start";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for groups data');
return false;
}
$result->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
$result->bindValue(':start', (int)$start, PDO::PARAM_INT);
if (!$result->execute()) {
$this->dbglog('error while executing query for groups data');
return false;
}
$groups = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
if (isset($row['group_name']) && !empty($row['group_name'])) {
$groups[$row['group_name']] = $row;
}
}
$result->closeCursor();
$result = null;
ksort($groups);
return $groups;
}
/**
* Returns the number of users which meet $filter criteria (does not use cache system).
*
* @param array $filter Array of field/pattern pairs, empty array for no filter.
* @return int Number of users matching criteria.
*/
public function getUserCount($filter = array()) {
$users = $this->retrieveUsers(0, 0, $filter);
if ($users === false) {
return 0;
}
return count($users);
}
/**
* Logs off the user.
*/
public function logOff() {
if (method_exists(get_parent_class($this), 'logOff')) {
parent::logOff();
}
if (empty($this->_phpbb_user_session_id)) {
$this->get_phpbb_cookie_name();
if (empty($this->_phpbb_conf['cookie_name'])) {
return ;
}
$phpbb_cookie_sid_name = $this->_phpbb_conf['cookie_name'] . '_sid';
if (array_key_exists($phpbb_cookie_sid_name, $_COOKIE)) {
$this->_phpbb_user_session_id = $_COOKIE[$phpbb_cookie_sid_name];
}
}
if (!empty($this->_phpbb_user_session_id) &&
($this->get_phpbb_url() !== false) &&
$this->_phpbb_user_id) {
//global $ID;
$url = $this->_phpbb_conf['url'] . '/ucp.php?mode=logout&sid=' . $this->_phpbb_user_session_id;
// phpBB doesn't natively support the logout redirection yet.
//$url .= '&redirect=' . urlencode(wl($ID, '', true));
send_redirect($url);
}
}
/**
* Loads the plugin's configuration.
*
* @return boolean True on success, false otherwise.
*/
private function load_configuration() {
if ($this->use_phpbb_cache()) {
$this->_phpbb_conf = unserialize($this->_cache->retrieveCache(false));
} else {
$this->_cache->removeCache();
$this->_phpbb_conf['root_path'] = DOKU_INC . rtrim(trim($this->getConf('phpbb_root_path')), '/') . '/';
$this->_phpbb_conf['phpEx'] = substr(strrchr(__FILE__, '.'), 1);
if (!@file_exists($this->_phpbb_conf['root_path'] . 'config.' . $this->_phpbb_conf['phpEx'])) {
$this->dbglog('phpBB installation not found');
return false;
}
include($this->_phpbb_conf['root_path'] . 'config.' . $this->_phpbb_conf['phpEx']);
$this->_phpbb_conf['dbms'] = $dbms;
$this->_phpbb_conf['dbhost'] = empty($dbhost) ? '127.0.0.1' : $dbhost;
$this->_phpbb_conf['dbport'] = $dbport;
$this->_phpbb_conf['dbname'] = $dbname;
$this->_phpbb_conf['dbuser'] = $dbuser;
$this->_phpbb_conf['dbpasswd'] = $dbpasswd;
$this->_phpbb_conf['table_prefix'] = $table_prefix;
foreach (array('dbms', 'dbhost', 'dbname', 'dbuser') as $member) {
if (empty($this->_phpbb_conf[$member])) {
$this->dbglog("phpBB config variable {$member} not set");
return false;
}
}
if ($this->get_phpbb_url() === false) {
$this->dbglog('cannot get phpBB URL');
return false;
}
if (!$this->get_phpbb_cookie_name()) {
$this->dbglog('cannot get phpBB cookie name');
return false;
}
$this->_cache->storeCache(serialize($this->_phpbb_conf));
}
return (!empty($this->_phpbb_conf['url']) &&
!empty($this->_phpbb_conf['cookie_name']));
}
/**
* Gets the phpBB configuration cache.
*
* @return object Cache of the phpBB configuration.
*/
private function get_phpbb_cache() {
if ($this->_cache === null) {
$this->_cache = new cache('authphpbb3', $this->_cache_ext_name);
}
return $this->_cache;
}
/**
* Can use the phpBB configuration cache.
*
* @return object Cache of the phpBB configuration.
*/
private function use_phpbb_cache() {
$depends = array();
$this->get_phpbb_cache();
$this->_cache_duration = intval($this->getConf('phpbb_cache'));
if ($this->_cache_duration > 0) {
$depends['age'] = self::CACHE_DURATION_UNIT * $this->_cache_duration;
} else {
$depends['purge'] = true;
}
return $this->_cache->useCache($depends);
}
/**
* Connects to phpBB database.
*
* @return boolean True on success, false otherwise.
*/
private function phpbb_connect() {
if (!$this->_phpbb_db_link) {
$host = strtolower(end(explode('\\', $this->_phpbb_conf['dbms'])));
$port = '';
$dsn = '';
if (!empty($this->_phpbb_conf['dbport'])) {
$port = ';port=' . intval($this->_phpbb_conf['dbport']);
}
$dsn = ':host=' . $this->_phpbb_conf['dbhost'] . $port .
';dbname=' . $this->_phpbb_conf['dbname'];
try {
switch ($host) {
case 'mysql':
case 'mysqli':
$dsn = 'mysql' . $dsn . ';charset=utf8';
$this->_phpbb_sql_link = new PDO(
$dsn,
$this->_phpbb_conf['dbuser'],
$this->_phpbb_conf['dbpasswd']);
break;
case 'postgres':
$dsn = 'pgsql' . $dsn .
';user=' . $this->_phpbb_conf['dbuser'] .
';password=' . $this->_phpbb_conf['dbpasswd'];
$this->_phpbb_sql_link = new PDO($dsn);
$this->_phpbb_sql_link->exec("SET NAMES 'UTF8'");
break;
case 'oracle':
$dsn = 'oci' . $dsn . ';charset=utf8';
$this->_phpbb_sql_link = new PDO(
$dsn,
$this->_phpbb_conf['dbuser'],
$this->_phpbb_conf['dbpasswd']);
break;
case 'sqlite':
case 'sqlite3':
if ($host === 'sqlite') {
// SQLite 2 prefix.
$host = 'sqlite2';
} else {
// SQLite 3 prefix.
$host = 'sqlite';
}
$dsn = $host . ':' . $this->_phpbb_conf['root_path'] . $this->_phpbb_conf['dbhost'];
$this->_phpbb_sql_link = new PDO($dsn);
break;
default:
msg($this->getLang('database_support'), -1);
return false;
}
} catch (PDOException $e) {
$this->dbglog('cannot connect to database server (' . $e->getMessage() .')');
msg($this->getLang('database_error'), -1);
$this->_phpbb_db_link = null;
return false;
}
}
return true;
}
/**
* Disconnects from phpBB database.
*/
private function phpbb_disconnect() {
if ($this->_phpbb_db_link !== null) {
$this->_phpbb_db_link = null;
}
}
/**
* Gets phpBB cookie's name.
*
* @return boolean True for success, false otherwise.
*/
private function get_phpbb_cookie_name() {
if (!empty($this->_phpbb_conf['cookie_name'])) {
return true;
}
if ($this->use_phpbb_cache()) {
$result = unserialize($this->_cache->retrieveCache(false));
if (is_array($result) && array_key_exists('cookie_name', $result)) {
$this->_phpbb_conf['cookie_name'] = $result['cookie_name'];
}
}
if (!empty($this->_phpbb_conf['cookie_name'])) {
return true;
}
if (!$this->phpbb_connect()) {
return false;
}
// Query for cookie_name.
$query = "SELECT config_name, config_value
FROM {$this->_phpbb_conf['table_prefix']}config
WHERE config_name = 'cookie_name'";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for cookie.');
return false;
}
if (!$result->execute()) {
$this->dbglog('error while executing query for cookie.');
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
$this->_phpbb_conf['cookie_name'] = $row['config_value'];
$result->closeCursor();
$result = null;
return true;
}
/**
* Gets phpBB user's groups.
*
* @return boolean True for success, false otherwise.
*/
private function get_phpbb_user_groups() {
$this->_phpbb_groups = array();
$this->_phpbb_user_id = filter_var($this->_phpbb_user_id, FILTER_VALIDATE_INT);
if (!$this->_phpbb_user_id) {
return false;
}
if (!$this->phpbb_connect()) {
return false;
}
$query = "SELECT *
FROM {$this->_phpbb_conf['table_prefix']}groups g,
{$this->_phpbb_conf['table_prefix']}users u,
{$this->_phpbb_conf['table_prefix']}user_group ug
WHERE u.user_id = ug.user_id AND g.group_id = ug.group_id AND u.user_id = ?";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for user\'s groups');
return false;
}
if (!$result->execute(array($this->_phpbb_user_id))) {
$this->dbglog('error while executing query for user\'s groups');
return false;
}
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$this->_phpbb_groups[] = $row['group_name'];
}
// If the user is a founder.
if ($this->_phpbb_user_type === 3) {
$this->_phpbb_groups[] = 'admin';
}
$result->closeCursor();
$result = null;
return true;
}
/**
* Authenticate the user using cookie. Called on every page load.
*
* @return boolean True for success, false otherwise.
*/
private function do_login_cookie() {
if (!$this->phpbb_connect()) {
return false;
}
if (!$this->get_phpbb_cookie_name()) {
return false;
}
$phpbb_cookie_user_sid = $this->_phpbb_conf['cookie_name'] . '_sid';
$phpbb_cookie_user_id = $this->_phpbb_conf['cookie_name'] . '_u';
$this->_phpbb_user_session_id =
array_key_exists($phpbb_cookie_user_sid, $_COOKIE) ? $_COOKIE[$phpbb_cookie_user_sid] : null;
$phpbb_cookie_user_id =
array_key_exists($phpbb_cookie_user_id, $_COOKIE) ? intval($_COOKIE[$phpbb_cookie_user_id]) : null;
if (empty($this->_phpbb_user_session_id) || !ctype_xdigit($this->_phpbb_user_session_id)) {
$this->dbglog('invalid SID in user\'s cookie ' .
'(SID=' . $phpbb_cookie_user_sid . ', Value=' . $this->_phpbb_user_session_id . ')');
return false;
}
// Get session data from database.
$query = "SELECT session_id, session_user_id
FROM {$this->_phpbb_conf['table_prefix']}sessions
WHERE session_id = ?";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for session');
return false;
}
if (!$result->execute(array($this->_phpbb_user_session_id))) {
$this->dbglog('error while executing query for session');
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
if ($phpbb_cookie_user_id !== (int)$row['session_user_id']) {
$this->dbglog('invalid SID/User ID pair');
$result->closeCursor();
$result = null;
return false;
}
$this->_phpbb_user_id = (int)$row['session_user_id'];
$this->_phpbb_sessiontime = $row['session_time'];
$result->closeCursor();
$result = null;
// Update session time.
$current_time = time();
if ($current_time > $this->_phpbb_sessiontime) {
$query = "UPDATE {$this->_phpbb_conf['table_prefix']}sessions
SET session_time = ?
WHERE session_id = ?";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for session update');
return false;
}
if (!$result->execute(array($current_time, $this->_phpbb_user_session_id))) {
$this->dbglog('error while executing query for session update');
}
$result = null;
}
// Check for guest session.
if ($this->_phpbb_user_id === 1) {
return false;
}
// Get username from database.
$query = "SELECT user_id, username, user_email, user_type
FROM {$this->_phpbb_conf['table_prefix']}users
WHERE user_id = ?";
$result = $this->_phpbb_sql_link->prepare($query);
if ($result === false) {
$this->dbglog('error while preparing query for username');
return false;
}
if (!$result->execute(array($this->_phpbb_user_id))) {
$this->dbglog('error while executing query for username');
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
$this->_phpbb_user_type = (int)$row['user_type'];
$this->_phpbb_username = $row['username'];
$this->_phpbb_user_email = $row['user_email'];
$result->closeCursor();
$result = null;
// Get user groups from database.
$this->get_phpbb_user_groups();
return true;
}
}