1
|
<?php
|
2
|
/**
|
3
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
4
|
*
|
5
|
* This program is free software: you can redistribute it and/or modify
|
6
|
* it under the terms of the GNU General Public License as published by
|
7
|
* the Free Software Foundation, either version 3 of the License, or
|
8
|
* (at your option) any later version.
|
9
|
*
|
10
|
* This program is distributed in the hope that it will be useful,
|
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
* GNU General Public License for more details.
|
14
|
*
|
15
|
* You should have received a copy of the GNU General Public License
|
16
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
*/
|
18
|
|
19
|
/**
|
20
|
* initialize.php
|
21
|
*
|
22
|
* @category Core
|
23
|
* @package Core_Environment
|
24
|
* @author Werner v.d.Decken <wkl@isteam.de>
|
25
|
* @copyright Werner v.d.Decken <wkl@isteam.de>
|
26
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
27
|
* @version 0.0.1
|
28
|
* @revision $Revision: 1865 $
|
29
|
* @link $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/initialize.php $
|
30
|
* @lastmodified $Date: 2013-02-19 17:56:36 +0100 (Tue, 19 Feb 2013) $
|
31
|
* @since File replaced since 05.02.2013
|
32
|
* @description set the basic environment to run WebsiteBaker
|
33
|
*/
|
34
|
|
35
|
/* *** define some helper functions *** */
|
36
|
/**
|
37
|
* sanitize $_SERVER['HTTP_REFERER']
|
38
|
* @param string $sWbUrl qualified startup URL of current application
|
39
|
*/
|
40
|
function SanitizeHttpReferer($sWbUrl = WB_URL) {
|
41
|
$sTmpReferer = '';
|
42
|
if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
|
43
|
$aRefUrl = parse_url($_SERVER['HTTP_REFERER']);
|
44
|
if ($aRefUrl !== false) {
|
45
|
$aRefUrl['host'] = isset($aRefUrl['host']) ? $aRefUrl['host'] : '';
|
46
|
$aRefUrl['path'] = isset($aRefUrl['path']) ? $aRefUrl['path'] : '';
|
47
|
$aRefUrl['fragment'] = isset($aRefUrl['fragment']) ? '#'.$aRefUrl['fragment'] : '';
|
48
|
$aWbUrl = parse_url(WB_URL);
|
49
|
if ($aWbUrl !== false) {
|
50
|
$aWbUrl['host'] = isset($aWbUrl['host']) ? $aWbUrl['host'] : '';
|
51
|
$aWbUrl['path'] = isset($aWbUrl['path']) ? $aWbUrl['path'] : '';
|
52
|
if (strpos($aRefUrl['host'].$aRefUrl['path'],
|
53
|
$aWbUrl['host'].$aWbUrl['path']) !== false) {
|
54
|
$aRefUrl['path'] = preg_replace('#^'.$aWbUrl['path'].'#i', '', $aRefUrl['path']);
|
55
|
$sTmpReferer = WB_URL.$aRefUrl['path'].$aRefUrl['fragment'];
|
56
|
}
|
57
|
unset($aWbUrl);
|
58
|
}
|
59
|
unset($aRefUrl);
|
60
|
}
|
61
|
}
|
62
|
$_SERVER['HTTP_REFERER'] = $sTmpReferer;
|
63
|
}
|
64
|
/**
|
65
|
* Set constants for system/install values
|
66
|
* @throws RuntimeException
|
67
|
*/
|
68
|
function SetInstallPathConstants() {
|
69
|
if(!defined('DEBUG')){ define('DEBUG', false); } // normaly set in config file
|
70
|
if(!defined('ADMIN_DIRECTORY')){ define('ADMIN_DIRECTORY', 'admin'); }
|
71
|
if(!preg_match('/xx[a-z0-9_][a-z0-9_\-\.]+/i', 'xx'.ADMIN_DIRECTORY)) {
|
72
|
throw new RuntimeException('Invalid admin-directory: ' . ADMIN_DIRECTORY);
|
73
|
}
|
74
|
if(!defined('WB_PATH')){ define('WB_PATH', dirname(dirname(__FILE__))); }
|
75
|
if(!defined('ADMIN_URL')){ define('ADMIN_URL', WB_URL.'/'.ADMIN_DIRECTORY); }
|
76
|
if(!defined('ADMIN_PATH')){ define('ADMIN_PATH', WB_PATH.'/'.ADMIN_DIRECTORY); }
|
77
|
if(!defined('WB_REL')){
|
78
|
$x1 = parse_url(WB_URL);
|
79
|
define('WB_REL', (isset($x1['path']) ? $x1['path'] : ''));
|
80
|
}
|
81
|
define('ADMIN_REL', WB_REL.'/'.ADMIN_DIRECTORY);
|
82
|
if(!defined('DOCUMENT_ROOT')) {
|
83
|
|
84
|
define('DOCUMENT_ROOT', preg_replace('/'.preg_quote(WB_REL, '/').'$/', '', WB_PATH));
|
85
|
}
|
86
|
define('TMP_PATH', WB_PATH.'/temp');
|
87
|
}
|
88
|
/**
|
89
|
* Read DB settings from configuration file
|
90
|
* @return string
|
91
|
* @throws RuntimeException
|
92
|
*
|
93
|
*/
|
94
|
function readConfiguration($sRetvalType = 'url') {
|
95
|
$x = debug_backtrace();
|
96
|
if(sizeof($x) != 0) { throw new RuntimeException('illegal function request!'); }
|
97
|
$aRetval = array();
|
98
|
$sSetupFile = dirname(dirname(__FILE__)).'/setup.ini.php';
|
99
|
if(is_readable($sSetupFile)) {
|
100
|
$aCfg = parse_ini_file($sSetupFile, true);
|
101
|
foreach($aCfg['Constants'] as $key=>$value) {
|
102
|
if($key == 'debug') { $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); }
|
103
|
if(!defined(strtoupper($key))) { define(strtoupper($key), $value); }
|
104
|
}
|
105
|
$db = $aCfg['DataBase'];
|
106
|
$db['type'] = isset($db['type']) ? $db['type'] : 'mysql';
|
107
|
$db['user'] = isset($db['user']) ? $db['user'] : 'foo';
|
108
|
$db['pass'] = isset($db['pass']) ? $db['pass'] : 'bar';
|
109
|
$db['host'] = isset($db['host']) ? $db['host'] : 'localhost';
|
110
|
$db['port'] = isset($db['port']) ? $db['port'] : '3306';
|
111
|
$db['port'] = ($db['port'] != '3306') ? $db['port'] : '';
|
112
|
$db['name'] = isset($db['name']) ? $db['name'] : 'dummy';
|
113
|
$db['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8';
|
114
|
$db['table_prefix'] = (isset($db['table_prefix']) ? $db['table_prefix'] : '');
|
115
|
define('TABLE_PREFIX', $db['table_prefix']);
|
116
|
if($sRetvalType == 'dsn') {
|
117
|
$aRetval[0] = $db['type'].':dbname='.$db['name'].';host='.$db['host'].';'
|
118
|
. ($db['port'] != '' ? 'port='.(int)$db['port'].';' : '');
|
119
|
$aRetval[1] = array('CHARSET' => $db['charset'], 'TABLE_PREFIX' => $db['table_prefix']);
|
120
|
$aRetval[2] = array( 'user' => $db['user'], 'pass' => $db['pass']);
|
121
|
}else { // $sRetvalType == 'url'
|
122
|
$aRetval[0] = $db['type'].'://'.$db['user'].':'.$db['pass'].'@'
|
123
|
. $db['host'].($db['port'] != '' ? ':'.$db['port'] : '').'/'.$db['name'];
|
124
|
}
|
125
|
unset($db, $aCfg);
|
126
|
return $sRetval;
|
127
|
}
|
128
|
throw new RuntimeException('unable to read setup.ini.php');
|
129
|
}
|
130
|
/* ***************************************************************************************
|
131
|
* Start initialization *
|
132
|
****************************************************************************************/
|
133
|
// initialize debug evaluation values ---
|
134
|
$sDbConnectType = 'url'; // depending from class WbDatabase it can be 'url' or 'dsn'
|
135
|
$starttime = array_sum(explode(" ",microtime()));
|
136
|
$iPhpDeclaredClasses = sizeof(get_declared_classes());
|
137
|
// disable all kind of magic_quotes in PHP versions before 5.4 ---
|
138
|
if(version_compare(PHP_VERSION, '5.4.0', '<')) {
|
139
|
if(get_magic_quotes_gpc() || get_magic_quotes_runtime()) {
|
140
|
@ini_set('magic_quotes_sybase', 0);
|
141
|
@ini_set('magic_quotes_gpc', 0);
|
142
|
@ini_set('magic_quotes_runtime', 0);
|
143
|
}
|
144
|
}
|
145
|
// load db configuration ---
|
146
|
if(defined('DB_TYPE')) {
|
147
|
$aSqlData = array( 0 => DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
|
148
|
}else {
|
149
|
$aSqlData = readConfiguration($sDbConnectType);
|
150
|
}
|
151
|
// sanitize $_SERVER['HTTP_REFERER'] ---
|
152
|
SanitizeHttpReferer(WB_URL);
|
153
|
SetInstallPathConstants();
|
154
|
// define constant systemenvironment settings ---
|
155
|
date_default_timezone_set('UTC');
|
156
|
if(!defined('MAX_TIME')) { define('MAX_TIME', (pow(2, 31)-1)); } // 32-Bit Timestamp of 19 Jan 2038 03:14:07 GMT
|
157
|
if(!defined('DO_NOT_TRACK')) { define('DO_NOT_TRACK', (isset($_SERVER['HTTP_DNT']))); }
|
158
|
// register WB basic autoloader ---
|
159
|
$sTmp = dirname(__FILE__).'/WbAutoloader.php';
|
160
|
if(!class_exists('WbAutoloader', false)){ include($sTmp); }
|
161
|
WbAutoloader::doRegister(array(ADMIN_DIRECTORY=>'a', 'modules'=>'m'));
|
162
|
// register TWIG autoloader ---
|
163
|
$sTmp = dirname(dirname(__FILE__)).'/include/Sensio/Twig/lib/Twig/Autoloader.php';
|
164
|
if(!class_exists('Twig_Autoloader', false)){ include($sTmp); }
|
165
|
Twig_Autoloader::register();
|
166
|
// aktivate exceptionhandler ---
|
167
|
if(!function_exists('globalExceptionHandler')) {
|
168
|
include(dirname(__FILE__).'/globalExceptionHandler.php');
|
169
|
}
|
170
|
// ---------------------------
|
171
|
// Create global database instance ---
|
172
|
$database = WbDatabase::getInstance();
|
173
|
if($sDbConnectType == 'dsn') {
|
174
|
$database->doConnect($aSqlData[0], $aSqlData[1]['user'], $aSqlData[1]['pass'], $aSqlData[2]);
|
175
|
}else {
|
176
|
$database->doConnect($aSqlData[0], TABLE_PREFIX);
|
177
|
}
|
178
|
unset($aSqlData);
|
179
|
// load global settings from database and define global consts from ---
|
180
|
$sql = 'SELECT `name`, `value` FROM `'.TABLE_PREFIX.'settings`';
|
181
|
if(($oSettings = $database->query($sql))) {
|
182
|
if(!$oSettings->numRows()) { throw new AppException('no settings found'); }
|
183
|
while($aSetting = $oSettings->fetchRow(MYSQL_ASSOC)) {
|
184
|
//sanitize true/false values
|
185
|
$aSetting['value'] = ($aSetting['value'] == 'true'
|
186
|
? true
|
187
|
: ($aSetting['value'] == 'false'
|
188
|
? false
|
189
|
: $aSetting['value'])
|
190
|
);
|
191
|
// make global const from setting
|
192
|
@define(strtoupper($aSetting['name']), $aSetting['value']);
|
193
|
}
|
194
|
}else { throw new AppException($database->get_error()); }
|
195
|
// get/set users timezone ---
|
196
|
define('TIMEZONE', (isset($_SESSION['TIMEZONE']) ? $_SESSION['TIMEZONE'] : DEFAULT_TIMEZONE));
|
197
|
define('DATE_FORMAT', (isset($_SESSION['DATE_FORMAT']) ? $_SESSION['DATE_FORMAT'] : DEFAULT_DATE_FORMAT));
|
198
|
define('TIME_FORMAT', (isset($_SESSION['TIME_FORMAT']) ? $_SESSION['TIME_FORMAT'] : DEFAULT_TIME_FORMAT));
|
199
|
// set Theme directory ---
|
200
|
define('THEME_URL', WB_URL.'/templates/'.DEFAULT_THEME);
|
201
|
define('THEME_PATH', WB_PATH.'/templates/'.DEFAULT_THEME);
|
202
|
define('THEME_REL', WB_REL.'/templates/'.DEFAULT_THEME);
|
203
|
// extended wb editor settings
|
204
|
define('EDIT_ONE_SECTION', false);
|
205
|
define('EDITOR_WIDTH', 0);
|
206
|
// define numeric, octal values for chmod operations ---
|
207
|
$string_file_mode = STRING_FILE_MODE; // STRING_FILE_MODE is set deprecated
|
208
|
define('OCTAL_FILE_MODE',(int) octdec($string_file_mode));
|
209
|
$string_dir_mode = STRING_DIR_MODE; // STRING_DIR_MODE is set deprecated
|
210
|
define('OCTAL_DIR_MODE',(int) octdec($string_dir_mode));
|
211
|
// define form security class and preload it ---
|
212
|
$sSecMod = (defined('SECURE_FORM_MODULE') && SECURE_FORM_MODULE != '') ? '.'.SECURE_FORM_MODULE : '';
|
213
|
$sSecMod = WB_PATH.'/framework/SecureForm'.$sSecMod.'.php';
|
214
|
require_once($sSecMod);
|
215
|
// set error-reporting from loaded settings ---
|
216
|
if(intval(ER_LEVEL) > 0 ) {
|
217
|
error_reporting(ER_LEVEL);
|
218
|
if( intval(ini_get ( 'display_errors' )) == 0 ) {
|
219
|
ini_set('display_errors', 1);
|
220
|
}
|
221
|
}
|
222
|
// Start a session ---
|
223
|
if(!defined('SESSION_STARTED')) {
|
224
|
session_name(APP_NAME.'_session_id');
|
225
|
@session_start();
|
226
|
define('SESSION_STARTED', true);
|
227
|
}
|
228
|
// *** begin deprecated part *************************************************************
|
229
|
// load settings for use in Captch and ASP module
|
230
|
if (!defined("WB_INSTALL_PROCESS")) {
|
231
|
$sql = 'SELECT * FROM `'.TABLE_PREFIX.'mod_captcha_control`';
|
232
|
// request settings from database
|
233
|
if(($oSettings = $database->query($sql))) {
|
234
|
if(($aSetting = $oSettings->fetchRow(MYSQL_ASSOC))) {
|
235
|
define('ENABLED_CAPTCHA', ($aSetting['enabled_captcha'] == '1'));
|
236
|
define('ENABLED_ASP', ($aSetting['enabled_asp'] == '1'));
|
237
|
define('CAPTCHA_TYPE', $aSetting['captcha_type']);
|
238
|
define('ASP_SESSION_MIN_AGE', (int)$aSetting['asp_session_min_age']);
|
239
|
define('ASP_VIEW_MIN_AGE', (int)$aSetting['asp_view_min_age']);
|
240
|
define('ASP_INPUT_MIN_AGE', (int)$aSetting['asp_input_min_age']);
|
241
|
}
|
242
|
}
|
243
|
}
|
244
|
if(defined('ENABLED_ASP') && ENABLED_ASP && !isset($_SESSION['session_started'])) {
|
245
|
$_SESSION['session_started'] = time();
|
246
|
}
|
247
|
// *** end of deprecated part ************************************************************
|
248
|
// get user language ---
|
249
|
$sRequestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
|
250
|
// check if get/post value is available
|
251
|
$sTempLanguage = (isset(${$sRequestMethod}['lang']) ? ${$sRequestMethod}['lang'] : '');
|
252
|
// validate language code
|
253
|
if(preg_match('/^[a-z]{2}$/si', $sTempLanguage)) {
|
254
|
// if there's valid get/post
|
255
|
define('LANGUAGE', strtoupper($sTempLanguage));
|
256
|
$_SESSION['LANGUAGE']=LANGUAGE;
|
257
|
}else {
|
258
|
if(isset($_SESSION['LANGUAGE']) && $_SESSION['LANGUAGE']) {
|
259
|
// if there's valid session value
|
260
|
define('LANGUAGE', $_SESSION['LANGUAGE']);
|
261
|
}else {
|
262
|
// otherwise set to default
|
263
|
define('LANGUAGE', DEFAULT_LANGUAGE);
|
264
|
}
|
265
|
}
|
266
|
// activate translations / load language definitions
|
267
|
/** begin of deprecated part || will be replaced by class Translate **/
|
268
|
// Load Language file
|
269
|
if(!file_exists(WB_PATH.'/languages/'.LANGUAGE.'.php')) {
|
270
|
$sMsg = 'Error loading language file '.LANGUAGE.', please check configuration';
|
271
|
throw new AppException($sMsg);
|
272
|
} else {
|
273
|
// include language file
|
274
|
require_once(WB_PATH.'/languages/'.LANGUAGE.'.php');
|
275
|
}
|
276
|
/** end of deprecated part **/
|
277
|
// instantiate and initialize adaptor for temporary registry replacement ---
|
278
|
if(class_exists('WbAdaptor')) {
|
279
|
WbAdaptor::getInstance()->getWbConstants();
|
280
|
}
|
281
|
// load and activate new global translation table
|
282
|
Translate::getInstance()->initialize('en',
|
283
|
(defined('DEFAULT_LANGUAGE') ? DEFAULT_LANGUAGE : ''),
|
284
|
(defined('LANGUAGE') ? LANGUAGE : ''),
|
285
|
'WbOldStyle',
|
286
|
Translate::CACHE_ENABLED);
|
287
|
// *** END OF FILE ***********************************************************************
|
288
|
|