3 require_once('include/bbcode.php');
4 require_once('include/map.php');
6 function format_event_html($ev) {
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 $o = '<div class="vevent">' . "\r\n";
18 $o .= '<p class="summary event-summary">' . bbcode($ev['summary']) . '</p>' . "\r\n";
20 $o .= '<p class="description event-description">' . bbcode($ev['desc']) . '</p>' . "\r\n";
22 $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
23 . datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
25 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
26 $ev['start'] , $bd_format ))
27 : day_translate(datetime_convert('UTC', 'UTC',
28 $ev['start'] , $bd_format)))
29 . '</abbr></p>' . "\r\n";
32 $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
33 . datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
35 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
36 $ev['finish'] , $bd_format ))
37 : day_translate(datetime_convert('UTC', 'UTC',
38 $ev['finish'] , $bd_format )))
39 . '</abbr></p>' . "\r\n";
41 if(strlen($ev['location'])){
42 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
43 . bbcode($ev['location'])
44 . '</span></p>' . "\r\n";
46 if (strpos($ev['location'], "[map")===False) {
47 $map = generate_named_map($ev['location']);
48 if ($map!==$ev['location']) $o.=$map;
53 $o .= '</div>' . "\r\n";
58 function parse_event($h) {
60 require_once('include/Scrape.php');
61 require_once('library/HTMLPurifier.auto.php');
62 require_once('include/html2bbcode');
64 $h = '<html><body>' . $h . '</body></html>';
70 $dom = HTML5_Parser::parse($h);
71 } catch (DOMException $e) {
72 logger('parse_event: parse error: ' . $e);
78 $items = $dom->getElementsByTagName('*');
80 foreach($items as $item) {
81 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
82 $level2 = $item->getElementsByTagName('*');
83 foreach($level2 as $x) {
84 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
85 $ret['start'] = $x->getAttribute('title');
86 if(! strpos($ret['start'],'Z'))
87 $ret['adjust'] = true;
89 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
90 $ret['finish'] = $x->getAttribute('title');
92 if(attribute_contains($x->getAttribute('class'),'description'))
93 $ret['desc'] = $x->textContent;
94 if(attribute_contains($x->getAttribute('class'),'location'))
95 $ret['location'] = $x->textContent;
102 if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
103 $config = HTMLPurifier_Config::createDefault();
104 $config->set('Cache.DefinitionImpl', null);
105 $purifier = new HTMLPurifier($config);
106 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
109 if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
110 $config = HTMLPurifier_Config::createDefault();
111 $config->set('Cache.DefinitionImpl', null);
112 $purifier = new HTMLPurifier($config);
113 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
117 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
119 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
125 function format_event_bbcode($ev) {
130 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
133 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
136 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
138 if(($ev['finish']) && (! $ev['nofinish']))
139 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
142 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
145 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
152 function bbtovcal($s) {
156 $o = format_event_html($ev);
161 function bbtoevent($s) {
166 if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
167 $ev['summary'] = $match[1];
169 if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
170 $ev['desc'] = $match[1];
172 if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
173 $ev['start'] = $match[1];
175 if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
176 $ev['finish'] = $match[1];
178 if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
179 $ev['location'] = $match[1];
181 if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
182 $ev['adjust'] = $match[1];
183 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
189 function sort_by_date($a) {
191 usort($a,'ev_compare');
196 function ev_compare($a,$b) {
198 $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
199 $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
201 if($date_a === $date_b)
202 return strcasecmp($a['desc'],$b['desc']);
204 return strcmp($date_a,$date_b);
209 function event_store($arr) {
211 require_once('include/datetime.php');
212 require_once('include/items.php');
213 require_once('include/bbcode.php');
217 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
218 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
219 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
220 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
221 $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
222 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
225 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
230 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
238 // Existing event being modified
242 // has the event actually changed?
244 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
248 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
250 // Nothing has changed. Grab the item id to return.
252 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
256 return((count($r)) ? $r[0]['id'] : 0);
259 // The event changed. Update it.
261 $r = q("UPDATE `event` SET
275 WHERE `id` = %d AND `uid` = %d",
277 dbesc($arr['edited']),
278 dbesc($arr['start']),
279 dbesc($arr['finish']),
280 dbesc($arr['summary']),
282 dbesc($arr['location']),
284 intval($arr['adjust']),
285 intval($arr['nofinish']),
286 dbesc($arr['allow_cid']),
287 dbesc($arr['allow_gid']),
288 dbesc($arr['deny_cid']),
289 dbesc($arr['deny_gid']),
293 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
298 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
299 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
300 $object .= '</object>' . "\n";
303 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",
304 dbesc(format_event_bbcode($arr)),
306 dbesc($arr['allow_cid']),
307 dbesc($arr['allow_gid']),
308 dbesc($arr['deny_cid']),
309 dbesc($arr['deny_gid']),
310 dbesc($arr['edited']),
311 intval($arr['private']),
316 $item_id = $r[0]['id'];
321 call_hooks("event_updated", $arr['id']);
327 // New event. Store it.
329 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
330 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
331 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
335 dbesc($arr['created']),
336 dbesc($arr['edited']),
337 dbesc($arr['start']),
338 dbesc($arr['finish']),
339 dbesc($arr['summary']),
341 dbesc($arr['location']),
343 intval($arr['adjust']),
344 intval($arr['nofinish']),
345 dbesc($arr['allow_cid']),
346 dbesc($arr['allow_gid']),
347 dbesc($arr['deny_cid']),
348 dbesc($arr['deny_gid'])
352 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
361 $item_arr['uid'] = $arr['uid'];
362 $item_arr['contact-id'] = $arr['cid'];
363 $item_arr['uri'] = $arr['uri'];
364 $item_arr['parent-uri'] = $arr['uri'];
365 $item_arr['type'] = 'activity';
366 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
367 $item_arr['contact-id'] = $contact['id'];
368 $item_arr['owner-name'] = $contact['name'];
369 $item_arr['owner-link'] = $contact['url'];
370 $item_arr['owner-avatar'] = $contact['thumb'];
371 $item_arr['author-name'] = $contact['name'];
372 $item_arr['author-link'] = $contact['url'];
373 $item_arr['author-avatar'] = $contact['thumb'];
374 $item_arr['title'] = '';
375 $item_arr['allow_cid'] = $arr['allow_cid'];
376 $item_arr['allow_gid'] = $arr['allow_gid'];
377 $item_arr['deny_cid'] = $arr['deny_cid'];
378 $item_arr['deny_gid'] = $arr['deny_gid'];
379 $item_arr['private'] = $arr['private'];
380 $item_arr['last-child'] = 1;
381 $item_arr['visible'] = 1;
382 $item_arr['verb'] = ACTIVITY_POST;
383 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
384 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
385 $item_arr['body'] = format_event_bbcode($event);
388 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
389 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
390 $item_arr['object'] .= '</object>' . "\n";
392 $item_id = item_store($item_arr);
394 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
398 // $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
402 //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
404 // intval($event['id']),
405 // intval($arr['uid']),
408 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
409 intval($event['id']),
415 call_hooks("event_created", $event['id']);