/
ical-parser-class.php
103 lines (96 loc) · 2.97 KB
/
ical-parser-class.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
<?php
class Ical
{
public function iCalReader($filename, $rootDirectory = '')
{
$iCaltoArray = $this->iCalDecoder($filename, $rootDirectory);
return $iCaltoArray;
}
public function iCalDecoder($file, $rootDirectory)
{
$ical = file_get_contents($rootDirectory . $file);
preg_match_all('/BEGIN:VEVENT.*?END:VEVENT/si', $ical, $eventresult, PREG_PATTERN_ORDER);
preg_match_all('/BEGIN:VTODO.*?END:VTODO/si', $ical, $todoresult, PREG_PATTERN_ORDER);
$countEventResult = count($eventresult[0]);
for ($i = 0; $i < $countEventResult; ++$i) {
$tmpbyline = explode("\n", $eventresult[0][$i]);
$begin = false;
$key = null;
foreach ($tmpbyline as $item) {
$item = str_replace("\r", '', $item);
$item = str_replace('\\n', '<br />', $item);
$item = str_replace("\,", ',', $item);
$tmpholderarray = explode(':', $item, 2);
if (count($tmpholderarray) > 1) {
if ($tmpholderarray[0] == 'BEGIN') {
if ($begin === false) {
$begin = true;
$majorarray['TYPE'] = $tmpholderarray[1];
} else {
$majorarray[$tmpholderarray[1]] = [];
$key = $tmpholderarray[1];
}
} elseif ($tmpholderarray[0] == 'END') {
if (!empty($key)) {
$key = null;
}
} else {
$tmpholderarrayKey = $tmpholderarray[0];
if (strpos($tmpholderarrayKey, ';') !== false) {
$tmpholderarrayKeyArray = explode(';', $tmpholderarrayKey);
$tmpholderarrayKey = $tmpholderarrayKeyArray[0];
}
if (!empty($key)) {
$majorarray[$key][$tmpholderarrayKey] = $tmpholderarray[1];
} else {
$majorarray[$tmpholderarrayKey] = $tmpholderarray[1];
}
}
}
}
$icalarray[] = $majorarray;
unset($majorarray);
}
$countTodoResult = count($todoresult[0]);
for ($i = 0; $i < $countTodoResult; ++$i) {
$tmpbyline = explode("\n", $todoresult[0][$i]);
$begin = false;
$key = null;
foreach ($tmpbyline as $item) {
$item = str_replace("\r", '', $item);
$item = str_replace('\\n', '<br />', $item);
$item = str_replace("\,", ',', $item);
$tmpholderarray = explode(':', $item);
if (count($tmpholderarray) > 1) {
if ($tmpholderarray[0] == 'BEGIN') {
if ($begin === false) {
$begin = true;
$majorarray['TYPE'] = $tmpholderarray[1];
} else {
$majorarray[$tmpholderarray[1]] = [];
$key = $tmpholderarray[1];
}
} elseif ($tmpholderarray[0] == 'END') {
if (!empty($key)) {
$key = null;
}
} else {
$tmpholderarrayKey = $tmpholderarray[0];
if (strpos($tmpholderarrayKey, ';') !== false) {
$tmpholderarrayKeyArray = explode(';', $tmpholderarrayKey);
$tmpholderarrayKey = $tmpholderarrayKeyArray[0];
}
if (!empty($key)) {
$majorarray[$key][$tmpholderarrayKey] = $tmpholderarray[1];
} else {
$majorarray[$tmpholderarrayKey] = $tmpholderarray[1];
}
}
}
}
$icalarray[] = $majorarray;
unset($majorarray);
}
return $icalarray;
}
}