forked from Pezmc/Exam-to-iCal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamiCal.php
99 lines (74 loc) · 2.84 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
<?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');
// For every row in the input
foreach($rows as $row){
$data = explode("\t", $row);
// 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[0])=="exam code") continue;
$start = strtotime($data[2]." ".$data[5]);
$end = strtotime($data[2]." ".$data[6]);
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[1], 40)." (".$data[0].")\r\n");
print("DESCRIPTION:Title: ".$data[1]
."\\nCode: ".$data[0]
."\\nSeat: ".$data[4]
."\\nStart: ".$data[5]
."\\nEnd: ".$data[6]
."\\n\\nRaw\\n". str_replace("\t", " - ", $row)."\r\n");
print("LOCATION:".$data[3]."\r\n");
print("END:VEVENT\r\n");
}
@fclose($fh);
print("END:VCALENDAR\r\n");
exit;
} else {
include("examiCal/template.php");
}
?>