-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.php
91 lines (70 loc) · 2.68 KB
/
common.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
<?php
if (!ob_get_level())
ob_start();
define('CLASSES_DIR', dirname(__FILE__) .'/classes/');
define('HTTPS', @$_SERVER['HTTPS'] != '' && @$_SERVER['HTTPS'] != 'off');
define('SERVER_URL',
'http'. (HTTPS ? 's' : '') .'://'.
$_SERVER['SERVER_NAME'] . (@$_SERVER['SERVER_PORT'] != (HTTPS ? 443 : 80) ? ':'.$_SERVER['SERVER_PORT'] : '') .
(@$_SERVER['PHP_AUTH_USER'] ? '@'. @$_SERVER['PHP_AUTH_USER'] .':'. @$_SERVER['PHP_AUTH_PW'] : '') .'/');
define('BASE_URL', SERVER_URL . substr(dirname($_SERVER['SCRIPT_NAME']), 1) .'/');
/*
** Autoloads a class from this folder or a subfolder (if the class is namespaced)
*/
function __autoload( $class ) {
require_once( CLASSES_DIR . str_replace("\\", "/", $class) .'.php' );
} // __autoload( $class )
require_once("creds.php");
$db = new DBInterface("localhost", $dbName, $dbUsername, $dbPassword);
/**
* Initiates a PHP session if a session ID cookie is found.
*/
if (array_key_exists(session_name(), $_COOKIE)) {
try {
session_start();
$loginSession = $db->readLoginSession($_COOKIE[session_name()]);
} catch (Exception $ex) {
// Unable to restore session for some reason
session_destroy();
unset($loginSession);
}
}
function doUnauthorizedRedirect() {
while (@ob_end_clean());
header("Location: ". BASE_URL ."page.php/unauthorized");
exit;
} // doUnauthorizedRedirect()
function doUnauthenticatedRedirect() {
$pathInfo = '';
if (array_key_exists('PATH_INFO', $_SERVER)) {
$pathInfo = $_SERVER['PATH_INFO'];
if ($pathInfo{0} != '/')
$pathInfo = '/'. $pathInfo;
}
while (@ob_end_clean());
header("Location: ". BASE_URL ."index.php". $pathInfo);
exit;
} // doUnauthenticatedRedirect()
function getLoginRedirect(LoginSession $session, $page) {
if (!$page)
$page = ("/air");
if ($page{0} != '/')
$page = '/'. $page;
return BASE_URL ."page.php". $page;
} // getLoginRedirect(LoginSession $session)
function doLoginRedirect(LoginSession $session, $page) {
while (@ob_end_clean());
header("Location: ". getLoginRedirect($session, $page));
exit;
} // doLoginRedirect(LoginSession $session, $page)
function handleDBException($ex) {
echo '
<div class="alert alert-danger" style="vertical-align:top;position:absolute;left:20px;right:20px;">
<span class="glyphicon glyphicon-exclamation-sign" style="color:red;font-size:30px"></span>
<div style="display:inline-block;padding-left:10px;">
An error occurred while processing your request.<br/>'.
htmlentities($ex->getMessage()) .
'</div>
</div>
';
} // handleDBException($ex)