-
Notifications
You must be signed in to change notification settings - Fork 0
/
navbar.php
161 lines (137 loc) · 5.57 KB
/
navbar.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
<?php
require "constants.php";
require "database.php";
require "admin_profile_utils.php";
session_start();
define("HOME", "home");
define("CONNECTIONS", "connections");
define("VACANCIES", "vacancies");
define("NOTIFICATIONS", "notifications");
define("SEARCH", "search");
define("PROFILE", "profile");
define("ADMINISTRATION", "administration");
define("ACTIVE", "class=\"nav-link active\" aria-current=\"page\" ");
define ("INACTIVE", "class=\"nav-link\" ");
/**
* The user name that has been set by the session check in this navigation php file
*/
$username = "";
/**
* The type of user logged in
*/
$user_type = "";
/**
* Goes to the login page
*/
function goToLogin() {
$query = parse_url($_SERVER["REQUEST_URI"], PHP_URL_QUERY);
$script_name = htmlspecialchars($_SERVER["PHP_SELF"]);
if ($query != null) {
$script_name .= "?{$query}";
}
$data = array('goto' => $script_name);
$url = "login.php?".http_build_query($data);
header("Location: $url");
exit;
}
if (empty($_SERVER["HTTPS"]) && $_SERVER["SERVER_NAME"] != "localhost") {
header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], true, 301);
exit;
}
if (isset($_SESSION[LOGGED_IN]) && $_SESSION[LOGGED_IN] == true) {
if (!isset($_SESSION[USERNAME]) || !isset($_SESSION[USER_TYPE])) {
goToLogin();
}
$username = $_SESSION[USERNAME];
$user_type = $_SESSION[USER_TYPE];
} else {
if (isset($_COOKIE[USERNAME]) && isset($_COOKIE[USER_TYPE])) {
$username = $_SESSION[USERNAME] = $_COOKIE[USERNAME];
$user_type = $_SESSION[USER_TYPE] = $_COOKIE[USER_TYPE];
$_SESSION[LOGGED_IN] = true;
} else {
goToLogin();
}
}
if ($user_type != ADMIN) {
if (checkBanned($username) || checkBlacklist($username)) {
header("Location: logout.php");
}
}
/**
* Checks if the page name provided to generateNavBar is valid
*/
function checkPageName($page) {
if (empty($page) || ($page != HOME && $page != CONNECTIONS && $page != VACANCIES
&& $page != NOTIFICATIONS && $page != PROFILE && $page != SEARCH && $page != ADMINISTRATION)) {
die("Invalid page option given to navbar.php. Value: {$page}");
}
}
/**
* Generates the navigation bar. The page provided is the constant defined
* for the selected page e.g. HOME for home page, CONNECTIONS for connections page etc.
* If the name is not a defined constant, an error will be thrown
*/
function generateNavBar($page) {
global $user_type;
checkPageName($page);
echo "<script src=\"https://cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper.min.js\" integrity=\"sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi\" crossorigin=\"anonymous\"></script>
<script src=\"https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.min.js\" integrity=\"sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG\" crossorigin=\"anonymous\"></script>
<!-- style=\"border: 1px solid red;\" -->
<nav class=\"navbar navbar-expand-lg navbar-light bg-light sticky-top\">
<div class=\"container\">
<a class=\"navbar-brand\" href=\"#\">
<img src=\"images/logo.png\" alt=\"Logo\" style=\"width:125px; \">
</a>
<button class=\"navbar-toggler\" type=\"button\" data-bs-toggle=\"collapse\" data-bs-target=\"#navbarSupportedContent\" aria-controls=\"navbarSupportedContent\" aria-expanded=\"false\" aria-label=\"Toggle navigation\">
<span class=\"navbar-toggler-icon\"></span>
</button>
<div class=\"collapse navbar-collapse\" id=\"navbarSupportedContent\">
<ul class=\"navbar-nav me-auto mb-2 mb-lg-100\">";
$class_name = ($page == HOME) ? ACTIVE:INACTIVE;
echo "<li class=\"nav-item\">
<a {$class_name} href=\"feed.php\">Home</a>
</li>";
if ($user_type == TEACHER) {
$class_name = ($page == CONNECTIONS) ? ACTIVE:INACTIVE;
echo "<li class=\"nav-item\">
<a {$class_name} href=\"connections.php\">Network</a>
</li>";
}
$class_name = ($page == VACANCIES) ? ACTIVE:INACTIVE;
echo "<li class=\"nav-item\">
<a {$class_name} href=\"vacancies.php\">Vacancies</a>
</li>";
$class_name = ($page == NOTIFICATIONS) ? ACTIVE:INACTIVE;
echo "<li class=\"nav-item\">
<a {$class_name} href=\"notifications.php\">Notifications</a>
</li>";
if ($user_type != ADMIN) {
$class_name = ($page == PROFILE) ? ACTIVE:INACTIVE;
$link = ($user_type == TEACHER) ? "teacher_profile.php":"organisation_profile.php";
echo "<li class=\"nav-item\">
<a {$class_name} href=\"{$link}\">Profile</a>
</li>";
}
if($page != SEARCH) {
echo "</ul><form class=\"d-flex\">
<input class=\"form-control me-2\" name =\"q\" type=\"search\" placeholder=\"Search\" aria-label=\"Search\">
<button class=\"btn btn-outline-success\" type=\"submit\">Search</button>
</form>";
if(isset($_GET['q'])) {
$q = $_GET['q'];
header('Location: '. 'search.php?q=' . $q);
}
} else {
echo "</ul>";
}
echo "<a class=\"nav-link\" href=\"logout.php\">Logout</a>";
if ($user_type == ADMIN) {
$class_name = ($page == ADMINISTRATION) ? ACTIVE:INACTIVE;
echo "<a {$class_name} href=\"administration.php\">Administration</a>";
}
echo "</div>
</div>
</nav>";
}
?>