Test Failed
Branch main (fda838)
by Rafael
50:22
created

Config::loadConf()   F

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 87
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 60
nc 1114113
nop 1
dl 0
loc 87
rs 0
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\Globals;
22
use Conf;
23
use MenuManager;
24
use stdClass;
25
26
require_once BASE_PATH . '/core/class/conf.class.php';
27
28
/**
29
 * Generate an object with the configuration of the Dolibarr conf.php file.
30
 *
31
 * @info https://wiki.dolibarr.org/index.php/Configuration_file
32
 *
33
 * @deprecated This class is only needed for compatibility with Dolibarr.
34
 */
35
abstract class Config
36
{
37
    /**
38
     * Contains the information from the conf.php file.
39
     *
40
     * @var null|stdClass
41
     */
42
    protected static $config = null;
43
44
    /**
45
     * Contains the information of the old conf global var.
46
     *
47
     * @var null|stdClass
48
     */
49
    protected static $conf = null;
50
51
    /**
52
     * Simply replace /htdocs with /documents in $pathDir
53
     *
54
     * @param $pathDir
55
     *
56
     * @return string
57
     */
58
    private static function getDataDir($pathDir)
59
    {
60
        return preg_replace("/\/htdocs$/", "", $pathDir) . '/documents';
61
    }
62
63
    /**
64
     * Returns the Dolibarr conf.php complete path.
65
     *
66
     * @return string
67
     */
68
    public static function getDolibarrConfigFilename()
69
    {
70
        return BASE_PATH . '/conf/conf.php';
71
    }
72
73
    /**
74
     * Returns a stdClass with the information contained in the conf.php file.
75
     *
76
     * @param $reload
77
     *
78
     * @return stdClass|null
79
     */
80
    public static function loadConfig($reload = false): ?stdClass
81
    {
82
        if (isset(static::$config) && !$reload) {
83
            return static::$config;
84
        }
85
86
        $filename = static::getDolibarrConfigFilename();
87
        if (!file_exists($filename) || !is_readable($filename)) {
88
            return null;
89
        }
90
91
        include $filename;
92
93
        $config = new stdClass();
94
95
        // 'main' section
96
        $config->main = new stdClass();
97
        $config->main->base_path = trim($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...
98
        $config->main->base_url = trim($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...
99
        $config->main->data_path = trim($dolibarr_main_data_root ?? static::getDataDir($config->main->base_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...
100
101
        $alt_base_path = $dolibarr_main_document_root_alt ?? false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root_alt seems to never exist and therefore isset should always be false.
Loading history...
102
        if ($alt_base_path !== false) {
103
            $config->main->alt_base_path = trim($dolibarr_main_document_root_alt);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root_alt seems to be never defined.
Loading history...
104
        }
105
106
        $alt_base_url = $dolibarr_main_url_root_alt ?? false;
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...
107
        if ($alt_base_url !== false) {
108
            $config->main->alt_base_url = trim($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...
109
        }
110
111
        $alt_data_path = $dolibarr_main_data_root_alt ?? false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root_alt seems to never exist and therefore isset should always be false.
Loading history...
112
        if ($alt_data_path !== false) {
113
            $config->main->alt_data_path = trim($dolibarr_main_data_root_alt);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root_alt seems to be never defined.
Loading history...
114
        }
115
116
        // 'db' section
117
        $config->db = new stdClass();
118
        $config->db->type = trim($dolibarr_main_db_type ?? 'mysql');
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...
119
        $config->db->host = trim($dolibarr_main_db_host ?? 'localhost');
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...
120
        $config->db->port = trim($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...
121
        $config->db->name = trim($dolibarr_main_db_name ?? 'dolibarr');
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...
122
        $config->db->user = trim($dolibarr_main_db_user ?? 'dolibarr');
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...
123
        $config->db->pass = trim($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...
124
        $config->db->prefix = trim($dolibarr_main_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...
125
        $config->db->charset = trim($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...
126
        $config->db->collation = trim($dolibarr_main_db_collation ?? 'utf8mb4_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...
127
128
        // 'security' section
129
        $config->security = new stdClass();
130
        $config->security->authentication_type = $dolibarr_main_authentication ?? 'dolibarr';
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...
131
        $config->security->force_https = intval($dolibarr_main_force_https ?? 1);
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...
132
        $config->security->unique_id = $dolibarr_main_instance_unique_id ?? null;
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...
133
134
        $config->file = new stdClass();
135
        $config->file->instance_unique_id = $config->security->unique_id;
136
137
        // Others
138
        $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...
139
        if ($demo !== false) {
140
            $credentials = explode(',', $demo);
141
            if (count($credentials) === 2) {
142
                $config->demo->user = trim($credentials[0]);
143
                $config->demo->pass = trim($credentials[1]);
144
            }
145
        }
146
147
        $config->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...
148
149
        // 'Server' section
150
        $config->server = new stdClass();
151
        $config->server->detailed_info = !empty($_SERVER['MAIN_SHOW_TUNING_INFO']);
152
153
        return $config;
154
    }
155
156
    /**
157
     * Create the Dolibarr conf variable.
158
     *
159
     * @param $reload
160
     *
161
     * @return stdClass|null
162
     */
163
    public static function loadConf($reload = false): ?stdClass
164
    {
165
        if (isset(static::$conf) && !$reload) {
166
            return static::$conf;
167
        }
168
169
        $filename = static::getDolibarrConfigFilename();
170
        $exists = file_exists($filename) && is_readable($filename);
171
        if ($exists) {
172
            include $filename;
173
        }
174
175
        /*
176
         * Create $conf object
177
         */
178
179
        $conf = new Conf();
180
181
// Set properties specific to database
182
        $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...
183
        $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...
184
        $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...
185
        $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...
186
        $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...
187
        $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...
188
        $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...
189
        $conf->db->character_set = $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...
190
        $conf->db->dolibarr_main_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...
191
        $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption ?? null;
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...
192
        $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey ?? null;
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...
193
        if (defined('TEST_DB_FORCE_TYPE')) {
194
            $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
195
        }
196
197
// Set properties specific to conf file
198
        $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...
199
        $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...
200
        $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...
201
        $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...
202
        $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...
203
        $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...
204
        $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...
205
        $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...
206
        $conf->file->dol_main_url_root = $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...
207
        $conf->file->dol_document_root = ['main' => (string) DOL_DOCUMENT_ROOT]; // Define array of document root directories ('/home/htdocs')
208
        $conf->file->dol_url_root = ['main' => (string) DOL_URL_ROOT]; // Define array of url root path ('' or '/dolibarr')
209
        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...
210
            // dolibarr_main_document_root_alt can contains several directories
211
            $values = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
212
            $i = 0;
213
            foreach ($values as $value) {
214
                $conf->file->dol_document_root['alt' . ($i++)] = (string) $value;
215
            }
216
            $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...
217
            $i = 0;
218
            foreach ($values as $value) {
219
                if (preg_match('/^http(s)?:/', $value)) {
220
                    // Show error message
221
                    $correct_value = str_replace($dolibarr_main_url_root, '', $value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root seems to be never defined.
Loading history...
222
                    print '<b>Error:</b><br>' . "\n";
223
                    print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>' . "\n";
224
                    print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>' . "\n";
225
                    print 'Value found: ' . $value . '<br>' . "\n";
226
                    print 'Should be replaced by: ' . $correct_value . '<br>' . "\n";
227
                    print "Or something like following examples:<br>\n";
228
                    print "\"/extensions\"<br>\n";
229
                    print "\"/extensions1,/extensions2,...\"<br>\n";
230
                    print "\"/../extensions\"<br>\n";
231
                    print "\"/custom\"<br>\n";
232
                    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...
233
                }
234
                $conf->file->dol_url_root['alt' . ($i++)] = (string) $value;
235
            }
236
        }
237
238
        // Load the main includes of common libraries
239
        if (!defined('NOREQUIREUSER')) {
240
            require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php'; // Need 500ko memory
241
        }
242
        if (!defined('NOREQUIRETRAN')) {
243
            require_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php';
244
        }
245
        if (!defined('NOREQUIRESOC')) {
246
            require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
247
        }
248
249
        return $conf;
250
    }
251
252
    /**
253
     * Create the content for the menumanager variable.
254
     *
255
     * @param $conf
256
     *
257
     * @return MenuManager
258
     * @throws \Exception
259
     */
260
    public static function getMenuManager($conf)
261
    {
262
        // Init menu manager
263
        $db = Globals::getDb($conf);
264
265
        $menumanager = null;
266
        if (!defined('NOREQUIREMENU')) {
267
            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...
268
                $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);
269
            } else {
270
                // If external user
271
                $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);
272
            }
273
274
            // Load the menu manager (only if not already done)
275
            $file_menu = $conf->standard_menu;
276
            if (GETPOST('menu', 'alpha')) {
277
                $file_menu = GETPOST('menu', 'alpha'); // example: menu=eldy_menu.php
278
            }
279
            if (!class_exists('MenuManager')) {
280
                $menufound = 0;
281
                $dirmenus = array_merge(["/core/menus/"], (array) $conf->modules_parts['menus']);
282
                foreach ($dirmenus as $dirmenu) {
283
                    $menufound = dol_include_once($dirmenu . "standard/" . $file_menu);
284
                    if (class_exists('MenuManager')) {
285
                        break;
286
                    }
287
                }
288
                if (!class_exists('MenuManager')) { // If failed to include, we try with standard eldy_menu.php
289
                    dol_syslog("You define a menu manager '" . $file_menu . "' that can not be loaded.", LOG_WARNING);
290
                    $file_menu = 'eldy_menu.php';
291
                    include_once DOL_DOCUMENT_ROOT . "/core/menus/standard/" . $file_menu;
292
                }
293
            }
294
            $menumanager = new MenuManager($db, empty($user->socid) ? 0 : 1);
295
            $menumanager->loadMenu();
296
        }
297
        return $menumanager;
298
    }
299
}
300