Issues (2497)

Dolibarr/Core/Base/Config.php (61 issues)

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 Conf;
22
use DoliCore\Lib\TraceableDB;
23
use DoliCore\Tools\Debug;
24
use DoliDB;
25
use DoliModules\User\Model\User;
26
use HookManager;
27
use MenuManager;
28
use stdClass;
29
use Translate;
0 ignored issues
show
This use statement conflicts with another class in this namespace, DoliCore\Base\Translate. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
30
31
require_once BASE_PATH . '/core/class/conf.class.php';
32
require_once BASE_PATH . '/core/class/hookmanager.class.php';
33
require_once BASE_PATH . '/core/class/translate.class.php';
34
35
/**
36
 * Generate an object with the configuration of the Dolibarr conf.php file.
37
 *
38
 * @info https://wiki.dolibarr.org/index.php/Configuration_file
39
 *
40
 * This class is only needed for compatibility with Dolibarr.
41
 *
42
 * @package DoliCore\Base
43
 */
44
abstract class Config
45
{
46
    const DEFAULT_DB_PREFIX = 'alx_';
47
48
    /**
49
     * Contains the information of the old $conf global var.
50
     *
51
     * Config::getConf() can be used at any point to retrieve the contents of the
52
     * $conf variable used globally by Dolibarr.
53
     *
54
     * The content of the variable is saved with the first call and this copy is
55
     * returned. If it is necessary to regenerate it, the parameter true can be
56
     * passed to it.
57
     *
58
     * @var null|Conf
59
     *
60
     * @deprecated Use $config instead
61
     */
62
    private static $dolibarrConfig = null;
63
64
    /**
65
     * Contains the information from the conf.php file in a normalized stdClass.
66
     *
67
     * The objective is to move what is really needed to this object and update the
68
     * configuration file to a data file outside of public space.
69
     *
70
     * @var null|stdClass
71
     */
72
    private static $config = null;
73
74
    /**
75
     * Contains a DoliDB connection.
76
     *
77
     * @var DoliDB, null
78
     */
79
    private static $db;
80
81
    /**
82
     * Contains a HookManager class.
83
     *
84
     * @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...
85
     */
86
    private static $hookManager;
87
88
    /**
89
     * Contains a Translate class
90
     *
91
     * @var Translate
92
     */
93
    private static $langs;
94
95
    /**
96
     * Contains a User class instance.
97
     *
98
     * @var User
99
     */
100
    private static $user;
101
102
    private static $menumanager;
103
104
    /**
105
     * Load the Dolibarr configuration file and enter the content for the Dolibarr global
106
     * variable $conf.
107
     *
108
     * The result is cached for future queries. If we want to reload the configuration file
109
     * we will have to pass the parameter true.
110
     *
111
     * @param $reload
112
     *
113
     * @return stdClass|null
114
     */
115
    public static function getConf($reload = false): ?stdClass
116
    {
117
        if ($reload || !isset(static::$dolibarrConfig)) {
0 ignored issues
show
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...
118
            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

118
            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...
119
        }
120
121
        return static::$dolibarrConfig;
122
    }
123
124
    /**
125
     * Load the configuration file and return the content that the $conf variable
126
     * used globally by Dolibarr should have.
127
     *
128
     * @return Conf|null
129
     *
130
     * @deprecated Use loadConfig() instead!
131
     */
132
    private static function loadConf()
133
    {
134
        $filename = static::getDolibarrConfigFilename();
135
        $exists = file_exists($filename) && is_readable($filename);
136
        if ($exists) {
137
            include $filename;
138
        }
139
140
        /*
141
         * Create $conf object
142
         */
143
        $conf = new Conf();
144
145
        // Set properties specific to database
146
        $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...
147
        $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...
148
        $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...
149
        $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...
150
        $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...
151
        $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...
152
        $conf->db->prefix = $dolibarr_main_db_prefix ?? 'alx_';
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...
153
        $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...
154
        $conf->db->collation = $dolibarr_main_db_collation ?? 'utf8-unicode-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...
155
        $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...
156
        $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...
157
        if (defined('TEST_DB_FORCE_TYPE')) {
158
            $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
159
        }
160
161
        // Set properties specific to conf file
162
        $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...
163
        $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...
164
        $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...
165
        $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...
166
        $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...
167
        $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...
168
        $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...
169
        $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_cookie_cryptkey seems to never exist and therefore empty should always be true.
Loading history...
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...
170
        $conf->file->main_path = $dolibarr_main_document_root ?? BASE_PATH;  // Define htdocs path inside the config file
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...
171
        $conf->file->main_url = $dolibarr_main_url_root ?? BASE_URL;    // 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...
172
        $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...
173
        $conf->file->path = ['main' => $conf->file->main_path];
174
        $conf->file->url = ['main' => '/'];
175
        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...
176
            $path = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
177
            $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...
178
179
            if (count($path) !== count($url)) {
180
                print '<b>Error:</b><br>$dolibarr_main_document_root_alt and $dolibarr_main_url_root_alt must contain the same number of elements.<br>';
181
                die();
0 ignored issues
show
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...
182
            }
183
184
            $i = 0;
185
            foreach ($path as $value) {
186
                $conf->file->path['alt' . ($i++)] = (string) $value;
187
            }
188
            $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
189
            $i = 0;
190
            foreach ($url as $value) {
191
                if (preg_match('/^http(s)?:/', $value)) {
192
                    // Show error message
193
                    $correct_value = str_replace($conf->file->url, '', $value);
194
                    print '<b>Error:</b><br>' . "\n";
195
                    print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>' . "\n";
196
                    print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>' . "\n";
197
                    print 'Value found: ' . $value . '<br>' . "\n";
198
                    print 'Should be replaced by: ' . $correct_value . '<br>' . "\n";
199
                    print "Or something like following examples:<br>\n";
200
                    print "\"/extensions\"<br>\n";
201
                    print "\"/extensions1,/extensions2,...\"<br>\n";
202
                    print "\"/../extensions\"<br>\n";
203
                    print "\"/custom\"<br>\n";
204
                    exit;
0 ignored issues
show
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...
205
                }
206
                $conf->file->url['alt' . ($i++)] = (string) $value;
207
            }
208
        }
209
210
        $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...
211
        $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...
212
213
        // Load the main includes of common libraries
214
        if (!defined('NOREQUIRETRAN')) {
215
            require_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php';
216
        }
217
218
        static::$dolibarrConfig = $conf;
0 ignored issues
show
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...
219
        return $conf;
220
    }
221
222
    /**
223
     * Returns the Dolibarr conf.php complete path.
224
     *
225
     * @return string
226
     */
227
    public static function getDolibarrConfigFilename()
228
    {
229
        return BASE_PATH . '/conf/conf.php';
230
    }
231
232
    /**
233
     * Simply replace /htdocs with /documents in $pathDir
234
     *
235
     * @param $pathDir
236
     *
237
     * @return string
238
     */
239
    public static function getDataDir($pathDir)
240
    {
241
        return preg_replace("/\/htdocs$/", "", $pathDir) . '/documents';
242
    }
243
244
    /**
245
     * Returns a stdClass with the information contained in the conf.php file.
246
     *
247
     * @param $reload
248
     *
249
     * @return stdClass|null
250
     */
251
    public static function getConfig($reload = false): ?stdClass
252
    {
253
        if ($reload || !isset(static::$config)) {
0 ignored issues
show
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...
254
            static::$config = static::loadConfig();
255
        }
256
257
        return static::$config;
258
    }
259
260
    /**
261
     * Returns a normalized config file.
262
     *
263
     * @return stdClass|null
264
     */
265
    private static function loadConfig()
266
    {
267
        $conf = 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

267
        $conf = /** @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...
268
        if (empty($conf)) {
269
            return null;
270
        }
271
272
        $config = new stdClass();
273
274
        // 'main' section
275
        $config->main = new stdClass();
276
        $config->main->base_path = $conf->file->main_path ?? constant('BASE_PATH');
277
        $config->main->base_url = $conf->file->main_url ?? constant('BASE_URL');
278
        $config->main->data_path = $conf->file->main_doc ?? '';
279
        $config->main->alt_base_path = $conf->file->path;
280
        $config->main->alt_base_url = $conf->file->url;
281
282
        // 'db' section
283
        $config->db = $conf->db;
284
        $config->db->charset = $conf->db->charset;
285
        $config->db->collation = $conf->db->collation;
286
        $config->db->encryption = $conf->db->encryption;
287
        $config->db->cryptkey = $conf->db->cryptkey;
288
289
        // 'security' section
290
        $config->security = new stdClass();
291
        $config->security->authentication_type = $conf->file->main_authentication;
292
        $config->security->force_https = $conf->file->main_force_https;
293
        $config->security->unique_id = $conf->file->instance_unique_id;
294
        $config->security->stream_to_disable = null;
295
        if (is_array($conf->file->dol_main_stream_to_disable)) {
296
            $config->security->stream_to_disable = $conf->file->dol_main_stream_to_disable;
297
        }
298
299
        $config->file = $conf->file;
300
301
        // Others
302
        $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...
303
        if ($demo !== false) {
304
            $credentials = explode(',', $demo);
305
            if (count($credentials) === 2) {
306
                $config->demo->user = trim($credentials[0]);
307
                $config->demo->pass = trim($credentials[1]);
308
            }
309
        }
310
311
        $config->debug = $conf->debug;
312
313
        // 'Server' section
314
        $config->server = new stdClass();
315
        $config->server->detailed_info = !empty($_SERVER['MAIN_SHOW_TUNING_INFO']);
316
317
        static::$config = $config;
318
319
        return $config;
320
    }
321
322
    /**
323
     * Returns a DoliDB connection instance.
324
     *
325
     * @return DoliDB|null
326
     */
327
    public static function getDb(): ?DoliDB
328
    {
329
        return static::$db;
0 ignored issues
show
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...
330
    }
331
332
    /**
333
     * Fills in the additional data of the $conf variable, taken once the database
334
     * is initialized
335
     *
336
     * @param $conf
337
     */
338
    public static function setConfigValues($conf)
339
    {
340
        // Here we read database (llx_const table) and define conf var $conf->global->XXX.
341
        // print "We work with data into entity instance number '".$conf->entity."'";
342
        $conf->setValues(static::$db);
0 ignored issues
show
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...
343
    }
344
345
    /**
346
     * Returns a TraceableDB connection instance.
347
     *
348
     * @return TraceableDB|null
349
     */
350
    public static function debugDb(): ?TraceableDB
351
    {
352
        if (isModEnabled('debugbar')) {
353
            static::$db = new TraceableDB(static::$db);
0 ignored issues
show
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...
354
        }
355
        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...
356
    }
357
358
    /**
359
     * Returns a HookManager class instance.
360
     *
361
     * @return HookManager|null
362
     */
363
    public static function getHookManager(): ?HookManager
364
    {
365
        if (empty(static::$hookManager)) {
0 ignored issues
show
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...
366
            static::$hookManager = static::loadHookManager();
367
        }
368
        return static::$hookManager;
369
    }
370
371
    /**
372
     * Returns a HookManager class instance.
373
     *
374
     * @return mixed
375
     */
376
    private static function loadHookManager()
377
    {
378
        static::$hookManager = new HookManager(static::$db);
0 ignored issues
show
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...
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...
379
        return static::$hookManager;
380
    }
381
382
    /**
383
     * Returns a Translate class instance.
384
     *
385
     * @return Translate|null
386
     */
387
    public static function getLangs(): ?Translate
388
    {
389
        if (empty(static::$langs)) {
0 ignored issues
show
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...
390
            static::$langs = static::loadLangs();
391
        }
392
        return static::$langs;
393
    }
394
395
    /**
396
     * Returns a Translate class instance.
397
     *
398
     * @return Translate
399
     */
400
    private static function loadLangs()
401
    {
402
        static::$langs = new Translate('', static::$dolibarrConfig);
0 ignored issues
show
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...
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...
403
        return static::$langs;
404
    }
405
406
    /**
407
     * Returns a User class instance.
408
     *
409
     * @return User|null
410
     */
411
    public static function getUser(): ?User
412
    {
413
        if (empty(static::$user)) {
0 ignored issues
show
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...
414
            static::$user = static::loadUser();
415
        }
416
        return static::$user;
417
    }
418
419
    public static function getMenuManager($conf)
420
    {
421
        if (!empty(static::$menumanager)) {
0 ignored issues
show
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...
422
            static::$menumanager = static::loadMenuManager();
0 ignored issues
show
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...
423
        }
424
        return static::$menumanager;
425
    }
426
427
    private static function loadMenuManager()
428
    {
429
        $conf = static::$dolibarrConfig;
0 ignored issues
show
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...
430
        $db = static::$db;
0 ignored issues
show
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...
431
432
        $menumanager = null;
433
        if (!defined('NOREQUIREMENU')) {
434
            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...
435
                $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);
436
            } else {
437
                // If external user
438
                $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);
439
            }
440
441
            // Load the menu manager (only if not already done)
442
            $file_menu = $conf->standard_menu;
443
            if (GETPOST('menu', 'alpha')) {
444
                $file_menu = GETPOST('menu', 'alpha'); // example: menu=eldy_menu.php
445
            }
446
            if (!class_exists('MenuManager')) {
447
                $menufound = 0;
448
                $dirmenus = array_merge(["/core/menus/"], (array) $conf->modules_parts['menus']);
449
                foreach ($dirmenus as $dirmenu) {
450
                    $menufound = dol_include_once($dirmenu . "standard/" . $file_menu);
451
                    if (class_exists('MenuManager')) {
452
                        break;
453
                    }
454
                }
455
                if (!class_exists('MenuManager')) { // If failed to include, we try with standard eldy_menu.php
456
                    dol_syslog("You define a menu manager '" . $file_menu . "' that can not be loaded.", LOG_WARNING);
457
                    $file_menu = 'eldy_menu.php';
458
                    include_once DOL_DOCUMENT_ROOT . "/core/menus/standard/" . $file_menu;
459
                }
460
            }
461
            $menumanager = new MenuManager($db, empty($user->socid) ? 0 : 1);
462
            $menumanager->loadMenu();
463
        }
464
465
        static::$menumanager = $menumanager;
0 ignored issues
show
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...
466
    }
467
468
    /**
469
     * Load all Dolibar global variables.
470
     *
471
     * @return false|void
472
     * @throws \DebugBar\DebugBarException
473
     */
474
    public static function load()
475
    {
476
        global $conf;
477
        global $config;
478
        global $db;
479
        global $hookmanager;
480
        global $langs;
481
        global $user;
482
        global $menumanager;
483
484
        $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

484
        $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...
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...
485
        if (empty($conf->db->name ?? '')) {
486
            return false;
487
        }
488
489
        $config = static::$config = static::loadConfig();
490
        $db = static::$db = static::loadDb();
491
        $hookmanager = static::$hookManager = static::loadHookManager();
492
        $langs = static::$langs = static::loadLangs();
493
        $user = static::$user = static::loadUser();
494
        if ($user->id > 0) {
495
            $menumanager = static::$menumanager = static::loadMenuManager();
496
        }
497
        Debug::load();
498
499
        new Database($config->db);
500
501
        // TODO: Example of calling a SELECT from Eloquent and from Dolibarr
502
        // DB::select('SELECT * FROM alx_user'); // use Illuminate\Database\Capsule\Manager as DB;
503
        // $db->query('SELECT * FROM alx_user');
504
    }
505
506
    /**
507
     * Returns a Dolibarr DB connection (DoliDB) instance.
508
     *
509
     * @return DoliDb
510
     * @throws \Exception
511
     */
512
    private static function loadDb()
513
    {
514
        $conf = static::$dolibarrConfig;
0 ignored issues
show
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...
515
        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
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...
516
        static::$dolibarrConfig->setValues(static::$db);
0 ignored issues
show
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

516
        static::$dolibarrConfig->/** @scrutinizer ignore-call */ 
517
                                 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...
517
518
        return static::$db;
519
    }
520
521
    private static function loadUser()
522
    {
523
        static::$user = new User(static::$db);
0 ignored issues
show
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...
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...
524
        return static::$user;
525
    }
526
}
527