forked from qtranslate/qtranslate-xt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtranslate_core.php
1634 lines (1434 loc) · 59.2 KB
/
qtranslate_core.php
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once( QTRANSLATE_DIR . '/modules/qtx_modules_handler.php' );
function qtranxf_init_language() {
global $q_config, $pagenow;
//qtranxf_dbg_log('1.qtranxf_init_language:');
qtranxf_loadConfig();
// 'url_info' hash is not for external use, it is subject to change at any time.
// 'url_info' is preserved on reloadConfig
if ( ! isset( $q_config['url_info'] ) ) {
$q_config['url_info'] = array();
}
$url_info = &$q_config['url_info'];
$url_info['cookie_enabled'] = isset( $_COOKIE[ QTX_COOKIE_NAME_FRONT ] ) || isset( $_COOKIE[ QTX_COOKIE_NAME_ADMIN ] );
if ( $url_info['cookie_enabled'] ) {
if ( isset( $_COOKIE[ QTX_COOKIE_NAME_FRONT ] ) ) {
$url_info['cookie_lang_front'] = $_COOKIE[ QTX_COOKIE_NAME_FRONT ];
}
if ( isset( $_COOKIE[ QTX_COOKIE_NAME_ADMIN ] ) ) {
$url_info['cookie_lang_admin'] = $_COOKIE[ QTX_COOKIE_NAME_ADMIN ];
}
}
if ( WP_DEBUG ) {
$url_info['pagenow'] = $pagenow;
$url_info['REQUEST_METHOD'] = isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : '';
if ( is_admin() ) {
$url_info['WP_ADMIN'] = true;
}
if ( wp_doing_ajax() ) {
$url_info['DOING_AJAX_POST'] = $_POST;
}
if ( wp_doing_cron() ) {
$url_info['DOING_CRON_POST'] = $_POST;
}
}
// fill url_info similarly to qtranxf_parseURL
$url_info['scheme'] = is_ssl() ? 'https' : 'http';
// see https://wordpress.org/support/topic/messy-wp-cronphp-command-line-output
$url_info['host'] = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '';
$url_info['path'] = strtok( $_SERVER['REQUEST_URI'], '?' );
if ( ! empty ( $_SERVER['QUERY_STRING'] ) ) {
$url_info['query'] = qtranxf_sanitize_url( $_SERVER['QUERY_STRING'] ); // to prevent xss
if ( isset( $_GET['qtranslate-mode'] ) && $_GET['qtranslate-mode'] == 'raw' ) {
$url_info['qtranslate-mode'] = 'raw';
$url_info['doing_front_end'] = true;
$q_config['url_info'] = $url_info;
$q_config['url_info']['language'] = $q_config['default_language'];
$q_config['language'] = $q_config['default_language'];
return;
}
}
// TODO fix qtranslate-slug still using 'original_url' field and remove it from here, this has no sense!
if ( defined( 'QTS_VERSION' ) ) {
$url_info['original_url'] = $_SERVER['REQUEST_URI'];
}
//qtranxf_dbg_log('qtranxf_init_language: SERVER: ',$_SERVER);
$url_info['language'] = qtranxf_detect_language( $url_info );
$q_config['language'] = apply_filters( 'qtranslate_language', $url_info['language'], $url_info );
//qtranxf_dbg_log('qtranxf_init_language: detected: url_info: ',$url_info);
if ( $q_config['url_info']['doing_front_end'] && qtranxf_can_redirect() ) {
$lang = $q_config['language'];
$url_orig = $url_info['scheme'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url_lang = qtranxf_convertURL( '', $lang ); // uses $q_config['url_info'] and caches information, which will be needed later anyway.
if ( ! isset( $url_info['doredirect'] ) && $url_orig != $url_lang ) {
$url_info['doredirect'] = '$url_orig != $url_lang';
}
if ( isset( $url_info['doredirect'] ) ) {
/**
* Filter provides a chance to alter redirect behaviour.
*
* @param string $url_lang proposed target URL for the active language to redirect to.
* @param string $url_orig original URL supplied to browser, which needs to be standardized.
* @param array $url_info a hash of various information parsed from original URL, coockies and other site configuration. The key names should be self-explanatory.
*
* @return mixed A new URL to be redirected to instead of $url_lang or "false" to cancel redirection.
*/
$target = apply_filters( 'qtranslate_language_detect_redirect', $url_lang, $url_orig, $url_info );
//qtranxf_dbg_log('qtranxf_init_language: doredirect to '.$lang.PHP_EOL .'urlorg:'.$url_orig.PHP_EOL .'target:'.$target.PHP_EOL .'url_info: ',$url_info);
if ( $target !== false && $target != $url_orig ) {
wp_redirect( $target );
nocache_headers(); // prevent browser from caching redirection
exit();
} else {
// neutral path
$url_info['doredirect'] .= ' - cancelled, because it goes to the same target - neutral URL';
if ( $pagenow == 'index.php' && $q_config['url_mode'] == QTX_URL_PATH ) {
$_SERVER['REQUEST_URI'] = trailingslashit( $url_info['path-base'] ) . $lang . $url_info['wp-path']; // should not hurt?
}
}
//qtranxf_dbg_log('qtranxf_init_language: doredirect canceled: $url_info: ',$url_info);
}
} elseif ( isset( $url_info['doredirect'] ) ) {
$url_info['doredirect'] .= ' - cancelled by can_redirect';
// this should never happen! We are now in a bad state.
assert( false, $url_info['doredirect'] );
}
// TODO clarify fix url to prevent xss - how does this prevents xss?
// $q_config['url_info']['url'] = qtranxf_convertURL(add_query_arg('lang',$q_config['default_language'],$q_config['url_info']['url']));
// qtranslate_hooks.php has to go before load_plugin_textdomain()
require_once( dirname( __FILE__ ) . '/qtranslate_hooks.php' );//common hooks moved here from qtranslate.php since 3.2.9.2, because they all need language already detected
// load plugin translations
// since 3.2-b3 moved it here as https://codex.wordpress.org/Function_Reference/load_plugin_textdomain seem to recommend to run load_plugin_textdomain in 'plugins_loaded' action, which is this function responds to
qtranxf_load_plugin_textdomain();
/**
* allow other plugins to initialize whatever they need before the fork between front and admin.
*/
do_action( 'qtranslate_load_front_admin', $url_info );
if ( $q_config['url_info']['doing_front_end'] ) {
require_once( QTRANSLATE_DIR . '/qtranslate_frontend.php' );
} else {
require_once( QTRANSLATE_DIR . '/admin/qtx_admin.php' );
}
apply_filters( 'wp_translator', null );//create QTX_Translator object
qtranxf_load_option_qtrans_compatibility();
QTX_Modules_Handler::load_modules_enabled();
/**
* allow other plugins and modules to initialize whatever they need for language
*/
do_action( 'qtranslate_init_language', $url_info );
//qtranxf_dbg_log('qtranxf_init_language: done: url_info: ',$url_info);
}
function qtranxf_detect_language( &$url_info ) {
global $q_config;
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
$siteinfo = qtranxf_get_site_info();
$url_info['path-base'] = $siteinfo['path'];
$url_info['path-base-length'] = $siteinfo['path-length'];
} else {
qtranxf_complete_url_info( $url_info );
if ( ! isset( $url_info['path-base'] ) ) {
// path did not match neither 'site' nor 'home', but it came to this code, then redirect to home.
// may happen for testing
$target = get_option( 'home' );
wp_redirect( $target );
exit();
}
$url_info['doing_front_end'] = true;
}
$lang = qtranxf_parse_language_info( $url_info );
// TODO check if we shouldn't generalize the referrer parsing to all cases, do we need all these limitations?
$parse_referrer = qtranxf_is_rest_request_expected() ||
( ( ! $lang || ! isset( $url_info['doing_front_end'] ) ) &&
( wp_doing_ajax() || ! $url_info['cookie_enabled'] ) );
// parse language and front info from HTTP_REFERER
if ( isset( $_SERVER['HTTP_REFERER'] ) && $parse_referrer ) {
$http_referer = $_SERVER['HTTP_REFERER'];
$url_info['http_referer'] = $http_referer;
// if needed, detect front- vs back-end
$parse_referrer_language = true;
if ( strpos( $http_referer, '/wp-admin' ) !== false ) {
$url_info['referer_admin'] = true;
if ( ! isset( $url_info['doing_front_end'] ) ) {
$url_info['doing_front_end'] = false;
}
if ( qtranxf_is_rest_request_expected() ) {
// TODO see if we can detect front better for REST to avoid overriding here
$url_info['doing_front_end'] = false;
} else {
// TODO check if language shouldn't be parsed for non-admin referrer as well, potentially a legacy bug!
$parse_referrer_language = false;
}
}
if ( $parse_referrer_language ) {
$ref_info = qtranxf_parseURL( $http_referer );
if ( ! qtranxf_external_host( $ref_info['host'] ) ) {
// determine $ref_info['path-base']
qtranxf_complete_url_info( $ref_info );
if ( isset( $ref_info['path-base'] ) ) {
// TODO remove internal_refer, not used
$url_info['internal_referer'] = true;
if ( ! $lang || ! ( isset( $url_info['doing_front_end'] ) || isset( $ref_info['doing_front_end'] ) ) ) {
$lang = qtranxf_parse_language_info( $ref_info, true );
if ( $lang === false ) {
unset( $url_info['internal_referer'] );
}
}
$url_info['referer_language'] = $lang;
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 && $q_config['hide_default_language']
&& isset( $url_info['doing_front_end'] ) && $url_info['doing_front_end'] ) {
$lang = $q_config['default_language'];
}
}
}
if ( ! isset( $url_info['doing_front_end'] ) ) {
$url_info['doing_front_end'] = ! is_admin();
}
if ( ! $lang ) {
$lang = $url_info['doing_front_end'] ? qtranxf_detect_language_front( $url_info ) : qtranxf_detect_language_admin( $url_info );
}
$url_info['language'] = $lang;
// REST calls should be deterministic (stateless), no special language detection e.g. based on cookie
$url_info['set_cookie'] = ! wp_doing_ajax() && ! qtranxf_is_rest_request_expected();
/**
* 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 ( $url_info['set_cookie'] ) {
qtranxf_set_language_cookie( $lang );
}
return $lang;
}
/**
* Parse language from the URL and/or query var, update url_info accordingly
*
* Expects to be set before call:
* - $url_info['host']
* - $url_info['path']
* - $url_info['query']
* - $url_info['path-base']
* - $url_info['path-base-length']
*
* @param array $url_info
* @param bool $link true when url_info concerns internal referrer (HTTP_REFERER)
*
* @return bool|string
*/
function qtranxf_parse_language_info( &$url_info, $link = false ) {
global $q_config;
qtranxf_complete_url_info_path( $url_info );
if ( ! isset( $url_info['wp-path'] ) ) {
return false; // url is not from this WP installation
}
$doredirect = false;
// parse URL lang
if ( ! is_admin() || $link ) {
$url_mode = $q_config['url_mode'];
switch ( $url_mode ) {
case QTX_URL_PATH:
if ( ! empty( $url_info['wp-path'] ) && 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['wp-path'] = substr( $url_info['wp-path'], 3 );
$url_info['doing_front_end'] = true;
}
}
break;
case QTX_URL_DOMAIN:
if ( ! empty( $url_info['host'] ) ) {
if ( preg_match( '#^([a-z]{2})\.#i', $url_info['host'], $match ) ) {
$lang = qtranxf_resolveLangCase( $match[1], $doredirect );
if ( $lang ) {
$url_info['lang_url'] = $lang;
$url_info['host'] = substr( $url_info['host'], 3 );
$url_info['doing_front_end'] = true;
}
}
}
break;
case QTX_URL_DOMAINS:
if ( ! empty( $url_info['host'] ) ) {
// TODO should 'enabled_languages' be defined as host->lang for domains?
foreach ( $q_config['enabled_languages'] as $lang ) {
if ( ! isset( $q_config['domains'][ $lang ] ) ) {
continue;
}
if ( $q_config['domains'][ $lang ] != $url_info['host'] ) {
continue;
}
$url_info['lang_url'] = $lang;
if ( $lang != $q_config['default_language'] || strpos( get_option( 'siteurl' ), $url_info['host'] ) === false ) {
$url_info['doing_front_end'] = true;
}
break;
}
}
break;
default:
// TODO why don't we parse query lang here as 'lang_url'?!
assert( $url_mode == QTX_URL_QUERY );
/**
* Hook for possible other methods
* Set, if applicable:
* $url_info['lang_url']
* $url_info['doing_front_end']
* $url_info['path'] - convert to language neutral or default
*/
// TODO why do we have this spooky hook only for query mode?!
$url_info = apply_filters( 'qtranslate_parse_language_info_mode', $url_info, $q_config['url_mode'] );
break;
}
}
// parse query lang (even for non query mode!)
$query_lang = false;
if ( ! $link ) {
if ( isset( $_GET['lang'] ) ) {
$query_lang = qtranxf_resolveLangCase( $_GET['lang'], $doredirect );
if ( $query_lang ) {
$url_info['lang_query_get'] = $query_lang; // only used in qtranxf_url_set_language
}
} else if ( isset( $_POST['lang'] ) ) {
$query_lang = qtranxf_resolveLangCase( $_POST['lang'], $doredirect );
}
} elseif ( ! empty( $url_info['query'] ) && preg_match( '/(^|&|&|&|\?)lang=([a-z]{2})/i', $url_info['query'], $match ) ) {
// checked for query mode, see https://github.com/qTranslate-Team/qtranslate-x/issues/288
$query_lang = qtranxf_resolveLangCase( $match[2], $doredirect );
}
$parsed_lang = false;
// don't allow query lang switch with REST request
if ( qtranxf_is_rest_request_expected() ) {
if ( isset( $url_info['lang_url'] ) ) {
$parsed_lang = $url_info['lang_url'];
} elseif ( $query_lang && ( $q_config['url_mode'] == QTX_URL_QUERY || $link ) ) {
// consider query lang for query mode or fallback for referrer links (from REST)
$parsed_lang = $query_lang;
}
// 'hide_default_language' should also be set in query mode
if ( ! isset( $parsed_lang ) && $q_config['hide_default_language'] ) {
$parsed_lang = $q_config['default_language'];
}
// TODO validate URL-case redirections
if ( $doredirect ) {
assert( isset( $parsed_lang ) );
$doredirect = false;
}
} else {
if ( $query_lang ) {
// query overrides URL lang for a language switch
$parsed_lang = $query_lang;
// TODO can we avoid removing query args?
qtranxf_del_query_arg( $url_info['query'], 'lang' );
if ( $q_config['url_mode'] != QTX_URL_QUERY && ! is_admin() ) {
// force lang switch from query var
$doredirect = true;
}
} elseif ( isset( $url_info['lang_url'] ) ) {
$parsed_lang = $url_info['lang_url'];
assert( $parsed_lang !== false );
if ( $q_config['hide_default_language'] && $parsed_lang == $q_config['default_language'] ) {
// default lang should not be part of the URL when hidden
$doredirect = true;
}
}
}
if ( $parsed_lang ) {
$url_info['language'] = $parsed_lang;
}
if ( $doredirect ) {
$url_info['doredirect'] = 'detected in parse_language_info';
}
if ( ! isset( $url_info['doing_front_end'] ) ) {
$url_info['language_neutral_path'] = qtranxf_language_neutral_path( $url_info['wp-path'] );
if ( ! $url_info['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 );
if ( isset( $url_info['language'] ) ) {
$parsed_lang = $url_info['language'];
}
assert( isset( $parsed_lang ) );
return $parsed_lang;
}
function qtranxf_detect_language_admin( &$url_info ) {
require_once( dirname( __FILE__ ) . '/admin/qtx_admin_utils.php' );
$url_info = apply_filters( 'qtranslate_detect_admin_language', $url_info );
return $url_info['lang_admin'];
}
function qtranxf_detect_language_front( &$url_info ) {
global $q_config;
$lang = null;
if ( isset( $_COOKIE[ QTX_COOKIE_NAME_FRONT ] ) ) {
$cs = null;
$lang = qtranxf_resolveLangCase( $_COOKIE[ QTX_COOKIE_NAME_FRONT ], $cs );
$url_info['lang_cookie_front'] = $lang;
}
if ( ! $lang && $q_config['detect_browser_language']
&& ( ! isset( $_SERVER['HTTP_REFERER'] ) || strpos( $_SERVER['HTTP_REFERER'], $url_info['host'] ) === false )//external referrer or no referrer
&& ( empty( $url_info['wp-path'] ) || $url_info['wp-path'] == '/' ) // home page is requested
) {
$lang = qtranxf_http_negotiate_language();
$url_info['lang_browser'] = $lang;
}
if ( ! $lang ) {
$lang = $q_config['default_language'];
}
if ( ! isset( $url_info['doredirect'] )
&& ( ! qtranxf_is_rest_request_expected() ) // fallback case where language can be read from cookie with REST
&& ( ! $q_config['hide_default_language'] || $lang != $q_config['default_language'] )
&& ( ! qtranxf_language_neutral_path( $url_info['wp-path'] ) )
) {
$url_info['doredirect'] = 'language needs to be shown in url';
}
return $lang;
}
function qtranxf_setcookie_language( $lang, $cookie_name, $cookie_path ) {
global $q_config;
// only meant for server-side, set 'httponly' flag
setcookie( $cookie_name, $lang, strtotime( '+1year' ), $cookie_path, null, $q_config['use_secure_cookie'], true );
}
function qtranxf_set_language_cookie( $lang ) {
global $q_config;
assert( ! qtranxf_is_rest_request_expected() );
if ( is_admin() ) {
qtranxf_setcookie_language( $lang, QTX_COOKIE_NAME_ADMIN, ADMIN_COOKIE_PATH );
} elseif ( ! $q_config['disable_client_cookies'] ) {
qtranxf_setcookie_language( $lang, QTX_COOKIE_NAME_FRONT, COOKIEPATH );
}
}
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;
}
if ( ! preg_match_all( "#([^;,]+)(;[^,0-9]*([0-9.]+)[^,]*)?#i", $_SERVER["HTTP_ACCEPT_LANGUAGE"], $matches, PREG_SET_ORDER ) ) {
return null;
}
$prefered_languages = array();
$priority = 1.0;
foreach ( $matches as $match ) {
if ( ! isset( $match[3] ) ) {
$pr = $priority;
$priority -= 0.001;
} else {
$pr = floatval( $match[3] );
}
$prefered_languages[ $match[1] ] = $pr;
}
arsort( $prefered_languages, SORT_NUMERIC );
//qtranxf_dbg_log('qtranxf_get_browser_language: prefered_languages:',$prefered_languages);
foreach ( $prefered_languages as $language => $priority ) {
$lang = qtranxf_match_language_locale( $language );
if ( $lang ) {
return $lang;
}
}
return null;
}
function qtranxf_http_negotiate_language() {
global $q_config;
if ( function_exists( 'http_negotiate_language' ) ) {
$default_language = $q_config['default_language'];
$supported = array();
$supported[] = qtranxf_html_locale( $q_config['locale'][ $default_language ] ); // needs to be the first
if ( ! empty( $q_config['locale_html'][ $default_language ] ) ) {
$supported[] = $q_config['locale_html'][ $default_language ];
}
foreach ( $q_config['enabled_languages'] as $lang ) {
if ( $lang == $default_language ) {
continue;
}
$supported[] = qtranxf_html_locale( $q_config['locale'][ $lang ] );
if ( ! empty( $q_config['locale_html'][ $lang ] ) ) {
$supported[] = $q_config['locale_html'][ $lang ];
}
}
$locale_negotiated = http_negotiate_language( $supported );
return qtranxf_match_language_locale( $locale_negotiated );
} else {
return qtranxf_get_browser_language();
}
}
function qtranxf_resolveLangCase( $lang, &$caseredirect ) {
if ( qtranxf_isEnabled( $lang ) ) {
return $lang;
}
$lng = strtolower( $lang );
if ( qtranxf_isEnabled( $lng ) ) {
$caseredirect = true;
return $lng;
}
$lng = strtoupper( $lang );
if ( qtranxf_isEnabled( $lng ) ) {
$caseredirect = true;
return $lng;
}
return false;
}
function qtranxf_load_option_qtrans_compatibility() {
global $q_config;
qtranxf_load_option_bool( 'qtrans_compatibility', false );
$q_config['qtrans_compatibility'] = apply_filters( 'qtranslate_compatibility', $q_config['qtrans_compatibility'] );
if ( ! isset( $q_config['qtrans_compatibility'] ) || ! $q_config['qtrans_compatibility'] ) {
return;
}
require_once( dirname( __FILE__ ) . '/qtranslate_compatibility.php' );
}
function qtranxf_load_plugin_textdomain() {
$domain = 'qtranslate';
$lang_dir = qtranxf_plugin_dirname() . '/lang';
if ( load_plugin_textdomain( $domain, false, $lang_dir ) ) {
return true;
}
return false;
}
/**
* Response to action 'init', which runs after user is authenticated.
* Currently unused.
* Is in use by 3rd-party plugins (for example, alo_easymail) to test q-X presence,
* which they should have done by testing "if ( defined( 'QTRANSLATE_FILE' )" instead.
* @since 3.4
*/
// TODO this function should be removed but some legacy plugins might still use this to check if q-XT is enabled...
function qtranxf_init() {
//qtranxf_dbg_log('3.qtranxf_init:');
}
function qtranxf_front_header_css_default() {
global $q_config;
$flag_location = qtranxf_flag_location();
$css = '';
foreach ( $q_config['enabled_languages'] as $lang ) {
$css .= '.qtranxs_flag_' . $lang . ' {background-image: url(' . $flag_location . $q_config['flag'][ $lang ] . '); background-repeat: no-repeat;}' . PHP_EOL;
}
return $css;
}
function qtranxf_flag_location() {
global $q_config;
return trailingslashit( content_url() ) . $q_config['flag_location'];
}
function qtranxf_flag_location_default() {
return qtranxf_plugin_dirname_from_wp_content() . '/flags/';
}
function qtranxf_load_option_flag_location( $nm ) {
global $q_config;
$default_value = qtranxf_flag_location_default();
$option_value = get_option( 'qtranslate_' . $nm );
if ( $option_value === false ) {
$q_config[ $nm ] = $default_value;
} else {
// url fix for upgrading users
$flag_location = trailingslashit( preg_replace( '#^wp-content/#', '', $option_value ) );
if ( file_exists( trailingslashit( WP_CONTENT_DIR ) . $flag_location ) && $default_value != $flag_location ) {
$q_config[ $nm ] = $flag_location;
} else {
$q_config[ $nm ] = $default_value;
delete_option( 'qtranslate_' . $nm );
}
}
}
function qtranxf_validateBool( $var, $default_value ) {
if ( $var === '0' ) {
return false;
} elseif ( $var === '1' ) {
return true;
} else {
return $default_value;
}
}
function qtranxf_load_option( $name, $default_value = null ) {
global $q_config, $qtranslate_options;
$val = get_option( 'qtranslate_' . $name );
if ( $val === false ) {
if ( is_null( $default_value ) ) {
if ( ! isset( $qtranslate_options['default_value'][ $name ] ) ) {
return;
}
$default_value = $qtranslate_options['default_value'][ $name ];
}
if ( is_string( $default_value ) && function_exists( $default_value ) ) {
$val = call_user_func( $default_value );
} else {
$val = $default_value;
}
}
$q_config[ $name ] = $val;
}
function qtranxf_load_option_array( $name, $default_value = null ) {
global $q_config;
$vals = get_option( 'qtranslate_' . $name );
if ( $vals === false ) {
if ( is_null( $default_value ) ) {
return;
}
if ( is_string( $default_value ) ) {
if ( function_exists( $default_value ) ) {
$vals = call_user_func( $default_value );
} else {
$vals = preg_split( '/[\s,]+/', $default_value, null, PREG_SPLIT_NO_EMPTY );
}
} else if ( is_array( $default_value ) ) {
$vals = $default_value;
}
}
if ( ! is_array( $vals ) ) {
return;
}
// clean up array due to previous configuration imperfections
foreach ( $vals as $key => $val ) {
if ( ! empty( $val ) ) {
continue;
}
unset( $vals[ $key ] );
if ( ! empty( $vals ) ) {
continue;
}
delete_option( 'qtranslate_' . $name );
break;
}
$q_config[ $name ] = $vals;
}
function qtranxf_load_option_bool( $name, $default_value = null ) {
global $q_config;
$val = get_option( 'qtranslate_' . $name );
if ( $val === false ) {
if ( ! is_null( $default_value ) ) {
$q_config[ $name ] = $default_value;
}
} else {
switch ( $val ) {
case '0':
$q_config[ $name ] = false;
break;
case '1':
$q_config[ $name ] = true;
break;
default:
$val = strtolower( $val );
switch ( $val ) {
case 'n':
case 'no':
$q_config[ $name ] = false;
break;
case 'y':
case 'yes':
$q_config[ $name ] = true;
break;
default:
$q_config[ $name ] = ! empty( $val );
break;
}
break;
}
}
}
function qtranxf_load_option_func( $name, $opn = null, $func = null ) {
global $q_config;
if ( ! $opn ) {
$opn = 'qtranslate_' . $name;
}
$val = get_option( $opn );
if ( $val === false ) {
if ( ! $func ) {
$func = 'qtranxf_default_' . $name;
}
$val = call_user_func( $func );
}
$q_config[ $name ] = $val;
}
function qtranxf_is_permalink_structure_query() {
$permalink_structure = get_option( 'permalink_structure' );
//qtranxf_dbg_echo('qtranxf_is_permalink_structure_query: ', $permalink_structure);
return empty( $permalink_structure ) || strpos( $permalink_structure, '?' ) !== false || strpos( $permalink_structure, 'index.php' ) !== false;
}
function qtranxf_loadConfig() {
global $qtranslate_options, $q_config;
qtranxf_set_default_options( $qtranslate_options );
$q_config = array();
qtranxf_load_option_func( 'default_language' );
qtranxf_load_option_array( 'enabled_languages' );
qtranxf_load_option_flag_location( 'flag_location' );
qtranxf_load_languages_enabled();
foreach ( $qtranslate_options['front']['int'] as $name => $def ) {
qtranxf_load_option( $name, $def );
}
foreach ( $qtranslate_options['front']['bool'] as $name => $def ) {
qtranxf_load_option_bool( $name, $def );
}
foreach ( $qtranslate_options['front']['str'] as $name => $def ) {
qtranxf_load_option( $name, $def );
}
foreach ( $qtranslate_options['front']['text'] as $name => $def ) {
qtranxf_load_option( $name, $def );
}
foreach ( $qtranslate_options['front']['array'] as $name => $def ) {
qtranxf_load_option_array( $name, $def );
}
qtranxf_load_option_array( 'term_name', array() );
if ( $q_config['filter_options_mode'] == QTX_FILTER_OPTIONS_LIST ) {
qtranxf_load_option_array( 'filter_options', QTX_FILTER_OPTIONS_DEFAULT );
}
$url_mode = $q_config['url_mode'];
// check for invalid permalink/url mode combinations
if ( qtranxf_is_permalink_structure_query() ) {
switch ( $url_mode ) {
case QTX_URL_QUERY:
case QTX_URL_DOMAIN:
case QTX_URL_DOMAINS:
break;
default:
$q_config['url_mode'] = $url_mode = QTX_URL_QUERY;
break;
}
}
switch ( $url_mode ) {
case QTX_URL_DOMAINS:
$q_config['domains'] = array();
qtranxf_load_option_array( 'domains' );
//qtranxf_dbg_echo('domains loaded: ',$q_config['domains']);
foreach ( $q_config['enabled_languages'] as $lang ) {
if ( isset( $q_config['domains'][ $lang ] ) ) {
continue;
}
$homeinfo = qtranxf_get_home_info();
$q_config['domains'][ $lang ] = $lang . '.' . $homeinfo['host'];
}
$q_config['disable_client_cookies'] = true;
$q_config['hide_default_language'] = false;
break;
case QTX_URL_QUERY:
case QTX_URL_PATH:
$q_config['disable_client_cookies'] = false;
qtranxf_load_option_bool( 'disable_client_cookies' );
break;
case QTX_URL_DOMAIN:
default:
$q_config['disable_client_cookies'] = true;
break;
}
$ignore_file_types = get_option( 'qtranslate_ignore_file_types' );
$val = explode( ',', QTX_IGNORE_FILE_TYPES );
if ( ! empty( $ignore_file_types ) ) {
$vals = preg_split( '/[\s,]+/', strtolower( $ignore_file_types ), null, PREG_SPLIT_NO_EMPTY );
foreach ( $vals as $v ) {
if ( empty( $v ) ) {
continue;
}
if ( in_array( $v, $val ) ) {
continue;
}
$val[] = $v;
}
}
$q_config['ignore_file_types'] = $val;
if ( empty( $q_config['front_config'] ) ) {
// TODO this should be granulated to load only what is needed
require_once( QTRANSLATE_DIR . '/admin/qtx_activation_hook.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_admin_options_update.php' );
qtranxf_update_i18n_config();
}
/**
* Opportunity to load additional front-end features.
*/
do_action( 'qtranslate_loadConfig' );
}
// check if it is a link to an ignored file type
function qtranxf_ignored_file_type( $path ) {
global $q_config;
//qtranxf_dbg_echo('qtranxf_ignored_file_type: pathinfo:',$pathinfo);
$i = strpos( $path, '?' );
if ( $i !== false ) {
$path = substr( $path, 0, $i );
}
$i = strpos( $path, '#' );
if ( $i !== false ) {
$path = substr( $path, 0, $i );
}
$i = strrpos( $path, '.' );
//qtranxf_dbg_echo('qtranxf_ignored_file_type: path='.$path.'; i='.$i);
if ( $i === false ) {
return false;
}
$ext = substr( $path, $i + 1 );
//qtranxf_dbg_echo('qtranxf_ignored_file_type: ext='.$ext);
return in_array( $ext, $q_config['ignore_file_types'] );
}
function qtranxf_language_neutral_path( $path ) {
//qtranxf_dbg_log('qtranxf_language_neutral_path: path='.$path);
if ( empty( $path ) ) {
return false;
}
static $language_neutral_path_cache;
if ( isset( $language_neutral_path_cache[ $path ] ) ) {
//qtranxf_dbg_log('qtranxf_language_neutral_path: cached='.$language_neutral_path_cache[$path].': path='.$path);
return $language_neutral_path_cache[ $path ];
}
if ( preg_match( '#^/(wp-.*\.php|wp-login/|wp-admin/|xmlrpc.php|robots.txt|oauth/)#', $path ) ) {
$language_neutral_path_cache[ $path ] = true;
//qtranxf_dbg_log('qtranxf_language_neutral_path: preg_match: path='.$path);
return true;
}
if ( qtranxf_ignored_file_type( $path ) ) {
$language_neutral_path_cache[ $path ] = true;
//qtranxf_dbg_log('qtranxf_language_neutral_path: file_type: path='.$path);
return true;
}
$language_neutral_path_cache[ $path ] = false;
return false;
}
/**
* @since 3.0
*/
function qtranxf_url_del_language( &$urlinfo ) {
global $q_config;
if ( ! empty( $urlinfo['query'] ) ) {
$query = &$urlinfo['query'];
// & workaround
if ( strpos( $query, '&' ) !== false ) {
$query = str_replace( '&', '&', $query );
$urlinfo['query_amp'] = true;
}
if ( strpos( $urlinfo['query'], '&' ) !== false ) {
$query = str_replace( '&', '&', $query );
$urlinfo['query_amp'] = true;
}
qtranxf_del_query_arg( $query, 'lang' );
}
$url_mode = $q_config['url_mode'];
switch ( $url_mode ) {
case QTX_URL_PATH:
// might already have language information
if ( ! empty( $urlinfo['wp-path'] ) && preg_match( '!^/([a-z]{2})(/|$)!i', $urlinfo['wp-path'], $match ) ) {
if ( qtranxf_isEnabled( $match[1] ) ) {
// found language information, remove it
$urlinfo['wp-path'] = substr( $urlinfo['wp-path'], 3 );
}
}
break;
case QTX_URL_DOMAIN:
// remove language information
$homeinfo = qtranxf_get_home_info();
$urlinfo['host'] = $homeinfo['host'];
break;
case QTX_URL_DOMAINS:
$urlinfo['host'] = $q_config['domains'][ $q_config['default_language'] ];
break;
case QTX_URL_QUERY:
break;
default:
$urlinfo = apply_filters( 'qtranslate_url_del_language', $urlinfo, $url_mode );
break;
}
//qtranxf_dbg_log('qtranxf_url_del_language: $urlinfo:',$urlinfo);
}
function qtranxf_url_set_language( $urlinfo, $lang, $showLanguage ) {
global $q_config;
$urlinfo = qtranxf_copy_url_info( $urlinfo );
//qtranxf_dbg_log('qtranxf_url_set_language: $urlinfo:',$urlinfo);
if ( $showLanguage ) {
$url_mode = $q_config['url_mode'];
switch ( $url_mode ) {
case QTX_URL_PATH:
//qtranxf_dbg_log_if(!isset($urlinfo['wp-path']),'qtranxf_url_set_language: wp-path not set $urlinfo:',$urlinfo,true);
$urlinfo['wp-path'] = '/' . $lang . $urlinfo['wp-path'];
break;
case QTX_URL_DOMAIN:
$urlinfo['host'] = $lang . '.' . $urlinfo['host'];
//qtranxf_dbg_log('qtranxf_url_set_language: QTX_URL_DOMAIN: urlinfo:',$urlinfo);
break;
case QTX_URL_DOMAINS:
$urlinfo['host'] = $q_config['domains'][ $lang ];
break;
case QTX_URL_QUERY:
qtranxf_add_query_arg( $urlinfo['query'], 'lang=' . $lang );
break;
}
$urlinfo = apply_filters( 'qtranslate_url_set_language', $urlinfo, $lang, $url_mode );
}
// see if cookies are activated
if ( ! $showLanguage//there still is no language information in the converted URL
&& ! $q_config['url_info']['cookie_enabled']// there will be no way to take language from the cookie
&& $q_config['language'] != $q_config['default_language']//we need to be able to get language other than default
&& empty( $q_config['url_info']['lang_url'] )//we will not be able to get language from referrer path
&& empty( $q_config['url_info']['lang_query_get'] )//we will not be able to get language from referrer query
) {
// :( now we have to make unpretty URLs
qtranxf_add_query_arg( $urlinfo['query'], 'lang=' . $lang );
}
// & workaround
if ( isset( $urlinfo['query_amp'] ) ) {
$urlinfo['query'] = str_replace( '&', '&', $urlinfo['query'] );
}