/
ezmodule.php
2147 lines (1990 loc) · 70.6 KB
/
ezmodule.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File containing the eZModule class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package lib
*/
/**
* The eZModule class is used to instanciate and use modules & views.
*
* Loading the "content" module, and running the "history" view
* <code>
* <?php
* $contentModule = eZModule::findModule( 'content' );
* $result = $contentModule->run( 'history', array( 1 ) );
* ?>
* </code>
*
* Running the CopyVersion action of the content/history view:
* <code>
* <?php
* $contentModule = eZModule::findModule( 'content' );
* $contentModule->setCurrentView( 'history' );
* $contentModule->setCurrentAction( 'CopyVersion' );
* // we will copy version 3
* $contentModule->setActionParameter( 'VersionID', 3 );
* $contentModule->run( 'history', array( 20 ) );
* ?>
* </code>
*/
class eZModule
{
/**
* Module execution status: IDLE
* @var int
*/
const STATUS_IDLE = 0;
/**
* Module execution status: OK
* @var int
*/
const STATUS_OK = 1;
/**
* Module execution status: FAILED
* @var int
*/
const STATUS_FAILED = 2;
/**
* Module execution status: REDIRECT
* @var int
*/
const STATUS_REDIRECT = 3;
/**
* Module execution status: RERUN
* @var int
*/
const STATUS_RERUN = 4;
/**
* Hooks execution status: OK
* @var int
*/
const HOOK_STATUS_OK = 0;
/**
* Hooks execution status: CANCEL_RUN
* @var int
*/
const HOOK_STATUS_CANCEL_RUN = 1;
/**
* Hooks execution status: FAILED
* @var int
*/
const HOOK_STATUS_FAILED = 2;
/**
* List of defined views for the module, as defined in the $ViewList variable
* in module.php
* @var array
* @private
*/
public $Functions;
/**
* Array of module information.
* Available keys:
* - string name: the module name
* - array function: the known function (view) list
* - boolean variable_params
* - string ui_component_match
* @var array
* @private
*/
public $Module;
/**
* The module name
* @var string
*/
public $Name;
/**
* The module's path, without the module name and module.php
* Examples: kernel, extension/mymoduleextension/modules
* @var string
*/
public $Path;
/**
* The last execution's exit status.
* Accepts one of the STATUS_ constants.
* @see STATUS_OK, STATUS_FAILED, STATUS_REDIRECT, STATUS_RERUN
* @see setExitStatus(), exitStatus()
* @var int
*/
public $ExitStatus;
/**
* The last execution's error code, if an error occured
* @see errorCode(), setErrorCode()
* @var int
*/
public $ErrorCode;
/**
* The redirection URI that will be used to redirect after execution has ended.
* @see redirectURI(), setRedirectURI(), redirectTo(), STATUS_REDIRECT
* @var string
*/
public $RedirectURI;
/**
* The redirection HTTP status
* @see setRedirectStatus(), redirectStatus(), STATUS_REDIRECT
* @var string
*/
public $RedirectStatus;
/**
* The last execution's result title
* @var string
* @see title(), setTitle()
*/
public $Title;
/**
* The hook list for this module
* @see addHook(), runHooks()
* @var array
*/
public $HookList;
/**
* Current action per view, as an associative array.
* Each key is a view name, and the value the current action
* @var array
* @see viewAction(), setCurrentAction(), isCurrentAction()
*/
public $ViewActions;
/**
* The last execution view result, as an array
* Common keys: content, title, url...
* @var array
*/
public $ViewResult;
/**
* Ordered view parameters values
* @var array
* @private
*/
public $ViewParameters;
/**
* Original parameters, before they're mapped to view/unordered/user
* @var array
* @private
*/
public $OriginalParameters;
/**
* View parameters values
* @var array
* @private
*/
public $OriginalViewParameters;
/**
* Named parameters, indexed by name
* @var array
* @private
*/
public $NamedParameters;
/**
* Unordered parameters
* @var array
* @private
*/
public $OriginalUnorderedParameters;
/**
* User parameters (customized ones, as the content/view "view" parameters)
* @var array
* @private
*/
public $UserParameters;
/**
* The current UI context
* By default 'navigation' but can be changed depending on module or PHP code
* @var string
* @private
*/
public $UIContext;
/**
* The current UI context
* By default the current module but can be changed depending on module or PHP code
* @var string
* @private
*/
public $UIComponent;
/**
* Controls at which level UI component matching is done:
* either 'module' which uses module name or 'view' which uses view name
* @var string
* @private
*/
public $UIComponentMatch;
public static $useExceptions;
/**
* Constructor. Initializes the module.
*
* @param string $path
* Relative path to the module, without the module name
* @param string $file
* Relative path to the module definition file module.php
* @param string $moduleName
* @param boolean $checkFileExistence
* Always set to false in the current code base, since the check is
* usually performed before the constructor is called
*/
public function __construct( $path, $file, $moduleName, $checkFileExistence = true )
{
$this->initialize( $path, $file, $moduleName, $checkFileExistence);
}
/**
* Initializes the module object.
*
* @param string $path
* Directory where the module is declared, without the modulename
* component
* @param string $file
* Full (relative) path to the module.php file describing the module
* @param string $moduleName
* The module name (content, user...)
* @param bool $checkFileExistence
* Wether or not $file's existence should be checked
* @return void
*/
function initialize( $path, $file, $moduleName, $checkFileExistence = true )
{
if ( $checkFileExistence === false || file_exists( $file ) )
{
unset( $FunctionList );
unset( $Module );
unset( $ViewList );
include( $file );
$this->Functions = $ViewList;
if ( isset( $FunctionList ) and
is_array( $FunctionList ) and
count( $FunctionList ) > 0 )
{
ksort( $FunctionList, SORT_STRING );
$this->FunctionList = $FunctionList;
}
else
{
$this->FunctionList = array();
}
if ( empty( $Module ) )
{
$Module = array( "name" => "null",
"variable_params" => false,
"function" => array() );
}
$this->Module = $Module;
$this->Name = $moduleName;
$this->Path = $path;
$this->Title = "";
$this->UIContext = 'navigation';
$this->UIComponent = $moduleName;
$uiComponentMatch = 'module';
if ( isset( $this->Module['ui_component_match'] ) )
{
$uiComponentMatch = $this->Module['ui_component_match'];
}
$this->UIComponentMatch = $uiComponentMatch;
foreach( $this->Functions as $key => $dummy)
{
$this->Functions[$key]["uri"] = "/$moduleName/$key";
}
}
else
{
$this->Functions = array();
$this->Module = array( "name" => "null",
"variable_params" => false,
"function" => array() );
$this->Name = $moduleName;
$this->Path = $path;
$this->Title = "";
$this->UIContext = 'navigation';
$this->UIComponent = $moduleName;
$this->UIComponentMatch = 'module';
}
$this->HookList = array();
$this->ExitStatus = self::STATUS_IDLE;
$this->ErrorCode = 0;
$this->ViewActions = array();
$this->OriginalParameters = null;
$this->UserParameters = array();
// Load in navigation part overrides
$ini = eZINI::instance( 'module.ini' );
$this->NavigationParts = $ini->variable( 'ModuleOverrides', 'NavigationPart' );
}
/**
* Returns the module's URI (/content, /user...)
* @return string The module's URI
*
* @see functionURI()
*/
function uri()
{
return "/" . $this->Name;
}
/**
* Returns the URI to a module's function
*
* @param string $function The function to return the URI for
* @return string|null
* - the function's URI (content/edit, user/login, etc)
* - if $function is empty or the module is a singleView one,
* the module's uri (content/, user/...)
* - null if the function's not found
*
* @see uri()
*/
function functionURI( $function )
{
if ( $this->singleFunction() or
$function == '' )
return $this->uri();
if ( isset( $this->Functions[$function] ) )
return $this->Functions[$function]["uri"];
else
return null;
}
/**
* Returns the title of the last ran view. Normally set by the view itself,
* and displayed as the page's title
*
* @return string
*
* @see setTitle()
*/
function title()
{
return $this->Title;
}
/**
* Sets the current view for the module to \a $title.
*
* @param string $title The title to be set
*
* @see title()
*/
function setTitle( $title )
{
$this->Title = $title;
}
/**
* Sets the name of the currently running module. The URIs will be updated
* accordingly
*
* @param string $name The name to be set
*
* @return void
*
* @see uri(), functionURI()
*/
function setCurrentName( $name )
{
$this->Name = $name;
foreach( $this->Functions as $key => $dummy )
{
$this->Functions[$key]["uri"] = "/$name/$key";
}
}
/**
* Sets the currently executed view
*
* @param string $name The view name
*
* @return void
*
* @see currentView()
*/
function setCurrentView( $name )
{
$GLOBALS['eZModuleCurrentView'] = $name;
}
/**
* Checks if the module is a single view one
* @return bool
*/
function singleFunction()
{
return count( $this->Functions ) == 0;
}
/**
* Returns the UI context
* @return string The current UI context. Default: 'navigation'
*
* @see setUIContextName()
*/
function uiContextName()
{
return $this->UIContext;
}
/**
* Returns the UI component, by default the module name
*
* @return string The current UI component
*
* @see setUIComponentName()
*/
function uiComponentName()
{
return $this->UIComponent;
}
/**
* Sets the current context
*
* @param string $context The new context string
*
* @see uiContextName()
*
* @return void
*/
function setUIContextName( $context )
{
$this->UIContext = $context;
}
/**
* Sets the current component name
*
* @param string $component The new component name
*
* @see uiComponentName()
*
* @return void
*/
function setUIComponentName( $component )
{
$this->UIComponent = $component;
}
/**
* Returns the last exit status after a view has been executed
*
* @return int one of STATUS_* constants
*
* @see setExitStatus()
*/
function exitStatus()
{
return $this->ExitStatus;
}
/**
* Sets the exit status. This status will be used to inform the user,
* perform a redirection...
*
* @param int $stat One of the eZModule::STATUS_* constants
*
* @see exitStatus()
* @return void
*/
function setExitStatus( $stat )
{
$this->ExitStatus = $stat;
}
/**
* Returns the last error code. An error should only be returned if the
* module's status is eZModule::STATUS_FAILED
*
* @return int The error code, or 0 if no error occured
*
* @see setErrorCode(), exitStatus(), setExitStatus()
*/
function errorCode()
{
return $this->ErrorCode;
}
/**
* Sets the current error code.
* @note For the error code to be used, the module's status needs to be set
* to eZModule::STATUS_FAILED
* @see setExitStatus(), errorCode()
* @return void
*/
function setErrorCode( $errorCode )
{
$this->ErrorCode = $errorCode;
}
/**
* Returns the error module which will be ran if an error occurs
*
* @return array the error module name (keys: module, view)
*
* @see handleError()
*/
function errorModule()
{
if ( !isset( $GLOBALS['eZModuleGlobalErrorModule'] ) )
$GLOBALS['eZModuleGlobalErrorModule'] = array( 'module' => 'error',
'view' => 'view' );
return $GLOBALS['eZModuleGlobalErrorModule'];
}
/**
* Sets the module to be used to handle errors
*
* @param string $moduleName
* @param string $viewName
*
* @see handleError(), errorModule()
*/
function setErrorModule( $moduleName, $viewName )
{
$GLOBALS['eZModuleGlobalErrorModule'] = array( 'module' => $moduleName,
'view' => $viewName );
}
/**
* Runs the defined error module
* Sets the state of the module object to \c failed and sets the error code.
*
* @param mixed $errorCode
* @param mixed $errorType
* @param array $parameters
* @param mixed $userParameters
*
* @see setErrorModule(), errorModule()
*/
function handleError( $errorCode, $errorType = false, $parameters = array(), $userParameters = false )
{
if ( self::$useExceptions && $errorType === "kernel" )
{
switch ( $errorCode )
{
case eZError::KERNEL_MODULE_NOT_FOUND:
throw new ezpModuleNotFound( $parameters["module"] );
case eZError::KERNEL_MODULE_DISABLED:
if ( $parameters["check"]["view_checked"] )
throw new ezpModuleViewDisabled( $parameters["check"]['module'], $parameters["check"]['view'] );
throw new ezpModuleDisabled( $parameters["check"]['module'] );
case eZError::KERNEL_MODULE_VIEW_NOT_FOUND:
throw new ezpModuleViewNotFound( $parameters["check"]['module'], $parameters["check"]['view'] );
case eZError::KERNEL_ACCESS_DENIED:
throw new ezpAccessDenied;
//case eZError::KERNEL_MOVED:
// @todo ?
case eZError::KERNEL_NOT_AVAILABLE:
case eZError::KERNEL_NOT_FOUND:
throw new ezpContentNotFoundException( "" );
case eZError::KERNEL_LANGUAGE_NOT_FOUND:
throw new ezpLanguageNotFound;
}
}
if ( !$errorType )
{
eZDebug::writeWarning( "No error type specified for error code $errorCode, assuming kernel.\nA specific error type should be supplied, please check your code.", __METHOD__ );
$errorType = 'kernel';
}
$errorModule = $this->errorModule();
$module = self::findModule( $errorModule['module'], $this );
if ( $module === null )
{
return false;
}
$result = $module->run( $errorModule['view'], array( $errorType, $errorCode, $parameters, $userParameters ) );
// The error module may want to redirect to another URL, see error.ini
if ( $this->exitStatus() != self::STATUS_REDIRECT and
$this->exitStatus() != self::STATUS_RERUN )
{
$this->setExitStatus( self::STATUS_FAILED );
$this->setErrorCode( $errorCode );
}
return $result;
}
/**
* Redirects to another module / view
*
* @note Use redirectModule() If the target module object is already available
* @note Use redirectToView() if you want to redirect to another view in the same module
*
* @see redirectionURI(), redirectModule(), redirectToView()
*
* @param string $moduleName Target module name
* @param string $viewName Target view name
* @param array $parameters View parameters array
* @param array $unorderedParameters Unordered parameters array
* @param array $userParameters User parameters array
* @param string $anchor Anchor to use in the redirection (prepended to the URL)
*
* @return bool true if the redirection was performed, false if the module wasn't found
*/
function redirect( $moduleName, $viewName, $parameters = array(),
$unorderedParameters = null, $userParameters = false,
$anchor = false )
{
$module = self::exists( $moduleName );
if ( $module )
{
return $this->redirectModule( $module, $viewName, $parameters,
$unorderedParameters, $userParameters, $anchor );
}
else
{
eZDebug::writeError( 'Undefined module: ' . $moduleName, __METHOD__ );
}
return false;
}
/**
* Redirects to another view in the current module
*
* @see redirectionURI(), redirectModule(), redirect()
*
* @param string $viewName Target view name
* @param array $parameters View parameters
* @param array $unorderedParameters Unordered view parameters
* @param array $userParameters User parameters
* @param string $anchor Redirection URI anchor
*
* @return boolean true if successful, false if the view isn't found
*/
function redirectToView( $viewName = '', $parameters = array(),
$unorderedParameters = null, $userParameters = false,
$anchor = false )
{
return $this->redirectModule( $this, $viewName, $parameters,
$unorderedParameters, $userParameters, $anchor );
}
/**
* Redirects to another module / view.
*
* The difference with redirect is that the $module parameter is an object
* instead of a string
*
* @param eZModule $moduleName Target module name
* @param string $viewName Target view name
* @param array $parameters View parameters array
* @param array $unorderedParameters Unordered parameters array
* @param array $userParameters User parameters array
* @param string $anchor Redirection URI anchor
*
* @return boolean true. Just true.
*
* @todo Deprecate; have redirect() check if $module is an eZModule or a string
*/
function redirectModule( $module, $viewName, $parameters = array(),
$unorderedParameters = null, $userParameters = false,
$anchor = false )
{
$uri = $this->redirectionURIForModule( $module, $viewName, $parameters,
$unorderedParameters, $userParameters, $anchor );
$this->redirectTo( $uri );
return true;
}
/**
* Creates the redirection URI for a given module, view & parameters.
* Unlike redirectionURIForModule(), the $module parameter is the module name
*
* @param string $moduleName Redirection module name
* @param string $viewName Redirection view name
* @param array $parameters View parameters
* @param array $unorderedParameters Unordered parameters
* @param array $userParameters User parameters
* @param string $anchor Redirection URI anchor
*
* @return string|boolean The redirection URI, or false if the module isn't found
*
* @see redirect(), redirectionURIForModule(), redirectToView(), redirectModule()
*/
function redirectionURI( $moduleName, $viewName, $parameters = array(),
$unorderedParameters = null, $userParameters = false,
$anchor = false )
{
$module = self::exists( $moduleName );
if ( $module )
{
return $this->redirectionURIForModule( $module, $viewName, $parameters,
$unorderedParameters, $userParameters, $anchor );
}
else
eZDebug::writeError( 'Undefined module: ' . $moduleName, __METHOD__ );
return false;
}
/**
* Creates the redirection URI for the current module, view & parameters
*
* @return string The redirection URI
*
* @see redirectionURIForModule()
*/
function currentRedirectionURI()
{
$module = $this;
$viewName = self::currentView();
$parameters = $this->OriginalViewParameters;
$unorderedParameters = $this->OriginalUnorderedParameters;
$userParameters = $this->UserParameters;
return $this->redirectionURIForModule( $module, $viewName, $parameters,
$unorderedParameters, $userParameters );
}
/**
* Redirects to the current module and view, it will use currentRedirectionURI() to
* figure out the URL.
*
* @note By changing using setCurrentName() and setCurrentView() first it is
* possible to redirect to another module or view with the same
* parameters.
*
* @see currentRedirectionURI(), redirectTo()
*
* @return void
*/
function redirectCurrent()
{
$this->redirectTo( $this->currentRedirectionURI() );
}
/**
* Creates the redirection URI for a given module, view & parameters.
* Unlike redirectionURI(), the $module parameter is a module object
*
* @param string $moduleName Redirection module name
* @param string $viewName
* Redirection view name. If empty, the current view will be used
* @param array $parameters View parameters
* @param array $unorderedParameters Unordered parameters
* @param array $userParameters User parameters
* @param string $anchor Redirection URI anchor
*
* @return string|boolean The redirection URI, or false if the module isn't found
*
* @see redirect(), redirectionURIForModule(), redirectToView(), redirectModule()
*/
function redirectionURIForModule( $module, $viewName, $parameters = array(),
$unorderedParameters = null, $userParameters = false,
$anchor = false )
{
if ( $viewName == '' )
$viewName = self::currentView();
$uri = $module->functionURI( $viewName );
$uri .= '/';
$viewParameters = $module->parameters( $viewName );
$parameterIndex = 0;
$unorderedURI = '';
$hasUnorderedParameter = false;
if ( $unorderedParameters !== null )
{
$unorderedViewParameters = $module->unorderedParameters( $viewName );
if ( is_array( $unorderedViewParameters ) )
{
foreach ( $unorderedViewParameters as $unorderedViewParameterName => $unorderedViewParameterVariable )
{
if ( isset( $unorderedParameters[$unorderedViewParameterVariable] ) )
{
$unorderedURI .= $unorderedViewParameterName . '/' . $unorderedParameters[$unorderedViewParameterVariable] . '/';
$hasUnorderedParameter = true;
}
}
}
}
if( !isset( $viewParameters ) )
$viewParameters = array(); // prevent PHP warning below
foreach ( $viewParameters as $viewParameter )
{
if ( !isset( $parameters[$parameterIndex] ) )
{
// We don't show a warning anymore since some parameters can be optional
// In future versions we will need required and optional parameters
// for modules and give warnings for required ones.
// eZDebug::writeWarning( "Missing parameter(s) " . implode( ', ', array_slice( $viewParameters, $parameterIndex ) ) .
// " in view '$viewName'", 'eZModule::redirect' );
}
else
$uri .= $parameters[$parameterIndex] . '/';
++$parameterIndex;
}
if ( $hasUnorderedParameter )
{
$uri .= $unorderedURI;
}
if ( !empty( $userParameters ) && is_array( $userParameters ) )
{
// Remove any slash(es) at the end of the uri, because the user params are appended with a leading slash
$uri = preg_replace( "#(^.*)(/+)$#", "\$1", $uri );
foreach ( $userParameters as $name => $value )
{
$uri .= '/(' . $name . ')/' . $value;
}
}
// Remove multiple slashes at the end of the uri
$uri = preg_replace( "#(^.*)(//+)$#", "\$1", $uri );
if ( $anchor !== false )
$uri .= '#' . urlencode( $anchor );
return $uri;
}
/**
* Returns the defined parameter for a view.
*
* @param string $viewName
* The view to get parameters for. If not specified, the current view
* is used
*
* @return array The parameters definition
* @see unorderedParameters(), viewData(), currentView(), currentModule()
*/
function parameters( $viewName = '' )
{
if ( $viewName == '' )
$viewName = self::currentView();
$viewData = $this->viewData( $viewName );
if ( isset( $viewData['params'] ) )
{
return $viewData['params'];
}
return null;
}
/**
* Returns the unordered parameters definition.
*
* @param string $viewName
* The view to return parameters for. If npt specified, the current
* view is used
*
* @return the unordered parameter definition for the requested view
*
* @see parameters(), viewData(), currentView(), currentModule()
*/
function unorderedParameters( $viewName = '' )
{
if ( $viewName == '' )
$viewName = self::currentView();
$viewData = $this->viewData( $viewName );
if ( isset( $viewData['unordered_params'] ) )
{
return $viewData['unordered_params'];
}
return null;
}
/**
* Returns data for a view
*
* @param string $viewName
* The view to return data for. If omited, the current view is used
* @see parameters(), unorderedParameters(), currentView(), currentModule()
*
* @return array
*/
function viewData( $viewName = '' )
{
if ( $viewName == '' )
$viewName = self::currentView();
if ( $this->singleFunction() )
$viewData = $this->Module["function"];
else
$viewData = $this->Functions[$viewName];
return $viewData;
}
/**
* Sets the module to redirect at the end of the execution
*
* @param string $uri the URI to redirect to
*
* @see setRedirectURI(), setExitStatus()
*
* @return void
*/
function redirectTo( $uri )
{
$originalURI = $uri;
$uri = preg_replace( "#(^.*)(/+)$#", "\$1", $uri );
if ( strlen( $originalURI ) != 0 and
strlen( $uri ) == 0 )
$uri = '/';
$urlComponents = parse_url( $uri );
// eZSys::hostname() can contain port if present.
// So parsing it with parse_url() as well to only get host.
$currentHostname = eZSys::hostname();
$currentHostnameParsed = parse_url( $currentHostname, PHP_URL_HOST );
$currentHostname = $currentHostnameParsed ? $currentHostnameParsed : $currentHostname;
if ( isset( $urlComponents['host'] ) && $urlComponents['host'] !== $currentHostname )
{
$allowedHosts = $this->getAllowedRedirectHosts();
if ( !isset( $allowedHosts[$urlComponents['host']] ) )
{
// Non-authorized host, return only the URI (without host) + query string and fragment if present.
eZDebug::writeError( "Redirection requested on non-authorized host '{$urlComponents['host']}'" );
header( $_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden' );
echo "Redirection requested on non-authorized host";
eZDB::checkTransactionCounter();
eZExecution::cleanExit();
}
}
$this->RedirectURI = $uri;
$this->setExitStatus( self::STATUS_REDIRECT );
}
/**
* Returns the set of hosts that are allowed for absolute redirection
*
* @return array
*/
private function getAllowedRedirectHosts()
{
$ini = eZINI::instance();
$allowedHosts = array_fill_keys( $ini->variable( 'SiteSettings', 'AllowedRedirectHosts' ), true );
// Adding HostMatchMapItems from siteaccess match if present
if ( $ini->hasVariable( 'SiteAccessSettings', 'HostMatchMapItems' ) )
{
$hostMatchMapItems = $ini->variable( 'SiteAccessSettings', 'HostMatchMapItems' );
foreach ( $hostMatchMapItems as $item )
{
list( $host, $sa ) = explode( ';', $item );
$allowedHosts[$host] = true;
}