This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Bootstrap.php
634 lines (521 loc) · 20.1 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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<?php
/**
* This file is part of the {@link http://ontowiki.net OntoWiki} project.
*
* @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/**
* OntoWiki bootstrap class.
*
* Provides on-demand loading of application resources.
*
* @category OntoWiki
* @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
* @author Norman Heino <norman.heino@gmail.com>
*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Initializes the the extension manager which in turn scans
* for components, modules, plugins and wrapper and registers them.
*
* @since 0.9.5
*/
public function _initExtensionManager()
{
// require Front controller
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
//NOTICE: i swtiched loading of erfurt and session
//because serialized erfurt objects in the session need constants defined in erfurt
//is this ok?
// require Erfurt
$this->bootstrap('Erfurt');
// $erfurt = $this->getResource('Erfurt');
// require Session
$this->bootstrap('Session');
// require Dispatcher
$this->bootstrap('Dispatcher');
$dispatcher = $this->getResource('Dispatcher');
// require OntoWiki
$this->bootstrap('OntoWiki');
$ontoWiki = $this->getResource('OntoWiki');
// require Translate
$this->bootstrap('Translate');
$translate = $this->getResource('Translate');
// require View
$this->bootstrap('View');
$view = $this->getResource('View');
// make sure router is bootstrapped
$this->bootstrap('Router');
// set view
$ontoWiki->view = $view;
// initialize components
$extensionPath = ONTOWIKI_ROOT
. $config->extensions->base;
$extensionPathBase = $config->staticUrlBase
. $config->extensions->base;
OntoWiki_Navigation::reset();
$extensionManager = new OntoWiki_Extension_Manager($extensionPath);
$extensionManager->setTranslate($translate)
->setComponentUrlBase($extensionPathBase);
// register component controller directories
foreach ($extensionManager->getComponents() as $extensionName => $extensionConfig) {
$frontController->addControllerDirectory($extensionConfig->path, '_component_' . $extensionName);
}
// make component manager available to dispatcher
$dispatcher = $frontController->getDispatcher();
$dispatcher->setExtensionManager($extensionManager);
// keep component manager in OntoWiki
$ontoWiki->extensionManager = $extensionManager;
// actionhelper
Zend_Controller_Action_HelperBroker::addPrefix('OntoWiki_Controller_ActionHelper_');
Zend_Controller_Action_HelperBroker::addHelper(new OntoWiki_Controller_ActionHelper_List());
}
/**
* Loads the application config file
*
* @since 0.9.5
*/
public function _initConfig()
{
// load default application configuration file
try {
$config = new Zend_Config_Ini(APPLICATION_PATH . 'config/default.ini', 'default', true);
} catch (Zend_Config_Exception $e) {
exit($e->getMessage());
}
// load user application configuration files
try {
$privateConfig = new Zend_Config_Ini(ONTOWIKI_ROOT . 'config.ini', 'private', true);
$config->merge($privateConfig);
} catch (Zend_Config_Exception $e) {
$message = '<p>OntoWiki can not find a proper configuration.</p>' . PHP_EOL .
'<p>Maybe you have to copy and modify the distributed <code>config.ini-dist</code> file?</p>' . PHP_EOL .
'<details><summary>Error Details</summary>' . $e->getMessage() . '</details>';
exit($message);
}
// normalize path names
$config->themes->path = rtrim($config->themes->path, '/\\') . '/';
$config->themes->default = rtrim($config->themes->default, '/\\') . '/';
$config->extensions->base = rtrim($config->extensions->base, '/\\') . '/';
if (!defined('EXTENSION_PATH')) {
define('EXTENSION_PATH', $config->extensions->base);
}
$config->extensions->legacy = EXTENSION_PATH . rtrim($config->extensions->legacy, '/\\') . '/';
$config->languages->path = EXTENSION_PATH . rtrim($config->languages->path, '/\\') . '/';
$config->libraries->path = rtrim($config->libraries->path, '/\\') . '/';
$config->cache->path = rtrim($config->cache->path, '/\\') . '/';
$config->log->path = rtrim($config->log->path, '/\\') . '/';
// support absolute path
$matches = array();
if (!(preg_match('/^(\w:[\/|\\\\]|\/)/', $config->cache->path, $matches) === 1)) {
$config->cache->path = ONTOWIKI_ROOT . $config->cache->path;
}
// set path variables
$rewriteBase = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], BOOTSTRAP_FILE));
$protocol = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https' : 'http';
$port = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443')
? (':' . $_SERVER['SERVER_PORT'])
: '';
$urlBase = sprintf('%s://%s%s%s',
$protocol,
isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost',
$port,
$rewriteBase);
// construct URL variables
$config->host = parse_url($urlBase, PHP_URL_HOST);
$config->urlBase = rtrim($urlBase . (ONTOWIKI_REWRITE ? '' : BOOTSTRAP_FILE), '/\\') . '/';
$config->staticUrlBase = rtrim($urlBase, '/\\') . '/';
$config->themeUrlBase = $config->staticUrlBase
. $config->themes->path
. $config->themes->default;
$config->libraryUrlBase = $config->staticUrlBase
. $config->libraries->path;
// define constants for development/debugging
if (isset($config->debug) and (boolean)$config->debug) {
// display errors
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
// enable debugging options
define('_OWDEBUG', 1);
// log everything
$config->log->level = 7;
}
return $config;
}
/**
* Initializes the modified Zend_Conroller_Dispatcher to allow for
* pluggable controllers
*
* @since 0.9.5
*/
public function _initDispatcher()
{
// require Front controller
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
$dispatcher = new OntoWiki_Dispatcher(array('url_base' => $config->urlBase));
$dispatcher->setControllerDirectory(APPLICATION_PATH . 'controllers');
$frontController->setDispatcher($dispatcher);
return $dispatcher;
}
/**
* Initializes the Erfurt framework
*
* @since 0.9.5
*/
public function _initErfurt()
{
$erfurt = null;
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
// require OntoWiki
$this->bootstrap('OntoWiki');
$ontoWiki = $this->getResource('OntoWiki');
// require Logger, since Erfurt logger should write into OW logs dir
$this->bootstrap('Logger');
try {
$erfurt = Erfurt_App::getInstance(false)->start($config);
} catch (Erfurt_Exception $ee) {
exit('Error loading Erfurt framework: ' . $ee->getMessage());
} catch (Exception $e) {
exit('Unexpected error: ' . $e->getMessage());
}
// make available
$ontoWiki->erfurt = $erfurt;
return $erfurt;
}
/**
* Initializes the event dispatcher
*
* @since 0.9.5
*/
public function _initEventDispatcher()
{
// load event dispatcher for Erfurt and OntoWiki events
$eventDispatcher = Erfurt_Event_Dispatcher::getInstance();
return $eventDispatcher;
}
/**
* Loads logging capability
*
* @since 0.9.5
*/
public function _initLogger()
{
// require config
$this->bootstrap('Config');
$config = $this->getResource('Config');
// support absolute path
if (!(preg_match('/^(\w:[\/|\\\\]|\/)/', $config->log->path) === 1)) {
// prepend OntoWiki root for relative paths
$config->log->path = ONTOWIKI_ROOT . $config->log->path;
}
// initialize logger
if (is_writable($config->log->path) && ((boolean)$config->log->level !== false)) {
$levelFilter = new Zend_Log_Filter_Priority((int)$config->log->level, '<=');
$writer = new Zend_Log_Writer_Stream($config->log->path . 'ontowiki.log');
$logger = new Zend_Log($writer);
$logger->addFilter($levelFilter);
return $logger;
}
// fallback to NULL logger
$writer = new Zend_Log_Writer_Null();
$logger = new Zend_Log($writer);
return $logger;
}
/**
* Initializes the navigation
*
* @since 0.9.5
*/
public function _initNavigation()
{
// require Session
$this->bootstrap('Session');
$session = $this->getResource('Session');
$this->bootstrap('Request');
$request = $this->getResource('Request');
$this->bootstrap('Config');
$config = $this->getResource('Config');
// get current action name
$currentAction = $request->getActionName();
// is current action a default action?
if ($currentAction == 'properties' or $currentAction == 'instances') {
// save it to session
$session->lastRoute = $currentAction;
}
// get last route or default
$route = isset($session->lastRoute)
? $session->lastRoute
: $config->route->default->name;
// Reset navigation for multiple boostraping (tests)
OntoWiki_Navigation::reset();
// register with navigation
if (isset($config->routes->{$route})) {
extract($config->routes->{$route}->defaults->toArray());
// and add last routed component
OntoWiki_Navigation::register('index', array(
'route' => $route,
'controller' => $controller,
'action' => $action,
'name' => ucfirst($route),
'priority' => 0
));
}
}
/**
* Initializes the OntoWiki main class
*
* @since 0.9.5
*/
public function _initOntoWiki()
{
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
OntoWiki::reset();
$ontoWiki = OntoWiki::getInstance();
$ontoWiki->language = isset($config->languages->locale) ? $config->languages->locale : null;
$ontoWiki->config = $config;
return $ontoWiki;
}
public function _initPlugins()
{
// require front controller
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
$frontController->registerPlugin(new OntoWiki_Controller_Plugin_HttpAuth(), 1); // Needs to be done first!
$frontController->registerPlugin(new OntoWiki_Controller_Plugin_SetupHelper(), 2);
$frontController->registerPlugin(new OntoWiki_Controller_Plugin_ListSetupHelper(), 3); //needs to be done after SetupHelper
}
/**
* Initializes the request object
*
* @since 0.9.5
*/
public function _initRequest()
{
$this->bootstrap('FrontController');
$frontController = $this->getResource('FrontController');
$this->bootstrap('Router');
$router = $this->getResource('Router');
$request = new OntoWiki_Request();
$frontController->setRequest($request);
$router->route($request);
return $request;
}
/**
* Initializes the router
*
* @since 0.9.5
*/
public function _initRouter()
{
// require front controller
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
$router = $frontController->getRouter();
$router->addConfig($config->routes);
return $router;
}
/**
* Initializes the session and loads session variables
*
* @since 0.9.5
*/
public function _initSession()
{
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
// require Config
$this->bootstrap('OntoWiki');
$ontoWiki = $this->getResource('OntoWiki');
// init session
$sessionKey = 'ONTOWIKI' . (isset($config->session->identifier) ? $config->session->identifier : '');
$session = new Zend_Session_Namespace($sessionKey);
// define the session key as a constant for global reference
if (!defined('_OWSESSION')) {
define('_OWSESSION', $sessionKey);
}
// inject session vars into OntoWiki
if (array_key_exists('sessionVars', $this->_options['bootstrap'])) {
$ontoWiki->setSessionVars((array)$this->_options['bootstrap']['sessionVars']);
}
// make available
$ontoWiki->session = $session;
return $session;
}
/**
* Initializes the toolbar
*
* @since 0.9.5
*/
public function _initToolbar()
{
$this->bootstrap('Config');
$config = $this->getResource('Config');
$this->bootstrap('Translate');
$translate = $this->getResource('Translate');
// configure toolbar
$toolbar = OntoWiki_Toolbar::getInstance();
$toolbar->setThemeUrlBase($config->themeUrlBase)
->setTranslate($translate);
return $toolbar;
}
/**
* Loads the translation
*
* @since 0.9.5
*/
public function _initTranslate()
{
$this->bootstrap('Config');
$config = $this->getResource('Config');
// setup translation cache
if ((boolean)$config->cache->translation and is_writable($config->cache->path)) {
$translationFile = ONTOWIKI_ROOT
. $config->languages->path
. $config->languages->locale
. DIRECTORY_SEPARATOR
. 'core.csv';
$frontendOptions = array(
'lifetime' => 3600,
'automatic_serialization' => true,
'master_file' => $translationFile
);
$backendOptions = array(
'cache_dir' => $config->cache->path
);
$translationCache = Zend_Cache::factory('File', 'File', $frontendOptions, $backendOptions);
// set translation cache
Zend_Translate::setCache($translationCache);
}
// set up translations
$options = array(
// scan locale from directories
'scan' => Zend_Translate::LOCALE_DIRECTORY,
// don't emit notices
'disableNotices' => true
);
$translate = new Zend_Translate('csv', ONTOWIKI_ROOT . $config->languages->path, null, $options);
try {
$translate->setLocale($config->languages->locale);
} catch (Zend_Translate_Exception $e) {
$config->languages->locale = 'en';
$translate->setLocale('en');
}
return $translate;
}
/**
* Authenticates the current user or Anonymous with Erfurt
*
* @since 0.9.5
*/
public function _initUser()
{
$user = null;
// require Erfurt
$this->bootstrap('Erfurt');
$erfurt = $this->getResource('Erfurt');
// get logged in user
$auth = $erfurt->getAuth();
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
}
if (null === $user) {
// authenticate anonymous user
$erfurt->authenticate('Anonymous', '');
$user = $auth->getIdentity();
}
return $user;
}
/**
* Sets up the view environment
*
* @since 0.9.5
*/
public function _initView()
{
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
// require Config
$this->bootstrap('Translate');
$translate = $this->getResource('Translate');
// standard template path
$defaultTemplatePath = ONTOWIKI_ROOT
. 'application/views/templates';
// path for theme template
$themeTemplatePath = ONTOWIKI_ROOT
. $config->themes->path
. $config->themes->default
. 'templates';
$viewOptions = array(
'use_module_cachce' => (bool)$config->cache->modules,
'cache_path' => $config->cache->path,
'lang' => $config->languages->locale
);
// init view
$view = new OntoWiki_View($viewOptions, $translate);
$view->addScriptPath($defaultTemplatePath) // default templates
->addScriptPath($themeTemplatePath) // theme templates override default ones
->addScriptPath($config->extensions->base) // extension templates
->setEncoding($config->encoding)
->setHelperPath(ONTOWIKI_ROOT . 'application/classes/OntoWiki/View/Helper', 'OntoWiki_View_Helper');
// set Zend_View to emit notices in debug mode
$view->strictVars(defined('_OWDEBUG'));
// init view renderer action helper
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
$themeLayoutTemplate = $themeTemplatePath
. DIRECTORY_SEPARATOR
. 'layouts'
. DIRECTORY_SEPARATOR
. 'layout.phtml';
$layoutPath = $defaultTemplatePath . DIRECTORY_SEPARATOR . 'layouts';
if (is_readable($themeLayoutTemplate)) {
$layoutPath = $themeTemplatePath
. DIRECTORY_SEPARATOR
. 'layouts';
}
// initialize layout
Zend_Layout::startMvc(array(
// for layouts we use the default path
'layoutPath' => $layoutPath
));
return $view;
}
/**
* Initializes the wrapper manager
*
* @since 0.9.5
*/
public function _initWrapperManager()
{
// require Erfurt
$this->bootstrap('Erfurt');
$erfurt = $this->getResource('Erfurt');
// require Config
$this->bootstrap('Config');
$config = $this->getResource('Config');
// initialize wrapper manager and load wrapper
$wrapperManager = $erfurt->getWrapperManager(false);
$wrapperManager->addWrapperPath(ONTOWIKI_ROOT . $config->extensions->wrapper);
return $wrapperManager;
}
}