forked from scott-panton/Bulletin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.php
501 lines (420 loc) · 16.4 KB
/
core.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/**
*
* This file contains functions common to all other source files.
*
* Copyright (c) 2003-2005 Chris Goerner
* Copyright (c) 2012-2013 Daniel Jolly
* Copyright (c) 2015 Scott Panton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/
$time_start = getmicrotime();
/**
* @return float Returns current time in microseconds
**/
function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* Sends HTML header
**/
function SendPageHeader($username,$password)
{
global $page_bgcolour, $page_title;
echo("<!DOCTYPE html><HTML><HEAD><TITLE>$page_title</TITLE>
<STYLE type=\"text/css\">
BODY{
background-color:\"$page_bgcolour\";
font-family:verdana,sans-serif;
font-size:9pt
}
TABLE{
font-family:verdana,sans-serif;
font-size:8pt
}
PRE{
font-family:sans-serif
}
</STYLE>
<!--Nicked from http://stackoverflow.com/a/15279599-->
<script type=\"text/javascript\">
var refresh_rate = 300; //In seconds
var last_user_action = 0;
function reset() {
last_user_action = 0;
console.log(\"Reset\");
}
setInterval(function () {
last_user_action++;
refreshCheck();
}, 1000);
function refreshCheck() {
if ((last_user_action >= refresh_rate && document.readyState == \"complete\")) {
window.location.href = \"$PHP_SELF?username=$username&password=$password\"; // If this is called no reset is needed
reset(); // We want to reset just to make sure the location reload is not called.
}
}
window.addEventListener(\"click\", reset, false);
window.addEventListener(\"mousemove\", reset, false);
window.addEventListener(\"keypress\", reset, false);
</script>
</HEAD>");
echo("<BODY>");
}
/**
* Sends HTML footer
**/
function SendPageFooter()
{
global $app_version, $app_name, $time_start, $show_server_software, $show_server_name, $show_update_time;
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "<small><p><a href=\"https://github.com/scott-panton/Bulletin\">$app_name</a> " . $app_version;
if ($show_pagetime) {
echo "<br>" . number_format($time, 3) . " seconds elapsed.";
}
if ($show_server_software) {
echo "<br>".$_SERVER["SERVER_SOFTWARE"]." ";
}
if ($show_server_name) {
echo "<br>Running on ".$_SERVER["SERVER_NAME"];
}
if ($show_update_time) {
$current_time = date("D g:ia");
echo "<br>Page updated: $current_time";
}
echo "</small></BODY></HTML>";
}
/**
* Sends page logo
**/
function SendPageTitle()
{
global $page_logo;
echo("<img border=\"0\" src=\"$page_logo\"></img>");
}
/**
* Gets the value of GET or POST variables
*
* @param string $nm name of variable to return
* @return string Returns value of GET or POST variable
**/
function getVAR($nm)
{
$tmp = "";
if (isset($_COOKIE['bulletin'][$nm])) {
$tmp = $_COOKIE['bulletin'][$nm];
} elseif (isset($_POST[$nm])) {
$tmp = $_POST[$nm];
} elseif (isset($_GET[$nm])) {
$tmp = $_GET[$nm];
}
return $tmp;
}
/**
* Will return the value of a particular field given the ID, table, and field name.
*
* @param $table The name of the table
* @param $id The ID of the record
* @param $field The name of the field
* @return string The value of the field
**/
function getValueFromID($table, $id, $field)
{
$result = do_mysql_query("SELECT * FROM $table WHERE ID=$id");
$tempOutput = "";
while ($row = mysql_fetch_array($result)) {
$tempOutput = $row[$field];
}
return $tempOutput;
}
/**
* Gets the highest ID in a table
*
* @param $table The name of the table
* @return integer The highest ID found
**/
function getLastID($table)
{
$result = do_mysql_query("SELECT * FROM $table ORDER BY ID");
$tmp = "0";
while ($row = mysql_fetch_array($result)) {
$tmp = $row["ID"];
}
return $tmp;
}
/**
* The function queries the database for the number of records in a table. If $condition is not
* empty then it will be used to filter records.
*
* @param $table The name of the table
* @param string $condition SQL compliant condition
* @return integer The number of rows
**/
function rowCount($table,$condition = "")
{
if ($condition == "") {
$result = do_mysql_query("SELECT COUNT(*) AS count FROM $table");
} else {
$result = do_mysql_query("SELECT COUNT(*) AS count FROM $table WHERE $condition");
}
$row = mysql_fetch_assoc($result);
return $row['count'];
}
function do_mysql_query($query) {
$result = mysql_query("$query");
if (!$result) {
echo "<p>Error while performing SQL query!<p>" . mysql_error();
echo "<p>Query: $query";
exit();
}
return $result;
}
function sendNavBar($links,$username,$password,$display_date) {
global $PHP_SELF, $table_heading_bgcolour;
echo "<table width=100%><tr bgcolor=$table_heading_bgcolour>";
foreach ($links as $l) {
switch($l){
case "home":
echo "<td align=center><a href=\"$PHP_SELF?username=$username&password=$password\">Today</a></td>";
break;
case "edit":
echo "<td align=center><a href=\"$PHP_SELF?display_date=$display_date&edit=on&username=$username&password=$password\">Edit Mode</a></td>";
break;
case "logout":
echo "<td align=center><a href=\"$PHP_SELF?action=logout\">Log Out</a></td>";
break;
case "create_notice":
echo "<td align=center><a href=\"$PHP_SELF?action=create_notice&display_date=$display_date&username=$username&password=$password\">Create Notice</a></td>";
break;
case "add_event":
echo "<td align=center><a href=\"$PHP_SELF?action=add_event&display_date=$display_date&username=$username&password=$password\">Add Event</a></td>";
break;
case "previous":
$tempdate = mktime (0,0,0,date("m",$display_date) ,date("d",$display_date)-1,date("Y",$display_date));
echo "<td align=center><a href=\"$PHP_SELF?display_date=$tempdate&username=$username&password=$password\">Previous Day</a></td>";
break;
case "next":
$tempdate = mktime (0,0,0,date("m",$display_date) ,date("d",$display_date)+1,date("Y",$display_date));
echo "<td align=center><a href=\"$PHP_SELF?display_date=$tempdate&username=$username&password=$password\">Next Day</a></td>";
break;
case "manage_users":
echo "<td align=center><a href=\"$PHP_SELF?action=manage_users&username=$username&password=$password\">Manage Users</a></td>";
break;
} // switch
} // foreach
echo "</tr></table>";
}
function setSetting($setting_name, $setting_value) {
global $settings_table;
$result = do_mysql_query("DELETE FROM $settings_table WHERE name='$setting_name'");
$result = do_mysql_query("INSERT INTO $settings_table (ID,name,value) VALUES ('','$setting_name','$setting_value')");
}
function authenticate($username,$password) {
global $users_table;
$username = strtolower($username);
$result = do_mysql_query("SELECT * FROM $users_table WHERE username='$username'");
$row = mysql_fetch_array($result);
if ($password==$row["password"]) {
return true;
} else {
return false;
}
}
function getImportanceList($default="") {
global $importance_table;
$result = do_mysql_query("SELECT * FROM $importance_table");
$tempOutput = "<select size=\"1\" name=\"lstimportance\">";
while ($row = mysql_fetch_array($result)) {
if ($row["ID"] == $default) { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=".$row["ID"]." $selected>".$row["importance"]."</option>";
}
$tempOutput .= "</select>";
return $tempOutput;
}
function getUserList() {
global $users_table;
$result = do_mysql_query("SELECT * FROM $users_table ORDER BY realname");
$tempOutput = "<select size=\"3\" name=\"lstusers[]\">";
$tempOutput .= "<option value=\"-1\">Staff and Students</option>";
while ($row = mysql_fetch_array($result)) {
if ($row["username"]!="admin") {
$selected="";
if ($row["ID"]=="3") {$selected = "selected";}
$tempOutput .= "<option $selected value=".$row["ID"].">".$row["realname"]." only</option>";
}
}
$tempOutput .= "</select>";
return $tempOutput;
}
function getEditUserList($nid) {
global $permissions_table,$users_table;
$result = do_mysql_query("SELECT * FROM $users_table ORDER BY realname");
$tempOutput = "<select size=\"3\" name=\"lstusers[]\">";
$p_result = do_mysql_query("SELECT * FROM $permissions_table WHERE notice_id='$nid' AND user_id='-1'");
if (mysql_num_rows($p_result)) { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"-1\" $selected>Staff and Students</option>";
while ($row = mysql_fetch_array($result)) {
if ($row["username"]!="admin") {
$id = $row["ID"];
$p_result = do_mysql_query("SELECT * FROM $permissions_table WHERE notice_id='$nid' AND user_id='$id'");
if (mysql_num_rows($p_result)) { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=$id $selected>".$row["realname"]." only</option>";
}
}
$tempOutput .= "</select>";
return $tempOutput;
}
function getDateList($multi=true,$default) {
if ($multi) {
$tempOutput = "<select size=\"5\" name=\"lstdate[]\" multiple>";
} else {
$tempOutput = "<select size=\"5\" name=\"lstdate[]\">";
}
$tempdate = mktime (0,0,0,date("m") ,date("d"),date("Y"));
if ($tempdate == $default) { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"$tempdate\" $selected>Today (".date("jS \of F",$tempdate).")</option>";
$tempdate = mktime (0,0,0,date("m") ,date("d")+1,date("Y"));
if ($tempdate == $default) { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"$tempdate\" $selected>Tomorrow (".date("jS \of F",$tempdate).")</option>";
for ($i=2; $i<100; $i++) {
$tempdate = mktime (0,0,0,date("m") ,date("d")+$i,date("Y"));
if (date("l",$tempdate)!="Saturday" AND date("l",$tempdate)!="Sunday") {
if ($tempdate == $default) { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"$tempdate\" $selected>".date("l \\t\h\\e jS \of F",$tempdate)."</option>";
}
}
$tempOutput .= "</select>";
return $tempOutput;
}
function getTypeFaceList($default="") {
$tempOutput = "<select size=\"1\" name=\"lsttypeface\">";
if ($default == "Default") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"Default\" $selected>Default</option>";
if ($default == "Times New Roman") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"Times New Roman\" $selected>Times New Roman</option>";
if ($default == "Arial") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"Arial\" $selected>Arial</option>";
if ($default == "Courier New") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"Courier New\" $selected>Courier New</option>";
$tempOutput .= "</select>";
return $tempOutput;
}
function getFontSizeList($default="") {
$tempOutput = "<select size=\"1\" name=\"lstfontsize\">";
if ($default == "2") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"2\" $selected>Normal</option>";
if ($default == "3") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"3\" $selected>+1</option>";
if ($default == "4") { $selected="selected"; } else { $selected=""; }
$tempOutput .= "<option value=\"4\" $selected>+2</option>";
$tempOutput .= "</select>";
return $tempOutput;
}
function haveaccess($username,$notice_id) {
global $users_table,$permissions_table;
$result = do_mysql_query("SELECT * FROM $users_table WHERE username='$username' LIMIT 1");
$row = mysql_fetch_array($result);
$user_id=$row["ID"];
$tempOutput = FALSE;
$result = do_mysql_query("SELECT * FROM $permissions_table WHERE notice_id=$notice_id");
while ($row = mysql_fetch_array($result)) {
if ($row["user_id"]==$user_id OR $row["user_id"]=="-1") {
$tempOutput = TRUE;
}
}
return $tempOutput;
}
function send_notice($type,$msg) {
global $page_bgcolour, $table_heading_bgcolour;
switch($type){
case "ERROR":
$text = "<font color=red><big>$msg</big></font>";
$img = "<img src=\"images/stop.png\">";
break;
case "OK":
$text = "<font color=green><big>$msg</big></font>";
$img = "<img src=\"images/ok.png\">";
break;
case "QUESTION":
$text = "<font><big>$msg</big></font>";
$img = "<img src=\"images/question.png\">";
break;
case "EXCLAMATION":
$text = "<fontn><big>$msg</big></font>";
$img = "<img src=\"images/exclamation.png\">";
break;
case "NONE":
$text = "<big>$msg</big>";
$img = "";
break;
}
return "<table bgcolor=$page_bgcolour style=\"border: 3px solid $table_heading_bgcolour; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1\"><tr><td>$img</td><td>$text</td></tr></table>";
}
function formattext($text) {
$temp = trim($text);
$temp = str_replace("\n* ","\n<li>",$temp);
$temp = str_replace("\n","<br>\n",$temp);
return $temp;
}
# PHP Calendar (version 2.3), written by Keith Devens
# http://keithdevens.com/software/php_calendar
# see example at http://keithdevens.com/weblog
# License: http://keithdevens.com/software/license
function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()){
global $page_bgcolour; //cgoerner - added
$first_of_month = gmmktime(0,0,0,$month,1,$year);
#remember that mktime will automatically correct if invalid dates are entered
# for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
# this provides a built in "rounding" feature to generate_calendar()
$day_names = array(); #generate all the day names according to the current locale
for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
$day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name
list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
$weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
$title = htmlentities(ucfirst($month_name)).' '.$year; #note that some locales don't capitalize month and day names
#Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
@list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span> ';
if($n) $n = ' <span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
$calendar = '<table class="calendar">'."\n".
'<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";
if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
#if day_name_length is >3, the full name of the day will be printed
foreach($day_names as $d)
$calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
$calendar .= "</tr>\n<tr>";
}
if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'"> </td>'; #initial 'empty' days
for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
if($weekday == 7){
$weekday = 0; #start a new week
$calendar .= "</tr>\n<tr>";
}
if(isset($days[$day]) and is_array($days[$day])){
@list($link, $classes, $content) = $days[$day];
if(is_null($content)) $content = $day;
//cgoerner - added bgcolour here
$calendar .= "<td bgcolor=$page_bgcolour ".($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
}
else $calendar .= "<td>$day</td>";
}
if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'"> </td>'; #remaining "empty" days
return $calendar."</tr>\n</table>\n";
}
?>