forked from AnttiKurittu/kirjuri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude_functions.php
736 lines (649 loc) · 27.8 KB
/
include_functions.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<?php
/* This is the 'header' file in all php files containing shared functions etc.
** It also performs some other tasks.
*/
$mysql_timer_start = microtime(true);
if (version_compare(PHP_VERSION, '7.0.0') <= 0) {
echo "Kirjuri requires PHP7 to run. You are using " . phpversion() . ". Please upgrade your PHP environment.";
die;
}
// Go to installer if no credentials found.
if (!file_exists('conf/mysql_credentials.php')) {
header('Location: install.php');
die;
}
// Load dependencies
require __DIR__.'/vendor/autoload.php';
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$loader = new Twig_Loader_Filesystem('views/');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
'auto_reload' => true,
));
$pur_config = HTMLPurifier_Config::createDefault();
$pur_config->set('Cache.SerializerPath', './cache');
$purifier = new HTMLPurifier($pur_config);
session_name('KirjuriSessionID');
session_start(); // Start a PHP session
foreach ($_GET as $key => $value) { // Lightly sanitize GET variables
$strip_chars = array("<", ">", "'", ";");
$value = str_replace($strip_chars, "", $value);
$_GET[$key] = isset($value) ? $value : '';
}
// Set variables for message display.
$_SESSION['message_set'] = isset($_SESSION['message_set']) ? $_SESSION['message_set'] : '';
$_SESSION['user'] = isset($_SESSION['user']) ? $_SESSION['user'] : ''; //
// If message has been set, do not clear it. Invidial files set message as shown before rendering page.
if ($_SESSION['message_set'] === false) {
$_SESSION['message']['type'] = '';
$_SESSION['message']['content'] = '';
}
function event_log_write($case_id = "0", $event_level = "Action", $description, $audit_log_file = "-") {
// Logging function.
if (!isset($_SESSION['user']['token'])) {
$sessiontoken = "-";
} else {
$sessiontoken = $_SESSION['user']['token'];
}
if (!isset($_SESSION['user']['username'])) {
$session_username = "-";
} else {
$session_username = $_SESSION['user']['username'];
}
$case_id = filter_numbers($case_id);
$log = strftime("%d/%b/%Y:%H:%M:%S %z", time()).';'.$session_username.';'.$event_level.';"'.$description.'";'.$_SERVER['REQUEST_URI'].';'.$_SERVER['REMOTE_ADDR'].';'.$audit_log_file.';Session ID: '.$sessiontoken.';';
if ($case_id === "0") {
file_put_contents('logs/kirjuri.log', $log."-;\r\n", FILE_APPEND);
return true;
}
else {
if (!file_exists('logs/cases/')) {
mkdir('logs/cases');
}
if (!file_exists('logs/cases/uid'. $case_id)) {
mkdir('logs/cases/uid'. $case_id);
}
$case_logfile = 'logs/cases/uid' . $case_id . '/events.log';
}
file_put_contents($case_logfile, $log."\r\n", FILE_APPEND);
file_put_contents('logs/kirjuri.log', $log."uid".$case_id.";"."\r\n", FILE_APPEND);
return true;
}
function kirjuri_error_handler($errno, $errstr, $errfile, $errline) // Trigger an error
{
global $twig;
global $prefs;
if ($prefs['settings']['show_errors'] === '1') {
// Show a message if errors are permitted on screen.
$errnums = array(
'1' => 'Error',
'2' => 'Warning',
'4' => 'Parse error',
'8' => 'Notice',
'16' => 'Core error',
'32' => 'Compile warning',
'64' => 'Compile error',
'128' => 'Compile warning',
'256' => 'User error',
'512' => 'User warning',
'1024' => 'User notice',
'2048' => 'Strict',
'4096' => 'Recoverable error',
'8192' => 'Deprecated',
'16384' => 'User deprecated',
'32767' => 'All errors'
);
$_SESSION['message']['type'] = 'error';
$_SESSION['message']['content'] = $errnums[$errno].': '.$errstr;
$_SESSION['message_set'] = true;
}
event_log_write('0', 'Error', $errno.' '.$errstr.', File: '.$errfile.', line '.$errline);
}
function array_trim($array) {
foreach ($array as $key => $value) {
if ( ($value === "") || ($value === null) ) {
unset($array[$key]);
}
}
return $array;
}
function local_authenticate($username, $password) {
// Authenticate against a local account
$username = filter_username($username);
try {
$kirjuri_database = connect_database('kirjuri-database');
$query = $kirjuri_database->prepare('SELECT * FROM users WHERE username = :username AND (NOT attr_3 = :attr_3 OR attr_3 IS NULL) LIMIT 1');
$query->execute(array(':username' => $username, ':attr_3' => "LDAP_AUTH_ONLY"));
$user_record = $query->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
if (password_verify($password, $user_record['password'])) {
$_SESSION['user'] = $user_record;
event_log_write('0', "Auth", "Succesful local authentication for user " . $username);
return true;
} else {
$_SESSION['user'] = null;
event_log_write('0', "Auth", "Failure on local authentication for user " . $username);
return false;
}
}
function ldap_authenticate($username, $password) {
// Source: https://www.exchangecore.com/blog/how-use-ldap-active-directory-authentication-php/
//$username = filter_username($username);
global $prefs;
if ($prefs['settings']['enable_ldap_authentication'] !== "1") {
return false;
}
$ldap_domain = $prefs['settings']['ldap_domain'];
$search_string = $prefs['settings']['ldap_search_string'];
$ldaprdn = $ldap_domain . "\\" . $username;
if (strpos($username, '@') !== false) {
$ldaprdn = $username;
$username = substr( $ldaprdn, 0, strpos($username, '@') );
}
event_log_write('0', 'Auth', 'LDAP login attempt: ' . $ldaprdn . ' for ' . $username);
$ldap = ldap_connect($prefs['settings']['ldap_server_address']);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = @ldap_bind($ldap, $ldaprdn, $password);
if ($bind) { // On succesfull LDAP auth.
$allowedNetgroups = explode(',', str_replace(' ', '', $prefs['settings']['ldap_allowed_netgroups']) );
// see if user is a member of an allowed netgroup
$filter="(sAMAccountName=".$username.")";
$result = ldap_search($ldap, $search_string, $filter);
$info = ldap_get_entries($ldap, $result);
$isMember = false;
if ($prefs['settings']['ldap_allowed_netgroups'] == '') {
$isMember = true;
}
for ($i=0; $i<$info['count']; $i++) {
if ($info['count'] > 1)
break;
$ldap_realname = $info[$i]["displayname"][0];
for ($j=0; $j<$info[$i]['memberof']['count']; $j++) {
if ($isMember) {
break;
}
foreach ($allowedNetgroups as $thisInst) {
$thisGroup = 'CN=' . $thisInst . ',';
$thisLen = strlen($thisGroup);
if (substr($info[$i]['memberof'][$j], 0, $thisLen) === $thisGroup) {
$isMember = true;
event_log_write('0', 'Auth', 'LDAP netgroup match found: ' . $thisInst);
break;
}
}
}
}
if ($isMember === false) {
event_log_write('0', 'Auth', 'LDAP user not a member of any required group');
return false;
}
@ldap_close($ldap);
try {
$kirjuri_database = connect_database('kirjuri-database');
$query = $kirjuri_database->prepare('SELECT * FROM users WHERE username = :username AND attr_3 = "LDAP_AUTH_ONLY" LIMIT 1');
$query->execute(array(':username' => $username));
$user_record = $query->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
if (empty($user_record)) {
$query = $kirjuri_database->prepare('SELECT * FROM users WHERE username = :username AND (NOT attr_3 = :attr_3 OR attr_3 IS NULL) LIMIT 1');
$query->execute(array(':username' => $username, ':attr_3' => "LDAP_AUTH_ONLY"));
$user_record = $query->fetch(PDO::FETCH_ASSOC);
if ($user_record !== false) {
event_log_write('0', "Error", "Local-only account exists for succesfully remote authenticated user " . $username);
return false; // FAIL LOGIN IF LOCAL ACCOUNT EXISTS
} else {
$query = $kirjuri_database->prepare('INSERT INTO users (username, password, name, access, flags, attr_1, attr_2, attr_3, attr_4, attr_5, attr_6, attr_7, attr_8)
VALUES (:username, :password, :name, :access, :flags, :attr_1, :attr_2, :attr_3, NULL, NULL, NULL, NULL, NULL)');
$query->execute(array(
':username' => $username,
':name' => $ldap_realname,
':password' => "API_ONLY_" . generate_token(32),
':flags' => "MF",
':access' => "1",
':attr_1' => 'User imported from LDAP ' . $_SESSION['user']['username'] . ' at ' . date('Y-m-d H:i'),
':attr_2' => "",
':attr_3' => "LDAP_AUTH_ONLY"
));
}
event_log_write('0', "Auth", "LDAP: Created account for user " . $username);
$query = $kirjuri_database->prepare('SELECT * FROM users WHERE username = :username AND name = :name AND attr_3 = :attr_3');
$query->execute(array(
':username' => $username,
':name' => $ldap_realname,
':attr_3' => "LDAP_AUTH_ONLY"
));
$user_record = $query->fetch(PDO::FETCH_ASSOC);
$_SESSION['user'] = $user_record;
event_log_write('0', "Auth", "Succesful remote authentication for user " . $username);
return true;
} elseif ($username === $user_record['username']) {
event_log_write('0', "Auth", "Succesful remote authentication for user " . $username);
$_SESSION['user'] = $user_record;
return true;
} else {
echo "Something went really wrong.";
die;
}
}
// LDAP auth failure
return false;
}
function ip_allowed() {
$access_allowed_from_ip = false; // Deny access by default
$ip_access_list = json_decode($_SESSION['user']['attr_2'], TRUE); // Get blacklists
if (empty($ip_access_list)) {
$ip_access_list['allow'] = array();
$ip_access_list['deny'] = array();
}
if (file_exists('conf/access_list.php')) {
$global_ip_access_list = include 'conf/access_list.php';
foreach ($global_ip_access_list['allow'] as $ip) {
array_push($ip_access_list['allow'], $ip);
}
foreach ($global_ip_access_list['deny'] as $ip) {
array_push($ip_access_list['deny'], $ip);
}
unset($global_ip_access_list);
}
if (!empty($ip_access_list['allow'][0])) {
foreach ($ip_access_list['allow'] as $ip) {
if (ip_in_range($_SERVER['REMOTE_ADDR'], $ip)) {
$access_allowed_from_ip = true;
}
}
} else {
$access_allowed_from_ip = true; // No whitelist set, default to allow.
}
if (!empty($ip_access_list['deny'][0])) {
foreach ($ip_access_list['deny'] as $ip) {
if (ip_in_range($_SERVER['REMOTE_ADDR'], $ip)) {
$access_allowed_from_ip = false; // If IP is on blacklist, deny.
}
}
}
return $access_allowed_from_ip;
}
function filter_username($username) {
$strip_chars = array("!", "<", ">", "'", ":", ";", "/", "\"", "#", "%", "\\", "&", "|", "?", "*", "$", ")", "(", "[", "]", "{", "}");
$username = strtolower(trim(str_replace($strip_chars, "", $username)));
return $username;
}
function upgrade_insecure_password($username, $password) {
$kirjuri_database = connect_database('kirjuri-database');
$query = $kirjuri_database->prepare('UPDATE users SET password = :secure_password_hash WHERE username = :username AND password = :legacy_password');
$query->execute(array(
':secure_password_hash' => password_hash($password, PASSWORD_DEFAULT),
':username' => $username,
':legacy_password' => hash('sha256', $password)
));
return $query->rowCount();
}
function generate_token($length) {
// Generate a token for use as a session token.
return substr(str_shuffle(hash('sha256', random_bytes(1024))), 0, $length);
}
function seconds_to_time($seconds) {
// Thanks to https://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
function delete_directory($dir) {
// Thanks to http://stackoverflow.com/questions/1653771/how-do-i-remove-a-directory-that-is-not-empty
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!delete_directory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
function ip_in_range( $ip, $range ) {
if ( strpos( $ip, ":" ) !== false ) {
// Return default false for ipv6 addresses.
return false;
}
// Copied and modified from https://gist.github.com/tott/7684443, thanks!
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
// $range is in IP/CIDR format eg 127.0.0.1/24
list( $range, $netmask ) = explode( '/', $range, 2 );
$range_decimal = ip2long( $range );
$ip_decimal = ip2long( $ip );
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal );
}
function ksess_init() {
// Initialize a session token.
$_SESSION['user']['token'] = generate_token(16); // Set session token
if (!file_exists('cache/user_' . $_SESSION['user']['username'])) {
mkdir('cache/user_' . $_SESSION['user']['username']);
}
file_put_contents('cache/user_' . $_SESSION['user']['username'] . '/session_' . $_SESSION['user']['token'] . '.txt', $_SESSION['user']['username'] . ' is logged in at ' . $_SERVER['REMOTE_ADDR'] . ', user agent ' . $_SERVER['HTTP_USER_AGENT'] . '. Request timestamp ' . gmdate("Y-m-d\TH:i:s\Z", $_SERVER['REQUEST_TIME']) . ". Remove this file to force logout.\r\n");
}
function ksess_validate($token) {
// Validate a session token against token stored on user session.
if ($token === $_SESSION['user']['token']) {
return true;
}
else {
trigger_error("CSRF token mismatch. Try again.");
if (isset($_SERVER['HTTP_REFERER'])) {
header('Location: '.$_SERVER['HTTP_REFERER']);
} else {
header('Location: index.php');
}
die();
}
}
function ksess_destroy() {
// Destroy a session file.
if (isset($_SESSION['user']['username'])) {
if (file_exists('cache/user_' . $_SESSION['user']['username'] . '/session_' . $_SESSION['user']['token'] . '.txt')) {
unlink('cache/user_' . $_SESSION['user']['username'] . '/session_' . $_SESSION['user']['token'] . '.txt');
}
}
if (!isset($_SESSION['user']['token'])) {
$_SESSION['user']['token'] = "Not set.";
}
event_log_write('0', "Auth", "Destroyed session " . $_SESSION['user']['token']);
$_SESSION = null;
session_destroy();
header('Location: login.php');
die;
}
function csrf_case_validate($token, $case_id) {
// Validate a case access token. A case access token is generated on succesful
// opening of a case.
if (empty($token)) {
trigger_error("Case access token missing. Try again.");
if (isset($_SERVER['HTTP_REFERER'])) {
header('Location: '.$_SERVER['HTTP_REFERER']);
} else {
header('Location: index.php');
}
}
if (($token === $_SESSION['case_token'][$case_id]) || ($_SESSION['user']['access'] === "0")) {
return true;
}
else {
trigger_error("Case access token mismatch. Try again.");
if (isset($_SERVER['HTTP_REFERER'])) {
header('Location: '.$_SERVER['HTTP_REFERER']);
} else {
header('Location: index.php');
}
die();
}
}
function ksess_verify($required_access_level) {
// Check user access level before rendering page. User details are stored in a session variable.
if (!isset($_SESSION['user']['username'])) {
ksess_destroy();
}
if (!file_exists('cache/user_' . $_SESSION['user']['username'] . "/session_" . $_SESSION['user']['token'] . ".txt" )) {
// Drop session if session file has been removed.
$_SESSION = array();
session_destroy();
header('Location: login.php');
die;
}
if ((empty($_SESSION['user']) && $_SERVER['PHP_SELF'] !== '/api.php')) {
// Check if user variable is set.
header('Location: login.php');
die;
} else {
if ($_SESSION['user']['access'] > $required_access_level) {
message('Access', $_SESSION['lang']['insufficient_privileges']);
if (isset($_SERVER['HTTP_REFERER'])) {
header('Location: '.$_SERVER['HTTP_REFERER']);
} else {
header('Location: index.php');
}
die;
} else {
return true;
}
}
}
function verify_case_ownership($id) {
global $kirjuri_database;
if ($_SESSION['user']['access'] === "0") {
return true;
}
$query = $kirjuri_database->prepare('SELECT case_owner FROM exam_requests WHERE id = :id');
$query->execute(array(':id' => $id));
$case_owner = $query->fetch(PDO::FETCH_ASSOC);
$case_owner = explode(";", $case_owner['case_owner']);
if ( (in_array($_SESSION['user']['username'], $case_owner)) || (empty($case_owner[0])) ) {
return true;
}
else {
event_log_write($id, 'Error', 'User initiated out-of-bounds POST request to case where not in access group.');
message('error', $_SESSION['lang']['not_in_access_group']);
header('Location: index.php');
die;
}
}
function filter_html($string) // Purify HTML content for raw presentation.
{
if (empty($string)) {
return "";
}
else {
global $purifier;
$out = $purifier->purify($string);
if (empty($out)) {
message('error', 'Invalid HTML input.');
header('Location: '.$_SERVER['HTTP_REFERER']);
die;
}
return $out;
}
}
function filter_numbers($a) // Filter out everything but numbers.
{
return preg_replace('/[^0-9]/', '', $a);
}
function filter_letters_and_numbers($a) {
return preg_replace('/[^a-zA-Z0-9_]/', '', $a);
}
function encrypt($in, $key) {
// Encrypt a string with AES-256-CBC
if (!function_exists('openssl_encrypt')) {
event_log_write('0', 'Error', 'Missing dependency: OpenSSL. Can not encrypt audit log files. Please install OpenSSL.');
return $in;
}
$iv = trim(substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 16));
$key = base64_encode($key);
$in = gzencode($in);
$encrypted = openssl_encrypt($in, 'AES-256-CBC', $key, 0, $iv);
return $iv.$encrypted;
}
function decrypt($in, $key) {
// Decrypt a string.
if (!function_exists('openssl_decrypt')) {
event_log_write('0', 'Error', 'Missing dependency: OpenSSL. Can not decrypt audit log files. Please install OpenSSL.');
return $in;
}
$iv = substr($in, 0, 16);
$key = base64_encode($key);
$decrypted = openssl_decrypt(substr($in, 16), 'AES-256-CBC', $key, 0, $iv);
$decrypted = gzdecode($decrypted);
return $decrypted;
}
function show_saved_succesfully() {
// Display a "changes saved"-message
$_SESSION['message']['type'] = 'info';
$_SESSION['message']['content'] = $_SESSION['lang']['changes_saved'];
$_SESSION['message_set'] = true;
return true;
}
function message($type = "info", $content) {
// Display a message. Message is rendered by Twig in base.twig, class set according to $type, either error or info.
$_SESSION['message']['type'] = $type;
$_SESSION['message']['content'] = $content;
$_SESSION['message_set'] = true;
return true;
}
function connect_database($database) {
// PDO Database connector
global $mysql_config;
global $prefs;
if (!isset($mysql_config['mysql_server'])) {
$server = 'localhost';
} else {
$server = $mysql_config['mysql_server'];
}
if ($database === 'kirjuri-database') {
try {
$pdo_connect_string = 'mysql:host='.$server.';dbname='.$mysql_config['mysql_database'];
$kirjuri_database = new PDO($pdo_connect_string, $mysql_config['mysql_username'], $mysql_config['mysql_password']);
$kirjuri_database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$kirjuri_database->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$kirjuri_database->exec('SET NAMES utf8');
return $kirjuri_database;
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
}
}
function audit_log_write($post_data) {
// Store an audit log entry of the request.
if (!file_exists('conf/audit_credentials.php')) {
// Autogenerate an audit log encryption key on first entry.
file_put_contents('conf/audit_credentials.php', '<?php return "'.generate_token(64).'" ?>');
event_log_write('0', 'Audit', 'No encryption key found at conf/audit_credentials.php, audit log encryption key autogenerated.');
}
if (isset($post_data['REVERT_FROM_AUDIT'])) {
event_log_write('0', 'Update', "Pushed audit log entry back to database: " . $post_data['REVERT_FROM_AUDIT']);
}
$data['user']['username'] = $_SESSION['user']['username'];
$data['user']['ip_address'] = $_SERVER['REMOTE_ADDR'];
$data['user']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$data['user']['referer'] = $_SERVER['HTTP_REFERER'];
$data['user']['request_uri'] = $_SERVER['REQUEST_URI'];
$data['user']['request_method'] = $_SERVER['REQUEST_METHOD'];
$data['request_contents'] = $post_data;
$data = json_encode($data, JSON_PRETTY_PRINT);
$audit_file_sha256 = hash('sha256', $data);
$data = encrypt($data, include 'conf/audit_credentials.php');
$audit_file_epoch = time();
if (!file_exists('logs/audit/' . substr($audit_file_epoch, 0, 6))) {
mkdir('logs/audit/' . substr($audit_file_epoch, 0, 6));
}
$auditfile_identifier = $audit_file_epoch . "_". strtoupper(generate_token(4)) . ".log";
if (file_put_contents('logs/audit/' . substr($audit_file_epoch, 0, 6) . '/' . $auditfile_identifier, $data)) {
event_log_write('0', 'Audit', 'Logged request '. $auditfile_identifier .', sha256: '. $audit_file_sha256);
return $auditfile_identifier;
}
else {
return "Audit file failure.";
}
}
set_error_handler('kirjuri_error_handler'); // Give errors to the custom error handler.
/* Some things to run on every page load. */
if (file_exists('conf/mysql_credentials.php')) {
// Read credentials array from a file
$mysql_config = include 'conf/mysql_credentials.php';
} else {
session_destroy();
header('Location: install.php'); // If file not found, assume install.php needs to be run.
die;
}
if (file_exists('conf/settings.local')) {
$settings_file = 'conf/settings.local';
} elseif (file_exists('conf/settings.conf')) {
$settings_file = 'conf/settings.conf'; // Fall back to default settings.
} else {
echo "Missing settings file at conf/settings.conf. Can not continue.";
die;
}
$prefs = parse_ini_file($settings_file, true); // Parse settings file
$prefs['settings']['self'] = $_SERVER['PHP_SELF'];
$prefs['settings']['release'] = file_get_contents('conf/RELEASE');
if (file_exists('conf/' . basename($prefs['settings']['lang'], '.conf') . '.JSON')) {
$_SESSION['lang'] = json_decode(file_get_contents('conf/' . basename($prefs['settings']['lang'], '.conf') . '.JSON'), true); // Parse language file
} else {
$_SESSION['lang'] = parse_ini_file('conf/' . basename($prefs['settings']['lang'], '.conf') . '.conf', true); // Parse language file
}
if (isset($prefs['settings']['timezone'])) {
date_default_timezone_set($prefs['settings']['timezone']);
}
try {
// Create the attachments table.
$kirjuri_database = connect_database('kirjuri-database');
$query = $kirjuri_database->prepare('CREATE TABLE IF NOT EXISTS attachments (id INT(10) AUTO_INCREMENT PRIMARY KEY,
request_id INT(10), name VARCHAR(256), description TEXT, type VARCHAR(256), size INT NOT NULL, content MEDIUMBLOB NOT NULL,
uploader VARCHAR(256), date_uploaded DATETIME, hash VARCHAR(256), attr_1 TEXT, attr_2 TEXT, attr_3 TEXT) ');
$query->execute();
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
try {
// Read users from database to settings.
$query = $kirjuri_database->prepare('SELECT * from users ORDER BY access, username;');
$query->execute();
$users = $query->fetchAll(PDO::FETCH_ASSOC);
$_SESSION['all_users'] = $users;
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
try {
// Read tools from database to settings.
$query = $kirjuri_database->prepare('SELECT * FROM tools ORDER BY product_name;');
$query->execute();
$tools = $query->fetchAll(PDO::FETCH_ASSOC);
$_SESSION['all_tools'] = $tools;
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
if ($_SESSION['user']) { // Get unread message count
try {
$query = $kirjuri_database->prepare('SELECT (SELECT COUNT(id) FROM messages WHERE msgto = :username AND received = "0") AS new');
$query->execute(array(':username' => $_SESSION['user']['username']));
$_SESSION['unread'] = $query->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
session_destroy();
echo 'Database error: '.$e->getMessage().'. Run <a href="install.php">install</a> to create or upgrade tables and check your credentials.';
die;
}
}
if ( (microtime(true) - $mysql_timer_start) > "2.0") {
trigger_error("Your MySQL connection is slow. This might be a timeout issue when resolving the localhost hostname to an IP address. Try setting the MySQL server to your server IP from conf/mysql_credentials.php.");
}
/* Really extensive access logging.
if (isset($_SERVER['HTTP_REFERER'])) {
$url = "http".(!empty($_SERVER['HTTPS'])?"s":"")."://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($_SERVER['HTTP_REFERER'] !== $url) {
event_log_write('0', "Access", $_SERVER['HTTP_REFERER'] . ' -> ' . $url);
}
}
*/