]> git.mxchange.org Git - friendica.git/blob - include/event.php
Merge pull request #2099 from rabuzarus/2611_new_user_addr
[friendica.git] / include / event.php
1 <?php
2
3 require_once('include/bbcode.php');
4 require_once('include/map.php');
5
6 function format_event_html($ev, $simple = false) {
7
8
9
10         if(! ((is_array($ev)) && count($ev)))
11                 return '';
12
13         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
14
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)));
19
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 )));
24
25         if ($simple) {
26                 $o = "<h3>".bbcode($ev['summary'])."</h3>";
27
28                 $o .= "<p>".bbcode($ev['desc'])."</p>";
29
30                 $o .= "<h4>".t('Starts:')."</h4><p>".$event_start."</p>";
31
32                 if(! $ev['nofinish'])
33                         $o .= "<h4>".t('Finishes:')."</h4><p>".$event_end."</p>";
34
35                 if(strlen($ev['location']))
36                         $o .= "<h4>".t('Location:')."</h4><p>".$ev['location']."</p>";
37
38                 return $o;
39         }
40
41         $o = '<div class="vevent">' . "\r\n";
42
43
44         $o .= '<p class="summary event-summary">' . bbcode($ev['summary']) .  '</p>' . "\r\n";
45
46         $o .= '<p class="description event-description">' . bbcode($ev['desc']) .  '</p>' . "\r\n";
47
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' ))
50                 . '" >'.$event_start
51                 . '</abbr></p>' . "\r\n";
52
53         if(! $ev['nofinish'])
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' ))
56                         . '" >'.$event_end
57                         . '</abbr></p>'  . "\r\n";
58
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";
63
64                 if (strpos($ev['location'], "[map")===False) {
65                         $map = generate_named_map($ev['location']);
66                         if ($map!==$ev['location']) $o.=$map;
67                 }
68
69         }
70
71         $o .= '</div>' . "\r\n";
72         return $o;
73 }
74
75 /*
76 function parse_event($h) {
77
78         require_once('include/Scrape.php');
79         require_once('library/HTMLPurifier.auto.php');
80         require_once('include/html2bbcode');
81
82         $h = '<html><body>' . $h . '</body></html>';
83
84         $ret = array();
85
86
87         try {
88                 $dom = HTML5_Parser::parse($h);
89         } catch (DOMException $e) {
90                 logger('parse_event: parse error: ' . $e);
91         }
92
93         if(! $dom)
94                 return $ret;
95
96         $items = $dom->getElementsByTagName('*');
97
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;
106                                 }
107                                 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
108                                         $ret['finish'] = $x->getAttribute('title');
109
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;
114                         }
115                 }
116         }
117
118         // sanitise
119
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']));
125         }
126
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']));
132         }
133
134         if(x($ret,'start'))
135                 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
136         if(x($ret,'finish'))
137                 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
138
139         return $ret;
140 }
141 */
142
143 function format_event_bbcode($ev) {
144
145         $o = '';
146
147         if($ev['summary'])
148                 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
149
150         if($ev['desc'])
151                 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
152
153         if($ev['start'])
154                 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
155
156         if(($ev['finish']) && (! $ev['nofinish']))
157                 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
158
159         if($ev['location'])
160                 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
161
162         if($ev['adjust'])
163                 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
164
165
166         return $o;
167
168 }
169
170 function bbtovcal($s) {
171         $o = '';
172         $ev = bbtoevent($s);
173         if($ev['desc'])
174                 $o = format_event_html($ev);
175         return $o;
176 }
177
178
179 function bbtoevent($s) {
180
181         $ev = array();
182
183         $match = '';
184         if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
185                 $ev['summary'] = $match[1];
186         $match = '';
187         if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
188                 $ev['desc'] = $match[1];
189         $match = '';
190         if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
191                 $ev['start'] = $match[1];
192         $match = '';
193         if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
194                 $ev['finish'] = $match[1];
195         $match = '';
196         if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
197                 $ev['location'] = $match[1];
198         $match = '';
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);
202         return $ev;
203
204 }
205
206
207 function sort_by_date($a) {
208
209         usort($a,'ev_compare');
210         return $a;
211 }
212
213
214 function ev_compare($a,$b) {
215
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']);
218
219         if($date_a === $date_b)
220                 return strcasecmp($a['desc'],$b['desc']);
221
222         return strcmp($date_a,$date_b);
223 }
224
225 function event_delete($event_id) {
226         if ($event_id == 0)
227                 return;
228
229         q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
230         logger("Deleted event ".$event_id, LOGGER_DEBUG);
231 }
232
233 function event_store($arr) {
234
235         require_once('include/datetime.php');
236         require_once('include/items.php');
237         require_once('include/bbcode.php');
238
239         $a = get_app();
240
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);
247
248         if($arr['cid'])
249                 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
250                         intval($arr['cid']),
251                         intval($arr['uid'])
252                 );
253         else
254                 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
255                         intval($arr['uid'])
256                 );
257
258         if(count($c))
259                 $contact = $c[0];
260
261
262         // Existing event being modified
263
264         if($arr['id']) {
265
266                 // has the event actually changed?
267
268                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
269                         intval($arr['id']),
270                         intval($arr['uid'])
271                 );
272                 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
273
274                         // Nothing has changed. Grab the item id to return.
275
276                         $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
277                                 intval($arr['id']),
278                                 intval($arr['uid'])
279                         );
280                         return((count($r)) ? $r[0]['id'] : 0);
281                 }
282
283                 // The event changed. Update it.
284
285                 $r = q("UPDATE `event` SET
286                         `edited` = '%s',
287                         `start` = '%s',
288                         `finish` = '%s',
289                         `summary` = '%s',
290                         `desc` = '%s',
291                         `location` = '%s',
292                         `type` = '%s',
293                         `adjust` = %d,
294                         `nofinish` = %d,
295                         `allow_cid` = '%s',
296                         `allow_gid` = '%s',
297                         `deny_cid` = '%s',
298                         `deny_gid` = '%s'
299                         WHERE `id` = %d AND `uid` = %d",
300
301                         dbesc($arr['edited']),
302                         dbesc($arr['start']),
303                         dbesc($arr['finish']),
304                         dbesc($arr['summary']),
305                         dbesc($arr['desc']),
306                         dbesc($arr['location']),
307                         dbesc($arr['type']),
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']),
314                         intval($arr['id']),
315                         intval($arr['uid'])
316                 );
317                 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
318                         intval($arr['id']),
319                         intval($arr['uid'])
320                 );
321                 if(count($r)) {
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";
325
326
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)),
329                                 dbesc($object),
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']),
336                                 intval($r[0]['id']),
337                                 intval($arr['uid'])
338                         );
339
340                         $item_id = $r[0]['id'];
341                 }
342                 else
343                         $item_id = 0;
344
345                 call_hooks("event_updated", $arr['id']);
346
347                 return $item_id;
348         }
349         else {
350
351                 // New event. Store it.
352
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' ) ",
356                         intval($arr['uid']),
357                         intval($arr['cid']),
358                         dbesc($arr['uri']),
359                         dbesc($arr['created']),
360                         dbesc($arr['edited']),
361                         dbesc($arr['start']),
362                         dbesc($arr['finish']),
363                         dbesc($arr['summary']),
364                         dbesc($arr['desc']),
365                         dbesc($arr['location']),
366                         dbesc($arr['type']),
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'])
373
374                 );
375
376                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
377                         dbesc($arr['uri']),
378                         intval($arr['uid'])
379                 );
380                 if(count($r))
381                         $event = $r[0];
382
383                 $item_arr = array();
384
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);
411
412
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";
416
417                 $item_id = item_store($item_arr);
418
419                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
420                         intval($arr['uid'])
421                 );
422                 //if(count($r))
423                 //      $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
424
425
426                 if($item_id) {
427                         //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d  WHERE `uid` = %d AND `id` = %d",
428                         //      dbesc($plink),
429                         //      intval($event['id']),
430                         //      intval($arr['uid']),
431                         //      intval($item_id)
432                         //);
433                         q("UPDATE `item` SET `event-id` = %d  WHERE `uid` = %d AND `id` = %d",
434                                 intval($event['id']),
435                                 intval($arr['uid']),
436                                 intval($item_id)
437                         );
438                 }
439
440                 call_hooks("event_created", $event['id']);
441
442                 return $item_id;
443         }
444 }