GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( 52ab9c...bd9ff0 )
by Thorsten
21:26
created

PMF_User_CurrentUser::createCsrfToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Manages authentication process using PHP sessions.
5
 *
6
 * The CurrentUser class is an extension of the User class. It provides methods
7
 * manage user authentication using multiple database accesses. There are three
8
 * ways of making a new current user object, using the login(), getFromSession(),
9
 * getFromCookie() or manually. login(), getFromSession() and getFromCookie() may
10
 * be combined.
11
 *
12
 * PHP Version 5.5
13
 *
14
 * This Source Code Form is subject to the terms of the Mozilla Public License,
15
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
16
 * obtain one at http://mozilla.org/MPL/2.0/.
17
 *
18
 * @category  phpMyFAQ
19
 *
20
 * @author    Lars Tiedemann <[email protected]>
21
 * @author    Thorsten Rinne <[email protected]>
22
 * @copyright 2005-2017 phpMyFAQ Team
23
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
24
 *
25
 * @link      http://www.phpmyfaq.de
26
 * @since     2005-09-28
27
 */
28
if (!defined('IS_VALID_PHPMYFAQ')) {
29
    exit();
30
}
31
32
/* user defined constants */
33
define('PMF_SESSION_CURRENT_USER', 'PMF_CURRENT_USER');
34
define('PMF_SESSION_ID_TIMESTAMP', 'PMF_SESSION_TIMESTAMP');
35
36
/**
37
 * PMF_User_CurrentUser.
38
 *
39
 * @category  phpMyFAQ
40
 *
41
 * @author    Lars Tiedemann <[email protected]>
42
 * @author    Thorsten Rinne <[email protected]>
43
 * @copyright 2005-2017 phpMyFAQ Team
44
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
45
 *
46
 * @link      http://www.phpmyfaq.de
47
 * @since     2005-09-28
48
 */
49
class PMF_User_CurrentUser extends PMF_User
50
{
51
    /**
52
     * true if CurrentUser is logged in, otherwise false.
53
     *
54
     * @var bool
55
     */
56
    private $loggedIn = false;
57
58
    /**
59
     * Specifies the timeout for the session in minutes. If the session ID was
60
     * not updated for the last $this->_sessionTimeout minutes, the CurrentUser
61
     * will be logged out automatically if no cookie was set.
62
     *
63
     * @var int
64
     */
65
    private $sessionTimeout = PMF_AUTH_TIMEOUT;
66
67
    /**
68
     * Specifies the timeout for the session-ID in minutes. If the session ID
69
     * was not updated for the last $this->_sessionIdTimeout minutes, it will
70
     * be updated. If set to 0, the session ID will be updated on every click.
71
     * The session ID timeout must not be greater than Session timeout.
72
     *
73
     * @var int
74
     */
75
    private $sessionIdTimeout = 1;
76
77
    /**
78
     * LDAP configuration if available.
79
     *
80
     * @var array
81
     */
82
    private $ldapConfig = [];
83
84
    /**
85
     * Remember me activated or deactivated.
86
     *
87
     * @var bool
88
     */
89
    private $rememberMe = false;
90
91
    /**
92
     * Login successful or auth failure:
93
     * 1 -> success
94
     * 0 -> failure.
95
     *
96
     * @var int
97
     */
98
    private $loginState = 1;
99
100
    private $lockoutTime = 600;
101
102
    /**
103
     * Constructor.
104
     *
105
     * @param PMF_Configuration $config
106
     */
107
    public function __construct(PMF_Configuration $config)
108
    {
109
        parent::__construct($config);
110
        $this->ldapConfig = $config->getLdapConfig();
111
    }
112
113
    /**
114
     * Checks the given login and password in all auth-objects. Returns true
115
     * on success, otherwise false. Raises errors that can be checked using
116
     * the error() method. On success, the CurrentUser instance will be
117
     * labeled as logged in. The name of the successful auth container will
118
     * be stored in the user table. A new auth object may be added by using
119
     * addAuth() method. The given password must not be encrypted, since the
120
     * auth object takes care about the encryption method.
121
     *
122
     * @param string $login    Login name
123
     * @param string $password Password
124
     *
125
     * @return bool
126
     */
127
    public function login($login, $password)
128
    {
129
        $optData = [];
130
        $loginError = $passwordError = $count = 0;
131
132
        // First check for brute force attack
133
        $this->getUserByLogin($login);
134
        if ($this->isFailedLastLoginAttempt()) {
135
            $this->errors[] = parent::ERROR_USER_TOO_MANY_FAILED_LOGINS;
136
            return false;
137
        }
138
139
        // Additional code for LDAP: user\\domain
140
        if ($this->config->get('security.ldapSupport') && isset($this->ldapConfig['ldap_use_domain_prefix']) &&
141
            $this->ldapConfig['ldap_use_domain_prefix'] && '' !== $password) {
142
            // if LDAP configuration is enabled, and ldap_use_domain_prefix is available (in file constants_ldap.php)
143
            // and ldap_use_domain_prefix is set to true and LDAP data are provided (password is not empty)
144
            if (($pos = strpos($login, '\\')) !== false) {
145
                if ($pos !== 0) {
146
                    $optData['domain'] = substr($login, 0, $pos);
147
                }
148
149
                $login = substr($login, $pos + 1);
150
            }
151
        }
152
153
        // Additional code for SSO
154
        if ($this->config->get('security.ssoSupport') && isset($_SERVER['REMOTE_USER']) && '' === $password) {
155
            // if SSO configuration is enabled, REMOTE_USER is provided and we try to login using SSO (no password)
156 View Code Duplication
            if (($pos = strpos($login, '@')) !== false) {
157
                if ($pos !== 0) {
158
                    $login = substr($login, 0, $pos);
159
                }
160
            }
161 View Code Duplication
            if (($pos = strpos($login, '\\')) !== false) {
162
                if ($pos !== 0) {
163
                    $login = substr($login, $pos + 1);
164
                }
165
            }
166
        }
167
168
        // authenticate user by login and password
169
        foreach ($this->authContainer as $name => $auth) {
170
            ++$count;
171
172
            // $auth is an invalid Auth object, so continue
173
            if (!$this->checkAuth($auth)) {
0 ignored issues
show
Documentation introduced by
$auth is of type object<PMF_Auth_Driver>, but the function expects a object<PMF_Auth>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
174
                --$count;
175
                continue;
176
            }
177
178
            // $login does not exist, so continue
179
            if (!$auth->checkLogin($login, $optData)) {
180
                ++$loginError;
181
                continue;
182
            }
183
184
            // $login exists, but $pass is incorrect, so stop!
185
            if (!$auth->checkPassword($login, $password, $optData)) {
186
                ++$passwordError;
187
                // Don't stop, as other auth method could work:
188
                continue;
189
            }
190
191
            // but hey, this must be a valid match, so get user object
192
            $this->getUserByLogin($login);
193
            $this->loggedIn = true;
194
            $this->updateSessionId(true);
195
            $this->saveToSession();
196
            $this->saveCrsfTokenToSession();
197
198
            // save remember me cookie if set
199
            if (true === $this->rememberMe) {
200
                $rememberMe = sha1(session_id());
201
                $this->setRememberMe($rememberMe);
202
                PMF_Session::setCookie(
203
                    PMF_Session::PMF_COOKIE_NAME_REMEMBERME,
204
                    $rememberMe,
205
                    $_SERVER['REQUEST_TIME'] + PMF_REMEMBERME_EXPIRED_TIME
206
                );
207
            }
208
209
            // remember the auth container for administration
210
            $update = sprintf("
211
                UPDATE
212
                    %sfaquser
213
                SET
214
                    auth_source = '%s'
215
                WHERE
216
                    user_id = %d",
217
                PMF_Db::getTablePrefix(),
218
                $this->config->getDb()->escape($name),
219
                $this->getUserId()
220
            );
221
            $result = $this->config->getDb()->query($update);
222
            if (!$result) {
223
                $this->setSuccess(false);
224
                return false;
225
            }
226
227
            // Login successful
228
            $this->setSuccess(true);
229
            return true;
230
        }
231
232
        // raise errors and return false
233
        if ($loginError === $count) {
234
            $this->setSuccess(false);
235
            $this->errors[] = parent::ERROR_USER_INCORRECT_LOGIN;
236
        }
237
        if ($passwordError > 0) {
238
            $this->getUserByLogin($login);
239
            $this->setLoginAttempt();
240
            $this->errors[] = parent::ERROR_USER_INCORRECT_PASSWORD;
241
        }
242
243
        return false;
244
    }
245
246
    /**
247
     * Returns true if CurrentUser is logged in, otherwise false.
248
     *
249
     * @return bool
250
     */
251
    public function isLoggedIn()
252
    {
253
        return $this->loggedIn;
254
    }
255
256
    /**
257
     * Returns false if the CurrentUser object stored in the
258
     * session is valid and not timed out. There are two
259
     * parameters for session timeouts: $this->_sessionTimeout
260
     * and $this->_sessionIdTimeout.
261
     *
262
     * @return bool
263
     */
264
    public function sessionIsTimedOut()
265
    {
266
        if ($this->sessionTimeout <= $this->sessionAge()) {
267
            return true;
268
        }
269
270
        return false;
271
    }
272
273
    /**
274
     * Returns false if the session-ID is not timed out.
275
     *
276
     * @return bool
277
     */
278
    public function sessionIdIsTimedOut()
279
    {
280
        if ($this->sessionIdTimeout <= $this->sessionAge()) {
281
            return true;
282
        }
283
284
        return false;
285
    }
286
287
    /**
288
     * Returns the age of the current session-ID in minutes.
289
     *
290
     * @return float
291
     */
292
    public function sessionAge()
293
    {
294
        if (!isset($_SESSION[PMF_SESSION_ID_TIMESTAMP])) {
295
            return 0;
296
        }
297
298
        return ($_SERVER['REQUEST_TIME'] - $_SESSION[PMF_SESSION_ID_TIMESTAMP]) / 60;
299
    }
300
301
    /**
302
     * Returns an associative array with session information stored
303
     * in the user table. The array has the following keys:
304
     * session_id, session_timestamp and ip.
305
     *
306
     * @return array
307
     */
308
    public function getSessionInfo()
309
    {
310
        $select = sprintf('
311
            SELECT
312
                session_id,
313
                session_timestamp,
314
                ip,
315
                success
316
            FROM
317
                %sfaquser
318
            WHERE
319
                user_id = %d',
320
           PMF_Db::getTablePrefix(),
321
           $this->getUserId()
322
        );
323
324
        $res = $this->config->getDb()->query($select);
325
        if (!$res or $this->config->getDb()->numRows($res) != 1) {
326
            return [];
327
        }
328
329
        return $this->config->getDb()->fetchArray($res);
330
    }
331
332
    /**
333
     * Updates the session-ID, does not care about time outs.
334
     * Stores session information in the user table: session_id,
335
     * session_timestamp and ip.
336
     * Optionally it should update the 'last login' time.
337
     * Returns true on success, otherwise false.
338
     *
339
     * @param bool $updateLastlogin Update the last login time?
340
     *
341
     * @return bool
342
     */
343
    public function updateSessionId($updateLastlogin = false)
344
    {
345
        // renew the session-ID
346
        $oldSessionId = session_id();
347
        if (session_regenerate_id(true)) {
348
            $sessionPath = session_save_path();
349
            if (strpos($sessionPath, ';') !== false) {
350
                $sessionPath = substr($sessionPath, strpos($sessionPath, ';') + 1);
351
            }
352
            $sessionFilename = $sessionPath.'/sess_'.$oldSessionId;
353
            if (@file_exists($sessionFilename)) {
354
                @unlink($sessionFilename);
355
            }
356
        }
357
        // store session-ID age
358
        $_SESSION[PMF_SESSION_ID_TIMESTAMP] = $_SERVER['REQUEST_TIME'];
359
        // save session information in user table
360
        $update = sprintf("
361
            UPDATE
362
                %sfaquser
363
            SET
364
                session_id = '%s',
365
                session_timestamp = %d,
366
                %s
367
                ip = '%s'
368
            WHERE
369
                user_id = %d",
370
            PMF_Db::getTablePrefix(),
371
            session_id(),
372
            $_SERVER['REQUEST_TIME'],
373
            $updateLastlogin ?  "last_login = '".date('YmdHis', $_SERVER['REQUEST_TIME'])."'," : '',
374
            $_SERVER['REMOTE_ADDR'],
375
            $this->getUserId()
376
        );
377
378
        $res = $this->config->getDb()->query($update);
379
        if (!$res) {
380
            $this->errors[] = $this->config->getDb()->error();
381
382
            return false;
383
        }
384
385
        return true;
386
    }
387
388
    /**
389
     * Saves the CurrentUser into the session. This method
390
     * may be called after a successful login.
391
     */
392
    public function saveToSession()
393
    {
394
        $_SESSION[PMF_SESSION_CURRENT_USER] = $this->getUserId();
395
    }
396
397
    /**
398
     * Deletes the CurrentUser from the session. The user
399
     * will be logged out. Return true on success, otherwise false.
400
     *
401
     * @param bool $deleteCookie
402
     *
403
     * @return bool
404
     */
405
    public function deleteFromSession($deleteCookie = false)
406
    {
407
        // delete CSRF Token
408
        $this->deleteCsrfTokenFromSession();
409
410
        // delete CurrentUser object from session
411
        $_SESSION[PMF_SESSION_CURRENT_USER] = null;
412
        unset($_SESSION[PMF_SESSION_CURRENT_USER]);
413
414
        // log CurrentUser out
415
        $this->loggedIn = false;
416
417
        // delete session-ID
418
        $update = sprintf('
419
            UPDATE
420
                %sfaquser
421
            SET
422
                session_id = NULL
423
                %s
424
            WHERE
425
                user_id = %d',
426
                PMF_Db::getTablePrefix(),
427
                $deleteCookie ? ', remember_me = NULL' : '',
428
                $this->getUserId()
429
        );
430
431
        $res = $this->config->getDb()->query($update);
432
433
        if (!$res) {
434
            $this->errors[] = $this->config->getDb()->error();
435
436
            return false;
437
        }
438
439
        if ($deleteCookie) {
440
            PMF_Session::setCookie(PMF_Session::PMF_COOKIE_NAME_REMEMBERME);
441
        }
442
443
        session_destroy();
444
445
        return true;
446
    }
447
448
    /**
449
     * This static method returns a valid CurrentUser object if there is one
450
     * in the session that is not timed out. The session-ID is updated if
451
     * necessary. The CurrentUser will be removed from the session, if it is
452
     * timed out. If there is no valid CurrentUser in the session or the
453
     * session is timed out, null will be returned. If the session data is
454
     * correct, but there is no user found in the user table, false will be
455
     * returned. On success, a valid CurrentUser object is returned.
456
     *
457
     * @static
458
     *
459
     * @param PMF_Configuration $config
460
     *
461
     * @return null|PMF_User_CurrentUser
462
     */
463
    public static function getFromSession(PMF_Configuration $config)
464
    {
465
        // there is no valid user object in session
466
        if (!isset($_SESSION[PMF_SESSION_CURRENT_USER]) || !isset($_SESSION[PMF_SESSION_ID_TIMESTAMP])) {
467
            return;
468
        }
469
470
        // create a new CurrentUser object
471
        $user = new self($config);
472
        $user->getUserById($_SESSION[PMF_SESSION_CURRENT_USER]);
473
474
        // user object is timed out
475
        if ($user->sessionIsTimedOut()) {
476
            $user->deleteFromSession();
477
            $user->errors[] = 'Session timed out.';
478
479
            return;
480
        }
481
        // session-id not found in user table
482
        $session_info = $user->getSessionInfo();
483
        $session_id = (isset($session_info['session_id']) ? $session_info['session_id'] : '');
484
        if ($session_id == '' || $session_id != session_id()) {
485
            return false;
486
        }
487
        // check ip
488
        if ($config->get('security.ipCheck') &&
489
            $session_info['ip'] != $_SERVER['REMOTE_ADDR']) {
490
            return false;
491
        }
492
        // session-id needs to be updated
493
        if ($user->sessionIdIsTimedOut()) {
494
            $user->updateSessionId();
495
        }
496
        // user is now logged in
497
        $user->loggedIn = true;
498
        // save current user to session and return the instance
499
        $user->saveToSession();
500
501
        return $user;
502
    }
503
504
    /**
505
     * This static method returns a valid CurrentUser object if there is one
506
     * in the cookie that is not timed out. The session-ID is updated then.
507
     * The CurrentUser will be removed from the session, if it is
508
     * timed out. If there is no valid CurrentUser in the cookie or the
509
     * cookie is timed out, null will be returned. If the cookie is correct,
510
     * but there is no user found in the user table, false will be returned.
511
     * On success, a valid CurrentUser object is returned.
512
     *
513
     * @static
514
     *
515
     * @param PMF_Configuration $config
516
     *
517
     * @return null|PMF_User_CurrentUser
518
     */
519
    public static function getFromCookie(PMF_Configuration $config)
520
    {
521
        if (!isset($_COOKIE[PMF_Session::PMF_COOKIE_NAME_REMEMBERME])) {
522
            return;
523
        }
524
525
        // create a new CurrentUser object
526
        $user = new self($config);
527
        $user->getUserByCookie($_COOKIE[PMF_Session::PMF_COOKIE_NAME_REMEMBERME]);
528
529
        if (-1 === $user->getUserId()) {
530
            return;
531
        }
532
533
        // sessionId needs to be updated
534
        $user->updateSessionId(true);
535
        // user is now logged in
536
        $user->loggedIn = true;
537
        // save current user to session and return the instance
538
        $user->saveToSession();
539
        // add CSRF token to session
540
        $user->saveCrsfTokenToSession();
541
542
        return $user;
543
    }
544
545
    /**
546
     * Sets the number of minutes when the current user stored in
547
     * the session gets invalid.
548
     *
549
     * @param float $timeout Timeout
550
     */
551
    public function setSessionTimeout($timeout)
552
    {
553
        $this->sessionTimeout = abs($timeout);
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($timeout) can also be of type double. However, the property $sessionTimeout is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
554
    }
555
556
    /**
557
     * Sets the number of minutes when the session-ID needs to be
558
     * updated. By setting the session-ID timeout to zero, the
559
     * session-ID will be updated on each click.
560
     *
561
     * @param float $timeout Timeout
562
     */
563
    public function setSessionIdTimeout($timeout)
564
    {
565
        $this->sessionIdTimeout = abs($timeout);
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($timeout) can also be of type double. However, the property $sessionIdTimeout is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
566
    }
567
568
    /**
569
     * Enables the remember me decision.
570
     */
571
    public function enableRememberMe()
572
    {
573
        $this->rememberMe = true;
574
    }
575
576
    /**
577
     * Saves remember me token in the database.
578
     *
579
     * @param string $rememberMe
580
     *
581
     * @return bool
582
     */
583 View Code Duplication
    protected function setRememberMe($rememberMe)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
584
    {
585
        $update = sprintf("
586
            UPDATE
587
                %sfaquser
588
            SET
589
                remember_me = '%s'
590
            WHERE
591
                user_id = %d",
592
            PMF_Db::getTablePrefix(),
593
            $this->config->getDb()->escape($rememberMe),
594
            $this->getUserId()
595
        );
596
597
        return $this->config->getDb()->query($update);
598
    }
599
600
    /**
601
     * Sets login success/failure.
602
     *
603
     * @param bool $success
604
     *
605
     * @return bool
606
     */
607 View Code Duplication
    protected function setSuccess($success)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
608
    {
609
        $this->loginState = (int) $success;
610
611
        $update = sprintf('
612
            UPDATE
613
                %sfaquser
614
            SET
615
                success = %d
616
            WHERE
617
                user_id = %d',
618
            PMF_Db::getTablePrefix(),
619
            $this->loginState,
620
            $this->getUserId()
621
        );
622
623
        return $this->config->getDb()->query($update);
624
    }
625
626
    /**
627
     * Sets IP and session timestamp, success flag to false.
628
     *
629
     * @return mixed
630
     */
631 View Code Duplication
    protected function setLoginAttempt()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
632
    {
633
        $update = sprintf("
634
            UPDATE
635
                %sfaquser
636
            SET
637
                session_timestamp ='%s',
638
                ip = '%s',
639
                success = 0
640
            WHERE
641
                user_id = %d",
642
            PMF_Db::getTablePrefix(),
643
            $_SERVER['REQUEST_TIME'],
644
            $_SERVER['REMOTE_ADDR'],
645
            $this->getUserId()
646
        );
647
648
        return $this->config->getDb()->query($update);
649
    }
650
651
    /**
652
     * Checks if the last login attempt from current user failed.
653
     *
654
     * @return bool
655
     */
656
    protected function isFailedLastLoginAttempt()
657
    {
658
        $select = sprintf("
659
            SELECT
660
                session_timestamp,
661
                ip,
662
                success
663
            FROM
664
                %sfaquser
665
            WHERE
666
                user_id = %d
667
            AND
668
                session_timestamp < '%s'
669
            AND
670
                ip = '%s'
671
            AND
672
                success = 0",
673
            PMF_Db::getTablePrefix(),
674
            $this->getUserId(),
675
            $_SERVER['REQUEST_TIME'] + $this->lockoutTime,
676
            $_SERVER['REMOTE_ADDR']
677
        );
678
679
        $result = $this->config->getDb()->query($select);
680
        if ($this->config->getDb()->numRows($result) !== 0) {
681
            return true;
682
        } else {
683
            return false;
684
        }
685
    }
686
687
    /**
688
     * Returns the CSRF token from session.
689
     *
690
     * @return string
691
     */
692
    public function getCsrfTokenFromSession()
693
    {
694
        return $_SESSION['phpmyfaq_csrf_token'];
695
    }
696
697
    /**
698
     * Save CSRF token to session.
699
     */
700
    public function saveCrsfTokenToSession()
701
    {
702
        if (!isset($_SESSION['phpmyfaq_csrf_token'])) {
703
            $_SESSION['phpmyfaq_csrf_token'] = $this->createCsrfToken();
704
        }
705
    }
706
707
    /**
708
     * Deletes CSRF token from session.
709
     */
710
    protected function deleteCsrfTokenFromSession()
711
    {
712
        unset($_SESSION['phpmyfaq_csrf_token']);
713
    }
714
715
    /**
716
     * Creates a CSRF token.
717
     *
718
     * @return string
719
     */
720
    private function createCsrfToken()
721
    {
722
        return sha1(microtime().$this->getLogin());
723
    }
724
}
725