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