Fixes a bug in how locale tag guessed, also refactors some code from index.php to locale_function, a=chris

Chris Pollett [2011-10-27 16:Oct:th]
Fixes a bug in how locale tag guessed, also refactors some code from index.php to locale_function, a=chris
Filename
controllers/admin_controller.php
index.php
locale_functions.php
diff --git a/controllers/admin_controller.php b/controllers/admin_controller.php
index 78843201f..6208108de 100755
--- a/controllers/admin_controller.php
+++ b/controllers/admin_controller.php
@@ -186,20 +186,17 @@ class AdminController extends Controller implements CrawlConstants
         } else {
             $activity = "manageAccount";
         }
-
         $allowed = false;
         if(!PROFILE) {
             $allowed_activities = array( array(
                 "ACTIVITY_NAME" =>
                 $this->activityModel->getActivityNameFromMethodName($activity),
-                'METHOD_NAME' => $activity));
+                'METHOD_NAME' => $activity));
             $allowed = true;
         } else {
             $allowed_activities =
                  $this->userModel->getUserActivities($_SESSION['USER_ID']);
         }
-
-
         foreach($allowed_activities as $allowed_activity) {
             if($activity == $allowed_activity['METHOD_NAME']) {
                  $allowed = true;
diff --git a/index.php b/index.php
index 9e702d330..c5dd4d0c6 100755
--- a/index.php
+++ b/index.php
@@ -119,46 +119,7 @@ if(!PROFILE ) {
     $controller_name = "admin";
 }

-/* the request variable l and the browser's HTTP_ACCEPT_LANGUAGE
-   are used to determine the locale */
-if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
-    $l_parts = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
-    if(count($l_parts) > 0) {
-        $guess_l = $l_parts[0];
-    }
-    $guess_map = array(
-        "en" => "en-US",
-        "en-US" => "en-US",
-        "fr" => "fr-FR",
-        "ko" => "ko",
-        "in" => "in-ID",
-        "ja" => "ja",
-        "vi" => "vi-VN",
-        "vi-VN" => "vi-VN",
-        "zh" => "zh-CN",
-        "zh-CN" => "zh-CN"
-    );
-    if(isset($guess_map[$guess_l])) {
-        $guess_l = $guess_map[$guess_l];
-    }
-
-}
-
-if(isset($_SESSION['l']) || isset($_REQUEST['l']) || isset($guess_l)) {
-    $l = (isset($_REQUEST['l'])) ? $_REQUEST['l'] :
-        ((isset($_SESSION['l'])) ? $_SESSION['l'] : $guess_l);
-    if(strlen($l) < 10) {
-        $l= addslashes($l);
-        if(is_dir(LOCALE_DIR."/$l")) {
-
-            $locale_tag = $l;
-        }
-    }
-}
-
-if(!isset($locale_tag)) {
-    $locale_tag = DEFAULT_LOCALE;
-}
+$locale_tag = guessLocale();

 if(upgradeLocaleCheck()) {
     upgradeLocale();
diff --git a/locale_functions.php b/locale_functions.php
index 94f69112b..7b815c32c 100644
--- a/locale_functions.php
+++ b/locale_functions.php
@@ -38,6 +38,61 @@
  */
 require_once BASE_DIR."/models/locale_model.php";

+
+/**
+ *  Attempts to guess the user's locale based on the request, session,
+ *  and user-agent data
+ *
+ * @return string IANA language tag of the guessed locale
+ */
+function guessLocale()
+{
+    /* the request variable l and the browser's HTTP_ACCEPT_LANGUAGE
+       are used to determine the locale */
+    if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+        $l_parts = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+        if(count($l_parts) > 0) {
+            $guess_l = $l_parts[0];
+        }
+        $guess_map = array(
+            "en" => "en-US",
+            "en-us" => "en-US",
+            "en-US" => "en-US",
+            "fr" => "fr-FR",
+            "ko" => "ko",
+            "in" => "in-ID",
+            "ja" => "ja",
+            "vi" => "vi-VN",
+            "vi-vn" => "vi-VN",
+            "vi-VN" => "vi-VN",
+            "zh" => "zh-CN",
+            "zh-CN" => "zh-CN",
+            "zh-cn" => "zh-CN",
+        );
+        if(isset($guess_map[$guess_l])) {
+            $guess_l = $guess_map[$guess_l];
+        }
+
+    }
+
+    if(isset($_SESSION['l']) || isset($_REQUEST['l']) || isset($guess_l)) {
+        $l = (isset($_REQUEST['l'])) ? $_REQUEST['l'] :
+            ((isset($_SESSION['l'])) ? $_SESSION['l'] : $guess_l);
+        if(strlen($l) < 10) {
+            $l= addslashes($l);
+            if(is_dir(LOCALE_DIR."/$l")) {
+                $locale_tag = $l;
+            }
+        }
+    }
+
+    if(!isset($locale_tag)) {
+        $locale_tag = DEFAULT_LOCALE;
+    }
+
+    return $locale_tag;
+}
+
 /**
  * Translate the supplied arguments into the current locale.
  * This function takes a variable number of arguments. The first
ViewGit