KaiRo bug 399 - Move Auth Service to using php-utility-classes
[authserver.git] / app / authutils.php-class
index 44152a43ea64c3712a1b217dcee21f6ce6c8e888..e5a080b0c6c77ffd6c8e2b3545348ab625eb4ef6 100755 (executable)
@@ -82,6 +82,9 @@ class AuthUtils {
   // function pwdNeedsRehash($user)
   //   Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed.
   //
+  // function negotiateLocale($supportedLanguages)
+  //   Return the language to use out of the given array of supported locales, via netotiation based on the HTTP Accept-Language header.
+  //
   // function getGroupedEmails($group_id, [$exclude_email])
   //   Return all emails grouped in the specified group ID, optionally exclude a specific email (e.g. because you only want non-current entries)
   //
@@ -192,14 +195,14 @@ class AuthUtils {
       }
       else {
         $utils->log('create_session_failure', 'at login, prev session: '.$session['id'].', new user: '.$userid);
-        $errors[] = _('The session system is not working. Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
+        $errors[] = _('The session system is not working.').' '._('Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
       }
     }
     else {
       $result = $this->db->prepare('UPDATE `auth_sessions` SET `sesskey` = :sesskey, `user` = :userid, `logged_in` = TRUE, `time_expire` = :expire WHERE `id` = :sessid;');
       if (!$result->execute(array(':sesskey' => $sesskey, ':userid' => $userid, ':expire' => gmdate('Y-m-d H:i:s', strtotime('+1 day')), ':sessid' => $session['id']))) {
         $utils->log('login_failure', 'session: '.$session['id'].', user: '.$userid);
-        $errors[] = _('Login failed unexpectedly. Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
+        $errors[] = _('Login failed unexpectedly.').' '._('Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
       }
       else {
         // After update, actually fetch the session row from the DB so we have all values.
@@ -353,6 +356,30 @@ class AuthUtils {
     }
   }
 
+  function negotiateLocale($supportedLanguages) {
+    $nlocale = $supportedLanguages[0];
+    $headers = getAllHeaders();
+    $accLcomp = explode(',', @$headers['Accept-Language']);
+    $accLang = array();
+    foreach ($accLcomp as $lcomp) {
+      if (strlen($lcomp)) {
+        $ldef = explode(';', $lcomp);
+        $accLang[$ldef[0]] = (float)((strpos(@$ldef[1],'q=')===0)?substr($ldef[1],2):1);
+      }
+    }
+    if (count($accLang)) {
+      $pLang = ''; $pLang_q = 0;
+      foreach ($supportedLanguages as $wantedLang) {
+        if (isset($accLang[$wantedLang]) && ($accLang[$wantedLang] > $pLang_q)) {
+          $pLang = $wantedLang;
+          $pLang_q = $accLang[$wantedLang];
+        }
+      }
+      if (strlen($pLang)) { $nlocale = $pLang; }
+    }
+    return $nlocale;
+  }
+
   function getGroupedEmails($group_id, $exclude_email = '') {
     $emails = array();
     if (intval($group_id)) {