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('library/HTMLPurifier.auto.php');
80 require_once('include/html2bbcode');
82 $h = '<html><body>' . $h . '</body></html>';
88 $dom = HTML5_Parser::parse($h);
89 } catch (DOMException $e) {
90 logger('parse_event: parse error: ' . $e);
96 $items = $dom->getElementsByTagName('*');
98 foreach($items as $item) {
99 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
100 $level2 = $item->getElementsByTagName('*');
101 foreach($level2 as $x) {
102 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
103 $ret['start'] = $x->getAttribute('title');
104 if(! strpos($ret['start'],'Z'))
105 $ret['adjust'] = true;
107 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
108 $ret['finish'] = $x->getAttribute('title');
110 if(attribute_contains($x->getAttribute('class'),'description'))
111 $ret['desc'] = $x->textContent;
112 if(attribute_contains($x->getAttribute('class'),'location'))
113 $ret['location'] = $x->textContent;
120 if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
121 $config = HTMLPurifier_Config::createDefault();
122 $config->set('Cache.DefinitionImpl', null);
123 $purifier = new HTMLPurifier($config);
124 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
127 if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
128 $config = HTMLPurifier_Config::createDefault();
129 $config->set('Cache.DefinitionImpl', null);
130 $purifier = new HTMLPurifier($config);
131 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
135 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
137 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
143 function format_event_bbcode($ev) {
148 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
151 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
154 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
156 if(($ev['finish']) && (! $ev['nofinish']))
157 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
160 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
163 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
170 function bbtovcal($s) {
174 $o = format_event_html($ev);
179 function bbtoevent($s) {
184 if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
185 $ev['summary'] = $match[1];
187 if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
188 $ev['desc'] = $match[1];
190 if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
191 $ev['start'] = $match[1];
193 if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
194 $ev['finish'] = $match[1];
196 if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
197 $ev['location'] = $match[1];
199 if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
200 $ev['adjust'] = $match[1];
201 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
207 function sort_by_date($a) {
209 usort($a,'ev_compare');
214 function ev_compare($a,$b) {
216 $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
217 $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
219 if($date_a === $date_b)
220 return strcasecmp($a['desc'],$b['desc']);
222 return strcmp($date_a,$date_b);
225 function event_delete($event_id) {
229 q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
230 logger("Deleted event ".$event_id, LOGGER_DEBUG);
233 function event_store($arr) {
235 require_once('include/datetime.php');
236 require_once('include/items.php');
237 require_once('include/bbcode.php');
241 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
242 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
243 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
244 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
245 $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
246 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
249 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
254 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
262 // Existing event being modified
266 // has the event actually changed?
268 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
272 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
274 // Nothing has changed. Grab the item id to return.
276 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
280 return((count($r)) ? $r[0]['id'] : 0);
283 // The event changed. Update it.
285 $r = q("UPDATE `event` SET
299 WHERE `id` = %d AND `uid` = %d",
301 dbesc($arr['edited']),
302 dbesc($arr['start']),
303 dbesc($arr['finish']),
304 dbesc($arr['summary']),
306 dbesc($arr['location']),
308 intval($arr['adjust']),
309 intval($arr['nofinish']),
310 dbesc($arr['allow_cid']),
311 dbesc($arr['allow_gid']),
312 dbesc($arr['deny_cid']),
313 dbesc($arr['deny_gid']),
317 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
322 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
323 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
324 $object .= '</object>' . "\n";
327 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",
328 dbesc(format_event_bbcode($arr)),
330 dbesc($arr['allow_cid']),
331 dbesc($arr['allow_gid']),
332 dbesc($arr['deny_cid']),
333 dbesc($arr['deny_gid']),
334 dbesc($arr['edited']),
335 intval($arr['private']),
340 $item_id = $r[0]['id'];
345 call_hooks("event_updated", $arr['id']);
351 // New event. Store it.
353 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
354 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
355 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
359 dbesc($arr['created']),
360 dbesc($arr['edited']),
361 dbesc($arr['start']),
362 dbesc($arr['finish']),
363 dbesc($arr['summary']),
365 dbesc($arr['location']),
367 intval($arr['adjust']),
368 intval($arr['nofinish']),
369 dbesc($arr['allow_cid']),
370 dbesc($arr['allow_gid']),
371 dbesc($arr['deny_cid']),
372 dbesc($arr['deny_gid'])
376 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
385 $item_arr['uid'] = $arr['uid'];
386 $item_arr['contact-id'] = $arr['cid'];
387 $item_arr['uri'] = $arr['uri'];
388 $item_arr['parent-uri'] = $arr['uri'];
389 $item_arr['guid'] = $arr['guid'];
390 $item_arr['type'] = 'activity';
391 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
392 $item_arr['contact-id'] = $contact['id'];
393 $item_arr['owner-name'] = $contact['name'];
394 $item_arr['owner-link'] = $contact['url'];
395 $item_arr['owner-avatar'] = $contact['thumb'];
396 $item_arr['author-name'] = $contact['name'];
397 $item_arr['author-link'] = $contact['url'];
398 $item_arr['author-avatar'] = $contact['thumb'];
399 $item_arr['title'] = '';
400 $item_arr['allow_cid'] = $arr['allow_cid'];
401 $item_arr['allow_gid'] = $arr['allow_gid'];
402 $item_arr['deny_cid'] = $arr['deny_cid'];
403 $item_arr['deny_gid'] = $arr['deny_gid'];
404 $item_arr['private'] = $arr['private'];
405 $item_arr['last-child'] = 1;
406 $item_arr['visible'] = 1;
407 $item_arr['verb'] = ACTIVITY_POST;
408 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
409 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
410 $item_arr['body'] = format_event_bbcode($event);
413 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
414 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
415 $item_arr['object'] .= '</object>' . "\n";
417 $item_id = item_store($item_arr);
419 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
423 // $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
427 //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
429 // intval($event['id']),
430 // intval($arr['uid']),
433 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
434 intval($event['id']),
440 call_hooks("event_created", $event['id']);