-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamiCal.php
126 lines (96 loc) · 3.89 KB
/
examiCal.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
<?php
date_default_timezone_set("Europe/London");
/**
* Trim a string, breaking at spaces if possible!
*/
function smartTruncate($string, $limit = 50, $break=' ', $pad='...')
{
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}
// If a $_POST was sent
if(!empty($_POST)) {
// Basic scripting protection
if(isset($_SERVER['HTTP_REFERER']) && isset($_SERVER['SERVER_NAME'])
&& strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) === false)
die("You need to post from the original domain.");
// Basic robot trap
if(isset($_POST['email']) && $_POST['email'] != "email@email.com"
&& isset($_POST['comment']) && $_POST['comment'] != "")
die("You look like a robot, please ignore the email and comment fields");
// Dumb validation
if(empty($_POST['data']))
die("You posted an empty form.");
// Read user input
$string = trim($_POST['data']);
// Split the posted data into rows
$rows = preg_split("/(\r?\n)/", $string);
// Simple error detection
if(empty($rows))
die("You appear to have posted no information.");
// Forces file download instead of web page
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=calendar.ics');
// iCal header
print("BEGIN:VCALENDAR\r\n");
print("VERSION:2.0\r\n");
print("PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n");
print("UID:" . md5(uniqid(mt_rand(), true)) . "@PezCuckow.com\r\n");
// Basic log
$fh = @fopen("simpleLog.txt", 'a');
// Make life easier
$mapping = array(
'code', 'name', 'date', 'location', 'seat', 'start', 'end'
);
$mapping = array_flip($mapping);
// For every row in the input
foreach($rows as $row){
// Should be tab separated
$data = array_filter(explode("\t", $row));
// Sometimes empty columns sneak in...
$data = array_filter($data);
// Reindex now some columns may have been deleted
$data = array_values($data);
// MyManchester has a thing for trailing whitespace
foreach($data as &$field) {
$field = trim($field);
}
// Simple file log for debugging
@fwrite($fh, '['. date("Y-m-d H:i:s") . '] ' . $row. "\n");
//Exam Code Title Date Location Seat Start Finish
if(empty($data)||empty($data[6])||strtolower($data[$mapping['code']])=="exam code") continue;
$start = strtotime($data[$mapping['date']]." ".$data[$mapping['start']]);
$end = strtotime($data[$mapping['date']]." ".$data[$mapping['end']]);
// List of buildings at Uom, all campuses as array.
$lines = file("buildings.txt", FILE_IGNORE_NEW_LINES);
// Loop through all building names, try to find one that matches
$fixedLocation=$data[$mapping['location']];
foreach ($lines as $value)
if (strpos($data[$mapping['location']], $value) !== FALSE) {
$fixedLocation=$value . ", University of Manchester";
}
print("BEGIN:VEVENT\r\n");
print("DTSTART:".date("Ymd", $start)."T".date("His", $start)."\r\n");
print("DTEND:".date("Ymd", $end)."T".date("His", $end)."\r\n");
print("SUMMARY:".smartTruncate($data[$mapping['name']], 40)." (".$data[$mapping['code']].")\r\n");
print("DESCRIPTION:Title: ".$data[$mapping['name']]
."\\nCode: ".$data[$mapping['code']]
."\\nSeat: ".$data[$mapping['seat']]
."\\nStart: ".$data[$mapping['start']]
."\\nEnd: ".$data[$mapping['end']]
."\\n\\nRaw\\n". str_replace("\t", " - ", $row)."\r\n");
print("LOCATION:".$fixedLocation."\r\n");
print("END:VEVENT\r\n");
}
@fclose($fh);
print("END:VCALENDAR\r\n");
exit;
} else {
include("examiCal/template.php");
}
?>