Passed
Push — teampass_3.0 ( 416a97...870788 )
by Nils
06:42
created

cleanFields()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 16
rs 9.6111
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)));
0 ignored issues
show
Bug Best Practice introduced by
The expression return trim(str_replace(...trpos($val, ';') - 1))) returns the type string which is incompatible with the documented return type void.
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
It seems like $columns can also be of type boolean; however, parameter $result of mysqli_fetch_assoc() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
    while ($col = mysqli_fetch_assoc(/** @scrutinizer ignore-type */ $columns)) {
Loading history...
109
        if ($col['Field'] == $column) {
110
            $exists = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $exists is dead and can be removed.
Loading history...
111
            return true;
112
        }
113
    }
114
    if (!$exists) {
0 ignored issues
show
introduced by
The condition $exists is always false.
Loading history...
115
        return mysqli_query($db_link, "ALTER TABLE `$dbname` ADD `$column`  $columnAttr");
0 ignored issues
show
Bug Best Practice introduced by
The expression return mysqli_query($db_...lumn.'` '.$columnAttr) also could return the type mysqli_result which is incompatible with the documented return type boolean.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $mysqli_result can also be of type boolean; however, parameter $result of mysqli_fetch_row() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
    $res = mysqli_fetch_row(/** @scrutinizer ignore-type */ $mysqli_result);
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res also could return the type boolean|mysqli_result which is incompatible with the documented return type array.
Loading history...
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