Menu

[55c200]: / app / Bootstrap.php  Maximize  Restore  History

Download this file

490 lines (426 with data), 16.5 kB

  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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* Class Bootstrap
*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* @var Nwicode_Controller_Request_Http
*/
public $_request = null;
/**
* @var Application_Model_Application
*/
public $_application = null;
/**
* @var Zend_Controller_Front
*/
public $_front_controller = false;
/**
* @throws Zend_Exception
* @throws Zend_Loader_Exception
* @throws \DebugBar\DebugBarException
*/
protected function _initPaths()
{
$loader = \Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Core');
$loader->registerNamespace('Symfony');
$loader->registerNamespace('Plesk');
$loader->registerNamespace('Woocommerce');
$loader->registerNamespace('PListEditor');
$includePaths = [get_include_path()];
$includePaths[] = realpath(APPLICATION_PATH . '/local/modules');
switch (\Nwicode\Version::TYPE) {
case "PE":
$includePaths[] = realpath(APPLICATION_PATH . "/pe/modules");
$includePaths[] = realpath(APPLICATION_PATH . "/mae/modules");
$includePaths[] = realpath(APPLICATION_PATH . "/sae/modules");
break;
case "MAE":
$includePaths[] = realpath(APPLICATION_PATH . "/mae/modules");
$includePaths[] = realpath(APPLICATION_PATH . "/sae/modules");
break;
case "SAE":
default:
$includePaths[] = realpath(APPLICATION_PATH . "/sae/modules");
}
// Updating the include_paths!
set_include_path(implode(PATH_SEPARATOR, $includePaths));
$this->bootstrap('CacheManager');
$dbCache = $this->getResource('CacheManager')->getCache('database');
Zend_Db_Table_Abstract::setDefaultMetadataCache($dbCache);
$base_path = '';
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$base_path = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
}
\Core_Model_Directory::setBasePath($base_path);
// include Stubs
require_once \Core_Model_Directory::getBasePathTo('/lib/vendor/autoload.php');
require_once \Core_Model_Directory::getBasePathTo('/lib/Nwicode/Pure.php');
require_once \Core_Model_Directory::getBasePathTo('/lib/Nwicode/Stubs.php');
// Then load class aliases
\Nwicode\Stubs::loadAliases();
$path = '';
if (isset($_SERVER['SCRIPT_NAME'])) {
$path = $_SERVER['SCRIPT_NAME'];
} else if (isset($_SERVER['PHP_SELF'])) {
$path = $_SERVER['PHP_SELF'];
}
$path = str_replace('/' . basename($path), '', $path);
\Core_Model_Directory::setPath($path);
// Init debugger if needed!
\Nwicode\Debug::init();
\Nwicode\Exec::start();
}
/**
*
*/
protected function _initErrorMessages()
{
if (APPLICATION_ENV === 'production') {
error_reporting(0);
} else {
// Reports all errors, handled by Nwicode_Error!
error_reporting(E_ALL);
Nwicode_Error::init();
}
}
/**
* @throws Zend_Application_Bootstrap_Exception
* @throws Zend_Cache_Exception
*/
protected function _initCache()
{
$defaultCache = $this->getResource("CacheManager")->getCache("default");
$outputCache = $this->getResource("CacheManager")->getCache("output");
$cacheDir = Core_Model_Directory::getCacheDirectory(true);
if (is_writable($cacheDir)) {
$frontendConf = [
'lifetime' => 345600,
'automatic_seralization' => true
];
$backendConf = [
'cache_dir' => $cacheDir
];
$cache = Zend_Cache::factory("Core", "File", $frontendConf, $backendConf);
$cache->setOption("automatic_serialization", true);
Zend_Locale::setCache($defaultCache);
Zend_Registry::set("cache", $defaultCache);
Zend_Registry::set("cacheOutput", $outputCache);
}
}
/**
*
*/
protected function _initHtaccess()
{
$oldHtaccess = path('htaccess.txt');
$newHtaccess = path('.htaccess');
if (!file_exists($newHtaccess) &&
is_readable($oldHtaccess) &&
is_writable(path())) {
$content = file_get_contents($oldHtaccess);
$content = str_replace(
'# ${RewriteBase}',
'RewriteBase ' . Core_Model_Directory::getPathTo(),
$content);
$htaccess = fopen($newHtaccess, "w");
fputs($htaccess, $content);
fclose($htaccess);
}
}
/**
* @throws Zend_Log_Exception
*/
protected function _initLogger()
{
if (!is_dir(path('var/log'))) {
mkdir(path('var/log'), 0777, true);
}
// Clean-up old template installer!
$tmp = path('var/tmp/template.install.php');
if (is_file($tmp)) {
unlink($tmp);
}
$writer = new \Zend_Log_Writer_Stream(path('var/log/output.log'));
$logger = new \Nwicode\Log($writer);
\Zend_Registry::set('logger', $logger);
}
protected function _initConnection()
{
$this->bootstrap('db');
$resource = $this->getResource('db');
// Set profiler if needed!
$resource = Nwicode_Debug::setProfiler($resource);
// Disabling strict mode on run!
try {
$resource->query('SET sql_mode = \'\';');
} catch (Exception $e) {
$logger = Zend_Registry::get('logger');
$logger->err('Fatal Error when trying to disable SQL strict mode: ' . PHP_EOL . print_r($e, true));
}
Zend_Registry::set('db', $resource);
if (Installer_Model_Installer::isInstalled()) {
try {
$default = new Core_Model_Db_Table();
$default->checkConnection();
} catch (Exception $e) {
$logger = Zend_Registry::get('logger');
$logger->sendException('Fatal Error When Connecting to The Database: ' . PHP_EOL . print_r($e, true));
}
}
}
/**
* Permet de garder le nom des modules avec une majuscule et les url en minuscule
*/
protected function _initDispatcher()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->setDispatcher(new Nwicode_Controller_Dispatcher_Standard());
$this->bootstrap('frontController');
$this->_front_controller = $frontController;
}
/**
* Core modules are loaded dynamically depending on the Edition
*
* @version 4.12.18
*/
protected function _initModuleDirectory()
{
$base = Core_Model_Directory::getBasePathTo('app');
if (!is_dir($base . '/local') ||
!is_dir($base . '/local/modules') ||
!is_dir($base . '/local/design')) {
mkdir($base . '/local', 0777);
mkdir($base . '/local/modules', 0777);
mkdir($base . '/local/design', 0777);
}
// Priorities are inverted for controllers!
switch (\Nwicode\Version::TYPE) {
default:
case 'SAE':
$this->_front_controller->addModuleDirectory($base . '/sae/modules');
break;
case 'MAE':
$this->_front_controller->addModuleDirectory($base . '/sae/modules');
$this->_front_controller->addModuleDirectory($base . '/mae/modules');
break;
case 'PE':
$this->_front_controller->addModuleDirectory($base . '/sae/modules');
$this->_front_controller->addModuleDirectory($base . '/mae/modules');
$this->_front_controller->addModuleDirectory($base . '/pe/modules');
break;
}
if (is_readable($base . '/local/modules')) {
$this->_front_controller->addModuleDirectory($base . '/local/modules');
}
Nwicode_Utils::load();
}
protected function _initInstaller()
{
$front = $this->_front_controller;
$module_names = $front->getDispatcher()->getModuleDirectories();
Installer_Model_Installer::setModules($module_names);
}
protected function _initRequest()
{
Core_Model_Language::prepare();
$frontController = $this->_front_controller;
$this->_request = new Nwicode_Controller_Request_Http();
$this->_request->isInstalling(!Installer_Model_Installer::isInstalled());
$this->_request->setPathInfo();
$baseUrl = $this->_request->getScheme() . '://' . $this->_request->getHttpHost() .
$this->_request->getBaseUrl();
$this->_request->setBaseUrl($baseUrl);
$frontController->setRequest($this->_request);
Nwicode_View::setRequest($this->_request);
Core_Model_Default::setBaseUrl($this->_request->getBaseUrl());
// Translator
Nwicode_Cache_Translation::init();
Core_Model_Translator::init();
}
// Loading individual bootstrappers!
protected function _initModuleBoostrap()
{
$edition_path = strtolower(Nwicode_Version::TYPE);
require_once Core_Model_Directory::getBasePathTo('app/' . $edition_path . '/bootstrap.php');
Module_Bootstrap::init($this);
$module_names = $this->_front_controller->getDispatcher()->getModuleDirectories();
foreach ($module_names as $module) {
$path = $this->_front_controller->getModuleDirectory($module) . '/bootstrap.php';
$path_init = $this->_front_controller->getModuleDirectory($module) . '/init.php';
// Init is the new flavor 5.0, and has priority over bootstrap!
if (is_readable($path_init)) {
try {
global $init;
ob_start();
require_once $path_init;
if (is_callable($init)) {
$init($this);
}
ob_end_clean();
} catch (Exception $e) {
// Silently catch & log malformed init module!
trigger_error($e->getMessage());
}
} else if (is_readable($path)) {
try {
$classname = $module . '_Bootstrap';
// Ensure this Class is not duplicated!
if (!class_exists($classname, false)) {
require_once $path;
if (class_exists($classname)) {
$bs = new $classname();
if (method_exists($bs, "init")) {
$bs::init($this);
}
}
} else {
throw new \Nwicode\Exception('The bootstrap file located at \'' . $path .
'\' redefines/or is already loaded, Class \'' . $classname .
'\', please remove it or rename it.');
}
} catch (Exception $e) {
// Silently catch & log malformed bootstrap module!
trigger_error($e->getMessage());
}
}
}
}
/**
*
*/
protected function _initDesign()
{
if (!$this->_request->isInstalling()) {
// Ensure 'flat' design is used for everyone!
try {
__set('editor_design', 'flat');
} catch (\Exception $e) {
// Nope!
}
// Monkey patch admin with wrong parentId
try {
$adminRole = (new Acl_Model_Role())->find('Admin', 'code');
$adminRole->setParentId(null)->save();
} catch (\Exception $e) {
// Nope!
}
// Check default role
try {
$fixDefaultRole = __get('fix_default_role_4.15.3');
if ($fixDefaultRole !== 'done') {
$roleId = __get('admin_default_role_id');
$role = (new Acl_Model_Role())->find($roleId);
if (!$role->getId()) {
// Get admin role
$adminRole = (new Acl_Model_Role())->find('Admin', 'code');
if ($adminRole->getId()) {
__set('admin_default_role_id', $adminRole->getId());
__set('fix_default_role_4.15.3', 'done');
}
} else {
__set('fix_default_role_4.15.3', 'done');
}
}
} catch (\Exception $e) {
// Nope!
}
}
Nwicode_Cache_Design::init();
$this->getPluginLoader()->addPrefixPath('Nwicode_Application_Resource', 'Nwicode/Application/Resource');
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNeverRender(true);
}
protected function _initPurifier()
{
$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.SerializerPath', Core_Model_Directory::getBasePathTo('var/cache'));
$def = $config->getHTMLDefinition(true);
// Attributes for in-app links
$def->addAttribute('a', 'data-offline', 'Text');
$def->addAttribute('a', 'data-params', 'Text');
$def->addAttribute('a', 'data-state', 'Text');
$htmlPurifier = new HTMLPurifier($config);
Zend_Registry::set('htmlPurifier', $htmlPurifier);
}
/**
*
*/
protected function _initRouter()
{
$front = $this->_front_controller;
$router = $front->getRouter();
$router
->addRoute('default',
new Nwicode_Controller_Router_Route_Module(
[],
$front->getDispatcher(),
$front->getRequest()
));
}
public function _initMinify()
{
// Minify Cache!
if (Installer_Model_Installer::isInstalled()) {
$minifier = new Nwicode_Minify();
$minifier->build();
}
}
/**
* @throws Exception
*/
protected function _initModules()
{
if (!$this->_request->isInstalling()) {
$front = $this->_front_controller;
$module_names = $front->getDispatcher()->getSortedModuleDirectories();
if (APPLICATION_ENV === 'development') {
foreach ($module_names as $module_name) {
$module = new Installer_Model_Installer_Module();
$module->prepare($module_name);
if ($module->canUpdate()) {
$module->install();
$module->insertData();
}
}
}
}
}
/**
* @throws Zend_Config_Exception
* @throws Zend_Session_Exception
*/
public function _initSession()
{
$session_ini = Core_Model_Directory::getBasePathTo('/app/configs/session.ini');
$config = new Zend_Config_Ini($session_ini, 'production');
$_config = $config->toArray();
// Awesome session alteration!
if ($this->_request->isApplication()) {
$_config['name'] = 'front';
}
Zend_Session::setOptions($_config);
}
/**
* @return mixed|Zend_Controller_Response_Abstract
* @throws Exception
* @throws Zend_Application_Bootstrap_Exception
*/
public function run()
{
$front = $this->_front_controller;
$default = $front->getDefaultModule();
if (null === $front->getControllerDirectory($default)) {
throw new Zend_Application_Bootstrap_Exception(
'No default controller directory registered with front controller'
);
}
$front->setParam('bootstrap', $this);
$request = $front->getRequest();
$response = $front->dispatch($request);
if ($front->returnResponse()) {
return $response;
}
}
}