1
difenduandada
2024-10-15 7fd2948ee35c8e147ed35ce6d8502f94a98ddd22
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
<?php
 
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
 
define('IS_VISITOR_PAGE', true);
 
if(file_exists('static') && !defined('NO_STATIC')){
    if(file_exists('index_static.php')){
        require_once('index_static.php');
        exit();
    }
}
 
require( 'config.php' );
require( 'init.php' );
require( 'classes/Collection.php' );
require( 'includes/plugin.php' );
 
$caching_system = null;
 
$_wgts = get_pref('widgets');
$_wgts = ($_wgts) ? json_decode($_wgts, true) : [];
$stored_widgets = $_wgts;
 
$lang_code = get_setting_value('language');
$lang_code_url = null; // Store language ID in url
$url_params = [];
if (PRETTY_URL && isset($_GET['viewpage']) == 'search' && strpos($_SERVER['REQUEST_URI'], '?viewpage=search')) {
    // If search page with query string URL
    // Then redirect to pretty url version
    header('Location: '.get_permalink('search', $_GET['slug']), true, 301);
    exit();
}
if(PRETTY_URL){
    $url_params = array_values(array_filter(explode('/', urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)))));
} else {
    if (isset($_GET['viewpage'])) {
        $url_params = array_values(array_filter(array_map('trim', $_GET)));
    }
}
if(SUB_FOLDER != ""){
    // Is using sub-folder
    $fname = str_replace("/", "", SUB_FOLDER);
    if(isset($url_params[0]) && $url_params[0] == $fname){
        array_shift($url_params);
    }
}
 
// BEGIN MULTI-LANGUAGE
if (array_key_exists('lang', $_GET)) {
    // Switch language with ?lang=en parameter
    $lang_code = $_GET['lang'];
}
 
$language_file_exist = true; // Set false if lang file not exist
$lang_url_enabled = get_setting_value('lang_code_in_url');
if($lang_url_enabled && PRETTY_URL){
    // Put language ID on url
    // example: domain.com/en/game
    if (!array_key_exists('lang', $_GET)) {
        $lang_code = isset($url_params[0]) ? $url_params[0] : (isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en');
    }
    if (!preg_match('/^[a-z]{2}$/', $lang_code) || empty($url_params)) {
        // If url doesn't contain language ID on it's url
        // or current url is home page
        // then redirect to a new url (cur url) that contain language ID
        $is_search = (isset($url_params[1]) && $url_params[1] == 'search') ? true : false;
        if(!$is_search){
            // Exception for search page
            $lang_code = isset($_COOKIE['lang']) ? $_COOKIE['lang'] : get_setting_value('language');
            $redirect_url = DOMAIN . "$lang_code{$_SERVER['REQUEST_URI']}";
            if(empty($url_params)){
                // Is home page
                if(get_setting_value('trailing_slash')){
                    // If trailing slash is activated
                } else {
                    if(substr($redirect_url, -1) == '/'){
                        $redirect_url = substr($redirect_url, 0, -1);
                    }
                }
                // Home page will be domain.com/en/
                // If trailing_slash is inactive, then '/' in the last character of the url will be removed
            }
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: $redirect_url");
            exit();
        }
    } else {
        // url contain language ID
        $lang_code_url = $url_params[0];
    }
    if(isset($url_params[0]) && $url_params[0] == $lang_code){
        // Remove language ID from url array
        array_shift($url_params);
    }
    $file = ABSPATH . 'locales/public/' . $lang_code . '.json';
    if (!file_exists($file) && $lang_code != 'en') {
        $file = TEMPLATE_PATH . '/locales/' . $lang_code . '.json'; // Backward compatibility
        if (!file_exists($file)){
            // Language file does not exist
            $lang_code = 'en';
            $language_file_exist = false;
        }
    }
    $_GET['lang'] = $lang_code;
}
// END
 
if (PRETTY_URL) {
    $_GET['viewpage'] = isset($url_params[0]) ? $url_params[0] : 'homepage';
    if(isset($url_params[1])) {
        $_GET['slug'] = $url_params[1];
    }
    if(get_setting_value('trailing_slash')){
        // If trailing slash is activated
        if(count($url_params)){
            $cur_url = $_SERVER['REQUEST_URI'];
            if(substr($cur_url, -1) != '/' && !strpos($cur_url, '?')){
                // Add trailing slash, then redirect
                header('Location: '.substr(DOMAIN, 0, -1).$cur_url.'/', true, 301);
                exit();
            }
        }
    } else {
        // Trailing slash is deactivated
        if(count($url_params)){
            $cur_url = $_SERVER['REQUEST_URI'];
            if(substr($cur_url, -1) == '/' && !strpos($cur_url, '?')){ // Have trailing slash
                // Remove trailing slash, then redirect
                header('Location: '.substr(DOMAIN, 0, -1).substr($cur_url, 0, -1), true, 301);
                exit();
            }
        }
    }
}
 
load_language('index');
 
$page_name = isset( $_GET['viewpage'] ) ? $_GET['viewpage'] : 'homepage';
 
$base_taxonomy = get_base_taxonomy($page_name);
$custom_path = $base_taxonomy;
//
if ($base_taxonomy == $page_name && $page_name != get_custom_path($base_taxonomy)) {
    // Visitor is accessing old base_taxonomy, redirect to custom path
    $new_url = get_permalink('404'); // Default to 404
    if($base_taxonomy != 'login'){
        switch (count($url_params)) {
            case 1:
                $new_url = get_permalink($page_name);
                break;
            case 2:
                $new_url = get_permalink($page_name, $url_params[1]);
                break;
            default:
                if (count($url_params) >= 3) {
                    $arrs = [];
                    for ($i = 2; $i < count($url_params); $i++) {
                        $key = "param" . ($i - 1);
                        $arrs[$key] = $url_params[$i];
                    }
                    $new_url = get_permalink($page_name, $url_params[1], $arrs);
                }
                break;
        }
    }  
    header('Location: ' . $new_url, true, 301);
    exit();
}
 
if($base_taxonomy == 'search'){
    if(PRETTY_URL){
        if(isset($_GET['slug']) && strpos($_SERVER['REQUEST_URI'], 'index.php?viewpage=search')){
            header('Location: '.get_permalink('search', $_GET['slug']), true, 301);
            exit();
        }
    }
}
 
require_once( ABSPATH.'content/themes/theme-functions.php' );
load_plugins('index');
require_once( TEMPLATE_PATH . '/functions.php' );
 
if($lang_url_enabled && PRETTY_URL){
    if(!$language_file_exist && ($lang_code_url != 'en')){
        // Language file requested in url is not exist
        // Show 404 page
        require( 'includes/page-404.php' );
        exit();
    }
}
 
if(file_exists( 'includes/page-' . $base_taxonomy . '.php' )){
    require( 'includes/page-' . $base_taxonomy . '.php' );
} else {
    if(file_exists( TEMPLATE_PATH.'/page-' . $page_name . '.php' )){
        require( TEMPLATE_PATH.'/page-' . $page_name . '.php' );
    } else {
        require( 'includes/page-404.php' );
    }
}
 
?>