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