-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobipro.php
575 lines (547 loc) · 16.5 KB
/
sobipro.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
<?php
/**
* @version: $Id: sobipro.php 4387 2015-02-19 12:24:35Z Radek Suski $
* @package: SobiPro Library
* @author
* Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH
* Email: sobi[at]sigsiu.net
* Url: http://www.Sigsiu.NET
* @copyright Copyright (C) 2006 - 2015 Sigsiu.NET GmbH (http://www.sigsiu.net). All rights reserved.
* @license GNU/LGPL Version 3
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3.
* See http://www.gnu.org/licenses/lgpl.html and http://sobipro.sigsiu.net/licenses.
* 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 General Public License for more details.
* $Date: 2015-02-19 13:24:35 +0100 (Thu, 19 Feb 2015) $
* $Revision: 4387 $
* $Author: Radek Suski $
* $HeadURL: file:///opt/svn/SobiPro/Component/branches/SobiPro-1.1/Site/lib/ctrl/sobipro.php $
*/
defined( 'SOBIPRO' ) || exit( 'Restricted access' );
/**
* @author Radek Suski
* @version 1.0
* @created 10-Jan-2009 5:35:35 PM
*/
final class SobiProCtrl
{
/**
* @var SPMainFrame
*/
private $_mainframe = null;
/**
* @var SPConfig
*/
private $_config = null;
/**
* @var int
*/
private $_mem = 0;
/**
* @var int
*/
private $_time = 0;
/**
* @var int
*/
private $_section = 0;
/**
* @var string
*/
private $_task = null;
/**
* @var int
*/
private $_sid = 0;
/**
* @var SPUser
*/
private $_user = null;
/**
* @var SPController - can be also array of
*/
private $_ctrl = null;
/**
* @var mixed
*/
private $_model = null;
/**
* @var int
*/
private $_err = 0;
/**
* @var int
*/
private $_deb = 0;
/**
* @var array
*/
private $_cache = array();
/**
* @param string $task
* @return \SobiProCtrl
*/
function __construct( $task )
{
$this->_mem = memory_get_usage();
$this->_time = microtime( true );
SPLoader::loadClass( 'base.exception' );
set_error_handler( 'SPExceptionHandler' );
$this->_err = ini_set( 'display_errors', 'on' );
$this->_task = $task;
/* load all needed classes */
SPLoader::loadClass( 'base.const' );
SPLoader::loadClass( 'base.factory' );
SPLoader::loadClass( 'base.object' );
SPLoader::loadClass( 'base.filter' );
SPLoader::loadClass( 'base.request' );
SPLoader::loadClass( 'sobi' );
SPLoader::loadClass( 'base.config' );
SPLoader::loadClass( 'cms.base.lang' );
/* get sid if any */
$this->_sid = SPRequest::sid();
/* determine section */
$access = $this->getSection();
/* initialise mainframe interface to CMS */
$this->_mainframe = SPFactory::mainframe();
/* initialise config */
$this->createConfig();
ini_set( 'display_errors', Sobi::Cfg( 'debug.display_errors', false ) );
$this->_deb = error_reporting( Sobi::Cfg( 'debug.level', 0 ) );
/* trigger plugin */
Sobi::Trigger( 'Start' );
/* initialise translator and load language files */
SPLang::setLang( Sobi::Lang( false ) );
try {
SPLang::registerDomain( 'site' );
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'Cannot register language domain: %s.', $x->getMessage() ), SPC::WARNING, 0, __LINE__, __FILE__ );
}
if ( !( $access ) ) {
if ( Sobi::Cfg( 'redirects.section_enabled', false ) ) {
$redirect = Sobi::Cfg( 'redirects.section_url', null );
$msg = Sobi::Cfg( 'redirects.section_msg', SPLang::e( 'UNAUTHORIZED_ACCESS', SPRequest::task() ) );
$msgtype = Sobi::Cfg( 'redirects.section_msgtype', 'message' );
Sobi::Redirect( Sobi::Url( $redirect ), Sobi::Txt( $msg ), $msgtype, true );
}
else {
SPFactory::mainframe()->runAway( 'You have no permission to access this site', 403, null, true );
}
}
/* load css and js files */
SPFactory::header()->initBase();
$sectionName = SPLang::translateObject( $this->_section, 'name', 'section' );
if( $this->_section ) {
SPFactory::registry()->set( 'current_section_name', SPLang::clean( $sectionName[ $this->_section ][ 'value' ] ) );
}
$start = array( $this->_mem, $this->_time );
SPFactory::registry()->set( 'start', $start );
/* check if it wasn't plugin custom task */
if ( !( Sobi::Trigger( 'custom', 'task', array( &$this, SPRequest::task() ) ) ) ) {
/* if not, start to route */
try {
$this->route();
} catch ( SPException $x ) {
if ( defined( 'SOBI_TESTS' ) ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'Cannot route: %s.', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
else {
SPFactory::mainframe()->setRedirect( Sobi::Reg( 'live_site' ), SPLang::e( 'PAGE_NOT_FOUND' ), SPC::ERROR_MSG, true );
}
}
}
return true;
}
/**
* initialise config object
* @return void
*/
private function createConfig()
{
$this->_config = & SPFactory::config();
/* load basic configuration settings */
$this->_config->addIniFile( 'etc.config', true );
$this->_config->addTable( 'spdb_config', $this->_section );
/* initialise interface config setting */
$this->_mainframe->getBasicCfg();
/* initialise config */
$this->_config->init();
}
/**
* get the right section
* @return void
*/
private function getSection()
{
$sid = SPRequest::int( 'pid' ) ? SPRequest::int( 'pid' ) : $this->_sid;
$db =& SPFactory::db();
$section = null;
if ( $sid ) {
$section = & SPFactory::object( $sid );
if ( $section && $section->oType == 'section' ) {
$this->_section = $section->id;
$state = $section->state;
}
else {
$path = array();
$id = $sid;
while ( $id > 0 ) {
try {
$id = $db->select( 'pid', 'spdb_relations', array( 'id' => $id ) )
->loadResult();
if ( $id ) {
$path[ ] = ( int )$id;
}
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
}
if ( count( $path ) ) {
$path = array_reverse( $path );
$this->_section = $path[ 0 ];
$section = & SPFactory::object( $this->_section );
$state = $section->state;
}
else {
Sobi::Error( 'CoreCtrl', SPLang::e( 'PAGE_NOT_FOUND' ), SPC::NOTICE, 404 );
exit;
}
}
}
else {
$this->_section = '0';
}
/* set current section in the registry */
SPFactory::registry()->set( 'current_section', $this->_section );
if ( !( isset( $state ) ) ) {
$section = SPFactory::object( $sid );
if ( isset( $section->state ) ) {
$state = $section->state;
}
else {
$state = false;
}
}
/* if this section isn't published */
if ( !$state ) {
if ( !( Sobi::Can( 'section.access.any' ) ) ) {
return false;
}
}
if ( $section && $section->id == SPRequest::sid() ) {
$this->_model = $section;
}
elseif ( SPRequest::sid() ) {
$this->_model = & SPFactory::object( SPRequest::sid() );
}
return true;
}
/**
* Try to find out what we have to do
* - If we have a task - parse task
* - If we don't have a task, but sid, we are going via default object task
* - Otherwise it could be only the frontpage
* @throws SPException
* @return void
*/
private function route()
{
$cache = true;
if ( Sobi::Cfg( 'cache.xml_enabled' ) ) {
if ( ( $this->_model instanceof stdClass ) && !( ( $this->_model instanceof stdClass ) && $this->_model->owner == Sobi::My( 'id' ) ) ) {
if ( in_array( $this->_model->owner, array( 'entry' ) ) ) {
$cache = false;
}
}
}
if ( $cache && Sobi::Cfg( 'cache.xml_enabled' ) ) {
$this->_cache = SPFactory::cache()->view();
}
if ( !( $this->_cache ) ) {
/* if we have a task */
if ( $this->_task && $this->_task != 'panel' ) {
if ( !( $this->routeTask() ) ) {
throw new SPException( SPLang::e( 'Cannot interpret task "%s"', $this->_task ) );
}
}
/* if there is no task - execute default task for object */
elseif ( $this->_sid ) {
if ( !( $this->routeObj() ) ) {
throw new SPException( SPLang::e( 'Cannot route object with id "%d"', $this->_sid ) );
}
}
/* otherwise show the frontpage */
else {
$this->frontpage();
}
}
else {
try {
$task = $this->_task;
if ( !( $task ) && $this->_sid ) {
$ctrl = SPFactory::Controller( $this->_model->oType );
$this->setController( $ctrl );
$this->_model = SPFactory::object( $this->_sid );
$model = SPLoader::loadModel( $this->_model->oType, false, false );
if ( $model ) {
$this->_ctrl->setModel( $model );
if ( ( $this->_model instanceof stdClass ) ) {
$this->_ctrl->extend( $this->_model, true );
}
}
}
if ( strstr( $task, '.' ) ) {
$task = explode( '.', $task );
$obj = trim( array_shift( $task ) );
if ( $obj == 'list' || $obj == 'ls' ) {
$obj = 'listing';
}
$task = trim( implode( '.', $task ) );
$ctrl = SPFactory::Controller( $obj );
$this->setController( $ctrl );
$model = SPLoader::loadModel( $obj, false, false );
if ( $model ) {
$this->_ctrl->setModel( $model );
if ( ( $this->_model instanceof stdClass ) ) {
$this->_ctrl->extend( $this->_model, true );
}
}
else {
$this->_ctrl->setModel( SPFactory::Section( $this->_section ) );
if ( ( $this->_model instanceof stdClass ) ) {
$this->_ctrl->extend( $this->_model, true );
}
}
}
elseif ( $task ) {
/** Special controllers not inherited from object and without model */
$ctrl = SPFactory::Controller( $task );
$this->setController( $ctrl );
$this->_ctrl->setModel( SPFactory::Section( $this->_section ) );
if ( ( $this->_model instanceof stdClass ) ) {
$this->_ctrl->extend( $this->_model, true );
}
}
$this->_ctrl->setTask( $task );
$this->_ctrl->visible();
} catch ( SPException $x ) {
Sobi::Error( 'CachedView', $x->getMessage() );
$this->_cache = null;
$this->route();
}
}
}
/**
* Route by task
* @return bool
*/
private function routeTask()
{
$r = true;
if ( strstr( $this->_task, '.' ) ) {
/* task consist of the real task and the object type */
$task = explode( '.', $this->_task );
$obj = trim( array_shift( $task ) );
$task = trim( implode( '.', $task ) );
if ( $obj == 'list' || $obj == 'ls' ) {
$obj = 'listing';
}
/* load the controller class definition and get the class name */
$ctrl = SPLoader::loadController( $obj );
/* route task for multiple objects - e.g removing or publishing elements from a list */
/* and there was some of multiple sids */
if ( count( SPRequest::arr( 'sid' ) ) || count( SPRequest::arr( 'c_sid' ) ) || count( SPRequest::arr( 'e_sid' ) ) ) {
$sid = array_key_exists( 'sid', $_REQUEST ) ? 'sid' : ( array_key_exists( 'c_sid', $_REQUEST ) ? 'c_sid' : 'e_sid' );
if ( count( SPRequest::arr( $sid ) ) ) {
/* @var SPdb $db */
$db =& SPFactory::db();
try {
$db->select( '*', 'spdb_object', array( 'id' => SPRequest::arr( $sid ) ) );
$objects = $db->loadObjectList();
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
$r = false;
}
if ( count( $objects ) ) {
$this->_ctrl = array();
foreach ( $objects as $object ) {
$this->_ctrl[ ] = $this->extendObj( $object, $obj, $ctrl, $task );
}
}
}
else {
Sobi::Error( 'CoreCtrl', SPLang::e( 'IDENTIFIER_EXPECTED' ), SPC::ERROR, 500, __LINE__, __FILE__ );
$r = false;
}
}
else {
/* set controller and model */
try {
$ctrl = new $ctrl();
$this->setController( $ctrl );
if ( $ctrl instanceof SPController ) {
$model = SPLoader::loadModel( $obj, false, false );
if ( $model ) {
$this->_ctrl->setModel( $model );
}
}
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
if ( $this->_sid ) {
$this->_model = & SPFactory::object( $this->_sid );
}
/* if the basic object we got from the #getSection method is the same one ... */
if ( ( $this->_model instanceof stdClass ) && ( $this->_model->oType == $obj ) ) {
/*... extend the empty model of these data we've already got */
$this->_ctrl->extend( $this->_model );
}
/* ... and so on... */
$this->_ctrl->setTask( $task );
}
}
else {
/** Special controllers not inherited from object and without model */
$task = $this->_task;
try {
$ctrl = SPLoader::loadController( $task );
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'PAGE_NOT_FOUND' ), SPC::NOTICE, 404 );
}
try {
$this->setController( new $ctrl() );
$this->_ctrl->setTask( null );
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'Cannot set controller. %s.', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
}
return $r;
}
/**
* @return bool
*/
private function routeObj()
{
try {
$ctrl = SPFactory::Controller( $this->_model->oType );
if ( $ctrl instanceof SPController ) {
$this->setController( $ctrl );
if ( $this->_model->id == SPRequest::sid() ) {
// just to use the pre-fetched entry
if ( $this->_model->oType == 'entry' ) {
$e = SPFactory::Entry( $this->_model->id );
// object has been stored anyway in the SPFactory::object method
// and also already initialized
// therefore it will be now rewritten and because the init flag is set to true
// the name is getting lost
// $e->extend( $this->_model );
$this->_ctrl->setModel( $e );
}
else {
$this->_ctrl->setModel( SPLoader::loadModel( $this->_model->oType ) );
$this->_ctrl->extend( $this->_model );
}
}
else {
$this->_ctrl->extend( SPFactory::object( SPRequest::sid() ) );
}
$this->_ctrl->setTask( $this->_task );
}
} catch ( SPException $x ) {
if ( defined( 'SOBI_TESTS' ) ) {
Sobi::Error( 'CoreCtrl', SPLang::e( 'Cannot set controller. %s.', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
else {
SPFactory::mainframe()->setRedirect( Sobi::Reg( 'live_site' ), SPLang::e( 'PAGE_NOT_FOUND' ), SPC::ERROR_MSG, true );
}
}
return true;
}
/**
* @param stdClass $obj
* @param string $ctrlClass
* @param string $objType
* @param string $task
* @return SPControl
*/
private function & extendObj( $obj, $objType, $ctrlClass, $task = null )
{
if ( $objType == $obj->oType ) {
if ( $ctrlClass ) {
/* create controller */
$ctrl = new $ctrlClass();
/* set model */
$ctrl->setModel( SPLoader::loadModel( $objType ) );
/* extend model of basic data */
$ctrl->extend( $obj );
/* set task */
if ( strlen( $task ) ) {
$ctrl->setTask( $task );
}
}
else {
Sobi::Error( 'CoreCtrl', SPLang::e( 'SUCH_TASK_NOT_FOUND', SPRequest::task() ), SPC::NOTICE, 404, __LINE__, __FILE__ );
}
}
return $ctrl;
}
/**
* @return void
*/
private function frontpage()
{
$ctrl = SPLoader::loadController( 'front' );
$this->setController( new $ctrl() );
$this->_ctrl->setTask( SPRequest::task() );
}
/**
* Executes the controller task
* @return void
*/
public function execute()
{
if ( !( $this->_cache ) ) {
try {
if ( is_array( $this->_ctrl ) ) {
foreach ( $this->_ctrl as &$c ) {
$c->execute();
}
}
else {
if ( $this->_ctrl instanceof SPControl ) {
$this->_ctrl->execute();
}
else {
Sobi::Error( 'CoreCtrl', SPLang::e( 'No controller to execute' ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
}
} catch ( SPException $x ) {
Sobi::Error( 'CoreCtrl', SPLang::e( '%s', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ );
}
}
else {
/** @var $view SPFrontView */
$view = SPFactory::View( 'cache' );
$view->cachedView( $this->_cache[ 'xml' ], $this->_cache[ 'template' ], $this->_cache[ 'cid' ], $this->_cache[ 'config' ] );
$view->display();
}
/* send header data etc ...*/
if ( SPRequest::cmd( 'format' ) == 'raw' && SPRequest::bool( 'xmlc' ) ) {
SPFactory::cache()->storeView( array() );
}
SPFactory::mainframe()->endOut();
Sobi::Trigger( 'End' );
/* redirect if any redirect has been set */
SPFactory::mainframe()->redirect();
ini_set( 'display_errors', $this->_err );
error_reporting( $this->_deb );
restore_error_handler();
}
/**
* @param $ctrl
* @return void
*/
public function setController( &$ctrl )
{
$this->_ctrl = & $ctrl;
}
}