kritbit

kritbit Commit Details


Date:2015-11-18 21:29:43 (9 years 1 month ago)
Author:Natalie Adams
Branch:master
Commit:4c9bba6ae1c2acc5063122fba0aabe69a4c39f7a
Parents: 3c5d9730e7d4934c5c073b79e3a328d4cdf642e1
Message:spaces to tabs

Changes:

File differences

web/application/controllers/base.php
44
55
66
7
8
9
10
11
12
13
14
15
16
17
18
19
7
8
9
10
11
12
13
14
15
16
17
18
19
2020
21
22
23
24
21
22
23
24
2525
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
4743
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
6565
66
67
68
66
67
68
6969
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
70
71
72
73
74
75
76
77
78
79
80
81
8682
87
88
89
90
91
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
92109
abstract class base extends \system\engine\HF_Controller {
/** @var \application\models\Users $user */
protected $user = null;
protected $session = null;
protected $sessionData = null;
protected $loginRequired = true;
protected function isLoggedIn() {
if (!$this->sessionData && !isset($this->sessionData->userId)) {
header("Location: /login");
return false;
} else {
return true;
}
}
/** @var \application\models\Users $user */
protected $user = null;
protected $session = null;
protected $sessionData = null;
protected $loginRequired = true;
protected function isLoggedIn() {
if (!$this->sessionData && !isset($this->sessionData->userId)) {
header("Location: /login");
return false;
} else {
return true;
}
}
protected function loadRender($template, $parameters=array()) {
$newParameters = array_merge($parameters, ["user" => $this->user]);
return parent::loadRender($template, $newParameters);
}
protected function loadRender($template, $parameters=array()) {
$newParameters = array_merge($parameters, ["user" => $this->user]);
return parent::loadRender($template, $newParameters);
}
protected function isUserLoggedIn() {
if (isset($_COOKIE["session"])) {
$validSession = Sessions::getByField("sessionid", $_COOKIE["session"]);
if ($validSession) {
try {
$this->session = $validSession[0];
$this->sessionData = json_decode($this->session->data);
if ($this->sessionData == null) {
return false;
}
$this->user = \application\models\Users::getByField("id", $this->sessionData->userId)[0];
return true;
} catch (\Exception $e) {
return false;
}
} else {
return false;
}
}
return false;
}
protected function setupUser() {
if (isset($_COOKIE["session"])) {
$validSession = Sessions::getByField("sessionid", $_COOKIE["session"]);
if ($validSession) {
try {
$this->session = $validSession[0];
$this->sessionData = json_decode($this->session->data);
if ($this->sessionData == null) {
return;
}
$this->user = \application\models\Users::getByField("id", $this->sessionData->userId)[0];
} catch (\Exception $e) {
}
} else {
}
}
}
protected function login() {
if (isset($_COOKIE["session"])) {
if (!$this->user) {
header("Location: /login");
}
} else {
$bool = true;
$bytes = openssl_random_pseudo_bytes(10, $bool);
$sessionId = bin2hex($bytes);
$this->session = new Sessions();
$this->session->ip = $_SERVER["REMOTE_ADDR"];
$this->session->userAgent = $_SERVER["HTTP_USER_AGENT"];
$this->session->sessionid = $sessionId;
$this->session->save();
setcookie("session", $sessionId, 2147483647);
}
}
protected function isUserLoggedIn() {
if (isset($_COOKIE["session"])) {
$validSession = Sessions::getByField("sessionid", $_COOKIE["session"]);
if ($validSession) {
try {
$this->session = $validSession[0];
$this->sessionData = json_decode($this->session->data);
if ($this->sessionData == null) {
return false;
}
$this->user = \application\models\Users::getByField("id", $this->sessionData->userId)[0];
return true;
} catch (\Exception $e) {
return false;
}
} else {
return false;
}
}
return false;
}
public function __construct($config, $core, $tpl)
{
parent::__construct($config, $core, $tpl);
protected function login() {
if (!$this->user) {
header("Location: /login");
if ($this->config["DATABASE_TYPE"] == "SQLITE") {
$this->pdo = new PDO("sqlite:kritbot.sqlite3");
\vendor\DB\DB::$c = $this->pdo;
} else {
$this->pdo = new PDO(
"mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}",
$this->config['MYSQL_USER'],
$this->config['MYSQL_PASS'],
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
\vendor\DB\DB::$c = $this->pdo;
}
} else {
$bool = true;
$bytes = openssl_random_pseudo_bytes(10, $bool);
$sessionId = bin2hex($bytes);
$this->session = new Sessions();
$this->session->ip = $_SERVER["REMOTE_ADDR"];
$this->session->userAgent = $_SERVER["HTTP_USER_AGENT"];
$this->session->sessionid = $sessionId;
$this->session->save();
setcookie("session", $sessionId, 2147483647);
}
}
$this->isUserLoggedIn();
if ($this->loginRequired) {
$this->login();
}
}
public function __construct($config, $core, $tpl)
{
parent::__construct($config, $core, $tpl);
if ($this->config["DATABASE_TYPE"] == "SQLITE") {
$this->pdo = new PDO("sqlite:kritbot.sqlite3");
\vendor\DB\DB::$c = $this->pdo;
} else {
$this->pdo = new PDO(
"mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}",
$this->config['MYSQL_USER'],
$this->config['MYSQL_PASS'],
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
\vendor\DB\DB::$c = $this->pdo;
}
$this->setupUser();
if ($this->loginRequired) {
$this->login();
}
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.05745s using 14 queries.