-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
168 lines (143 loc) · 5.71 KB
/
index.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
<?php
//Includes
include("db.php");
include("config.php");
include("email.php");
include("template.php");
//Start up sessions and initiate main class
session_start();
$main = new Main();
class Main {
//Setup some variables
public $config;
public $db;
public $page;
public $get;
public $pages = array("actionresult", "login", "logout", "make", "view", "refund", "product", "accounts", "societies");
public $title;
public $navbar = array();
public $user;
public $refresh = false;
public $loggedIn = false;
function __construct() {
$this->config = new Config();
$this->db = new Db($this);
$this->page = (isset($_GET["page"]) ? $_GET["page"] : $this->config->general["defaultPage"]);
$this->title = $this->config->general["defaultTitle"];
//Load user if logged in
if (isset($_SESSION["email"])) {
$this->loadUser($_SESSION["email"]);
$this->requireLogin();
}
//See if there is a 'get' to do
if (isset($_GET["get"])) $this->get = strtolower($_GET["get"]);
else unset($this->get);
//Add nav elements
$this->addNavElement("index.php", "Issue Receipt", false, true);
$this->addNavElement("index.php?page=view", "View Receipts", false, true);
$this->addNavElement("index.php?page=product", "Product Management", false, true);
$this->addNavElement("index.php?page=societies", "Society Management", true, true);
$this->addNavElement("index.php?page=accounts", "Account Management", true, true);
//Include requested page
if (!in_array($this->page, $this->pages)) {
$this->page = $this->config->general["defaultPage"];
}
include("pages/".$this->page."Page.php");
$child = new $this->page;
$child->run($this);
}
//Setup user from username
function loadUser($username) {
$this->user = $this->db->getUser($username);
if ($this->user != false)
$this->loggedIn = true;
}
//Display global page header
function displayHeader() {
//Format require information for template
$compiledTitle = $this->title . " | " . $this->config->general["titleSuffix"];
$userStatus = "";
if ($this->loggedIn)
$userStatus = "Welcome, " . $this->user->email . '. Click here to <a href="index.php?page=logout">logout</a>';
?>
<html>
<head>
<?php if ($this->refresh) echo '<meta http-equiv="refresh" content="3;url=index.php">'; ?>
<title><?php echo $compiledTitle; ?></title>
<link rel="shortcut icon" href="images/favicon.ico" />
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/custom-theme/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" href="css/uniform.default.css" />
<link rel="stylesheet" type="text/css" href="css/datatables.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<script type="text/javascript" src="js/jquery.uniform.min.js"></script>
<script type="text/javascript" src="js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="js/jquery.jeditable.min.js"></script>
<?php
if (file_exists("css/pages/" . $this->page . ".css")) echo '<link rel="stylesheet" type="text/css" href="css/pages/' . $this->page . '.css" />';
if (file_exists("js/pages/" . $this->page . ".js")) echo '<script type="text/javascript" src="js/pages/' . $this->page . '.js"></script>';
?>
</head>
<body>
<div class="navBarBack">
<ul>
<?php
//Echo navbar
foreach ($this->navbar as $element) {
if ((($element["login"] && $this->loggedIn) || !$element["login"]) && (($element["admin"] && $this->isAdmin()) || !$element["admin"]))
echo '<li><a href="' . $element["url"] . '">' . $element["title"] . '</a></li>';
}
?>
</ul>
</div>
<div class="mainContainer">
<div class="logoBar">
<b>e</b>-Receipts
</div>
<div class="titleBar">
<?php echo $this->title; ?>
<b><?php echo $userStatus; ?></b>
</div>
<div class="contentContainer">
<?php
}
//Display global footer
function displayFooter() {
?>
</div>
</div>
</body>
</html>
<?php
}
//Go to action result page
function actionResult($id) {
header("location:index.php?page=actionresult&type=".$id);
}
//Returns true if user is admin
function isAdmin() {
if ($this->loggedIn && in_array($this->user->email, $this->config->admin["adminAccounts"]))
return true;
return false;
}
//Check if user is logged in, if they aren't, redirect to the login page
function requireLogin() {
if (!$this->loggedIn) {
unset($_SESSION["email"]);
header("location:index.php?page=login&loggedin=true");
}
}
//Adds an element to the NavBar
function addNavElement($url, $title, $requireAdmin, $requireLogin) {
$this->navbar[] = array("url" => $url, "title" => $title, "admin" => $requireAdmin, "login" => $requireLogin);
}
//Issue a JSON error
function errorJSON($json, $msg) {
$json["error"] = $msg;
echo json_encode($json);
return;
}
}
?>