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