]> git.agnieray.net Git - galette.git/blob - galette/includes/galette.inc.php
Merge branch 'hotfix/1.0.3'
[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-2024 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-2024 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 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7-dev - 2007-10-07
35 */
36
37 //define galette's root directory
38 if (!defined('GALETTE_ROOT')) {
39 define('GALETTE_ROOT', __DIR__ . '/../');
40 }
41
42 require_once GALETTE_ROOT . 'config/versions.inc.php';
43 require_once GALETTE_ROOT . 'config/paths.inc.php';
44
45 // check required PHP version...
46 if (version_compare(PHP_VERSION, GALETTE_PHP_MIN, '<')) {
47 echo 'Galette is NOT compliant with your current PHP version. ' .
48 'Galette requires PHP ' . GALETTE_PHP_MIN .
49 ' minimum and current version is ' . phpversion();
50 die(1);
51 }
52
53 $time_start = microtime(true);
54 $cron = (PHP_SAPI === 'cli');
55
56 // define relative base path templating can use
57 if (!defined('GALETTE_BASE_PATH')) {
58 define('GALETTE_BASE_PATH', './');
59 }
60
61 //we'll only include relevant parts if we work from installer
62 if (!isset($installer)) {
63 $installer = false;
64 }
65 // test if galette is already installed or if we're form installer
66 // and redirect to install page if not
67 $installed = file_exists(GALETTE_CONFIG_PATH . 'config.inc.php');
68 if (!$installed && !$installer) {
69 header('location: ./installer.php');
70 die();
71 }
72
73 if (
74 file_exists(GALETTE_CONFIG_PATH . 'behavior.inc.php')
75 && !defined('GALETTE_TESTS')
76 ) {
77 include_once GALETTE_CONFIG_PATH . 'behavior.inc.php';
78 }
79
80 if (isset($installer) && $installer !== true) {
81 //If we're not working from installer
82 include_once GALETTE_CONFIG_PATH . 'config.inc.php';
83 }
84
85 use Analog\Analog;
86 use Analog\Handler;
87 use Analog\Handler\LevelName;
88 use Galette\Core;
89
90 require GALETTE_ROOT . '/vendor/autoload.php';
91
92 //start profiling
93 if (
94 defined('GALETTE_XHPROF_PATH')
95 && function_exists('xhprof_enable')
96 ) {
97 include_once __DIR__ . '/../lib/Galette/Common/XHProf.php';
98 $profiler = new Galette\Common\XHProf();
99 $profiler->start();
100 }
101
102 //Version to display
103 if (!defined('GALETTE_HIDE_VERSION')) {
104 define('GALETTE_DISPLAY_VERSION', \Galette\Core\Galette::gitVersion(false));
105 }
106
107 if (!defined('GALETTE_MODE')) {
108 define('GALETTE_MODE', \Galette\Core\Galette::MODE_PROD);
109 }
110
111 if (!isset($_COOKIE['show_galette_dashboard'])) {
112 setcookie(
113 'show_galette_dashboard',
114 true,
115 [
116 'expires' => time() + 31536000, //valid for a year
117 'path' => '/'
118 ]
119 );
120 }
121
122 ini_set('display_errors', (defined('GALETTE_TESTS') ? '1' : '0'));
123
124 /*------------------------------------------------------------------------------
125 Logger stuff
126 ------------------------------------------------------------------------------*/
127
128 error_reporting(E_ALL);
129 set_error_handler(function ($severity, $message, $file, $line) {
130 if (error_reporting() & $severity) {
131 throw new \ErrorException($message, 0, $severity, $file, $line);
132 }
133 });
134
135 //change default format so the 3rd param is a string for level name
136 Analog::$format = "%s - %s - %s - %s\n";
137 $galette_run_log = null;
138
139 if (!defined('GALETTE_LOG_LVL')) {
140 if (\Galette\Core\Galette::isDebugEnabled()) {
141 define('GALETTE_LOG_LVL', Analog::DEBUG);
142 } elseif (defined('GALETTE_TESTS')) {
143 define('GALETTE_LOG_LVL', Analog::NOTICE);
144 } else {
145 define('GALETTE_LOG_LVL', Analog::WARNING);
146 }
147 }
148
149 if (defined('GALETTE_TESTS')) {
150 $log_path = GALETTE_LOGS_PATH . 'tests.log';
151 $galette_run_log = LevelName::init(Handler\File::init($log_path));
152 } else {
153 $galette_log_var = null;
154
155 if (!$installer || ($installer && defined('GALETTE_LOGGER_CHECKED'))) {
156 //logs everything in galette log file
157 if (!isset($logfile)) {
158 //if no filename has been setted (ie. from install), set default one
159 $logfile = 'galette';
160 }
161 $log_path = GALETTE_LOGS_PATH . $logfile . '.log';
162 $galette_run_log = LevelName::init(Handler\File::init($log_path));
163 } else {
164 $galette_run_log = LevelName::init(Handler\Variable::init($galette_log_var));
165 }
166 if (!$installer) {
167 Core\Logs::cleanup();
168 }
169 }
170
171 Analog::handler($galette_run_log);
172
173 require_once GALETTE_ROOT . 'includes/functions.inc.php';
174
175 if (!$installer and !defined('GALETTE_TESTS')) {
176 //If we're not working from installer nor from tests
177 include_once GALETTE_CONFIG_PATH . 'config.inc.php';
178
179 /**
180 * Database instantiation
181 */
182 $zdb = new Core\Db();
183
184 if ($zdb->checkDbVersion()) {
185
186 /**
187 * Load preferences
188 */
189 $preferences = new Core\Preferences($zdb);
190
191 /**
192 * Set the path to the current theme templates
193 */
194 define(
195 '_CURRENT_THEME_PATH',
196 GALETTE_THEMES_PATH . $preferences->pref_theme . '/'
197 );
198
199 if (!defined('GALETTE_THEME')) {
200 define(
201 'GALETTE_THEME',
202 'themes/' . $preferences->pref_theme . '/'
203 );
204 }
205 } else {
206 $needs_update = true;
207 }
208 }
209
210 $plugins = new Galette\Core\Plugins();
211 //make sure plugins autoload is called before session start
212 $plugins->autoload(GALETTE_PLUGINS_PATH);