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>';
58 $dom = HTML5_Parser::parse($h);
59 } catch (DOMException $e) {
60 logger('parse_event: parse error: ' . $e);
66 $items = $dom->getElementsByTagName('*');
68 foreach($items as $item) {
69 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
70 $level2 = $item->getElementsByTagName('*');
71 foreach($level2 as $x) {
72 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
73 $ret['start'] = $x->getAttribute('title');
74 if(! strpos($ret['start'],'Z'))
75 $ret['adjust'] = true;
77 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
78 $ret['finish'] = $x->getAttribute('title');
80 if(attribute_contains($x->getAttribute('class'),'description'))
81 $ret['desc'] = $x->textContent;
82 if(attribute_contains($x->getAttribute('class'),'location'))
83 $ret['location'] = $x->textContent;
90 if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
91 $config = HTMLPurifier_Config::createDefault();
92 $config->set('Cache.DefinitionImpl', null);
93 $purifier = new HTMLPurifier($config);
94 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
97 if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
98 $config = HTMLPurifier_Config::createDefault();
99 $config->set('Cache.DefinitionImpl', null);
100 $purifier = new HTMLPurifier($config);
101 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
105 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
107 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
113 function format_event_bbcode($ev) {
118 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
121 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
123 if(($ev['finish']) && (! $ev['nofinish']))
124 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
127 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
130 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
137 function bbtovcal($s) {
141 $o = format_event_html($ev);
146 function bbtoevent($s) {
151 if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
152 $ev['desc'] = $match[1];
154 if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
155 $ev['start'] = $match[1];
157 if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
158 $ev['finish'] = $match[1];
160 if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
161 $ev['location'] = $match[1];
163 if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
164 $ev['adjust'] = $match[1];
166 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
172 function sort_by_date($a) {
174 usort($a,'ev_compare');
179 function ev_compare($a,$b) {
181 $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
182 $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
184 if($date_a === $date_b)
185 return strcasecmp($a['desc'],$b['desc']);
187 return strcmp($date_a,$date_b);
192 function event_store($arr) {
194 require_once('include/datetime.php');
195 require_once('include/items.php');
196 require_once('include/bbcode.php');
200 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
201 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
202 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
203 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
204 $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
205 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
208 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
213 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
221 // Existing event being modified
225 // has the event actually changed?
227 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
231 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
233 // Nothing has changed. Grab the item id to return.
235 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
239 return((count($r)) ? $r[0]['id'] : 0);
242 // The event changed. Update it.
244 $r = q("UPDATE `event` SET
257 WHERE `id` = %d AND `uid` = %d LIMIT 1",
259 dbesc($arr['edited']),
260 dbesc($arr['start']),
261 dbesc($arr['finish']),
263 dbesc($arr['location']),
265 intval($arr['adjust']),
266 intval($arr['nofinish']),
267 dbesc($arr['allow_cid']),
268 dbesc($arr['allow_gid']),
269 dbesc($arr['deny_cid']),
270 dbesc($arr['deny_gid']),
274 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
279 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
280 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
281 $object .= '</object>' . "\n";
284 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",
285 dbesc(format_event_bbcode($arr)),
287 dbesc($arr['allow_cid']),
288 dbesc($arr['allow_gid']),
289 dbesc($arr['deny_cid']),
290 dbesc($arr['deny_gid']),
291 dbesc($arr['edited']),
292 intval($arr['private']),
304 // New event. Store it.
306 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`desc`,`location`,`type`,
307 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
308 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
312 dbesc($arr['created']),
313 dbesc($arr['edited']),
314 dbesc($arr['start']),
315 dbesc($arr['finish']),
317 dbesc($arr['location']),
319 intval($arr['adjust']),
320 intval($arr['nofinish']),
321 dbesc($arr['allow_cid']),
322 dbesc($arr['allow_gid']),
323 dbesc($arr['deny_cid']),
324 dbesc($arr['deny_gid'])
328 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
337 $item_arr['uid'] = $arr['uid'];
338 $item_arr['contact-id'] = $arr['cid'];
339 $item_arr['uri'] = $arr['uri'];
340 $item_arr['parent-uri'] = $arr['uri'];
341 $item_arr['type'] = 'activity';
342 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
343 $item_arr['contact-id'] = $contact['id'];
344 $item_arr['owner-name'] = $contact['name'];
345 $item_arr['owner-link'] = $contact['url'];
346 $item_arr['owner-avatar'] = $contact['thumb'];
347 $item_arr['author-name'] = $contact['name'];
348 $item_arr['author-link'] = $contact['url'];
349 $item_arr['author-avatar'] = $contact['thumb'];
350 $item_arr['title'] = '';
351 $item_arr['allow_cid'] = $arr['allow_cid'];
352 $item_arr['allow_gid'] = $arr['allow_gid'];
353 $item_arr['deny_cid'] = $arr['deny_cid'];
354 $item_arr['deny_gid'] = $arr['deny_gid'];
355 $item_arr['private'] = $arr['private'];
356 $item_arr['last-child'] = 1;
357 $item_arr['visible'] = 1;
358 $item_arr['verb'] = ACTIVITY_POST;
359 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
360 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
361 $item_arr['body'] = format_event_bbcode($event);
364 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($uri) . '</id>';
365 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
366 $item_arr['object'] .= '</object>' . "\n";
368 $item_id = item_store($item_arr);
370 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
374 $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
378 q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d LIMIT 1",
380 intval($event['id']),