]> git.mxchange.org Git - friendica.git/blob - include/items.php
like, dislike, activity streams, etc.
[friendica.git] / include / items.php
1 <?php
2
3 require_once('bbcode.php');
4
5 function get_feed_for(&$a, $dfrn_id, $owner_id, $last_update, $direction = 0) {
6
7
8         // default permissions - anonymous user
9
10         $sql_extra = " 
11                 AND `allow_cid` = '' 
12                 AND `allow_gid` = '' 
13                 AND `deny_cid`  = '' 
14                 AND `deny_gid`  = '' 
15         ";
16
17         if(strlen($owner_id) && ! intval($owner_id)) {
18                 $r = q("SELECT `uid`, `nickname` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
19                         dbesc($owner_id)
20                 );
21                 if(count($r)) {
22                         $owner_id = $r[0]['uid'];
23                         $owner_nick = $r[0]['nickname'];
24                 }
25         }
26
27         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
28                 intval($owner_id)
29         );
30         if(count($r))
31                 $owner = $r[0];
32         else
33                 killme();
34
35         if($dfrn_id && $dfrn_id != '*') {
36
37                 $sql_extra = '';
38                 switch($direction) {
39                         case (-1):
40                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
41                                 $my_id = $dfrn_id;
42                                 break;
43                         case 0:
44                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45                                 $my_id = '1:' . $dfrn_id;
46                                 break;
47                         case 1:
48                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
49                                 $my_id = '0:' . $dfrn_id;
50                                 break;
51                         default:
52                                 return false;
53                                 break; // NOTREACHED
54                 }
55
56                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
57                         intval($owner_id)
58                 );
59
60                 if(! count($r))
61                         return false;
62
63                 $contact = $r[0];
64                 $groups = init_groups_visitor($contact['id']);
65
66                 if(count($groups)) {
67                         for($x = 0; $x < count($groups); $x ++) 
68                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
69                         $gs = implode('|', $groups);
70                 }
71                 else
72                         $gs = '<<>>' ; // Impossible to match 
73
74                 $sql_extra = sprintf(" 
75                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
76                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
77                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
78                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
79                 ",
80                         intval($contact['id']),
81                         intval($contact['id']),
82                         dbesc($gs),
83                         dbesc($gs)
84                 );
85         }
86
87         if($dfrn_id == '' || $dfrn_id == '*')
88                 $sort = 'DESC';
89         else
90                 $sort = 'ASC';
91
92         if(! strlen($last_update))
93                 $last_update = 'now - 30 days';
94         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
95
96         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
97                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
98                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
99                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
100                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
101                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
102                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
103                 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
104                 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
105                 $sql_extra
106                 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
107                 intval($owner_id),
108                 dbesc($check_date),
109                 dbesc($check_date),
110                 dbesc($sort)
111         );
112         if(! count($r))
113                 killme();
114
115         $items = $r;
116
117         $feed_template = file_get_contents('view/atom_feed.tpl');
118         $tomb_template = file_get_contents('view/atom_tomb.tpl');
119         $item_template = file_get_contents('view/atom_item.tpl');
120         $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
121
122         $atom = '';
123
124
125         $atom .= replace_macros($feed_template, array(
126                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
127                         '$feed_title'   => xmlify($owner['name']),
128                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
129                         '$name'         => xmlify($owner['name']),
130                         '$profile_page' => xmlify($owner['url']),
131                         '$photo'        => xmlify($owner['photo']),
132                         '$thumb'        => xmlify($owner['thumb']),
133                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
134                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
135                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) 
136         ));
137
138         
139         foreach($items as $item) {
140
141                 // public feeds get html, our own nodes use bbcode
142
143                 if($dfrn_id == '*') {
144                         $item['body'] = bbcode($item['body']);
145                         $type = 'html';
146                 }
147                 else {
148                         $type = 'text';
149                 }
150
151                 if($item['deleted']) {
152                         $atom .= replace_macros($tomb_template, array(
153                                 '$id'      => xmlify($item['uri']),
154                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
155                         ));
156                 }
157                 else {
158                         $verb = construct_verb($item);
159                         $actobj = construct_activity($item);
160
161                         if($item['parent'] == $item['id']) {
162                                 $atom .= replace_macros($item_template, array(
163                                         '$name'               => xmlify($item['name']),
164                                         '$profile_page'       => xmlify($item['url']),
165                                         '$thumb'              => xmlify($item['thumb']),
166                                         '$owner_name'         => xmlify($item['owner-name']),
167                                         '$owner_profile_page' => xmlify($item['owner-link']),
168                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
169                                         '$item_id'            => xmlify($item['uri']),
170                                         '$title'              => xmlify($item['title']),
171                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
172                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
173                                         '$location'           => xmlify($item['location']),
174                                         '$type'               => $type,
175                                         '$content'            => xmlify($item['body']),
176                                         '$verb'               => xmlify($verb),
177                                         '$actobj'             => $actobj,  // do not xmlify
178                                         '$comment_allow'      => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
179                                 ));
180                         }
181                         else {
182                                 $atom .= replace_macros($cmnt_template, array(
183                                         '$name'          => xmlify($item['name']),
184                                         '$profile_page'  => xmlify($item['url']),
185                                         '$thumb'         => xmlify($item['thumb']),
186                                         '$item_id'       => xmlify($item['uri']),
187                                         '$title'         => xmlify($item['title']),
188                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
189                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
190                                         '$type'          => $type,
191                                         '$content'       => xmlify($item['body']),
192                                         '$verb'          => xmlify($verb),
193                                         '$actobj'        => $actobj, // do not xmlify
194                                         '$parent_id'     => xmlify($item['parent-uri']),
195                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
196                                 ));
197                         }
198                 }
199         }
200
201         $atom .= '</feed>' . "\r\n";
202         return $atom;
203 }
204
205
206 function construct_verb($item) {
207         if($item['verb'])
208                 return $item['verb'];
209         return ACTIVITY_POST;
210 }
211
212 function construct_activity($item) {
213
214         if($item['object']) {
215                 $o = '<as:object>' . "\r\n";
216                 $r = @simplexml_load_string($item['object']);
217                 if($r->type)
218                         $o .= '<as:object-type>' . $r->type . '</as:object-type>' . "\r\n";
219                 if($r->id)
220                         $o .= '<id>' . $r->id . '</id>' . "\r\n";
221                 if($r->link)
222                         $o .= '<link rel="alternate" type="text/html" href="' . $r->link . '" />' . "\r\n";
223                 if($r->title)
224                         $o .= '<title>' . $r->title . '</title>' . "\r\n";
225                 if($r->content)
226                         $o .= '<content type="html" >' . bbcode($r->content) . '</content>' . "\r\n";
227                 $o .= '</as:object>' . "\r\n";
228                 return $o;
229         }
230
231         return '';
232
233
234
235
236
237 function get_atom_elements($item) {
238
239         require_once('library/HTMLPurifier.auto.php');
240         require_once('include/html2bbcode.php');
241
242         $res = array();
243
244         $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
245         if($raw_author) {
246                 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
247                 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
248         }
249
250         $author = $item->get_author();
251         $res['author-name'] = unxmlify($author->get_name());
252         $res['author-link'] = unxmlify($author->get_link());
253         if(! $res['author-avatar'])
254                 $res['author-avatar'] = unxmlify($author->get_avatar());
255         $res['uri'] = unxmlify($item->get_id());
256         $res['title'] = unxmlify($item->get_title());
257         $res['body'] = unxmlify($item->get_content());
258
259         $maxlen = get_max_import_size();
260         if($maxlen && (strlen($res['body']) > $maxlen))
261                 $res['body'] = substr($res['body'],0, $maxlen);
262
263         // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
264         // the content type. Our own network only emits text normally, though it might have been converted to 
265         // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
266         // have to assume it is all html and needs to be purified.
267
268         // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
269         // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
270         // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
271         // html.
272
273
274         if(strpos($res['body'],'<')) {
275
276                 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
277                         '[youtube]$1[/youtube]', $res['body']);
278
279                 $config = HTMLPurifier_Config::createDefault();
280                 $config->set('Core.DefinitionCache', null);
281
282                 // we shouldn't need a whitelist, because the bbcode converter
283                 // will strip out any unsupported tags.
284                 // $config->set('HTML.Allowed', 'p,b,a[href],i'); 
285
286                 $purifier = new HTMLPurifier($config);
287                 $res['body'] = $purifier->purify($res['body']);
288         }
289
290         
291         $res['body'] = html2bbcode($res['body']);
292
293
294         $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
295         if($allow && $allow[0]['data'] == 1)
296                 $res['last-child'] = 1;
297         else
298                 $res['last-child'] = 0;
299
300         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
301         if($rawcreated)
302                 $res['created'] = unxmlify($rawcreated[0]['data']);
303
304         $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
305         if($rawlocation)
306                 $res['location'] = unxmlify($rawlocation[0]['data']);
307
308
309         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
310         if($rawedited)
311                 $res['edited'] = unxmlify($rawcreated[0]['data']);
312
313         $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
314         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
315                 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
316         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
317                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
318         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
319                 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
320         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
321                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
322
323         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
324                 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
325         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
326                 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
327
328         $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
329         // select between supported verbs
330         if($rawverb)
331                 $res['verb'] = unxmlify($rawverb[0]['data']);
332
333         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
334
335
336         if($rawobj) {
337                 $res['object'] = '<object>' . "\n";
338                 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
339                         $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
340                         $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
341                 }       
342                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
343                         $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
344                 
345                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'alternate')
346                         $res['object'] .= '<link>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href'] . '</link>' . "\n";
347                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
348                         $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
349                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
350                         $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
351                         if(! $body)
352                                 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
353                         if(strpos($body,'<')) {
354
355                                 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
356                                         '[youtube]$1[/youtube]', $body);
357
358                                 $config = HTMLPurifier_Config::createDefault();
359                                 $config->set('Core.DefinitionCache', null);
360
361                                 $purifier = new HTMLPurifier($config);
362                                 $body = $purifier->purify($body);
363                         }
364
365                         $body = html2bbcode($body);
366                         $res['object'] .= '<content>' . $body . '</content>' . "\n";
367                 }
368
369                 $res['object'] .= '</object>' . "\n";
370         }
371
372         return $res;
373 }
374
375 function item_store($arr) {
376
377 //print_r($arr);
378
379
380         if($arr['gravity'])
381                 $arr['gravity'] = intval($arr['gravity']);
382         elseif($arr['parent-uri'] == $arr['uri'])
383                 $arr['gravity'] = 0;
384         elseif($arr['verb'] == ACTIVITY_POST)
385                 $arr['gravity'] = 6;
386
387         if(! x($arr,'type'))
388                 $arr['type'] = 'remote';
389         $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
390         $arr['uri'] = notags(trim($arr['uri']));
391         $arr['author-name'] = notags(trim($arr['author-name']));
392         $arr['author-link'] = notags(trim($arr['author-link']));
393         $arr['author-avatar'] = notags(trim($arr['author-avatar']));
394         $arr['owner-name'] = notags(trim($arr['owner-name']));
395         $arr['owner-link'] = notags(trim($arr['owner-link']));
396         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
397         $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
398         $arr['edited']  = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
399         $arr['changed'] = datetime_convert();
400         $arr['title'] = notags(trim($arr['title']));
401         $arr['location'] = notags(trim($arr['location']));
402         $arr['body'] = escape_tags(trim($arr['body']));
403         $arr['last-child'] = intval($arr['last-child']);
404         $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1);
405         $arr['deleted'] = 0;
406         $arr['parent-uri'] = notags(trim($arr['parent-uri']));
407         $arr['verb'] = notags(trim($arr['verb']));
408         $arr['object-type'] = notags(trim($arr['object-type']));
409         $arr['object'] = trim($arr['object']);
410
411         $parent_id = 0;
412         $parent_missing = false;
413
414         dbesc_array($arr);
415
416         $r = q("INSERT INTO `item` (`" 
417                         . implode("`, `", array_keys($arr)) 
418                         . "`) VALUES ('" 
419                         . implode("', '", array_values($arr)) 
420                         . "')" );
421
422         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
423                 dbesc($arr['parent-uri']),
424                 intval($arr['uid'])
425         );
426
427         if(count($r))
428                 $parent_id = $r[0]['id'];
429         else {
430                 $parent_missing = true;
431         }
432
433         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
434                 $arr['uri'],           // already dbesc'd
435                 intval($arr['uid'])
436         );
437         if(count($r))
438                 $current_post = $r[0]['id'];
439         else
440                 return 0;
441
442         if($parent_missing) {
443
444                 // perhaps the parent was deleted, but in any case, this thread is dead
445                 // and unfortunately our brand new item now has to be destroyed
446
447                 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
448                         intval($current_post)
449                 );
450                 return 0;
451         }
452
453         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
454                 intval($parent_id),
455                 intval($current_post)
456         );
457
458         return $current_post;
459 }