Test Setup Failed
Push — dev ( 608138...99eb65 )
by Rafael
61:41 queued 16s
created

Model::encrypt()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 10
nc 64
nop 2
dl 0
loc 21
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Dolibarr\Core\Base;
20
21
use Illuminate\Database\Eloquent\Model as EloquentModel;
22
23
/**
24
 * This class implements an Eloquent Model
25
 *
26
 * This class is only needed for compatibility with Dolibarr.
27
 *
28
 * @package DoliCore\Base
29
 */
30
abstract class Model extends EloquentModel
31
{
32
    /**
33
     * Name of the field containing the record creation date, by default 'created_at'.
34
     */
35
    public const CREATED_AT = 'date_creat';
36
37
    /**
38
     * Name of the field that contains the date of the last update of the record, by default 'updated_at'.
39
     */
40
    public const UPDATED_AT = 'tms';
41
42
    /**
43
     * Primary key name, default is 'id'.
44
     *
45
     * @var string
46
     */
47
    protected $primaryKey = 'rowid';
48
49
    /**
50
     * Encrypt sensitive data in database
51
     * Warning: This function includes the escape and add the SQL simple quotes on strings.
52
     *
53
     * @param string $fieldorvalue Field name or value to encrypt
54
     * @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
55
     * @return  string                  XXX(field) or XXX('value') or field or 'value'
56
     */
57
    public static function encrypt($fieldorvalue, $withQuotes = 1)
58
    {
59
        global $conf;
60
61
        // Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
62
        $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
63
64
        //Encryption key
65
        $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey : '');
66
67
        $escapedstringwithquotes = ($withQuotes ? "'" : "") . $fieldorvalue . ($withQuotes ? "'" : "");
68
69
        if ($cryptType && !empty($cryptKey)) {
70
            if ($cryptType == 2) {
71
                $escapedstringwithquotes = "AES_ENCRYPT(" . $escapedstringwithquotes . ", '" . $cryptKey . "')";
72
            } elseif ($cryptType == 1) {
73
                $escapedstringwithquotes = "DES_ENCRYPT(" . $escapedstringwithquotes . ", '" . $cryptKey . "')";
74
            }
75
        }
76
77
        return $escapedstringwithquotes;
78
    }
79
80
    /**
81
     *  Decrypt sensitive data in database
82
     *
83
     * @param string $value Value to decrypt
84
     * @return string                  Decrypted value if used
85
     */
86
    public static function decrypt($value)
87
    {
88
        global $conf;
89
90
        // Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
91
        $cryptType = (!empty($conf->db->dolibarr_main_db_encryption) ? $conf->db->dolibarr_main_db_encryption : 0);
92
93
        //Encryption key
94
        $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey) ? $conf->db->dolibarr_main_db_cryptkey : '');
95
96
        $return = $value;
97
98
        if ($cryptType && !empty($cryptKey)) {
99
            if ($cryptType == 2) {
100
                $return = 'AES_DECRYPT(' . $value . ',\'' . $cryptKey . '\')';
101
            } elseif ($cryptType == 1) {
102
                $return = 'DES_DECRYPT(' . $value . ',\'' . $cryptKey . '\')';
103
            }
104
        }
105
106
        return $return;
107
    }
108
}
109