forked from midgardproject/midgardmvc_core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.php
494 lines (427 loc) · 14.8 KB
/
interface.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
<?php
/**
* @package midgardmvc_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* Midgard MVC interface class
*
* @package midgardmvc_core
*/
class midgardmvc_core extends midgardmvc_core_component_baseclass
{
/**
* @var midgardmvc_core_services_configuration_yaml
*/
public $configuration;
/**
* @var midgardmvc_core_component_loader
*/
public $componentloader;
/**
* @var midgardmvc_core_services dispatcher
*/
public $dispatcher;
/**
* @var midgardmvc_core_helpers_context
*/
public $context;
/**
* Access to installed FirePHP logger
*
* @var FirePHP
*/
public $firephp = null;
private $track_autoloaded_files = false;
private $autoloaded_files = array();
private static $instance = null;
public function __construct()
{
// Register autoloader so we get all Midgard MVC classes loaded automatically
spl_autoload_register(array($this, 'autoload'));
}
/**
* Load all basic services needed for Midgard MVC usage. This includes configuration, authorization and the component loader.
*/
public function load_base_services($dispatcher = 'midgard')
{
// Load the context helper and initialize first context
$this->context = new midgardmvc_core_helpers_context();
$this->configuration = new midgardmvc_core_services_configuration_yaml();
$this->configuration->load_component('midgardmvc_core');
// Load the request dispatcher
$dispatcher_implementation = "midgardmvc_core_services_dispatcher_{$dispatcher}";
$this->dispatcher = new $dispatcher_implementation();
if ( $this->configuration->development_mode
and !class_exists('MFS\AppServer\DaemonicHandler') // firephp is not appserver-compatible
and !class_exists('MFS_AppServer_DaemonicHandler') // + check for php52 version of class
)
{
// Load FirePHP logger
// TODO: separate setting
include('FirePHPCore/FirePHP.class.php');
if (class_exists('FirePHP'))
{
$this->firephp = FirePHP::getInstance(true);
}
}
}
/**
* Helper for service initialization. Usually called via getters
*
* @param string $service Name of service to load
*/
private function load_service($service)
{
if (isset($this->$service))
{
return;
}
$interface_file = MIDGARDMVC_ROOT . "/midgardmvc_core/services/{$service}.php";
if (!file_exists($interface_file))
{
throw new InvalidArgumentException("Service {$service} not installed");
}
$service_implementation = $this->configuration->get("services_{$service}");
if (!$service_implementation)
{
throw new Exception("No implementation defined for service {$service}");
}
$this->$service = new $service_implementation();
}
/**
* Logging interface
*
* @param string $prefix Prefix to file the log under
* @param string $message Message to be logged
* @param string $loglevel Logging level, may be one of debug, info, message and warning
*/
public function log($prefix, $message, $loglevel = 'debug')
{
if (!extension_loaded('midgard2'))
{
// Temporary non-Midgard logger until midgard_error is backported to Ragnaroek
static $logger = null;
if (!$logger)
{
try
{
$logger = new midgardmvc_core_helpers_log();
}
catch (Exception $e)
{
// Unable to instantiate logger
return;
}
}
static $log_levels = array
(
'debug' => 4,
'info' => 3,
'message' => 2,
'warn' => 1,
);
if ($log_levels[$loglevel] > $log_levels[$this->configuration->get('log_level')])
{
// Skip logging, too low level
return;
}
$logger->log("{$prefix}: {$message}");
return;
}
$firephp_loglevel = $loglevel;
// Handle mismatching loglevels
switch ($loglevel)
{
case 'debug':
case 'message':
$firephp_loglevel = 'log';
break;
case 'warn':
case 'warning':
$loglevel = 'warning';
$firephp_loglevel = 'warn';
break;
case 'error':
case 'critical':
$firephp_loglevel = 'error';
break;
}
if ( $this->firephp
&& !$this->dispatcher->headers_sent())
{
$this->firephp->$firephp_loglevel("{$prefix}: {$message}");
}
midgard_error::$loglevel("{$prefix}: {$message}");
}
/**
* Magic getter for service loading
*/
public function __get($key)
{
$this->load_service($key);
return $this->$key;
}
/**
* Automatically load missing class files
*
* @param string $class_name Name of a missing PHP class
*/
public function autoload($class_name)
{
static $components = array('midgardmvc_core');
if ( count($components) < 2
&& isset(self::$instance->componentloader))
{
$components = array_keys(self::$instance->componentloader->manifests);
}
foreach ($components as $component)
{
// Look which component the file is under
$component_length = strlen($component);
if (substr($class_name, 0, $component_length) != $component)
{
continue;
}
if ($class_name == $component)
{
// Load the interface class
self::$instance->componentloader->load($component);
}
$path_under_component = str_replace('_', '/', substr($class_name, $component_length));
$path = MIDGARDMVC_ROOT . "/{$component}{$path_under_component}.php";
if (!file_exists($path))
{
return;
}
if ($this->track_autoloaded_files)
{
$this->autoloaded_files[] = $path;
}
require($path);
}
}
/**
* Process the current request, loading the page's component and dispatching the request to it
*/
public function process()
{
// Load the head helper
$this->head = new midgardmvc_core_helpers_head();
// php doesn't have "finally" keyword. emulating it
try
{
$this->_process();
}
catch (Exception $e)
{
}
// this will be executed even if _process() had exception
$this->_after_process();
if (isset($e))
{
// ->serve() wouldn't be called — do cleanup here
$this->cleanup_after_request();
// rethrowing exception, if there is one
throw $e;
}
}
private function _process()
{
$this->context->create();
date_default_timezone_set($this->configuration->get('default_timezone'));
$this->dispatcher->get_midgard_connection()->set_loglevel($this->configuration->get('log_level'));
// Let dispatcher populate request with the page and other information used
$request = $this->dispatcher->get_request();
$request->populate_context();
if (isset($this->context->page->guid))
{
// Load per-folder configuration
$this->configuration->load_instance($this->context->component, $this->context->page);
}
$this->log('Midgard MVC', "Serving " . $request->get_method() . " {$this->context->uri} at " . gmdate('r'), 'info');
// Let injectors do their work
$this->componentloader = new midgardmvc_core_component_loader();
$this->componentloader->inject_process();
// Load the cache service and check for content cache
self::$instance->context->cache_enabled = false;
/*
if (self::$instance->context->cache_enabled)
{
$request->generate_identifier();
$this->cache->register_object($this->context->page);
$this->cache->content->check($this->context->cache_request_identifier);
}
*/
// Show the world this is Midgard
$this->head->add_meta
(
array
(
'name' => 'generator',
'content' => "Midgard/" . mgd_version() . " MidgardMVC/{$this->componentloader->manifests['midgardmvc_core']['version']} PHP/" . phpversion()
)
);
if ($this->configuration->enable_attachment_cache)
{
$classname = $this->configuration->attachment_handler;
$handler = new $classname();
$handler->connect_to_signals();
}
// Then initialize the component, so it also goes to template stack
$this->dispatcher->initialize($request);
try
{
$this->dispatcher->dispatch();
}
catch (midgardmvc_exception_unauthorized $exception)
{
// Pass the exception to authentication handler
$this->authentication->handle_exception($exception);
}
$this->dispatcher->header('Content-Type: ' . $this->context->mimetype);
}
private function _after_process()
{
// add any cleanup after process() here
}
/**
* Serve a request either through templating or the WebDAV server
*/
public function serve()
{
try
{
$this->_serve();
}
catch (Exception $e)
{
}
// this will be executed even if _serve() had exception
$this->_after_serve();
if (isset($e))
{
// rethrowing exception, if there is one
throw $e;
}
}
private function _serve()
{
// Handle HTTP request
if (self::$instance->context->webdav_request)
{
// Start the full WebDAV server instance
// FIXME: Figure out how to prevent this with Variants
$webdav_server = new midgardmvc_core_helpers_webdav();
$webdav_server->serve();
// This will exit
}
// Prepate the templates
$this->templating->template();
// Read contents from the output buffer and pass to Midgard MVC rendering
$this->templating->display();
$this->cache->autoload->store($this->context->uri, $this->autoloaded_files);
}
private function _after_serve()
{
// add any cleanup after serve() here
$this->cleanup_after_request();
}
private function cleanup_after_request()
{
// commit session
if ($this->dispatcher->session_is_started())
{
$this->dispatcher->session_commit();
}
// Clean up the context
$this->context->delete();
}
/**
* Access to the Midgard MVC instance
*/
public static function get_instance($dispatcher = null)
{
static $dispatcher_used = null;
if (is_null($dispatcher_used))
{
$dispatcher_used = $dispatcher;
}
if ( !is_null($dispatcher)
&& $dispatcher != $dispatcher_used)
{
throw new BadMethodCallException("Dispatcher may be provided only once (using {$dispatcher_used} while you requested {$dispatcher})");
}
if (is_null(self::$instance))
{
// Load instance
self::$instance = new midgardmvc_core();
if (is_null($dispatcher))
{
self::$instance->load_base_services();
}
else
{
self::$instance->load_base_services($dispatcher);
}
}
return self::$instance;
}
public function get_object_actions(midgardmvc_core_node &$object, $variant = null)
{
$actions = array();
if (!midgardmvc_core::get_instance()->authorization->can_do('midgard:update', $object))
{
// User is not allowed to edit so we have no actions available
return $actions;
}
// This is the general action available for a page: forms-based editing
$actions['update'] = array
(
'url' => midgardmvc_core::get_instance()->dispatcher->generate_url('page_update', array(), $object),
'method' => 'GET',
'label' => midgardmvc_core::get_instance()->i18n->get('update', 'midgardmvc_core'),
'icon' => 'midgardmvc_core/stock-icons/16x16/update.png',
);
$actions['delete'] = array
(
'url' => midgardmvc_core::get_instance()->dispatcher->generate_url('page_delete', array(), $object),
'method' => 'GET',
'label' => midgardmvc_core::get_instance()->i18n->get('delete', 'midgardmvc_core'),
'icon' => 'midgardmvc_core/stock-icons/16x16/delete.png',
);
return $actions;
}
public function get_administer_actions(midgardmvc_core_node $folder)
{
$actions = array();
$actions['logout'] = array
(
'url' => midgardmvc_core::get_instance()->dispatcher->generate_url('logout', array()),
'method' => 'GET',
'label' => midgardmvc_core::get_instance()->i18n->get('logout', 'midgardmvc_core'),
'icon' => 'midgardmvc_core/stock-icons/16x16/exit.png',
);
return $actions;
}
public function get_create_actions(midgardmvc_core_node $folder)
{
$actions = array();
if (!midgardmvc_core::get_instance()->authorization->can_do('midgard:create', $folder))
{
// User is not allowed to create subfolders so we have no actions available
return $actions;
}
$actions['page_create'] = array
(
'url' => midgardmvc_core::get_instance()->dispatcher->generate_url('page_create', array()),
'method' => 'GET',
'label' => midgardmvc_core::get_instance()->i18n->get('create folder', 'midgardmvc_core'),
'icon' => 'midgardmvc_core/stock-icons/16x16/folder.png',
);
return $actions;
}
}
?>