-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
854 lines (783 loc) · 19.6 KB
/
Module.php
File metadata and controls
854 lines (783 loc) · 19.6 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
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
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage Module
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
*
* Module class. This is one of the most used class in the framework, in combination with Dispatcher, it
* represents the Controller layer.
* Modules are created using the factory() method. A module can be used as a result of a user request
* or as a component (using the c() method in Mtpl)
* this class also provides caching mechanism and user credentials
*
*/
class Module extends Maman {
/**
*
* description
*
* @var unknown_type
* @access protected
*/
protected $view;
/**
*
* description
*
* @var unknown_type
* @access protected
*/
public $currentAction;
/**
*
* description
*
* @var unknown_type
* @access protected
*/
private $_lastOutput;
/**
*
* description
*
* @var unknown_type
* @access protected
*/
protected $_cachedData = null;
/**
*
* description
*
* @param $modulename
* @return unknown_type
*/
function __construct($modulename)
{
$this->_modulename=$modulename;
$this->_lastOutput = $this;
}
/**
*
* description
*
* @return unknown_type
*/
public function getView()
{
return $this->view;
}
/**
*
* description
*
* @param $modulename
* @param $path
* @param $params
* @return unknown_type
*/
public static function &factory($modulename,$path=null,$params = null)
{
if(empty($path)) {
$path=array('modules');
}
if(!is_array($path)) {
$path = array($path);
}
Log::info('Module::factory '.$modulename);
$plugmod = explode(':',$modulename);
$moduleOpt = PEAR::getStaticProperty('Module','global');
$optionsGroup = PEAR::getStaticProperty('Options','group');
if($plugmod[1]) {
Log::info('Calling plugin module '.$modulename);
PluginRegistry::initPlugin($plugmod[0]);
$path = array(APP_ROOT.'app/plugins/'.$plugmod[0].'/modules/','plugins/'.$plugmod[0].'/modules/');
$moduleOpt['template_dir'][] = 'plugins/'.$plugmod[0].'/templates/';
$moduleOpt['template_dir'][] = APP_ROOT.'app/plugins/'.$plugmod[0].'/templates/';
$modulename = $plugmod[1];
$className = $plugmod[0].'_Module_'.$modulename;
} else {
$className = 'Module_'.$modulename;
}
$i=false;
foreach($path as $aPath) {
if (@include_once $aPath.'/'.$modulename.'.php') {
$i=true;
break;
}
}
if (!$i)
{
Log::info('Module::factory '.$modulename.' not found in path '.implode(',',$path));
throw new Error404Exception("No $modulename module in path ".implode(',',$path));
}
Log::info('Module::factory '.$modulename.' OK');
$module = new $className($modulename);
$module->_path=$path;
Log::info('Generating options');
$options = $module->generateOptions($moduleOpt, $optionsGroup);
$module->setConfig($options);
$module->setParams($params);
$module->startView();
Log::info('Module::factory '.$className.' configured');
return $module;
}
/**
* Getters / Setters
*
**/
/**
*
* description
*
* @return unknown_type
*/
protected function generateOptions($opt, $group = NULL)
{
$opt = array('all'=>$opt);
$options = array(
'caching' =>(MODE=='developpement'?false:true),
'cacheDir' => $opt['all']['cacheDir'].'/config/',
'lifeTime' => 72000,//TODO make configurable...
'fileNameProtection'=>false,
'automaticSerialization'=>true
);
Log::info('preparing options');
$optcache = new Cache_Lite($options);
if (empty($group)) { $group = 'default'; }
if( !$moduleopt = $optcache->get(get_class($this), $group) ) {
Log::info('no cache for options, live generating');
foreach($this->_path as $path) {
if (@include $path.'/'.$this->_modulename.'.conf.php')
{
Log::info('loading module config file');
if(!is_array($config)) {$config=array();}
$moduleopt = MArray::array_merge_recursive_unique($opt, $config);
break;
} else {
$moduleopt=$opt;
}
Log::info('no cache for options, live generating');
}
if(MODE!='developpement')
{
$ret = $optcache->save($moduleopt);
Log::info('group = '.$group);
if ($ret) {Log::info('options saved');}
}
}
Log::info('options prepared');
return $moduleopt;
}
/**
*
* description
*
* @param $params
* @return unknown_type
*/
public function setParams($params)
{
if(!is_array($params)) {
$params = array();
}
$this->_params = $params;
}
/**
*
* returns the parameter that was passed to $this using setParam() or setParams()
* handy when you insert a component in a template file (@see Mtpl::c ) with parameters passed to it
*
* @param string name of the param
* @return mixed
*/
public function getParam($value)
{
return $this->_params[$value];
}
/**
*
* returns the parameter that was passed to $this using setParam() or setParams()
* or, if it's not set, returns the request value
* handy when you want to use an action both as a component and as a page (this is a common-use with UOJS)
*
* @param string name of the param
* @return mixed
*/
public function getParamOrRequest($value)
{
if(key_exists($value,$this->_params)) {
return $this->_params[$value];
}
return $this->getRequestParam($value);
}
/**
*
* description
*
* @param $var
* @param $val
* @return unknown_type
*/
public function setParam($var,$val)
{
$this->_params[$var] = $val;
}
/**
*
* description
*
* @return unknown_type
*/
public function startView()
{
$this->view = new Mtpl(M::getPaths('template'),$this);
if($vars = $this->getconfig('templateVars',$action)) {
$this->view->assignArray($vars);
}
}
/**
*
* description
*
* @return unknown_type
*/
public function getCurrentAction()
{
return $this->currentAction;
}
/**
* Returns the name of the current module
*/
public function getCurrentModule()
{
return $this->_modulename;
}
/**
*
* description
*
* @param $action
* @return unknown_type
*/
public function setCurrentAction($action)
{
$this->currentAction=$action;
}
/**
*
* description
*
* @param $val
* @param $action
* @return unknown_type
*/
public function hasLayout ($val = null,$action = 'all')
{
if($val === NULL) {
return $this->getConfig('layout' , $action);
} elseif($val == TRUE) {
Log::info('Default Layout for '.get_class($this));
$this->setConfigValue('layout', 'index' , $action);
} else {
Log::info('No layout for '.get_class($this));
$this->setConfigValue('layout', '__self' , $action);
}
}
// Determines if an action can be executed
// If so, (forces) executes the action or throws an exception
/**
*
* description
*
* @param $action
* @return unknown_type
*/
public function executeAction($action)
{
$meth = 'doExec'.$action;
Log::info('trying Module::'.$meth);
if(!method_exists($this,$meth))
{
throw new Error404Exception($action.' not implemented in Module : '.get_class($this));
}
$conf = $this->getConfig('security',$action);
$disabled = $this->getConfig('disabled',$action);
if($disabled) {
throw new Error404Exception($action.' is disabled in Module : '.get_class($this));
}
if (is_array($conf))
{
foreach($conf as $alevel) {
if(User::getInstance($alevel)->isLoggedIn()) {
$userok=true;
break;
}
}
if ($userok)
{
$this->setCurrentAction($action);
if($vars = $this->getconfig('templateVars',$action)) {
$this->view->assignArray($vars);
}
$this->forceExecute($action);
}
else
{
throw new SecurityException('Not enough credential to enter here');
}
}
else
{
Log::info('No security nor disabled config');
$this->setCurrentAction($action);
if($vars = $this->getconfig('templateVars',$action)) {
$this->view->assignArray($vars);
}
Log::info('vars assigned to view');
Log::info($action.' forcing execution');
$this->forceExecute($action);
Log::info($action.' was forced for execution');
}
}
// These two abstract methods are called right before and after the action is executed
/**
*
* description
*
* @param $action
* @return unknown_type
*/
public function preExecuteAction($action)
{
}
/**
*
* description
*
* @param $action
* @return unknown_type
*/
public function postExecuteAction($action)
{
}
// Dumbly executes an action and checks for caching
// Sets up environment if not cached
// @param string action name
// @return void
/**
*
* description
*
* @param $action
* @return unknown_type
*/
protected function forceExecute($action)
{
$meth = 'doExec'.$action;
$options = array(
'caching' =>$this->getConfig('caching', $action),
'cacheDir' => $this->getConfig('cacheDir',$action),
'lifeTime' => $this->getConfig('cacheTime', $action, 7200),
'fileNameProtection'=>false,
);
$this->cache = new Cache_Lite($options);
if($cache_id = $this->getCacheId($action)) {
Log::info($action.' is cachable with cacheId = '.$cache_id);
if($this->_cachedData = $this->cache->get($cache_id.'_'.($this->isAjaxRequest()?'ajax':''))) {
Log::info($action.' is retreived from cache with cacheId = '.$cache_id);
return;
}
}
Log::info($action.' is not in cache');
// Mreg::get('setup')->setUpEnv();
// !gestion template_dir rajouté dans le setup coupons
Mreg::get('setup')->setUpEnv();
$optionsEnv = & PEAR :: getStaticProperty('Module', 'global');
$optionsThis = $this->config;
$diff = array_diff( $optionsEnv['template_dir'], $optionsThis['all']['template_dir']);
if (is_array($diff))
{
foreach($diff as $k => $v)
{
$this->view->addPath($v,'after');
}
}
Log::info('env setup. launching preExecute');
$this->preExecuteAction($action);
Log::info('preExecute launched. Launching '.get_class($this).'::'.$meth);
call_user_func(array($this,$meth));
Log::info('doExec launched');
$this->postExecuteAction($action);
}
// Builds a cache identifier for the requested module/action.
// if context does not allow caching, returns false.
// this method should be overridden in modules if the developer wants more specific conditions and ID's
// @params string the name of the action.
// @returns string or false if no caching
// ==============================================================
/**
*
* description
*
* @param $action
* @return unknown_type
*/
public function getCacheId($action)
{
if(count($_POST)==0) {
$hash = $this->_modulename.'_'.$action.'_'.T::getLang().'_'.md5(print_r($_GET,true));
return $hash;
}
return false;
}
/**
*
* Forces the current action to use the $tpl template instead of its default one (templates/moduleName/actionName.php)
*
* @param $tpl string path to the desired template (module/action)
*/
public function setTemplate($tpl)
{
$this->_lastOutput->setConfigValue('template',$tpl);
}
/**
*
* Forces the current action to be decorated with the $tpl layout instead of the one defined by default (templates/index.php or the one defined in the modules/modulename.conf.php config file)
*
* @param $tpl string path to the desired layout
*/
public function setLayout($tpl)
{
$this->_lastOutput->setConfigValue('layout',$tpl);
}
/**
* manually set output, which bypassed the view layer.
*/
public function setOuput($output)
{
$this->_output_processed = true;
$this->_processed_output = $output;
}
/**
*
* description
*
* @param $template
* @param $layout
* @return unknown_type
*/
public function output($template=null,$layout=null)
{
if($this->_output_processed) {
return $this->_processed_output;
}
if($this->_cachedData) {
return $this->_cachedData;
}
$a=$this->_lastOutput->getCurrentAction();
if(is_null($template)) {
$template = $this->_lastOutput->getConfig('template',$a)?
$this->_lastOutput->getConfig('template',$a):
strtolower(preg_replace('`^(.*Module_)`i', '', get_class($this->_lastOutput))).'/'.$a;
Log::info('Setting template '.$template.' for module '.get_class($this->_lastOutput));
}
if($template=='__none') {
return;
}
if(!is_array($template)) {
$template = array($template);
}
if(is_null($layout)) {
$layout = $this->_lastOutput->getConfig('layout',$a)?
$this->_lastOutput->getConfig('layout',$a):
'index';
}
Log::info('Setting layout '.$layout.' for module '.get_class($this->_lastOutput));
if($this->isAjaxRequest()) {
$layout='__self';
$t2=$template;
foreach($t2 as $t) {
array_unshift($template,$t.'.bloc');
}
}
if($this->isComponent()) {
$layout='__self';
} else {
// Module is rendered as page,
// Let's add postFilters if some are provided in the postFilters configuration key
$postFilters = $this->getConfig('postfilters',$a);
if(is_array($postFilters)) {
foreach($postFilters as $filter) {
$this->_lastOutput->view->addPostFilter($filter);
}
}
}
if($layout=='__self'){
Log::info('Displaying selfsufficient for module '.get_class($this->_lastOutput));
$ret = $this->_lastOutput->view->fetch($template);
} else {
// Sinon c'est que le layout posséde une variable $__action qui est utilisé pour inclure le template de l'action
Log::info('Decorate module '.get_class($this->_lastOutput).' with '.$layout);
$this->_lastOutput->view->assign("__action", $template);
$ret = $this->_lastOutput->view->fetch($layout);
}
if(MODE!='developpement' && is_a($this->cache,'Cache_Lite')) $this->cache->save($ret);
$this->_output_processed = true;
$this->_processed_output = $ret;
return $ret;
}
public function isComponent($bool = null)
{
if(!is_null($bool)) {
$this->__isComponent = $bool;
}
return $this->__isComponent;
}
/**
* returns the original action name requested by the enduser
* @return string
*/
public function getRequestedAction()
{
return ($_REQUEST['action']);
}
/**
* returns the request value for the $val key
* @param string $val key name
* @return mixed
*/
public function getRequestParam($val)
{
return ($_REQUEST[$val]);
}
/**
* returns the get value for the $val key
* @param string $val key name
* @return mixed
*/
public function getGetParam($val)
{
return ($_GET[$val]);
}
/**
* returns the post value for the $val key
* @param string $val key name
* @return mixed
*/
public function getPostParam($val)
{
return ($_POST[$val]);
}
/**
* handles exceptions that are not 404 or 403
*/
public function handleException($e)
{
if(MODE == 'production') {
$this->executeAction('index');
} else {
$out = '<h3>Damn, an exception occured !</h3>';
$out .= '<p>'.$e->getMessage().'</p>';
$out .= '<pre>';
$out .= $this->stackTrace($e->getTrace());
$out .= '</pre>';
$this->setOuput($out);
}
}
/**
* wether the request has a value
* @param $val key name
* @return bool
*/
public function hasRequest($val)
{
return key_exists($val,$_REQUEST);
}
/**
* Proxy methods for the view
**/
/**
*
* description
*
* @param $var
* @param $val
* @return unknown_type
*/
public function assign($var, $val) {
$this->view->assign($var,$val);
}
/**
*
* description
*
* @param $var
* @param $val
* @return unknown_type
*/
public function append($var, $val) {
$this->view->append($var,$val);
}
/**
*
* description
*
* @param $var
* @param $val
* @return unknown_type
*/
public function assignRef($var, &$val) {
$this->view->assignRef($var,$val);
}
/**
*
* description
*
* @param $module
* @param $action
* @return unknown_type
*/
public function forward($module,$action='index', $params = false) {
if($this->isComponent()) {
$d = new Component($module,$action, $params ? $params : $this->_params);
} else {
$d = new Dispatcher($module,$action, $params ? $params : $this->_params);
}
$d->execute();
$this->_lastOutput = $d->getPage();
}
/**
*
* description
*
* @param $modulaction
* @param $vars
* @param $lang
* @param $secure
* @return unknown_type
*/
public function redirect($modulaction,$vars = null,$lang=null,$secure=null,$status = '302') {
if(preg_match('`^(http|https)://`i',$modulaction)) {
header('location:'.$modulaction,true,$status);
exit(0);
}
if($this->isComponent()) {
list($module,$action)=explode('/',$modulaction);
$varsAr = is_null($vars) ? $this->_params : array_merge($vars,$this->_params);
return $this->forward($module[0],$module[1]?$module[1]:'index', $varAr);
}
if($this->isAjaxRequest()) {
$vars['__ajax']=1;
}
$url = URL::get($modulaction,$vars,$lang,$secure);
if ((stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && stristr($_SERVER['HTTP_USER_AGENT'], 'Mac')) || headers_sent()) {
echo '<script language="JavaScript1.1">
<!--
location.replace("'.$url.'");
//-->
</script>
<noscript>
<meta http-equiv="Refresh" content="0; URL='.$url.'"/>
</noscript>
Redirection... merci de patienter ou cliquer <a href="'.$url.'">ici</a>.
';
} else {
header('location:'.$url,true,$status);
}
flush();
exit(0);
}
public function redirect301($modulaction,$vars = null,$lang=null,$secure=null)
{
$this->addHeader('301 Moved Permanently');
$this->redirect($modulaction,$vars,$lang,$secure,'301');
}
public function redirect404($modulaction,$vars = null,$lang=null,$secure=null)
{
header('Status: 404');
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
$arr = explode('/',$modulaction);
$d = new Dispatcher($arr[0],$arr[1],$this->_params);
$d->execute();
echo $d->display();
exit(0);
}
public function addHeader($header)
{
if($this->isComponent()) return;
if (php_sapi_name()=='cgi') {
header('Status: '.$header);
} else {
header('HTTP/1.1 '.$header);
}
}
/**
*
* description
*
* @param $formName
* @param $method
* @param $action
* @param $target
* @param $attributes
* @param $trackSubmit
* @return unknown_type
*/
public function createForm($formName='', $method='post', $action='', $target='', $attributes=null, $trackSubmit = false)
{
if(empty($action)) {
$action = URL::getSelf();
}
return new MyQuickForm($formName,$method,$action,$target,$attributes,$trackSubmit);
}
/**
* Returns true if the current request is a XmlHttpRequest
*
* @return bool
*/
public function isAjaxRequest() {
return array_key_exists('HTTP_X_REQUESTED_WITH',$_SERVER) && $_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest';
}
/**
* Events triggering
*/
public function trigger($eventName,$params)
{
# code...
}
/**
* stores flash message, released until displayed
* @param string the message
* @param string the message type (may be info, error, warning, success, @see http://twitter.github.com/bootstrap/ for alert types)
* defaults to info
*
*/
public function flash($message,$type='info')
{
if(!$_SESSION) session_start();
$_SESSION['flash'][$type][]=$message;
}
public function popflash($type='info')
{
if(!$_SESSION) session_start();
if(!is_array($_SESSION['flash'][$type])) return false;
return array_shift($_SESSION['flash'][$type]);
}
public function accept_methods()
{
$args = func_get_args();
$requested_method = strtolower($_SERVER['REQUEST_METHOD']);
foreach($args as $allowed_method) {
if($requested_method == strtolower($allowed_method)) {
return true;
}
}
throw new InvalidRequestException('Method '.$requested_method.' not allowed');
}
}