]> git.agnieray.net Git - galette.git/blob - galette/includes/galette.inc.php
Bump version
[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 $galetteLoader = new ClassLoader('Galette', GALETTE_ROOT . 'lib');
98 $zendLoader = new ClassLoader('Zend', GALETTE_ZEND_PATH);
99 $analogLoader = new ClassLoader('Analog', GALETTE_ANALOG_PATH);
100 $smartyLoader = new ClassLoader(null, GALETTE_SMARTY_PATH);
101 $smartyLoader->setFileExtension('.class.php');
102 //register loaders
103 $galetteLoader->register();
104 $zendLoader->register();
105 $analogLoader->register();
106 $smartyLoader->register();
107
108 //start profiling
109 if (defined('GALETTE_XHPROF_PATH')
110 && function_exists('xhprof_enable')
111 ) {
112 include_once __DIR__ . '/../lib/Galette/Common/XHProf.php';
113 $profiler = new Galette\Common\XHProf();
114 $profiler->start();
115 }
116
117 //we start a php session
118 session_start();
119
120 define('GALETTE_VERSION', 'v0.8.3');
121 define('GALETTE_COMPAT_VERSION', '0.8');
122 define('GALETTE_DB_VERSION', '0.820');
123 if ( !defined('GALETTE_MODE') ) {
124 define('GALETTE_MODE', 'PROD'); //DEV, PROD, MAINT or DEMO
125 }
126
127 if ( !isset($_COOKIE['show_galette_dashboard']) ) {
128 setcookie(
129 'show_galette_dashboard',
130 true,
131 time()+31536000 //valid for a year
132 );
133 }
134
135 if ( !defined('GALETTE_DISPLAY_ERRORS') ) {
136 define('GALETTE_DISPLAY_ERRORS', 0);
137 }
138 ini_set('display_errors', GALETTE_DISPLAY_ERRORS);
139
140 set_include_path(
141 GALETTE_ZEND_PATH . PATH_SEPARATOR .
142 GALETTE_PHP_MAILER_PATH . PATH_SEPARATOR .
143 GALETTE_SMARTY_PATH . PATH_SEPARATOR .
144 get_include_path()
145 );
146
147 /*------------------------------------------------------------------------------
148 Logger stuff
149 ------------------------------------------------------------------------------*/
150 if ( !$cron && (!defined('GALETTE_HANDLE_ERRORS')
151 || GALETTE_HANDLE_ERRORS === true)
152 ) {
153 //set custom error handler
154 set_error_handler(
155 array(
156 "Galette\Core\Error",
157 "errorHandler"
158 )
159 );
160 }
161
162 $galette_run_log = null;
163 $galette_null_log = \Analog\Handler\Ignore::init();
164 $galette_debug_log = $galette_null_log;
165
166 //Log level cannot be <= 3, would be ignored.
167 if ( !defined('GALETTE_LOG_LVL') ) {
168 if ( GALETTE_MODE === 'DEV' ) {
169 define('GALETTE_LOG_LVL', 10);
170 } else {
171 define('GALETTE_LOG_LVL', 5);
172 }
173 }
174
175 if ( defined('GALETTE_TESTS') ) {
176 $galette_run_log = \Analog\Handler\Ignore::init();
177
178 } else {
179 if ( !$installer && !$cron ) {
180 $now = new \DateTime();
181 $dbg_log_path = GALETTE_LOGS_PATH . 'galette_debug_' .
182 $now->format('Y-m-d') . '.log';
183 $galette_debug_log = \Analog\Handler\File::init($dbg_log_path);
184 }
185 $galette_run_log = null;
186 $galette_log_var = null;
187
188 if ( GALETTE_MODE === 'DEV' || $cron
189 || ( defined('GALETTE_SYS_LOG') && GALETTE_SYS_LOG === true )
190 ) {
191 //logs everything in PHP logs (per chance /var/log/http/error_log)
192 $galette_run_log = \Analog\Handler\Stderr::init();
193 } else {
194 if ( !$installer || ($installer && defined('GALETTE_LOGGER_CHECKED')) ) {
195 //logs everything in galette log file
196 if ( !isset($logfile) ) {
197 //if no filename has been setetd (ie. from install), set default one
198 $logfile = 'galette_run';
199 }
200 $log_path = GALETTE_LOGS_PATH . $logfile . '_' .
201 $now->format('Y-m-d') . '.log';
202 $galette_run_log = \Analog\Handler\File::init($log_path);
203 } else {
204 $galette_run_log = \Analog\Handler\Variable::init($galette_log_var);
205 }
206 }
207 Core\Logs::cleanup();
208 }
209
210 Analog::handler(
211 \Analog\Handler\Multi::init(
212 array (
213 Analog::URGENT => $galette_run_log,
214 Analog::ALERT => $galette_run_log,
215 Analog::CRITICAL => $galette_run_log,
216 Analog::ERROR => $galette_run_log,
217 Analog::WARNING => (GALETTE_LOG_LVL >= Analog::WARNING)
218 ? $galette_run_log : $galette_null_log,
219 Analog::NOTICE => (GALETTE_LOG_LVL >= Analog::NOTICE)
220 ? $galette_run_log : $galette_null_log,
221 Analog::INFO => (GALETTE_LOG_LVL >= Analog::INFO)
222 ? $galette_run_log : $galette_null_log,
223 Analog::DEBUG => (GALETTE_LOG_LVL >= Analog::DEBUG)
224 ? $galette_debug_log : $galette_null_log
225 )
226 )
227 );
228
229 require_once GALETTE_ROOT . 'includes/functions.inc.php';
230
231 $session_name = null;
232 //since PREFIX_DB and NAME_DB are required to properly instanciate sessions,
233 // we have to check here if they're assigned
234 if ( $installer || !defined('PREFIX_DB') || !defined('NAME_DB') ) {
235 $session_name = 'galette_install';
236 } else {
237 $session_name = PREFIX_DB . '_' . NAME_DB;
238 }
239 $session = &$_SESSION['galette'][$session_name];
240
241 /**
242 * Language instantiation
243 */
244 if ( isset($session['lang']) ) {
245 $i18n = unserialize($session['lang']);
246 } else {
247 $i18n = new Core\I18n();
248 }
249
250 if ( isset($_POST['pref_lang'])
251 && (strpos($_SERVER['PHP_SELF'], 'self_adherent.php') !== false
252 || strpos($_SERVER['PHP_SELF'], 'install/index.php') !== false)
253 ) {
254 $_GET['pref_lang'] = $_POST['pref_lang'];
255 }
256 if ( isset($_GET['pref_lang']) ) {
257 $i18n->changeLanguage($_GET['pref_lang']);
258 }
259 $session['lang'] = serialize($i18n);
260 require_once GALETTE_ROOT . 'includes/i18n.inc.php';
261
262 // initialize messages arrays
263 $error_detected = array();
264 $warning_detected = array();
265 $success_detected = array();
266 /**
267 * "Flash" messages management
268 */
269 if ( isset($session['error_detected']) ) {
270 $error_detected = unserialize($session['error_detected']);
271 unset($session['error_detected']);
272 }
273 if ( isset($session['warning_detected']) ) {
274 $warning_detected = unserialize($session['warning_detected']);
275 unset($session['warning_detected']);
276 }
277 if ( isset($session['success_detected']) ) {
278 $success_detected = unserialize($session['success_detected']);
279 unset($session['success_detected']);
280 }
281
282 if ( !$installer and !defined('GALETTE_TESTS') ) {
283 //If we're not working from installer nor from tests
284 include_once GALETTE_CONFIG_PATH . 'config.inc.php';
285
286 /**
287 * Database instanciation
288 */
289 $zdb = new Core\Db();
290
291 if ( $zdb->checkDbVersion()
292 || strpos($_SERVER['PHP_SELF'], 'picture.php') !== false
293 ) {
294
295 /**
296 * Load preferences
297 */
298 $preferences = new Core\Preferences($zdb);
299
300 /**
301 * Set the path to the current theme templates
302 */
303 define(
304 '_CURRENT_TEMPLATE_PATH',
305 GALETTE_TEMPLATES_PATH . $preferences->pref_theme . '/'
306 );
307
308 /**
309 * Authentication
310 */
311 if ( isset($session['login']) ) {
312 $login = unserialize(
313 $session['login']
314 );
315 $login->setDb($zdb);
316 } else {
317 $login = new Core\Login($zdb, $i18n, $session);
318 }
319
320 if (GALETTE_MODE === 'MAINT' && !$login->isSuperAdmin() ) {
321 if ( $login->isLogged() ) {
322 //force logout
323 $login->logOut();
324 $session['login'] = null;
325 unset($session['login']);
326 $session['history'] = null;
327 unset($session['history']);
328 }
329 //redirect, if needed
330 if ( basename($_SERVER['SCRIPT_NAME']) !== 'maintenance.php' ) {
331 header('location: maintenance.php');
332 }
333 }
334
335 if ( $cron ) {
336 $login->logCron(basename($argv[0], '.php'));
337 }
338
339 /**
340 * Plugins
341 */
342 $plugins = new Core\Plugins();
343 $plugins->loadModules(GALETTE_PLUGINS_PATH, $i18n->getFileName());
344
345 /**
346 * Instanciate history object
347 */
348 if ( isset($session['history'])
349 && !GALETTE_MODE == 'DEV'
350 ) {
351 $hist = unserialize(
352 $session['history']
353 );
354 } else {
355 $hist = new Core\History();
356 }
357
358 /**
359 * Logo
360 */
361 if ( isset($session['logo'])
362 && !GALETTE_MODE == 'DEV'
363 ) {
364 $logo = unserialize(
365 $session['logo']
366 );
367 } else {
368 $logo = new Core\Logo();
369 }
370
371 /**
372 * Now that all objects are correctly setted,
373 * we can include files that need it
374 */
375 include_once GALETTE_ROOT . 'includes/session.inc.php';
376 include_once GALETTE_ROOT . 'includes/smarty.inc.php';
377 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
378 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
379 include_once GALETTE_ROOT . 'includes/fields_defs/texts_fields.php';
380 include_once GALETTE_ROOT . 'includes/fields_defs/pdfmodels_fields.php';
381 } else {
382 header('location: needs_update.php');
383 die();
384 }
385 }