Test Failed
Push — main ( ad6638...75849e )
by Rafael
54:44
created

getControllerRoute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 19
rs 9.9666
1
<?php
2
3
/**
4
 * Copyright (C) 2024       Rafael San José         <[email protected]>
5
 * Copyright (C) 2024       Rafael San José             <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
use Alxarafe\Lib\Dispatcher;
22
use Alxarafe\Lib\Functions;
23
24
/**
25
 * Gets the URL segment corresponding to the controller route.
26
 *
27
 * @return string
28
 */
29
function getControllerRoute(): string
30
{
31
    $full = $_SERVER['REQUEST_URI'] ?? '/index.php';
32
    $interrogation_position = strpos($full, '?');
33
    if ($interrogation_position !== false) {
34
        $full = substr($full, 0, $interrogation_position);
35
    }
36
    /**
37
     * If the URL is formed as a subdirectory, it will have to be accessed through
38
     * the public subdirectory.
39
     * In that case, the path is from the public folder, since up to public, it
40
     * will be part of BASE_URL.
41
     */
42
    $public_cad = '/public/';
43
    $public_pos = strpos($full, $public_cad);
44
    if ($public_pos !== false) {
45
        $full = substr($full, $public_pos + strlen($public_cad) - 1);
46
    }
47
    return $full;
48
}
49
50
/**
51
 * Obtains the main url
52
 *
53
 * TODO: This function is defined in Alxarafe.
54
 *       As soon as the Alxarafe code is brought in, it will be removed and
55
 *       Functions::getUrl() will be used instead.
56
 *
57
 * @return string
58
 */
59
function getUrl()
60
{
61
    $ssl = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
62
    $proto = strtolower($_SERVER['SERVER_PROTOCOL']);
63
    $proto = substr($proto, 0, strpos($proto, '/')) . ($ssl ? 's' : '');
64
    if (isset($_SERVER['HTTP_HOST'])) {
65
        $host = $_SERVER['HTTP_HOST'];
66
    } else {
67
        $port = $_SERVER['SERVER_PORT'];
68
        $port = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;
69
        $host = $_SERVER['SERVER_NAME'] . $port;
70
    }
71
72
    $script = $_SERVER['SCRIPT_NAME'];
73
74
    $script = substr($script, 0, strlen($script) - strlen('/index.php'));
75
    return $proto . '://' . $host . $script;
76
}
77
const BASE_PATH = __DIR__;
78
79
$autoload_filename = realpath(BASE_PATH . '/../vendor/autoload.php');
80
if (!file_exists($autoload_filename)) {
81
    die('<h1>COMPOSER ERROR</h1><p>You need to run: "composer install"</p>');
82
}
83
84
require_once $autoload_filename;
85
86
define('BASE_URL', getUrl());
87
88
// Define Dolibarr Constants
89
define('DOL_DOCUMENT_ROOT', constant('BASE_PATH') . '/htdocs');
90
define('DOL_URL_ROOT', constant('BASE_URL') . '/htdocs');
91
92
/**
93
 * Here we would have to check if the variables that determine the use of a
94
 * modern controller have been received via GET.
95
 *
96
 * For the moment, we are taking Dolibarr style routes.
97
 */
98
99
$route = getControllerRoute();
100
if (!isset($route) || $route === '/') {
101
    $route = '/index.php';
102
}
103
104
$fullname = DOL_DOCUMENT_ROOT . $route;
105
$last_slash_pos = strrpos($fullname, '/');
106
$fullpath = $fullname;
107
if ($last_slash_pos !== false) {
108
    $fullpath = substr($fullpath, 0, $last_slash_pos);
109
}
110
$relative_path = substr($fullpath, strlen(DOL_DOCUMENT_ROOT));
111
112
/**
113
 * Dolibarr uses the $_SERVER['PHP_SELF'] variable in much of the code to know how it
114
 * has been invoked. Now it doesn't work because the entry point is unique.
115
 * Here we put a temporary patch while it is migrating.
116
 */
117
$_SERVER['PHP_SELF'] = constant('BASE_URL') . DIRECTORY_SEPARATOR . $route;
118
119
chdir($fullpath);
120
121
require_once $fullname;
122