-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.php
executable file
·199 lines (160 loc) · 5.68 KB
/
template.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
<?php
require_once('db/obscure.php');
obscure(__FILE__);
try {
require_once('links.php');
//links.php is a declaration of constants which the Header class uses to
// construct the navigation links
} catch (Error $e) {
require_once('../links.php');
//allows for different header links based on what part of the site you're on
}
require_once(__DIR__ . '/db/user.php');
class Header {
public static bool $closeDbcSessionOnOutput = true;
public static string $head = '';
public static array $stylesheets = array(
'/stylesheets/main.css',
);
public static array $scripts = array();
public static function output(string $title = '', ?bool $closeDbcSession = null) {
if (Header::$done !== false) throw new Exception();
Header::$done = true;
$userToken = UserToken::getCurrentAuthenticated();
$closeDbcSession ??= static::$closeDbcSessionOnOutput;
if ($closeDbcSession) DBC::closeSession();
$title = htmlEscape(trim($title));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?= $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
foreach (Header::$stylesheets as $stylesheet) {
$stylesheet = htmlEscape(Request::getActualPath($stylesheet)) . '?' . rand(100000, 999999);
echo '<link rel="stylesheet" type="text/css" href="' . $stylesheet
. '">' . "\n";
}
foreach (Header::$scripts as $scriptName) {
$scriptPath = "/js/$scriptName.js";
$scriptPath = htmlEscape(Request::getActualPath($scriptPath)) . '?' . rand(100000, 999999);
echo '<script src="' . $scriptPath . '"></script>' . "\n";
}
echo static::$head;
?>
</head>
<body>
<header>
<!-- header stuff here -->
<div><h3>Welcome to Bill's Balls 'N Bricks Game</h3>
<?php
if ($title) {
echo "<h2>$title</h2>";
}
echo '</div><nav>';
foreach(array_merge(NAV_LINKS_GENERAL,
isset($userToken) ? NAV_LINKS_LOGGED_IN : NAV_LINKS_LOGGED_OUT)
as $filename => $linkText) {
if($filename === '/logout') {
$logoutAction = true;
$basename = basename($_SERVER['SCRIPT_NAME']);
if (key_exists($basename, REDIRECT_ON_LOGOUT)) {
$logoutAction = REDIRECT_ON_LOGOUT[$basename];
}
if (gettype($logoutAction) === 'string') {
if (substr($logoutAction, -1, 1) === '?') //add current query string
$logoutAction .= parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
$filename .= '?page=' . urlencode($logoutAction);
} else if ($logoutAction === true) {
$filename .= '?page=' . urlencode(basename($_SERVER['REQUEST_URI']));
} else if ($logoutAction !== false) throw new DBerror();
}
$filename = Request::getActualPath($filename);
?>
<a href="<?= $filename ?>">
<?= $linkText ?>
</a><?php
} ?>
</nav>
<?php
if ($userToken) { ?>
<span id="loggedInNotification"><span>You are logged in as:</span>
<?php
if($name = $userToken->getDisplayName()) { ?>
<span class="displayname"><?= $name ?></span>
<?php
} ?>
<span class="username">
<?= htmlEscape($userToken->getUsername()); ?>
</span>
</span><?php
}
?>
</header>
<main>
<hr />
<?php
}
private static bool $done = false;
public static function wasOutputed() : bool {
return Header::$done;
}
public static function outputIfNotAlready(?bool $closeDbcSession = null) {
if (!Header::$done) {
if (basename($_SERVER['SCRIPT_NAME']) === 'index.php') {
$title = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
$title = substr($title, strrpos($title, '/') + 1);
} else {
$title = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME);
}
$title[0] = strtoupper($title[0]);
static::output($title, $closeDbcSession);
}
}
private function __construct() {
throw new Exception();
// no instances allowed
}
}
class Footer {
//automatically outputs the Footer upon exiting the document, via the instance's destructor
public function __destruct() {
Header::outputIfNotAlready(false);
Footer::$done = true;
?>
</main>
<footer>
<hr />
Website made by Bill Anderson, Spring 2021 Intro to PHP class
<br />
(PHP Web Development with MySQL, Tue/Thur 12:30pm - 2:10pm)
</footer>
<?php
}
private static ?Footer $instance = null;
private static bool $done = false;
public static function output() {
if (Footer::$done === true || Footer::$instance === null) throw new Exception();
$instance = null; //calls the destruct method
}
private function __construct() {
if (Footer::$instance !== null || Footer::$done !== false)
throw new Exception();
}
public static function arm() {
if (Footer::$instance) throw new DBerror();
Footer::$instance = new Footer();
}
}
//initialize...
(function() {
Footer::arm();
$stylesheet = 'stylesheets/' . pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME)
. '.css';
if (file_exists($stylesheet)) {
Header::$stylesheets[] = $stylesheet;
}
})();
?>