-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.php
203 lines (169 loc) · 6.13 KB
/
functions.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
<?php
function url_for($script_path) {
// add the leading '/' if not present
if($script_path[0] != '/') {
$script_path = "/" . $script_path;
}
return WWW_ROOT . $script_path;
}
function u($string="") {
return urlencode($string);
}
function raw_u($string="") {
return rawurlencode($string);
}
function h($string="") {
return htmlspecialchars($string);
}
function error_404() {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
exit();
}
function error_500() {
header($_SERVER["SERVER_PROTOCOL"] . " 500 Internal Server Error");
exit();
}
function redirect_to($location) {
header("Location: " . $location);
exit;
}
function is_post_request() {
return $_SERVER['REQUEST_METHOD'] == 'POST';
}
function is_get_request() {
return $_SERVER['REQUEST_METHOD'] == 'GET';
}
function display_errors($errors=array()) {
$output = '';
if(!empty($errors)) {
$output .= "<div class=\"errors\">";
$output .= "Please fix the following errors:";
$output .= "<ul>";
foreach($errors as $error) {
$output .= "<li>" . h($error) . "</li>";
}
$output .= "</ul>";
$output .= "</div>";
}
return $output;
}
//------------------------------------------------------------------------
// Used to convert a user-created URL into an embeddable URL.
// Example: The typical URL pasted in from users will look like this: https://www.youtube.com/watch?v=CgSs3OvTnUQ
// But this will not work in an embedded iFrame and so must be converted to: https://www.youtube.com/embed/CgSs3OvTnUQ
//
function mangleurl($url, $urltype = "") {
if (strcasecmp($urltype, 'url') == 0 and strcasecmp(substr($url, 0, 3), 'http') != 0) {
$url = 'http://' . $url;
}
$urlchunks = parse_url(trim($url));
if (DEBUG_MODE == 'ONxx') {
echo 'DEBUG MODE: ' . dirname(__FILE__).'.mangleurl()<br/>';
echo var_dump($url) . '<br/>';
echo var_dump($urlchunks) . '<br/>';
echo '<br/>';
}
$baseurl = $url; // in case we don't need to do anything, send back what we were given
if (strpos(strtolower($urlchunks['host']), 'youtube') > 0) {
$baseurl = $urlchunks['scheme']."://".$urlchunks['host']."/embed/";
if (strtolower(substr($urlchunks['query'], 0, 2)) == "v=") {
$youtubeid = substr($urlchunks['query'], 2);
}
else {
$youtubeid = $urlchunks['query'];
}
return $baseurl.$youtubeid;
}
if (strcasecmp(strtolower($urlchunks['host']), 'drive.google.com') == 0) {
if (strcasecmp($urltype, 'image') == 0) {
$baseurl = $urlchunks['scheme']."://".$urlchunks['host']."/thumbnail?";
$baseurl .= $urlchunks['query'].'&sz=w400-h400';
}
else {
$baseurl = 'https://docs.google.com/document/d/' . substr($urlchunks['query'], 3) . '/edit';
}
}
return $baseurl;
}
//------------------------------------------------------------------------
// An item entry from the DB might have multiple URLs sepearated by ';' characters
// This function takes that string and returns an array of those items
// it will always return an array that is at least one item in size
// Example https://www.youtube.com/watch?v=Md0CGAtOPEw;https://www.youtube.com/watch?v=0uPQjgfjl2Q;https://www.youtube.com/watch?v=yA83mhXV3iU;https://www.youtube.com/watch?v=pwFy7eP-IGA
function handlemultipleitems($item) {
$urlarray = explode(';',$item);
// if (DEBUG_MODE == 'ON') {
// echo 'DEBUG MODE: ' . dirname(__FILE__).'.handlemultipleitems()<br/>';
// echo var_dump($urlarray);
// echo '<br/>';
// }
return $urlarray;
}
//------------------------------------------------------------------------
// An item entry from the DB might have multiple URLs sepearated by ';' characters
// This function counts how many items are in that long string
// it can return 0 if the string is empty
// Example https://www.youtube.com/watch?v=Md0CGAtOPEw;https://www.youtube.com/watch?v=0uPQjgfjl2Q;https://www.youtube.com/watch?v=yA83mhXV3iU;https://www.youtube.com/watch?v=pwFy7eP-IGA
function countitems($item) {
$urlarray = explode(';',$item);
$ctr = 0;
// if (DEBUG_MODE == 'ON') {
// echo 'DEBUG MODE: ' . dirname(__FILE__).'.countitems()<br/>';
// echo var_dump($urlarray);
// echo count($urlarray);
// echo '<br/>';
// }
foreach ($urlarray as $subitem) {
if (trim($subitem) != '') {
$ctr += 1;
}
}
return $ctr;
}
function is_logged_in() {
// Having a admin_id in the session serves a dual-purpose:
// - Its presence indicates the admin is logged in.
// - Its value tells which admin for looking up their record.
if (!isset($_SESSION['logged_in'])) {return false;}
if ($_SESSION['logged_in'] != true) {return false;}
return true;
}
function is_super_admin() {
// Having a admin_id in the session serves a dual-purpose:
// - Its presence indicates the admin is logged in.
// - Its value tells which admin for looking up their record.
if (!isset($_SESSION['logged_in'])) {return false;}
if (!isset($_SESSION['role'])) {return false;}
return ($_SESSION['logged_in'] == true and $_SESSION['role'] == 'SUPER-ADMIN');
}
function is_admin() {
// Having a admin_id in the session serves a dual-purpose:
// - Its presence indicates the admin is logged in.
// - Its value tells which admin for looking up their record.
if (!isset($_SESSION['logged_in'])) {return false;}
if (!isset($_SESSION['role'])) {return false;}
if (is_super_admin() == true) {return true;}
return ($_SESSION['logged_in'] == true and $_SESSION['role'] == 'ADMIN');
}
function is_user() {
// Having a admin_id in the session serves a dual-purpose:
// - Its presence indicates the admin is logged in.
// - Its value tells which admin for looking up their record.
if (is_super_admin() == true) {return true;}
if (is_admin() == true) {return true;}
if (!isset($_SESSION['logged_in'])) {return true;}
if (!isset($_SESSION['role'])) {
$_SESSION['role'] = 'USER';
return true;
}
return ($_SESSION['logged_in'] == true and $_SESSION['role'] == 'USER');
}
function show_flash_message() {
if (isset($_SESSION['action_message'])) {
$message = $_SESSION['action_message'];
unset($_SESSION['action_message']);
return $message;
}
return NULL;
}
?>