Newer
Older
<?php declare(strict_types=1);
namespace uib\ub\loadspeakr\modules;
use uib\ub\loadspeakr\HTTPStatus;
class SessionModule implements ModuleInterface
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
{
//Session module
private $sessionUri = "session";
public function match($uri)
{
global $conf;
global $localUri;
global $lodspk;
$method = ucwords($_SERVER['REQUEST_METHOD']);
$uriSegment = str_replace($conf['basedir'], '', $localUri);
//Check if looking for session validation
if ($uriSegment === $this->sessionUri) {
//GET will return the form
if ($method == "GET") {
$this->showSessionForm();
return true;
}
//POST will take the data and validate it
if ($method == "POST") {
if ($this->validateAuthentication($_POST)) {
session_start();
$_SESSION['lodspk'] = 1;
HTTPStatus::send303($conf['basedir'], '');
return false;
} else {
HTTPStatus::send401("Authentication not valid.");
return true;
}
}
} else {
session_start();
if (isset($_SESSION['lodspk'])) {
return false;
} else {
HTTPStatus::send303($conf['basedir'] . $this->sessionUri, '');
return true;
}
}
public function execute($uri)
{
global $conf;
global $localUri;
global $uri;
global $acceptContentType;
global $endpoints;
global $lodspk;
global $firstResults;
private function showSessionForm()
{
echo "<html>
<form action='" . $this->sessionUri . "' method='POST'>
<input name='user' type='text' />
<input name='password' type='password' /><br/>
<input name='submit' type='submit' />
</form>
</body>
</html>";
private function validateAuthentication($data)
{
global $conf;
if (isset($conf['session']['user']) && isset($conf['session']['password'])) {
if ($data['user'] == $conf['session']['user'] && $data['password'] == $conf['session']['password']) {
return true;
}
return false;
}
return false;
}