Passed
Push — main ( d07ee1...38048d )
by Rafael
50:23
created

Config::loadDolibarrConfig()   D

Complexity

Conditions 16
Paths 224

Size

Total Lines 102
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 71
nc 224
nop 0
dl 0
loc 102
rs 4.4333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 DoliCore\Base;
20
21
use Alxarafe\Base\Config as ConfigBase;
22
use DoliCore\Lib\Conf;
23
24
/**
25
 * Manage the Dolibarr configuration file.
26
 *
27
 * @info https://wiki.dolibarr.org/index.php/Configuration_file
28
 *
29
 * This class is only needed for compatibility with Dolibarr.
30
 *
31
 * @package DoliCore\Base
32
 */
33
abstract class Config extends ConfigBase
34
{
35
    /**
36
     * Dolibarr configuration filename.
37
     */
38
    private const CONFIG_FILENAME = '/conf/conf.php';
39
40
    /**
41
     * Defines the Dolibarr configuration file structure.
42
     */
43
    private const CONFIG_STRUCTURE = [
44
        'main' => [
45
            'dolibarr_main_document_root' => 'path',
46
            'dolibarr_main_url_root' => 'url',
47
            'dolibarr_main_data_root' => 'data',
48
        ],
49
        'db' => [
50
            'dolibarr_main_db_type' => 'type',
51
            'dolibarr_main_db_host' => 'host',
52
            'dolibarr_main_db_user' => 'user',
53
            'dolibarr_main_db_pass' => 'pass',
54
            'dolibarr_main_db_name' => 'name',
55
            'dolibarr_main_db_port' => 'port',
56
            'dolibarr_main_db_prefix' => 'prefix',
57
            'dolibarr_main_db_character_set' => 'charset',
58
            'dolibarr_main_db_collation' => 'collation',
59
            'dolibarr_main_db_encryption' => 'encryption',
60
            'dolibarr_main_db_cryptkey' => 'encrypt_type',
61
        ],
62
        'security' => [
63
            'dolibarr_main_authentication' => 'authentication_method',
64
            'dolibarr_main_instance_unique_id' => 'unique_id',
65
            'dolibarr_main_force_https' => 'https',
66
            'dolibarr_main_prod' => 'demo',
67
            'dolibarr_main_restrict_os_commands' => 'restrict_os_commands',
68
            'dolibarr_nocsrfcheck' => 'nocsrfcheck',
69
            'dolibarr_mailing_limit_sendbyweb' => 'mailing_limit_sendbyweb',
70
            'dolibarr_mailing_limit_sendbycli' => 'mailing_limit_sendbycli',
71
        ]
72
    ];
73
    private const DEFAULT_DB_PREFIX = 'alx_';
74
    private const DEFAULT_THEME = 'alixar';
75
    private const DEFAULT_DB_TYPE = 'mysqli';
76
    private const DEFAULT_CHARSET = 'utf8';
77
    private const DEFAULT_COLLATION = 'utf8_general_ci';
78
    private const DEFAULT_AUTHENTICATION_MODE = 'Dolibarr';
79
    /**
80
     * Contains Dolibarr configuration file information
81
     *
82
     * @var Conf|null
83
     */
84
    private static ?Conf $config = null;
85
86
    /**
87
     * Returns a Conf class with the Dolibarr configuration.
88
     * If the configuration file does not exist, is not accessible, or is not correct, returns null.
89
     *
90
     * @return Conf|null
91
     *
92
     * @deprecated Use
93
     */
94
    public static function getConf()
95
    {
96
        if (isset(self::$config)) {
97
            return self::$config;
98
        }
99
100
        self::$config = self::loadDolibarrConfig();
101
        return self::$config;
102
    }
103
104
    /**
105
     * Returns a Conf class with the Dolibarr configuration.
106
     * If the configuration file does not exist, is not accessible, or is not correct, returns null.
107
     *
108
     * @return Conf|null
109
     */
110
    public static function loadDolibarrConfig(): ?Conf
111
    {
112
        $filename = self::getDolibarrConfigFilename();
113
        if (file_exists($filename) && is_readable($filename)) {
114
            include $filename;
115
        }
116
117
        /*
118
         * Create $conf object
119
         */
120
        $conf = new Conf();
121
122
        // Set properties specific to database
123
        $conf->db->host = $dolibarr_main_db_host ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_host seems to never exist and therefore isset should always be false.
Loading history...
124
        $conf->db->port = $dolibarr_main_db_port ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_port seems to never exist and therefore isset should always be false.
Loading history...
125
        $conf->db->name = $dolibarr_main_db_name ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_name seems to never exist and therefore isset should always be false.
Loading history...
126
        $conf->db->user = $dolibarr_main_db_user ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_user seems to never exist and therefore isset should always be false.
Loading history...
127
        $conf->db->pass = $dolibarr_main_db_pass ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_pass seems to never exist and therefore isset should always be false.
Loading history...
128
        $conf->db->type = $dolibarr_main_db_type ?? self::DEFAULT_DB_TYPE;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_type seems to never exist and therefore isset should always be false.
Loading history...
129
        $conf->db->prefix = $dolibarr_main_db_prefix ?? self::DEFAULT_DB_PREFIX;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_prefix seems to never exist and therefore isset should always be false.
Loading history...
130
        $conf->db->charset = $dolibarr_main_db_character_set ?? self::DEFAULT_CHARSET;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_character_set seems to never exist and therefore isset should always be false.
Loading history...
131
        $conf->db->collation = $dolibarr_main_db_collation ?? self::DEFAULT_COLLATION;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_collation seems to never exist and therefore isset should always be false.
Loading history...
132
        $conf->db->encryption = $dolibarr_main_db_encryption ?? 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_encryption seems to never exist and therefore isset should always be false.
Loading history...
133
        $conf->db->cryptkey = $dolibarr_main_db_cryptkey ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_cryptkey seems to never exist and therefore isset should always be false.
Loading history...
134
        if (defined('TEST_DB_FORCE_TYPE')) {
135
            $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
136
        }
137
138
        // Set properties specific to conf file
139
        $conf->file->main_limit_users = $dolibarr_main_limit_users ?? null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_limit_users seems to never exist and therefore isset should always be false.
Loading history...
140
        $conf->file->mailing_limit_sendbyweb = $dolibarr_mailing_limit_sendbyweb ?? 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbyweb seems to never exist and therefore isset should always be false.
Loading history...
141
        $conf->file->mailing_limit_sendbycli = $dolibarr_mailing_limit_sendbycli ?? 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbycli seems to never exist and therefore isset should always be false.
Loading history...
142
        $conf->file->mailing_limit_sendbyday = $dolibarr_mailing_limit_sendbyday ?? 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbyday seems to never exist and therefore isset should always be false.
Loading history...
143
        $conf->file->main_authentication = $dolibarr_main_authentication ?? self::DEFAULT_AUTHENTICATION_MODE;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_authentication seems to never exist and therefore isset should always be false.
Loading history...
144
        $conf->file->main_force_https = isset($dolibarr_main_force_https) && $dolibarr_main_force_https ? true : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_force_https seems to never exist and therefore isset should always be false.
Loading history...
145
        $conf->file->strict_mode = $dolibarr_strict_mode ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_strict_mode seems to never exist and therefore isset should always be false.
Loading history...
146
        $conf->file->instance_unique_id = $dolibarr_main_instance_unique_id ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_instance_unique_id seems to never exist and therefore isset should always be false.
Loading history...
147
        $conf->file->main_path = $dolibarr_main_document_root ?? constant('BASE_PATH');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root seems to never exist and therefore isset should always be false.
Loading history...
148
        $conf->file->main_url = $dolibarr_main_url_root ?? constant('BASE_URL');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root seems to never exist and therefore isset should always be false.
Loading history...
149
        $conf->file->main_doc = $dolibarr_main_data_root ?? static::getDataDir($conf->file->main_path);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root seems to never exist and therefore isset should always be false.
Loading history...
150
        $conf->file->path = ['main' => $conf->file->main_path];
151
        $conf->file->url = ['main' => '/'];
152
        $conf->file->dol_document_root = $conf->file->main_doc;
153
        if (!empty($dolibarr_main_document_root_alt)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root_alt seems to never exist and therefore empty should always be true.
Loading history...
154
            $path = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
155
            $url = preg_split('/[;,]/', $dolibarr_main_url_root_alt ?? DIRECTORY_SEPARATOR);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root_alt seems to never exist and therefore isset should always be false.
Loading history...
156
157
            if (count($path) !== count($url)) {
158
                print '<b>Error:</b><br>$dolibarr_main_document_root_alt and $dolibarr_main_url_root_alt must contain the same number of elements.<br>';
159
                die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
160
            }
161
162
            $i = 0;
163
            foreach ($path as $value) {
164
                $conf->file->path['alt' . ($i++)] = (string)$value;
165
            }
166
            $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root_alt seems to be never defined.
Loading history...
167
            $i = 0;
168
            foreach ($url as $value) {
169
                if (preg_match('/^http(s)?:/', $value)) {
170
                    // Show error message
171
                    $correct_value = str_replace($conf->file->url, '', $value);
172
                    print '<b>Error:</b><br>' . "\n";
173
                    print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>' . "\n";
174
                    print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>' . "\n";
175
                    print 'Value found: ' . $value . '<br>' . "\n";
176
                    print 'Should be replaced by: ' . $correct_value . '<br>' . "\n";
177
                    print "Or something like following examples:<br>\n";
178
                    print "\"/extensions\"<br>\n";
179
                    print "\"/extensions1,/extensions2,...\"<br>\n";
180
                    print "\"/../extensions\"<br>\n";
181
                    print "\"/custom\"<br>\n";
182
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
183
                }
184
                $conf->file->url['alt' . ($i++)] = (string)$value;
185
            }
186
        }
187
188
        $conf->file->theme = $dolibarr_main_theme ?? self::DEFAULT_THEME;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_theme seems to never exist and therefore isset should always be false.
Loading history...
189
        $conf->file->dol_main_stream_to_disable = $dolibarr_main_stream_to_disable ?? null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_stream_to_disable seems to never exist and therefore isset should always be false.
Loading history...
190
        $conf->debug = intval($dolibarr_main_prod ?? 1) === 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_prod seems to never exist and therefore isset should always be false.
Loading history...
191
192
        // Detection browser (copy of code from main.inc.php)
193
        if (isset($_SERVER["HTTP_USER_AGENT"]) && is_object($conf) && empty($conf->browser->name)) {
194
            $tmp = getBrowserInfo($_SERVER["HTTP_USER_AGENT"]);
195
            $conf->browser->name = $tmp['browsername'];
196
            $conf->browser->os = $tmp['browseros'];
197
            $conf->browser->version = $tmp['browserversion'];
198
            $conf->browser->layout = $tmp['layout']; // 'classic', 'phone', 'tablet'
199
            //var_dump($conf->browser);
200
201
            if ($conf->browser->layout == 'phone') {
202
                $conf->dol_no_mouse_hover = 1;
203
            }
204
        }
205
206
        // Load the main includes of common libraries
207
        if (!defined('NOREQUIRETRAN')) {
208
            require_once BASE_PATH . '/core/class/translate.class.php';
209
        }
210
211
        return self::$config = $conf;
212
    }
213
214
    /**
215
     * Returns the Dolibarr conf.php complete path.
216
     *
217
     * @return string
218
     */
219
    private static function getDolibarrConfigFilename()
220
    {
221
        return BASE_PATH . self::CONFIG_FILENAME;
222
    }
223
224
    private static function getConfigFrom(string $filename): array
0 ignored issues
show
Unused Code introduced by
The method getConfigFrom() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
225
    {
226
        $result = [];
227
228
        if (!file_exists($filename)) {
229
            error_log($filename . ' does not exists!');
230
            return $result;
231
        }
232
233
        if (!is_readable($filename)) {
234
            error_log($filename . ' exists, but is not readable!');
235
            return $result;
236
        }
237
238
        require $filename;
239
240
        $data = [];
241
        foreach (self::CONFIG_STRUCTURE as $section => $values) {
242
            $data[$section] = [];
243
            foreach ($values as $key => $value) {
244
                if (!isset(${$key})) {
245
                    continue;
246
                }
247
                $data[$section][$value] = ${$key};
248
            }
249
        }
250
251
        return $data;
252
    }
253
254
    /**
255
     * Simply replace /htdocs with /documents in $pathDir
256
     *
257
     * @param $pathDir
258
     *
259
     * @return string
260
     */
261
    public static function getDataDir($pathDir)
262
    {
263
        return preg_replace("/\/htdocs$/", "", $pathDir) . '/documents';
264
    }
265
266
}
267