Blame view

mobile/source/modules/admin/events/admin_event_htaccess.php 2.57 KB
a1684257   Administrator   first commit
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
  <?php
  /**
  * @author:  Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
  * @copyright: Copyright (c) 2010, Bunzia Alexander
  * @version: 1.0
  * @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
  * @package: HiLi CMS
  */
  if ( !defined("event_admin_htaccess") ){
    define("event_admin_htaccess", 1); 
    
    
    /**
    * Заменяет файл htaccess. Выполняется на событии on_request  и перезаписывает глобальные массив
    * REQUEST и GET основываясь на правилах, которые задали модули
    *
    */
    function admin_event_htaccess(){
      if ( MAIN_CACHE_MODE==1 ){
        include_once(MAIN_PATH.'/tmp/config/htaccess.php');
        
      }else{
      
        //  собираем статистика
        $urls = array();
        include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.modules.php');
        $m = new modulesQuery('m');
        $m -> where_active(1);
        //$m -> set_debug(1);
        $m -> get('m_path');
        while( list($m_path) = $m -> row() ){
          if ( !file_exists(MAIN_SOURCE_PATH.'/modules/'.$m_path.'/inc/system.php') ){
            continue;
          }
          include_once( MAIN_SOURCE_PATH.'/modules/'.$m_path.'/inc/system.php');
          if ( !function_exists($m_path.'_htaccess') ){
            continue;
          }
          $f = $m_path.'_htaccess';
          $r = $f();
          if ( is_array($r) ){
            $urls = array_merge($urls, $r );
          }
        }
        sys_write_file(MAIN_PATH.'/tmp/config/htaccess.php', '<?php'."\n\r".'$urls='.var_export($urls,true).";\n\r?>");
      }
      if ( sizeof($urls)==0){
        return false;
      }
     $_SERVER['REQUEST_URI'] = ($_SERVER['REQUEST_URI']=='/') ? '/index.html' : $_SERVER['REQUEST_URI'] ;
      
      
      foreach( $urls as $k=>$v ){
        if ( empty($k) || empty($v) ){
          sys_error(ERROR_500);
        }
          //  ошибка регулярного выражения
        if ( false===preg_match($k,$_SERVER['REQUEST_URI'],$res) ){
          sys_error(ERROR_500,'Ошибка регулярного выражения "'.sys_in_html($k).'"');
        }
          //  ничего не найдено
        if ( sizeof($res)==0 ){
          continue;
        }
       
          //  выражение подходит, раскидаем результаты работы 
        foreach( $v as $key =>$value ){
          if( $value[0]=='$' ){
            $value = $res[ $value[1] ];
          }
          $_REQUEST[$key] = empty($_POST[$key]) ?  $value : $_POST[$key];
          $_GET[$key] = $value;
        }
        return true;
      }
      return false;
    }
    
    
  }
  ?>