Example #1
0
function update_locale($loc)
{
    // $LANG or DEFAULT_LANGUAGE is too less information, at least on unix for
    // setlocale(), for bindtextdomain() to succeed.
    $setlocale = guessing_setlocale(LC_ALL, $loc);
    // [56ms]
    if (!$setlocale) {
        // system has no locale for this language, so gettext might fail
        $setlocale = FileFinder::_get_lang();
        list($setlocale, ) = split('_', $setlocale, 2);
        $setlocale = guessing_setlocale(LC_ALL, $setlocale);
        // try again
        if (!$setlocale) {
            $setlocale = $loc;
        }
    }
    // Try to put new locale into environment (so any
    // programs we run will get the right locale.)
    if (!function_exists('bindtextdomain')) {
        // Reinitialize translation array.
        global $locale;
        $locale = array();
        // do reinit to purge PHP's static cache [43ms]
        if ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok', 'reinit')) {
            include $lcfile;
        }
    } else {
        // If PHP is in safe mode, this is not allowed,
        // so hide errors...
        @putenv("LC_ALL={$setlocale}");
        @putenv("LANG={$loc}");
        @putenv("LANGUAGE={$loc}");
    }
    // To get the POSIX character classes in the PCRE's (e.g.
    // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
    // to set the locale, using setlocale().
    //
    // The problem is which locale to set?  We would like to recognize all
    // upper-case characters in the iso-8859-1 character set as upper-case
    // characters --- not just the ones which are in the current $LANG.
    //
    // As it turns out, at least on my system (Linux/glibc-2.2) as long as
    // you setlocale() to anything but "C" it works fine.  (I'm not sure
    // whether this is how it's supposed to be, or whether this is a bug
    // in the libc...)
    //
    // We don't currently use the locale setting for anything else, so for
    // now, just set the locale to US English.
    //
    // FIXME: Not all environments may support en_US?  We should probably
    // have a list of locales to try.
    if (setlocale(LC_CTYPE, 0) == 'C') {
        $x = setlocale(LC_CTYPE, 'en_US.' . $GLOBALS['charset']);
    } else {
        $x = setlocale(LC_CTYPE, $setlocale);
    }
    return $loc;
}
Example #2
0
/** 
 * Define constants which are client or request specific and should not be dumped statically.
 * Such as the language, and the virtual and server paths, which might be overridden 
 * by startup scripts for wiki farms.
 */
function fixup_dynamic_configs($file)
{
    global $WikiNameRegexp;
    global $HTTP_SERVER_VARS, $DBParams, $LANG;
    if (defined('INCLUDE_PATH') and INCLUDE_PATH) {
        @ini_set('include_path', INCLUDE_PATH);
    }
    if (defined('SESSION_SAVE_PATH') and SESSION_SAVE_PATH) {
        @ini_set('session.save_path', SESSION_SAVE_PATH);
    }
    if (!defined('DEFAULT_LANGUAGE')) {
        // not needed anymore
        define('DEFAULT_LANGUAGE', '');
    }
    // detect from client
    update_locale(isset($LANG) ? $LANG : DEFAULT_LANGUAGE);
    if (empty($LANG)) {
        if (!defined("DEFAULT_LANGUAGE") or !DEFAULT_LANGUAGE) {
            // TODO: defer this to WikiRequest::initializeLang()
            $LANG = guessing_lang();
            guessing_setlocale(LC_ALL, $LANG);
        } else {
            $LANG = DEFAULT_LANGUAGE;
        }
    }
    // Set up (possibly fake) gettext()
    // Todo: this could be moved to fixup_static_configs()
    // Bug #1381464 with php-5.1.1
    if (!function_exists('bindtextdomain') and !function_exists('gettext') and !function_exists('_')) {
        $locale = array();
        function gettext($text)
        {
            global $locale;
            if (!empty($locale[$text])) {
                return $locale[$text];
            }
            return $text;
        }
        function _($text)
        {
            return gettext($text);
        }
    } else {
        // Working around really weird gettext problems: (4.3.2, 4.3.6 win)
        // bindtextdomain() returns the current domain path.
        // 1. If the script is not index.php but something like "de", on a different path
        //    then bindtextdomain() fails, but after chdir to the correct path it will work okay.
        // 2. But the weird error "Undefined variable: bindtextdomain" is generated then.
        $bindtextdomain_path = FindFile("locale", false, true);
        $chback = 0;
        if (isWindows()) {
            $bindtextdomain_path = str_replace("/", "\\", $bindtextdomain_path);
        }
        $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain_path);
        if (realpath($bindtextdomain_real) != realpath($bindtextdomain_path)) {
            // this will happen with virtual_paths. chdir and try again.
            chdir($bindtextdomain_path);
            $chback = 1;
            $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain_path);
        }
        // tell gettext not to use unicode. PHP >= 4.2.0. Thanks to Kai Krakow.
        if (defined('CHARSET') and function_exists('bind_textdomain_codeset')) {
            @bind_textdomain_codeset("phpwiki", CHARSET);
        }
        textdomain("phpwiki");
        if ($chback) {
            // change back
            chdir($bindtextdomain_real . (isWindows() ? "\\.." : "/.."));
        }
    }
    // language dependent updates:
    $WikiNameRegexp = pcre_fix_posix_classes($WikiNameRegexp);
    //if ($KeywordLinkRegexp) $KeywordLinkRegexp = pcre_fix_posix_classes($KeywordLinkRegexp);
    if (!defined('CATEGORY_GROUP_PAGE')) {
        define('CATEGORY_GROUP_PAGE', _("CategoryGroup"));
    }
    if (!defined('WIKI_NAME')) {
        define('WIKI_NAME', _("An unnamed PhpWiki"));
    }
    if (!defined('HOME_PAGE')) {
        define('HOME_PAGE', _("HomePage"));
    }
    //////////////////////////////////////////////////////////////////
    // Autodetect URL settings:
    //
    foreach (array('SERVER_NAME', 'SERVER_PORT') as $var) {
        //FIXME: for CGI without _SERVER
        if (!defined($var) and !empty($HTTP_SERVER_VARS[$var])) {
            define($var, $HTTP_SERVER_VARS[$var]);
        }
    }
    if (!defined('SERVER_NAME')) {
        define('SERVER_NAME', '127.0.0.1');
    }
    if (!defined('SERVER_PORT')) {
        define('SERVER_PORT', 80);
    }
    if (!defined('SERVER_PROTOCOL')) {
        if (empty($HTTP_SERVER_VARS['HTTPS']) || $HTTP_SERVER_VARS['HTTPS'] == 'off') {
            define('SERVER_PROTOCOL', 'http');
        } else {
            define('SERVER_PROTOCOL', 'https');
        }
    }
    if (!defined('SCRIPT_NAME')) {
        define('SCRIPT_NAME', deduce_script_name());
    }
    if (!defined('USE_PATH_INFO')) {
        if (isCGI()) {
            define('USE_PATH_INFO', false);
        } else {
            /*
             * If SCRIPT_NAME does not look like php source file,
             * or user cgi we assume that php is getting run by an
             * action handler in /cgi-bin.  In this case,
             * I think there is no way to get Apache to pass
             * useful PATH_INFO to the php script (PATH_INFO
             * is used to the the php interpreter where the
             * php script is...)
             */
            switch (php_sapi_name()) {
                case 'apache':
                case 'apache2handler':
                    define('USE_PATH_INFO', true);
                    break;
                case 'cgi':
                case 'apache2filter':
                    define('USE_PATH_INFO', false);
                    break;
                default:
                    define('USE_PATH_INFO', ereg('\\.(php3?|cgi)$', SCRIPT_NAME));
                    break;
            }
        }
    }
    if (SERVER_PORT && SERVER_PORT != (SERVER_PROTOCOL == 'https' ? 443 : 80)) {
        define('SERVER_URL', SERVER_PROTOCOL . '://' . SERVER_NAME . ':' . SERVER_PORT);
    } else {
        define('SERVER_URL', SERVER_PROTOCOL . '://' . SERVER_NAME);
    }
    if (!defined('VIRTUAL_PATH')) {
        // We'd like to auto-detect when the cases where apaches
        // 'Action' directive (or similar means) is used to
        // redirect page requests to a cgi-handler.
        //
        // In cases like this, requests for e.g. /wiki/HomePage
        // get redirected to a cgi-script called, say,
        // /path/to/wiki/index.php.  The script gets all
        // of /wiki/HomePage as it's PATH_INFO.
        //
        // The problem is:
        //   How to detect when this has happened reliably?
        //   How to pick out the "virtual path" (in this case '/wiki')?
        //
        // (Another time an redirect might occur is to a DirectoryIndex
        // -- the requested URI is '/wikidir/', the request gets
        // passed to '/wikidir/index.php'.  In this case, the
        // proper VIRTUAL_PATH is '/wikidir/index.php', since the
        // pages will appear at e.g. '/wikidir/index.php/HomePage'.
        //
        $REDIRECT_URL =& $HTTP_SERVER_VARS['REDIRECT_URL'];
        if (USE_PATH_INFO and isset($REDIRECT_URL) and !IsProbablyRedirectToIndex()) {
            // FIXME: This is a hack, and won't work if the requested
            // pagename has a slash in it.
            $temp = strtr(dirname($REDIRECT_URL . 'x'), "\\", '/');
            if ($temp == '/' || $temp == '\\') {
                $temp = '';
            }
            define('VIRTUAL_PATH', $temp);
        } else {
            define('VIRTUAL_PATH', SCRIPT_NAME);
        }
    }
    if (!defined('PATH_INFO_PREFIX')) {
        if (VIRTUAL_PATH != SCRIPT_NAME) {
            // Apache action handlers are used.
            define('PATH_INFO_PREFIX', VIRTUAL_PATH . '/');
        } else {
            define('PATH_INFO_PREFIX', '/');
        }
    }
    define('PHPWIKI_BASE_URL', SERVER_URL . (USE_PATH_INFO ? VIRTUAL_PATH . '/' : SCRIPT_NAME));
    // Detect PrettyWiki setup (not loading index.php directly)
    // $SCRIPT_FILENAME should be the same as __FILE__ in index.php
    if (!isset($SCRIPT_FILENAME)) {
        $SCRIPT_FILENAME = @$HTTP_SERVER_VARS['SCRIPT_FILENAME'];
    }
    if (!isset($SCRIPT_FILENAME)) {
        $SCRIPT_FILENAME = @$HTTP_ENV_VARS['SCRIPT_FILENAME'];
    }
    if (!isset($SCRIPT_FILENAME)) {
        $SCRIPT_FILENAME = dirname(__FILE__ . '/../') . '/index.php';
    }
    if (isWindows()) {
        $SCRIPT_FILENAME = str_replace('\\\\', '\\', strtr($SCRIPT_FILENAME, '/', '\\'));
    }
    define('SCRIPT_FILENAME', $SCRIPT_FILENAME);
    // Get remote host name, if apache hasn't done it for us
    if (empty($HTTP_SERVER_VARS['REMOTE_HOST']) and !empty($HTTP_SERVER_VARS['REMOTE_ADDR']) and ENABLE_REVERSE_DNS) {
        $HTTP_SERVER_VARS['REMOTE_HOST'] = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
    }
}