MkFramework
 All Data Structures Functions
class_root.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
23 class _root{
24 
25  static protected $_oRequest;
26  static protected $_oCache;
27  static protected $_oCacheVar;
28  static protected $_oAuth;
29  static protected $_oUrlrewriting;
30  static protected $_oLog;
31 
32  static protected $_tConfigFilename;
33  static protected $_tRequestTab;
34  static public $tAutoload;
35 
36  static public $tConfigVar;
37  static protected $_tGlobalVar;
38  static protected $_tObject;
39 
40  static protected $_oACL;
41 
42  static protected $_bSessionStarted=0;
43 
48  public function __construct(){
49 
50  self::$tConfigVar=array();
51 
52  self::$tConfigVar['path']=array(
53 
54  'lib' => 'lib/framework/',
55 
56  'conf' => 'conf/',
57  'module' => 'module/',
58  'plugin'=> 'plugin/',
59  'model'=> 'model/',
60  'img'=> 'data/img/',
61  'i18n'=>'data/i18n/',
62  'cache'=>'data/cache/',
63  'layout'=>'site/layout/',
64 
65  );
66 
67  self::$_tConfigFilename=array();
68 
69  }
70 
77  public static function addConf($sConfig,$sCat=null){
78  self::$_tConfigFilename[]=array($sConfig,$sCat);
79  }
84  public static function loadConf(){
85  try{
86  $bConfCacheEnabled=(int)self::getConfigVar('cache.conf.enabled');
87  $sCacheFilename=self::getConfigVar('path.cache').'conf.php';
88  if($bConfCacheEnabled==1 and file_exists($sCacheFilename) ){
89  include $sCacheFilename;
90  return;
91  }
92 
93  $tConfigVar=self::$tConfigVar;
94 
95  foreach(self::$_tConfigFilename as $tConfig){
96  $sConfig=$tConfig[0];
97  $sCatFilter=$tConfig[1];
98 
99  $tIni=array();
100  $tIniBrut=parse_ini_file($sConfig,true);
101 
102  if($sCatFilter!=null){
103  $tIni[$sCatFilter]=$tIniBrut[$sCatFilter];
104  }else{
105  $tIni=$tIniBrut;
106  }
107  $tConfigVar=self::arrayMergeRecursive($tConfigVar,$tIni);
108  }
109 
110  self::$tConfigVar=$tConfigVar;
111 
112 
113  if($bConfCacheEnabled==1){
114  $sCodeCache='<?php _root::$tConfigVar='.var_export(self::$tConfigVar,true).';';
115  file_put_contents($sCacheFilename,$sCodeCache);
116  }
117 
118  }catch(Exception $e){
119  self::erreurLog($e->getMessage()."\n".$e->getTraceAsString());
120  }
121  }
122 
123  public static function arrayMergeRecursive($tArray,$tNewArray){
124 
125  foreach($tNewArray as $sKey => $tValue){
126  foreach($tValue as $sChildKey => $sChildValue){
127  $tArray[$sKey][$sChildKey]=$sChildValue;
128  }
129  }
130 
131  return $tArray;
132 
133  }
134 
139  public static function loadLangueDate(){
140  include_once self::getConfigVar('path.i18n').'date_'.self::getConfigVar('language.default').'.php';
141 
142  }
143 
144  public static function nullbyteprotect($string){
145 
146  $string=trim($string);
147 
148  return preg_replace('/\\x00/','', preg_replace('/\\\0/','',$string));
149  }
150 
151  public static function startSession(){
152  if(self::$_bSessionStarted){
153  return null;
154  }else if( (int)self::getConfigVar('auth.session.use_cookies') == 1 ){
155  $bHttpOnly=null;
156  if((int)self::getConfigVar('auth.session.cookie_httponly')==1){
157  $bHttpOnly=true;
158  }
159  $bSecure=null;
160  if((int)self::getConfigVar('auth.session.cookie_secure')==1 and isset($_SERVER['HTTPS']) ){
161  $bSecure=true;
162  }
163  session_set_cookie_params(
164  (int)self::getConfigVar('auth.session.cookie_lifetime',0),
165  self::getConfigVar('auth.session.cookie_path',null),
166  self::getConfigVar('auth.session.cookie_domain',null),
167  $bSecure,$bHttpOnly
168  );
169  }
170  session_start();
171 
172  self::$_bSessionStarted=1;
173  }
174 
175 
180  public function run(){
181 
182  try{
183 
184  self::loadConf();
185  self::loadAutoload();
186  self::loadRequest();
187 
188  //parametrage du niveau d'erreur
189  if(self::getConfigVar('site.mode')=='dev'){
190  error_reporting(E_ALL);
191  }else{
192  error_reporting(0);
193  }
194 
195  self::getLog()->setInformation((int)self::getConfigVar('log.information'));
196  self::getLog()->setWarning((int)self::getConfigVar('log.warning'));
197  self::getLog()->setError((int)self::getConfigVar('log.error'));
198  self::getLog()->setApplication((int)self::getConfigVar('log.application'));
199 
200 
201 
202  date_default_timezone_set(self::getConfigVar('site.timezone'));
203  //auth
204  if( (int)self::getConfigVar('auth.enabled') == 1 ){
205  self::getAuth()->enable();
206  }
207 
208  //desactivation des magic quotes
209  if (get_magic_quotes_gpc()) {
210  $_POST = array_map('stripslashes_deep', $_POST);
211  $_GET = array_map('stripslashes_deep', $_GET);
212  $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
213  $this->getRequest()->magic_quote();
214  }
215 
216  if( (int)self::getConfigVar('urlrewriting.enabled') == 1 ){
217  self::getUrlRewriting()->parseUrl($_SERVER['REQUEST_URI']);
218  }
219 
220  $sModuleToLoad=self::getRequest()->getModule();
221  $sModuleActionToLoad=self::getRequest()->getAction();
222 
223  /*LOG*/self::getLog()->info('module a appeler ['.$sModuleToLoad.'::'.$sModuleActionToLoad.']');
224 
225  //chargement module/action
226  $sClassModule='module_'.$sModuleToLoad;
227 
228  $oModule=new $sClassModule;
229 
230  if( method_exists($oModule,'_'.$sModuleActionToLoad) ){
231 
232  /*LOG*/self::getLog()->info('appel module ['.$sModuleToLoad.'::before]');
233  $oModule->before();
234 
235  //pre action
236  if( method_exists($oModule,'before_'.$sModuleActionToLoad) ){
237  /*LOG*/self::getLog()->info('appel module ['.$sModuleToLoad.'::before_'.$sModuleActionToLoad.']');
238  $sActionBefore='before_'.$sModuleActionToLoad;
239  $oModule->$sActionBefore();
240  }
241 
242  //debut cache
243  if( (int)self::getConfigVar('cache.enabled') == 1 ){
244 
245  $sNomPageCache='cache_'.str_replace('::','_',implode('_',self::getRequest()->getParams())).'.html';
246  $oFichierCache=new _file(self::getConfigVar('path.cache').$sNomPageCache);
247 
248  if(
249  ( $oFichierCache->exist() and (int)self::getConfigVar('cache.lifetime') == 0 )
250  or
251  ( $oFichierCache->exist() and time()-$oFichierCache->filemtime() < (int)self::getConfigVar('cache.lifetime') )
252  ){
253  /*LOG*/self::getLog()->info('utilisation page en cache ['.$sNomPageCache.']');
254  echo $oFichierCache->getContent();
255  return;
256  }
257 
258  ob_start();
259  }
260  $sAction='_'.$sModuleActionToLoad;
261 
262  /*LOG*/self::getLog()->info('appel module ['.$sModuleToLoad.'::'.$sAction.']');
263  $oModule->$sAction();
264 
265  //post action
266  if( method_exists($oModule,'after_'.$sModuleActionToLoad) ){
267  /*LOG*/self::getLog()->info('appel module ['.$sModuleToLoad.'::after_'.$sModuleActionToLoad.']');
268 
269  $sActionAfter= 'after_'.$sModuleActionToLoad;
270  $oModule->$sActionAfter();
271  }
272 
273  //post module
274  /*LOG*/self::getLog()->info('appel module ['.$sModuleToLoad.'::after]');
275  $oModule->after();
276 
277  //fin cache
278  if( (int)self::getConfigVar('cache.enabled')== 1 ){
279 
280  $sSortie=ob_get_contents();
281  ob_end_clean();
282 
283  $oFichierCache->write($sSortie."\n<!--cache -->");
284 
285  echo $sSortie;
286  }
287  }
288  else{
289  $tErreur=array(
290  'Erreur dans module/'.$sModuleToLoad.'/main.php',
291  'Pas de m&eacute;thode _'.$sModuleActionToLoad.'() dans le module "'.$sModuleToLoad.'" &agrave; charger',
292  'Note: vous pouvez modifier le couple module/action par defaut ',
293  'en modifiant la section [navigation] dans le fichier conf/site.ini.php',
294  );
295  throw new Exception(implode("\n",$tErreur));
296  }
297 
298  }catch(Exception $e){
299  self::erreurLog($e->getMessage()."\n".self::showException($e),$e);
300  }
301 
302  }
303 
304  public static function showException(Exception $e) {
305  $tTrace = $e->getTrace();
306  $result=$e->getTraceAsString();
307 
308 
309  $result.="\n\nDetail:\n";
310 
311  foreach($tTrace as $i=> $trace){
312  $result.='#'.$i.' ';
313  if(isset($trace['file'])){$result.=$trace['file'];}
314  if(isset($trace['line'])){$result.=' ('.$trace['line'].') '."\n";}
315  $result.=' ';
316 
317  if(isset($trace['class'])){
318  $result.=$trace['class'].' '.$trace['type'].' '.$trace['function'].'( ';
319  }else{
320  $result.=$trace['function'].'( ';
321 
322  }
323 
324  if(isset($trace['args']) and is_array($trace['args'])){
325 
326  foreach($trace['args'] as $j => $arg){
327 
328  if($j>0){ $result.=' , ';}
329 
330  if(is_array($arg)){
331  $result.=preg_replace('/\n|\r/',' ',
332  print_r($arg,1)
333  );
334  }else{
335  if(is_null($arg)){ $result.='NULL';}
336 
337  if(is_string($arg)){
338  $result.="'$arg'";
339  }
340  }
341 
342  }
343 
344  }
345  $result.=' ) '."\n";
346 
347  }
348  $result.='#'.($i+1).' {main}';
349 
350 
351  return $result;
352  }
353 
354  private function loadAutoload(){
355  if((int)self::getConfigVar('cache.autoload.enabled')==1){
356  $sCacheFilename=self::getConfigVar('path.cache').'autoload.php';
357  if(file_exists($sCacheFilename)){
358  include $sCacheFilename;
359  }else{
360  //on creer un tableau associatif de tous les path des classes
361  $tDir=array(
362  'lib' => self::getConfigVar('path.lib'),
363  'abstract' => self::getConfigVar('path.lib').'abstract/',
364  'sgbd' => self::getConfigVar('path.lib').'sgbd/',
365  'sgbd_pdo' => self::getConfigVar('path.lib').'sgbd/pdo/',
366  'sgbd_syntax' => self::getConfigVar('path.lib').'sgbd/syntax/',
367  'plugin' => self::getConfigVar('path.plugin'),
368  'model' => self::getConfigVar('path.model'),
369  'module' => self::getConfigVar('path.module'),
370  );
371 
372  $tAutoload=array();
373 
374  foreach($tDir as $sType => $sDir){
375 
376  if(in_array($sType,array(
377  'lib',
378  'abstract',
379  'sgbd',
380  'sgbd_pdo',
381  'sgbd_syntax',
382  'plugin',
383  'model',
384  ))){
385 
386  $oDir=new _dir($sDir);
387 
388  $tFile=$oDir->getListFile();
389  foreach($tFile as $oFile){
390  $sFilename=$oFile->getName();
391  $tFilename=preg_split('/_/',$sFilename);
392  if($sType=='lib'){
393  $tAutoload[ '_'.substr($tFilename[1],0,-4) ]=$sDir.$sFilename;
394  }else{
395  $tAutoload[ substr($sFilename,0,-4) ]=$sDir.$sFilename;
396  }
397 
398  }
399  }else if($sType=='module'){
400  $oDir=new _dir($sDir);
401 
402  $tModuleDir=$oDir->getListDir();
403  foreach($tModuleDir as $oModuleDir){
404  $sModuleDirname=$oModuleDir->getName();
405  $tAutoload['module_'.$sModuleDirname]=$sDir.$sModuleDirname.'/main.php';
406  }
407  }
408  }
409 
410  $sCodeCache='<?php _root::$tAutoload='.var_export($tAutoload,true).';';
411  file_put_contents($sCacheFilename,$sCodeCache);
412  self::$tAutoload=$tAutoload;
413  }
414  }
415  }
416 
422  public static function autoload($sClass){
423 
424  $tab=preg_split('/_/',$sClass);
425  if(isset(self::$tAutoload[$sClass])){
426  include self::$tAutoload[$sClass];
427  }else if($sClass[0]=='_'){
428  include self::getConfigVar('path.lib').'class'.$sClass.'.php';
429  }else if(in_array($tab[0],array('plugin','model','abstract'))){
430  include self::getConfigVar('path.'.$tab[0]).$sClass.'.php';
431  }else if($tab[0]=='module'){
432  include self::getConfigVar('path.module').substr($sClass,7).'/main.php';
433  }else if($tab[0]=='row'){
434  include self::getConfigVar('path.model').'model_'.substr($sClass,4).'.php';
435  }else if($tab[0]=='sgbd' and in_array($tab[1],array('syntax','pdo'))){
436  include self::getConfigVar('path.lib').'sgbd/'.$tab[1].'/'.$sClass.'.php';
437  }else if($tab[0]=='sgbd'){
438  include self::getConfigVar('path.lib').'sgbd/'.$sClass.'.php';
439  }else{
440  return false;
441  }
442 
443 
444  }
445 
453  public static function setParam($sVar,$uValue){
454  self::getRequest()->setParam($sVar,$uValue);
455  }
456 
465  public static function getParam($sVar,$uElse=null){
466  return self::getRequest()->getParam($sVar,$uElse);
467  }
468 
469 
475  public static function setParamNav($sNav){
476  self::getRequest()->setParamNav($sNav);
477  }
478 
484  public static function getParamNav(){
485  return self::getRequest()->getParamNav();
486  }
487 
493  public static function getModule(){
494  return self::getRequest()->getModule();
495  }
496 
502  public static function getAction(){
503  return self::getRequest()->getAction();
504  }
505 
506 
512  public static function addRequest($tRequest){
513  self::$_tRequestTab[]=$tRequest;
514  }
515 
516  public static function loadRequest(){
517  if(self::$_oRequest==null){
518  self::$_oRequest=new _request();
519  }
520  foreach(self::$_tRequestTab as $tRequest){
521  foreach($tRequest as $sVar => $sVal){
522  self::getRequest()->setParam($sVar,$sVal);
523  }
524  }
525  }
526 
531  public static function resetRequest(){
532  self::$_oRequest=null;
533  self::$_tRequestTab=array();
534  }
535 
541  public static function getRequest(){
542  return self::$_oRequest;
543  }
544 
550  public static function getCache(){
551  if(self::$_oCache==null){ self::$_oCache=new _cache(); }
552  return self::$_oCache;
553  }
559  public static function getCacheVar(){
560  if(self::$_oCacheVar==null){ self::$_oCacheVar=new _cacheVar(); }
561  return self::$_oCacheVar;
562  }
568  public static function getAuth(){
569  if(self::$_oAuth==null){
570  $sClassAuth=self::getConfigVar('auth.class');
571  self::$_oAuth=new $sClassAuth;
572  }
573  return self::$_oAuth;
574  }
575 
581  public static function getACL(){
582  if(self::$_oACL==null){
583  $sClassACL=self::getConfigVar('acl.class');
584  self::$_oACL=new $sClassACL;
585  }
586  return self::$_oACL;
587  }
588 
589 
595  public static function getUrlRewriting(){
596  if(self::$_oUrlrewriting==null){
597  $sClassUrlrewriting=self::getConfigVar('urlrewriting.class');
598  self::$_oUrlrewriting=new $sClassUrlrewriting;
599  }
600  return self::$_oUrlrewriting;
601 
602  }
603 
604 
610  public static function getLog(){
611  if(self::$_oLog==null){
612  $sClassLog=self::getConfigVar('log.class');
613  if($sClassLog==''){
614  $tErreur=array(
615  'Il vous manque un bloc dans votre fichier conf/site.ini',
616  '[log]',
617  'class=plugin_log',
618  'application=1',
619  'warning=1',
620  'error=1',
621  'information=1',
622  );
623 
624  self::erreurLog(implode("\n",$tErreur));
625  }
626  self::$_oLog=new $sClassLog;
627  }
628  return self::$_oLog;
629  }
630 
637  public static function setConfigVar($sCatAndVar,$uValue){
638  if(preg_match('/\./',$sCatAndVar)){
639  list($sCategory,$sVar)=preg_split('/\./',$sCatAndVar,2);
640  self::$tConfigVar[$sCategory][$sVar]=$uValue;
641  }else{
642  self::$tConfigVar[$sCatAndVar]=$uValue;
643  }
644  }
645 
646 
654  public static function getConfigVar($sCatAndVar,$uDefaut=null){
655 
656  if(preg_match('/\./',$sCatAndVar)){
657  list($sCategory,$sVar)=preg_split('/\./',$sCatAndVar,2);
658 
659  if(in_array($sVar,array('sgbd','abstract','sgbd_pdo','sgbd_syntax'))){
660  if(preg_match('/_/',$sVar)){
661  $sVar=preg_replace('/_/','/',$sVar);
662  }
663  return self::$tConfigVar['path']['lib'].$sVar.'/';
664  }
665  else if(isset(self::$tConfigVar[$sCategory][$sVar])){
666  return self::$tConfigVar[$sCategory][$sVar];
667  }
668 
669  }else if(isset(self::$tConfigVar[$sCatAndVar])){
670  return self::$tConfigVar[$sCatAndVar];
671  }
672  return $uDefaut;
673  }
674 
681  public static function setGlobalVar($sVar,$sValue){
682  self::$_tGlobalVar[$sVar]=$sValue;
683  }
691  public static function getGlobalVar($sVar,$defaut=null){
692  if(isset(self::$_tGlobalVar[$sVar])){
693  return self::$_tGlobalVar[$sVar];
694  }
695  return $defaut;
696  }
697 
704  public static function setInstanceOf($sObj,$oObj){
705  self::$_tObject[$sObj]=$oObj;
706  }
714  public static function getObject($sObj,$defaut=null){
715  if(isset(self::$_tObject[$sObj])){
716  return self::$_tObject[$sObj];
717  }
718  return $defaut;
719  }
720 
727  public static function redirect($uNav,$tParam=null){
728  /*LOG*/self::getLog()->info('redirection ['.self::getLink($uNav,$tParam,false).']');
729  header('Location:'.self::getLink($uNav,$tParam,false));
730  exit(0);
731  }
732 
739  public static function getLinkWithCurrent($tParam=null,$bAmp=true){
740  $tOriginParam=self::getRequest()->getParamsGET();
741  unset($tOriginParam[ _root::getConfigVar('navigation.var') ] );
742 
743  $tNewParam=array_merge($tOriginParam,$tParam);
744  $sNav=self::getParamNav();
745 
746  return self::getLink($sNav,$tNewParam,$bAmp);
747  }
748 
756  public static function getLink($uNav,$tParam=null,$bAmp=true){
757 
758  if(is_array($uNav)){
759  $sNav=$uNav[0];
760  unset($uNav[0]);
761  $tParam=$uNav;
762  }else{
763  $sNav=$uNav;
764  }
765 
766  if( (int)self::getConfigVar('urlrewriting.enabled') ==1 ){
767  return self::getUrlRewriting()->getLink($sNav,$tParam);
768  }else{
769  return self::getLinkString($sNav,$tParam,$bAmp);
770  }
771  }
778  public static function getLinkString($sNav,$tParam=null,$bAmp=true){
779 
780  $sLink='';
781  if(is_array($tParam)){
782  foreach($tParam as $sKey => $sVal){
783  if($sKey=='#'){
784  continue;
785  }else if($bAmp){
786  $sLink.='&amp;';
787  }else{
788  $sLink.='&';
789  }
790  $sLink.=$sKey.'='.$sVal;
791  }
792  if(isset($tParam['#'])){
793  $sLink.='#'.$tParam['#'];
794  }
795  }
796 
797  return self::getConfigVar('navigation.scriptname').'?'.self::getConfigVar('navigation.var').'='.$sNav.$sLink;
798 
799  }
800 
801  public static function erreurLog($sText,$e=null){
802  if(self::getConfigVar('site.mode')=='dev'){
803 
804  if(self::getConfigVar('debug.class')){
805  $sClass=self::getConfigVar('debug.class');
806  $oDebug=new $sClass;
807  $oDebug->show($sText,$e);
808  }else{
809  $sText=nl2br($sText);
810  include self::getConfigVar('path.layout').'erreur.php';
811  }
812 
813  }else{
814  include self::getConfigVar('navigation.layout.erreur','../layout/erreurprod.php');
815  try{
816  if(self::getConfigVar('log.apache.enabled',1)==1){
817  error_log('[quiet error]:'.$sText);
818  }
819  if(self::getConfigVar('log.file.enabled',1)==1){
820  $open=fopen(self::getConfigVar('path.log','data/log').date('Y-m-d').'.txt','a+');
821  fputs($open,$sText);
822  fclose($open);
823  }
824 
825  }catch(Exception $e){
826  //en mode production, on est muet
827  }
828  }
829  }
830 
831 
832 
833 }
834 
835 function stripslashes_deep($value){
836  if(is_array($value)){
837  return array_map('stripslashes_deep', $value);
838  }else{
839  return stripslashes($value);
840  }
841 }
842 function customHtmlentities($string){
843  if(is_array($string)){ return array_map('customHtmlentities',$string) ;}
844  return _root::nullbyteprotect(htmlentities(
845  $string,
846  ENT_QUOTES,
847  _root::getConfigVar('encodage.charset'),
848  _root::getConfigVar('encodage.double_encode',1)));
849 }
850 
static getCacheVar()
Definition: class_root.php:559
static getModule()
Definition: class_root.php:493
static resetRequest()
Definition: class_root.php:531
static getConfigVar($sCatAndVar, $uDefaut=null)
Definition: class_root.php:654
static setGlobalVar($sVar, $sValue)
Definition: class_root.php:681
static autoload($sClass)
Definition: class_root.php:422
static getLink($uNav, $tParam=null, $bAmp=true)
Definition: class_root.php:756
static getCache()
Definition: class_root.php:550
static setConfigVar($sCatAndVar, $uValue)
Definition: class_root.php:637
static loadConf()
Definition: class_root.php:84
static getUrlRewriting()
Definition: class_root.php:595
static getAuth()
Definition: class_root.php:568
static loadLangueDate()
Definition: class_root.php:139
static getParamNav()
Definition: class_root.php:484
static addRequest($tRequest)
Definition: class_root.php:512
static getParam($sVar, $uElse=null)
Definition: class_root.php:465
static getRequest()
Definition: class_root.php:541
static setParamNav($sNav)
Definition: class_root.php:475
static getLinkString($sNav, $tParam=null, $bAmp=true)
Definition: class_root.php:778
static setInstanceOf($sObj, $oObj)
Definition: class_root.php:704
static getLog()
Definition: class_root.php:610
static setParam($sVar, $uValue)
Definition: class_root.php:453
static getAction()
Definition: class_root.php:502
static addConf($sConfig, $sCat=null)
Definition: class_root.php:77
__construct()
Definition: class_root.php:48
static getLinkWithCurrent($tParam=null, $bAmp=true)
Definition: class_root.php:739
static redirect($uNav, $tParam=null)
Definition: class_root.php:727
static getACL()
Definition: class_root.php:581
static getObject($sObj, $defaut=null)
Definition: class_root.php:714
static getGlobalVar($sVar, $defaut=null)
Definition: class_root.php:691