]> git.agnieray.net Git - galette.git/blob - galette/includes/galette.inc.php
acd9bdcd243a34fccc5d2267244415fbac938d27
[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-2018 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-2018 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 //define galette's root directory
39 if (!defined('GALETTE_ROOT')) {
40 define('GALETTE_ROOT', __DIR__ . '/../');
41 }
42
43 require_once GALETTE_ROOT . 'config/versions.inc.php';
44 require_once GALETTE_ROOT . 'config/paths.inc.php';
45
46 // check required PHP version...
47 if (version_compare(PHP_VERSION, GALETTE_PHP_MIN, '<')) {
48 echo 'Galette is NOT compliant with your current PHP version. ' .
49 'Galette requires PHP ' . GALETTE_PHP_MIN .
50 ' minimum and current version is ' . phpversion();
51 die();
52 }
53
54 $time_start = microtime(true);
55 $cron = (PHP_SAPI === 'cli');
56
57 // define relative base path templating can use
58 if (!defined('GALETTE_BASE_PATH')) {
59 define('GALETTE_BASE_PATH', './');
60 }
61
62 //we'll only include relevant parts if we work from installer
63 if (!isset($installer)) {
64 $installer = false;
65 }
66 // test if galette is already installed or if we're form installer
67 // and redirect to install page if not
68 $installed = file_exists(GALETTE_CONFIG_PATH . 'config.inc.php');
69 if (!$installed && !$installer) {
70 header('location: ./installer.php');
71 die();
72 }
73
74 if (file_exists(GALETTE_CONFIG_PATH . 'behavior.inc.php')
75 && !defined('GALETTE_TESTS') && !$cron
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 Galette\Core;
87
88 /*
89 BREAKS as of Galette 0.9-dev
90 // To help the built-in PHP dev server, check if the request was actually for
91 // something which should probably be served as a static file
92 if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
93 return false;
94 }*/
95
96 require GALETTE_ROOT . '/vendor/autoload.php';
97
98 //start profiling
99 if (defined('GALETTE_XHPROF_PATH')
100 && function_exists('xhprof_enable')
101 ) {
102 include_once __DIR__ . '/../lib/Galette/Common/XHProf.php';
103 $profiler = new Galette\Common\XHProf();
104 $profiler->start();
105 }
106
107 define('GALETTE_VERSION', 'v0.9.3');
108 define('GALETTE_COMPAT_VERSION', '0.9.2');
109 define('GALETTE_DB_VERSION', '0.931');
110 if (!defined('GALETTE_MODE')) {
111 define('GALETTE_MODE', 'PROD'); //DEV, PROD, MAINT or DEMO
112 }
113
114 if (!isset($_COOKIE['show_galette_dashboard'])) {
115 setcookie(
116 'show_galette_dashboard',
117 true,
118 time()+31536000 //valid for a year
119 );
120 }
121
122 if (!defined('GALETTE_DISPLAY_ERRORS')) {
123 if (GALETTE_MODE === 'DEV') {
124 define('GALETTE_DISPLAY_ERRORS', 1);
125 } else {
126 define('GALETTE_DISPLAY_ERRORS', 0);
127 }
128 }
129 ini_set('display_errors', 0);
130
131 set_include_path(
132 GALETTE_ZEND_PATH . PATH_SEPARATOR .
133 GALETTE_PHP_MAILER_PATH . PATH_SEPARATOR .
134 GALETTE_SMARTY_PATH . PATH_SEPARATOR .
135 get_include_path()
136 );
137
138 /*------------------------------------------------------------------------------
139 Logger stuff
140 ------------------------------------------------------------------------------*/
141 if (!$cron && !defined('GALETTE_TESTS')) {
142 //set custom error handler
143 set_error_handler(
144 array(
145 "Galette\Core\Error",
146 "errorHandler"
147 )
148 );
149 }
150
151 $galette_run_log = null;
152 $galette_debug_log = \Analog\Handler\Ignore::init();
153
154 if (!defined('GALETTE_LOG_LVL')) {
155 if (GALETTE_MODE === 'DEV') {
156 define('GALETTE_LOG_LVL', \Analog\Analog::DEBUG);
157 } elseif (defined('GALETTE_TESTS')) {
158 define('GALETTE_LOG_LVL', \Analog\Analog::ERROR);
159 } else {
160 define('GALETTE_LOG_LVL', \Analog\Analog::WARNING);
161 }
162 }
163
164 if (defined('GALETTE_TESTS')) {
165 $log_path = GALETTE_LOGS_PATH . 'tests.log';
166 $galette_run_log = \Analog\Handler\File::init($log_path);
167 } else {
168 if ((!$installer || ($installer && defined('GALETTE_LOGGER_CHECKED'))) && !$cron) {
169 if (GALETTE_LOG_LVL >= \Analog\Analog::INFO) {
170 $now = new \DateTime();
171 $dbg_log_path = GALETTE_LOGS_PATH . 'galette_debug_' .
172 $now->format('Y-m-d') . '.log';
173 $galette_debug_log = \Analog\Handler\File::init($dbg_log_path);
174 } else {
175 $galette_debug_log = \Analog\Handler\Ignore::init();
176 }
177 }
178 $galette_log_var = null;
179
180 if (GALETTE_MODE === 'DEV' || $cron
181 || ( defined('GALETTE_SYS_LOG') && GALETTE_SYS_LOG === true )
182 ) {
183 //logs everything in PHP logs (per chance /var/log/http/error_log or /var/log/php-fpm/error.log)
184 $galette_run_log = \Analog\Handler\Stderr::init();
185 } else {
186 if (!$installer || ($installer && defined('GALETTE_LOGGER_CHECKED'))) {
187 //logs everything in galette log file
188 if (!isset($logfile)) {
189 //if no filename has been setted (ie. from install), set default one
190 $logfile = 'galette_run';
191 }
192 $log_path = GALETTE_LOGS_PATH . $logfile . '.log';
193 $galette_run_log = \Analog\Handler\File::init($log_path);
194 } else {
195 $galette_run_log = \Analog\Handler\Variable::init($galette_log_var);
196 }
197 }
198 if (!$installer) {
199 Core\Logs::cleanup();
200 }
201 }
202
203 Analog::handler(
204 \Analog\Handler\Multi::init(
205 array (
206 Analog::NOTICE => \Analog\Handler\Threshold::init(
207 $galette_run_log,
208 GALETTE_LOG_LVL
209 ),
210 Analog::DEBUG => $galette_debug_log
211 )
212 )
213 );
214
215 require_once GALETTE_ROOT . 'includes/functions.inc.php';
216
217 if (!$installer and !defined('GALETTE_TESTS')) {
218 //If we're not working from installer nor from tests
219 include_once GALETTE_CONFIG_PATH . 'config.inc.php';
220
221 /**
222 * Database instanciation
223 */
224 $zdb = new Core\Db();
225
226 if ($zdb->checkDbVersion()) {
227
228 /**
229 * Load preferences
230 */
231 $preferences = new Core\Preferences($zdb);
232
233 /**
234 * Set the path to the current theme templates
235 */
236 define(
237 '_CURRENT_THEME_PATH',
238 GALETTE_THEMES_PATH . $preferences->pref_theme . '/'
239 );
240
241 if (!defined('GALETTE_TPL_SUBDIR')) {
242 define(
243 'GALETTE_TPL_SUBDIR',
244 'templates/' . $preferences->pref_theme . '/'
245 );
246 }
247
248 if (!defined('GALETTE_THEME')) {
249 define(
250 'GALETTE_THEME',
251 'themes/' . $preferences->pref_theme . '/'
252 );
253 }
254
255 /** TODO: login is now handled in dependencies.php; the cron case should be aswell */
256 if ($cron) {
257 $login->logCron(basename($argv[0], '.php'));
258 }
259 } else {
260 $needs_update = true;
261 }
262 }