4 function format_event_html($ev) {
6 require_once('include/bbcode.php');
8 if(! ((is_array($ev)) && count($ev)))
11 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
13 $o = '<div class="vevent">' . "\r\n";
15 $o .= '<p class="description event-description">' . bbcode($ev['desc']) . '</p>' . "\r\n";
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' ))
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>' . "\r\n";
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' ))
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>' . "\r\n";
36 if(strlen($ev['location']))
37 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
38 . bbcode($ev['location'])
39 . '</span></p>' . "\r\n";
41 $o .= '</div>' . "\r\n";
46 function parse_event($h) {
48 require_once('include/Scrape.php');
49 require_once('library/HTMLPurifier.auto.php');
50 require_once('include/html2bbcode');
52 $h = '<html><body>' . $h . '</body></html>';
56 $dom = HTML5_Parser::parse($h);
61 $items = $dom->getElementsByTagName('*');
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;
72 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
73 $ret['finish'] = $x->getAttribute('title');
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;
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']));
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']));
100 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
102 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
108 function format_event_bbcode($ev) {
113 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
116 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
118 if(($ev['finish']) && (! $ev['nofinish']))
119 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
122 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
125 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
132 function bbtovcal($s) {
136 $o = format_event_html($ev);
141 function bbtoevent($s) {
146 if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
147 $ev['desc'] = $match[1];
149 if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
150 $ev['start'] = $match[1];
152 if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
153 $ev['finish'] = $match[1];
155 if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
156 $ev['location'] = $match[1];
158 if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
159 $ev['adjust'] = $match[1];
161 $ev['nofinish'] = (($ev['start'] && (! $ev['finish'])) ? 1 : 0);
167 function sort_by_date($a) {
169 usort($a,'ev_compare');
174 function ev_compare($a,$b) {
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']);
179 if($date_a === $date_b)
180 return strcasecmp($a['desc'],$b['desc']);
182 return strcmp($date_a,$date_b);
187 function event_store($arr) {
189 require_once('include/datetime.php');
190 require_once('include/items.php');
191 require_once('include/bbcode.php');
195 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
196 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
197 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
198 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
199 $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
200 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
203 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
208 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
216 // Existing event being modified
220 // has the event actually changed?
222 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
226 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
228 // Nothing has changed. Grab the item id to return.
230 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
234 return((count($r)) ? $r[0]['id'] : 0);
237 // The event changed. Update it.
239 $r = q("UPDATE `event` SET
252 WHERE `id` = %d AND `uid` = %d LIMIT 1",
254 dbesc($arr['edited']),
255 dbesc($arr['start']),
256 dbesc($arr['finish']),
258 dbesc($arr['location']),
260 intval($arr['adjust']),
261 intval($arr['nofinish']),
262 dbesc($arr['allow_cid']),
263 dbesc($arr['allow_gid']),
264 dbesc($arr['deny_cid']),
265 dbesc($arr['deny_gid']),
269 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
274 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
275 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
276 $object .= '</object>' . "\n";
279 q("UPDATE `item` SET `body` = '%s', `object` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `edited` = '%s', `private` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
280 dbesc(format_event_bbcode($arr)),
282 dbesc($arr['allow_cid']),
283 dbesc($arr['allow_gid']),
284 dbesc($arr['deny_cid']),
285 dbesc($arr['deny_gid']),
286 dbesc($arr['edited']),
287 intval($arr['private']),
299 // New event. Store it.
301 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`desc`,`location`,`type`,
302 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
303 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
307 dbesc($arr['created']),
308 dbesc($arr['edited']),
309 dbesc($arr['start']),
310 dbesc($arr['finish']),
312 dbesc($arr['location']),
314 intval($arr['adjust']),
315 intval($arr['nofinish']),
316 dbesc($arr['allow_cid']),
317 dbesc($arr['allow_gid']),
318 dbesc($arr['deny_cid']),
319 dbesc($arr['deny_gid'])
323 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
332 $item_arr['uid'] = $arr['uid'];
333 $item_arr['contact-id'] = $arr['cid'];
334 $item_arr['uri'] = $arr['uri'];
335 $item_arr['parent-uri'] = $arr['uri'];
336 $item_arr['type'] = 'activity';
337 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
338 $item_arr['contact-id'] = $contact['id'];
339 $item_arr['owner-name'] = $contact['name'];
340 $item_arr['owner-link'] = $contact['url'];
341 $item_arr['owner-avatar'] = $contact['thumb'];
342 $item_arr['author-name'] = $contact['name'];
343 $item_arr['author-link'] = $contact['url'];
344 $item_arr['author-avatar'] = $contact['thumb'];
345 $item_arr['title'] = '';
346 $item_arr['allow_cid'] = $arr['allow_cid'];
347 $item_arr['allow_gid'] = $arr['allow_gid'];
348 $item_arr['deny_cid'] = $arr['deny_cid'];
349 $item_arr['deny_gid'] = $arr['deny_gid'];
350 $item_arr['private'] = $arr['private'];
351 $item_arr['last-child'] = 1;
352 $item_arr['visible'] = 1;
353 $item_arr['verb'] = ACTIVITY_POST;
354 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
356 $item_arr['body'] = format_event_bbcode($event);
359 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($uri) . '</id>';
360 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
361 $item_arr['object'] .= '</object>' . "\n";
363 $item_id = item_store($item_arr);
365 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
369 $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
373 q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d LIMIT 1",
375 intval($event['id']),