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