aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-01-07 14:28:58 +0100
committerArthurHoaro <arthur@hoa.ro>2017-03-06 20:32:17 +0100
commit1255a42cfed9ce419962c6cf29181a66c7e22bb8 (patch)
treee92764508705da4780653d8789ad90273e80e476 /application/Utils.php
parent236239be752a7bb24547237b5751ac4fcbc0e549 (diff)
downloadShaarli-1255a42cfed9ce419962c6cf29181a66c7e22bb8.tar.gz
Shaarli-1255a42cfed9ce419962c6cf29181a66c7e22bb8.tar.zst
Shaarli-1255a42cfed9ce419962c6cf29181a66c7e22bb8.zip
Improve autoLocale() detection
- Creates arrays_combination function to cover all cases - add the underscore separator in the regex - add `utf8` encoding in addition to `UTF-8`
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php51
1 files changed, 42 insertions, 9 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 35d65224..19fb7116 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -216,23 +216,56 @@ function is_session_id_valid($sessionId)
216function autoLocale($headerLocale) 216function autoLocale($headerLocale)
217{ 217{
218 // Default if browser does not send HTTP_ACCEPT_LANGUAGE 218 // Default if browser does not send HTTP_ACCEPT_LANGUAGE
219 $attempts = array('en_US'); 219 $attempts = array('en_US', 'en_US.utf8', 'en_US.UTF-8');
220 if (isset($headerLocale)) { 220 if (isset($headerLocale)) {
221 // (It's a bit crude, but it works very well. Preferred language is always presented first.) 221 // (It's a bit crude, but it works very well. Preferred language is always presented first.)
222 if (preg_match('/([a-z]{2})-?([a-z]{2})?/i', $headerLocale, $matches)) { 222 if (preg_match('/([a-z]{2,3})[-_]?([a-z]{2})?/i', $headerLocale, $matches)) {
223 $loc = $matches[1] . (!empty($matches[2]) ? '_' . strtoupper($matches[2]) : ''); 223 $first = [strtolower($matches[1]), strtoupper($matches[1])];
224 $attempts = array( 224 $separators = ['_', '-'];
225 $loc.'.UTF-8', $loc, str_replace('_', '-', $loc).'.UTF-8', str_replace('_', '-', $loc), 225 $encodings = ['utf8', 'UTF-8'];
226 $loc . '_' . strtoupper($loc).'.UTF-8', $loc . '_' . strtoupper($loc), 226 if (!empty($matches[2])) {
227 $loc . '_' . $loc.'.UTF-8', $loc . '_' . $loc, $loc . '-' . strtoupper($loc).'.UTF-8', 227 $second = [strtoupper($matches[2]), strtolower($matches[2])];
228 $loc . '-' . strtoupper($loc), $loc . '-' . $loc.'.UTF-8', $loc . '-' . $loc 228 $attempts = arrays_combination([$first, $separators, $second, ['.'], $encodings]);
229 ); 229 } else {
230 $attempts = arrays_combination([$first, $separators, $first, ['.'], $encodings]);
231 }
230 } 232 }
231 } 233 }
232 setlocale(LC_ALL, $attempts); 234 setlocale(LC_ALL, $attempts);
233} 235}
234 236
235/** 237/**
238 * Combine multiple arrays of string to get all possible strings.
239 * The order is important because this doesn't shuffle the entries.
240 *
241 * Example:
242 * [['a'], ['b', 'c']]
243 * will generate:
244 * - ab
245 * - ac
246 *
247 * TODO PHP 5.6: use the `...` operator instead of an array of array.
248 *
249 * @param array $items array of array of string
250 *
251 * @return array Combined string from the input array.
252 */
253function arrays_combination($items)
254{
255 $out = [''];
256 foreach ($items as $item) {
257 $add = [];
258 foreach ($item as $element) {
259 foreach ($out as $key => $existingEntry) {
260 $add[] = $existingEntry . $element;
261 }
262 }
263 $out = $add;
264 }
265 return $out;
266}
267
268/**
236 * Generates a default API secret. 269 * Generates a default API secret.
237 * 270 *
238 * Note that the random-ish methods used in this function are predictable, 271 * Note that the random-ish methods used in this function are predictable,