From fb295927a5f641aa89bd019bc17b53879f0ffd64 Mon Sep 17 00:00:00 2001 From: John Clause Date: Mon, 9 Feb 2015 00:10:58 -0700 Subject: [PATCH] 2.9.8.7 pre-release --- admin/admin_utils.php | 17 +- qtranslate.php | 7 +- qtranslate_configuration.php | 4 +- qtranslate_core.php | 612 ++++++++++++++++------------------- qtranslate_frontend.php | 2 + qtranslate_services.php | 165 ++++++---- readme.txt | 13 +- 7 files changed, 396 insertions(+), 424 deletions(-) diff --git a/admin/admin_utils.php b/admin/admin_utils.php index 0edb4e0..959731b 100644 --- a/admin/admin_utils.php +++ b/admin/admin_utils.php @@ -1,22 +1,25 @@ '.PHP_EOL; } diff --git a/qtranslate_core.php b/qtranslate_core.php index 28b7ba3..530481d 100644 --- a/qtranslate_core.php +++ b/qtranslate_core.php @@ -22,18 +22,7 @@ function qtranxf_init_language() { - global $q_config; - // check if it isn't already initialized - it is always called once only - //if(defined('QTRANX_INIT')){ echo "qtranxf_init_language second time"; return; } - //define('QTRANX_INIT',true); - //global $pagenow; //qtranxf_dbg_log('qtranxf_init_language: pagenow='.$pagenow); - //qtranxf_dbg_log('qtranxf_init_language: WP_ADMIN=',defined('WP_ADMIN')); - //qtranxf_dbg_log('qtranxf_init_language: DOING_AJAX=',defined('DOING_AJAX')); - //if(defined('DOING_AJAX')){ - // //qtranxf_dbg_log('qtranxf_init_language: _REQUEST=',$_REQUEST); - // //qtranxf_dbg_log('qtranxf_init_language: _GET=',$_GET); - // //qtranxf_dbg_log('qtranxf_init_language: _POST=',$_POST); - //} + global $q_config, $pagenow; qtranxf_loadConfig(); @@ -43,47 +32,53 @@ function qtranxf_init_language() { if(isset($_SERVER['SERVER_PORT']) && !empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT']!='80'){ $host.=':'.$_SERVER['SERVER_PORT']; } - //$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; - //qtranxf_dbg_log('qtranxf_init_language: IP='.$_SERVER['REMOTE_ADDR']); - //qtranxf_dbg_log('qtranxf_init_language: URL='.$host.$_SERVER['REQUEST_URI']); $url = $_SERVER['REQUEST_URI']; $url_info = array(); + + if(WP_DEBUG){ + $url_info['pagenow'] = $pagenow; + if(defined('WP_ADMIN')&&WP_ADMIN) $url_info['WP_ADMIN'] = true; + if(defined('DOING_AJAX')) $url_info['DOING_AJAX_POST'] = $_POST; + if(defined('DOING_CRON')) $url_info['DOING_CRON_POST'] = $_POST; + } + $url_info['host'] = $host; $url_info['url'] = $url; $url_info['original_url'] = $url;//move below for debugging purpose only $url_info['language'] = qtranxf_detect_language($url_info); - //qtranxf_dbg_log('qtranxf_init_language: url_info: ',$url_info); - - $q_config['url_info'] = apply_filters('qtranslate_url_info', $url_info);//slug redirection etc.? - $url_info = $q_config['url_info']; - - //qtranxf_dbg_log('qtranxf_init_language: url_info: ',$url_info); - - if( !defined('WP_ADMIN') && !defined('DOING_AJAX') && !defined('DOING_CRON') && isset($url_info['doredirect'])){ - $lang = $url_info['language']; - $scheme = isset($_SERVER['HTTPS'])?'https://':'http://'; - $urlorg = $scheme.$host.$url; - $urlstd = $scheme.$url_info['host'].$url_info['url']; - $urlnew = qtranxf_convertURL($urlstd,$lang); - $target = apply_filters('qtranslate_language_detect_redirect', $urlnew, $urlorg, $url_info); - //qtranxf_dbg_log('qtranxf_init_language: doredirect to '.$lang.PHP_EOL.'urlstd:'.$urlstd.PHP_EOL.'urlorg:'.$urlorg.PHP_EOL.'target:'.$target); - if($target!==false && $target != $urlorg){ - wp_redirect($target); - //header('Location: '.$target); - exit(); + $q_config['url_info'] = $url_info; + + if(isset($url_info['doredirect'])){ + if(!defined('WP_ADMIN') && !defined('DOING_AJAX') && !defined('DOING_CRON') && empty($_POST)){ + $lang = $url_info['language']; + $scheme = isset($_SERVER['HTTPS'])?'https://':'http://'; + $urlorg = $scheme.$host.$url; + $urlstd = $scheme.$url_info['host'].$url_info['url']; + $urlnew = qtranxf_convertURL($urlstd,$lang); + $target = apply_filters('qtranslate_language_detect_redirect', $urlnew, $urlorg, $url_info); + qtranxf_dbg_log('qtranxf_init_language: doredirect to '.$lang.PHP_EOL.'urlstd:'.$urlstd.PHP_EOL.'urlorg:'.$urlorg.PHP_EOL.'target:'.$target); + if($target!==false && $target != $urlorg){ + wp_redirect($target); + //header('Location: '.$target); + exit(); + }else{ + $url_info['doredirect'] .= ' - cancelled, because it goes to the same target.'; + } + }else{ + $url_info['doredirect'] .= ' - cancelled by WP_ADMIN or DOING_AJAX or DOING_CRON or not empty POST'; } } - $q_config['language'] = apply_filters('qtranslate_language', $url_info['language']); + $q_config['language'] = apply_filters('qtranslate_language', $url_info['language'], $url_info); // fix url to prevent xss $q_config['url_info']['url'] = qtranxf_convertURL(add_query_arg('lang',$q_config['default_language'],$q_config['url_info']['url'])); // Filter all options for language tags - if($q_config['doing_front_end']) { + if($q_config['url_info']['doing_front_end']) { $alloptions = wp_load_alloptions(); foreach($alloptions as $option => $value) { add_filter('option_'.$option, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage',0); @@ -104,181 +99,297 @@ function qtranxf_init_language() { //allow other plugins to initialize whatever they need for language do_action('qtranslate_init_language',$url_info); - -//qtranxf_dbg_log('qtranxf_init_language: url_info.url='.$q_config['url_info']['url']); -//qtranxf_dbg_log('qtranxf_init_language: language='.$q_config['language']); + qtranxf_dbg_log('qtranxf_init_language: url_info: ',$url_info); } +if(!function_exists('qtranxf_detect_language')){ function qtranxf_detect_language(&$url_info) { - global $q_config; - //qtranxf_dbg_log('qtranxf_detect_language: url_info passed: ',$url_info); - //$home = parse_url(get_option('home'));//qtranxf_dbg_log("home:",$home); - //$home['path'] = isset($home['path'])?trailingslashit($home['path']):'/'; - //$home['host'] = isset($home['host'])?$home['host']; - $home = qtranxf_parseURL(get_option('home')); - $home['path'] = isset($home['path']) ? trailingslashit($home['path']) : '/'; - $url_info['home'] = $home['path']; + global $q_config, $pagenow; + + $home_info = qtranxf_parseURL( get_option('home') ); + $home_info['path'] = isset($home_info['path']) ? trailingslashit($home_info['path']) : '/'; + $url_info['home'] = $home_info['path']; + + $site_info; + if(defined('WP_ADMIN')){ + $site_info = qtranxf_parseURL( get_option('siteurl') ); + $site_info['path'] = isset($site_info['path']) ? trailingslashit($site_info['path']) : '/'; + $url_info['url-home'] = $site_info['path']; + }else{ + $url_info['url-home'] = $url_info['home']; + } $lang = qtranxf_parse_language_info($url_info); - $q_config['doing_front_end'] = !defined('WP_ADMIN'); - //$q_config['doing_front_end'] defines if we are at front- or back-end. Why WP do not have that, or do they? - while(!$lang) - { - if( defined('DOING_AJAX') && isset($_SERVER['HTTP_REFERER']) ){ //try to get language from HTTP_REFERER - $http_referer = $_SERVER['HTTP_REFERER']; - if(WP_DEBUG){ - $url_info['http_referer'] = $http_referer; - //qtranxf_dbg_log('qtranxf_detect_language: http_referer=',$http_referer); - } - $referer = qtranxf_parseURL($http_referer); - if( $referer['host']==$url_info['host'] && qtranxf_startsWith($referer['path'],$home['path']) ) { - $url_info['internal_referer'] = true; - $referer['home'] = $home['path']; - $referer['url'] = $referer['path']; - if(!empty($referer['query'])) $referer['url'] .= '?'.$referer['query']; - //qtranxf_dbg_log('qtranxf_detect_language: referer_info: ',$referer); - $lang = qtranxf_parse_language_info($referer,true); - //qtranxf_dbg_log('qtranxf_detect_language: http_referer: lang=',$lang); - if($lang){ + if( (!$lang || !isset($url_info['doing_front_end'])) && defined('DOING_AJAX') && isset($_SERVER['HTTP_REFERER'])){ + //assert(WP_ADMIN); + //get language from HTTP_REFERER, if needed, and detect front- vs back-end + $http_referer = $_SERVER['HTTP_REFERER']; + $url_info['http_referer'] = $http_referer; + if(strpos($http_referer,'/wp-admin')!==FALSE){ + $url_info['referer_admin'] = true; + $url_info['doing_front_end'] = false; + }else{ + $ref_info = qtranxf_parseURL($http_referer); + if( $ref_info['host']==$url_info['host'] ) { + //determine $ref_info['url-home'] + if(!defined('WP_ADMIN')){ + $site_info = qtranxf_parseURL( get_option('siteurl') ); + $site_info['path'] = isset($site_info['path']) ? trailingslashit($site_info['path']) : '/'; + } + $home_path = $home_info['path']; + $site_path = $site_info['path']; + $home_path_len = strlen($home_path); + $site_path_len = strlen($site_path); + if($home_path_len > $site_path_len){ + if(qtranxf_startsWith($ref_info['path'],$home_path)){ + $ref_info['url-home'] = $home_path; + $ref_info['doing_front_end'] = true; + }elseif(qtranxf_startsWith($ref_info['path'],$site_path)){ + $ref_info['url-home'] = $site_path; + $ref_info['doing_front_end'] = false; + } + }elseif($home_path_len < $site_path_len){ + if(qtranxf_startsWith($ref_info['path'],$site_path)){ + $ref_info['url-home'] = $site_path; + $ref_info['doing_front_end'] = false; + }elseif(qtranxf_startsWith($ref_info['path'],$home_path)){ + $ref_info['url-home'] = $home_path; + $ref_info['doing_front_end'] = true; + } + }elseif($home_path != $site_path){ + if(qtranxf_startsWith($ref_info['path'],$home_path)){ + $ref_info['url-home'] = $home_path; + $ref_info['doing_front_end'] = true; + }elseif(qtranxf_startsWith($ref_info['path'],$site_path)){ + $ref_info['url-home'] = $site_path; + $ref_info['doing_front_end'] = false; + } + }else{//$home_path == $site_path + if(qtranxf_startsWith($ref_info['path'],$home_path)){ + $ref_info['url-home'] = $home_path; + } + } + if(isset($ref_info['url-home'])){ + $url_info['internal_referer'] = true; + $ref_info['url'] = $ref_info['path']; + if(!$lang || !(isset($url_info['doing_front_end']) || $ref_info['doing_front_end'])){ + if(!empty($ref_info['query'])) $ref_info['url'] .= '?'.$ref_info['query']; + $lang = qtranxf_parse_language_info($ref_info,true); + } $url_info['referer_language'] = $lang; - $q_config['doing_front_end'] = true; - }else{ - $url_info['referer_language'] = $q_config['default_language']; + if(!isset($url_info['doing_front_end']) && isset($ref_info['doing_front_end'])){ + $url_info['doing_front_end'] = $ref_info['doing_front_end']; + } } } - if(!$lang && !qtranxf_language_neutral_path($referer['path'])){ + if(!$lang && $q_config['hide_default_language'] + && isset($url_info['doing_front_end']) && $url_info['doing_front_end'] ) + { $lang = $q_config['default_language']; - $q_config['doing_front_end'] = true; } - //qtranxf_dbg_log('qtranxf_detect_language: DOING_AJAX: lang=',$lang); } - if($lang) break; + } + + //$url_info['doing_front_end'] defines if we are in front- or back-end. Why WP does not have that, or does it? + if(!isset($url_info['doing_front_end'])) $url_info['doing_front_end'] = !defined('WP_ADMIN'); - if( defined('WP_ADMIN') ){ - $lang = apply_filters('qtranslate_detect_admin_language',$lang,$url_info); - //qtranxf_dbg_log('qtranxf_detect_language: admin: lang=',$lang); + if(!$lang) { + if( $url_info['doing_front_end'] ){ + $lang = qtranxf_detect_language_front($url_info); + }else{ + $lang = qtranxf_detect_language_admin($url_info); } - if($lang) break; + } - if( !defined('WP_ADMIN') && isset($_COOKIE[QTX_COOKIE_NAME_FRONT]) ){ - $cs; + $url_info['language'] = $lang; + /** + * Hook for possible other methods + * Set $url_info['language'] with the result + */ + $url_info = apply_filters('qtranslate_detect_language', $url_info); + $lang = $url_info['language']; + if(!defined('DOING_AJAX')) qtranxf_set_language_cookie($lang); + return $lang; +} +} + +if(!function_exists('qtranxf_detect_language_admin')){ +function qtranxf_detect_language_admin(&$url_info) { + require_once(dirname(__FILE__).'/admin/admin_utils.php'); + $url_info = apply_filters('qtranslate_detect_admin_language',$url_info); + return $url_info['lang_admin']; +} +} + +if(!function_exists('qtranxf_detect_language_front')){ +function qtranxf_detect_language_front(&$url_info) { + global $q_config; + //assert($url_info['doing_front_end']); + while(true){ + if( isset($_COOKIE[QTX_COOKIE_NAME_FRONT]) ){ + $cs=null; $lang=qtranxf_resolveLangCase($_COOKIE[QTX_COOKIE_NAME_FRONT],$cs); - //qtranxf_dbg_log('qtranxf_detect_language: COOKIE: lang=',$lang); - if( $lang && (!$q_config['hide_default_language'] || $lang != $q_config['default_language']) ){ - $url_info['doredirect']=true; - } + $url_info['lang_cookie_front'] = $lang; + if($lang) break; } - if($lang) break; if($q_config['detect_browser_language'] - && ( !isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'],$home['host'])===FALSE ) ){ - $urlunslashed=untrailingslashit($url_info['url']); + && ( !isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'],$url_info['host'])===FALSE ) ){ + $urlunslashed=untrailingslashit($url_info['wp-path']); if(empty($urlunslashed)){ $lang=qtranxf_http_negotiate_language(); - //qtranxf_dbg_log('qtranxf_detect_language: browser: lang=',$lang); + $url_info['lang_browser'] = $lang; + if($lang) break; } } - if($lang) break; $lang = $q_config['default_language']; - if( !isset($url_info['doredirect']) - && !defined('WP_ADMIN') && !defined('DOING_CRON') && !defined('DOING_AJAX') - && (!$q_config['hide_default_language'] || $lang != $q_config['default_language']) - ){ - $language_neutral_path=qtranxf_language_neutral_path($url_info['path']); - //qtranxf_dbg_log('qtranxf_detect_language: language_neutral_path=',$language_neutral_path); - if(!$language_neutral_path){ - $url_info['doredirect']=true; - } - } break; } - - if(!defined('DOING_AJAX')) qtranxf_set_language_cookie($lang); - - //qtranxf_dbg_log('qtranxf_detect_language: url_info: result: ',$url_info); + if( !isset($url_info['doredirect']) + //&& !defined('WP_ADMIN') && !defined('DOING_CRON') && !defined('DOING_AJAX')//will check later + && (!$q_config['hide_default_language'] || $lang != $q_config['default_language']) + //&& !$url_info['language_neutral_path']//already so + ){ + $url_info['doredirect']='language needs to be shown in url'; + } return $lang; } +} /** - Expected: - - $url_info['url'] - - $url_info['host'] - - $url_info['home'] - + * Expects to be set before call: + * - $url_info['host'] + * - $url_info['url-home'] + * - $url_info['url'] */ +if(!function_exists('qtranxf_parse_language_info')){ function qtranxf_parse_language_info(&$url_info, $link=false) { global $q_config; + + $wp_path = substr($url_info['url'],strlen($url_info['url-home'])); + $url_info['wp-path'] = $wp_path ? $wp_path : ''; + $doredirect=false; - $lang_url=null; - if( !defined('WP_ADMIN') || $link ) - switch($q_config['url_mode']) { - case QTX_URL_PATH: // pre path - $count; - $url=preg_replace('#^'.$url_info['home'].'#','',$url_info['url'],-1,$count); - //qtranxf_dbg_log('qtranxf_parse_language_info: count='.$count.'; url='.$url.PHP_EOL.'url_info[url]='.$url_info['url']); - if( $count > 0 && $url ) { - // might have language information - if(preg_match("#^([a-z]{2})(/.*)?$#i",$url,$match)) { - $lang_url = qtranxf_resolveLangCase($match[1],$doredirect); - if($lang_url) $url_info['url'] = $url_info['home'].substr($url, 3); + + if( !defined('WP_ADMIN') || $link ){ + $lang = null; + switch($q_config['url_mode']) { + case QTX_URL_PATH: // pre path + if(preg_match('#^([a-z]{2})(/.*)?$#i',$url_info['wp-path'],$match)) { + $lang = qtranxf_resolveLangCase($match[1],$doredirect); + if($lang){ + $url_info['lang_url'] = $lang; + $url_info['url'] = $url_info['url-home'].substr($url_info['wp-path'],3); + $url_info['doing_front_end'] = true; + if(WP_DEBUG){ + $url_info['url_mode'] = 'pre-path'; + } + } } - } - break; - case QTX_URL_DOMAIN: // pre domain - $host=$url_info['host']; - if(!empty($host)) { - if(preg_match("#^([a-z]{2})\.#i",$host,$match)) { - $lang_url = qtranxf_resolveLangCase($match[1],$doredirect); - if($lang_url) $url_info['host'] = substr($host, 3); + //} + break; + case QTX_URL_DOMAIN: // pre domain + $host=$url_info['host']; + if(!empty($host)) { + if(preg_match("#^([a-z]{2})\.#i",$host,$match)) { + $lang = qtranxf_resolveLangCase($match[1],$doredirect); + if($lang){ + $url_info['lang_url'] = $lang; + $url_info['host'] = substr($host, 3); + $url_info['doing_front_end'] = true; + if(WP_DEBUG){ + $url_info['url_mode'] = 'pre-domain'; + } + } + } } - } - break; - case QTX_URL_DOMAINS: // domain per language - $host=$url_info['host']; - if(!empty($host)){ - foreach($q_config['enabled_languages'] as $lang){ - if(!isset($q_config['domains'][$lang])) continue; - if($q_config['domains'][$lang] != $host) continue; - $lang_url = $lang; - break; + break; + case QTX_URL_DOMAINS: // domain per language + $host=$url_info['host']; + if(!empty($host)){ + foreach($q_config['enabled_languages'] as $lang){//todo should have hash host->lang + if(!isset($q_config['domains'][$lang])) continue; + if($q_config['domains'][$lang] != $host) continue; + $url_info['lang_url'] = $lang; + if($lang != $q_config['default_language'] || strpos(get_option('siteurl'),$host) === FALSE){ + $url_info['doing_front_end'] = true; + } + if(WP_DEBUG){ + $url_info['url_mode'] = 'per-domain'; + } + break; + } } - } - break; - default: break; + break; + default: + /** + * Hook for possible other methods + * Set, if applicable: + * $url_info['lang_url'] + * $url_info['doing_front_end'] + * $url_info['url'] - convert to language neutral + */ + $url_info = apply_filters('qtranslate_parse_language_info_url_mode', $url_info, $q_config['url_mode'], $link); + break; + } } - //qtranxf_dbg_log('qtranxf_parse_language_info: lang_url:',$lang_url); $lang = null; - if( isset($_GET['lang']) && !$link ){ - $lang = qtranxf_resolveLangCase($_GET['lang'],$doredirect); - //qtranxf_dbg_log('qtranxf_parse_language_info: GET[lang]='.$lang); + if(!$link){ + if(isset($_GET['lang'])){ + $lang = qtranxf_resolveLangCase($_GET['lang'],$doredirect); + if($lang) $url_info['lang_query_get'] = $lang;//todo excessive? + }else if(isset($_POST['lang'])){ + $lang = qtranxf_resolveLangCase($_POST['lang'],$doredirect); + if($lang) $url_info['lang_query_post'] = $lang;//todo excessive? + } }else if(preg_match('/(&|&|&|\?)lang=([a-z]{2})/i',$url_info['url'],$match)) { - //qtranxf_dbg_log('qtranxf_parse_language_info: match:',$match); $lang = qtranxf_resolveLangCase($match[2],$doredirect); + if($lang) $url_info['lang_query_link'] = $lang;//todo excessive? } - //qtranxf_dbg_log('qtranxf_parse_language_info: lang_query:',$lang); + $url_info['lang_query'] = $lang; if($lang){ //$url_info['url'] = preg_replace("#(&|\?)lang=".$lang."&?#i","$1",$url_info['url']); //$url_info['url'] = preg_replace("#[\?\&]+$#i","",$url_info['url']); $url_info['url'] = preg_replace('/(&|&|&|\?)lang=[a-z]{2}(&|#)?/i',"$1",$url_info['url']); $url_info['url'] = preg_replace('/(&|&|&|\?)+$/','',$url_info['url']); - if($lang_url && $lang !== $lang_url) $doredirect=true; - if( $q_config['url_mode']!=QTX_URL_QUERY || ($lang_url && $lang !== $lang_url) ) $doredirect=true; - }else if($lang_url){ - $lang = $lang_url; - if($q_config['hide_default_language'] && $lang_url == $q_config['default_language']) $doredirect=true; + if(isset($url_info['lang_url'])){ + if($lang !== $lang_url) $doredirect=true; + }else{ + if( $q_config['url_mode'] != QTX_URL_QUERY ) $doredirect=true; + } + }else if(isset($url_info['lang_url'])){ + $lang = $url_info['lang_url']; + if($q_config['hide_default_language'] && $lang == $q_config['default_language']) $doredirect=true; } + if($lang) $url_info['language'] = $lang; + if($doredirect){ - $url_info['doredirect'] = true; + $url_info['doredirect'] = 'detected in parse_language_info'; } - //qtranxf_dbg_log('qtranxf_parse_language_info: lang=',$lang); - //qtranxf_dbg_log('qtranxf_parse_language_info: doredirect=',$doredirect); + + if(!isset($url_info['doing_front_end'])){ + $language_neutral_path=qtranxf_language_neutral_path($url_info['wp-path']); + $url_info['language_neutral_path'] = $language_neutral_path; + if(!$language_neutral_path){ + $url_info['doing_front_end'] = true; + } + } + + /** + * Hook for possible other methods + * Set $url_info['language'] with the result + */ + $url_info = apply_filters('qtranslate_parse_language_info', $url_info, $link);//slug? + + if(isset($url_info['language'])) $lang = $url_info['language']; return $lang; } +} function qtranxf_setcookie_language($lang, $cookie_name, $cookie_path) { @@ -296,190 +407,6 @@ function qtranxf_set_language_cookie($lang) } } - -/* -function qtranxf_get_language_cookie() -{ - if(defined('WP_ADMIN')){ - if(isset($_COOKIE[QTX_COOKIE_NAME_ADMIN])) return $_COOKIE[QTX_COOKIE_NAME_ADMIN]; - } - if(isset($_COOKIE[QTX_COOKIE_NAME_FRONT])) return $_COOKIE[QTX_COOKIE_NAME_FRONT]; - return false; -} - -function qtranxf_detect_language($url, $host, $referer) { - global $q_config; - //$home = parse_url(get_option('home'));//qtranxf_dbg_log("home:",$home); - //$home['path'] = isset($home['path'])?trailingslashit($home['path']):'/'; - //$home['host'] = isset($home['host'])?$home['host']; - $home = qtranxf_parseURL(get_option('home')); - $home['path'] = isset($home['path'])?trailingslashit($home['path']):'/'; - - $result = array(); - //$result['language'] = $q_config['default_language']; - $result['url'] = $url; - $result['original_url'] = $url; - $result['host'] = $host; - $result['redirect'] = false; - $result['internal_referer'] = false; - $result['referer'] = $referer; - $result['home'] = $home['path']; - - $referer = qtranxf_parseURL($referer); - - $site = qtranxf_parseURL(get_option('siteurl')); - $site['path'] = isset($site['path'])?trailingslashit($site['path']):'/'; - $language_neutral_path=$site['path']!=$home['path'] && qtranxf_startsWith($url,$site['path']); - if($language_neutral_path){ - $url = substr($url,strlen($site['path'])); - }else{ - $url = substr($url,strlen($home['path'])); - } - //$result['site_path']=$site['path']; - - $doredirect=false; - $lang_url=null; - switch($q_config['url_mode']) { - case QTX_URL_PATH: - // pre url - if(!$language_neutral_path && $url) { - // might have language information - if(preg_match("#^([a-z]{2})(/.*)?$#i",$url,$match)) { - $lang_url = qtranxf_resolveLangCase($match[1],$doredirect); - if($lang_url){ - // found language information - //$result['language'] = $lang_url; - $result['url'] = $home['path'].substr($url, 3); - } - } - } - break; - case QTX_URL_DOMAIN: - // pre domain - if($host) { - if(preg_match("#^([a-z]{2})\.#i",$host,$match)) { - $lang_url = qtranxf_resolveLangCase($match[1],$doredirect); - if($lang_url){ - // found language information - //$result['language'] = $lang_url; - $result['host'] = substr($host, 3); - } - } - } - default: break; - } - //qtranxf_dbg_log('qtranxf_detect_language: url='.$url); - //qtranxf_dbg_log('qtranxf_detect_language: lang_url='.$lang_url); - - // check if referrer is internal - if( $referer['host']==$result['host'] && qtranxf_startsWith($referer['path'], $home['path']) ) { - // user coming from internal link - $result['internal_referer'] = true; - } - - $lang=null; - if(isset($_GET['lang'])){// language override given - $lang = qtranxf_resolveLangCase($_GET['lang'],$doredirect); - if($lang){ - $result['url'] = preg_replace("#(&|\?)lang=".$lang."&?#i","$1",$result['url']); - $result['url'] = preg_replace("#[\?\&]+$#i","",$result['url']); - } elseif($home['host'] == $result['host'] && $home['path'] == $result['url']) { - if(empty($referer['host'])||!$q_config['hide_default_language']) { - $result['redirect'] = true; - }else{ - // check if activating language detection is possible - if(preg_match("#^([a-z]{2}).#i",$referer['host'],$match)) { - $cs=false; - $lang = qtranxf_resolveLangCase($match[1],$cs); - if($lang) { - // found language information - $referer['host'] = substr($referer['host'], 3); - } - } - if(!$result['internal_referer']) { - // user coming from external link - $result['redirect'] = true; - } - } - } - } - - if($lang){ - //qtranxf_dbg_log('qtranxf_detect_language: GET[lang]='.$lang); - if($lang_url && $lang !== $lang_url) $doredirect=true; - }else if($lang_url){ - $lang = $lang_url; - if($q_config['hide_default_language'] && $lang_url == $q_config['default_language']) - $doredirect=true; - }else{ - $lang=qtranxf_get_language_cookie(); - if($lang){ - $cs=false; - $lang=qtranxf_resolveLangCase($lang,$cs); - //qtranxf_dbg_log('qtranxf_detect_language: cookie: lang=',$lang); - } - - if(!$lang && $q_config['detect_browser_language']){ - $lang=qtranxf_http_negotiate_language(); - //qtranxf_dbg_log('qtranxf_detect_language: browser: lang=',$lang); - } - - if(!$lang) - $lang = $q_config['default_language']; - - if(!defined('WP_ADMIN') && !defined('DOING_CRON') - && (!$q_config['hide_default_language'] || $lang != $q_config['default_language']) - ){ - //qtranxf_dbg_log('qtranxf_detect_language: language_neutral_path=',$language_neutral_path); - if(!$language_neutral_path){ - $url_parsed=parse_url($url);//$url now does not have language information - //qtranxf_dbg_log('qtranxf_detect_language: url_parsed:',$url_parsed); - if(isset($url_parsed['path'])){ - $path=$url_parsed['path'];//$path does not have / in front - $language_neutral_path=qtranxf_language_neutral_path($path); - } - } - //qtranxf_dbg_log('qtranxf_detect_language: language_neutral_path=',$language_neutral_path); - if(!$language_neutral_path){ - //qtranxf_dbg_log('qtranxf_detect_language: ignored_file_type=false; path='.$path); - $doredirect=true; - } - } - } - - //qtranxf_dbg_log('doredirect=',$doredirect); - if($doredirect){ - $urlto=$result['host'].$result['url']; - if(isset($_SERVER['HTTPS'])) - $urlto='https://'.$urlto; - else - $urlto='http://'.$urlto; - //if(isset($_SERVER['QUERY_STRING'])) $url= - $target=qtranxf_convertURL($urlto,$lang,false,!$q_config['hide_default_language']); - $target = apply_filters('qtranslate_language_detect_redirect', $target, $result); - if($target!==false){ - //qtranxf_dbg_log('qtranxf_detect_language:doredirect: url='.$url); - //qtranxf_dbg_log('qtranxf_detect_language:doredirect: urlto='.$urlto); - //qtranxf_dbg_log('qtranxf_detect_language:doredirect: target='.$target); - //qtranxf_dbg_log('qtranxf_detect_language:doredirect: POST: ',$_POST); - $url_parsed=parse_url($url); - $tgt_parsed=parse_url($target); - $url_parsed_path=isset($url_parsed['path'])?rtrim($url_parsed['path'],'/?#'):''; - $tgt_parsed_path=isset($tgt_parsed['path'])?rtrim($tgt_parsed['path'],'/?#'):''; - if($url_parsed_path!=$tgt_parsed_path){ - qtranxf_set_language_cookie($lang); - wp_redirect($target); - //header('Location: '.$target); - exit(); - } - } - } - qtranxf_set_language_cookie($lang); - $result['language'] = $lang; - return $result; -} -*/ - function qtranxf_get_browser_language(){ //qtranxf_dbg_log('qtranxf_get_browser_language: HTTP_ACCEPT_LANGUAGE:',$_SERVER["HTTP_ACCEPT_LANGUAGE"]); if(!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) return null; @@ -554,7 +481,7 @@ function qtranxf_init() { // load plugin translations load_plugin_textdomain('qtranslate', false, dirname(plugin_basename( __FILE__ )).'/lang'); - if($q_config['doing_front_end']){ + if($q_config['url_info']['doing_front_end']){ // don't filter untranslated posts in admin if($q_config['hide_untranslated']){ add_filter('posts_where_request', 'qtranxf_excludeUntranslatedPosts',10,2); @@ -632,10 +559,11 @@ function qtranxf_load_option_array($nm) { $q_config[$nm]=$vals; } -function qtranxf_load_option_bool( $nm ) { +function qtranxf_load_option_bool( $nm, $default=null ) { global $q_config; $val = get_option('qtranslate_'.$nm); - if($val==='0') $q_config[$nm] = false; + if($val===FALSE){ if(!is_null($default)) $q_config[$nm] = $default; } + elseif($val==='0') $q_config[$nm] = false; elseif($val==='1') $q_config[$nm] = true; } diff --git a/qtranslate_frontend.php b/qtranslate_frontend.php index c51470d..b750398 100644 --- a/qtranslate_frontend.php +++ b/qtranslate_frontend.php @@ -134,6 +134,8 @@ function qtranxf_wp_get_nav_menu_items( $items, $menu, $args ) $item->post_title=qtranxf_use_language($language, $item->post_title, false, true); $item->post_excerpt=qtranxf_use_language($language, $item->post_excerpt, false, true); $item->description=qtranxf_use_language($language, $item->description, false, true); + if(isset($item->attr_title)) $item->attr_title=qtranxf_use_language($language, $item->attr_title, false, true); + //qtranxf_dbg_echo('qtranxf_wp_get_nav_menu_items: $item: ',$item); if($itemid<$item->ID) $itemid=$item->ID; if($menu_order<$item->menu_order) $menu_order=$item->menu_order; diff --git a/qtranslate_services.php b/qtranslate_services.php index 428b88d..8fad954 100644 --- a/qtranslate_services.php +++ b/qtranslate_services.php @@ -22,31 +22,13 @@ /* qTranslate Services */ -// generate public key -$qts_public_key = '-----BEGIN PUBLIC KEY-----|MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNccmB4Up9V9+vD5kWWiE6zpRV|m7y1sdFihreycdpmu3aPjKooG5LWUbTTyc993nTxV71SKuuYdkPzu5JxniAsI2N0|7DsySZ/bQ2/BEANNwJD3pmz4NmIHgIeNaUze/tvTZq6m+FTVHSvEqAaXJIsQbO19|HeegbfEpmCj1d/CgOwIDAQAB|-----END PUBLIC KEY-----|'; - -// OpenSSL functions used -$qts_openssl_functions_used = array( - 'openssl_pkey_new', - 'openssl_pkey_export', - 'openssl_pkey_get_details', - 'openssl_seal', - 'openssl_open', - 'openssl_free_key' - ); - -// check schedule -if (!wp_next_scheduled('qts_cron_hook')) { - wp_schedule_event( time(), 'hourly', 'qts_cron_hook' ); -} - -define('QTS_FAST_TIMEOUT', 10); -define('QTS_VERIFY', 'verify'); +define('QTS_FAST_TIMEOUT', 10); +define('QTS_VERIFY', 'verify'); define('QTS_GET_SERVICES', 'get_services'); define('QTS_INIT_TRANSLATION', 'init_translation'); define('QTS_RETRIEVE_TRANSLATION', 'retrieve_translation'); -define('QTS_QUOTE', 'quote'); -define('QTS_STATE_OPEN', 'open'); +define('QTS_QUOTE', 'quote'); +define('QTS_STATE_OPEN', 'open'); define('QTS_STATE_ERROR', 'error'); define('QTS_STATE_CLOSED', 'closed'); define('QTS_ERROR_INVALID_LANGUAGE', 'QTS_ERROR_INVALID_LANGUAGE'); @@ -57,23 +39,51 @@ define('QTS_ERROR_SERVICE_UNKNOWN', 'QTS_ERROR_SERVICE_UNKNOWN'); define('QTS_DEBUG','QTS_DEBUG'); -// error messages -$qts_error_messages[QTS_ERROR_INVALID_LANGUAGE] = __('The language/s do not have a valid ISO 639-1 representation.','qtranslate'); -$qts_error_messages[QTS_ERROR_NOT_SUPPORTED_LANGUAGE] = __('The language/s you used are not supported by the service.','qtranslate'); -$qts_error_messages[QTS_ERROR_INVALID_SERVICE] = __('There is no such service.','qtranslate'); -$qts_error_messages[QTS_ERROR_INVALID_ORDER] = __('The system could not process your order.','qtranslate'); -$qts_error_messages[QTS_ERROR_SERVICE_GENERIC] = __('There has been an error with the selected service.','qtranslate'); -$qts_error_messages[QTS_ERROR_SERVICE_UNKNOWN] = __('An unknown error occured with the selected service.','qtranslate'); -$qts_error_messages[QTS_DEBUG] = __('The server returned a debugging message.','qtranslate'); +/** runs once on file load */ +function qts_initialize() +{ + // generate public key + global $qts_public_key; + $qts_public_key = '-----BEGIN PUBLIC KEY-----|MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNccmB4Up9V9+vD5kWWiE6zpRV|m7y1sdFihreycdpmu3aPjKooG5LWUbTTyc993nTxV71SKuuYdkPzu5JxniAsI2N0|7DsySZ/bQ2/BEANNwJD3pmz4NmIHgIeNaUze/tvTZq6m+FTVHSvEqAaXJIsQbO19|HeegbfEpmCj1d/CgOwIDAQAB|-----END PUBLIC KEY-----|'; + + // OpenSSL functions used + global $qts_openssl_functions_used; + $qts_openssl_functions_used = array( + 'openssl_pkey_new', + 'openssl_pkey_export', + 'openssl_pkey_get_details', + 'openssl_seal', + 'openssl_open', + 'openssl_free_key' + ); + + // error messages + global $qts_error_messages; + $qts_error_messages[QTS_ERROR_INVALID_LANGUAGE] = __('The language/s do not have a valid ISO 639-1 representation.','qtranslate'); + $qts_error_messages[QTS_ERROR_NOT_SUPPORTED_LANGUAGE] = __('The language/s you used are not supported by the service.','qtranslate'); + $qts_error_messages[QTS_ERROR_INVALID_SERVICE] = __('There is no such service.','qtranslate'); + $qts_error_messages[QTS_ERROR_INVALID_ORDER] = __('The system could not process your order.','qtranslate'); + $qts_error_messages[QTS_ERROR_SERVICE_GENERIC] = __('There has been an error with the selected service.','qtranslate'); + $qts_error_messages[QTS_ERROR_SERVICE_UNKNOWN] = __('An unknown error occured with the selected service.','qtranslate'); + $qts_error_messages[QTS_DEBUG] = __('The server returned a debugging message.','qtranslate'); + + // check schedule + if (!wp_next_scheduled('qts_cron_hook')) { + wp_schedule_event( time(), 'hourly', 'qts_cron_hook' ); + } + qts_load(); +} +qts_initialize(); // hooks -add_action('qtranslate_css', 'qts_css'); +add_action('admin_menu', 'qts_init'); +//add_action('qtranslate_init', 'qts_init'); +add_action('qtranslate_admin_css', 'qts_css'); add_action('qts_cron_hook', 'qts_cron'); add_action('qtranslate_configuration', 'qts_config_hook', 10); add_action('qtranslate_loadConfig', 'qts_load'); add_action('qtranslate_saveConfig', 'qts_save'); add_action('qtranslate_clean_uri', 'qts_clean_uri'); -add_action('admin_menu', 'qts_init'); add_action('wp_ajax_qts_quote', 'qts_quote'); add_filter('manage_order_columns', 'qts_order_columns'); @@ -174,8 +184,7 @@ function qts_translateButtons($available_languages, $missing_languages) { } function qts_css() { -// +'; } function qts_load() { global $q_config, $qts_public_key; - $qtranslate_services = get_option('qtranslate_qtranslate_services'); - $qtranslate_services = qtranxf_validateBool($qtranslate_services, $q_config['qtranslate_services']); - $q_config['qtranslate_services'] = $qtranslate_services && qts_isOpenSSLAvailable(); - if($q_config['qtranslate_services'] && is_string($qts_public_key)) { + // qTranslate Services + //$q_config['qtranslate_services'] = false; + qtranxf_load_option_bool('qtranslate_services',false); + //$qtranslate_services = get_option('qtranslate_qtranslate_services'); + //$qtranslate_services = qtranxf_validateBool($qtranslate_services, $q_config['qtranslate_services']); + if(!$q_config['qtranslate_services']) return; + if(!qts_isOpenSSLAvailable()){ + $q_config['qtranslate_services'] = false; + return; + } + if(is_string($qts_public_key)) { $qts_public_key = openssl_get_publickey(join("\n",explode("|",$qts_public_key))); } } @@ -216,24 +231,23 @@ function qts_isOpenSSLAvailable() { function qts_init() { global $q_config; - if($q_config['qtranslate_services']) { + if(!$q_config['qtranslate_services']) return; /* disabled for meta box add_filter('qtranslate_toolbar', 'qts_toobar'); add_filter('qtranslate_modify_editor_js', 'qts_editor_js'); */ - add_meta_box('translatediv', __('Translate to','qtranslate'), 'qts_translate_box', 'post', 'side', 'core'); - add_meta_box('translatediv', __('Translate to','qtranslate'), 'qts_translate_box', 'page', 'side', 'core'); - - add_action('qtranslate_languageColumn', 'qts_translateButtons', 10, 2); - - // add plugin page without menu link for users with permission - if(current_user_can('edit_published_posts')) { - //add_posts_page(__('Translate','qtranslate'), __('Translate','qtranslate'), 'edit_published_posts', 'qtranslate_services', 'qts_service'); - global $_registered_pages; - $hookname = get_plugin_page_hookname('qtranslate_services', 'edit.php'); - add_action($hookname, 'qts_service'); - $_registered_pages[$hookname] = true; - } + add_meta_box('translatediv', __('Translate to','qtranslate'), 'qts_translate_box', 'post', 'side', 'core'); + add_meta_box('translatediv', __('Translate to','qtranslate'), 'qts_translate_box', 'page', 'side', 'core'); + + add_action('qtranslate_languageColumn', 'qts_translateButtons', 10, 2); + + // add plugin page without menu link for users with permission + if(current_user_can('edit_published_posts')) { + //add_posts_page(__('Translate','qtranslate'), __('Translate','qtranslate'), 'edit_published_posts', 'qtranslate_services', 'qts_service'); + global $_registered_pages; + $hookname = get_plugin_page_hookname('qtranslate_services', 'edit.php'); + add_action($hookname, 'qts_service'); + $_registered_pages[$hookname] = true; } } @@ -378,8 +392,8 @@ function qts_config_hook($request_uri) { -

qTranslate Services could not load OpenSSL!

'), 'http://www.php.net/manual/book.openssl.php'); } ?> - +

qTranslate Services could not load OpenSSL!

'), 'http://www.php.net/manual/book.openssl.php'); } ?> +
@@ -551,14 +565,14 @@ function qts_service() { printf(__('Post with id "%s" not found!','qtranslate'), $post_id); return; } - $default_service = intval(get_option('qts_default_service'),0); + $default_service = intval(get_option('qts_default_service'),5); $service_settings = get_option('qts_service_settings'); // Detect available Languages and possible target languages $available_languages = qtranxf_getAvailableLanguages($post->post_content); if(sizeof($available_languages)==0) { $error = __('The requested Post has no content, no Translation possible.', 'qtranslate'); } - + // try to guess source and target language if(!in_array($translate_from, $available_languages)) $translate_from = ''; $missing_languages = array_diff($q_config['enabled_languages'], $available_languages); @@ -593,12 +607,19 @@ function qts_service() { $post_excerpt = qtranxf_use($translate_from,$post->post_excerpt); if(!empty($translate_from)) $translate_from_name = $q_config['language_name'][$translate_from]; if(!empty($translate_to)) $translate_to_name = $q_config['language_name'][$translate_to]; + + $post_title_html = htmlspecialchars($post_title); + $permalink = get_permalink($post_id); + if($permalink){ + if($translate_from_name) $permalink = qtranxf_convertURL($translate_from_name,$permalink); + $post_title_html = ''.$post_title_html.''; + } if(!empty($translate_from) && !empty($translate_to)) { - $title = sprintf('Translate "%1$s" from %2$s to %3$s', htmlspecialchars($post_title), $translate_from_name, $translate_to_name); + $title = sprintf('Translate "%1$s" from %2$s to %3$s', $post_title_html, $translate_from_name, $translate_to_name); } elseif(!empty($translate_from)) { - $title = sprintf('Translate "%1$s" from %2$s', htmlspecialchars($post_title), $translate_from_name); + $title = sprintf('Translate "%1$s" from %2$s', $post_title_html, $translate_from_name); } else { - $title = sprintf('Translate "%1$s"', htmlspecialchars($post_title)); + $title = sprintf('Translate "%1$s"', $post_title_html); } // Check data @@ -768,6 +789,18 @@ function qts_service() {