From 803863571bf3282f98e345cf8c94da97422b7bfb Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev <simushevds@ossg.ru> Date: Fri, 11 Jul 2014 14:26:14 +0000 Subject: [PATCH] Replace const CURRENT_LOCALE with get_current_locale() func --- src/mibew/libs/chat.php | 12 ++-- .../Mibew/Controller/ButtonCodeController.php | 8 ++- .../Mibew/Controller/ButtonController.php | 2 +- .../Controller/CannedMessageController.php | 4 +- .../Mibew/Controller/Chat/MailController.php | 2 +- .../Localization/TranslationController.php | 2 +- .../TranslationImportController.php | 4 +- .../Controller/MailTemplateController.php | 4 +- .../Controller/PasswordRecoveryController.php | 2 +- .../RequestProcessor/ThreadProcessor.php | 10 +-- .../libs/classes/Mibew/Style/ChatStyle.php | 4 +- .../libs/classes/Mibew/Style/PageStyle.php | 4 +- src/mibew/libs/common/datetime.php | 11 +++- src/mibew/libs/common/locale.php | 63 +++++++++++-------- src/mibew/libs/groups.php | 4 +- src/mibew/libs/invitation.php | 8 +-- src/mibew/libs/operator.php | 2 +- 17 files changed, 82 insertions(+), 64 deletions(-) diff --git a/src/mibew/libs/chat.php b/src/mibew/libs/chat.php index 1dc277fc..a903d363 100644 --- a/src/mibew/libs/chat.php +++ b/src/mibew/libs/chat.php @@ -765,7 +765,7 @@ function chat_start_for_user( $thread->userName = $visitor_name; $thread->remote = $remote_host; $thread->referer = $referrer; - $thread->locale = CURRENT_LOCALE; + $thread->locale = get_current_locale(); $thread->userId = $visitor_id; $thread->userAgent = $user_browser; $thread->save(); @@ -786,7 +786,7 @@ function chat_start_for_user( getlocal( 'Visitor accepted invitation from operator {0}', array($operator_name), - CURRENT_LOCALE, + get_current_locale(), true ) ); @@ -794,7 +794,7 @@ function chat_start_for_user( if ($referrer) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('Vistor came from page {0}', array($referrer), CURRENT_LOCALE, true) + getlocal('Vistor came from page {0}', array($referrer), get_current_locale(), true) ); } if ($requested_operator && !$requested_operator_online) { @@ -803,14 +803,14 @@ function chat_start_for_user( getlocal( 'Thank you for contacting us. We are sorry, but requested operator <strong>{0}</strong> is offline. Another operator will be with you shortly.', array(get_operator_name($requested_operator)), - CURRENT_LOCALE, + get_current_locale(), true ) ); } else { $thread->postMessage( Thread::KIND_INFO, - getlocal('Thank you for contacting us. An operator will be with you shortly.', null, CURRENT_LOCALE, true) + getlocal('Thank you for contacting us. An operator will be with you shortly.', null, get_current_locale(), true) ); } } @@ -819,7 +819,7 @@ function chat_start_for_user( if ($info) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('Info: {0}', array($info), CURRENT_LOCALE, true) + getlocal('Info: {0}', array($info), get_current_locale(), true) ); } diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php index 8c230baa..b418dbff 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php @@ -96,8 +96,8 @@ class ButtonCodeController extends AbstractController $disable_invitation = false; if (!$lang || !in_array($lang, $image_locales)) { - $lang = in_array(CURRENT_LOCALE, $image_locales) - ? CURRENT_LOCALE + $lang = in_array(get_current_locale(), $image_locales) + ? get_current_locale() : $image_locales[0]; } @@ -120,7 +120,9 @@ class ButtonCodeController extends AbstractController $disable_invitation = true; if (!$lang || !in_array($lang, $locales_list)) { - $lang = in_array(CURRENT_LOCALE, $locales_list) ? CURRENT_LOCALE : $locales_list[0]; + $lang = in_array(get_current_locale(), $locales_list) + ? get_current_locale() + : $locales_list[0]; } $message = getlocal('Click to chat'); diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonController.php index e9985b60..e86d9c6c 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonController.php @@ -60,7 +60,7 @@ class ButtonController extends AbstractController $lang = ''; } if (!$lang || !locale_exists($lang)) { - $lang = CURRENT_LOCALE; + $lang = get_current_locale(); } $group_id = $request->query->get('group', ''); diff --git a/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php b/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php index d6a8be7a..69a47ca1 100644 --- a/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php +++ b/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php @@ -55,8 +55,8 @@ class CannedMessageController extends AbstractController // Get selected locale, if any. $lang = $this->extractLocale($request); if (!$lang) { - $lang = in_array(CURRENT_LOCALE, $all_locales) - ? CURRENT_LOCALE + $lang = in_array(get_current_locale(), $all_locales) + ? get_current_locale() : $all_locales[0]; } diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/MailController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/MailController.php index 725fa332..a9004c6c 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/MailController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/MailController.php @@ -111,7 +111,7 @@ class MailController extends AbstractController } // Load mail templates and substitute placeholders there. - $mail_template = mail_template_load('user_history', CURRENT_LOCALE); + $mail_template = mail_template_load('user_history', get_current_locale()); if (!$mail_template) { throw new \RuntimeException('Cannot load "user_history" mail template'); } diff --git a/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php b/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php index 2fdf31c7..4234f561 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php @@ -38,7 +38,7 @@ class TranslationController extends AbstractController $target = $request->query->get('target'); if (!preg_match("/^[\w-]{2,5}$/", $target)) { - $target = CURRENT_LOCALE; + $target = get_current_locale(); } $page = array( diff --git a/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationImportController.php b/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationImportController.php index 6d7c823a..c2c4b2a1 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationImportController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationImportController.php @@ -38,7 +38,7 @@ class TranslationImportController extends AbstractController $target = $request->request->get('target'); if (!preg_match("/^[\w-]{2,5}$/", $target)) { - $target = CURRENT_LOCALE; + $target = get_current_locale(); } $page = array( @@ -84,7 +84,7 @@ class TranslationImportController extends AbstractController $target = $request->request->get('target'); if (!preg_match("/^[\w-]{2,5}$/", $target)) { - $target = CURRENT_LOCALE; + $target = get_current_locale(); } // Validate uploaded file diff --git a/src/mibew/libs/classes/Mibew/Controller/MailTemplateController.php b/src/mibew/libs/classes/Mibew/Controller/MailTemplateController.php index 03fd7eb3..f6fefde4 100644 --- a/src/mibew/libs/classes/Mibew/Controller/MailTemplateController.php +++ b/src/mibew/libs/classes/Mibew/Controller/MailTemplateController.php @@ -208,8 +208,8 @@ class MailTemplateController extends AbstractController && preg_match("/^[\w-]{2,5}$/", $lang) && in_array($lang, $all_locales); if (!$correct_locale) { - $lang = in_array(CURRENT_LOCALE, $all_locales) - ? CURRENT_LOCALE + $lang = in_array(get_current_locale(), $all_locales) + ? get_current_locale() : $all_locales[0]; } diff --git a/src/mibew/libs/classes/Mibew/Controller/PasswordRecoveryController.php b/src/mibew/libs/classes/Mibew/Controller/PasswordRecoveryController.php index caf51755..d7fa8369 100644 --- a/src/mibew/libs/classes/Mibew/Controller/PasswordRecoveryController.php +++ b/src/mibew/libs/classes/Mibew/Controller/PasswordRecoveryController.php @@ -92,7 +92,7 @@ class PasswordRecoveryController extends AbstractController ); // Load mail templates and substitute placeholders there. - $mail_template = mail_template_load('password_recovery', CURRENT_LOCALE); + $mail_template = mail_template_load('password_recovery', get_current_locale()); if (!$mail_template) { throw new \RuntimeException('Cannot load "password_recovery" mail template'); } diff --git a/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php b/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php index 45fd9f86..ad73879b 100644 --- a/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php +++ b/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php @@ -503,7 +503,7 @@ class ThreadProcessor extends ClientSideProcessor if ($email) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('E-Mail: {0}', array($email), CURRENT_LOCALE, true) + getlocal('E-Mail: {0}', array($email), get_current_locale(), true) ); } @@ -606,7 +606,7 @@ class ThreadProcessor extends ClientSideProcessor $thread->userName = $name; $thread->remote = $remote_host; $thread->referer = $referrer; - $thread->locale = CURRENT_LOCALE; + $thread->locale = get_current_locale(); $thread->userId = $visitor['id']; $thread->userAgent = $user_browser; $thread->state = Thread::STATE_LEFT; @@ -617,19 +617,19 @@ class ThreadProcessor extends ClientSideProcessor if ($referrer) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('Vistor came from page {0}', array($referrer), CURRENT_LOCALE, true) + getlocal('Vistor came from page {0}', array($referrer), get_current_locale(), true) ); } if ($email) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('E-Mail: {0}', array($email), CURRENT_LOCALE, true) + getlocal('E-Mail: {0}', array($email), get_current_locale(), true) ); } if ($info) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('Info: {0}', array($info), CURRENT_LOCALE, true) + getlocal('Info: {0}', array($info), get_current_locale(), true) ); } $thread->postMessage(Thread::KIND_USER, $message, array('name' => $name)); diff --git a/src/mibew/libs/classes/Mibew/Style/ChatStyle.php b/src/mibew/libs/classes/Mibew/Style/ChatStyle.php index 7f829e9c..0f82f1b0 100644 --- a/src/mibew/libs/classes/Mibew/Style/ChatStyle.php +++ b/src/mibew/libs/classes/Mibew/Style/ChatStyle.php @@ -82,9 +82,9 @@ class ChatStyle extends AbstractStyle implements StyleInterface // Pass additional variables to template $data['mibewRoot'] = MIBEW_WEB_ROOT; $data['mibewVersion'] = MIBEW_VERSION; - $data['currentLocale'] = CURRENT_LOCALE; + $data['currentLocale'] = get_current_locale(); - $locale_info = get_locale_info(CURRENT_LOCALE); + $locale_info = get_locale_info(get_current_locale()); $data['rtl'] = $locale_info && $locale_info['rtl']; $data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath(); diff --git a/src/mibew/libs/classes/Mibew/Style/PageStyle.php b/src/mibew/libs/classes/Mibew/Style/PageStyle.php index e680a294..1db03039 100644 --- a/src/mibew/libs/classes/Mibew/Style/PageStyle.php +++ b/src/mibew/libs/classes/Mibew/Style/PageStyle.php @@ -82,9 +82,9 @@ class PageStyle extends AbstractStyle implements StyleInterface // Pass additional variables to template $data['mibewRoot'] = MIBEW_WEB_ROOT; $data['mibewVersion'] = MIBEW_VERSION; - $data['currentLocale'] = CURRENT_LOCALE; + $data['currentLocale'] = get_current_locale(); - $locale_info = get_locale_info(CURRENT_LOCALE); + $locale_info = get_locale_info(get_current_locale()); $data['rtl'] = $locale_info && $locale_info['rtl']; $data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath(); diff --git a/src/mibew/libs/common/datetime.php b/src/mibew/libs/common/datetime.php index 43cb1f43..610beb69 100644 --- a/src/mibew/libs/common/datetime.php +++ b/src/mibew/libs/common/datetime.php @@ -32,7 +32,7 @@ function date_diff_to_text($seconds) function get_month_selection($from_time, $to_time) { // Use correct months names and over translatable date/time strings. - $locale_info = get_locale_info(CURRENT_LOCALE); + $locale_info = get_locale_info(get_current_locale()); setlocale(LC_TIME, $locale_info['time_locale']); $start = getdate($from_time); @@ -92,12 +92,17 @@ function date_to_text($unixtime) * * @param int $timestamp Unix timestamp * @param string $format Format name. Can be one of "full", "date", "time". - * @param string $locale Locale code. + * @param string|null $locale Locale code. If null is passed in the current + * locale will be used. * @return string Formatted date. * @throws \InvalidArgumentException If $type argument has wrong value. */ -function format_date($timestamp, $format, $locale = CURRENT_LOCALE) +function format_date($timestamp, $format, $locale = null) { + if (is_null($locale)) { + $locale = get_current_locale(); + } + // Get locale info $locale_info = get_locale_info($locale); $date_format = $locale_info['date_format']; diff --git a/src/mibew/libs/common/locale.php b/src/mibew/libs/common/locale.php index bd611900..ee06d07d 100644 --- a/src/mibew/libs/common/locale.php +++ b/src/mibew/libs/common/locale.php @@ -47,11 +47,6 @@ define( : 'en') ); -/** - * Code of the current system locale - */ -define('CURRENT_LOCALE', get_locale()); - function locale_exists($locale) { return file_exists(MIBEW_FS_ROOT . "/locales/$locale/translation.po"); @@ -153,31 +148,42 @@ function get_user_locale() return 'en'; } -function get_locale() +/** + * Retrieves locale for the current request. + * + * @return string Locale code + */ +function get_current_locale() { - $locale = verify_param("locale", "/./", ""); + static $current_locale = null; - // Check if locale code passed in as a param is valid - $locale_param_valid = $locale - && locale_pattern_check($locale) - && locale_exists($locale); + if (is_null($current_locale)) { + $locale = verify_param("locale", "/./", ""); - // Check if locale code stored in session data is valid - $session_locale_valid = isset($_SESSION['locale']) - && locale_pattern_check($_SESSION['locale']) - && locale_exists($_SESSION['locale']); + // Check if locale code passed in as a param is valid + $locale_param_valid = $locale + && locale_pattern_check($locale) + && locale_exists($locale); - if ($locale_param_valid) { - $_SESSION['locale'] = $locale; - } elseif ($session_locale_valid) { - $locale = $_SESSION['locale']; - } else { - $locale = get_user_locale(); + // Check if locale code stored in session data is valid + $session_locale_valid = isset($_SESSION['locale']) + && locale_pattern_check($_SESSION['locale']) + && locale_exists($_SESSION['locale']); + + if ($locale_param_valid) { + $_SESSION['locale'] = $locale; + } elseif ($session_locale_valid) { + $locale = $_SESSION['locale']; + } else { + $locale = get_user_locale(); + } + + setcookie(LOCALE_COOKIE_NAME, $locale, time() + 60 * 60 * 24 * 1000, MIBEW_WEB_ROOT . "/"); + + $current_locale = $locale; } - setcookie(LOCALE_COOKIE_NAME, $locale, time() + 60 * 60 * 24 * 1000, MIBEW_WEB_ROOT . "/"); - - return $locale; + return $current_locale; } function get_locale_links() @@ -756,12 +762,17 @@ function read_locale_file($path) * * @param string $text A text which should be localized * @param array $params Indexed array with placeholders. - * @param string $locale Target locale code. + * @param string|null $locale Target locale code. If null is passed in the + * current locale will be used. * @param boolean $raw Indicates if the result should be sanitized or not. * @return string Localized text. */ -function getlocal($text, $params = null, $locale = CURRENT_LOCALE, $raw = false) +function getlocal($text, $params = null, $locale = null, $raw = false) { + if (is_null($locale)) { + $locale = get_current_locale(); + } + $string = get_localized_string($text, $locale); if ($params) { diff --git a/src/mibew/libs/groups.php b/src/mibew/libs/groups.php index c5d1c889..439f3bd7 100644 --- a/src/mibew/libs/groups.php +++ b/src/mibew/libs/groups.php @@ -72,7 +72,7 @@ function group_by_name($name) */ function get_group_name($group) { - if (HOME_LOCALE == CURRENT_LOCALE || !isset($group['vccommonname']) || !$group['vccommonname']) { + if (HOME_LOCALE == get_current_locale() || !isset($group['vccommonname']) || !$group['vccommonname']) { return $group['vclocalname']; } else { return $group['vccommonname']; @@ -242,7 +242,7 @@ function group_is_away($group) */ function get_group_description($group) { - $use_local_description = HOME_LOCALE == CURRENT_LOCALE + $use_local_description = HOME_LOCALE == get_current_locale() || !isset($group['vccommondescription']) || !$group['vccommondescription']; diff --git a/src/mibew/libs/invitation.php b/src/mibew/libs/invitation.php index 562e0c98..7f13e923 100644 --- a/src/mibew/libs/invitation.php +++ b/src/mibew/libs/invitation.php @@ -97,7 +97,7 @@ function invitation_invite($visitor_id, $operator) $thread->remote = $visitor_details['remote_host']; $thread->referer = $last_visited_page; // User's locale is unknown, set operator locale to the thread - $thread->locale = CURRENT_LOCALE; + $thread->locale = get_current_locale(); $thread->userId = $visitor['userid']; $thread->userAgent = $visitor_details['user_agent']; $thread->state = Thread::STATE_INVITED; @@ -122,13 +122,13 @@ function invitation_invite($visitor_id, $operator) getlocal( 'Operator {0} invites visitor at {1} page', array($operator_name, $last_visited_page), - CURRENT_LOCALE, + get_current_locale(), true ) ); $thread->postMessage( Thread::KIND_AGENT, - getlocal('Hello, how can I help you?', null, CURRENT_LOCALE, true), + getlocal('Hello, how can I help you?', null, get_current_locale(), true), array( 'name' => $operator_name, 'operator_id' => $operator['operatorid'], @@ -202,7 +202,7 @@ function invitation_reject($visitor_id) if ($thread) { $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('Visitor rejected invitation', null, CURRENT_LOCALE, true) + getlocal('Visitor rejected invitation', null, get_current_locale(), true) ); } diff --git a/src/mibew/libs/operator.php b/src/mibew/libs/operator.php index 7e049e9e..c7cb589b 100644 --- a/src/mibew/libs/operator.php +++ b/src/mibew/libs/operator.php @@ -509,7 +509,7 @@ function is_operator_online($operator_id) */ function get_operator_name($operator) { - if (HOME_LOCALE == CURRENT_LOCALE) { + if (HOME_LOCALE == get_current_locale()) { return $operator['vclocalename']; } else { return $operator['vccommonname'];