-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionsHtml.inc.php
More file actions
184 lines (163 loc) · 6.01 KB
/
functionsHtml.inc.php
File metadata and controls
184 lines (163 loc) · 6.01 KB
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
<?php
/**
* Subject: PHP functions related to html/Bootstrap4
* User: Frank
* Date: 04/08/2018
* Time: 17:02
*/
include_once("config/config.inc.php");
/***
* fctSessionCheck : Session timeout
* Function for each page refresh
*/
function fctSessionCheck()
{
if (isset($_SESSION['LAST_ACTIVITY']) && ($_SERVER['REQUEST_TIME'] - $_SESSION['LAST_ACTIVITY']) > CONST_TIMEOUT_DURATION) {
session_unset();
session_destroy();
session_start();
$_SESSION['Error']['Data'] = array("Type" => "warning", "Message" => "Session timeout");
}
$_SESSION['LAST_ACTIVITY'] = $_SERVER['REQUEST_TIME'];
}
/***
* getBadge: Return the badge of the user/admin
* Reference https://stackoverflow.com/a/49089293
* @return string
*/
function getBadge()
{
if ($_SESSION['user']['admin'] == 1) {
echo '<span class="badge badge-pill badge-danger align-top"><i class="fas fa-user-ninja"></i><strong> ADMIN</strong></span>';
} else {
echo '<span class="badge badge-pill badge-success align-top"><i class="fas fa-user" ></i><strong> USER</strong></span>';
}
}
/***
* getDebug : Return the $_SESSION details in an alert box if $debugMode = 1 (config)
* @return string
*/
function getDebug()
{
global $page;
if (CONST_DEBUGMODE == 1) {
$text = "Last activity: " . date('H:i:s', $_SESSION['LAST_ACTIVITY']) . "<br/>";
if (isset($_SESSION['user'])) {
$text .= "User: " . implode("<br/> ", $_SESSION['user']) . "<br/>";
}
if (isset($page)) {
$text .= "Page: " . implode("<br/> ", $page);
}
fctShowToast("info", "\$_SESSION details", $text, "false");
}
}
/***
* fctLoginMessage: Return error message in alert (index.php login form)
*/
function fctLoginMessage()
{
if (isset($_SESSION['Error']['Data']["Type"])) {
if ($_SESSION['Error']['Data']["Type"] == "danger") {
echo '
<div class="alert alert-danger"><i class="fas fa-times-circle"></i>
<small><strong>Error</strong> ' . $_SESSION['Error']['Data']["Message"] . '</small>
</div>
';
} elseif ($_SESSION['Error']['Data']["Type"] == "warning") {
echo '
<div class="alert alert-warning"><i class="fas fa-exclamation-circle"></i>
<small><strong>Warning</strong> ' . $_SESSION['Error']['Data']["Message"] . '</small>
</div>
';
}
}
}
/***
* Reference: http://php.net/manual/en/function.openssl-encrypt.php
* @param $plaintext
* @return string
*/
function fctUrlOpensslCipher($plaintext)
{
$key = $_SESSION['key'];
$ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary = true);
$temp = base64_encode($iv . $hmac . $ciphertext_raw);
return fctBase64UrlEncode($temp);
}
/***
* Reference: http://php.net/manual/en/function.openssl-encrypt.php
* @param $ciphertext
* @return
*/
function fctUrlOpensslDecipher($ciphertext)
{
//TODO when key does not exist or when no corresponding url is found
$key = $_SESSION['key'];
$c = fctBase64UrlDecode(base64_decode($ciphertext));
$ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len = 32);
$ciphertext_raw = substr($c, $ivlen + $sha2len);
$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary = true);
if (hash_equals($hmac, $calcmac))//PHP 5.6+ timing attack safe comparison
{
return $original_plaintext;
}else {return false;}
}
/***
* base64_url_en/de-code
* Reference : https://stackoverflow.com/a/5835352
* @param $input
* @return string
*/
function fctBase64UrlEncode($input)
{
return strtr(base64_encode($input), '+/=', '._-');
}
function fctBase64UrlDecode($input)
{
return base64_decode(strtr($input, '._-', '+/='));
}
/***
* Toaster
* Resource :
* https://kamranahmed.info/toast
* https://github.com/kamranahmedse/jquery-toast-plugin MIT LICENCE
* @param string $type
* warning, success, error, information
* @param string $title
* title
* @param string $content
* message
* @param $duration
* default=5000
*/
function fctShowToast($type, $title, $content, $duration = 5000)
{
echo '<script>
$.toast({
heading: "' . $title . '", // Optional heading to be shown on the toast
text: "' . $content . '", // Text that is to be shown in the toast
icon: "' . $type . '", // warning, success, error, info
// bgColor: "#444444", // Background color of the toast [NO ICON]
// textColor: "#eeeeee", // Text color of the toast [NO ICON]
showHideTransition: "fade", // fade, slide or plain
allowToastClose: true, // Boolean value true or false
hideAfter: ' . $duration . ', // false to make it sticky or number representing the miliseconds as time after which toast needs to be hidden
stack: 5, // false if there should be only one toast at a time or a number representing the maximum number of toasts to be shown at a time
position: "bottom-left", // bottom-center/right/left or top-center/right/left or mid-center or an object representing the left, right, top, bottom values
textAlign: "left", // Left, right or center
loader: true, // Whether to show loader or not. True by default
loaderBg: "#9EC600", // Background color of the toast loader
// beforeShow: function () {}, // will be triggered before the toast is shown
// afterShown: function () {}, // will be triggered after the toat has been shown
// beforeHide: function () {}, // will be triggered before the toast gets hidden
// afterHidden: function () {} // will be triggered after the toast has been hidden
});
</script>';
}
?>