router_class.php
Войти

router_class.php

router_class.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
<?php 

class Router{
    
    protected 
$user_id;
    protected 
$req_uri;
    protected 
$modules = array();
    private 
$lvl_url 1;
    public 
$db;
    private 
$url;
    private 
$params;
    private 
$tmp;
    
    public function 
__construct($req_uri=false,$user_id=false){
        
$this->db = new DB;
        
$this->req_uri $req_uri;
        if(
USER_ID){
            
            
$this->mod_list_auth();
        }
        else{

            
$this->mod_list_no_auth();
        }
        if(
$_POST['action']){
            
$this->loader_ajax_zapr();
        }else{
            if(
AJAX){
                
$this->loader_ajax();
            }else{
                
$this->loader();
            }
        }
        
        
        
#print_r($this->modules);
        #echo $req_uri.'';
    
}
    
    private function 
check_url(){
        if(
AJAX){$this->req_uri $_POST['link'];}
        
$this->url urldecode($this->req_uri);
        
$this->urlexplode('?'$this->url);
        
$this->params $this->url[1];
        
$this->tmp explode('/'trim($this->url[0], '/'));
        if(
count($this->tmp) > $this->lvl_url){
            
$this->er404();
        }
        
    }
    
    private function 
loader_ajax_zapr(){
        
$path "/modules/";
        
$ix "/index.php";
        switch(
$_POST['action']) {
            case 
"audio_stat":$zapr_cont dirname(__DIR__).$path."audio/zapr/audio_stat.php";break;
            case 
"audio_time":$zapr_cont dirname(__DIR__).$path."audio/zapr/audio_time.php";break;
            case 
"audio_next":$zapr_cont dirname(__DIR__).$path."audio/zapr/audio_next.php";break;
            case 
"audio_prev":$zapr_cont dirname(__DIR__).$path."audio/zapr/audio_prev.php";break;
            case 
"edit_osn":$zapr_cont dirname(__DIR__).$path."edit/zapr/edit_osn.php";break;
            case 
"edit_lich":$zapr_cont dirname(__DIR__).$path."edit/zapr/edit_lich.php";break;
            case 
"edit_izm_region":$zapr_cont dirname(__DIR__).$path."edit/zapr/edit_izm_region.php";break;
            case 
"edit_izm_city":$zapr_cont dirname(__DIR__).$path."edit/zapr/edit_izm_city.php";break;
            default:
                
$zapr_cont false;
        }
        
        require_once(
$zapr_cont);
    }
            
            
    private function 
loader(){
        
        
$this->check_url();
        require_once(
ROOT_DIR.'class/lang.php');
        if(
USER_ID){
            if(empty(
$this->tmp[0])){
                echo 
"<script>document.location.replace('".SITE_URL."/id".USER_ID."');</script>";die();
            }
            else if(
$this->tmp[0]=="login" && empty($this->tmp[1])){
                echo 
$this->modules['login'];
                exit();
            }else{

                
$content $this->handler_url($this->tmp[0]);
                require_once(
dirname(__DIR__)."/basic/page.php");
            }

        }
        else{
            if(empty(
$tmp[0])){
                
$content '/modules/reg/reg.php';
            }
            else{
                if(
array_key_exists($tmp[0],$this->modules)){            
                    
$content $this->modules[$tmp[0]];
                }    
            }
            
            require_once(
dirname(__DIR__)."/basic/page_no_auth.php");    
        }

    }
    
    private function 
loader_ajax(){
        
        
$this->check_url();
        require_once(
$_SERVER['DOCUMENT_ROOT'].'/class/lang.php');
        
        if(
USER_ID){
            if(empty(
$this->tmp[0])){
                
$content $this->handler_url("id".USER_ID);
            }else{
                
$content $this->handler_url($this->tmp[0]);
            }
        }else{
            
$content $this->handler_url("id".USER_ID);
        }
        require_once(
ROOT_DIR.$content);
        
        
    }
    
    private function 
handler_url($first){
        
$path "modules/";
        
$ix "/index.php";
        if(
$this->size_url($first,"id",true)){return $path."profile".$ix;}
        if(
$this->size_url($first,"feed")){return $path."feed".$ix;}
        if(
$this->size_url($first,"friends",true)){return $path."friends".$ix;}
        if(
$this->size_url($first,"am",true)){return $path."photo".$ix;}
        if(
$this->size_url($first,"video",true)){return $path."video".$ix;}
        if(
$this->size_url($first,"edit")){return $path."edit/pod_module/edit_osn.php";}
        if(
$this->size_url($first,"edit_lich")){return $path."edit/pod_module/edit_lich.php";}
        if(
$this->size_url($first,"edit_cont")){return $path."edit/pod_module/edit_cont.php";}
        if(
$this->size_url($first,"audio")){return $path."audio".$ix;}
        if(
$this->size_url($first,"mail")){return $path."mail".$ix;}
        if(
$this->size_url($first,"logout")){return $path."logout".$ix;}
        if(
$this->size_url($first,"public",true)){return $path."groups/page/public.php";}
        if(
$this->size_url($first,"groups")){return $path."groups".$ix;}
        if(
$this->size_url($first,"fave")){return $path."fave".$ix;}
        if(
$this->size_url($first,"priv")){return $path."settings".$ix;}
        if(
$this->size_url($first,"poisk_people")){return $path."poisk/poisk_cnt.php";}
        if(
$this->size_url($first,"poisk_publ")){return $path."poisk/poisk_cnt.php";}
        if(
$this->size_url($first,"poisk_audio")){return $path."poisk/poisk_cnt.php";}
        if(
$this->size_url($first,"poisk_audio")){return $path."poisk/poisk_cnt.php";}
        if(
$this->size_url($first,"poisk_news")){return $path."poisk/poisk_cnt.php";}
        if(
$this->size_url($first,"poisk_video")){return $path."poisk/poisk_cnt.php";}
        if(
$this->size_url($first,"help")){return $path."/help/index_no_auth.php";}
        
    }
    
    private function 
size_url($first,$inc,$no=false){    
    
        if(
$no){
            if(
$this->sokr($first,strlen($inc))==$inc){
                return 
true;
            }
        }else{
            if(
strlen($first)==strlen($inc) && $first==$inc){
                return 
true;
            }
        }
        
        return 
false;
    }
    
    private function 
sokr($first,$size){    
            return 
substr($first0$size);    
    }

    
    
    private function 
er404(){
        
header($_SERVER['SERVER_PROTOCOL'].'HTTP/1.1 404 Not Found');
        
header("Status: 404 Not Found");
        
header("location: /404/");
        exit();
    }
    
    private function 
mod_list_auth(){
        
$this->modules = array(
            
"index" => "<script>document.location.replace('{".SITE_URL."}/id{USER_ID}');</script>",
            
404 => "/err/error404.php",
            
#"login" => "<script>document.location.replace('{".SITE_URL."}/id{USER_ID}');</script>",
            
"profile" => "/modules/profile/profile_cnt.php",
            
"logout" => "/modules/logout/index.php",
        );
    }
    
    private function 
mod_list_no_auth(){
        
$this->modules = array(
            
"index" => "/modules/reg/reg.php",
            
"about" => "/modules/about/index.php",
            
"audio" => "/modules/audio/index.php",
            
"help" => "/modules/help/index_no_auth.php",
            
404 => "/err/error404.php",
            
#"login" => "<script>document.location.replace('{".SITE_URL."}/id{USER_ID}');</script>",
            
"profile" => "/modules/profile/profile_cnt.php",
            
"logout" => "/modules/logout/index.php",
        );
    }
    
    public static function 
getInstance()
    {
        if (
null === self::$instance)
        {
            
self::$instance = new self();
        }
        return 
self::$instance;
    }
    
    private function 
__clone() {
    }

    private function 
__wakeup() {
    } 
    
    
}

$apl = new Router(REQ_URI,USER_ID);

?>
Теги:
php