|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Undocumented function |
|
6
|
|
|
* |
|
7
|
|
|
* @param string $message Message |
|
8
|
|
|
* @param string $ascii_key Key |
|
9
|
|
|
* @param string $type Type |
|
10
|
|
|
* |
|
11
|
|
|
* @return array |
|
12
|
|
|
*/ |
|
13
|
|
|
function defuseCryption($message, $ascii_key, $type) |
|
14
|
|
|
{ |
|
15
|
|
|
// load PhpEncryption library |
|
16
|
|
|
$path = '../includes/libraries/Encryption/Encryption/'; |
|
17
|
|
|
|
|
18
|
|
|
include_once $path.'Crypto.php'; |
|
19
|
|
|
include_once $path.'Encoding.php'; |
|
20
|
|
|
include_once $path.'DerivedKeys.php'; |
|
21
|
|
|
include_once $path.'Key.php'; |
|
22
|
|
|
include_once $path.'KeyOrPassword.php'; |
|
23
|
|
|
include_once $path.'File.php'; |
|
24
|
|
|
include_once $path.'RuntimeTests.php'; |
|
25
|
|
|
include_once $path.'KeyProtectedByPassword.php'; |
|
26
|
|
|
include_once $path.'Core.php'; |
|
27
|
|
|
|
|
28
|
|
|
// init |
|
29
|
|
|
$err = ''; |
|
30
|
|
|
if (empty($ascii_key) === true) { |
|
31
|
|
|
$ascii_key = file_get_contents(SECUREPATH.'/teampass-seckey.txt'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// convert KEY |
|
35
|
|
|
$key = \Defuse\Crypto\Key::loadFromAsciiSafeString($ascii_key); |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
if ($type === 'encrypt') { |
|
39
|
|
|
$text = \Defuse\Crypto\Crypto::encrypt($message, $key); |
|
40
|
|
|
} elseif ($type === 'decrypt') { |
|
41
|
|
|
$text = \Defuse\Crypto\Crypto::decrypt($message, $key); |
|
42
|
|
|
} |
|
43
|
|
|
} catch (Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException $ex) { |
|
44
|
|
|
$err = 'an attack! either the wrong key was loaded, or the ciphertext has changed since it was created either corrupted in the database or intentionally modified by someone trying to carry out an attack.'; |
|
45
|
|
|
} catch (Defuse\Crypto\Exception\BadFormatException $ex) { |
|
46
|
|
|
$err = $ex; |
|
47
|
|
|
} catch (Defuse\Crypto\Exception\EnvironmentIsBrokenException $ex) { |
|
48
|
|
|
$err = $ex; |
|
49
|
|
|
} catch (Defuse\Crypto\Exception\CryptoException $ex) { |
|
50
|
|
|
$err = $ex; |
|
51
|
|
|
} catch (Defuse\Crypto\Exception\IOException $ex) { |
|
52
|
|
|
$err = $ex; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return array( |
|
56
|
|
|
'string' => isset($text) ? $text : '', |
|
57
|
|
|
'error' => $err, |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Decrypt a defuse string if encrypted |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $value Encrypted string |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
function defuse_return_decrypted($value) |
|
70
|
|
|
{ |
|
71
|
|
|
if (substr($value, 0, 3) === "def") { |
|
72
|
|
|
$value = defuseCryption( |
|
73
|
|
|
$value, |
|
74
|
|
|
"", |
|
75
|
|
|
"decrypt" |
|
76
|
|
|
)['string']; |
|
77
|
|
|
} |
|
78
|
|
|
return $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Function permits to get the value from a line |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $val A string |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
function getSettingValue($val) |
|
89
|
|
|
{ |
|
90
|
|
|
$val = trim(strstr($val, "=")); |
|
91
|
|
|
return trim(str_replace('"', '', substr($val, 1, strpos($val, ";") - 1))); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Undocumented function |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $dbname DB |
|
98
|
|
|
* @param string $column Column |
|
99
|
|
|
* @param string $columnAttr Attribute |
|
100
|
|
|
* |
|
101
|
|
|
* @return boolean |
|
102
|
|
|
*/ |
|
103
|
|
|
function addColumnIfNotExist($dbname, $column, $columnAttr = "VARCHAR(255) NULL") |
|
104
|
|
|
{ |
|
105
|
|
|
global $db_link; |
|
106
|
|
|
$exists = false; |
|
107
|
|
|
$columns = mysqli_query($db_link, "show columns from $dbname"); |
|
108
|
|
|
while ($col = mysqli_fetch_assoc($columns)) { |
|
|
|
|
|
|
109
|
|
|
if ($col['Field'] == $column) { |
|
110
|
|
|
$exists = true; |
|
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
if (!$exists) { |
|
|
|
|
|
|
115
|
|
|
return mysqli_query($db_link, "ALTER TABLE `$dbname` ADD `$column` $columnAttr"); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Undocumented function |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $table Table |
|
125
|
|
|
* @param string $index Index |
|
126
|
|
|
* @param string $sql SQL |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
function addIndexIfNotExist($table, $index, $sql) |
|
131
|
|
|
{ |
|
132
|
|
|
global $db_link; |
|
133
|
|
|
|
|
134
|
|
|
$mysqli_result = mysqli_query($db_link, "SHOW INDEX FROM $table WHERE key_name LIKE \"$index\""); |
|
135
|
|
|
$res = mysqli_fetch_row($mysqli_result); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
// if index does not exist, then add it |
|
138
|
|
|
if (!$res) { |
|
139
|
|
|
$res = mysqli_query( |
|
140
|
|
|
$db_link, |
|
141
|
|
|
"ALTER TABLE `$table` ".$sql |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $res; |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Undocumented function |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $tablename Table |
|
152
|
|
|
* |
|
153
|
|
|
* @return boolean |
|
154
|
|
|
*/ |
|
155
|
|
|
function tableExists($tablename) |
|
156
|
|
|
{ |
|
157
|
|
|
global $db_link, $database; |
|
158
|
|
|
|
|
159
|
|
|
$res = mysqli_query( |
|
160
|
|
|
$db_link, |
|
161
|
|
|
"SELECT COUNT(*) as count |
|
162
|
|
|
FROM information_schema.tables |
|
163
|
|
|
WHERE table_schema = '".$database."' |
|
164
|
|
|
AND table_name = '$tablename'" |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
if ($res > 0) { |
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return false; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Undocumented function |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $txt My text |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
function cleanFields($txt) |
|
182
|
|
|
{ |
|
183
|
|
|
$tmp = str_replace(",", ";", trim($txt)); |
|
184
|
|
|
if (empty($tmp)) { |
|
185
|
|
|
return $tmp; |
|
186
|
|
|
} |
|
187
|
|
|
if ($tmp === ";") { |
|
188
|
|
|
return ""; |
|
189
|
|
|
} |
|
190
|
|
|
if (strpos($tmp, ';') === 0) { |
|
191
|
|
|
$tmp = substr($tmp, 1); |
|
192
|
|
|
} |
|
193
|
|
|
if (substr($tmp, -1) !== ";") { |
|
194
|
|
|
$tmp = $tmp.";"; |
|
195
|
|
|
} |
|
196
|
|
|
return $tmp; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Undocumented function |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
function generateRandomKey() |
|
205
|
|
|
{ |
|
206
|
|
|
// load passwordLib library |
|
207
|
|
|
$path = '../includes/libraries/PasswordGenerator/Generator/'; |
|
208
|
|
|
include_once $path.'ComputerPasswordGenerator.php'; |
|
209
|
|
|
|
|
210
|
|
|
$generator = new PasswordGenerator\Generator\ComputerPasswordGenerator(); |
|
211
|
|
|
|
|
212
|
|
|
$generator->setLength(40); |
|
213
|
|
|
$generator->setSymbols(false); |
|
214
|
|
|
$generator->setLowercase(true); |
|
215
|
|
|
$generator->setUppercase(true); |
|
216
|
|
|
$generator->setNumbers(true); |
|
217
|
|
|
|
|
218
|
|
|
$key = $generator->generatePasswords(); |
|
219
|
|
|
|
|
220
|
|
|
return $key[0]; |
|
221
|
|
|
} |
|
222
|
|
|
|