| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | /** Debug levels: |
| 22 | * DEBUG_BT = show the backtraces (SQL/XMLRPC/...) |
| 23 | * DEBUG_VALID = run html validation |
| 24 | * DEBUG_SMARTY = don't hide smarty errors/warnings/notices |
| 25 | */ |
| 26 | define('DEBUG_BT', 1); |
| 27 | define('DEBUG_VALID', 2); |
| 28 | define('DEBUG_SMARTY', 4); |
| 29 | |
| 30 | /** PlGlobals provides functions to read a set of configuration files and gives |
| 31 | * access to this configurations. |
| 32 | * |
| 33 | * The configuration files ares ini files with sections: |
| 34 | * <pre> |
| 35 | * [SectionName] |
| 36 | * fieldname = value |
| 37 | * </pre> |
| 38 | * |
| 39 | * With this configuration file, you'll be able to access 'value' via |
| 40 | * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace |
| 41 | * |
| 42 | * |
| 43 | * You should derivate this class into a local Globals class. In this class |
| 44 | * you can specify configuration variables that belongs to the 'global' namespace |
| 45 | * (accessible via $global->fieldname). To do so, just define the fieldname |
| 46 | * in your class and set its value in the [Core] section of you ini file. |
| 47 | */ |
| 48 | class PlGlobals |
| 49 | { |
| 50 | public $coreVersion = '0.9.17'; |
| 51 | |
| 52 | /** Debug level. |
| 53 | * This is a combination of the DEBUG_ flags. As soon as at least |
| 54 | * one flag is set, the debug mode is activated, this means: |
| 55 | * - debug panel on the top of the pages |
| 56 | * - don't hide php notices |
| 57 | * - recompile templates when they have been changed |
| 58 | */ |
| 59 | public $debug = 0; |
| 60 | |
| 61 | /** Access mode. |
| 62 | */ |
| 63 | public $mode = 'rw'; // 'rw' => read/write, |
| 64 | // 'r' => read/only |
| 65 | // '' => site down |
| 66 | |
| 67 | /** BaseURL of the site. |
| 68 | * This is read from the HTTP headers if available but you MUST give a |
| 69 | * default value for this field in you configuration file (because, this |
| 70 | * can be used in CLI scripts that has no access no HTTP headers...) |
| 71 | * |
| 72 | * [Core] |
| 73 | * baseurl = "https//www.mysite.org/" |
| 74 | */ |
| 75 | public $baseurl; |
| 76 | |
| 77 | /** In case your base url is https-based, this provied an HTTP-based value |
| 78 | * for the URL. |
| 79 | */ |
| 80 | public $baseurl_http; |
| 81 | |
| 82 | /** paths */ |
| 83 | public $spoolroot; |
| 84 | |
| 85 | /** Localization configuration. |
| 86 | */ |
| 87 | public $locale; |
| 88 | public $timezone; |
| 89 | |
| 90 | /** You must give a list of file to load. |
| 91 | * The filenames given are relatives to the config path of your plat/al installation. |
| 92 | */ |
| 93 | public function __construct(array $files) |
| 94 | { |
| 95 | $this->spoolroot = dirname(dirname(dirname(__FILE__))); |
| 96 | |
| 97 | $this->readConfig($files); |
| 98 | if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) { |
| 99 | $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
| 100 | $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); |
| 101 | $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); |
| 102 | } |
| 103 | |
| 104 | $this->setLocale(); |
| 105 | } |
| 106 | |
| 107 | private function readIniFile($filename) |
| 108 | { |
| 109 | $array = parse_ini_file($filename, true); |
| 110 | if (!is_array($array)) { |
| 111 | return; |
| 112 | } |
| 113 | foreach ($array as $cat => $conf) { |
| 114 | $c = strtolower($cat); |
| 115 | foreach ($conf as $k => $v) { |
| 116 | if ($c == 'core' && property_exists($this, $k)) { |
| 117 | $this->$k=$v; |
| 118 | } else { |
| 119 | if (!isset($this->$c)) { |
| 120 | $this->$c = new stdClass; |
| 121 | } |
| 122 | $this->$c->$k = $v; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private function readConfig(array $files) |
| 129 | { |
| 130 | foreach ($files as $file) { |
| 131 | $this->readIniFile($this->spoolroot . '/configs/' . $file); |
| 132 | } |
| 133 | if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) { |
| 134 | $this->readIniFile($this->spoolroot.'/spool/conf/platal.dynamic.conf'); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** Writes an ini file separated in categories |
| 139 | * @param filename the name of the file to write (overwrite existing) |
| 140 | * @param categories an array of categories (array of keys and values) |
| 141 | */ |
| 142 | private function writeIniFile($filename, &$categories) |
| 143 | { |
| 144 | // [category] |
| 145 | // key = value |
| 146 | $f = fopen($filename, 'w'); |
| 147 | foreach ($categories as $cat => $conf) { |
| 148 | fwrite($f, '; {{{ '.$cat."\n\n"); |
| 149 | fwrite($f, '['.$cat.']'."\n\n"); |
| 150 | foreach ($conf as $k => $v) { |
| 151 | fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n"); |
| 152 | } |
| 153 | fwrite($f, "\n".'; }}}'."\n"); |
| 154 | } |
| 155 | fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n"); |
| 156 | fclose($f); |
| 157 | } |
| 158 | |
| 159 | /** Change dynamic config file |
| 160 | * @param conf array of keys and values to add or replace |
| 161 | * @param category name of category to change |
| 162 | * |
| 163 | * Opens the dynamic conf file and set values from conf in specified |
| 164 | * category. Updates config vars too. |
| 165 | */ |
| 166 | public function changeDynamicConfig($conf, $category = 'Core') |
| 167 | { |
| 168 | $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf'; |
| 169 | if (file_exists($dynamicfile)) { |
| 170 | $array = parse_ini_file($dynamicfile, true); |
| 171 | } else { |
| 172 | $array = null; |
| 173 | } |
| 174 | if (!is_array($array)) { |
| 175 | // dynamic conf is empty |
| 176 | $array = array($category => $conf); |
| 177 | } else { |
| 178 | // looks for a category that looks the same (case insensitive) |
| 179 | $same = false; |
| 180 | foreach ($array as $m => &$c) { |
| 181 | if (strtolower($m) == strtolower($category)) { |
| 182 | $same = $m; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | if (!$same) { |
| 187 | // this category doesn't exist yet |
| 188 | $array[$category] = $conf; |
| 189 | } else { |
| 190 | // this category already exists |
| 191 | $conflower = array(); |
| 192 | foreach ($conf as $k => $v) { |
| 193 | $conflower[strtolower($k)] = $v; |
| 194 | } |
| 195 | // $conflower is now same as $conf but with lower case keys |
| 196 | // replaces values of keys that already exists |
| 197 | foreach ($array[$same] as $k => $v) { |
| 198 | if (isset($conflower[strtolower($k)])) { |
| 199 | $array[$same][$k] = $conflower[strtolower($k)]; |
| 200 | unset($conflower[strtolower($k)]); |
| 201 | } |
| 202 | } |
| 203 | // add new keys |
| 204 | foreach ($conf as $k => $v) { |
| 205 | if (isset($conflower[strtolower($k)])) { |
| 206 | $array[$same][$k] = $v; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | // writes the file over |
| 212 | $this->writeIniFile($dynamicfile, $array); |
| 213 | // rereads the new config to correctly set vars |
| 214 | $this->readIniFile($dynamicfile); |
| 215 | } |
| 216 | |
| 217 | public function bootstrap($conf, $callback, $category = 'Core') |
| 218 | { |
| 219 | $bootstrap = false; |
| 220 | $category = strtolower($category); |
| 221 | foreach ($conf as $key) { |
| 222 | if (!isset($this->$category->$key)) { |
| 223 | $bootstrap = true; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if ($bootstrap) { |
| 228 | call_user_func($callback); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private function setLocale() |
| 233 | { |
| 234 | setlocale(LC_MESSAGES, $this->locale); |
| 235 | setlocale(LC_TIME, $this->locale); |
| 236 | setlocale(LC_CTYPE, $this->locale); |
| 237 | date_default_timezone_set($this->timezone); |
| 238 | mb_internal_encoding("UTF-8"); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 243 | ?> |