forked from kiang/LampCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
!inc.php
321 lines (277 loc) · 10.3 KB
/
!inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
*
* PHP 5.3 or better is required
*
* @package Global functions
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* @author Dmitri Snytkine <[email protected]>
* @copyright 2005-2011 (or current year) ExamNotes.net inc.
* @license http://www.gnu.org/licenses/gpl-3.0.txt The GNU General Public License (GPL) version 3
* @link http://cms.lampcms.com Lampcms.com project
* @version Release: @package_version@
*
*
*/
error_reporting(E_ALL | E_DEPRECATED);
/**
* For those unfortunate souls
* who has magic_quotes enabled on their
* server despite having php 5.3
* this code will remove those
* genocidal quotes added by magic_quotes
*/
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
/**
* Set all the multibyte
* functions to use UTF-8
* as internal encoding
*/
if(function_exists('mb_internal_encoding')){
mb_internal_encoding("UTF-8");
}
function exception_handler($e){
if(!($e instanceof \OutOfBoundsException)){
try {
$err = Lampcms\Responder::makeErrorPage('<strong>Error:</strong> '.Lampcms\Exception::formatException($e));
$extra = (isset($_SERVER)) ? ' $_SERVER: '.print_r($_SERVER, 1) : ' no extra';
if(defined('LAMPCMS_DEVELOPER_EMAIL') && strlen(trim(constant('LAMPCMS_DEVELOPER_EMAIL'))) > 1){
@mail(LAMPCMS_DEVELOPER_EMAIL, 'ErrorHandle in inc.php', $err.$extra);
}
echo ($err);
} catch(\Exception $e) {
echo 'Error in Exception handler: : '.$e->getMessage().' line '.$e->getLine().$e->getTraceAsString();
}
} else {
d('Got exit signal in error_handler from '.$e->getTraceAtString());
}
}
/**
* if php NOT running as fastcgi
* then we need to create a dummy function
*
*/
if(!function_exists('fastcgi_finish_request')){
define('NO_FFR', true);
function fastcgi_finish_request(){}
}
set_exception_handler('exception_handler');
define('LAMPCMS_PATH', realpath(dirname(__FILE__)));
$libDir = LAMPCMS_PATH.DIRECTORY_SEPARATOR.'lib';
$lampcmsClasses = $libDir.DIRECTORY_SEPARATOR.'Lampcms'.DIRECTORY_SEPARATOR;
require $lampcmsClasses.'Interfaces'.DIRECTORY_SEPARATOR.'All.php';
require $lampcmsClasses.'Exception.php';
require $lampcmsClasses.'Object.php';
require $lampcmsClasses.'Responder.php';
require $lampcmsClasses.'Mongo'.DIRECTORY_SEPARATOR.'Collections.php';
require LAMPCMS_PATH.DIRECTORY_SEPARATOR.'Mycollections.php';
require $lampcmsClasses.'Ini.php';
require $lampcmsClasses.'Log.php';
require $lampcmsClasses.'Request.php';
require $lampcmsClasses.DIRECTORY_SEPARATOR.'Mongo'.DIRECTORY_SEPARATOR.'DB.php';
require $lampcmsClasses.DIRECTORY_SEPARATOR.'Mongo'.DIRECTORY_SEPARATOR.'Doc.php'; // User extends it
require $lampcmsClasses.'User.php'; // User is always used
require $lampcmsClasses.'SplClassLoader.php';
require $lampcmsClasses.'Registry.php';
require $lampcmsClasses.'Template'.DIRECTORY_SEPARATOR.'Fast.php';
/**
* Points.php is in non-standard directory,
* in fact this file is not even included in distro
* User must rename Points.php.dist to Points.php
* That's why we should manually included it
* because autoloader will not be able to find it.
* This file only contains a few constants - it's cheap
* to include it every time, and with APC cache it will
* be cached.
*/
require LAMPCMS_PATH.DIRECTORY_SEPARATOR.'Points.php';
/**
* Custom error handle
* the purpose of this is to catch
* the catchable fatal error, intoduced in php 5.2.0
* We use it with type hinting in class constructors
* if the object passed to class constructor is not of the same type
* as hinted, it generates a catchable fatal error.
* The only way to catch it in the normal way is to set custom error handler
* like this one, then re-throw exception and it will then be caught.
*
* This error handler also turns any php error into
* a ErrorException
*
* So almost any error will be displayed in a better way on
* the page, will be logged and email will be sent to admin.
*
*
* @param integer $errno
* @param string $errstr
* @param string $errfile
* @param integer $errline
* @return true
*/
function LampcmsErrorHandler($errno, $errstr, $errfile, $errline)
{
$errLevel = error_reporting();
//echo 'Booooooooo ' .$errLevel. ' '.$errno.' '. $errstr.' '. $errfile.' '. $errline.'<br>';
if ($errno === E_RECOVERABLE_ERROR) {
d($errfile.' '.$errline.' '.$errstr);
$e = 'Caught catchable fatal error in file: %1$s<br>
on line: %2$s
<br><br>
Error message: <br>
%3$s<br>';
$err = vsprintf($e, array($errfile, $errline, $errstr));
throw new Lampcms\DevException($err);
} else {
if ($errLevel === 0) {
return;
}
/**
* If error level falls within our
* error reporting mask, then throw an ErrorException
*/
if ($errLevel & $errno) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
}
return true;
}
$old_error_handler = set_error_handler("LampcmsErrorHandler");
// autoloader here
require 'autoload.php';
$Registry = \Lampcms\Registry::getInstance();
try{
$oINI = $Registry->Ini;
$dataDir = $oINI->LAMPCMS_DATA_DIR;
$dataDir = rtrim($dataDir, '/');
define('LAMPCMS_WWW_DIR', LAMPCMS_PATH.DIRECTORY_SEPARATOR.\Lampcms\WWW_DIR.DIRECTORY_SEPARATOR);
define('LAMPCMS_DEVELOPER_EMAIL', $oINI->EMAIL_DEVELOPER);
define('LAMPCMS_SALT', $oINI->SALT);
define('LAMPCMS_COOKIE_SALT', $oINI->COOKIE_SALT);
define('LAMPCMS_DEFAULT_LANG', $oINI->DEFAULT_LANG);
define('LAMPCMS_DEFAULT_LOCALE', $oINI->DEFAULT_LOCALE);
define('LAMPCMS_TR_DIR', $oINI->TRANSLATIONS_DIR);
define('LAMPCMS_COOKIE_DOMAIN', $oINI->COOKIE_DOMAIN );
define('LAMPCMS_IMAGE_SITE', $oINI->IMAGE_SITE);
define('LAMPCMS_AVATAR_IMG_SITE', $oINI->AVATAR_IMG_SITE);
if (!empty($dataDir)) {
define('LAMPCMS_DATA_DIR', $dataDir.DIRECTORY_SEPARATOR);
} else {
define('LAMPCMS_DATA_DIR', LAMPCMS_WWW_DIR.'w'.DIRECTORY_SEPARATOR);
}
} catch(Lampcms\IniException $e){
throw new \OutOfBoundsException($e->getMessage());
}
/**
* First thing is to set our timezone
*/
if (false === date_default_timezone_set($oINI->SERVER_TIMEZONE)) {
throw new \Lampcms\DevException('Invalid name of "SERVER_TIMEZONE" in !config.ini constant. The list of valid timezone names can be found here: http://us.php.net/manual/en/timezones.php');
}
/**
* The DEBUG is automatically enabled for
* users whose ip addresses are added to
* MY_IP section of config.inc
* or if script is run from console
*/
$myIP = \Lampcms\Request::getIP();
$aMyIPs = $oINI->offsetGet('MY_IP');
$debug = $oINI->DEBUG;
if ($debug || isset($aMyIPs[$myIP]) || defined('SPECIAL_LOG_FILE')) {
define('LAMPCMS_DEBUG', true);
error_reporting(E_ALL | E_DEPRECATED);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('warn_plus_overloading', 1);
/**
* Turn on session garbage collection
* to be run at every session start
* to give us consistant behaviour
* in debug mode
* Session expiration is 5 minutes
* which means when logged in without
* using "remember me" option,
* you supposed to be logged out after 5 minutes
* of inactivity. The only way to test it
* is to login, then after 6-7 minutes access site
* with a different browser. Then go to the browser with logged
* in user and try to use any other link. User should not
* be logged in anymore at this time.
*/
ini_set("session.gc_maxlifetime", "300");
ini_set('session.gc_probability', "1");
ini_set('session.gc_divisor', "1");
} else {
define('LAMPCMS_DEBUG', false);
error_reporting(E_ALL ^ E_WARNING);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set('warn_plus_overloading', 0);
}
define('LOG_FILE_PATH', $oINI->LOG_FILE_PATH);
/**
* Empty the log file if
* necessary
*/
/**
* LOG_PER_SCRIPT
* will return string '1' for true
* or empty string for false
*/
if((true === LAMPCMS_DEBUG) && ('' !== LOG_FILE_PATH) && (true === (bool)$oINI->LOG_PER_SCRIPT) && !\Lampcms\Request::isAjax()){
file_put_contents(LOG_FILE_PATH, PHP_SAPI.' '.print_r($_SERVER, 1), LOCK_EX);
}
/**
* Shortcuts to log debug and log error
* MUST BE CALLED after DEBUG MODE and LOG_FILE_PATH
* has been defined
*/
function d($message){
if(defined('LAMPCMS_DEBUG') && true === LAMPCMS_DEBUG){
\Lampcms\Log::d($message, 2);
}
}
function e($message){
\Lampcms\Log::e($message, 2);
}
/**
* Must be called here AFTER autoloaders have
* been registered because it relies on autoloaders
* to find observer classes
*/
$Registry->registerObservers();