-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbootstrap.php
184 lines (163 loc) · 5.78 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* \details © 2018 Open Ximdex Evolution SL [http://www.ximdex.org]
*
* Ximdex a Semantic Content Management System (CMS)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* See the Affero GNU General Public License for more details.
* You should have received a copy of the Affero GNU General Public License
* version 3 along with Ximdex (see LICENSE file).
*
* If not, visit http://gnu.org/licenses/agpl-3.0.html.
*
* @author Ximdex DevTeam <dev@ximdex.com>
* @version $Revision$
*/
use Ximdex\Logger;
use Ximdex\Modules\Manager;
use Ximdex\Runtime\App;
// For legacy compatibility
if (! defined('XIMDEX_ROOT_PATH')) {
define('XIMDEX_ROOT_PATH', __DIR__);
} else {
return false; // Only once this file
}
if (! defined('XIMDEX_VENDOR')) {
define('XIMDEX_VENDOR', '/vendor');
}
/**
* XIMDEX_DIRECT is true when bootstrap is called directly
* XIMDEX_DIRECT is false when bootstrap is called from other php
*/
if (! defined('XIMDEX_DIRECT')) {
$included_files = get_included_files();
define('XIMDEX_DIRECT', !isset($included_files[1]));
}
// Checking cli mode
if (! defined('CLI_MODE')) {
global $argv, $argc;
if ('cli' != php_sapi_name() || empty($argv) || 0 == $argc) {
$cli_mode = false;
} else {
$cli_mode = true;
}
define('CLI_MODE', $cli_mode);
}
include_once XIMDEX_ROOT_PATH . XIMDEX_VENDOR . '/autoload.php';
// Initialize XIMDEX_ROOT_PATH
App::setValue('XIMDEX_ROOT_PATH', dirname(dirname(__FILE__)));
// Get Config from install file
if (file_exists(XIMDEX_ROOT_PATH . '/conf/install-params.conf.php')) {
$conf = require_once(XIMDEX_ROOT_PATH . '/conf/install-params.conf.php');
foreach ($conf as $key => $value) {
App::setValue($key, $value);
}
}
// Generate general purpose logs files
Logger::generate('XMD', 'xmd', true);
Logger::generate('ACTIONS', 'actions');
Logger::generate('PREVIEW', 'preview');
// Set default log (xmd.log)
Logger::setActiveLog();
// Use config values
define('DEFAULT_LOCALE', App::getValue('locale', 'ES_es'));
date_default_timezone_set(App::getValue('timezone', 'Europe/Madrid'));
// Set DB Connection
$dbConfig = App::getValue('db');
if (! empty($dbConfig)) {
try {
$dbConn = new \PDO("{$dbConfig['type']}:host={$dbConfig['host']};port={$dbConfig['port']};dbname={$dbConfig['db']};charset=utf8",
$dbConfig['user'], $dbConfig['password']);
} catch (\PDOException $e) {
$error = 'Can\'t connect to database at ' . $dbConfig['host'] . ':' . $dbConfig['port'] . ' (' . $e->getMessage() . ')';
Logger::error($error);
if (CLI_MODE) {
$color = new Colors\Color();
echo $color($error)->red()->bold() . PHP_EOL;
}
die();
}
$dbConn->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
App::addDbConnection($dbConn);
// Get Persistent Config
$stm = App::Db()->prepare('select * from Config');
$stm->execute();
foreach ($stm as $row) {
App::setValue($row['ConfigKey'], $row['ConfigValue']);
}
}
// Read install-modules.php
$installModulesPath = XIMDEX_ROOT_PATH . '/conf/install-modules.php';
if (file_exists($installModulesPath)) {
$modulesConfString = file_get_contents($installModulesPath);
$matches = array();
preg_match_all('/define\(\'(.*)\',(.*)\);/iUs', $modulesConfString, $matches);
foreach ($matches[1] as $key => $value) {
App::setValue($value, str_replace('\'', '', $matches[2][$key]));
}
}
// From MVC
if (! defined('RENDERER_ROOT_PATH')) {
define('RENDERER_ROOT_PATH', XIMDEX_ROOT_PATH . '/src/MVC/Render');
}
if (! defined('SMARTY_TMP_PATH')) {
define('SMARTY_TMP_PATH', XIMDEX_ROOT_PATH . App::getValue('TempRoot'));
}
if (! defined('APP_ROOT_PATH')) {
define('APP_ROOT_PATH', XIMDEX_ROOT_PATH . (App::getValue('UrlFrontController') ?? '/public_xmd'));
}
// Initialize Modules Manager
$modulesFile = Manager::get_modules_install_params();
if (file_exists(XIMDEX_ROOT_PATH . $modulesFile)) {
Manager::file($modulesFile, 'XIMDEX');
} else {
Manager::file('/install/InstallController.class.php');
if (InstallController::isInstalled()) {
Logger::error('Cannot load the modules configuration file');
}
}
// special objects (pseudo-DI)
class_alias('Ximdex\Utils\Messages', 'Messages');
// Extensions setup
include_once XIMDEX_ROOT_PATH . '/conf/extensions.conf.php';
/**
* Execute function init for each enabled module
*/
foreach (Manager::getEnabledModules() as $module) {
$name = $module["name"];
$mManager = new Manager;
$moduleInstance = $mManager->instanceModule($name);
if (method_exists($moduleInstance, 'init')) {
$moduleInstance->init();
}
}
// CLI load
if (XIMDEX_DIRECT && CLI_MODE && isset($argv[1])) {
/* e.g: $ /bootstrap.php src/Sync/scripts/scheduler/scheduler.php
* $command = /bootstrap.php => argv[0]
* $script = src/Sync/scripts/scheduler/scheduler.php => new_command
*/
array_shift($argv);
$argc--;
$script = parse_url($argv[0], PHP_URL_PATH);
$is_absolute_path = ('/' == $script[0]) ? : false;
if ($is_absolute_path) {
$external_script = $script;
} else {
$external_script = XIMDEX_ROOT_PATH . '/' . $script;
}
$new_command = $argv[0] = $external_script;
if (file_exists($new_command)) {
require_once($new_command);
}
}