コード例 #1
0
ファイル: l10n.php プロジェクト: netcarver/mlp_pack
function _l10n_process_url($use_get_params = false)
{
    global $l10n_language, $prefs;
    $redirects = array('', '/');
    $redirect = false;
    $new_first_path = '';
    $debug = 0 && @txpinterface === 'public';
    @session_start();
    $site_langs = MLPLanguageHandler::get_site_langs();
    $req_method = serverSet('REQUEST_METHOD');
    $request = serverSet('REQUEST_URI');
    #$request = $_SESSION['l10n_request_uri'] = serverSet('REQUEST_URI');
    #	Redirect empty GETs on the public side so that the URL used has the language code
    # embedded in it.
    #
    #	This should stop search engines from caching 'fake' images of pages.
    #
    if (@txpinterface === 'public' && 'GET' === $req_method && in_array($request, $redirects)) {
        $redirect = true;
    }
    #	The following section is taken verbatim from Textpattern's own Pretext().
    # -- START VERBATIM --
    // IIS fixes...
    if (!$request and serverSet('SCRIPT_NAME')) {
        $request = serverSet('SCRIPT_NAME') . (serverSet('QUERY_STRING') ? '?' . serverSet('QUERY_STRING') : '');
    }
    if (!$request and serverSet('argv')) {
        $argv = serverSet('argv');
        $request = @substr($argv[0], strpos($argv[0], ';') + 1);
    }
    $subpath = preg_quote(preg_replace("/https?:\\/\\/.*(\\/.*)/Ui", "\$1", hu), '/');
    $req = preg_replace("/^{$subpath}/i", '/', $request);
    # -- END VERBATIM --
    if (!$use_get_params) {
        $parts = chopUrl($req);
        extract($parts);
    }
    if ($debug) {
        echo br, 'hu=' . hu . " REQUEST = ", $req_method, ' : ', var_dump($req);
        echo br, var_dump($parts);
        #global $plugin_callback;
        #foreach( $plugin_callback as $cb )
        #	if( $cb['event'] === 'pretext' )
        #		echo br , var_dump( $cb );
    }
    $ssname = 'l10n_short_lang';
    $lsname = 'l10n_long_lang';
    if ($use_get_params) {
        #
        #	Admin session variables differ from public to stop crosstalk...
        #
        $ssname = 'l10n_admin_short_lang';
        $lsname = 'l10n_admin_long_lang';
        $temp = gps('adminlang');
        $tmp = substr($temp, 0, 2);
        #
        #	Admin side we use the installation languages, not just the more
        # restricive 'site' languages used on the public side...
        #
        $site_langs = MLPLanguageHandler::get_installation_langs();
        if (!empty($temp) and in_array($temp, $site_langs)) {
            #
            #	Hit! We can serve this language...
            #
            $_SESSION[$ssname] = $tmp;
            $_SESSION[$lsname] = $temp;
            if ($debug) {
                echo br, "L10N MLP: Set session vars ({$ssname} < {$tmp}) ({$lsname} < {$temp}).";
            }
        }
    }
    if (!$use_get_params and !empty($u1)) {
        if ($debug) {
            echo br, "L10N MLP: Public - Checking URL ({$req}), LANG = ", LANG;
        }
        #
        #	Examine the first path entry for the language request.
        #
        $reduce_uri = true;
        $reduced_uri = null;
        if (isset($prefs['l10n_detect_language_func']) and is_callable($prefs['l10n_detect_language_func'])) {
            $callback_detect_language = call_user_func($prefs['l10n_detect_language_func'], $req);
        }
        if (is_array($callback_detect_language) and isset($callback_detect_language['lang']) and isset($callback_detect_language['lang']['long']) and in_array($callback_detect_language['lang']['long'], $site_langs)) {
            if ($debug) {
                echo br, "L10N MLP: Plugin callback returned ", var_dump($callback_detect_language);
            }
            $_SESSION[$ssname] = $callback_detect_language['lang']['short'];
            $_SESSION[$lsname] = $callback_detect_language['lang']['long'];
            $reduced_uri = $callback_detect_language['uri']['reduced'];
            $new_first_path = array_shift(explode('/', ltrim($reduced_uri, '/'), 2));
            $u1 = $callback_detect_language['uri']['code'];
        } else {
            if (empty($temp)) {
                if ($debug) {
                    echo br, 'L10N MLP: Checking start of path for language ... ' . $u1;
                }
                $temp = MLPLanguageHandler::expand_code($u1);
                if ($debug) {
                    echo br, "L10N MLP: expand_code({$u1}) returned ", var_dump($temp);
                }
                $new_first_path = isset($u2) ? $u2 : '';
            }
            if (!empty($temp) and in_array($temp, $site_langs)) {
                #
                #	Hit! We can serve this language...
                #
                if ($debug) {
                    echo br, "L10N MLP: Set session vars ({$ssname} < {$u1}) ({$lsname} < {$temp}).";
                }
                $_SESSION[$ssname] = $u1;
                $_SESSION[$lsname] = $temp;
            } else {
                if ($debug) {
                    echo br, 'L10N MLP: no-match branch';
                }
                #
                #	Not a language this site can serve...
                #
                if (!MLPLanguageHandler::is_valid_short_code($u1)) {
                    #	And not a known language so don't reduce the uri and use the original part of the path...
                    $reduce_uri = false;
                    $new_first_path = $u1;
                }
            }
        }
        if ($reduce_uri) {
            #
            #	Request_uri needs the subdir + lang stripping to prevent 404s
            #
            #	Subdir will be restored in a callback to the textpattern action to prevent conflicts with
            # commenting and plugins...
            #
            $lang_code_pos = strpos($_SERVER['REQUEST_URI'], "/{$u1}/");
            $_SESSION['l10n_request_uri'] = substr($_SERVER['REQUEST_URI'], 0, $lang_code_pos + 1) . substr($_SERVER['REQUEST_URI'], $lang_code_pos + strlen($u1) + 2, strlen($_SERVER['REQUEST_URI']));
            if (!($new_uri = $reduced_uri)) {
                $new_uri = substr($req, strlen($u1) + 1);
            }
            if (empty($new_uri)) {
                $new_uri = '/';
            }
            $_SERVER['REQUEST_URI'] = $new_uri;
            if ($debug) {
                echo br, "REQUEST reduced to ... [{$new_uri}]";
            }
        }
    }
    if (@$prefs['l10n_l10n-use_browser_languages'] == '1') {
        if ($debug) {
            echo br, br, "Checking browser accept-language headers.", br, br;
        }
        if (!isset($_SESSION[$ssname]) or empty($_SESSION[$ssname])) {
            #
            #	If we are still missing a language for the session, try to get the prefered selection
            # from the user agent's HTTP header.
            #
            $req_lang = serverSet('HTTP_ACCEPT_LANGUAGE');
            if ($debug) {
                echo br, "L10N MLP: processing browser language header :", var_dump($req_lang);
            }
            if (isset($req_lang) and !empty($req_lang)) {
                $chunks = explode(',', $req_lang);
                if (count($chunks)) {
                    foreach ($chunks as $chunk) {
                        $info = explode(';', $chunk);
                        if (false === $info) {
                            $info[] = $chunk;
                        }
                        $code = $info[0];
                        if (isset($code) and !empty($code)) {
                            $len = strlen($code);
                            if ($len === 2) {
                                $lang = MLPLanguageHandler::expand_code($info[0]);
                                if (!empty($lang)) {
                                    $lang = MLPLanguageHandler::compact_code($lang);
                                } else {
                                    continue;
                                }
                            } elseif ($len === 5) {
                                $lang = MLPLanguageHandler::compact_code($info[0]);
                            } else {
                                continue;
                            }
                            if (in_array($lang['long'], $site_langs)) {
                                $_SESSION[$ssname] = $lang['short'];
                                $_SESSION[$lsname] = $lang['long'];
                                if ($debug) {
                                    echo 'Setting language to ' . $lang['long'] . ' from browser headers', br, br;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    #
    #	If we are still missing a language for the session, use the site default...
    #
    if (!isset($_SESSION[$ssname]) or empty($_SESSION[$ssname])) {
        $long = $site_langs[0];
        $short = substr($long, 0, 2);
        $_SESSION[$ssname] = $short;
        $_SESSION[$lsname] = $long;
        if ($debug) {
            echo br, "L10N MLP: No language match found, setting to site default ... {$long} as {$short}";
        }
    }
    if ($redirect) {
        if (isset($prefs['l10n_language_marker_func']) and is_callable($prefs['l10n_language_marker_func'])) {
            $callback_language_marker = call_user_func($prefs['l10n_language_marker_func'], $_SESSION[$lsname]);
        }
        if (!$callback_language_marker) {
            $callback_language_marker = $_SESSION[$ssname];
        }
        $location = hu . $callback_language_marker . '/';
        # QUESTION: Does this need a trailing slash?
        if ($debug) {
            echo br, 'L10N MLP: About to redirect to: <a href="' . $location . '">' . $location . '</a>';
            exit(0);
        } else {
            header('Location: ' . $location);
            header('Connection: close');
            header('Content-Length: 0');
            exit(0);
        }
    }
    if (_l10n_set_browse_language($_SESSION[$lsname], true, $debug)) {
    } else {
        _l10n_set_browse_language($_SESSION[$ssname], false, $debug);
    }
    if ($debug) {
        echo br, "New first path is: {$new_first_path}";
    }
    return $new_first_path;
}