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