Rename CoreLogger to PlLogger
[platal.git] / include / globals.inc.php.in
index 7672412..f052c6b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 
 class PlatalGlobals
 {
-    var $session;
+    public $session;
 
     /** The x.org version */
-    var $version = '@VERSION@';
-    var $debug   = 0;
+    public $version = '@VERSION@';
+    public $debug   = 0;
+    public $mode    = 'rw';    // 'rw' => read/write,
+                               // 'r'  => read/only
+                               // ''   => site down
 
     /** db params */
-    var $dbdb               = 'x4dat';
-    var $dbhost             = 'localhost';
-    var $dbuser             = 'x4dat';
-    var $dbpwd              = 'x4dat';
+    public $dbdb               = 'x4dat';
+    public $dbhost             = 'localhost';
+    public $dbuser             = 'x4dat';
+    public $dbpwd              = 'x4dat';
+    public $dbcharset          = 'utf8';
+
+    /** default skin */
+    public $skin;
+    public $register_skin;
 
     /** paths */
-    var $baseurl;
-    var $spoolroot;
+    public $baseurl;
+    public $baseurl_http;
+    public $spoolroot;
+
+    public $locale;
+    public $timezone;
 
-    function PlatalGlobals($sess)
+    public function __construct($sess)
     {
         $this->session   = $sess;
-
-        $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
-        $this->baseurl   = trim($base.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
-        $this->spoolroot = dirname(dirname(dirname(__FILE__)));
+        $this->spoolroot = dirname(dirname(__FILE__));
 
         $this->read_config();
+        if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
+            $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
+            $this->baseurl      = @trim($base    .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
+            $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
+        }
 
-        $this->dbconnect();
         $this->setlocale();
     }
 
-    function read_config()
+    private function read_ini_file($filename)
     {
-        $array = parse_ini_file($this->spoolroot.'/configs/platal.ini', true);
+        $array = parse_ini_file($filename, true);
+        if (!is_array($array)) {
+            return;
+        }
         foreach ($array as $cat => $conf) {
             $c = strtolower($cat);
             foreach ($conf as $k => $v) {
-                $this->$c->$k = $v;
+                if ($c == 'core' && property_exists($this, $k)) {
+                    $this->$k=$v;
+                } else {
+                    if (!isset($this->$c)) {
+                        $this->$c = new stdClass;
+                    }
+                    $this->$c->$k = $v;
+                }
             }
         }
+    }
 
-        $array = parse_ini_file($this->spoolroot.'/configs/platal.conf', true);
-        if (!is_array($array)) {
-            return;
+    private function read_config()
+    {
+        $this->read_ini_file($this->spoolroot.'/configs/platal.ini');
+        $this->read_ini_file($this->spoolroot.'/configs/platal.conf');
+        if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) {
+            $this->read_ini_file($this->spoolroot.'/spool/conf/platal.dynamic.conf');
         }
+    }
 
-        foreach ($array as $cat=>$conf) {
-            $c = strtolower($cat);
-            foreach ($conf as $key=>$val) {
-                if ($c == 'core' && isset($this->$key)) {
-                    $this->$key=$val;
-                } else {
-                    $this->$c->$key = $val;
+    /** Writes an ini file separated in categories
+     * @param filename the name of the file to write (overwrite existing)
+     * @param categories an array of categories (array of keys and values)
+     */              
+    private static function write_ini_file($filename, &$categories)
+    {
+        // [category]
+        // key = value
+        $f = fopen($filename, 'w');
+        foreach ($categories as $cat => $conf) {
+            fwrite($f, '; {{{ '.$cat."\n\n");
+            fwrite($f, '['.$cat.']'."\n\n");
+            foreach ($conf as $k => $v) {
+                fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
+            }
+            fwrite($f, "\n".'; }}}'."\n");
+        }
+        fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
+        fclose($f);
+    }
+
+    /** Change dynamic config file
+     * @param conf array of keys and values to add or replace
+     * @param category name of category to change
+     * 
+     * Opens the dynamic conf file and set values from conf in specified
+     * category. Updates config vars too.
+     */ 
+    public function change_dynamic_config($conf, $category = 'Core')
+    {
+        $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf';
+        if (file_exists($dynamicfile)) {
+            $array = parse_ini_file($dynamicfile, true);
+        } else {
+            $array = null;
+        }
+        if (!is_array($array)) {
+            // dynamic conf is empty
+            $array = array($category => $conf);
+        } else {
+            // looks for a category that looks the same (case insensitive)
+            $same = false;
+            foreach ($array as $m => &$c) {
+                if (strtolower($m) == strtolower($category)) {
+                    $same = $m;
+                    break;
                 }
             }
+            if (!$same) {
+                // this category doesn't exist yet
+                $array[$category] = $conf;
+            } else {
+                // this category already exists
+                $conflower = array();
+                foreach ($conf as $k => $v) {
+                    $conflower[strtolower($k)] = $v;
+                }
+                // $conflower is now same as $conf but with lower case keys
+                // replaces values of keys that already exists
+                foreach ($array[$same] as $k => $v) {
+                    if (isset($conflower[strtolower($k)])) {
+                        $array[$same][$k] = $conflower[strtolower($k)];
+                        unset($conflower[strtolower($k)]);
+                    }
+                }
+                // add new keys
+                foreach ($conf as $k => $v) {
+                    if (isset($conflower[strtolower($k)])) {
+                        $array[$same][$k] = $v;
+                    }
+                } 
+            }
         }
+        // writes the file over
+        PlatalGlobals::write_ini_file($dynamicfile, $array);
+        // rereads the new config to correctly set vars
+        $this->read_ini_file($dynamicfile);
     }
 
-    function dbconnect()
+    public function bootstrap($conf, $callback, $category = 'Core')
     {
-        @mysql_connect($this->dbhost, $this->dbuser, $this->dbpwd);
-        @mysql_select_db($this->dbdb);
+        $bootstrap = false;
+        $category = strtolower($category);
+        foreach ($conf as $key) {
+            if (!isset($this->$category->$key)) {
+                $bootstrap = true;
+                break;
+            }
+        }
+        if ($bootstrap) {
+            call_user_func($callback);
+        }
     }
 
-    function setlocale()
+    private function setlocale()
     {
-        global $globals;
-        setlocale(LC_MESSAGES, $globals->core->locale);
-        setlocale(LC_TIME,     $globals->core->locale);
-        setlocale(LC_CTYPE,    $globals->core->locale);
+        setlocale(LC_MESSAGES, $this->locale);
+        setlocale(LC_TIME,     $this->locale);
+        setlocale(LC_CTYPE,    $this->locale);
+        date_default_timezone_set($this->timezone);
+        mb_internal_encoding("UTF-8");
     }
 
-    function asso($key=null)
+    public function asso($key=null)
     {
         static $aid = null;
 
         if (is_null($aid)) {
             $gp = Get::v('n');
-            $gp = substr($gp, 0, strpos($gp, '/'));
+            if ($p = strpos($gp, '/')) {
+                $gp = substr($gp, 0, $p);
+            }
 
             if ($gp) {
-                $res = XDB::query('SELECT  a.*, d.nom AS domnom
+                $res = XDB::query('SELECT  a.*, d.nom AS domnom,
+                                           FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
+                                           FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub
                                      FROM  groupex.asso AS a
                                 LEFT JOIN  groupex.dom  AS d ON d.id = a.dom
                                     WHERE  diminutif = {?}', $gp);
@@ -122,5 +231,5 @@ class PlatalGlobals
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>