]> git.mxchange.org Git - friendica.git/blob - include/event.php
31cf536ffd4853f55cffc9b6d5b304bb27ff7342
[friendica.git] / include / event.php
1 <?php
2
3
4 function format_event_html($ev,$pre = '') {
5
6         require_once('include/bbcode.php');
7
8         if(! ((is_array($ev)) && count($ev)))
9                 return '';
10
11         $bd_format = t('l F d, Y \@ g A') ; // Friday January 18, 2011 @ 8 AM
12
13         $o = '<div class="vevent">';
14
15         $o .= '<p class="description event-description">' . bbcode($ev['desc']) .  '</p>';
16
17         $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
18                 . datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
19                 . '" >' 
20                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), 
21                         $ev['start'] , $bd_format ))
22                         :  day_translate(datetime_convert('UTC', 'UTC', 
23                         $ev['start'] , $bd_format)))
24                 . '</abbr></p>';
25
26         if(! $ev['nofinish'])
27                 $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
28                         . datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
29                         . '" >' 
30                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), 
31                                 $ev['finish'] , $bd_format ))
32                                 :  day_translate(datetime_convert('UTC', 'UTC', 
33                                 $ev['finish'] , $bd_format )))
34                         . '</abbr></p>';
35
36         if(strlen($ev['location']))
37                 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">' 
38                         . bbcode($ev['location']) 
39                         . '</span></p>';
40
41         $o .= '</div>';
42         return $o;
43 }
44
45
46 function parse_event($h) {
47
48         require_once('include/Scrape.php');
49         require_once('library/HTMLPurifier.auto.php');
50         require_once('include/html2bbcode');
51
52         $h = '<html><body>' . $h . '</body></html>';
53
54         $ret = array();
55
56         $dom = HTML5_Parser::parse($h);
57
58         if(! $dom)
59                 return $ret;
60
61         $items = $dom->getElementsByTagName('*');
62
63         foreach($items as $item) {
64                 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
65                         $level2 = $item->getElementsByTagName('*');
66                         foreach($level2 as $x) {
67                                 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
68                                         $ret['start'] = $x->getAttribute('title');
69                                         if(! strpos($ret['start'],'Z'))
70                                                 $ret['adjust'] = true;
71                                 }
72                                 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
73                                         $ret['finish'] = $x->getAttribute('title');
74
75                                 if(attribute_contains($x->getAttribute('class'),'description'))
76                                         $ret['desc'] = $x->textContent;
77                                 if(attribute_contains($x->getAttribute('class'),'location'))
78                                         $ret['location'] = $x->textContent;
79                         }
80                 }
81         }
82
83         // sanitise
84
85         if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
86                 $config = HTMLPurifier_Config::createDefault();
87                 $config->set('Cache.DefinitionImpl', null);
88                 $purifier = new HTMLPurifier($config);
89                 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
90         }
91
92         if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
93                 $config = HTMLPurifier_Config::createDefault();
94                 $config->set('Cache.DefinitionImpl', null);
95                 $purifier = new HTMLPurifier($config);
96                 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
97         }
98
99         if(x($ret,'start'))
100                 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
101         if(x($ret,'finish'))
102                 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
103
104         return $ret;
105 }
106
107
108 function format_event_bbcode($ev) {
109
110         $o = '';
111
112         if($ev['desc'])
113                 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
114
115         if($ev['start'])
116                 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
117
118         if($ev['start'])
119                 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
120  
121         if($ev['location'])
122                 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
123
124         if($ev['adjust'])
125                 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
126
127
128         return $o;
129
130 }
131
132 function bbtovcal($s) {
133         $o = '';
134         $ev = bbtoevent($s);
135         if($ev['desc'])
136                 $o = format_event_html($ev);
137         return $o;
138 }
139
140
141 function bbtoevent($s) {
142
143         $ev = array();
144
145         $match = '';
146         if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
147                 $ev['desc'] = $match[1];
148         $match = '';
149         if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
150                 $ev['start'] = $match[1];
151         $match = '';
152         if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
153                 $ev['finish'] = $match[1];
154         $match = '';
155         if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
156                 $ev['location'] = $match[1];
157         $match = '';
158         if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
159                 $ev['adjust'] = $match[1];
160         $match = '';
161
162         return $ev;
163
164 }
165
166
167 function sort_by_date($a) {
168
169         usort($a,'ev_compare');
170         return $a;
171 }
172
173
174 function ev_compare($a,$b) {
175
176         $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
177         $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
178         
179         return strcmp($date_a,$date_b);
180 }