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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
<?php
include("config/parametres.php");
include("includes/sessions.php");
include("includes/functions.php");
## -- 01. User Management Functions ////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
## LOGIN ---------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////
function try_login() {
// --- CASE ONE --- -- -- - - -
## User is loged in already
if ( (isset($_SESSION['user_status'])) && ($_SESSION['user_status'] == 1) ) {
return array(
'success' => true,
'data' => common_session_data__('array')
);
}
// --- CASE TWO --- -- -- - - -
## User POSTed credentials to authenticate
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { // if data posted, then ...
## check if user made too many attempts; if so, add delay
// -------------------------------------------------------------------
if (!isset($_SESSION['login_attempts'])) {
$_SESSION['login_attempts'] = array();
$_SESSION['login_attempts'][] = time();
}
else { // Check login attempts
$TOTatts = sizeof($_SESSION['login_attempts']);
// DENY if too many deny and force user to try later
if (($TOTatts > 32) && (time()-$_SESSION['login_attempts'][$TOTatts - 1] < 600))
return array('success' => false, 'data' => "Can not check; please try again after 10 minutes.");
if (($TOTatts > 16) && (time()-$_SESSION['login_attempts'][$TOTatts - 1] < 300))
return array('success' => false, 'data' => "Can not check; please try again after 5 minutes.");
if (($TOTatts > 8) && (time()-$_SESSION['login_attempts'][$TOTatts - 1] < 55))
return array('success' => false, 'data' => "Can not check; please try after 1 minute.");
if (($TOTatts > 4) && (time()-$_SESSION['login_attempts'][$TOTatts - 1] < 25))
return array('success' => false, 'data' => "Can not check; please try after 30 seconds. Forgot your password? Contact the administrator!");
// if too many attempts but stil ok on timings
// remove the first 10 attempts
if ($TOTatts > 75) {
for ($ti = 0 ; $ti < 16 ; $ti++) array_shift($_SESSION['login_attempts']);
}
// if not gone already allow try to login; so log the attempt and continue
$_SESSION['login_attempts'][] = time();
}
## validate posted data (email, password)
// -------------------------------------------------------------------
// POST sent
$post = array('user' => $_POST['user'], 'pass' => $_POST['pass']);
// validation rules
$rules = array('user' => FILTER_VALIDATE_EMAIL, 'pass' => true );
// sanitize input array
$input = filter_var_array($post, $rules);
// check if all valid and non empty; keep errors
$valid = true; // valid (bool)
$error = array(); // errors (array)
$nonEmpty = array('user', 'pass'); // required fields
foreach($input as $k => $v) {
if ($v === false) { // check not validated
$valid = false; $error[] = $k ." not validated";
}
if ( (($v =="") || ($v === false)) && (in_array($k, $nonEmpty)) ) { // check required non empty
$valid = false; $error[] = $k . " is required";
}
}
// echo "inp: "; print_r($input); echo "\nerr: "; print_r($error); die();
if (!$valid) { // if no valid data
return array('success' => false, 'data' => join(", ", $error)); // return error(s)
}
## CHECK DATABASE FOR USER
// use prepare statements for security
// -------------------------------------------------------------------
// init database connection
$_dbc = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBMS);
if ($_dbc->connect_errno) {
$_cError = "Database connection failed; Please try in a few minutes. ";
if (TESTING) echo $_dbc->connect_error;
$_dbc->close(); die();
}
$_dbc->set_charset("utf8");
// prepare statemet
$_stm = $_dbc->prepare("SELECT user_id, `user`, salt, pass, role, realname, birthstamp FROM `users` WHERE `user` = ?");
$_stm->bind_param("s", $post['user']); // bind values
$_stm->execute(); // execute query
$_stm->store_result(); // store result
if ($_stm->num_rows == 1) { // only one user should exist
$_stm->bind_result($uid, $user, $salt, $pass, $role, $name, $birth); // bind result variables
$_stm->fetch(); // fetch FIRST record values
}
else { // return error(s)
return array('success' => false, 'data' => "More than one users? Please contact system administrator");
}
$_stm->close(); // close statement
$_dbc->close(); // close connection
## decide if user is ok
// -------------------------------------------------------------------
if ( $pass == md5($post['user'].$salt.$post['pass']) ) { // User is validated and ok
session_unset(); // unset $_SESSION, and
session_destroy(); // destroy current session's data before continue;
session_start(); // then start a new session;
session_regenerate_id(); // get a new session id to mitigate session fixation
// setup his session data
$_SESSION['user_status'] = 1; // update session status
$_SESSION['sess_expire'] = time() + SESSION_TTL; // session will expire after SESS_TTL seconds
$_SESSION['sess_update'] = time() + SESSION_UPD; // session need to update agter SESS_UPD sec
// keep critical user info into session
$_SESSION['uid'] = $uid;
$_SESSION['name'] = $name;
$_SESSION['role'] = $role;
$_SESSION['email'] = $post['user'];
$_SESSION['birth'] = $birth;
return array('success' => true, 'data' => array('uid' => $uid, 'name' => $name, 'role' => $role) );
}
else { // user is not ok unset critical session variables
$_SESSION['user_status'] = 0;
if (isset($_SESSION['uid'])) unset($_SESSION['uid']);
if (isset($_SESSION['name'])) unset($_SESSION['name']);
if (isset($_SESSION['role'])) unset($_SESSION['role']);
if (isset($_SESSION['role'])) unset($_SESSION['role']);
// then return failed
return array('success' => false, 'data' => "Not valid user credentials.");
}
} // --- end CASE TWO
// --- CASE THREE --- -- -- - - -
## credentials not send via POST
else return array('success' => false, 'data' => "Data not sent correctly");
}
## LOGOUT --------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////
function try_logout() {
session_unset(); // unset, and
session_destroy(); // destroy session data;
session_start(); // then start a new session
session_regenerate_id(); // get a new session id to mitigate session fixation
// remove cookies
if (isset($_COOKIE[MAIN_COOKIE])) unset($_COOKIE[MAIN_COOKIE]);
if (isset($_COOKIE[USER_COOKIE])) unset($_COOKIE[USER_COOKIE]);
return array('success' => true, 'data' => "User disconected!"); // no chance for the function to fail
}
## -- 02. Items Catalog Management ////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
## ADD ITEM ------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////
function add_item() {
if ((ROLE) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
// POST sent
$post = array(
'ccode' => $_POST['ccode'],
'summary' => $_POST['summary'],
'condition' => $_POST['condition'],
'decade' => $_POST['decade'],
'operator' => $_SESSION['name'],
'timestamp' => time(),
'box' => $_POST['box'],
'exhibition' => $_POST['exhibition'],
'record' => serialize($_POST)
);
// VALIDATE RULES
$rules = array(
'ccode' => true,
'summary' => FILTER_SANITIZE_STRING,
'condition' => array('filter' => FILTER_VALIDATE_REGEXP, "options" => array("regexp" => "/^[A-Za-z0-9,]+$/")),
'decade' => FILTER_VALIDATE_INT,
'operator' => true,
'timestamp' => true,
'box' => FILTER_SANITIZE_STRING,
'exhibition' => FILTER_SANITIZE_STRING,
'record' => true
);
// SANITIZE input array
$input = filter_var_array($post, $rules);
// CHECK if all VALID and NON EMPTY
// --- -- -- - - -
$valid = true; // valid flag
$error = array(); // errors array
$nonEmpty = array( // required fields
'ccode','operator','timestamp'
);
foreach($input as $k => $v) {
// check not validated
if ($v === false) {
$valid = false;
$error[] = $k ." not validated";
}
// check empty from required
if ( (($v =="") || ($v === false)) && (in_array($k, $nonEmpty)) ) {
$valid = false;
$error[] = $k . " is required";
}
}
// next line is check (uncomment for checking)
// if (TESTING) { echo "\nses: "; print_r($_SESSION); echo "\npost:"; print_r($_POST); echo "\n, inp: "; print_r($input); echo "\nerr: "; print_r($error); die(); }
// IF VALID add item
// --- -- -- - - -
if ($valid) {
// init database connection
$_dbc = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBMS);
if ($_dbc->connect_errno) {
echo "Database connection failed; Please try in a few minutes.";
if (TESTING) echo $_dbc->connect_error;
$_dbc->close(); die();
}
$_dbc->set_charset("utf8");
$_stm = $_dbc->prepare("INSERT INTO items
(ccode, summary, `condition`, decade, operator, `timestamp`, `box`, exhibition, record)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
);
$_stm->bind_param("sssisdsss",
$post['ccode'], $post['summary'], $post['condition'], $post['decade'], $post['operator'], $post['timestamp'], $post['box'], $post['exhibition'], $post['record']
);
$_stm->execute();
$_newid = $_dbc->insert_id;
$_stm->close();
if ($_newid > 0) {
// update individual category table
// -------------------------------------------------------------------
$cc = parse_ccode($post['ccode']);
$_Table = 'tt_'. $cc->idx; // find table
// do the insert
$_stm2 = $_dbc->prepare("INSERT INTO {$_Table} ( id ) VALUES ( ? )");
$_stm2->bind_param("d", $_newid);
$_stm2->execute();
$_iid = $_dbc->insert_id; // get new index
$_stm2->close();
// update record with individual index
// -------------------------------------------------------------------
$identity = $post['ccode'] .'.'. $_iid;
// do the insert
$_upd = $_dbc->prepare("UPDATE items SET identity = ? WHERE id = ?");
$_upd->bind_param("sd", $identity, $_newid);
$_upd->execute();
$_upd->close();
return array('success' => true, 'data' => "Item added", id => $_newid );
}
else
return array('success' => false, 'data' => "Unknown database error. Please contact the administrator");
}
else
return array('success' => false, 'data' => implode(", ", $error));
}
else return array('success' => false, 'data' => "Operation Denied.");
}
## UPDATE ITEM ---------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////
function update_item($id) {
if ((ROLE) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
// POST sent
// * CCODE : sent correctly but WILL NOT CHANGE; exclude it from UPDATE statement
// * OPERATOR, TIMESTAMP : not need to change
$post = array(
'summary' => $_POST['summary'],
'condition' => $_POST['condition'],
'decade' => $_POST['decade'],
'box' => $_POST['box'],
'exhibition' => $_POST['exhibition'],
'record' => serialize($_POST)
);
// VALIDATE RULES
$rules = array(
'summary' => FILTER_SANITIZE_STRING,
'condition' => array('filter' => FILTER_VALIDATE_REGEXP, "options" => array("regexp" => "/^[A-Za-z0-9,]+$/")),
'decade' => FILTER_VALIDATE_INT,
'box' => FILTER_SANITIZE_STRING,
'exhibition' => FILTER_SANITIZE_STRING,
'record' => true
);
// SANITIZE input array
$input = filter_var_array($post, $rules);
// CHECK if all VALID and NON EMPTY
// * ALL required fields ARE ALREADY SET; no need to check if empty
// --- -- -- - - -
$valid = true; // valid flag
$error = array(); // errors array
foreach($input as $k => $v) {
// check not validated
if ($v === false) {
$valid = false;
$error[] = $k ." not validated";
}
}
// next line is check (uncomment for checking)
// if (TESTING) { echo "\nses: "; print_r($_SESSION); echo "\npost:"; print_r($_POST); echo "\n, inp: "; print_r($input); echo "\nerr: "; print_r($error); die(); }
// IF VALID add item
// --- -- -- - - -
if ($valid) {
// init database connection
$_dbc = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBMS);
if ($_dbc->connect_errno) {
echo "Database connection failed; Please try in a few minutes.";
if (TESTING) echo $_dbc->connect_error;
$_dbc->close(); die();
}
$_dbc->set_charset("utf8");
$_stm = $_dbc->prepare("UPDATE items
SET summary = ?, `condition` = ?, decade = ?, `box` = ?, exhibition = ?, record = ?
WHERE id = ?"
);
$_stm->bind_param("ssisssd",
$post['summary'], $post['condition'], $post['decade'], $post['box'], $post['exhibition'], $post['record'], $id
);
$_stm->execute();
$_stm->close();
return array('success' => true, 'data' => "Item Updated", id => $_newid );
}
else
return array('success' => false, 'data' => implode(", ", $error));
}
else return array('success' => false, 'data' => "Operation Denied.");
}
## -- 10. SWITCH THE requested CASES ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// parse GET argument(s)
$do = (isset($_GET['do'])) ? $_GET['do'] : "none";
// switch the 'do' cases
switch ($do) {
case 'login':
$reply = try_login();
echo json_encode($reply, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
case 'logout':
$reply = try_logout();
echo json_encode($reply, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
case 'additem':
$reply = add_item();
echo json_encode($reply, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
case 'upditem':
if (isset($_GET['id']) && is_numeric($_GET['id']) && (($_GET['id'] != '0'))) {
$item_id = $_GET['id']; // id of photo to upload
$reply = update_item($item_id);
}
else {
$reply = array('success' => false, 'data' => "ID error");
}
echo json_encode($reply, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
default:
// code...
break;
}
|