Exemple #1
0
function _l10n_inject_js()
{
    $ltr = doSlash(gTxt('l10n-ltr'));
    $rtl = doSlash(gTxt('l10n-rtl'));
    $toggle_title = doSlash(gTxt('l10n-toggle'));
    $langs = MLPLanguageHandler::get_installation_langs();
    $langs = MLPLanguageHandler::do_fleshout_dirs($langs);
    $langs = _l10n_php2js_array('langs', $langs);
    $fn = <<<end_js
var {$langs}
\tvar search_box   = null;
\tvar search_term  = null;
\tvar result_div   = null;
\tvar result_list  = null;
\tvar cresult_div  = null;
\tvar result_num   = null;
\tvar csearch_box  = null;
\tvar csearch_lang = null;
\tvar str_edit_div = null;
\tvar sbn_lang_sel = null;
\tvar last_req     = "";

\tvar\txml_manager = false;
\tif( window.XMLHttpRequest )
\t\t{
\t\txml_manager = new XMLHttpRequest();
\t\t}

function addLoadEvent(func)
\t{
\tvar oldonload = window.onload;
\tif (typeof window.onload != 'function')
\t\t{
\t\twindow.onload = func;
\t\t}
\telse
\t\t{
\t\twindow.onload = function()
\t\t\t{
\t\t\toldonload();
\t\t\tfunc();
\t\t\t}
\t\t}
\t}

function l10nSetCookie(name, value, days)
\t{
\tif (days)
\t\t{
\t\tvar date = new Date();
\t\tdate.setTime(date.getTime() + (days*24*60*60*1000));
\t\tvar expires = '; expires=' + date.toGMTString();
\t\t}
\telse
\t\t{
\t\tvar expires = '';
\t\t}

\tdocument.cookie = name + '=' + value + expires + '; path=/';
\t}

function l10nGetCookie(name)
\t{
\tvar nameEQ = name + '=';

\tvar ca = document.cookie.split(';');

\tfor (var i = 0; i < ca.length; i++)
\t\t{
\t\tvar c = ca[i];

\t\twhile (c.charAt(0)==' ')
\t\t\t{
\t\t\tc = c.substring(1, c.length);
\t\t\t}

\t\tif (c.indexOf(nameEQ) == 0)
\t\t\t{
\t\t\treturn c.substring(nameEQ.length, c.length);
\t\t\t}
\t\t}

\treturn null;
\t}

addLoadEvent( function(){l10n_js_init();} );
function l10n_js_init()
\t{
\tif (!document.getElementById)
\t\t{
\t\treturn false;
\t\t}

\tsearch_box   = document.getElementById('l10n_search_by_name');
\tresult_div   = document.getElementById('l10n_div_sbn_result_list');
\tresult_num   = document.getElementById('l10n_result_count');
\tcsearch_box  = document.getElementById('l10n_search_by_content');
\tcresult_div  = document.getElementById('l10n_sbc_result_list');
\tcsearch_lang = document.getElementById('sbc_lang_selection');
\tstr_edit_div = document.getElementById('l10n_div_string_edit');
\tsbn_lang_sel = document.getElementById('sbn_lang_selection');

\tif( search_box == null )
\t\treturn;

\taddEvent( search_box , 'keyup' , l10nRefineResultsEventHandler , false );

\tvar search_type = getCookie( 'l10n_string_search_by' );
\tif( search_type == null || search_type == '' || search_type == 'name' )
\t\t{
\t\tvar selection = getCookie( 'l10n_string_search_by_subtype' );
\t\tif( selection == null || selection == 'all' )
\t\t\t{
\t\t\tsbn_lang_sel.disabled = true;
\t\t\tselection = '';
\t\t\t}
\t\telse
\t\t\t{
\t\t\tsbn_lang_sel.disabled = false;
\t\t\tselection = sbn_lang_sel.value;
\t\t\t}
\t\tdo_name_search( selection );
\t\t}
\telse
\t\t{
\t\tresult_div.className="l10n_hidden";
\t\tcresult_div.className="l10n_visible";
\t\tdo_content_search();
\t\t}
\t}

function l10nRefineResultsEventHandler(event)
\t{
\tl10nRefineResults();
\t}

function l10nRefineResults()
\t{
\tvar result_list  = document.getElementById('l10n_sbn_result_list');
\tvar target = trim( search_box.value );
\ttarget = target.toLowerCase()
\tvar t_len = target.length;

\t//
\t// Iterate over all strings showing those that match, hiding those that don't...
\t//
\tvar item = result_list.firstChild;
\tvar visible = 0;
\twhile( item != null )
\t\t{
\t\tvar match_text = item.id;
\t\tvar tmp = match_text.substring(0,t_len)
\t\tvar match = (tmp == target);

\t\tif( match )
\t\t\t{
\t\t\titem.className = 'l10n_visible';
\t\t\t++visible;
\t\t\t}
\t\telse
\t\t\t{
\t\t\titem.className = 'l10n_hidden';
\t\t\t}

\t\titem = item.nextSibling;
\t\t}
\tvar result_num   = document.getElementById('l10n_result_count');
\tresult_num.innerHTML = visible;
\tl10nSetCookie( 'search_string_name_live' , target , 365 );
\t}

function trim(term)
\t{
\tvar len = term.length;
\tvar lenm1 = len - 1;

\twhile (term.substring(0,1) == ' ')
\t\t{
\t\tterm = term.substring(1, term.length);
\t\t}
\twhile (term.substring(term.length-1, term.length) == ' ')
\t\t{
\t\tterm = term.substring(0,term.length-1);
\t\t}
\treturn term;
\t}

function make_xml_req(req,req_receiver)
\t{
\tif( !xml_manager || (req_receiver == null) )
\t\treturn false;

\tif( (last_req != req) && (req != '') )
\t\t{
\t\tif( xml_manager && xml_manager.readyState < 4 )
\t\t\t{
\t\t\txml_manager.abort();
\t\t\t}
\t\tif( window.ActiveXObject )
\t\t\t{
\t\t\txml_manager = new ActiveXObject("Microsoft.XMLHTTP");
\t\t\t}

\t\txml_manager.onreadystatechange = req_receiver;
\t\txml_manager.open("GET", req);
\t\txml_manager.send(null);
\t\tlast_req = req;
\t\t}
\t}

function do_name_search( lang )
\t{
\tvar req = "?event=l10n&tab=snippets&step=l10n_search_for_names&l10n-sfn=" + lang;
\tmake_xml_req( req , ns_result_handler );
\t}
function ns_result_handler()
\t{
\tif (xml_manager.readyState == 4)
\t\t{
\t\tvar results = xml_manager.responseText;
\t\tresult_div.innerHTML = results;
\t\tl10nRefineResults();
\t\t}
\t}

function do_content_search()
\t{
\tvar search_term = encodeURI(csearch_box.value);
\tvar search_lang = csearch_lang.value;
\tvar query       = search_term + search_lang;

\tif( search_term != '' )
\t\t{
\t\tvar req = "?event=l10n&tab=snippets&step=l10_search_for_content&l10n-sfc=" + search_term + "&l10n-lang=" + search_lang;
\t\tmake_xml_req( req , cs_result_handler );
\t\tl10nSetCookie( 'search_string_content' , search_term , 365 );
\t\tl10nSetCookie( 'search_string_lang' , search_lang , 365 );
\t\t}
\t}

function cs_result_handler()
\t{
\tif (xml_manager.readyState == 4)
\t\t{
\t\tvar results = xml_manager.responseText;
\t\tcresult_div.innerHTML = results;
\t\t}
\t}

function do_string_edit(id)
\t{
\tvar req = "?event=l10n&tab=snippets&XMLHTTP=true&id=" + id;
\tmake_xml_req( req , string_edit_handler );
\t}


function string_edit_handler()
\t{
\tif (xml_manager.readyState == 4)
\t\t{
\t\tvar results = xml_manager.responseText;
\t\tstr_edit_div.innerHTML = results;
        window.scrollTo(0,128);
\t\t}
\t}

function update_search( id )
\t{
\tvar by_name = document.getElementById('l10n_div_s_by_n');
\tvar by_cont = document.getElementById('l10n_div_s_by_c');
\tif( id == 'sbn_radio_button' )
\t\t{
\t\tby_name.className="l10n_visible";
\t\tby_cont.className="l10n_hidden";
\t\tresult_div.className="l10n_visible";
\t\tcresult_div.className="l10n_hidden";
\t\tl10nSetCookie( 'l10n_string_search_by' , 'name' , 365 );
\t\tvar selection = l10nGetCookie( 'l10n_string_search_by_subtype' );
\t\tif( selection == null || selection == 'all' )
\t\t\tselection = '';
\t\telse
\t\t\tselection = sbn_lang_sel.value;
\t\tdo_name_search( selection );
\t\t}
\telse if ( id == 'sbc_radio_button' )
\t\t{
\t\tby_name.className="l10n_hidden";
\t\tby_cont.className="l10n_visible";
\t\tresult_div.className="l10n_hidden";
\t\tcresult_div.className="l10n_visible";
\t\tl10nSetCookie( 'l10n_string_search_by' , 'cont' , 365 );
\t\tdo_content_search();
\t\t}
\telse if( id == 'sbn_missing_radio_button' )
\t\t{
\t\tsbn_lang_sel.disabled = false;
\t\tvar selection = sbn_lang_sel.value;
\t\tl10nSetCookie( 'l10n_string_search_by_subtype' , 'missing' , 365 );
\t\tdo_name_search( selection );
\t\t}
\telse if( id == 'sbn_all_radio_button' )
\t\t{
\t\tsbn_lang_sel.disabled = true;
\t\tl10nSetCookie( 'l10n_string_search_by_subtype' , 'all' , 365 );
\t\tdo_name_search( '' );
\t\t}
\t}
function on_sbn_lang_change()
\t{
\tvar selection = sbn_lang_sel.value;
\tl10nSetCookie( 'search_string_name_lang' , selection , 365 );
\tdo_name_search( selection );
\t}

function toggleTextElements()
\t{
\ttoggleDirection('title');
\ttoggleDirection('body');
\ttoggleDirection('excerpt');
\t}

function togglePreview()
\t{
\ttoggleDirection('article-main');
\t}

function toggleDirection(id)
\t{
\tif (!document.getElementById)
\t\t{
\t\treturn false;
\t\t}

\tvar textarea = document.getElementById(id + '-data');
\tif( textarea == null )
\t\ttextarea = document.getElementById(id);
\tvar toggler  = document.getElementById(id + '-toggle');

\tif (textarea.style.direction == 'ltr')
\t\t{
\t\ttextarea.style.direction = 'rtl';
\t\tif( toggler != null )
\t\t\ttoggler.innerHTML = '{$rtl}';
\t\t}
\telse
\t\t{
\t\ttextarea.style.direction = 'ltr';
\t\tif( toggler != null )
\t\t\ttoggler.innerHTML = '{$ltr}';
\t\t}
\t}
function resetToggleDir( id , dir )
\t{
\tvar e = document.getElementById(id);
\tif( e == null )
\t\treturn;

\te.style.direction = dir;
\t}
function on_lang_selection_change()
\t{
\tvar selection = document.getElementById('l10n_lang_selector').value;
\tvar dir = langs[selection];

\tl10nSetCookie( 'rendition_lang_selection' , selection , 365 );

\tresetToggleDir( 'title', dir );
\tresetToggleDir( 'body', dir );
\tresetToggleDir( 'excerpt', dir );
\tresetToggleDir( 'article-main', dir );

\tvar toggler = document.getElementById('title-toggle');
\tif( toggler != null )
\t\ttoggler.innerHTML = '{$toggle_title}';

\tvar toggler  = document.getElementById('article-main-toggle');
\tif( toggler != null )
\t\ttoggler.innerHTML = '{$toggle_title}';
\t}

end_js;
    return $fn;
}
 function setup_2()
 {
     global $l10n_wiz_upgrade, $l10n_default_strings_lang, $l10n_default_strings_perm, $l10n_default_strings, $prefs;
     #
     #	First things first, try to set the installation langs...
     #
     $gbp_l10n_key = 'gbp_admin_library_languages';
     $l10n_wiz_upgrade = array();
     $prev_gbp_l10n_langs = array();
     if (isset($prefs[$gbp_l10n_key])) {
         $prev_gbp_l10n_langs = $prefs[$gbp_l10n_key];
     }
     $prev_l10n_langs = $this->parent->preferences['l10n-languages']['value'];
     if (!empty($prev_gbp_l10n_langs)) {
         #
         #	Expand all two digit codes...
         #
         $langs = array();
         foreach ($prev_gbp_l10n_langs as $lang) {
             $lang = trim($lang);
             $len = strlen($lang);
             if (2 === $len) {
                 $ok = MLPLanguageHandler::iso_639_langs($lang, 'valid_short');
                 if ($ok) {
                     $lang = MLPLanguageHandler::iso_639_langs($lang, 'short2long');
                 }
             } elseif (5 === $len) {
                 $ok = MLPLanguageHandler::iso_639_langs($lang, 'valid_long');
             }
             if ($ok) {
                 $langs[] = $lang;
             }
         }
         $l10n_wiz_upgrade = $langs;
         $languages = $langs;
         @safe_delete('txp_prefs', "`name`='{$gbp_l10n_key}'");
     } elseif (!empty($prev_l10n_langs)) {
         # reinstall, keep old language settings.
         $languages = $prev_l10n_langs;
     } else {
         # fresh install, use all currently installed languages.
         $languages = MLPLanguageHandler::get_installation_langs();
     }
     $this->set_preference('l10n-languages', $languages);
     $this->add_report_item(gTxt('l10n-setup_2_langs', array('{langs}' => join(', ', $languages))), true);
     #
     #	Reset the session variable (in case of a language switch and then a reinstall)...
     #
     @session_start();
     $temp = LANG;
     $tmp = substr($temp, 0, 2);
     if (!empty($temp)) {
         $_SESSION['l10n_admin_short_lang'] = $tmp;
         $_SESSION['l10n_admin_long_lang'] = $temp;
     }
     # Adds the strings this class needs. These lines makes them editable via the "plugins" string tab.
     $l10n_default_strings = array_merge($l10n_default_strings, $l10n_default_strings_perm);
     $ok = MLPStrings::insert_strings($this->parent->strings_prefix, $l10n_default_strings, $l10n_default_strings_lang, 'admin', 'l10n');
     $this->add_report_item(gTxt('l10n-setup_2_main'), $ok);
     #
     #	Also add any strings we can for other installation languages...
     #
     $langs = MLPLanguageHandler::get_installation_langs();
     if (empty($langs)) {
         return;
     }
     $tmp_lang = $l10n_default_strings_lang;
     $tmp_str_perm = $l10n_default_strings_perm;
     $tmp_str_def = $l10n_default_strings;
     foreach ($langs as $lang) {
         if ($lang === $tmp_lang) {
             continue;
         }
         $merged = array();
         $file_name = txpath . DS . 'lib' . DS . 'l10n_' . $lang . '_strings.php';
         if (is_readable($file_name)) {
             include_once $file_name;
             $merged = array_merge($l10n_default_strings, $l10n_default_strings_perm);
             MLPStrings::insert_strings($this->parent->strings_prefix, $merged, $l10n_default_strings_lang, 'admin', 'l10n', true);
         }
     }
     if (isset($merged)) {
         unset($merged);
     }
     #
     #	Restore values...
     #
     $l10n_default_strings_lang = $tmp_lang;
     $l10n_default_strings_perm = $tmp_str_perm;
     $l10n_default_strings = $tmp_str_def;
 }
Exemple #3
0
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;
}