]> git.mxchange.org Git - friendica.git/blob - include/event.php
string update
[friendica.git] / include / event.php
1 <?php
2
3
4 function format_event_html($ev) {
5
6         require_once('include/bbcode.php');
7
8         if(! ((is_array($ev)) && count($ev)))
9                 return '';
10
11         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
12
13         $o = '<div class="vevent">' . "\r\n";
14
15         $o .= '<p class="description event-description">' . bbcode($ev['desc']) .  '</p>' . "\r\n";
16
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' ))
19                 . '" >' 
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";
25
26         if(! $ev['nofinish'])
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' ))
29                         . '" >' 
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";
35
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";
40
41         $o .= '</div>' . "\r\n";
42         return $o;
43 }
44
45
46 function parse_event($h) {
47
48         require_once('include/Scrape.php');
49         require_once('library/HTMLPurifier.auto.php');
50         require_once('include/html2bbcode');
51
52         $h = '<html><body>' . $h . '</body></html>';
53
54         $ret = array();
55
56
57         try {
58                 $dom = HTML5_Parser::parse($h);
59         } catch (DOMException $e) {
60                 logger('parse_event: parse error: ' . $e);
61         }
62
63         if(! $dom)
64                 return $ret;
65
66         $items = $dom->getElementsByTagName('*');
67
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;
76                                 }
77                                 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
78                                         $ret['finish'] = $x->getAttribute('title');
79
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;
84                         }
85                 }
86         }
87
88         // sanitise
89
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']));
95         }
96
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']));
102         }
103
104         if(x($ret,'start'))
105                 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
106         if(x($ret,'finish'))
107                 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
108
109         return $ret;
110 }
111
112
113 function format_event_bbcode($ev) {
114
115         $o = '';
116
117         if($ev['desc'])
118                 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
119
120         if($ev['start'])
121                 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
122
123         if(($ev['finish']) && (! $ev['nofinish']))
124                 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
125  
126         if($ev['location'])
127                 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
128
129         if($ev['adjust'])
130                 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
131
132
133         return $o;
134
135 }
136
137 function bbtovcal($s) {
138         $o = '';
139         $ev = bbtoevent($s);
140         if($ev['desc'])
141                 $o = format_event_html($ev);
142         return $o;
143 }
144
145
146 function bbtoevent($s) {
147
148         $ev = array();
149
150         $match = '';
151         if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
152                 $ev['desc'] = $match[1];
153         $match = '';
154         if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
155                 $ev['start'] = $match[1];
156         $match = '';
157         if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
158                 $ev['finish'] = $match[1];
159         $match = '';
160         if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
161                 $ev['location'] = $match[1];
162         $match = '';
163         if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
164                 $ev['adjust'] = $match[1];
165         $match = '';
166         $ev['nofinish'] = (($ev['start'] && (! $ev['finish'])) ? 1 : 0);
167         return $ev;
168
169 }
170
171
172 function sort_by_date($a) {
173
174         usort($a,'ev_compare');
175         return $a;
176 }
177
178
179 function ev_compare($a,$b) {
180
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']);
183
184         if($date_a === $date_b)
185                 return strcasecmp($a['desc'],$b['desc']);
186         
187         return strcmp($date_a,$date_b);
188 }
189
190
191
192 function event_store($arr) {
193
194         require_once('include/datetime.php');
195         require_once('include/items.php');
196         require_once('include/bbcode.php');
197
198         $a = get_app();
199
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);
206
207         if($arr['cid'])
208                 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
209                         intval($arr['cid']),
210                         intval($arr['uid'])
211                 );
212         else
213                 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
214                         intval($arr['uid'])
215                 );
216
217         if(count($c))
218                 $contact = $c[0];
219
220
221         // Existing event being modified
222
223         if($arr['id']) {
224
225                 // has the event actually changed?
226
227                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
228                         intval($arr['id']),
229                         intval($arr['uid'])
230                 );
231                 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
232
233                         // Nothing has changed. Grab the item id to return.
234
235                         $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
236                                 intval($arr['id']),
237                                 intval($arr['uid'])
238                         );
239                         return((count($r)) ? $r[0]['id'] : 0);
240                 }
241
242                 // The event changed. Update it.
243
244                 $r = q("UPDATE `event` SET
245                         `edited` = '%s',
246                         `start` = '%s',
247                         `finish` = '%s',
248                         `desc` = '%s',
249                         `location` = '%s',
250                         `type` = '%s',
251                         `adjust` = %d,
252                         `nofinish` = %d,
253                         `allow_cid` = '%s',
254                         `allow_gid` = '%s',
255                         `deny_cid` = '%s',
256                         `deny_gid` = '%s'
257                         WHERE `id` = %d AND `uid` = %d LIMIT 1",
258
259                         dbesc($arr['edited']),
260                         dbesc($arr['start']),
261                         dbesc($arr['finish']),
262                         dbesc($arr['desc']),
263                         dbesc($arr['location']),
264                         dbesc($arr['type']),
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']),
271                         intval($arr['id']),
272                         intval($arr['uid'])
273                 );
274                 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
275                         intval($arr['id']),
276                         intval($arr['uid'])
277                 );
278                 if(count($r)) {
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";
282
283
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)),
286                                 dbesc($object),
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']),
293                                 intval($r[0]['id']),
294                                 intval($arr['uid'])
295                         );
296
297                         return $r[0]['id'];
298                 }
299                 else
300                         return 0;
301         }
302         else {
303
304                 // New event. Store it. 
305
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' ) ",
309                         intval($arr['uid']),
310                         intval($arr['cid']),
311                         dbesc($arr['uri']),
312                         dbesc($arr['created']),
313                         dbesc($arr['edited']),
314                         dbesc($arr['start']),
315                         dbesc($arr['finish']),
316                         dbesc($arr['desc']),
317                         dbesc($arr['location']),
318                         dbesc($arr['type']),
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'])
325
326                 );
327
328                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
329                         dbesc($arr['uri']),
330                         intval($arr['uid'])
331                 );
332                 if(count($r))
333                         $event = $r[0];
334
335                 $item_arr = array();
336
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);
362
363
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";
367
368                 $item_id = item_store($item_arr);
369
370                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
371                         intval($arr['uid'])
372                 );
373                 if(count($r))
374                         $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
375
376
377                 if($item_id) {
378                         q("UPDATE `item` SET `plink` = '%s', `event-id` = %d  WHERE `uid` = %d AND `id` = %d LIMIT 1",
379                                 dbesc($plink),
380                                 intval($event['id']),
381                                 intval($arr['uid']),
382                                 intval($item_id)
383                         );
384                 }
385
386                 return $item_id;
387         }
388 }