Passed
Push — main ( 7af4bb...131f20 )
by Rafael
58:13
created

Config::checkOldConfig()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 12
nop 0
dl 0
loc 21
rs 9.2222
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
use DoliCore\Lib\HookManager;
24
use DoliCore\Lib\TraceableDB;
25
use DoliCore\Tools\Debug;
26
use DoliDB;
27
use DoliModules\User\Model\User;
28
use MenuManager;
29
use stdClass;
30
31
require_once BASE_PATH . '/../Dolibarr/Core/Menu/standard/eldy_menu.php';
32
33
/**
34
 * Generate an object with the configuration of the Dolibarr conf.php file.
35
 *
36
 * @info https://wiki.dolibarr.org/index.php/Configuration_file
37
 *
38
 * This class is only needed for compatibility with Dolibarr.
39
 *
40
 * @package DoliCore\Base
41
 */
42
abstract class Config extends ConfigBase
43
{
44
    /**
45
     * Dolibarr configuration filename.
46
     */
47
    private const CONFIG_FILENAME = '/conf/conf.php';
48
49
    public const DEFAULT_DB_PREFIX = 'alx_';
50
51
    private const DEFAULT_THEME = 'alixar';
52
53
    private const DOLIBARR_CONFIG_STRUCTURE = [
54
        'main' => [
55
            'dolibarr_main_document_root' => 'path',
56
            'dolibarr_main_url_root' => 'url',
57
            'dolibarr_main_data_root' => 'data',
58
        ],
59
        'db' => [
60
            'dolibarr_main_db_type' => 'type',
61
            'dolibarr_main_db_host' => 'host',
62
            'dolibarr_main_db_user' => 'user',
63
            'dolibarr_main_db_pass' => 'pass',
64
            'dolibarr_main_db_name' => 'name',
65
            'dolibarr_main_db_port' => 'port',
66
            'dolibarr_main_db_prefix' => 'prefix',
67
            'dolibarr_main_db_character_set' => 'charset',
68
            'dolibarr_main_db_collation' => 'collation',
69
            'dolibarr_main_db_encryption' => 'encryption',
70
            'dolibarr_main_db_cryptkey' => 'encrypt_type',
71
        ],
72
        'security' => [
73
            'dolibarr_main_authentication' => 'authentication_method',
74
            'dolibarr_main_instance_unique_id' => 'unique_id',
75
            'dolibarr_main_force_https' => 'https',
76
            'dolibarr_main_prod' => 'demo',
77
            'dolibarr_main_restrict_os_commands' => 'restrict_os_commands',
78
            'dolibarr_nocsrfcheck' => 'nocsrfcheck',
79
            'dolibarr_mailing_limit_sendbyweb' => 'mailing_limit_sendbyweb',
80
            'dolibarr_mailing_limit_sendbycli' => 'mailing_limit_sendbycli',
81
        ]
82
    ];
83
    /**
84
     * Contains the information of the old $conf global var.
85
     *
86
     * Config::getConf() can be used at any point to retrieve the contents of the
87
     * $conf variable used globally by Dolibarr.
88
     *
89
     * The content of the variable is saved with the first call and this copy is
90
     * returned. If it is necessary to regenerate it, the parameter true can be
91
     * passed to it.
92
     *
93
     * @var null|Conf
94
     *
95
     * @deprecated Use $config instead
96
     */
97
    private static $dolibarrConfig = null;
98
    /**
99
     * Contains the information from the conf.php file in a normalized stdClass.
100
     *
101
     * The objective is to move what is really needed to this object and update the
102
     * configuration file to a data file outside of public space.
103
     *
104
     * @var null|stdClass
105
     */
106
    private static $config = null;
107
    /**
108
     * Contains a DoliDB connection.
109
     *
110
     * @var DoliDB, null
111
     */
112
    private static $db;
113
    /**
114
     * Contains a HookManager class.
115
     *
116
     * @var $hookManager
0 ignored issues
show
Documentation Bug introduced by
The doc comment $hookManager at position 0 could not be parsed: Unknown type name '$hookManager' at position 0 in $hookManager.
Loading history...
117
     */
118
    private static $hookManager;
119
    /**
120
     * Contains a Translate class
121
     *
122
     * @var Translate
123
     */
124
    private static $langs;
125
    /**
126
     * Contains a User class instance.
127
     *
128
     * @var User
129
     */
130
    private static $user;
131
    private static $menumanager;
132
133
    public static function checkOldConfig()
134
    {
135
        if (empty(self::$config)) {
136
            self::$config = parent::loadConfig();
137
        }
138
        if (empty(self::$config)) {
139
            self::$config = new stdClass();
140
            self::$config->main = self::getDefaultMainFileInfo();
141
        }
142
143
        $filename = self::getDolibarrConfigFilename();
144
        if (!file_exists($filename) || !is_readable($filename)) {
145
            return false;
146
        }
147
148
        $data = self::getConfigFrom($filename);
149
        if (empty($data)) {
150
            return false;
151
        }
152
153
        return static::setConfig($data);
154
    }
155
156
    /**
157
     * Returns a normalized config file.
158
     *
159
     * @param bool $reload
160
     * @return stdClass|null
161
     */
162
    public static function loadConfig(bool $reload = false): ?stdClass
163
    {
164
        $conf = parent::loadConfig($reload);
165
166
        if ($conf) {
167
            return $conf;
168
        }
169
170
        $conf = static::loadConf();
171
        if (empty($conf)) {
172
            return null;
173
        }
174
175
        $config = new stdClass();
176
177
        // 'main' section
178
        $config->main = new stdClass();
179
        $config->main->path = $conf->file->main_path ?? constant('BASE_PATH');
180
        $config->main->url = $conf->file->main_url ?? constant('BASE_URL');
181
        $config->main->data_path = $conf->file->main_doc ?? '';
182
        $config->main->alt_base_path = $conf->file->path;
183
        $config->main->alt_base_url = $conf->file->url;
184
        $config->main->theme = $conf->file->theme;
185
186
        // 'db' section
187
        $config->db = $conf->db;
188
        $config->db->charset = $conf->db->charset;
189
        $config->db->collation = $conf->db->collation;
190
        $config->db->encryption = $conf->db->encryption;
191
        $config->db->cryptkey = $conf->db->cryptkey;
192
193
        // 'security' section
194
        $config->security = new stdClass();
195
        $config->security->authentication_type = $conf->file->main_authentication;
196
        $config->security->force_https = $conf->file->main_force_https;
197
        $config->security->unique_id = $conf->file->instance_unique_id;
198
        $config->security->stream_to_disable = null;
199
        if (is_array($conf->file->dol_main_stream_to_disable)) {
200
            $config->security->stream_to_disable = $conf->file->dol_main_stream_to_disable;
201
        }
202
203
        $config->file = $conf->file;
204
205
        // Others
206
        $demo = $dolibarr_main_demo ?? false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_demo seems to never exist and therefore isset should always be false.
Loading history...
207
        if ($demo !== false) {
208
            $credentials = explode(',', $demo);
209
            if (count($credentials) === 2) {
210
                $config->demo->user = trim($credentials[0]);
211
                $config->demo->pass = trim($credentials[1]);
212
            }
213
        }
214
215
        $config->debug = $conf->debug;
216
217
        // 'Server' section
218
        $config->server = new stdClass();
219
        $config->server->detailed_info = !empty($_SERVER['MAIN_SHOW_TUNING_INFO']);
220
221
        static::$config = $config;
222
223
        return $config;
224
    }
225
226
    /**
227
     * Load the configuration file and return the content that the $conf variable
228
     * used globally by Dolibarr should have.
229
     *
230
     * @return Conf|null
231
     *
232
     * @deprecated Use loadConfig() instead!
233
     */
234
    private static function loadConf()
235
    {
236
        $filename = static::getDolibarrConfigFilename();
237
        $exists = file_exists($filename) && is_readable($filename);
238
        if ($exists) {
239
            include $filename;
240
        }
241
242
        /*
243
         * Create $conf object
244
         */
245
        $conf = new Conf();
246
247
        // Set properties specific to database
248
        $conf->db->host = empty($dolibarr_main_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 empty should always be true.
Loading history...
249
        $conf->db->port = empty($dolibarr_main_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 empty should always be true.
Loading history...
250
        $conf->db->name = empty($dolibarr_main_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 empty should always be true.
Loading history...
251
        $conf->db->user = empty($dolibarr_main_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 empty should always be true.
Loading history...
252
        $conf->db->pass = empty($dolibarr_main_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 empty should always be true.
Loading history...
253
        $conf->db->type = $dolibarr_main_db_type ?? 'mysqli';
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...
254
        $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...
255
        $conf->db->charset = $dolibarr_main_db_character_set ?? 'utf8';
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...
256
        $conf->db->collation = $dolibarr_main_db_collation ?? 'utf8_general_ci';
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...
257
        $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...
258
        $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...
259
        if (defined('TEST_DB_FORCE_TYPE')) {
260
            $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
261
        }
262
263
        // Set properties specific to conf file
264
        $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...
265
        $conf->file->mailing_limit_sendbyweb = empty($dolibarr_mailing_limit_sendbyweb) ? 0 : $dolibarr_mailing_limit_sendbyweb;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbyweb seems to never exist and therefore empty should always be true.
Loading history...
266
        $conf->file->mailing_limit_sendbycli = empty($dolibarr_mailing_limit_sendbycli) ? 0 : $dolibarr_mailing_limit_sendbycli;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbycli seems to never exist and therefore empty should always be true.
Loading history...
267
        $conf->file->mailing_limit_sendbyday = empty($dolibarr_mailing_limit_sendbyday) ? 0 : $dolibarr_mailing_limit_sendbyday;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbyday seems to never exist and therefore empty should always be true.
Loading history...
268
        $conf->file->main_authentication = empty($dolibarr_main_authentication) ? 'dolibarr' : $dolibarr_main_authentication; // Identification mode
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_authentication seems to never exist and therefore empty should always be true.
Loading history...
269
        $conf->file->main_force_https = empty($dolibarr_main_force_https) ? '' : $dolibarr_main_force_https; // Force https
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_force_https seems to never exist and therefore empty should always be true.
Loading history...
270
        $conf->file->strict_mode = empty($dolibarr_strict_mode) ? '' : $dolibarr_strict_mode; // Force php strict mode (for debug)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_strict_mode seems to never exist and therefore empty should always be true.
Loading history...
271
        $conf->file->instance_unique_id = empty($dolibarr_main_instance_unique_id) ? (empty($dolibarr_main_cookie_cryptkey) ? '' : $dolibarr_main_cookie_cryptkey) : $dolibarr_main_instance_unique_id; // Unique id of instance
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_instance_unique_id seems to never exist and therefore empty should always be true.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_cookie_cryptkey seems to never exist and therefore empty should always be true.
Loading history...
272
        $conf->file->main_path = empty($dolibarr_main_document_root ?? '') ? BASE_PATH : $dolibarr_main_document_root;  // Define htdocs path inside the config file
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root seems to never exist and therefore isset should always be false.
Loading history...
273
        $conf->file->main_url = empty($dolibarr_main_url_root ?? '') ? BASE_URL : $dolibarr_main_url_root;    // Define url inside the config file
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...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root seems to be never defined.
Loading history...
274
        $conf->file->main_doc = empty($dolibarr_main_data_root ?? '') ? static::getDataDir($conf->file->main_path) : $dolibarr_main_data_root;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root seems to never exist and therefore isset should always be false.
Loading history...
275
        $conf->file->path = ['main' => $conf->file->main_path];
276
        $conf->file->url = ['main' => '/'];
277
        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...
278
            $path = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
279
            $url = 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...
280
281
            if (count($path) !== count($url)) {
282
                print '<b>Error:</b><br>$dolibarr_main_document_root_alt and $dolibarr_main_url_root_alt must contain the same number of elements.<br>';
283
                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...
284
            }
285
286
            $i = 0;
287
            foreach ($path as $value) {
288
                $conf->file->path['alt' . ($i++)] = (string)$value;
289
            }
290
            $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
291
            $i = 0;
292
            foreach ($url as $value) {
293
                if (preg_match('/^http(s)?:/', $value)) {
294
                    // Show error message
295
                    $correct_value = str_replace($conf->file->url, '', $value);
296
                    print '<b>Error:</b><br>' . "\n";
297
                    print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>' . "\n";
298
                    print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>' . "\n";
299
                    print 'Value found: ' . $value . '<br>' . "\n";
300
                    print 'Should be replaced by: ' . $correct_value . '<br>' . "\n";
301
                    print "Or something like following examples:<br>\n";
302
                    print "\"/extensions\"<br>\n";
303
                    print "\"/extensions1,/extensions2,...\"<br>\n";
304
                    print "\"/../extensions\"<br>\n";
305
                    print "\"/custom\"<br>\n";
306
                    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...
307
                }
308
                $conf->file->url['alt' . ($i++)] = (string)$value;
309
            }
310
        }
311
312
        $conf->file->theme = $dolibarr_main_theme ?? 'eldy';
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...
313
        $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...
314
        $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...
315
316
        // Load the main includes of common libraries
317
        if (!defined('NOREQUIRETRAN')) {
318
            require_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php';
319
        }
320
321
        static::$dolibarrConfig = $conf;
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
322
        return $conf;
323
    }
324
325
    /**
326
     * Returns the Dolibarr conf.php complete path.
327
     *
328
     * @return string
329
     */
330
    public static function getDolibarrConfigFilename()
331
    {
332
        return BASE_PATH . self::CONFIG_FILENAME;
333
    }
334
335
    /**
336
     * Simply replace /htdocs with /documents in $pathDir
337
     *
338
     * @param $pathDir
339
     *
340
     * @return string
341
     */
342
    public static function getDataDir($pathDir)
343
    {
344
        return preg_replace("/\/htdocs$/", "", $pathDir) . '/documents';
345
    }
346
347
    /**
348
     * Those configuration parameters that we can obtain at run time,
349
     * or their default values, are obtained.
350
     *
351
     * @return stdClass
352
     */
353
    public static function getDefaultMainFileInfo(): stdClass
354
    {
355
        $result = parent::getDefaultMainFileInfo();
356
        $result->data = static::getDataDir($result->path);
357
        $result->language = 'auto';
358
        $result->theme = self::DEFAULT_THEME;
359
        return $result;
360
    }
361
362
    private static function getConfigFrom(string $filename): array
363
    {
364
        $result = [];
365
366
        if (!file_exists($filename)) {
367
            error_log($filename . ' does not exists!');
368
            return $result;
369
        }
370
371
        if (!is_readable($filename)) {
372
            error_log($filename . ' exists, but is not readable!');
373
            return $result;
374
        }
375
376
        require $filename;
377
378
        $data = [];
379
        foreach (self::DOLIBARR_CONFIG_STRUCTURE as $section => $values) {
380
            $data[$section] = [];
381
            foreach ($values as $key => $value) {
382
                if (!isset(${$key})) {
383
                    continue;
384
                }
385
                $data[$section][$value] = ${$key};
386
            }
387
        }
388
389
        return $data;
390
    }
391
392
    /**
393
     * Load the Dolibarr configuration file and enter the content for the Dolibarr global
394
     * variable $conf.
395
     *
396
     * The result is cached for future queries. If we want to reload the configuration file
397
     * we will have to pass the parameter true.
398
     *
399
     * @param $reload
400
     *
401
     * @return stdClass|null
402
     */
403
    public static function getConf($reload = false): ?stdClass
404
    {
405
        if ($reload || !isset(static::$dolibarrConfig)) {
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
406
            static::$dolibarrConfig = static::loadConf();
0 ignored issues
show
Deprecated Code introduced by
The function DoliCore\Base\Config::loadConf() has been deprecated: Use loadConfig() instead! ( Ignorable by Annotation )

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

406
            static::$dolibarrConfig = /** @scrutinizer ignore-deprecated */ static::loadConf();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
407
        }
408
409
        return static::$dolibarrConfig;
410
    }
411
412
    /**
413
     * Returns a stdClass with the information contained in the conf.php file.
414
     *
415
     * @param $reload
416
     *
417
     * @return stdClass|null
418
     */
419
    public static function getConfig($reload = false): ?stdClass
420
    {
421
        if ($reload || !isset(static::$config)) {
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $config to at least protected.
Loading history...
422
            static::$config = static::loadConfig();
423
        }
424
425
        return static::$config;
426
    }
427
428
    /**
429
     * Returns a DoliDB connection instance.
430
     *
431
     * @return DoliDB|null
432
     */
433
    public static function getDb(): ?DoliDB
434
    {
435
        return static::$db;
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
436
    }
437
438
    /**
439
     * Fills in the additional data of the $conf variable, taken once the database
440
     * is initialized
441
     *
442
     * @param $conf
443
     */
444
    public static function setConfigValues($conf)
445
    {
446
        // Here we read database (llx_const table) and define conf var $conf->global->XXX.
447
        // print "We work with data into entity instance number '".$conf->entity."'";
448
        $conf->setValues(static::$db);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
449
    }
450
451
    /**
452
     * Returns a TraceableDB connection instance.
453
     *
454
     * @return TraceableDB|null
455
     */
456
    public static function debugDb(): ?TraceableDB
457
    {
458
        if (isModEnabled('debugbar')) {
459
            static::$db = new TraceableDB(static::$db);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
460
        }
461
        return static::$db;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::db returns the type DoliDB which includes types incompatible with the type-hinted return DoliCore\Lib\TraceableDB|null.
Loading history...
462
    }
463
464
    /**
465
     * Returns a HookManager class instance.
466
     *
467
     * @return HookManager|null
468
     */
469
    public static function getHookManager(): ?HookManager
470
    {
471
        if (empty(static::$hookManager)) {
0 ignored issues
show
Bug introduced by
Since $hookManager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hookManager to at least protected.
Loading history...
472
            static::$hookManager = static::loadHookManager();
473
        }
474
        return static::$hookManager;
475
    }
476
477
    /**
478
     * Returns a HookManager class instance.
479
     *
480
     * @return mixed
481
     */
482
    private static function loadHookManager()
483
    {
484
        static::$hookManager = new HookManager(static::$db);
0 ignored issues
show
Bug introduced by
Since $hookManager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hookManager to at least protected.
Loading history...
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
485
        return static::$hookManager;
486
    }
487
488
    /**
489
     * Returns a Translate class instance.
490
     *
491
     * @return Translate|null
492
     */
493
    public static function getLangs(): ?Translate
494
    {
495
        if (empty(static::$langs)) {
0 ignored issues
show
Bug introduced by
Since $langs is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $langs to at least protected.
Loading history...
496
            static::$langs = static::loadLangs();
497
        }
498
        return static::$langs;
499
    }
500
501
    /**
502
     * Returns a Translate class instance.
503
     *
504
     * @return Translate
505
     */
506
    private static function loadLangs()
507
    {
508
        static::$langs = new Translate('', static::$dolibarrConfig);
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
Bug introduced by
Since $langs is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $langs to at least protected.
Loading history...
509
        return static::$langs;
510
    }
511
512
    /**
513
     * Returns a User class instance.
514
     *
515
     * @return User|null
516
     */
517
    public static function getUser(): ?User
518
    {
519
        if (empty(static::$user)) {
0 ignored issues
show
Bug introduced by
Since $user is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $user to at least protected.
Loading history...
520
            static::$user = static::loadUser();
521
        }
522
        return static::$user;
523
    }
524
525
    private static function loadUser()
526
    {
527
        static::$user = new User(static::$db);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
Bug introduced by
Since $user is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $user to at least protected.
Loading history...
528
        return static::$user;
529
    }
530
531
    public static function getMenuManager($conf)
532
    {
533
        if (empty(static::$menumanager)) {
0 ignored issues
show
Bug introduced by
Since $menumanager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $menumanager to at least protected.
Loading history...
534
            static::$menumanager = static::loadMenuManager();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to static::menumanager is correct as static::loadMenuManager() targeting DoliCore\Base\Config::loadMenuManager() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
535
        }
536
        return static::$menumanager;
537
    }
538
539
    private static function loadMenuManager()
540
    {
541
        $conf = static::$dolibarrConfig;
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
542
        $db = static::$db;
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
543
544
        $menumanager = null;
545
        if (!defined('NOREQUIREMENU')) {
546
            if (empty($user->socid)) {    // If internal user or not defined
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
547
                $conf->standard_menu = (!getDolGlobalString('MAIN_MENU_STANDARD_FORCED') ? (!getDolGlobalString('MAIN_MENU_STANDARD') ? 'eldy_menu.php' : $conf->global->MAIN_MENU_STANDARD) : $conf->global->MAIN_MENU_STANDARD_FORCED);
548
            } else {
549
                // If external user
550
                $conf->standard_menu = (!getDolGlobalString('MAIN_MENUFRONT_STANDARD_FORCED') ? (!getDolGlobalString('MAIN_MENUFRONT_STANDARD') ? 'eldy_menu.php' : $conf->global->MAIN_MENUFRONT_STANDARD) : $conf->global->MAIN_MENUFRONT_STANDARD_FORCED);
551
            }
552
553
            // Load the menu manager (only if not already done)
554
            $file_menu = $conf->standard_menu;
555
            if (GETPOST('menu', 'alpha')) {
556
                $file_menu = GETPOST('menu', 'alpha'); // example: menu=eldy_menu.php
557
            }
558
            if (!class_exists('MenuManager')) {
559
                $menufound = 0;
560
                $dirmenus = array_merge(["/core/menus/"], (array)$conf->modules_parts['menus']);
561
                foreach ($dirmenus as $dirmenu) {
562
                    $menufound = dol_include_once($dirmenu . "standard/" . $file_menu);
563
                    if (class_exists('MenuManager')) {
564
                        break;
565
                    }
566
                }
567
                if (!class_exists('MenuManager')) { // If failed to include, we try with standard eldy_menu.php
568
                    dol_syslog("You define a menu manager '" . $file_menu . "' that can not be loaded.", LOG_WARNING);
569
                    $file_menu = 'eldy_menu.php';
570
                    include_once DOL_DOCUMENT_ROOT . "/core/menus/standard/" . $file_menu;
571
                }
572
            }
573
            $menumanager = new MenuManager($db, empty($user->socid) ? 0 : 1);
574
            $menumanager->loadMenu();
575
        }
576
577
        static::$menumanager = $menumanager;
0 ignored issues
show
Bug introduced by
Since $menumanager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $menumanager to at least protected.
Loading history...
578
    }
579
580
    /**
581
     * Load all Dolibar global variables.
582
     *
583
     * @return false|void
584
     * @throws \DebugBar\DebugBarException
585
     */
586
    public static function load(): bool
587
    {
588
        global $conf;
589
        global $config;
590
        global $db;
591
        global $hookmanager;
592
        global $langs;
593
        global $user;
594
        global $menumanager;
595
596
        self::checkOldConfig();
597
598
        $config = parent::loadConfig();
599
        $conf = static::$dolibarrConfig = static::loadConf();
0 ignored issues
show
Deprecated Code introduced by
The function DoliCore\Base\Config::loadConf() has been deprecated: Use loadConfig() instead! ( Ignorable by Annotation )

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

599
        $conf = static::$dolibarrConfig = /** @scrutinizer ignore-deprecated */ static::loadConf();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
600
        if (!isset($config->db) || empty($conf->db->name ?? '')) {
601
            return false;
602
        }
603
604
        $config = static::$config = static::loadConfig();
605
        $db = static::$db = static::loadDb();
606
        $hookmanager = static::$hookManager = static::loadHookManager();
607
        $langs = static::$langs = static::loadLangs();
608
        $user = static::$user = static::loadUser();
609
        if ($user->id > 0) {
610
            $menumanager = static::$menumanager = static::loadMenuManager();
611
        }
612
        Debug::load();
613
614
        new Database($config->db);
615
616
        // TODO: Example of calling a SELECT from Eloquent and from Dolibarr
617
        // DB::select('SELECT * FROM alx_user'); // use Illuminate\Database\Capsule\Manager as DB;
618
        // $db->query('SELECT * FROM alx_user');
619
620
        return true;
621
    }
622
623
    /**
624
     * Returns a Dolibarr DB connection (DoliDB) instance.
625
     *
626
     * @return DoliDb
627
     * @throws \Exception
628
     */
629
    private static function loadDb()
630
    {
631
        $conf = static::$dolibarrConfig;
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
632
        static::$db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int)$conf->db->port);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
633
        static::$dolibarrConfig->setValues(static::$db);
0 ignored issues
show
Bug introduced by
The method setValues() does not exist on null. ( Ignorable by Annotation )

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

633
        static::$dolibarrConfig->/** @scrutinizer ignore-call */ 
634
                                 setValues(static::$db);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
634
635
        return static::$db;
636
    }
637
}
638