-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.php
159 lines (147 loc) · 4.47 KB
/
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
<?php
function convertDates($date){
$daysoweek = array();
# Made dates work out
if ($date === '1,1,1,1,1,1,1') {
$date = "Every Day";
} else if ($date === '1,0,0,0,0,0,1') {
$date = "Weekends";
} else if ($date === '0,1,1,1,1,1,0') {
$date = "Weekdays";
} else {
$days = explode(',',$date);
if ($days[0] == 1) { array_push($daysoweek, 'Sun'); }
if ($days[1] == 1) { array_push($daysoweek, 'Mon'); }
if ($days[2] == 1) { array_push($daysoweek, 'Tue'); }
if ($days[3] == 1) { array_push($daysoweek, 'Wed'); }
if ($days[4] == 1) { array_push($daysoweek, 'Thu'); }
if ($days[5] == 1) { array_push($daysoweek, 'Fri'); }
if ($days[6] == 1) { array_push($daysoweek, 'Sat'); }
$date = implode(' ',$daysoweek);
}
return $date;
}
function getAdditionals() {
$gets = $_GET;
return $gets;
}
function printHelp() {
$helpInfo = "You're missing some information";
return $helpInfo;
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function generateHash($saltLength = 16, $passString, $customSalt = '') {
if ($customSalt == '') {
$posCharacters = 'adcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./';
$string = '';
$max = strlen($posCharacters) - 1;
for ($i = 0; $i < $saltLength; $i++) {
$string .= $posCharacters[mt_rand(0, $max)];
}
} else {
$string = $customSalt;
}
return crypt($passString, '$6$'.$string);
}
function msgBox($message, $type) {
$msgBox = "<div class='alert alert-".$type." alert-dismissible fade show' role='alert'>
$message
<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>";
$_SESSION['msgBox'] = $msgBox;
}
function checkUser($currentPassString, $userName, $usersDir) {
if (file_exists($usersDir.$userName.'.json')) {
$userCreds = file_get_contents($usersDir.$userName.'.json');
$decodeUser = json_decode($userCreds, true);
$splitPasswd = explode('$', $decodeUser['password']);
$verifyKey = generateHash(strlen($splitPasswd[2]), $currentPassString, $splitPasswd[2]);
# FOR DEBUGGING!
#echo "CurrentKey: ".$decodeUser['password']."\n";
#echo "NewKey: ".$verifyKey."\n";
if (hash_equals($decodeUser['password'], $verifyKey)) {
$output = "0";
if ($decodeUser['admin'] == 1) {
$output = "3";
}
} else {
$output = "1";
}
} else {
$output = "2";
}
return $output;
}
if (!function_exists('hash_equals')) {
function hash_equals($str1, $str2) {
if(strlen($str1) != strlen($str2)) {
return false;
} else {
$res = $str1 ^ $str2;
$ret = 0;
for($i = strlen($res) - 1; $i >= 0; $i--) {
$ret |= ord($res[$i]);
}
return !$ret;
}
}
}
# Removed pretty print from json_encode to keep php a lower version. rewrote it here.
function prettyPrint( $json ) {
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );
for( $i = 0; $i < $json_length; $i++ ) {
$char = $json[$i];
$new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':': $post = " "; break; case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( "\t", $new_line_level );
}
$result .= $char.$post;
}
return $result;
}
?>