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