]> git.mxchange.org Git - friendica.git/blob - include/event.php
The global contact for server users is now stored directly
[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('include/html2bbcode');
80
81         $h = '<html><body>' . $h . '</body></html>';
82
83         $ret = array();
84
85
86         try {
87                 $dom = HTML5_Parser::parse($h);
88         } catch (DOMException $e) {
89                 logger('parse_event: parse error: ' . $e);
90         }
91
92         if(! $dom)
93                 return $ret;
94
95         $items = $dom->getElementsByTagName('*');
96
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;
105                                 }
106                                 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
107                                         $ret['finish'] = $x->getAttribute('title');
108
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;
113                         }
114                 }
115         }
116
117         // sanitise
118
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']));
124         }
125
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']));
131         }
132
133         if(x($ret,'start'))
134                 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
135         if(x($ret,'finish'))
136                 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
137
138         return $ret;
139 }
140 */
141
142 function format_event_bbcode($ev) {
143
144         $o = '';
145
146         if($ev['summary'])
147                 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
148
149         if($ev['desc'])
150                 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
151
152         if($ev['start'])
153                 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
154
155         if(($ev['finish']) && (! $ev['nofinish']))
156                 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
157
158         if($ev['location'])
159                 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
160
161         if($ev['adjust'])
162                 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
163
164
165         return $o;
166
167 }
168
169 function bbtovcal($s) {
170         $o = '';
171         $ev = bbtoevent($s);
172         if($ev['desc'])
173                 $o = format_event_html($ev);
174         return $o;
175 }
176
177
178 function bbtoevent($s) {
179
180         $ev = array();
181
182         $match = '';
183         if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
184                 $ev['summary'] = $match[1];
185         $match = '';
186         if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
187                 $ev['desc'] = $match[1];
188         $match = '';
189         if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
190                 $ev['start'] = $match[1];
191         $match = '';
192         if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
193                 $ev['finish'] = $match[1];
194         $match = '';
195         if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
196                 $ev['location'] = $match[1];
197         $match = '';
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);
201         return $ev;
202
203 }
204
205
206 function sort_by_date($a) {
207
208         usort($a,'ev_compare');
209         return $a;
210 }
211
212
213 function ev_compare($a,$b) {
214
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']);
217
218         if($date_a === $date_b)
219                 return strcasecmp($a['desc'],$b['desc']);
220
221         return strcmp($date_a,$date_b);
222 }
223
224 function event_delete($event_id) {
225         if ($event_id == 0)
226                 return;
227
228         q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
229         logger("Deleted event ".$event_id, LOGGER_DEBUG);
230 }
231
232 function event_store($arr) {
233
234         require_once('include/datetime.php');
235         require_once('include/items.php');
236         require_once('include/bbcode.php');
237
238         $a = get_app();
239
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);
246
247         if($arr['cid'])
248                 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
249                         intval($arr['cid']),
250                         intval($arr['uid'])
251                 );
252         else
253                 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
254                         intval($arr['uid'])
255                 );
256
257         if(count($c))
258                 $contact = $c[0];
259
260
261         // Existing event being modified
262
263         if($arr['id']) {
264
265                 // has the event actually changed?
266
267                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
268                         intval($arr['id']),
269                         intval($arr['uid'])
270                 );
271                 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
272
273                         // Nothing has changed. Grab the item id to return.
274
275                         $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
276                                 intval($arr['id']),
277                                 intval($arr['uid'])
278                         );
279                         return((count($r)) ? $r[0]['id'] : 0);
280                 }
281
282                 // The event changed. Update it.
283
284                 $r = q("UPDATE `event` SET
285                         `edited` = '%s',
286                         `start` = '%s',
287                         `finish` = '%s',
288                         `summary` = '%s',
289                         `desc` = '%s',
290                         `location` = '%s',
291                         `type` = '%s',
292                         `adjust` = %d,
293                         `nofinish` = %d,
294                         `allow_cid` = '%s',
295                         `allow_gid` = '%s',
296                         `deny_cid` = '%s',
297                         `deny_gid` = '%s'
298                         WHERE `id` = %d AND `uid` = %d",
299
300                         dbesc($arr['edited']),
301                         dbesc($arr['start']),
302                         dbesc($arr['finish']),
303                         dbesc($arr['summary']),
304                         dbesc($arr['desc']),
305                         dbesc($arr['location']),
306                         dbesc($arr['type']),
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']),
313                         intval($arr['id']),
314                         intval($arr['uid'])
315                 );
316                 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
317                         intval($arr['id']),
318                         intval($arr['uid'])
319                 );
320                 if(count($r)) {
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";
324
325
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)),
328                                 dbesc($object),
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']),
335                                 intval($r[0]['id']),
336                                 intval($arr['uid'])
337                         );
338
339                         $item_id = $r[0]['id'];
340                 }
341                 else
342                         $item_id = 0;
343
344                 call_hooks("event_updated", $arr['id']);
345
346                 return $item_id;
347         }
348         else {
349
350                 // New event. Store it.
351
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' ) ",
355                         intval($arr['uid']),
356                         intval($arr['cid']),
357                         dbesc($arr['uri']),
358                         dbesc($arr['created']),
359                         dbesc($arr['edited']),
360                         dbesc($arr['start']),
361                         dbesc($arr['finish']),
362                         dbesc($arr['summary']),
363                         dbesc($arr['desc']),
364                         dbesc($arr['location']),
365                         dbesc($arr['type']),
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'])
372
373                 );
374
375                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
376                         dbesc($arr['uri']),
377                         intval($arr['uid'])
378                 );
379                 if(count($r))
380                         $event = $r[0];
381
382                 $item_arr = array();
383
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);
410
411
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";
415
416                 $item_id = item_store($item_arr);
417
418                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
419                         intval($arr['uid'])
420                 );
421                 //if(count($r))
422                 //      $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
423
424
425                 if($item_id) {
426                         //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d  WHERE `uid` = %d AND `id` = %d",
427                         //      dbesc($plink),
428                         //      intval($event['id']),
429                         //      intval($arr['uid']),
430                         //      intval($item_id)
431                         //);
432                         q("UPDATE `item` SET `event-id` = %d  WHERE `uid` = %d AND `id` = %d",
433                                 intval($event['id']),
434                                 intval($arr['uid']),
435                                 intval($item_id)
436                         );
437                 }
438
439                 call_hooks("event_created", $event['id']);
440
441                 return $item_id;
442         }
443 }