forked from aureleduda/mqtranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtranslate_utils.php
executable file
·235 lines (210 loc) · 6.72 KB
/
mqtranslate_utils.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
<?php // encoding: utf-8
/* Copyright 2008 Qian Qin (email : mail@qianqin.de)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* mqTranslate Utilitys */
function qtrans_parseURL($url) {
$r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
$r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
preg_match ( $r, $url, $out );
$result = @array(
"scheme" => $out[1],
"host" => $out[4].(($out[5]=='')?'':':'.$out[5]),
"user" => $out[2],
"pass" => $out[3],
"path" => $out[6],
"query" => $out[7],
"fragment" => $out[8]
);
return $result;
}
function qtrans_stripSlashesIfNecessary($str) {
return get_magic_quotes_gpc() ? stripslashes($str) : $str;
}
function qtrans_insertDropDownElement($language, $url, $id){
global $q_config;
$html ="
var sb = document.getElementById('qtrans_select_".$id."');
var o = document.createElement('option');
var l = document.createTextNode('".$q_config['language_name'][$language]."');
";
if($q_config['language']==$language)
$html .= "o.selected = 'selected';";
$html .= "
o.value = '".addslashes(htmlspecialchars_decode($url, ENT_NOQUOTES))."';
o.appendChild(l);
sb.appendChild(o);
";
return $html;
}
function qtrans_getLanguage() {
global $q_config;
return $q_config['language'];
}
function qtrans_getLanguageName($lang = '') {
global $q_config;
if($lang=='' || !qtrans_isEnabled($lang)) $lang = $q_config['language'];
return $q_config['language_name'][$lang];
}
function qtrans_isEnabled($lang) {
global $q_config;
return in_array($lang, $q_config['enabled_languages']);
}
function qtrans_startsWith($s, $n) {
if(strlen($n)>strlen($s)) return false;
if($n == substr($s,0,strlen($n))) return true;
return false;
}
function qtrans_getAvailableLanguages($text) {
global $q_config;
$result = array();
$content = qtrans_split($text);
foreach($content as $language => $lang_text) {
$lang_text = trim($lang_text);
if(!empty($lang_text)) $result[] = $language;
}
if(sizeof($result)==0) {
// add default language to keep default URL
$result[] = $q_config['language'];
}
return $result;
}
function qtrans_isAvailableIn($post_id, $language='') {
global $q_config;
if($language == '') $language = $q_config['default_language'];
$post = &get_post($post_id);
$languages = qtrans_getAvailableLanguages($post->post_content);
return in_array($language,$languages);
}
function qtrans_convertDateFormatToStrftimeFormat($format) {
$mappings = array(
'd' => '%d',
'D' => '%a',
'j' => '%E',
'l' => '%A',
'N' => '%u',
'S' => '%q',
'w' => '%f',
'z' => '%F',
'W' => '%V',
'F' => '%B',
'm' => '%m',
'M' => '%b',
'n' => '%i',
't' => '%J',
'L' => '%k',
'o' => '%G',
'Y' => '%Y',
'y' => '%y',
'a' => '%P',
'A' => '%p',
'B' => '%K',
'g' => '%l',
'G' => '%L',
'h' => '%I',
'H' => '%H',
'i' => '%M',
's' => '%S',
'u' => '%N',
'e' => '%Q',
'I' => '%o',
'O' => '%O',
'U' => '%s',
'T' => '%v',
'Z' => '%1',
'c' => '%2',
'r' => '%3',
'P' => '%4'
);
$date_parameters = array();
$strftime_parameters = array();
$date_parameters[] = '#%#'; $strftime_parameters[] = '%%';
foreach($mappings as $df => $sf) {
$date_parameters[] = '#(([^%\\\\])'.$df.'|^'.$df.')#'; $strftime_parameters[] = '${2}'.$sf;
}
// convert everything
$format = preg_replace($date_parameters, $strftime_parameters, $format);
// remove single backslashes from dates
$format = preg_replace('#\\\\([^\\\\]{1})#','${1}',$format);
// remove double backslashes from dates
$format = preg_replace('#\\\\\\\\#','\\\\',$format);
return $format;
}
function qtrans_convertFormat($format, $default_format) {
global $q_config;
// check for multilang formats
$format = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($format);
$default_format = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($default_format);
switch($q_config['use_strftime']) {
case QT_DATE:
if($format=='') $format = $default_format;
return qtrans_convertDateFormatToStrftimeFormat($format);
case QT_DATE_OVERRIDE:
return qtrans_convertDateFormatToStrftimeFormat($default_format);
case QT_STRFTIME:
return $format;
case QT_STRFTIME_OVERRIDE:
return $default_format;
}
}
function qtrans_convertDateFormat($format) {
global $q_config;
if(isset($q_config['date_format'][$q_config['language']])) {
$default_format = $q_config['date_format'][$q_config['language']];
} elseif(isset($q_config['date_format'][$q_config['default_language']])) {
$default_format = $q_config['date_format'][$q_config['default_language']];
} else {
$default_format = '';
}
return qtrans_convertFormat($format, $default_format);
}
function qtrans_convertTimeFormat($format) {
global $q_config;
if(isset($q_config['time_format'][$q_config['language']])) {
$default_format = $q_config['time_format'][$q_config['language']];
} elseif(isset($q_config['time_format'][$q_config['default_language']])) {
$default_format = $q_config['time_format'][$q_config['default_language']];
} else {
$default_format = '';
}
return qtrans_convertFormat($format, $default_format);
}
function qtrans_formatCommentDateTime($format) {
global $comment;
return qtrans_strftime(qtrans_convertFormat($format, $format), mysql2date('U',$comment->comment_date), '', $before, $after);
}
function qtrans_formatPostDateTime($format) {
global $post;
return qtrans_strftime(qtrans_convertFormat($format, $format), mysql2date('U',$post->post_date), '', $before, $after);
}
function qtrans_formatPostModifiedDateTime($format) {
global $post;
return qtrans_strftime(qtrans_convertFormat($format, $format), mysql2date('U',$post->post_modified), '', $before, $after);
}
function qtrans_realURL($url = '') {
global $q_config;
return $q_config['url_info']['original_url'];
}
function qtrans_getSortedLanguages($reverse = false) {
global $q_config;
$languages = $q_config['enabled_languages'];
ksort($languages);
// fix broken order
$clean_languages = array();
foreach($languages as $lang) {
$clean_languages[] = $lang;
}
if($reverse) krsort($clean_languages);
return $clean_languages;
}
?>