-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_sessions.php
174 lines (138 loc) · 3.74 KB
/
functions_sessions.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
<?php
/**
* session class.
*/
class session {
var $user = array();
function __construct() {
global $config, $MySQL, $template;
//Start Session
session_name($config["cookie_name"]);
session_start();
if (isset($_SESSION["user_id"])) {
$this->getUserDetail();
} else {
$this->resetUserDetail();
}
}
function getUserDetail() {
global $config, $MySQL, $template;
//On va chercher nos informations
$ligne = $MySQL->select_one('users','*',array('user_id'=>$_SESSION["user_id"]));
if ($ligne != "") {
$this->user["logged_in"] = true;
$this->user["data"] = array(
"user_id" => $_SESSION["user_id"],
"username" => $ligne["username"],
);
} else {
$this->logout();
}
}
function resetUserDetail() {
$this->user["logged_in"] = false;
$this->user["data"] = array(
"user_id" => -1,
"username" => "Annonyme"
);
}
function setup($require_login = false) {
if ($require_login and !$this->user["logged_in"]) {
//redirect("/login.php?redirect=" . $_SERVER["PHP_SELF"]);
echo "require login";
}
}
/**
* user_data_template function.
*
* @access public
* @return void
*/
function user_data_template() {
$user_data = array(
"LOGGED_IN" => $this->user["logged_in"],
"USERNAME" => $this->user["data"]["username"],
"USER_ID" => $this->user["data"]["user_id"]
);
return $user_data;
}
/**
* login function.
*
* @access public
* @param mixed $name
* @param mixed $pass
* @return void
*/
function login($email, $pass) {
global $MySQL;
//Var qui va contenir les erreurs
$errors = array();
//On va chercher les infos
$ligne = $MySQL->select_one('users','*',array('email'=>$email));
//on vérifie si le user existe
if ($ligne != "") {
//On vérifie le mot de passe
if (crypt(md5($pass), $ligne['password']) == $ligne['password']) {
//On ajoute dans la session
$_SESSION['user_id'] = $ligne['user_id'];
} else {
$errors[] = "Le mot de passe est erroné !";
}
} else {
$errors[] = "Le nom d'utilisateur est introuvable !";
}
return $errors;
}
function logout() {
session_destroy();
$this->resetUserDetail();
//redirect("/");
//exit;
}
}
/**
* request_var function.
*
* @access public
* @param mixed $var
* @param mixed $default
* @param bool $cookie (default: false)
* @return void
*/
function request_var($var_name, $default, $cookie = false) {
if (!$cookie && isset($_COOKIE[$var_name]))
{
if (!isset($_GET[$var_name]) && !isset($_POST[$var_name]))
{
return (is_array($default)) ? array() : $default;
}
$_REQUEST[$var_name] = isset($_POST[$var_name]) ? $_POST[$var_name] : $_GET[$var_name];
}
$super_global = ($cookie) ? '_COOKIE' : '_REQUEST';
if (!isset($GLOBALS[$super_global][$var_name]) || is_array($GLOBALS[$super_global][$var_name]) != is_array($default))
{
return (is_array($default)) ? array() : $default;
} else {
return $GLOBALS[$super_global][$var_name];
}
}
function LEGO_Cache_Data($data) {
global $MySQL;
//Decode as an associative array
$data_json = json_decode($data, true);
//We store the data in the Cache Database
foreach ($data_json['Bricks'] as $i => $brickValue) {
$MySQL->rawQuery("INSERT INTO " . $MySQL->DBprfx . "elementCache (
`elementID`, `designID`, `ColourDescr`, `ItemDescr`, `cachedOn`, `Asset`
) VALUES (
".$brickValue['ItemNo'].",
".$brickValue['DesignId'].",
'".addslashes($brickValue['ColourDescr'])."',
'".addslashes($brickValue['ItemDescr'])."',
".time().",
'".$brickValue['Asset']."'
) ON DUPLICATE KEY UPDATE `designID`=".$brickValue['DesignId'].", `ColourDescr`='".addslashes($brickValue['ColourDescr'])."', `ItemDescr`='".addslashes($brickValue['ItemDescr'])."', `cachedOn`=".time().", `Asset`='".$brickValue['Asset']."'");
}
}
?>