]> git.mxchange.org Git - friendica.git/blob - include/event.php
never enough comments
[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 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         $dom = HTML5_Parser::parse($h);
57
58         if(! $dom)
59                 return $ret;
60
61         $items = $dom->getElementsByTagName('*');
62
63         foreach($items as $item) {
64                 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
65                         $level2 = $item->getElementsByTagName('*');
66                         foreach($level2 as $x) {
67                                 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
68                                         $ret['start'] = $x->getAttribute('title');
69                                         if(! strpos($ret['start'],'Z'))
70                                                 $ret['adjust'] = true;
71                                 }
72                                 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
73                                         $ret['finish'] = $x->getAttribute('title');
74
75                                 if(attribute_contains($x->getAttribute('class'),'description'))
76                                         $ret['desc'] = $x->textContent;
77                                 if(attribute_contains($x->getAttribute('class'),'location'))
78                                         $ret['location'] = $x->textContent;
79                         }
80                 }
81         }
82
83         // sanitise
84
85         if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
86                 $config = HTMLPurifier_Config::createDefault();
87                 $config->set('Cache.DefinitionImpl', null);
88                 $purifier = new HTMLPurifier($config);
89                 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
90         }
91
92         if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
93                 $config = HTMLPurifier_Config::createDefault();
94                 $config->set('Cache.DefinitionImpl', null);
95                 $purifier = new HTMLPurifier($config);
96                 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
97         }
98
99         if(x($ret,'start'))
100                 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
101         if(x($ret,'finish'))
102                 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
103
104         return $ret;
105 }
106
107
108 function format_event_bbcode($ev) {
109
110         $o = '';
111
112         if($ev['desc'])
113                 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
114
115         if($ev['start'])
116                 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
117
118         if(($ev['finish']) && (! $ev['nofinish']))
119                 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
120  
121         if($ev['location'])
122                 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
123
124         if($ev['adjust'])
125                 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
126
127
128         return $o;
129
130 }
131
132 function bbtovcal($s) {
133         $o = '';
134         $ev = bbtoevent($s);
135         if($ev['desc'])
136                 $o = format_event_html($ev);
137         return $o;
138 }
139
140
141 function bbtoevent($s) {
142
143         $ev = array();
144
145         $match = '';
146         if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
147                 $ev['desc'] = $match[1];
148         $match = '';
149         if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
150                 $ev['start'] = $match[1];
151         $match = '';
152         if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
153                 $ev['finish'] = $match[1];
154         $match = '';
155         if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
156                 $ev['location'] = $match[1];
157         $match = '';
158         if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
159                 $ev['adjust'] = $match[1];
160         $match = '';
161         $ev['nofinish'] = (($ev['start'] && (! $ev['finish'])) ? 1 : 0);
162         return $ev;
163
164 }
165
166
167 function sort_by_date($a) {
168
169         usort($a,'ev_compare');
170         return $a;
171 }
172
173
174 function ev_compare($a,$b) {
175
176         $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
177         $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
178
179         if($date_a === $date_b)
180                 return strcasecmp($a['desc'],$b['desc']);
181         
182         return strcmp($date_a,$date_b);
183 }
184
185
186
187 function event_store($arr) {
188
189         require_once('include/datetime.php');
190         require_once('include/items.php');
191         require_once('include/bbcode.php');
192
193         $a = get_app();
194
195         $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
196         $arr['edited']  = (($arr['edited']) ? $arr['edited'] : datetime_convert());
197         $arr['type']    = (($arr['type']) ? $arr['type'] : 'event' );   
198         $arr['cid']     = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
199         $arr['uri']     = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
200
201         if($arr['cid'])
202                 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
203                         intval($arr['cid']),
204                         intval($arr['uid'])
205                 );
206         else
207                 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
208                         intval($arr['uid'])
209                 );
210
211         if(count($c))
212                 $contact = $c[0];
213
214
215         // Existing event being modified
216
217         if($arr['id']) {
218
219                 // has the event actually changed?
220
221                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
222                         intval($arr['id']),
223                         intval($arr['uid'])
224                 );
225                 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
226
227                         // Nothing has changed. Grab the item id to return.
228
229                         $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
230                                 intval($arr['id']),
231                                 intval($arr['uid'])
232                         );
233                         return((count($r)) ? $r[0]['id'] : 0);
234                 }
235
236                 // The event changed. Update it.
237
238                 $r = q("UPDATE `event` SET
239                         `edited` = '%s',
240                         `start` = '%s',
241                         `finish` = '%s',
242                         `desc` = '%s',
243                         `location` = '%s',
244                         `type` = '%s',
245                         `adjust` = %d,
246                         `nofinish` = %d,
247                         `allow_cid` = '%s',
248                         `allow_gid` = '%s',
249                         `deny_cid` = '%s',
250                         `deny_gid` = '%s'
251                         WHERE `id` = %d AND `uid` = %d LIMIT 1",
252
253                         dbesc($arr['edited']),
254                         dbesc($arr['start']),
255                         dbesc($arr['finish']),
256                         dbesc($arr['desc']),
257                         dbesc($arr['location']),
258                         dbesc($arr['type']),
259                         intval($arr['adjust']),
260                         intval($arr['nofinish']),
261                         dbesc($arr['allow_cid']),
262                         dbesc($arr['allow_gid']),
263                         dbesc($arr['deny_cid']),
264                         dbesc($arr['deny_gid']),
265                         intval($arr['id']),
266                         intval($arr['uid'])
267                 );
268                 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
269                         intval($arr['id']),
270                         intval($arr['uid'])
271                 );
272                 if(count($r)) {
273                         $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
274                         $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
275                         $object .= '</object>' . "\n";
276
277
278                         q("UPDATE `item` SET `body` = '%s', `object` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
279                                 dbesc(format_event_bbcode($arr)),
280                                 dbesc($object),
281                                 dbesc($arr['allow_cid']),
282                                 dbesc($arr['allow_gid']),
283                                 dbesc($arr['deny_cid']),
284                                 dbesc($arr['deny_gid']),
285                                 dbesc($arr['edited']),
286                                 intval($r[0]['id']),
287                                 intval($arr['uid'])
288                         );
289
290                         return $r[0]['id'];
291                 }
292                 else
293                         return 0;
294         }
295         else {
296
297                 // New event. Store it. 
298
299                 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`desc`,`location`,`type`,
300                         `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
301                         VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
302                         intval($arr['uid']),
303                         intval($arr['cid']),
304                         dbesc($arr['uri']),
305                         dbesc($arr['created']),
306                         dbesc($arr['edited']),
307                         dbesc($arr['start']),
308                         dbesc($arr['finish']),
309                         dbesc($arr['desc']),
310                         dbesc($arr['location']),
311                         dbesc($arr['type']),
312                         intval($arr['adjust']),
313                         intval($arr['nofinish']),
314                         dbesc($arr['allow_cid']),
315                         dbesc($arr['allow_gid']),
316                         dbesc($arr['deny_cid']),
317                         dbesc($arr['deny_gid'])
318
319                 );
320
321                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
322                         dbesc($arr['uri']),
323                         intval($arr['uid'])
324                 );
325                 if(count($r))
326                         $event = $r[0];
327
328                 $item_arr = array();
329
330                 $item_arr['uid']           = $arr['uid'];
331                 $item_arr['contact-id']    = $arr['cid'];
332                 $item_arr['uri']           = $arr['uri'];
333                 $item_arr['parent-uri']    = $arr['uri'];
334                 $item_arr['type']          = 'activity';
335                 $item_arr['wall']          = (($arr['cid']) ? 0 : 1);
336                 $item_arr['contact-id']    = $contact['id'];
337                 $item_arr['owner-name']    = $contact['name'];
338                 $item_arr['owner-link']    = $contact['url'];
339                 $item_arr['owner-avatar']  = $contact['thumb'];
340                 $item_arr['author-name']   = $contact['name'];
341                 $item_arr['author-link']   = $contact['url'];
342                 $item_arr['author-avatar'] = $contact['thumb'];
343                 $item_arr['title']         = '';
344                 $item_arr['allow_cid']     = $str_contact_allow;
345                 $item_arr['allow_gid']     = $str_group_allow;
346                 $item_arr['deny_cid']      = $str_contact_deny;
347                 $item_arr['deny_gid']      = $str_group_deny;
348                 $item_arr['last-child']    = 1;
349                 $item_arr['visible']       = 1;
350                 $item_arr['verb']          = ACTIVITY_POST;
351                 $item_arr['object-type']   = ACTIVITY_OBJ_EVENT;
352
353                 $item_arr['body']          = format_event_bbcode($event);
354
355
356                 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($uri) . '</id>';
357                 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
358                 $item_arr['object'] .= '</object>' . "\n";
359
360                 $item_id = item_store($item_arr);
361
362                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
363                         intval($arr['uid'])
364                 );
365                 if(count($r))
366                         $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
367
368
369                 if($item_id) {
370                         q("UPDATE `item` SET `plink` = '%s', `event-id` = %d  WHERE `uid` = %d AND `id` = %d LIMIT 1",
371                                 dbesc($plink),
372                                 intval($event['id']),
373                                 intval($arr['uid']),
374                                 intval($item_id)
375                         );
376                 }
377
378                 return $item_id;
379         }
380 }