Rename PlatalPage to PlPage
[platal.git] / include / globals.inc.php.in
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 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
f1ca33de 22class PlatalGlobals
0337d704 23{
612a2d8a 24 public $session;
0337d704 25
26 /** The x.org version */
612a2d8a 27 public $version = '@VERSION@';
28 public $debug = 0;
fe556813
FB
29 public $mode = 'rw'; // 'rw' => read/write,
30 // 'r' => read/only
31 // '' => site down
0337d704 32
33 /** db params */
612a2d8a 34 public $dbdb = 'x4dat';
35 public $dbhost = 'localhost';
36 public $dbuser = 'x4dat';
37 public $dbpwd = 'x4dat';
38 public $dbcharset = 'utf8';
72542de0 39
25962601 40 /** default skin */
41 public $skin;
42 public $register_skin;
43
0337d704 44 /** paths */
612a2d8a 45 public $baseurl;
131b15a7 46 public $baseurl_http;
612a2d8a 47 public $spoolroot;
0337d704 48
612a2d8a 49 public $locale;
50 public $timezone;
d941feeb 51
612a2d8a 52 public function __construct($sess)
0337d704 53 {
72542de0 54 $this->session = $sess;
296075a4 55 $this->spoolroot = dirname(dirname(__FILE__));
72542de0 56
296075a4 57 $this->read_config();
45319df1 58 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
296075a4 59 $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
131b15a7
VZ
60 $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
61 $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
45319df1 62 }
0337d704 63
ea6398d1 64 $this->setlocale();
f1ca33de 65 }
66
612a2d8a 67 private function read_ini_file($filename)
0337d704 68 {
d941feeb 69 $array = parse_ini_file($filename, true);
0337d704 70 if (!is_array($array)) {
71 return;
72 }
d941feeb 73 foreach ($array as $cat => $conf) {
0337d704 74 $c = strtolower($cat);
d941feeb 75 foreach ($conf as $k => $v) {
1a013db7 76 if ($c == 'core' && property_exists($this, $k)) {
d941feeb 77 $this->$k=$v;
0337d704 78 } else {
d941feeb 79 if (!isset($this->$c)) {
80 $this->$c = new stdClass;
81 }
82 $this->$c->$k = $v;
0337d704 83 }
84 }
85 }
86 }
87
612a2d8a 88 private function read_config()
d941feeb 89 {
90 $this->read_ini_file($this->spoolroot.'/configs/platal.ini');
d941feeb 91 $this->read_ini_file($this->spoolroot.'/configs/platal.conf');
e6705972
FB
92 if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) {
93 $this->read_ini_file($this->spoolroot.'/spool/conf/platal.dynamic.conf');
94 }
145006c0 95 }
99027475 96
145006c0
PC
97 /** Writes an ini file separated in categories
98 * @param filename the name of the file to write (overwrite existing)
99 * @param categories an array of categories (array of keys and values)
100 */
101 private static function write_ini_file($filename, &$categories)
102 {
103 // [category]
104 // key = value
105 $f = fopen($filename, 'w');
106 foreach ($categories as $cat => $conf) {
107 fwrite($f, '; {{{ '.$cat."\n\n");
108 fwrite($f, '['.$cat.']'."\n\n");
109 foreach ($conf as $k => $v) {
110 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
111 }
112 fwrite($f, "\n".'; }}}'."\n");
113 }
114 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
115 fclose($f);
116 }
99027475 117
145006c0
PC
118 /** Change dynamic config file
119 * @param conf array of keys and values to add or replace
120 * @param category name of category to change
121 *
122 * Opens the dynamic conf file and set values from conf in specified
123 * category. Updates config vars too.
124 */
125 public function change_dynamic_config($conf, $category = 'Core')
126 {
127 $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf';
e6705972
FB
128 if (file_exists($dynamicfile)) {
129 $array = parse_ini_file($dynamicfile, true);
130 } else {
131 $array = null;
132 }
145006c0
PC
133 if (!is_array($array)) {
134 // dynamic conf is empty
135 $array = array($category => $conf);
136 } else {
137 // looks for a category that looks the same (case insensitive)
138 $same = false;
139 foreach ($array as $m => &$c) {
140 if (strtolower($m) == strtolower($category)) {
141 $same = $m;
142 break;
143 }
144 }
145 if (!$same) {
146 // this category doesn't exist yet
147 $array[$category] = $conf;
148 } else {
149 // this category already exists
150 $conflower = array();
151 foreach ($conf as $k => $v) {
152 $conflower[strtolower($k)] = $v;
153 }
154 // $conflower is now same as $conf but with lower case keys
155 // replaces values of keys that already exists
156 foreach ($array[$same] as $k => $v) {
157 if (isset($conflower[strtolower($k)])) {
158 $array[$same][$k] = $conflower[strtolower($k)];
159 unset($conflower[strtolower($k)]);
160 }
161 }
162 // add new keys
163 foreach ($conf as $k => $v) {
164 if (isset($conflower[strtolower($k)])) {
165 $array[$same][$k] = $v;
166 }
167 }
168 }
169 }
170 // writes the file over
171 PlatalGlobals::write_ini_file($dynamicfile, $array);
172 // rereads the new config to correctly set vars
173 $this->read_ini_file($dynamicfile);
d941feeb 174 }
175
99027475
FB
176 public function bootstrap($conf, $callback, $category = 'Core')
177 {
178 $bootstrap = false;
179 $category = strtolower($category);
180 foreach ($conf as $key) {
181 if (!isset($this->$category->$key)) {
182 $bootstrap = true;
183 break;
184 }
185 }
186 if ($bootstrap) {
187 call_user_func($callback);
188 }
189 }
190
612a2d8a 191 private function setlocale()
0337d704 192 {
d941feeb 193 setlocale(LC_MESSAGES, $this->locale);
194 setlocale(LC_TIME, $this->locale);
195 setlocale(LC_CTYPE, $this->locale);
1a013db7 196 date_default_timezone_set($this->timezone);
dcdcd18a 197 mb_internal_encoding("UTF-8");
0337d704 198 }
ea6398d1 199
612a2d8a 200 public function asso($key=null)
ea6398d1 201 {
202 static $aid = null;
203
204 if (is_null($aid)) {
205 $gp = Get::v('n');
27ae65e3 206 if ($p = strpos($gp, '/')) {
207 $gp = substr($gp, 0, $p);
208 }
ea6398d1 209
210 if ($gp) {
359f9ca3
FB
211 $res = XDB::query('SELECT a.*, d.nom AS domnom,
212 FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc,
213 FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub
ea6398d1 214 FROM groupex.asso AS a
215 LEFT JOIN groupex.dom AS d ON d.id = a.dom
216 WHERE diminutif = {?}', $gp);
217 if (!($aid = $res->fetchOneAssoc())) {
218 $aid = array();
219 }
220 } else {
221 $aid = array();
222 }
223 }
224 if (empty($key)) {
225 return $aid;
226 } elseif ( isset($aid[$key]) ) {
227 return $aid[$key];
228 } else {
229 return null;
230 }
231 }
0337d704 232}
233
a7de4ef7 234// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 235?>