]> git.agnieray.net Git - galette.git/blob - galette/includes/galette.inc.php
Fix some resources path
[galette.git] / galette / includes / galette.inc.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Main Galette initialisation
7 *
8 * PHP version 5
9 *
10 * Copyright © 2009-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Main
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2007-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7-dev - 2007-10-07
36 */
37
38 if ( !defined('GALETTE_PHP_MIN') ) {
39 define('GALETTE_PHP_MIN', '5.4');
40 }
41
42 // check required PHP version...
43 if ( version_compare(PHP_VERSION, GALETTE_PHP_MIN, '<') ) {
44 echo 'Galette is NOT compliant with your current PHP version. ' .
45 'Galette requires PHP ' . GALETTE_PHP_MIN .
46 ' minimum and current version is ' . phpversion();
47 die();
48 }
49
50 $time_start = microtime(true);
51 $cron = (PHP_SAPI === 'cli');
52
53 //define galette's root directory
54 if ( !defined('GALETTE_ROOT') ) {
55 define('GALETTE_ROOT', __DIR__ . '/../');
56 }
57
58 // define relative base path templating can use
59 if ( !defined('GALETTE_BASE_PATH') ) {
60 define('GALETTE_BASE_PATH', './');
61 }
62
63 require_once GALETTE_ROOT . 'config/versions.inc.php';
64 require_once GALETTE_ROOT . 'config/paths.inc.php';
65
66 //we'll only include relevant parts if we work from installer
67 if ( !isset($installer) ) {
68 $installer = false;
69 }
70 // test if galette is already installed or if we're form installer
71 // and redirect to install page if not
72 $installed = file_exists(GALETTE_CONFIG_PATH . 'config.inc.php');
73 if ( !$installed && !$installer ) {
74 header('location: install/index.php');
75 die();
76 }
77
78 if ( file_exists(GALETTE_CONFIG_PATH . 'behavior.inc.php')
79 && !defined('GALETTE_TESTS') && !$cron
80 ) {
81 include_once GALETTE_CONFIG_PATH . 'behavior.inc.php';
82 }
83
84 if ( isset($installer) && $installer !== true ) {
85 //If we're not working from installer
86 include_once GALETTE_CONFIG_PATH . 'config.inc.php';
87 }
88
89 if ( !function_exists('password_hash') ) {
90 include_once GALETTE_PASSWORD_COMPAT_PATH . '/password.php';
91 }
92
93 use Galette\Common\ClassLoader;
94 use Analog\Analog;
95 use Galette\Core;
96 require_once GALETTE_ROOT . 'lib/Galette/Common/ClassLoader.php';
97 require_once GALETTE_SLIM_PATH . 'Slim/Slim.php';
98
99 $galetteLoader = new ClassLoader('Galette', GALETTE_ROOT . 'lib');
100 $zendLoader = new ClassLoader('Zend', GALETTE_ZEND_PATH);
101 $analogLoader = new ClassLoader('Analog', GALETTE_ANALOG_PATH);
102 $smartyLoader = new ClassLoader(null, GALETTE_SMARTY_PATH);
103 $smartyLoader->setFileExtension('.class.php');
104 //register loaders
105 $galetteLoader->register();
106 $zendLoader->register();
107 $analogLoader->register();
108 $smartyLoader->register();
109
110 \Slim\Slim::registerAutoloader();
111 require_once GALETTE_SLIM_VIEWS_PATH . 'Smarty.php';
112
113 //start profiling
114 if (defined('GALETTE_XHPROF_PATH')
115 && function_exists('xhprof_enable')
116 ) {
117 include_once __DIR__ . '/../lib/Galette/Common/XHProf.php';
118 $profiler = new Galette\Common\XHProf();
119 $profiler->start();
120 }
121
122 //we start a php session
123 session_start();
124
125 define('GALETTE_VERSION', 'v0.8.3');
126 define('GALETTE_COMPAT_VERSION', '0.8');
127 define('GALETTE_DB_VERSION', '0.820');
128 if ( !defined('GALETTE_MODE') ) {
129 define('GALETTE_MODE', 'PROD'); //DEV, PROD, MAINT or DEMO
130 }
131
132 if ( !isset($_COOKIE['show_galette_dashboard']) ) {
133 setcookie(
134 'show_galette_dashboard',
135 true,
136 time()+31536000 //valid for a year
137 );
138 }
139
140 if ( !defined('GALETTE_DISPLAY_ERRORS') ) {
141 define('GALETTE_DISPLAY_ERRORS', 0);
142 }
143 ini_set('display_errors', GALETTE_DISPLAY_ERRORS);
144
145 set_include_path(
146 GALETTE_ZEND_PATH . PATH_SEPARATOR .
147 GALETTE_PHP_MAILER_PATH . PATH_SEPARATOR .
148 GALETTE_SMARTY_PATH . PATH_SEPARATOR .
149 get_include_path()
150 );
151
152 /*------------------------------------------------------------------------------
153 Logger stuff
154 ------------------------------------------------------------------------------*/
155 if ( !$cron && (!defined('GALETTE_HANDLE_ERRORS')
156 || GALETTE_HANDLE_ERRORS === true)
157 ) {
158 //set custom error handler
159 set_error_handler(
160 array(
161 "Galette\Core\Error",
162 "errorHandler"
163 )
164 );
165 }
166
167 $galette_run_log = null;
168 $galette_null_log = \Analog\Handler\Ignore::init();
169 $galette_debug_log = $galette_null_log;
170
171 //Log level cannot be <= 3, would be ignored.
172 if ( !defined('GALETTE_LOG_LVL') ) {
173 if ( GALETTE_MODE === 'DEV' ) {
174 define('GALETTE_LOG_LVL', 10);
175 } else {
176 define('GALETTE_LOG_LVL', 5);
177 }
178 }
179
180 if ( defined('GALETTE_TESTS') ) {
181 $galette_run_log = \Analog\Handler\Ignore::init();
182
183 } else {
184 if ( !$installer && !$cron ) {
185 $now = new \DateTime();
186 $dbg_log_path = GALETTE_LOGS_PATH . 'galette_debug_' .
187 $now->format('Y-m-d') . '.log';
188 $galette_debug_log = \Analog\Handler\File::init($dbg_log_path);
189 }
190 $galette_run_log = null;
191 $galette_log_var = null;
192
193 if ( GALETTE_MODE === 'DEV' || $cron
194 || ( defined('GALETTE_SYS_LOG') && GALETTE_SYS_LOG === true )
195 ) {
196 //logs everything in PHP logs (per chance /var/log/http/error_log)
197 $galette_run_log = \Analog\Handler\Stderr::init();
198 } else {
199 if ( !$installer || ($installer && defined('GALETTE_LOGGER_CHECKED')) ) {
200 //logs everything in galette log file
201 if ( !isset($logfile) ) {
202 //if no filename has been setetd (ie. from install), set default one
203 $logfile = 'galette_run';
204 }
205 $log_path = GALETTE_LOGS_PATH . $logfile . '_' .
206 $now->format('Y-m-d') . '.log';
207 $galette_run_log = \Analog\Handler\File::init($log_path);
208 } else {
209 $galette_run_log = \Analog\Handler\Variable::init($galette_log_var);
210 }
211 }
212 Core\Logs::cleanup();
213 }
214
215 Analog::handler(
216 \Analog\Handler\Multi::init(
217 array (
218 Analog::URGENT => $galette_run_log,
219 Analog::ALERT => $galette_run_log,
220 Analog::CRITICAL => $galette_run_log,
221 Analog::ERROR => $galette_run_log,
222 Analog::WARNING => (GALETTE_LOG_LVL >= Analog::WARNING)
223 ? $galette_run_log : $galette_null_log,
224 Analog::NOTICE => (GALETTE_LOG_LVL >= Analog::NOTICE)
225 ? $galette_run_log : $galette_null_log,
226 Analog::INFO => (GALETTE_LOG_LVL >= Analog::INFO)
227 ? $galette_run_log : $galette_null_log,
228 Analog::DEBUG => (GALETTE_LOG_LVL >= Analog::DEBUG)
229 ? $galette_debug_log : $galette_null_log
230 )
231 )
232 );
233
234 require_once GALETTE_ROOT . 'includes/functions.inc.php';
235
236 $session_name = null;
237 //since PREFIX_DB and NAME_DB are required to properly instanciate sessions,
238 // we have to check here if they're assigned
239 if ( $installer || !defined('PREFIX_DB') || !defined('NAME_DB') ) {
240 $session_name = 'galette_install';
241 } else {
242 $session_name = PREFIX_DB . '_' . NAME_DB;
243 }
244 $session = &$_SESSION['galette'][$session_name];
245
246 /**
247 * Language instantiation
248 */
249 if ( isset($session['lang']) ) {
250 $i18n = unserialize($session['lang']);
251 } else {
252 $i18n = new Core\I18n();
253 }
254
255 if ( isset($_POST['pref_lang'])
256 && (strpos($_SERVER['PHP_SELF'], 'self_adherent.php') !== false
257 || strpos($_SERVER['PHP_SELF'], 'install/index.php') !== false)
258 ) {
259 $_GET['pref_lang'] = $_POST['pref_lang'];
260 }
261 if ( isset($_GET['pref_lang']) ) {
262 $i18n->changeLanguage($_GET['pref_lang']);
263 }
264 $session['lang'] = serialize($i18n);
265 require_once GALETTE_ROOT . 'includes/i18n.inc.php';
266
267 // initialize messages arrays
268 $error_detected = array();
269 $warning_detected = array();
270 $success_detected = array();
271 /**
272 * "Flash" messages management
273 */
274 if ( isset($session['error_detected']) ) {
275 $error_detected = unserialize($session['error_detected']);
276 unset($session['error_detected']);
277 }
278 if ( isset($session['warning_detected']) ) {
279 $warning_detected = unserialize($session['warning_detected']);
280 unset($session['warning_detected']);
281 }
282 if ( isset($session['success_detected']) ) {
283 $success_detected = unserialize($session['success_detected']);
284 unset($session['success_detected']);
285 }
286
287 if ( !$installer and !defined('GALETTE_TESTS') ) {
288 //If we're not working from installer nor from tests
289 include_once GALETTE_CONFIG_PATH . 'config.inc.php';
290
291 /**
292 * Database instanciation
293 */
294 $zdb = new Core\Db();
295
296 if ( $zdb->checkDbVersion()
297 || strpos($_SERVER['PHP_SELF'], 'picture.php') !== false
298 ) {
299
300 /**
301 * Load preferences
302 */
303 $preferences = new Core\Preferences($zdb);
304
305 /**
306 * Set the path to the current theme templates
307 */
308 define(
309 '_CURRENT_TEMPLATE_PATH',
310 GALETTE_TEMPLATES_PATH . $preferences->pref_theme . '/'
311 );
312
313 if ( !defined('GALETTE_TPL_SUBDIR') ) {
314 define(
315 'GALETTE_TPL_SUBDIR',
316 'templates/' . $preferences->pref_theme . '/'
317 );
318 }
319
320 if ( !defined('GALETTE_THEME') ) {
321 define(
322 'GALETTE_THEME',
323 'themes/' . $preferences->pref_theme . '/'
324 );
325 }
326
327 /**
328 * Authentication
329 */
330 if ( isset($session['login']) ) {
331 $login = unserialize(
332 $session['login']
333 );
334 $login->setDb($zdb);
335 } else {
336 $login = new Core\Login($zdb, $i18n, $session);
337 }
338
339 if (GALETTE_MODE === 'MAINT' && !$login->isSuperAdmin() ) {
340 if ( $login->isLogged() ) {
341 //force logout
342 $login->logOut();
343 $session['login'] = null;
344 unset($session['login']);
345 $session['history'] = null;
346 unset($session['history']);
347 }
348 //redirect, if needed
349 if ( basename($_SERVER['SCRIPT_NAME']) !== 'maintenance.php' ) {
350 header('location: maintenance.php');
351 }
352 }
353
354 if ( $cron ) {
355 $login->logCron(basename($argv[0], '.php'));
356 }
357
358 /**
359 * Plugins
360 */
361 $plugins = new Core\Plugins($preferences);
362 $plugins->loadModules(GALETTE_PLUGINS_PATH, $i18n->getFileName());
363
364 /**
365 * Instanciate history object
366 */
367 if ( isset($session['history'])
368 && !GALETTE_MODE == 'DEV'
369 ) {
370 $hist = unserialize(
371 $session['history']
372 );
373 } else {
374 $hist = new Core\History();
375 }
376
377 /**
378 * Logo
379 */
380 if ( isset($session['logo'])
381 && !GALETTE_MODE == 'DEV'
382 ) {
383 $logo = unserialize(
384 $session['logo']
385 );
386 } else {
387 $logo = new Core\Logo();
388 }
389
390 /**
391 * Now that all objects are correctly setted,
392 * we can include files that need it
393 */
394 include_once GALETTE_ROOT . 'includes/session.inc.php';
395 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
396 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
397 include_once GALETTE_ROOT . 'includes/fields_defs/texts_fields.php';
398 include_once GALETTE_ROOT . 'includes/fields_defs/pdfmodels_fields.php';
399 } else {
400 header('location: needs_update.php');
401 die();
402 }
403 }