3 require_once('include/bbcode.php');
4 require_once('include/map.php');
6 function format_event_html($ev, $simple = false) {
10 if(! ((is_array($ev)) && count($ev)))
13 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
15 $event_start = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
16 $ev['start'] , $bd_format ))
17 : day_translate(datetime_convert('UTC', 'UTC',
18 $ev['start'] , $bd_format)));
20 $event_end = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
21 $ev['finish'] , $bd_format ))
22 : day_translate(datetime_convert('UTC', 'UTC',
23 $ev['finish'] , $bd_format )));
26 $o = "<h3>".bbcode($ev['summary'])."</h3>";
28 $o .= "<p>".bbcode($ev['desc'])."</p>";
30 $o .= "<h4>".t('Starts:')."</h4><p>".$event_start."</p>";
33 $o .= "<h4>".t('Finishes:')."</h4><p>".$event_end."</p>";
35 if(strlen($ev['location']))
36 $o .= "<h4>".t('Location:')."</h4><p>".$ev['location']."</p>";
41 $o = '<div class="vevent">' . "\r\n";
44 $o .= '<p class="summary event-summary">' . bbcode($ev['summary']) . '</p>' . "\r\n";
46 $o .= '<p class="description event-description">' . bbcode($ev['desc']) . '</p>' . "\r\n";
48 $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
49 . datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
51 . '</abbr></p>' . "\r\n";
54 $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
55 . datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
57 . '</abbr></p>' . "\r\n";
59 if(strlen($ev['location'])){
60 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
61 . bbcode($ev['location'])
62 . '</span></p>' . "\r\n";
64 if (strpos($ev['location'], "[map") !== False) {
65 $map = generate_named_map($ev['location']);
66 if ($map!==$ev['location']) $o.=$map;
71 $o .= '</div>' . "\r\n";
76 function parse_event($h) {
78 require_once('include/Scrape.php');
79 require_once('include/html2bbcode');
81 $h = '<html><body>' . $h . '</body></html>';
87 $dom = HTML5_Parser::parse($h);
88 } catch (DOMException $e) {
89 logger('parse_event: parse error: ' . $e);
95 $items = $dom->getElementsByTagName('*');
97 foreach($items as $item) {
98 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
99 $level2 = $item->getElementsByTagName('*');
100 foreach($level2 as $x) {
101 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
102 $ret['start'] = $x->getAttribute('title');
103 if(! strpos($ret['start'],'Z'))
104 $ret['adjust'] = true;
106 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
107 $ret['finish'] = $x->getAttribute('title');
109 if(attribute_contains($x->getAttribute('class'),'description'))
110 $ret['desc'] = $x->textContent;
111 if(attribute_contains($x->getAttribute('class'),'location'))
112 $ret['location'] = $x->textContent;
119 if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
120 $config = HTMLPurifier_Config::createDefault();
121 $config->set('Cache.DefinitionImpl', null);
122 $purifier = new HTMLPurifier($config);
123 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
126 if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
127 $config = HTMLPurifier_Config::createDefault();
128 $config->set('Cache.DefinitionImpl', null);
129 $purifier = new HTMLPurifier($config);
130 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
134 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
136 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
142 function format_event_bbcode($ev) {
147 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
150 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
153 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
155 if(($ev['finish']) && (! $ev['nofinish']))
156 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
159 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
162 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
169 function bbtovcal($s) {
173 $o = format_event_html($ev);
178 function bbtoevent($s) {
183 if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
184 $ev['summary'] = $match[1];
186 if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
187 $ev['desc'] = $match[1];
189 if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
190 $ev['start'] = $match[1];
192 if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
193 $ev['finish'] = $match[1];
195 if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
196 $ev['location'] = $match[1];
198 if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
199 $ev['adjust'] = $match[1];
200 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
206 function sort_by_date($a) {
208 usort($a,'ev_compare');
213 function ev_compare($a,$b) {
215 $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
216 $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
218 if($date_a === $date_b)
219 return strcasecmp($a['desc'],$b['desc']);
221 return strcmp($date_a,$date_b);
224 function event_delete($event_id) {
228 q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
229 logger("Deleted event ".$event_id, LOGGER_DEBUG);
232 function event_store($arr) {
234 require_once('include/datetime.php');
235 require_once('include/items.php');
236 require_once('include/bbcode.php');
240 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
241 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
242 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
243 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
244 $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
245 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
248 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
253 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
261 // Existing event being modified
265 // has the event actually changed?
267 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
271 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
273 // Nothing has changed. Grab the item id to return.
275 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
279 return((count($r)) ? $r[0]['id'] : 0);
282 // The event changed. Update it.
284 $r = q("UPDATE `event` SET
298 WHERE `id` = %d AND `uid` = %d",
300 dbesc($arr['edited']),
301 dbesc($arr['start']),
302 dbesc($arr['finish']),
303 dbesc($arr['summary']),
305 dbesc($arr['location']),
307 intval($arr['adjust']),
308 intval($arr['nofinish']),
309 dbesc($arr['allow_cid']),
310 dbesc($arr['allow_gid']),
311 dbesc($arr['deny_cid']),
312 dbesc($arr['deny_gid']),
316 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
321 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
322 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
323 $object .= '</object>' . "\n";
326 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",
327 dbesc(format_event_bbcode($arr)),
329 dbesc($arr['allow_cid']),
330 dbesc($arr['allow_gid']),
331 dbesc($arr['deny_cid']),
332 dbesc($arr['deny_gid']),
333 dbesc($arr['edited']),
334 intval($arr['private']),
339 $item_id = $r[0]['id'];
344 call_hooks("event_updated", $arr['id']);
350 // New event. Store it.
352 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
353 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
354 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
358 dbesc($arr['created']),
359 dbesc($arr['edited']),
360 dbesc($arr['start']),
361 dbesc($arr['finish']),
362 dbesc($arr['summary']),
364 dbesc($arr['location']),
366 intval($arr['adjust']),
367 intval($arr['nofinish']),
368 dbesc($arr['allow_cid']),
369 dbesc($arr['allow_gid']),
370 dbesc($arr['deny_cid']),
371 dbesc($arr['deny_gid'])
375 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
384 $item_arr['uid'] = $arr['uid'];
385 $item_arr['contact-id'] = $arr['cid'];
386 $item_arr['uri'] = $arr['uri'];
387 $item_arr['parent-uri'] = $arr['uri'];
388 $item_arr['guid'] = $arr['guid'];
389 $item_arr['type'] = 'activity';
390 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
391 $item_arr['contact-id'] = $contact['id'];
392 $item_arr['owner-name'] = $contact['name'];
393 $item_arr['owner-link'] = $contact['url'];
394 $item_arr['owner-avatar'] = $contact['thumb'];
395 $item_arr['author-name'] = $contact['name'];
396 $item_arr['author-link'] = $contact['url'];
397 $item_arr['author-avatar'] = $contact['thumb'];
398 $item_arr['title'] = '';
399 $item_arr['allow_cid'] = $arr['allow_cid'];
400 $item_arr['allow_gid'] = $arr['allow_gid'];
401 $item_arr['deny_cid'] = $arr['deny_cid'];
402 $item_arr['deny_gid'] = $arr['deny_gid'];
403 $item_arr['private'] = $arr['private'];
404 $item_arr['last-child'] = 1;
405 $item_arr['visible'] = 1;
406 $item_arr['verb'] = ACTIVITY_POST;
407 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
408 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
409 $item_arr['body'] = format_event_bbcode($event);
412 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
413 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
414 $item_arr['object'] .= '</object>' . "\n";
416 $item_id = item_store($item_arr);
418 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
422 // $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
426 //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
428 // intval($event['id']),
429 // intval($arr['uid']),
432 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
433 intval($event['id']),
439 call_hooks("event_created", $event['id']);