forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthentication.php
More file actions
120 lines (101 loc) · 2.62 KB
/
Authentication.php
File metadata and controls
120 lines (101 loc) · 2.62 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
<?PHP
namespace helpers;
/**
* Helper class for authenticate user
*
* @package helpers
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
class Authentication {
/**
* loggedin
* @var bool
*/
private $loggedin = false;
/**
* enabled
* @var bool
*/
private $enabled = false;
/**
* start session and check login
*/
public function __construct() {
// session cookie will be valid for one month
session_set_cookie_params((3600*24*30), "/");
session_name();
if(session_id()=="")
session_start();
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin']===true)
$this->loggedin = true;
$this->enabled = strlen(trim(\F3::get('username')))!=0 && strlen(trim(\F3::get('password')))!=0;
// autologin if request contains unsername and password
if( $this->enabled===true
&& $this->loggedin===false
&& isset($_REQUEST['username'])
&& isset($_REQUEST['password'])) {
$this->login($_REQUEST['username'], $_REQUEST['password']);
}
}
/**
* login enabled
*
* @return bool
* @param string $username
* @param string $password
*/
public function enabled() {
return $this->enabled;
}
/**
* login user
*
* @return bool
* @param string $username
* @param string $password
*/
public function loginWithoutUser() {
$this->loggedin = true;
}
/**
* login user
*
* @return bool
* @param string $username
* @param string $password
*/
public function login($username, $password) {
if($this->enabled()) {
if(
$username == \F3::get('username') && hash("sha512", \F3::get('salt') . $password) == \F3::get('password')
) {
$this->loggedin = true;
$_SESSION['loggedin'] = true;
return true;
}
}
return false;
}
/**
* isloggedin
*
* @return bool
*/
public function isLoggedin() {
if($this->enabled()===false)
return true;
return $this->loggedin;
}
/**
* logout
*
* @return void
*/
public function logout() {
$this->loggedin = false;
$_SESSION['loggedin'] = false;
session_destroy();
}
}