-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicalparse.php
81 lines (70 loc) · 2.47 KB
/
icalparse.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
<?php
//convert ical to XML
function icalparse($string) {
$folder = "\r\n".' ';
$prop = str_replace($folder,'',$string);
$prop = str_replace('\;', ';',$prop);
$prop = str_replace('\:', ':',$prop);
$prop = str_replace('\,', ',',$prop);
$prop = str_replace('\"', '"',$prop);
$prop = str_replace(' ', ' ',$prop);
$prop = str_replace("\n".' ',' ',$prop);
$prop = str_replace("\r",'',$prop);
$prop = explode("\n",$prop);
$xml = '<?xml version="1.0" standalone="yes"?>'."\n";
$xml = '<?xml-stylesheet type="text/xsl" href="datesort.xsl"?>'."\n";
foreach($prop as $line) {
$matches = array();
// This matches PROPERTYNAME;ATTRIBUTES:VALUE
if (preg_match('/^([^:^;]*)(?:;([^:]*))?:(.*)$/',$line,$matches)) {
$propertyName = strtolower($matches[1]);
$attributes = strtolower($matches[2]);
$value = $matches[3];
if ($propertyName == 'begin') {
$xml .= '<'.strtolower($value).'>'."\n";
continue;
} elseif ($propertyName == 'end') {
$xml .= '</'.strtolower($value).'>'."\n";
continue;
}
$xml .= str_repeat(" ",$spaces);
$xml .= '<'.$propertyName;
if ($attributes) {
// There can be multiple attributes
$attributes = explode(';',strtolower($attributes));
foreach($attributes as $att) {
list($attName,$attValue) = explode('=',$att,2);
$xml .= ' '.$attName.'="'.$attValue.'"';
}
}
$xml .= '>'.htmlspecialchars($value).'</'.$propertyName.'>'."\n";
}
}
return $xml;
}
//Able to be used as XHTTP GET resource
$uri = $_GET['uri'] ?? '';
if ($uri){
$uri = str_replace('webcal://','http://',$uri);
$options = array('http' => array('user_agent' => 'PHP/7.0.27'));
$context = stream_context_create($options);
$body = file_get_contents($uri,false,$context);
$event = icalparse($body);
$ev = simplexml_load_string($event);
if ($ev->vevent) {
foreach ($ev->vevent as $e) {
//$now = strtotime("now");
//if (strtotime($e->dtstart) >= $now) {
$date = date('j M Y\, g:ia', strtotime($e->dtstart));
$band = $e->summary;
$band = str_replace("’","'", $band);
$band = str_replace(', '.date('j F Y', strtotime($e->dtstart)),'', $band);
$location = $e->location;
$loc = explode(',',$location);
$desc = htmlentities($e->description);
echo '<li>'.$date.': '.$band.' - '.$location.'</li>'."\n";
//}
}
}
}
?>