Code

Updated model
authorcajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 13 Nov 2007 18:24:36 +0000 (18:24 +0000)
committercajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 13 Nov 2007 18:24:36 +0000 (18:24 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6-playground@7791 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa.conf
include/accept-to-gettext.inc [new file with mode: 0644]
include/autoload-data.inc
include/autoload.inc
include/class_ConfigManager.inc
include/class_Utils.inc [new file with mode: 0644]
include/class_ViewportController.inc [new file with mode: 0644]
test

index dee0901005cd7758d759803a5d76921d9e2deaec..fd005b097202e70629965dcd35d281bf1cac75af 100644 (file)
--- a/gosa.conf
+++ b/gosa.conf
@@ -3,6 +3,7 @@
 
 ;------------------------------------------------------------------------------
 
+
 ; GONICUS GmbH configuration. Most features directly in the configuration.
 
 [gonicus]
@@ -122,3 +123,4 @@ ppd-path = "/var/spool/ppds"
 headline= "|{18px}|{:L}Name|{40%:R}Actions|"
 footer= "Statistics with no information currently"
 entryFormat= "|{_icon}|{cn} ({_filter(uppercase,{cn})})|{_actions}|"
+filter= "sample"
diff --git a/include/accept-to-gettext.inc b/include/accept-to-gettext.inc
new file mode 100644 (file)
index 0000000..621863c
--- /dev/null
@@ -0,0 +1,209 @@
+<?php
+/*
+ * accept-to-gettext.inc -- convert information in 'Accept-*' headers to
+ * gettext language identifiers.
+ * Copyright (c) 2003, Wouter Verhelst <wouter@debian.org>
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Usage:
+ *
+ *  $locale=al2gt(<array of supported languages/charsets in gettext syntax>,
+ *                <MIME type of document>);
+ *  setlocale('LC_ALL', $locale); // or 'LC_MESSAGES', or whatever...
+ *
+ * Example:
+ *
+ *  $langs=array('nl_BE.ISO-8859-15','nl_BE.UTF-8','en_US.UTF-8','en_GB.UTF-8');
+ *  $locale=al2gt($langs, 'text/html');
+ *  setlocale('LC_ALL', $locale);
+ *
+ * Note that this will send out header information (to be
+ * RFC2616-compliant), so it must be called before anything is sent to
+ * the user.
+ * 
+ * Assumptions made:
+ * * Charset encodings are written the same way as the Accept-Charset
+ *   HTTP header specifies them (RFC2616), except that they're parsed
+ *   case-insensitive.
+ * * Country codes and language codes are the same in both gettext and
+ *   the Accept-Language syntax (except for the case differences, which
+ *   are dealt with easily). If not, some input may be ignored.
+ * * The provided gettext-strings are fully qualified; i.e., no "en_US";
+ *   always "en_US.ISO-8859-15" or "en_US.UTF-8", or whichever has been
+ *   used. "en.ISO-8859-15" is OK, though.
+ * * The language is more important than the charset; i.e., if the
+ *   following is given:
+ * 
+ *   Accept-Language: nl-be, nl;q=0.8, en-us;q=0.5, en;q=0.3
+ *   Accept-Charset: ISO-8859-15, utf-8;q=0.5
+ *
+ *   And the supplied parameter contains (amongst others) nl_BE.UTF-8
+ *   and nl.ISO-8859-15, then nl_BE.UTF-8 will be picked.
+ * 
+ * $Log: accept-to-gettext.inc,v $
+ * Revision 1.1.1.1  2003/11/19 19:31:15  wouter
+ * * moved to new CVS repo after death of the old
+ * * Fixed code to apply a default to both Accept-Charset and
+ *   Accept-Language if none of those headers are supplied; patch from
+ *   Dominic Chambers <dominic@encasa.com>
+ *
+ * Revision 1.2  2003/08/14 10:23:59  wouter
+ * Removed little error in Content-Type header syntaxis.
+ *
+ */
+
+/* not really important, this one; perhaps I could've put it inline with
+ * the rest. */
+function find_match($curlscore,$curcscore,$curgtlang,$langval,$charval,
+                    $gtlang)
+{
+  if($curlscore < $langval) {
+    $curlscore=$langval;
+    $curcscore=$charval;
+    $curgtlang=$gtlang;
+  } else if ($curlscore == $langval) {
+    if($curcscore < $charval) {
+      $curcscore=$charval;
+      $curgtlang=$gtlang;
+    }
+  }
+  return array($curlscore, $curcscore, $curgtlang);
+}
+
+
+function al2gt($gettextlangs, $mime) 
+{
+  /* Check if ACCEPT_LANGUAGE isset */
+  if(!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){
+    $_SERVER["HTTP_ACCEPT_LANGUAGE"] = "";
+  }
+  if(!isset($_SERVER["HTTP_ACCEPT_CHARSET"])){
+    $_SERVER["HTTP_ACCEPT_CHARSET"] = "";
+  }
+
+  /* default to "everything is acceptable", as RFC2616 specifies */
+  $acceptLang=(($_SERVER["HTTP_ACCEPT_LANGUAGE"] == '') ? '*' :
+    $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
+  $acceptChar=(($_SERVER["HTTP_ACCEPT_CHARSET"] == '') ? '*' :
+    $_SERVER["HTTP_ACCEPT_CHARSET"]);
+  $alparts=@preg_split("/,/",$acceptLang);
+  $acparts=@preg_split("/,/",$acceptChar);
+  
+  /* Parse the contents of the Accept-Language header.*/
+  foreach($alparts as $part) {
+    $part=trim($part);
+    if(preg_match("/;/", $part)) {
+      $lang=@preg_split("/;/",$part);
+      $score=@preg_split("/=/",$lang[1]);
+      $alscores[$lang[0]]=$score[1];
+    } else {
+      $alscores[$part]=1;
+    }
+  }
+
+  /* Do the same for the Accept-Charset header. */
+
+  /* RFC2616: ``If no "*" is present in an Accept-Charset field, then
+   * all character sets not explicitly mentioned get a quality value of
+   * 0, except for ISO-8859-1, which gets a quality value of 1 if not
+   * explicitly mentioned.''
+   * 
+   * Making it 2 for the time being, so that we
+   * can distinguish between "not specified" and "specified as 1" later
+   * on. */
+  $acscores["ISO-8859-1"]=2;
+
+  foreach($acparts as $part) {
+    $part=trim($part);
+    if(preg_match("/;/", $part)) {
+      $cs=@preg_split("/;/",$part);
+      $score=@preg_split("/=/",$cs[1]);
+      $acscores[strtoupper($cs[0])]=$score[1];
+    } else {
+      $acscores[strtoupper($part)]=1;
+    }
+  }
+  if($acscores["ISO-8859-1"]==2) {
+    $acscores["ISO-8859-1"]=(isset($acscores["*"])?$acscores["*"]:1);
+  }
+
+  /* 
+   * Loop through the available languages/encodings, and pick the one
+   * with the highest score, excluding the ones with a charset the user
+   * did not include.
+   */
+  $curlscore=0;
+  $curcscore=0;
+  $curgtlang=NULL;
+  foreach($gettextlangs as $gtlang) {
+
+    $tmp1=preg_replace("/\_/","-",$gtlang);
+    $tmp2=@preg_split("/\./",$tmp1);
+    $allang=strtolower($tmp2[0]);
+    $gtcs=strtoupper($tmp2[1]);
+    $noct=@preg_split("/-/",$allang);
+
+    if(!isset($alscores["*"])){
+      $alscores["*"] = "";
+    }
+
+    if(!isset($alscores[$allang])){
+      $alscores[$allang] = "";
+    }
+    
+    if(!isset($alscores[$noct[0]])){
+      $alscores[$noct[0]] = "";
+    }
+  
+    if(!isset($acscores[$gtcs])){
+      $acscores[$gtcs] = "";
+    }
+    $testvals=array(
+        array($alscores[$allang], $acscores[$gtcs]),
+        array($alscores[$noct[0]], $acscores[$gtcs]),
+        array($alscores[$allang], $acscores["*"]),
+        array($alscores[$noct[0]], $acscores["*"]),
+        array($alscores["*"], $acscores[$gtcs]),
+        array($alscores["*"], $acscores["*"]));
+
+    $found=FALSE;
+    foreach($testvals as $tval) {
+      if(!$found && isset($tval[0]) && isset($tval[1])) {
+        $arr=find_match($curlscore, $curcscore, $curgtlang, $tval[0],
+            $tval[1], $gtlang);
+        $curlscore=$arr[0];
+        $curcscore=$arr[1];
+        $curgtlang=$arr[2];
+        $found=TRUE;
+      }
+    }
+  }
+
+  /* We must re-parse the gettext-string now, since we may have found it
+   * through a "*" qualifier.*/
+  $gtparts=@preg_split("/\./",$curgtlang);
+  $tmp=strtolower($gtparts[0]);
+  $lang=preg_replace("/\_/", "-", $tmp);
+  header("Content-Language: $lang");
+  if(isset($gtparts[1])){
+    $charset=$gtparts[1];
+    header("Content-Type: $mime; charset=$charset");
+  }
+  return $curgtlang;
+}
+
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
+?>
index e28b621b17eae707544edc36bd5441acd5037096..14ff98e2b6f40522aa00e8b3841b4a4867d39b47 100644 (file)
@@ -11,6 +11,9 @@ $class_mapping= array(
                 "ObjectListViewportException" => "include/class_ObjectListViewport.inc",
                 "ObjectListViewport" => "include/class_ObjectListViewport.inc",
                 "ObjectListIterator" => "include/class_ObjectListIterator.inc",
+                "Utils" => "include/class_Utils.inc",
+                "ViewportControllerException" => "include/class_ViewportController.inc",
+                "ViewportController" => "include/class_ViewportController.inc",
                 "ObjectListEntryFilter" => "include/interface_ObjectListEntryFilter.inc",
                 "GOsaGuiElementInteraction" => "include/interface_GOsaGuiElementInteraction.inc",
                 "ObjectListEntryFilter_uppercase" => "include/class_ObjectListEntryFilter_uppercase.inc",
index 99f096951c46f53e1c1b66cdd1b354d4d24890b2..fb571083028f815b144b74a0b72a5193e6ca1790 100644 (file)
@@ -3,9 +3,6 @@
 /* This function needs the pre-defined class mapping from autoload-data.inc */
 require_once("autoload-data.inc");
 
-/* FIXME: this does not belong here */
-require_once("smarty/Smarty.class.php");
-
 /* Set BASE_DIR for the complete code as constant */
 define('BASE_DIR', dirname(dirname(__FILE__)));
 
index 8c40ea19f2a5bcd03ebff34fc768590cd3ae4f53..4840a83ac120c782bf4d096ce903460eeb5bb3a7 100644 (file)
@@ -65,15 +65,15 @@ class ConfigManager {
 
 
        public function setSection($name){
+    $this->section= $name;
+
                if (isset($this->config[$name])){
-                       $this->section= $name;
                        $this->current= &$this->config[$name];
                        return TRUE;
-               } else {
-                       $this->section= "";
-                       $this->current= NULL;
-                       return FALSE;
-               }
+    } else {
+      $this->current= array();
+      return FALSE;
+    }
        }
 
 
@@ -86,14 +86,16 @@ class ConfigManager {
        }
 
 
-       public function getValue($name){
-               if (!$this->current){
+       public function getValue($name, $default= ""){
+               if (!$this->section){
                        throw new ConfigManagerException(_("Can't get value without current section set!"));
                }
                
                if (isset($this->current[$name])){
                        return $this->current[$name];
-               } else {
+               } elseif ($default) {
+      return $default;
+    } else {
                        return NULL;
                }
        }
diff --git a/include/class_Utils.inc b/include/class_Utils.inc
new file mode 100644 (file)
index 0000000..94f71bc
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+require_once("accept-to-gettext.inc");
+
+class Utils {
+
+  static public function getBrowserLanguage() {
+
+    #TODO: Support users language
+
+    /* Return gettext based string */
+    return (al2gt(Utils::getLanguages(), 'text/html'));  
+  }
+
+
+  static public function getLanguages() {
+    #TODO: crawl the locale directory for languages
+
+    return array(
+        "German" => "de_DE.UTF-8",
+        "French" => "fr_FR.UTF-8",
+        );
+  }
+
+}
+
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
+?>
diff --git a/include/class_ViewportController.inc b/include/class_ViewportController.inc
new file mode 100644 (file)
index 0000000..692de50
--- /dev/null
@@ -0,0 +1,118 @@
+<?php
+
+/* Provide Smarty capabilities */
+require("smarty/Smarty.class.php");
+
+/*! \brief   Exception implementation for ViewPortController
+    \author  Cajus Pollmeier <pollmeier@gonicus.de>
+    \version 1.00
+    \date    2007/11/02
+
+    This class handles the exceptions occuring in ObjectList.
+ */
+class ViewportControllerException extends Exception {
+       public function __construct($message, $code = 0) {
+               parent::__construct($message, $code);
+       }
+}
+
+/*! \brief   Implementation for rendering the main view
+    \author  Cajus Pollmeier <pollmeier@gonicus.de>
+    \version 1.00
+    \date    2007/11/02
+
+    The class ViewportController handles the rendering of the main view port,
+    manages the location for images and templates based on themes.
+ */
+class ViewportController {
+
+  private $theme= 'default';
+  private $smarty;
+  private $language= '';
+  private $timezone;
+
+  /*! \brief ViewportController constructor
+
+    The ViewportController loads the 'display' config option from the
+    ConfigRegistry and acts depending on these settings.
+   */
+       public function __construct(){
+
+    /* Do language setup during initialization, so we can really be sure
+       that most stuff gets translated in the beginning. */
+    $this->languageSetup();
+
+    /* Get configuration instance */
+    $config= Registry::getInstance("ConfigManager");
+    $config->setSection('display');
+
+    /* Set timezone */
+    $this->timezone= $config->getValue('timezone', 'GMT');
+    if (!date_default_timezone_set($this->timezone)){
+      throw new ViewportControllerException(_("Timezone '%s' is not valid!"), $this->timezone);
+    }
+
+    /* Do Smarty setup */
+    $this->Smarty= new Smarty;
+    $this->Smarty->template_dir= $BASE_DIR.'/templates/';
+    $this->Smarty->caching= $config->getValue('cache-templates', FALSE);
+    $this->Smarty->php_handling= SMARTY_PHP_REMOVE;
+
+    /* Set template compile directory */
+    $this->Smarty->compile_dir= $config->getValue('compile-directory', '/var/spool/gosa');
+
+    /* Check if our template directory is accessible */
+    if (!(is_dir($this->Smarty->compile_dir) && is_writable($this->Smarty->compile_dir))){
+      throw new ViewportControllerException(sprintf(_("Directory '%s' specified as compile-directory is not accessible!"), $smarty->compile_dir));
+    }
+
+    /* Check for old files in compile directory */
+    $this->cleanSmartyCompileDir();
+  }
+
+
+  private function cleanSmartyCompileDir() {
+    #TODO: Clean compile dir
+    echo "TODO: clean compile dir\n";
+  }
+
+
+  private function languageSetup() {
+
+    /* Get the browser language if */
+    $lang= Utils::getBrowserLanguage();
+    if ($this->language != $lang){
+      echo "Emit EventObject for changed language\n";
+      $this->language= $lang;
+    }
+
+    /* Get the browser language */
+    putenv("LANGUAGE=");
+    putenv("LANG=$this->language");
+    setlocale(LC_ALL, $this->language);
+
+    /* Set the text domain as 'messages' */
+    $domain= 'messages';
+    bindtextdomain($domain, BASE_DIR.'/locale');
+    textdomain($domain);
+
+    /* Set global Smarty variable to language and */
+    $GLOBALS['t_language']= $this->language;
+    $GLOBALS['t_gettext_message_dir']= BASE_DIR.'/locale/';
+  }
+
+
+  public function getSmartyInstance() {
+    return $this->Smarty;
+  }
+
+
+  public function render() {
+    echo "Render called\n";
+  }
+
+}
+
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
+?>
diff --git a/test b/test
index c023fd69ce70ef93f135b19abe680f44ef57cb52..5d71f7c21721279fca819def607617a4b0d37ba0 100755 (executable)
--- a/test
+++ b/test
@@ -1,5 +1,4 @@
 #!/usr/bin/php
-
 <?php
 
 
@@ -23,9 +22,9 @@ try {
        $cr= Registry::getInstance("ConfigManager");
        $cr->load("gosa.conf");
 
-       /* Get a new test instance of ObjectListViewports */
-       $vp= new ObjectListViewport("plugin/sample");
-       echo $vp->render();
+       /* Get a new instance of the main Viewport */
+       $vc= new ViewportController();
+       echo $vc->render();
 
 } catch (Exception $e) {
        echo "\n-GOsa Exception-----------------------------------------------------------\n\n".