]> git.mxchange.org Git - friendica.git/blob - include/items.php
Preloading of items in the cache when receiving them.
[friendica.git] / include / items.php
1 <?php
2
3 require_once('include/bbcode.php');
4 require_once('include/oembed.php');
5 require_once('include/salmon.php');
6 require_once('include/crypto.php');
7 require_once('include/Photo.php');
8 require_once('include/tags.php');
9 require_once('include/text.php');
10
11 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
12
13
14         $sitefeed    = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
15         $public_feed = (($dfrn_id) ? false : true);
16         $starred     = false;   // not yet implemented, possible security issues
17         $converse    = false;
18
19         if($public_feed && $a->argc > 2) {
20                 for($x = 2; $x < $a->argc; $x++) {
21                         if($a->argv[$x] == 'converse')
22                                 $converse = true;
23                         if($a->argv[$x] == 'starred')
24                                 $starred = true;
25                         if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
26                                 $category = $a->argv[$x+1];
27                 }
28         }
29
30
31
32         // default permissions - anonymous user
33
34         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid`  = '' AND `deny_gid`  = '' ";
35
36         $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
37                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
38                 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
39                 dbesc($owner_nick)
40         );
41
42         if(! count($r))
43                 killme();
44
45         $owner = $r[0];
46         $owner_id = $owner['user_uid'];
47         $owner_nick = $owner['nickname'];
48
49         $birthday = feed_birthday($owner_id,$owner['timezone']);
50
51         if(! $public_feed) {
52
53                 $sql_extra = '';
54                 switch($direction) {
55                         case (-1):
56                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
57                                 $my_id = $dfrn_id;
58                                 break;
59                         case 0:
60                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
61                                 $my_id = '1:' . $dfrn_id;
62                                 break;
63                         case 1:
64                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
65                                 $my_id = '0:' . $dfrn_id;
66                                 break;
67                         default:
68                                 return false;
69                                 break; // NOTREACHED
70                 }
71
72                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
73                         intval($owner_id)
74                 );
75
76                 if(! count($r))
77                         killme();
78
79                 $contact = $r[0];
80                 require_once('include/security.php');
81                 $groups = init_groups_visitor($contact['id']);
82
83                 if(count($groups)) {
84                         for($x = 0; $x < count($groups); $x ++) 
85                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
86                         $gs = implode('|', $groups);
87                 }
88                 else
89                         $gs = '<<>>' ; // Impossible to match 
90
91                 $sql_extra = sprintf(" 
92                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
93                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
94                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
95                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
96                 ",
97                         intval($contact['id']),
98                         intval($contact['id']),
99                         dbesc($gs),
100                         dbesc($gs)
101                 );
102         }
103
104         if($public_feed)
105                 $sort = 'DESC';
106         else
107                 $sort = 'ASC';
108
109         if(! strlen($last_update))
110                 $last_update = 'now -30 days';
111
112         if(isset($category)) {
113                 $sql_extra .= file_tag_file_query('item',$category,'category');
114         }
115
116         if($public_feed) {
117                 if(! $converse)
118                         $sql_extra .= " AND `contact`.`self` = 1 ";
119         }
120
121         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
122
123         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
124                 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`, 
125                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
126                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
127                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
128                 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
129                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
130                 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
131                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0 
132                 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
133                 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
134                 $sql_extra
135                 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
136                 intval($owner_id),
137                 dbesc($check_date),
138                 dbesc($check_date),
139                 dbesc($sort)
140         );
141
142         // Will check further below if this actually returned results.
143         // We will provide an empty feed if that is the case.
144
145         $items = $r;
146
147         $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
148
149         $atom = '';
150
151         $hubxml = feed_hublinks();
152
153         $salmon = feed_salmonlinks($owner_nick);
154
155         $atom .= replace_macros($feed_template, array(
156                 '$version'      => xmlify(FRIENDICA_VERSION),
157                 '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
158                 '$feed_title'   => xmlify($owner['name']),
159                 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
160                 '$hub'          => $hubxml,
161                 '$salmon'       => $salmon,
162                 '$name'         => xmlify($owner['name']),
163                 '$profile_page' => xmlify($owner['url']),
164                 '$photo'        => xmlify($owner['photo']),
165                 '$thumb'        => xmlify($owner['thumb']),
166                 '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
167                 '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
168                 '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) , 
169                 '$birthday'     => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
170                 '$community'    => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
171         ));
172
173         call_hooks('atom_feed', $atom);
174
175         if(! count($items)) {
176
177                 call_hooks('atom_feed_end', $atom);
178
179                 $atom .= '</feed>' . "\r\n";
180                 return $atom;
181         }
182
183         foreach($items as $item) {
184
185                 // prevent private email from leaking.
186                 if($item['network'] === NETWORK_MAIL)
187                         continue;
188
189                 // public feeds get html, our own nodes use bbcode
190
191                 if($public_feed) {
192                         $type = 'html';
193                         // catch any email that's in a public conversation and make sure it doesn't leak
194                         if($item['private'])
195                                 continue;
196                 }
197                 else {
198                         $type = 'text';
199                 }
200
201                 $atom .= atom_entry($item,$type,null,$owner,true);
202         }
203
204         call_hooks('atom_feed_end', $atom);
205
206         $atom .= '</feed>' . "\r\n";
207
208         return $atom;
209 }
210
211
212 function construct_verb($item) {
213         if($item['verb'])
214                 return $item['verb'];
215         return ACTIVITY_POST;
216 }
217
218 function construct_activity_object($item) {
219
220         if($item['object']) {
221                 $o = '<as:object>' . "\r\n";
222                 $r = parse_xml_string($item['object'],false);
223
224
225                 if(! $r)
226                         return '';
227                 if($r->type)
228                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
229                 if($r->id)
230                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
231                 if($r->title)
232                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
233                 if($r->link) {
234                         if(substr($r->link,0,1) === '<') {
235                                 // patch up some facebook "like" activity objects that got stored incorrectly
236                                 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
237                                 // we can probably remove this hack here and in the following function in a few months time.
238                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
239                                         $r->link = str_replace('&','&amp;', $r->link);
240                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
241                                 $o .= $r->link;
242                         }
243                         else
244                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
245                 }
246                 if($r->content)
247                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
248                 $o .= '</as:object>' . "\r\n";
249                 return $o;
250         }
251
252         return '';
253
254
255 function construct_activity_target($item) {
256
257         if($item['target']) {
258                 $o = '<as:target>' . "\r\n";
259                 $r = parse_xml_string($item['target'],false);
260                 if(! $r)
261                         return '';
262                 if($r->type)
263                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
264                 if($r->id)
265                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
266                 if($r->title)
267                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
268                 if($r->link) {
269                         if(substr($r->link,0,1) === '<') {
270                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
271                                         $r->link = str_replace('&','&amp;', $r->link);
272                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
273                                 $o .= $r->link;
274                         }
275                         else
276                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
277                 }
278                 if($r->content)
279                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
280                 $o .= '</as:target>' . "\r\n";
281                 return $o;
282         }
283
284         return '';
285 }
286
287 /* limit_body_size()
288  *
289  *              The purpose of this function is to apply system message length limits to
290  *              imported messages without including any embedded photos in the length
291  */
292 if(! function_exists('limit_body_size')) {
293 function limit_body_size($body) {
294
295         logger('limit_body_size: start', LOGGER_DEBUG);
296
297         $maxlen = get_max_import_size();
298
299         // If the length of the body, including the embedded images, is smaller
300         // than the maximum, then don't waste time looking for the images
301         if($maxlen && (strlen($body) > $maxlen)) {
302
303                 logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
304
305                 $orig_body = $body;
306                 $new_body = '';
307                 $textlen = 0;
308                 $max_found = false;
309
310                 $img_start = strpos($orig_body, '[img');
311                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
312                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
313                 while(($img_st_close !== false) && ($img_end !== false)) {
314
315                         $img_st_close++; // make it point to AFTER the closing bracket
316                         $img_end += $img_start;
317                         $img_end += strlen('[/img]');
318
319                         if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
320                                 // This is an embedded image
321
322                                 if( ($textlen + $img_start) > $maxlen ) {
323                                         if($textlen < $maxlen) {
324                                                 logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
325                                                 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
326                                                 $textlen = $maxlen;
327                                         }
328                                 }
329                                 else {
330                                         $new_body = $new_body . substr($orig_body, 0, $img_start);
331                                         $textlen += $img_start;
332                                 }
333
334                                 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
335                         }
336                         else {
337
338                                 if( ($textlen + $img_end) > $maxlen ) {
339                                         if($textlen < $maxlen) {
340                                                 logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
341                                                 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
342                                                 $textlen = $maxlen;
343                                         }
344                                 }
345                                 else {
346                                         $new_body = $new_body . substr($orig_body, 0, $img_end);
347                                         $textlen += $img_end;
348                                 }
349                         }
350                         $orig_body = substr($orig_body, $img_end);
351
352                         if($orig_body === false) // in case the body ends on a closing image tag
353                                 $orig_body = '';
354
355                         $img_start = strpos($orig_body, '[img');
356                         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
357                         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
358                 }
359
360                 if( ($textlen + strlen($orig_body)) > $maxlen) {
361                         if($textlen < $maxlen) {
362                                 logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
363                                 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
364                                 $textlen = $maxlen;
365                         }
366                 }
367                 else {
368                         logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
369                         $new_body = $new_body . $orig_body;
370                         $textlen += strlen($orig_body);
371                 }
372
373                 return $new_body;
374         }
375         else
376                 return $body;
377 }}
378
379 function title_is_body($title, $body) {
380
381         $title = strip_tags($title);
382         $title = trim($title);
383         $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
384         $title = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $title);
385
386         $body = strip_tags($body);
387         $body = trim($body);
388         $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
389         $body = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $body);
390
391         if (strlen($title) < strlen($body))
392                 $body = substr($body, 0, strlen($title));
393
394         if (($title != $body) and (substr($title, -3) == "...")) {
395                 $pos = strrpos($title, "...");
396                 if ($pos > 0) {
397                         $title = substr($title, 0, $pos);
398                         $body = substr($body, 0, $pos);
399                 }
400         }
401
402         return($title == $body);
403 }
404
405
406
407 function get_atom_elements($feed,$item) {
408
409         require_once('library/HTMLPurifier.auto.php');
410         require_once('include/html2bbcode.php');
411
412         $best_photo = array();
413
414         $res = array();
415
416         $author = $item->get_author();
417         if($author) { 
418                 $res['author-name'] = unxmlify($author->get_name());
419                 $res['author-link'] = unxmlify($author->get_link());
420         }
421         else {
422                 $res['author-name'] = unxmlify($feed->get_title());
423                 $res['author-link'] = unxmlify($feed->get_permalink());
424         }
425         $res['uri'] = unxmlify($item->get_id());
426         $res['title'] = unxmlify($item->get_title());
427         $res['body'] = unxmlify($item->get_content());
428         $res['plink'] = unxmlify($item->get_link(0));
429
430         // removing the content of the title if its identically to the body
431         // This helps with auto generated titles e.g. from tumblr
432         if (title_is_body($res["title"], $res["body"]))
433                 $res['title'] = "";
434
435         if($res['plink'])
436                 $base_url = implode('/', array_slice(explode('/',$res['plink']),0,3));
437         else
438                 $base_url = '';
439
440         // look for a photo. We should check media size and find the best one,
441         // but for now let's just find any author photo
442
443         $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
444
445         if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
446                 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
447                 foreach($base as $link) {
448                         if(!x($res, 'author-avatar') || !$res['author-avatar']) {
449                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
450                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
451                         }
452                 }
453         }
454
455         $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
456
457         if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
458                 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
459                 if($base && count($base)) {
460                         foreach($base as $link) {
461                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
462                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
463                                 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
464                                         if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
465                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
466                                 }
467                         }
468                 }
469         }
470
471         // No photo/profile-link on the item - look at the feed level
472
473         if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
474                 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
475                 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
476                         $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
477                         foreach($base as $link) {
478                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
479                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
480                                 if(! $res['author-avatar']) {
481                                         if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
482                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
483                                 }
484                         }
485                 }
486
487                 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
488
489                 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
490                         $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
491
492                         if($base && count($base)) {
493                                 foreach($base as $link) {
494                                         if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
495                                                 $res['author-link'] = unxmlify($link['attribs']['']['href']);
496                                         if(! (x($res,'author-avatar'))) {
497                                                 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
498                                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
499                                         }
500                                 }
501                         }
502                 }
503         }
504
505         $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
506         if($apps && $apps[0]['attribs']['']['source']) {
507                 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
508                 if($res['app'] === 'web')
509                         $res['app'] = 'OStatus';
510         }
511
512         // base64 encoded json structure representing Diaspora signature
513
514         $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
515         if($dsig) {
516                 $res['dsprsig'] = unxmlify($dsig[0]['data']);
517         }
518
519         $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
520         if($dguid)
521                 $res['guid'] = unxmlify($dguid[0]['data']);
522
523         $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
524         if($bm)
525                 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
526
527
528         /**
529          * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
530          */
531
532         $have_real_body = false;
533
534         $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
535         if($rawenv) {
536                 $have_real_body = true;
537                 $res['body'] = $rawenv[0]['data'];
538                 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
539                 // make sure nobody is trying to sneak some html tags by us
540                 $res['body'] = notags(base64url_decode($res['body']));
541         }
542
543         
544         $res['body'] = limit_body_size($res['body']);
545
546         // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
547         // the content type. Our own network only emits text normally, though it might have been converted to 
548         // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
549         // have to assume it is all html and needs to be purified.
550
551         // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
552         // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
553         // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
554         // html.
555
556         if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
557
558                 $res['body'] = reltoabs($res['body'],$base_url);
559
560                 $res['body'] = html2bb_video($res['body']);
561
562                 $res['body'] = oembed_html2bbcode($res['body']);
563
564                 $config = HTMLPurifier_Config::createDefault();
565                 $config->set('Cache.DefinitionImpl', null);
566
567                 // we shouldn't need a whitelist, because the bbcode converter
568                 // will strip out any unsupported tags.
569
570                 $purifier = new HTMLPurifier($config);
571                 $res['body'] = $purifier->purify($res['body']);
572
573                 $res['body'] = @html2bbcode($res['body']);
574
575
576         }
577         elseif(! $have_real_body) {
578
579                 // it's not one of our messages and it has no tags
580                 // so it's probably just text. We'll escape it just to be safe.
581
582                 $res['body'] = escape_tags($res['body']);
583         }
584
585
586         // this tag is obsolete but we keep it for really old sites
587
588         $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
589         if($allow && $allow[0]['data'] == 1)
590                 $res['last-child'] = 1;
591         else
592                 $res['last-child'] = 0;
593
594         $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
595         if($private && intval($private[0]['data']) > 0)
596                 $res['private'] = intval($private[0]['data']);
597         else
598                 $res['private'] = 0;
599
600         $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
601         if($extid && $extid[0]['data'])
602                 $res['extid'] = $extid[0]['data'];
603
604         $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
605         if($rawlocation)
606                 $res['location'] = unxmlify($rawlocation[0]['data']);
607
608
609         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
610         if($rawcreated)
611                 $res['created'] = unxmlify($rawcreated[0]['data']);
612
613
614         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
615         if($rawedited)
616                 $res['edited'] = unxmlify($rawedited[0]['data']);
617
618         if((x($res,'edited')) && (! (x($res,'created'))))
619                 $res['created'] = $res['edited']; 
620
621         if(! $res['created'])
622                 $res['created'] = $item->get_date('c');
623
624         if(! $res['edited'])
625                 $res['edited'] = $item->get_date('c');
626
627
628         // Disallow time travelling posts
629
630         $d1 = strtotime($res['created']);
631         $d2 = strtotime($res['edited']);
632         $d3 = strtotime('now');
633
634         if($d1 > $d3)
635                 $res['created'] = datetime_convert();
636         if($d2 > $d3)
637                 $res['edited'] = datetime_convert();
638
639         $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
640         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
641                 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
642         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
643                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
644         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
645                 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
646         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
647                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
648
649         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
650                 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
651
652                 foreach($base as $link) {
653                         if(!x($res, 'owner-avatar') || !$res['owner-avatar']) {
654                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
655                                         $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
656                         }
657                 }
658         }
659
660         $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
661         if($rawgeo)
662                 $res['coord'] = unxmlify($rawgeo[0]['data']);
663
664
665         $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
666
667         // select between supported verbs
668
669         if($rawverb) {
670                 $res['verb'] = unxmlify($rawverb[0]['data']);
671         }
672
673         // translate OStatus unfollow to activity streams if it happened to get selected
674
675         if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
676                 $res['verb'] = ACTIVITY_UNFOLLOW;
677
678         $cats = $item->get_categories();
679         if($cats) {
680                 $tag_arr = array();
681                 foreach($cats as $cat) {
682                         $term = $cat->get_term();
683                         if(! $term)
684                                 $term = $cat->get_label();
685                         $scheme = $cat->get_scheme();
686                         if($scheme && $term && stristr($scheme,'X-DFRN:'))
687                                 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
688                         elseif($term)
689                                 $tag_arr[] = notags(trim($term));
690                 }
691                 $res['tag'] =  implode(',', $tag_arr);
692         }
693
694         $attach = $item->get_enclosures();
695         if($attach) {
696                 $att_arr = array();
697                 foreach($attach as $att) {
698                         $len   = intval($att->get_length());
699                         $link  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
700                         $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
701                         $type  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
702                         if(strpos($type,';'))
703                                 $type = substr($type,0,strpos($type,';'));
704                         if((! $link) || (strpos($link,'http') !== 0))
705                                 continue;
706
707                         if(! $title)
708                                 $title = ' ';
709                         if(! $type)
710                                 $type = 'application/octet-stream';
711
712                         $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]'; 
713                 }
714                 $res['attach'] = implode(',', $att_arr);
715         }
716
717         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
718
719         if($rawobj) {
720                 $res['object'] = '<object>' . "\n";
721                 $child = $rawobj[0]['child'];
722                 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
723                         $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
724                         $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
725                 }
726                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
727                         $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
728                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
729                         $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
730                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
731                         $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
732                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
733                         $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
734                         if(! $body)
735                                 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
736                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
737                         $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
738                         if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
739
740                                 $body = html2bb_video($body);
741
742                                 $config = HTMLPurifier_Config::createDefault();
743                                 $config->set('Cache.DefinitionImpl', null);
744
745                                 $purifier = new HTMLPurifier($config);
746                                 $body = $purifier->purify($body);
747                                 $body = html2bbcode($body);
748                         }
749
750                         $res['object'] .= '<content>' . $body . '</content>' . "\n";
751                 }
752
753                 $res['object'] .= '</object>' . "\n";
754         }
755
756         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
757
758         if($rawobj) {
759                 $res['target'] = '<target>' . "\n";
760                 $child = $rawobj[0]['child'];
761                 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
762                         $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
763                 }
764                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
765                         $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
766                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
767                         $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
768                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
769                         $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
770                 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
771                         $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
772                         if(! $body)
773                                 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
774                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
775                         $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
776                         if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
777
778                                 $body = html2bb_video($body);
779
780                                 $config = HTMLPurifier_Config::createDefault();
781                                 $config->set('Cache.DefinitionImpl', null);
782
783                                 $purifier = new HTMLPurifier($config);
784                                 $body = $purifier->purify($body);
785                                 $body = html2bbcode($body);
786                         }
787
788                         $res['target'] .= '<content>' . $body . '</content>' . "\n";
789                 }
790
791                 $res['target'] .= '</target>' . "\n";
792         }
793
794         // This is some experimental stuff. By now retweets are shown with "RT:"
795         // But: There is data so that the message could be shown similar to native retweets
796         // There is some better way to parse this array - but it didn't worked for me.
797         $child = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://activitystrea.ms/spec/1.0/"][object][0]["child"];
798         if (is_array($child)) {
799                 logger('get_atom_elements: Looking for status.net repeated message');
800
801                 $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
802                 $orig_uri = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"];
803                 $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
804                 $uri = $author["uri"][0]["data"];
805                 $name = $author["name"][0]["data"];
806                 $avatar = @array_shift($author["link"][2]["attribs"]);
807                 $avatar = $avatar["href"];
808
809                 if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
810                         logger('get_atom_elements: fixing sender of repeated message.');
811
812                         if (intval(get_config('system','new_share'))) {
813                                 $prefix = "[share author='".str_replace("'", "&#039;",$name).
814                                                 "' profile='".$uri.
815                                                 "' avatar='".$avatar.
816                                                 "' link='".$orig_uri."']";
817
818                                 $res["body"] = $prefix.html2bbcode($message)."[/share]";
819                         } else {
820                                 $res["owner-name"] = $res["author-name"];
821                                 $res["owner-link"] = $res["author-link"];
822                                 $res["owner-avatar"] = $res["author-avatar"];
823
824                                 $res["author-name"] = $name;
825                                 $res["author-link"] = $uri;
826                                 $res["author-avatar"] = $avatar;
827
828                                 $res["body"] = html2bbcode($message);
829                         }
830                 }
831         }
832
833         $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
834
835         call_hooks('parse_atom', $arr);
836
837         //if (($res["title"] != "") or (strpos($res["body"], "RT @") > 0)) {
838         //if (strpos($res["body"], "RT @") !== false) {
839         //      $debugfile = tempnam("/home/ike/log", "item-res2-");
840         //      file_put_contents($debugfile, serialize($arr));
841         //}
842
843         return $res;
844 }
845
846 function encode_rel_links($links) {
847         $o = '';
848         if(! ((is_array($links)) && (count($links))))
849                 return $o;
850         foreach($links as $link) {
851                 $o .= '<link ';
852                 if($link['attribs']['']['rel'])
853                         $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
854                 if($link['attribs']['']['type'])
855                         $o .= 'type="' . $link['attribs']['']['type'] . '" ';
856                 if($link['attribs']['']['href'])
857                         $o .= 'href="' . $link['attribs']['']['href'] . '" ';
858                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
859                         $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
860                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
861                         $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
862                 $o .= ' />' . "\n" ;
863         }
864         return xmlify($o);
865 }
866
867
868
869 function item_store($arr,$force_parent = false) {
870
871         // If a Diaspora signature structure was passed in, pull it out of the 
872         // item array and set it aside for later storage.
873
874         $dsprsig = null;
875         if(x($arr,'dsprsig')) {
876                 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
877                 unset($arr['dsprsig']);
878         }
879
880         if(x($arr, 'gravity'))
881                 $arr['gravity'] = intval($arr['gravity']);
882         elseif($arr['parent-uri'] === $arr['uri'])
883                 $arr['gravity'] = 0;
884         elseif(activity_match($arr['verb'],ACTIVITY_POST))
885                 $arr['gravity'] = 6;
886         else
887                 $arr['gravity'] = 6;   // extensible catchall
888
889         if(! x($arr,'type'))
890                 $arr['type']      = 'remote';
891
892         // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
893
894         if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) 
895                 $arr['body'] = strip_tags($arr['body']);
896
897
898         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
899                 require_once('library/langdet/Text/LanguageDetect.php');
900                 $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
901                 $l = new Text_LanguageDetect;
902                 //$lng = $l->detectConfidence($naked_body);
903                 //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
904                 $lng = $l->detect($naked_body, 3);
905
906                 if (sizeof($lng) > 0) {
907                         $postopts = "";
908
909                         foreach ($lng as $language => $score) {
910                                 if ($postopts == "")
911                                         $postopts = "lang=";
912                                 else
913                                         $postopts .= ":";
914
915                                 $postopts .= $language.";".$score;
916                         }
917                         $arr['postopts'] = $postopts;
918                 }
919         }
920
921         $arr['wall']          = ((x($arr,'wall'))          ? intval($arr['wall'])                : 0);
922         $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : random_string());
923         $arr['extid']         = ((x($arr,'extid'))         ? notags(trim($arr['extid']))         : '');
924         $arr['author-name']   = ((x($arr,'author-name'))   ? notags(trim($arr['author-name']))   : '');
925         $arr['author-link']   = ((x($arr,'author-link'))   ? notags(trim($arr['author-link']))   : '');
926         $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
927         $arr['owner-name']    = ((x($arr,'owner-name'))    ? notags(trim($arr['owner-name']))    : '');
928         $arr['owner-link']    = ((x($arr,'owner-link'))    ? notags(trim($arr['owner-link']))    : '');
929         $arr['owner-avatar']  = ((x($arr,'owner-avatar'))  ? notags(trim($arr['owner-avatar']))  : '');
930         $arr['created']       = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
931         $arr['edited']        = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
932         $arr['commented']     = datetime_convert();
933         $arr['received']      = datetime_convert();
934         $arr['changed']       = datetime_convert();
935         $arr['title']         = ((x($arr,'title'))         ? notags(trim($arr['title']))         : '');
936         $arr['location']      = ((x($arr,'location'))      ? notags(trim($arr['location']))      : '');
937         $arr['coord']         = ((x($arr,'coord'))         ? notags(trim($arr['coord']))         : '');
938         $arr['last-child']    = ((x($arr,'last-child'))    ? intval($arr['last-child'])          : 0 );
939         $arr['visible']       = ((x($arr,'visible') !== false) ? intval($arr['visible'])         : 1 );
940         $arr['deleted']       = 0;
941         $arr['parent-uri']    = ((x($arr,'parent-uri'))    ? notags(trim($arr['parent-uri']))    : '');
942         $arr['verb']          = ((x($arr,'verb'))          ? notags(trim($arr['verb']))          : '');
943         $arr['object-type']   = ((x($arr,'object-type'))   ? notags(trim($arr['object-type']))   : '');
944         $arr['object']        = ((x($arr,'object'))        ? trim($arr['object'])                : '');
945         $arr['target-type']   = ((x($arr,'target-type'))   ? notags(trim($arr['target-type']))   : '');
946         $arr['target']        = ((x($arr,'target'))        ? trim($arr['target'])                : '');
947         $arr['plink']         = ((x($arr,'plink'))         ? notags(trim($arr['plink']))         : '');
948         $arr['allow_cid']     = ((x($arr,'allow_cid'))     ? trim($arr['allow_cid'])             : '');
949         $arr['allow_gid']     = ((x($arr,'allow_gid'))     ? trim($arr['allow_gid'])             : '');
950         $arr['deny_cid']      = ((x($arr,'deny_cid'))      ? trim($arr['deny_cid'])              : '');
951         $arr['deny_gid']      = ((x($arr,'deny_gid'))      ? trim($arr['deny_gid'])              : '');
952         $arr['private']       = ((x($arr,'private'))       ? intval($arr['private'])             : 0 );
953         $arr['bookmark']      = ((x($arr,'bookmark'))      ? intval($arr['bookmark'])            : 0 );
954         $arr['body']          = ((x($arr,'body'))          ? trim($arr['body'])                  : '');
955         $arr['tag']           = ((x($arr,'tag'))           ? notags(trim($arr['tag']))           : '');
956         $arr['attach']        = ((x($arr,'attach'))        ? notags(trim($arr['attach']))        : '');
957         $arr['app']           = ((x($arr,'app'))           ? notags(trim($arr['app']))           : '');
958         $arr['origin']        = ((x($arr,'origin'))        ? intval($arr['origin'])              : 0 );
959         $arr['guid']          = ((x($arr,'guid'))          ? notags(trim($arr['guid']))          : get_guid());
960
961
962         $arr['thr-parent'] = $arr['parent-uri'];
963         if($arr['parent-uri'] === $arr['uri']) {
964                 $parent_id = 0;
965                 $parent_deleted = 0;
966                 $allow_cid = $arr['allow_cid'];
967                 $allow_gid = $arr['allow_gid'];
968                 $deny_cid  = $arr['deny_cid'];
969                 $deny_gid  = $arr['deny_gid'];
970         }
971         else { 
972
973                 // find the parent and snarf the item id and ACL's
974                 // and anything else we need to inherit
975
976                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
977                         dbesc($arr['parent-uri']),
978                         intval($arr['uid'])
979                 );
980
981                 if(count($r)) {
982
983                         // is the new message multi-level threaded?
984                         // even though we don't support it now, preserve the info
985                         // and re-attach to the conversation parent.
986
987                         if($r[0]['uri'] != $r[0]['parent-uri']) {
988                                 $arr['parent-uri'] = $r[0]['parent-uri'];
989                                 $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d 
990                                         ORDER BY `id` ASC LIMIT 1",
991                                         dbesc($r[0]['parent-uri']),
992                                         dbesc($r[0]['parent-uri']),
993                                         intval($arr['uid'])
994                                 );
995                                 if($z && count($z))
996                                         $r = $z;
997                         }
998
999                         $parent_id      = $r[0]['id'];
1000                         $parent_deleted = $r[0]['deleted'];
1001                         $allow_cid      = $r[0]['allow_cid'];
1002                         $allow_gid      = $r[0]['allow_gid'];
1003                         $deny_cid       = $r[0]['deny_cid'];
1004                         $deny_gid       = $r[0]['deny_gid'];
1005                         $arr['wall']    = $r[0]['wall'];
1006
1007                         // if the parent is private, force privacy for the entire conversation
1008                         // This differs from the above settings as it subtly allows comments from 
1009                         // email correspondents to be private even if the overall thread is not. 
1010
1011                         if($r[0]['private'])
1012                                 $arr['private'] = $r[0]['private'];
1013
1014                         // Edge case. We host a public forum that was originally posted to privately.
1015                         // The original author commented, but as this is a comment, the permissions
1016                         // weren't fixed up so it will still show the comment as private unless we fix it here. 
1017
1018                         if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
1019                                 $arr['private'] = 0;
1020                 }
1021                 else {
1022
1023                         // Allow one to see reply tweets from status.net even when
1024                         // we don't have or can't see the original post.
1025
1026                         if($force_parent) {
1027                                 logger('item_store: $force_parent=true, reply converted to top-level post.');
1028                                 $parent_id = 0;
1029                                 $arr['parent-uri'] = $arr['uri'];
1030                                 $arr['gravity'] = 0;
1031                         }
1032                         else {
1033                                 logger('item_store: item parent was not found - ignoring item');
1034                                 return 0;
1035                         }
1036
1037                         $parent_deleted = 0;
1038                 }
1039         }
1040
1041         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1042                 dbesc($arr['uri']),
1043                 intval($arr['uid'])
1044         );
1045         if($r && count($r)) {
1046                 logger('item-store: duplicate item ignored. ' . print_r($arr,true));
1047                 return 0;
1048         }
1049
1050         call_hooks('post_remote',$arr);
1051
1052         if(x($arr,'cancel')) {
1053                 logger('item_store: post cancelled by plugin.');
1054                 return 0;
1055         }
1056
1057         dbesc_array($arr);
1058
1059         logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
1060
1061         $r = dbq("INSERT INTO `item` (`" 
1062                         . implode("`, `", array_keys($arr)) 
1063                         . "`) VALUES ('" 
1064                         . implode("', '", array_values($arr)) 
1065                         . "')" );
1066
1067         // find the item we just created
1068
1069         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC ",
1070                 $arr['uri'],           // already dbesc'd
1071                 intval($arr['uid'])
1072         );
1073
1074         if(count($r)) {
1075                 $current_post = $r[0]['id'];
1076                 logger('item_store: created item ' . $current_post);
1077                 create_tags_from_item($r[0]['id']);
1078         } else {
1079                 logger('item_store: could not locate created item');
1080                 return 0;
1081         }
1082         if(count($r) > 1) {
1083                 logger('item_store: duplicated post occurred. Removing duplicates.');
1084                 q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ",
1085                         $arr['uri'],
1086                         intval($arr['uid']),
1087                         intval($current_post)
1088                 );
1089         }
1090
1091         if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
1092                 $parent_id = $current_post;
1093
1094         if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
1095                 $private = 1;
1096         else
1097                 $private = $arr['private']; 
1098
1099         // Set parent id - and also make sure to inherit the parent's ACL's.
1100
1101         $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
1102                 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
1103                 intval($parent_id),
1104                 dbesc($allow_cid),
1105                 dbesc($allow_gid),
1106                 dbesc($deny_cid),
1107                 dbesc($deny_gid),
1108                 intval($private),
1109                 intval($parent_deleted),
1110                 intval($current_post)
1111         );
1112         create_tags_from_item($current_post);
1113
1114         $arr['id'] = $current_post;
1115         $arr['parent'] = $parent_id;
1116         $arr['allow_cid'] = $allow_cid;
1117         $arr['allow_gid'] = $allow_gid;
1118         $arr['deny_cid'] = $deny_cid;
1119         $arr['deny_gid'] = $deny_gid;
1120         $arr['private'] = $private;
1121         $arr['deleted'] = $parent_deleted;
1122         call_hooks('post_remote_end',$arr);
1123
1124         // update the commented timestamp on the parent
1125
1126         q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1127                 dbesc(datetime_convert()),
1128                 dbesc(datetime_convert()),
1129                 intval($parent_id)
1130         );
1131
1132         if($dsprsig) {
1133                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1134                         intval($current_post),
1135                         dbesc($dsprsig->signed_text),
1136                         dbesc($dsprsig->signature),
1137                         dbesc($dsprsig->signer)
1138                 );
1139         }
1140
1141
1142         /**
1143          * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
1144          */
1145
1146         if($arr['last-child']) {
1147                 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
1148                         dbesc($arr['uri']),
1149                         intval($arr['uid']),
1150                         intval($current_post)
1151                 );
1152         }
1153
1154         tag_deliver($arr['uid'],$current_post);
1155
1156         // Store the fresh generated item into the cache
1157         $cachefile = get_cachefile($arr["guid"]."-".hash("md5", $arr['body']));
1158
1159         if (($cachefile != '') AND !file_exists($cachefile)) {
1160                 $s = prepare_text($arr['body']);
1161                 file_put_contents($cachefile, $s);
1162                 logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
1163         }
1164
1165         return $current_post;
1166 }
1167
1168 function get_item_contact($item,$contacts) {
1169         if(! count($contacts) || (! is_array($item)))
1170                 return false;
1171         foreach($contacts as $contact) {
1172                 if($contact['id'] == $item['contact-id']) {
1173                         return $contact;
1174                         break; // NOTREACHED
1175                 }
1176         }
1177         return false;
1178 }
1179
1180
1181 function tag_deliver($uid,$item_id) {
1182
1183         // look for mention tags and setup a second delivery chain for forum/community posts if appropriate
1184
1185         $a = get_app();
1186
1187         $mention = false;
1188
1189         $u = q("select * from user where uid = %d limit 1",
1190                 intval($uid)
1191         );
1192         if(! count($u))
1193                 return;
1194
1195         $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1196         $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1197
1198
1199         $i = q("select * from item where id = %d and uid = %d limit 1",
1200                 intval($item_id),
1201                 intval($uid)
1202         );
1203         if(! count($i))
1204                 return;
1205
1206         $item = $i[0];
1207
1208         $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1209
1210         // Diaspora uses their own hardwired link URL in @-tags
1211         // instead of the one we supply with webfinger
1212
1213         $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1214
1215         $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1216         if($cnt) {
1217                 foreach($matches as $mtch) {
1218                         if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1219                                 $mention = true;
1220                                 logger('tag_deliver: mention found: ' . $mtch[2]);
1221                         }
1222                 }
1223         }
1224
1225         if(! $mention)
1226                 return;
1227
1228         // send a notification
1229
1230         // use a local photo if we have one
1231
1232         $r = q("select * from contact where uid = %d and nurl = '%s' limit 1",
1233                 intval($u[0]['uid']),
1234                 dbesc(normalise_link($item['author-link']))
1235         );
1236         $photo = (($r && count($r)) ? $r[0]['thumb'] : $item['author-avatar']);
1237
1238
1239         require_once('include/enotify.php');
1240         notification(array(
1241                 'type'         => NOTIFY_TAGSELF,
1242                 'notify_flags' => $u[0]['notify-flags'],
1243                 'language'     => $u[0]['language'],
1244                 'to_name'      => $u[0]['username'],
1245                 'to_email'     => $u[0]['email'],
1246                 'uid'          => $u[0]['uid'],
1247                 'item'         => $item,
1248                 'link'         => $a->get_baseurl() . '/display/' . $u[0]['nickname'] . '/' . $item['id'],
1249                 'source_name'  => $item['author-name'],
1250                 'source_link'  => $item['author-link'],
1251                 'source_photo' => $photo,
1252                 'verb'         => ACTIVITY_TAG,
1253                 'otype'        => 'item'
1254         ));
1255
1256
1257         $arr = array('item' => $item, 'user' => $u[0], 'contact' => $r[0]);
1258
1259         call_hooks('tagged', $arr);
1260
1261         if((! $community_page) && (! $prvgroup))
1262                 return;
1263
1264
1265         // tgroup delivery - setup a second delivery chain
1266         // prevent delivery looping - only proceed
1267         // if the message originated elsewhere and is a top-level post
1268
1269         if(($item['wall']) || ($item['origin']) || ($item['id'] != $item['parent']))
1270                 return;
1271
1272         // now change this copy of the post to a forum head message and deliver to all the tgroup members
1273
1274
1275         $c = q("select name, url, thumb from contact where self = 1 and uid = %d limit 1",
1276                 intval($u[0]['uid'])
1277         );
1278         if(! count($c))
1279                 return;
1280
1281         // also reset all the privacy bits to the forum default permissions
1282
1283         $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
1284
1285         $forum_mode = (($prvgroup) ? 2 : 1);
1286
1287         q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s', 
1288                 `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'  where id = %d limit 1",
1289                 intval($forum_mode),
1290                 dbesc($c[0]['name']),
1291                 dbesc($c[0]['url']),
1292                 dbesc($c[0]['thumb']),
1293                 intval($private),
1294                 dbesc($u[0]['allow_cid']),
1295                 dbesc($u[0]['allow_gid']),
1296                 dbesc($u[0]['deny_cid']),
1297                 dbesc($u[0]['deny_gid']),
1298                 intval($item_id)
1299         );
1300
1301         proc_run('php','include/notifier.php','tgroup',$item_id);
1302
1303 }
1304
1305
1306
1307 function tgroup_check($uid,$item) {
1308
1309         $a = get_app();
1310
1311         $mention = false;
1312
1313         // check that the message originated elsewhere and is a top-level post
1314
1315         if(($item['wall']) || ($item['origin']) || ($item['uri'] != $item['parent-uri']))
1316                 return false;
1317
1318
1319         $u = q("select * from user where uid = %d limit 1",
1320                 intval($uid)
1321         );
1322         if(! count($u))
1323                 return false;
1324
1325         $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1326         $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1327
1328
1329         $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1330
1331         // Diaspora uses their own hardwired link URL in @-tags
1332         // instead of the one we supply with webfinger
1333
1334         $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1335
1336         $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1337         if($cnt) {
1338                 foreach($matches as $mtch) {
1339                         if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1340                                 $mention = true;
1341                                 logger('tgroup_check: mention found: ' . $mtch[2]);
1342                         }
1343                 }
1344         }
1345
1346         if(! $mention)
1347                 return false;
1348
1349         if((! $community_page) && (! $prvgroup))
1350                 return false;
1351
1352
1353
1354         return true;
1355
1356 }
1357
1358
1359
1360
1361
1362
1363 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
1364
1365         $a = get_app();
1366
1367         $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
1368
1369         if($contact['duplex'] && $contact['dfrn-id'])
1370                 $idtosend = '0:' . $orig_id;
1371         if($contact['duplex'] && $contact['issued-id'])
1372                 $idtosend = '1:' . $orig_id;
1373
1374         $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
1375
1376         $rino_enable = get_config('system','rino_encrypt');
1377
1378         if(! $rino_enable)
1379                 $rino = 0;
1380
1381         $ssl_val = intval(get_config('system','ssl_policy'));
1382         $ssl_policy = '';
1383
1384         switch($ssl_val){
1385                 case SSL_POLICY_FULL:
1386                         $ssl_policy = 'full';
1387                         break;
1388                 case SSL_POLICY_SELFSIGN:
1389                         $ssl_policy = 'self';
1390                         break;
1391                 case SSL_POLICY_NONE:
1392                 default:
1393                         $ssl_policy = 'none';
1394                         break;
1395         }
1396
1397         $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
1398
1399         logger('dfrn_deliver: ' . $url);
1400
1401         $xml = fetch_url($url);
1402
1403         $curl_stat = $a->get_curl_code();
1404         if(! $curl_stat)
1405                 return(-1); // timed out
1406
1407         logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
1408
1409         if(! $xml)
1410                 return 3;
1411
1412         if(strpos($xml,'<?xml') === false) {
1413                 logger('dfrn_deliver: no valid XML returned');
1414                 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
1415                 return 3;
1416         }
1417
1418         $res = parse_xml_string($xml);
1419
1420         if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
1421                 return (($res->status) ? $res->status : 3);
1422
1423         $postvars     = array();
1424         $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
1425         $challenge    = hex2bin((string) $res->challenge);
1426         $perm         = (($res->perm) ? $res->perm : null);
1427         $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
1428         $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
1429         $page         = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
1430
1431         if($owner['page-flags'] == PAGE_PRVGROUP)
1432                 $page = 2;
1433
1434         $final_dfrn_id = '';
1435
1436         if($perm) {
1437                 if((($perm == 'rw') && (! intval($contact['writable']))) 
1438                 || (($perm == 'r') && (intval($contact['writable'])))) {
1439                         q("update contact set writable = %d where id = %d limit 1",
1440                                 intval(($perm == 'rw') ? 1 : 0),
1441                                 intval($contact['id'])
1442                         );
1443                         $contact['writable'] = (string) 1 - intval($contact['writable']);
1444                 }
1445         }
1446
1447         if(($contact['duplex'] && strlen($contact['pubkey'])) 
1448                 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1449                 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1450                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
1451                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
1452         }
1453         else {
1454                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
1455                 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
1456         }
1457
1458         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
1459
1460         if(strpos($final_dfrn_id,':') == 1)
1461                 $final_dfrn_id = substr($final_dfrn_id,2);
1462
1463         if($final_dfrn_id != $orig_id) {
1464                 logger('dfrn_deliver: wrong dfrn_id.');
1465                 // did not decode properly - cannot trust this site 
1466                 return 3;
1467         }
1468
1469         $postvars['dfrn_id']      = $idtosend;
1470         $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
1471         if($dissolve)
1472                 $postvars['dissolve'] = '1';
1473
1474
1475         if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1476                 $postvars['data'] = $atom;
1477                 $postvars['perm'] = 'rw';
1478         }
1479         else {
1480                 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1481                 $postvars['perm'] = 'r';
1482         }
1483
1484         $postvars['ssl_policy'] = $ssl_policy;
1485
1486         if($page)
1487                 $postvars['page'] = $page;
1488         
1489         if($rino && $rino_allowed && (! $dissolve)) {
1490                 $key = substr(random_string(),0,16);
1491                 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1492                 $postvars['data'] = $data;
1493                 logger('rino: sent key = ' . $key, LOGGER_DEBUG);       
1494
1495
1496                 if($dfrn_version >= 2.1) {      
1497                         if(($contact['duplex'] && strlen($contact['pubkey'])) 
1498                                 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1499                                 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1500
1501                                 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1502                         }
1503                         else {
1504                                 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1505                         }
1506                 }
1507                 else {
1508                         if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1509                                 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1510                         }
1511                         else {
1512                                 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1513                         }
1514                 }
1515
1516                 logger('md5 rawkey ' . md5($postvars['key']));
1517
1518                 $postvars['key'] = bin2hex($postvars['key']);
1519         }
1520
1521         logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1522
1523         $xml = post_url($contact['notify'],$postvars);
1524
1525         logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1526
1527         $curl_stat = $a->get_curl_code();
1528         if((! $curl_stat) || (! strlen($xml)))
1529                 return(-1); // timed out
1530
1531         if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
1532                 return(-1);
1533
1534         if(strpos($xml,'<?xml') === false) {
1535                 logger('dfrn_deliver: phase 2: no valid XML returned');
1536                 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1537                 return 3;
1538         }
1539
1540         if($contact['term-date'] != '0000-00-00 00:00:00') {
1541                 logger("dfrn_deliver: $url back from the dead - removing mark for death");
1542                 require_once('include/Contact.php');
1543                 unmark_for_death($contact);
1544         }
1545
1546         $res = parse_xml_string($xml);
1547
1548         return $res->status; 
1549 }
1550
1551
1552 /**
1553  *
1554  * consume_feed - process atom feed and update anything/everything we might need to update
1555  *
1556  * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1557  *
1558  * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1559  *             It is this person's stuff that is going to be updated.
1560  * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1561  *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST 
1562  *             have a contact record.
1563  * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or 
1564  *        might not) try and subscribe to it.
1565  * $datedir sorts in reverse order
1566  * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been 
1567  *      imported prior to its children being seen in the stream unless we are certain
1568  *      of how the feed is arranged/ordered.
1569  * With $pass = 1, we only pull parent items out of the stream.
1570  * With $pass = 2, we only pull children (comments/likes).
1571  *
1572  * So running this twice, first with pass 1 and then with pass 2 will do the right
1573  * thing regardless of feed ordering. This won't be adequate in a fully-threaded
1574  * model where comments can have sub-threads. That would require some massive sorting
1575  * to get all the feed items into a mostly linear ordering, and might still require
1576  * recursion.  
1577  */
1578
1579 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
1580
1581         require_once('library/simplepie/simplepie.inc');
1582
1583         if(! strlen($xml)) {
1584                 logger('consume_feed: empty input');
1585                 return;
1586         }
1587
1588         $feed = new SimplePie();
1589         $feed->set_raw_data($xml);
1590         if($datedir)
1591                 $feed->enable_order_by_date(true);
1592         else
1593                 $feed->enable_order_by_date(false);
1594         $feed->init();
1595
1596         if($feed->error())
1597                 logger('consume_feed: Error parsing XML: ' . $feed->error());
1598
1599         $permalink = $feed->get_permalink();
1600
1601         // Check at the feed level for updated contact name and/or photo
1602
1603         $name_updated  = '';
1604         $new_name = '';
1605         $photo_timestamp = '';
1606         $photo_url = '';
1607         $birthday = '';
1608
1609         $hubs = $feed->get_links('hub');
1610         logger('consume_feed: hubs: ' . print_r($hubs,true), LOGGER_DATA);
1611
1612         if(count($hubs))
1613                 $hub = implode(',', $hubs);
1614
1615         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1616         if(! $rawtags)
1617                 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1618         if($rawtags) {
1619                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1620                 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1621                         $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1622                         $new_name = $elems['name'][0]['data'];
1623                 }
1624                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1625                         $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1626                         $photo_url = $elems['link'][0]['attribs']['']['href'];
1627                 }
1628
1629                 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1630                         $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1631                 }
1632         }
1633
1634         if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1635                 logger('consume_feed: Updating photo for ' . $contact['name']);
1636                 require_once("include/Photo.php");
1637                 $photo_failure = false;
1638                 $have_photo = false;
1639
1640                 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1641                         intval($contact['id']),
1642                         intval($contact['uid'])
1643                 );
1644                 if(count($r)) {
1645                         $resource_id = $r[0]['resource-id'];
1646                         $have_photo = true;
1647                 }
1648                 else {
1649                         $resource_id = photo_new_resource();
1650                 }
1651                         
1652                 $img_str = fetch_url($photo_url,true);
1653                 // guess mimetype from headers or filename
1654                 $type = guess_image_type($photo_url,true);
1655                 
1656                 
1657                 $img = new Photo($img_str, $type);
1658                 if($img->is_valid()) {
1659                         if($have_photo) {
1660                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1661                                         dbesc($resource_id),
1662                                         intval($contact['id']),
1663                                         intval($contact['uid'])
1664                                 );
1665                         }
1666
1667                         $img->scaleImageSquare(175);
1668
1669                         $hash = $resource_id;
1670                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1671
1672                         $img->scaleImage(80);
1673                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1674
1675                         $img->scaleImage(48);
1676                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1677
1678                         $a = get_app();
1679
1680                         q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1681                                 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1682                                 dbesc(datetime_convert()),
1683                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
1684                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
1685                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
1686                                 intval($contact['uid']),
1687                                 intval($contact['id'])
1688                         );
1689                 }
1690         }
1691
1692         if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1693                 $r = q("select * from contact where uid = %d and id = %d limit 1",
1694                         intval($contact['uid']),
1695                         intval($contact['id'])
1696                 );
1697
1698                 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1699                         dbesc(notags(trim($new_name))),
1700                         dbesc(datetime_convert()),
1701                         intval($contact['uid']),
1702                         intval($contact['id'])
1703                 );
1704
1705                 // do our best to update the name on content items
1706
1707                 if(count($r)) {
1708                         q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
1709                                 dbesc(notags(trim($new_name))),
1710                                 dbesc($r[0]['name']),
1711                                 dbesc($r[0]['url']),
1712                                 intval($contact['uid'])
1713                         );
1714                 }
1715         }
1716
1717         if(strlen($birthday)) {
1718                 if(substr($birthday,0,4) != $contact['bdyear']) {
1719                         logger('consume_feed: updating birthday: ' . $birthday);
1720
1721                         /**
1722                          *
1723                          * Add new birthday event for this person
1724                          *
1725                          * $bdtext is just a readable placeholder in case the event is shared
1726                          * with others. We will replace it during presentation to our $importer
1727                          * to contain a sparkle link and perhaps a photo. 
1728                          *
1729                          */
1730
1731                         $bdtext = sprintf( t('%s\'s birthday'), $contact['name']);
1732                         $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ) ;
1733
1734
1735                         $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
1736                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1737                                 intval($contact['uid']),
1738                                 intval($contact['id']),
1739                                 dbesc(datetime_convert()),
1740                                 dbesc(datetime_convert()),
1741                                 dbesc(datetime_convert('UTC','UTC', $birthday)),
1742                                 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1743                                 dbesc($bdtext),
1744                                 dbesc($bdtext2),
1745                                 dbesc('birthday')
1746                         );
1747
1748
1749                         // update bdyear
1750
1751                         q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1752                                 dbesc(substr($birthday,0,4)),
1753                                 intval($contact['uid']),
1754                                 intval($contact['id'])
1755                         );
1756
1757                         // This function is called twice without reloading the contact
1758                         // Make sure we only create one event. This is why &$contact 
1759                         // is a reference var in this function
1760
1761                         $contact['bdyear'] = substr($birthday,0,4);
1762                 }
1763
1764         }
1765
1766         $community_page = 0;
1767         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
1768         if($rawtags) {
1769                 $community_page = intval($rawtags[0]['data']);
1770         }
1771         if(is_array($contact) && intval($contact['forum']) != $community_page) {
1772                 q("update contact set forum = %d where id = %d limit 1",
1773                         intval($community_page),
1774                         intval($contact['id'])
1775                 );
1776                 $contact['forum'] = (string) $community_page;
1777         }
1778
1779
1780         // process any deleted entries
1781
1782         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1783         if(is_array($del_entries) && count($del_entries) && $pass != 2) {
1784                 foreach($del_entries as $dentry) {
1785                         $deleted = false;
1786                         if(isset($dentry['attribs']['']['ref'])) {
1787                                 $uri = $dentry['attribs']['']['ref'];
1788                                 $deleted = true;
1789                                 if(isset($dentry['attribs']['']['when'])) {
1790                                         $when = $dentry['attribs']['']['when'];
1791                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1792                                 }
1793                                 else
1794                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1795                         }
1796                         if($deleted && is_array($contact)) {
1797                                 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id` 
1798                                         WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
1799                                         dbesc($uri),
1800                                         intval($importer['uid']),
1801                                         intval($contact['id'])
1802                                 );
1803                                 if(count($r)) {
1804                                         $item = $r[0];
1805
1806                                         if(! $item['deleted'])
1807                                                 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1808
1809                                         if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
1810                                                 $xo = parse_xml_string($item['object'],false);
1811                                                 $xt = parse_xml_string($item['target'],false);
1812                                                 if($xt->type === ACTIVITY_OBJ_NOTE) {
1813                                                         $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
1814                                                                 dbesc($xt->id),
1815                                                                 intval($importer['importer_uid'])
1816                                                         );
1817                                                         if(count($i)) {
1818
1819                                                                 // For tags, the owner cannot remove the tag on the author's copy of the post.
1820
1821                                                                 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
1822                                                                 $author_remove = (($item['origin'] && $item['self']) ? true : false);
1823                                                                 $author_copy = (($item['origin']) ? true : false);
1824
1825                                                                 if($owner_remove && $author_copy)
1826                                                                         continue;
1827                                                                 if($author_remove || $owner_remove) {
1828                                                                         $tags = explode(',',$i[0]['tag']);
1829                                                                         $newtags = array();
1830                                                                         if(count($tags)) {
1831                                                                                 foreach($tags as $tag)
1832                                                                                         if(trim($tag) !== trim($xo->body))
1833                                                                                                 $newtags[] = trim($tag);
1834                                                                         }
1835                                                                         q("update item set tag = '%s' where id = %d limit 1",
1836                                                                                 dbesc(implode(',',$newtags)),
1837                                                                                 intval($i[0]['id'])
1838                                                                         );
1839                                                                         create_tags_from_item($i[0]['id']);
1840                                                                 }
1841                                                         }
1842                                                 }
1843                                         }
1844
1845                                         if($item['uri'] == $item['parent-uri']) {
1846                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1847                                                         `body` = '', `title` = ''
1848                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
1849                                                         dbesc($when),
1850                                                         dbesc(datetime_convert()),
1851                                                         dbesc($item['uri']),
1852                                                         intval($importer['uid'])
1853                                                 );
1854                                                 create_tags_from_itemuri($item['uri'], $importer['uid']);
1855                                         }
1856                                         else {
1857                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1858                                                         `body` = '', `title` = '' 
1859                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1860                                                         dbesc($when),
1861                                                         dbesc(datetime_convert()),
1862                                                         dbesc($uri),
1863                                                         intval($importer['uid'])
1864                                                 );
1865                                                 create_tags_from_itemuri($uri, $importer['uid']);
1866                                                 if($item['last-child']) {
1867                                                         // ensure that last-child is set in case the comment that had it just got wiped.
1868                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1869                                                                 dbesc(datetime_convert()),
1870                                                                 dbesc($item['parent-uri']),
1871                                                                 intval($item['uid'])
1872                                                         );
1873                                                         // who is the last child now? 
1874                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d 
1875                                                                 ORDER BY `created` DESC LIMIT 1",
1876                                                                         dbesc($item['parent-uri']),
1877                                                                         intval($importer['uid'])
1878                                                         );
1879                                                         if(count($r)) {
1880                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1881                                                                         intval($r[0]['id'])
1882                                                                 );
1883                                                         }
1884                                                 }       
1885                                         }
1886                                 }       
1887                         }
1888                 }
1889         }
1890
1891         // Now process the feed
1892
1893         if($feed->get_item_quantity()) {
1894
1895                 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1896
1897         // in inverse date order
1898                 if ($datedir)
1899                         $items = array_reverse($feed->get_items());
1900                 else
1901                         $items = $feed->get_items();
1902
1903
1904                 foreach($items as $item) {
1905
1906                         $is_reply = false;
1907                         $item_id = $item->get_id();
1908                         $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1909                         if(isset($rawthread[0]['attribs']['']['ref'])) {
1910                                 $is_reply = true;
1911                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1912                         }
1913
1914                         if(($is_reply) && is_array($contact)) {
1915
1916                                 if($pass == 1)
1917                                         continue;
1918
1919                                 // not allowed to post
1920
1921                                 if($contact['rel'] == CONTACT_IS_FOLLOWER)
1922                                         continue;
1923
1924
1925                                 // Have we seen it? If not, import it.
1926
1927                                 $item_id  = $item->get_id();
1928                                 $datarray = get_atom_elements($feed,$item);
1929
1930                                 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
1931                                         $datarray['author-name'] = $contact['name'];
1932                                 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
1933                                         $datarray['author-link'] = $contact['url'];
1934                                 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
1935                                         $datarray['author-avatar'] = $contact['thumb'];
1936
1937                                 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
1938                                         logger('consume_feed: no author information! ' . print_r($datarray,true));
1939                                         continue;
1940                                 }
1941
1942                                 $force_parent = false;
1943                                 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
1944                                         if($contact['network'] === NETWORK_OSTATUS)
1945                                                 $force_parent = true;
1946                                         if(strlen($datarray['title']))
1947                                                 unset($datarray['title']);
1948                                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1949                                                 dbesc(datetime_convert()),
1950                                                 dbesc($parent_uri),
1951                                                 intval($importer['uid'])
1952                                         );
1953                                         $datarray['last-child'] = 1;
1954                                 }
1955
1956
1957                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1958                                         dbesc($item_id),
1959                                         intval($importer['uid'])
1960                                 );
1961
1962                                 // Update content if 'updated' changes
1963
1964                                 if(count($r)) {
1965                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
1966
1967                                                 // do not accept (ignore) an earlier edit than one we currently have.
1968                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
1969                                                         continue;
1970
1971                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1972                                                         dbesc($datarray['title']),
1973                                                         dbesc($datarray['body']),
1974                                                         dbesc($datarray['tag']),
1975                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1976                                                         dbesc($item_id),
1977                                                         intval($importer['uid'])
1978                                                 );
1979                                                 create_tags_from_itemuri($item_id, $importer['uid']);
1980                                         }
1981
1982                                         // update last-child if it changes
1983
1984                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1985                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1986                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1987                                                         dbesc(datetime_convert()),
1988                                                         dbesc($parent_uri),
1989                                                         intval($importer['uid'])
1990                                                 );
1991                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1992                                                         intval($allow[0]['data']),
1993                                                         dbesc(datetime_convert()),
1994                                                         dbesc($item_id),
1995                                                         intval($importer['uid'])
1996                                                 );
1997                                         }
1998                                         continue;
1999                                 }
2000
2001
2002                                 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2003                                         // one way feed - no remote comment ability
2004                                         $datarray['last-child'] = 0;
2005                                 }
2006                                 $datarray['parent-uri'] = $parent_uri;
2007                                 $datarray['uid'] = $importer['uid'];
2008                                 $datarray['contact-id'] = $contact['id'];
2009                                 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
2010                                         $datarray['type'] = 'activity';
2011                                         $datarray['gravity'] = GRAVITY_LIKE;
2012                                         // only one like or dislike per person
2013                                         $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
2014                                                 intval($datarray['uid']),
2015                                                 intval($datarray['contact-id']),
2016                                                 dbesc($datarray['verb']),
2017                                                 dbesc($parent_uri),
2018                                                 dbesc($parent_uri)
2019                                         );
2020                                         if($r && count($r))
2021                                                 continue;
2022                                 }
2023
2024                                 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2025                                         $xo = parse_xml_string($datarray['object'],false);
2026                                         $xt = parse_xml_string($datarray['target'],false);
2027
2028                                         if($xt->type == ACTIVITY_OBJ_NOTE) {
2029                                                 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
2030                                                         dbesc($xt->id),
2031                                                         intval($importer['importer_uid'])
2032                                                 );
2033                                                 if(! count($r))
2034                                                         continue;
2035
2036                                                 // extract tag, if not duplicate, add to parent item
2037                                                 if($xo->id && $xo->content) {
2038                                                         $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2039                                                         if(! (stristr($r[0]['tag'],$newtag))) {
2040                                                                 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
2041                                                                         dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
2042                                                                         intval($r[0]['id'])
2043                                                                 );
2044                                                                 create_tags_from_item($r[0]['id']);
2045                                                         }
2046                                                 }
2047                                         }
2048                                 }
2049
2050                                 $r = item_store($datarray,$force_parent);
2051                                 continue;
2052                         }
2053
2054                         else {
2055
2056                                 // Head post of a conversation. Have we seen it? If not, import it.
2057
2058                                 $item_id  = $item->get_id();
2059
2060                                 $datarray = get_atom_elements($feed,$item);
2061
2062                                 if(is_array($contact)) {
2063                                         if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
2064                                                 $datarray['author-name'] = $contact['name'];
2065                                         if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
2066                                                 $datarray['author-link'] = $contact['url'];
2067                                         if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
2068                                                 $datarray['author-avatar'] = $contact['thumb'];
2069                                 }
2070
2071                                 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
2072                                         logger('consume_feed: no author information! ' . print_r($datarray,true));
2073                                         continue;
2074                                 }
2075
2076                                 // special handling for events
2077
2078                                 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2079                                         $ev = bbtoevent($datarray['body']);
2080                                         if(x($ev,'desc') && x($ev,'start')) {
2081                                                 $ev['uid'] = $importer['uid'];
2082                                                 $ev['uri'] = $item_id;
2083                                                 $ev['edited'] = $datarray['edited'];
2084                                                 $ev['private'] = $datarray['private'];
2085
2086                                                 if(is_array($contact))
2087                                                         $ev['cid'] = $contact['id'];
2088                                                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2089                                                         dbesc($item_id),
2090                                                         intval($importer['uid'])
2091                                                 );
2092                                                 if(count($r))
2093                                                         $ev['id'] = $r[0]['id'];
2094                                                 $xyz = event_store($ev);
2095                                                 continue;
2096                                         }
2097                                 }
2098
2099                                 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
2100                                         if(strlen($datarray['title']))
2101                                                 unset($datarray['title']);
2102                                         $datarray['last-child'] = 1;
2103                                 }
2104
2105
2106                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2107                                         dbesc($item_id),
2108                                         intval($importer['uid'])
2109                                 );
2110
2111                                 // Update content if 'updated' changes
2112
2113                                 if(count($r)) {
2114                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
2115
2116                                                 // do not accept (ignore) an earlier edit than one we currently have.
2117                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2118                                                         continue;
2119
2120                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2121                                                         dbesc($datarray['title']),
2122                                                         dbesc($datarray['body']),
2123                                                         dbesc($datarray['tag']),
2124                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2125                                                         dbesc($item_id),
2126                                                         intval($importer['uid'])
2127                                                 );
2128                                                 create_tags_from_itemuri($item_id, $importer['uid']);
2129                                         }
2130
2131                                         // update last-child if it changes
2132
2133                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2134                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2135                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2136                                                         intval($allow[0]['data']),
2137                                                         dbesc(datetime_convert()),
2138                                                         dbesc($item_id),
2139                                                         intval($importer['uid'])
2140                                                 );
2141                                         }
2142                                         continue;
2143                                 }
2144
2145                                 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
2146                                         logger('consume-feed: New follower');
2147                                         new_follower($importer,$contact,$datarray,$item);
2148                                         return;
2149                                 }
2150                                 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW))  {
2151                                         lose_follower($importer,$contact,$datarray,$item);
2152                                         return;
2153                                 }
2154
2155                                 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
2156                                         logger('consume-feed: New friend request');
2157                                         new_follower($importer,$contact,$datarray,$item,true);
2158                                         return;
2159                                 }
2160                                 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND))  {
2161                                         lose_sharer($importer,$contact,$datarray,$item);
2162                                         return;
2163                                 }
2164
2165
2166                                 if(! is_array($contact))
2167                                         return;
2168
2169
2170                                 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2171                                                 // one way feed - no remote comment ability
2172                                                 $datarray['last-child'] = 0;
2173                                 }
2174                                 if($contact['network'] === NETWORK_FEED)
2175                                         $datarray['private'] = 2;
2176
2177                                 // This is my contact on another system, but it's really me.
2178                                 // Turn this into a wall post.
2179
2180                                 if($contact['remote_self']) {
2181                                         $datarray['wall'] = 1;
2182                                         if($contact['network'] === NETWORK_FEED) {
2183                                                 $datarray['private'] = 0;
2184                                         }
2185                                 }
2186
2187                                 $datarray['parent-uri'] = $item_id;
2188                                 $datarray['uid'] = $importer['uid'];
2189                                 $datarray['contact-id'] = $contact['id'];
2190
2191                                 if(! link_compare($datarray['owner-link'],$contact['url'])) {
2192                                         // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, 
2193                                         // but otherwise there's a possible data mixup on the sender's system.
2194                                         // the tgroup delivery code called from item_store will correct it if it's a forum,
2195                                         // but we're going to unconditionally correct it here so that the post will always be owned by our contact. 
2196                                         logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
2197                                         $datarray['owner-name']   = $contact['name'];
2198                                         $datarray['owner-link']   = $contact['url'];
2199                                         $datarray['owner-avatar'] = $contact['thumb'];
2200                                 }
2201
2202                                 // We've allowed "followers" to reach this point so we can decide if they are 
2203                                 // posting an @-tag delivery, which followers are allowed to do for certain
2204                                 // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it. 
2205
2206                                 if(($contact['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['uid'],$datarray)))
2207                                         continue;
2208
2209
2210                                 $r = item_store($datarray);
2211                                 continue;
2212
2213                         }
2214                 }
2215         }
2216 }
2217
2218 function local_delivery($importer,$data) {
2219         $a = get_app();
2220
2221     logger(__function__, LOGGER_TRACE);
2222
2223         if($importer['readonly']) {
2224                 // We aren't receiving stuff from this person. But we will quietly ignore them
2225                 // rather than a blatant "go away" message.
2226                 logger('local_delivery: ignoring');
2227                 return 0;
2228                 //NOTREACHED
2229         }
2230
2231         // Consume notification feed. This may differ from consuming a public feed in several ways
2232         // - might contain email or friend suggestions
2233         // - might contain remote followup to our message
2234         //              - in which case we need to accept it and then notify other conversants
2235         // - we may need to send various email notifications
2236
2237         $feed = new SimplePie();
2238         $feed->set_raw_data($data);
2239         $feed->enable_order_by_date(false);
2240         $feed->init();
2241
2242
2243         if($feed->error())
2244                 logger('local_delivery: Error parsing XML: ' . $feed->error());
2245
2246
2247         // Check at the feed level for updated contact name and/or photo
2248
2249         $name_updated  = '';
2250         $new_name = '';
2251         $photo_timestamp = '';
2252         $photo_url = '';
2253
2254
2255         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
2256
2257 // Fallback should not be needed here. If it isn't DFRN it won't have DFRN updated tags
2258 //      if(! $rawtags)
2259 //              $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
2260
2261         if($rawtags) {
2262                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
2263                 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
2264                         $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
2265                         $new_name = $elems['name'][0]['data'];
2266                 }
2267                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
2268                         $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
2269                         $photo_url = $elems['link'][0]['attribs']['']['href'];
2270                 }
2271         }
2272
2273         if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $importer['avatar-date'])) {
2274                 logger('local_delivery: Updating photo for ' . $importer['name']);
2275                 require_once("include/Photo.php");
2276                 $photo_failure = false;
2277                 $have_photo = false;
2278
2279                 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
2280                         intval($importer['id']),
2281                         intval($importer['importer_uid'])
2282                 );
2283                 if(count($r)) {
2284                         $resource_id = $r[0]['resource-id'];
2285                         $have_photo = true;
2286                 }
2287                 else {
2288                         $resource_id = photo_new_resource();
2289                 }
2290                         
2291                 $img_str = fetch_url($photo_url,true);
2292                 // guess mimetype from headers or filename
2293                 $type = guess_image_type($photo_url,true);
2294                 
2295                 
2296                 $img = new Photo($img_str, $type);
2297                 if($img->is_valid()) {
2298                         if($have_photo) {
2299                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
2300                                         dbesc($resource_id),
2301                                         intval($importer['id']),
2302                                         intval($importer['importer_uid'])
2303                                 );
2304                         }
2305
2306                         $img->scaleImageSquare(175);
2307
2308                         $hash = $resource_id;
2309                         $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 4);
2310
2311                         $img->scaleImage(80);
2312                         $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 5);
2313
2314                         $img->scaleImage(48);
2315                         $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 6);
2316
2317                         $a = get_app();
2318
2319                         q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
2320                                 WHERE `uid` = %d AND `id` = %d LIMIT 1",
2321                                 dbesc(datetime_convert()),
2322                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
2323                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
2324                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
2325                                 intval($importer['importer_uid']),
2326                                 intval($importer['id'])
2327                         );
2328                 }
2329         }
2330
2331         if(($name_updated) && (strlen($new_name)) && ($name_updated > $importer['name-date'])) {
2332                 $r = q("select * from contact where uid = %d and id = %d limit 1",
2333                         intval($importer['importer_uid']),
2334                         intval($importer['id'])
2335                 );
2336
2337                 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2338                         dbesc(notags(trim($new_name))),
2339                         dbesc(datetime_convert()),
2340                         intval($importer['importer_uid']),
2341                         intval($importer['id'])
2342                 );
2343
2344                 // do our best to update the name on content items
2345
2346                 if(count($r)) {
2347                         q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
2348                                 dbesc(notags(trim($new_name))),
2349                                 dbesc($r[0]['name']),
2350                                 dbesc($r[0]['url']),
2351                                 intval($importer['importer_uid'])
2352                         );
2353                 }
2354         }
2355
2356
2357
2358         // Currently unsupported - needs a lot of work
2359         $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
2360         if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
2361                 $base = $reloc[0]['child'][NAMESPACE_DFRN];
2362                 $newloc = array();
2363                 $newloc['uid'] = $importer['importer_uid'];
2364                 $newloc['cid'] = $importer['id'];
2365                 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
2366                 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
2367                 $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data']));
2368                 $newloc['micro'] = notags(unxmlify($base['micro'][0]['data']));
2369                 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
2370                 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
2371                 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
2372                 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
2373                 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
2374                 $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data']));
2375                 /** relocated user must have original key pair */
2376                 /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
2377                 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
2378
2379         logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
2380
2381         // update contact
2382         $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
2383                     intval($importer['id']),
2384                                         intval($importer['importer_uid']));
2385                 if ($r === false)
2386                         return 1;
2387         $old = $r[0];
2388
2389         $x = q("UPDATE contact SET
2390                         name = '%s',
2391                         photo = '%s',
2392                         thumb = '%s',
2393                         micro = '%s',
2394                         url = '%s',
2395                         request = '%s',
2396                         confirm = '%s',
2397                         notify = '%s',
2398                         poll = '%s',
2399                         `site-pubkey` = '%s'
2400                 WHERE id=%d AND uid=%d;",
2401                     dbesc($newloc['name']),
2402                     dbesc($newloc['photo']),
2403                     dbesc($newloc['thumb']),
2404                     dbesc($newloc['micro']),
2405                     dbesc($newloc['url']),
2406                     dbesc($newloc['request']),
2407                     dbesc($newloc['confirm']),
2408                     dbesc($newloc['notify']),
2409                     dbesc($newloc['poll']),
2410                     dbesc($newloc['sitepubkey']),
2411                     intval($importer['id']),
2412                                         intval($importer['importer_uid']));
2413
2414         if ($x === false)
2415                         return 1;
2416         // update items
2417         $fields = array(
2418             'owner-link' => array($old['url'], $newloc['url']),
2419             'author-link' => array($old['url'], $newloc['url']),
2420             'owner-avatar' => array($old['photo'], $newloc['photo']),
2421             'author-avatar' => array($old['photo'], $newloc['photo']),
2422         );
2423         foreach ($fields as $n=>$f){
2424             $x = q("UPDATE item SET `%s`='%s' WHERE `%s`='%s' AND uid=%d",
2425                         $n, dbesc($f[1]),
2426                         $n, dbesc($f[0]),
2427                         intval($importer['importer_uid']));
2428                         if ($x === false)
2429                                 return 1;
2430                 }
2431
2432                 // TODO
2433                 // merge with current record, current contents have priority
2434                 // update record, set url-updated
2435                 // update profile photos
2436                 // schedule a scan?
2437         return 0;
2438         }
2439
2440
2441         // handle friend suggestion notification
2442
2443         $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
2444         if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
2445                 $base = $sugg[0]['child'][NAMESPACE_DFRN];
2446                 $fsugg = array();
2447                 $fsugg['uid'] = $importer['importer_uid'];
2448                 $fsugg['cid'] = $importer['id'];
2449                 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
2450                 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
2451                 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
2452                 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
2453                 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
2454
2455                 // Does our member already have a friend matching this description?
2456
2457                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
2458                         dbesc($fsugg['name']),
2459                         dbesc(normalise_link($fsugg['url'])),
2460                         intval($fsugg['uid'])
2461                 );
2462                 if(count($r))
2463                         return 0;
2464
2465                 // Do we already have an fcontact record for this person?
2466
2467                 $fid = 0;
2468                 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2469                         dbesc($fsugg['url']),
2470                         dbesc($fsugg['name']),
2471                         dbesc($fsugg['request'])
2472                 );
2473                 if(count($r)) {
2474                         $fid = $r[0]['id'];
2475
2476                         // OK, we do. Do we already have an introduction for this person ?
2477                         $r = q("select id from intro where uid = %d and fid = %d limit 1",
2478                                 intval($fsugg['uid']),
2479                                 intval($fid)
2480                         );
2481                         if(count($r))
2482                                 return 0;
2483                 }
2484                 if(! $fid)
2485                         $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
2486                         dbesc($fsugg['name']),
2487                         dbesc($fsugg['url']),
2488                         dbesc($fsugg['photo']),
2489                         dbesc($fsugg['request'])
2490                 );
2491                 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2492                         dbesc($fsugg['url']),
2493                         dbesc($fsugg['name']),
2494                         dbesc($fsugg['request'])
2495                 );
2496                 if(count($r)) {
2497                         $fid = $r[0]['id'];
2498                 }
2499                 // database record did not get created. Quietly give up.
2500                 else
2501                         return 0;
2502
2503
2504                 $hash = random_string();
2505
2506                 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
2507                         VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
2508                         intval($fsugg['uid']),
2509                         intval($fid),
2510                         intval($fsugg['cid']),
2511                         dbesc($fsugg['body']),
2512                         dbesc($hash),
2513                         dbesc(datetime_convert()),
2514                         intval(0)
2515                 );
2516
2517                 notification(array(
2518                         'type'         => NOTIFY_SUGGEST,
2519                         'notify_flags' => $importer['notify-flags'],
2520                         'language'     => $importer['language'],
2521                         'to_name'      => $importer['username'],
2522                         'to_email'     => $importer['email'],
2523                         'uid'          => $importer['importer_uid'],
2524                         'item'         => $fsugg,
2525                         'link'         => $a->get_baseurl() . '/notifications/intros',
2526                         'source_name'  => $importer['name'],
2527                         'source_link'  => $importer['url'],
2528                         'source_photo' => $importer['photo'],
2529                         'verb'         => ACTIVITY_REQ_FRIEND,
2530                         'otype'        => 'intro'
2531                 ));
2532
2533                 return 0;
2534         }
2535
2536         $ismail = false;
2537
2538         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
2539         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
2540
2541                 logger('local_delivery: private message received');
2542
2543                 $ismail = true;
2544                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
2545
2546                 $msg = array();
2547                 $msg['uid'] = $importer['importer_uid'];
2548                 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
2549                 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
2550                 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
2551                 $msg['contact-id'] = $importer['id'];
2552                 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
2553                 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
2554                 $msg['seen'] = 0;
2555                 $msg['replied'] = 0;
2556                 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
2557                 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
2558                 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
2559
2560                 dbesc_array($msg);
2561
2562                 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
2563                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
2564
2565                 // send notifications.
2566
2567                 require_once('include/enotify.php');
2568
2569                 $notif_params = array(
2570                         'type' => NOTIFY_MAIL,
2571                         'notify_flags' => $importer['notify-flags'],
2572                         'language' => $importer['language'],
2573                         'to_name' => $importer['username'],
2574                         'to_email' => $importer['email'],
2575                         'uid' => $importer['importer_uid'],
2576                         'item' => $msg,
2577                         'source_name' => $msg['from-name'],
2578                         'source_link' => $importer['url'],
2579                         'source_photo' => $importer['thumb'],
2580                         'verb' => ACTIVITY_POST,
2581                         'otype' => 'mail'
2582                 );
2583
2584                 notification($notif_params);
2585                 return 0;
2586
2587                 // NOTREACHED
2588         }
2589
2590         $community_page = 0;
2591         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
2592         if($rawtags) {
2593                 $community_page = intval($rawtags[0]['data']);
2594         }
2595         if(intval($importer['forum']) != $community_page) {
2596                 q("update contact set forum = %d where id = %d limit 1",
2597                         intval($community_page),
2598                         intval($importer['id'])
2599                 );
2600                 $importer['forum'] = (string) $community_page;
2601         }
2602
2603         logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
2604
2605         // process any deleted entries
2606
2607         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
2608         if(is_array($del_entries) && count($del_entries)) {
2609                 foreach($del_entries as $dentry) {
2610                         $deleted = false;
2611                         if(isset($dentry['attribs']['']['ref'])) {
2612                                 $uri = $dentry['attribs']['']['ref'];
2613                                 $deleted = true;
2614                                 if(isset($dentry['attribs']['']['when'])) {
2615                                         $when = $dentry['attribs']['']['when'];
2616                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
2617                                 }
2618                                 else
2619                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
2620                         }
2621                         if($deleted) {
2622
2623                                 // check for relayed deletes to our conversation
2624
2625                                 $is_reply = false;              
2626                                 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
2627                                         dbesc($uri),
2628                                         intval($importer['importer_uid'])
2629                                 );
2630                                 if(count($r)) {
2631                                         $parent_uri = $r[0]['parent-uri'];
2632                                         if($r[0]['id'] != $r[0]['parent'])
2633                                                 $is_reply = true;
2634                                 }                               
2635
2636                                 if($is_reply) {
2637                                         $community = false;
2638
2639                                         if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2640                                                 $sql_extra = '';
2641                                                 $community = true;
2642                                                 logger('local_delivery: possible community delete');
2643                                         }
2644                                         else
2645                                                 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2646  
2647                                         // was the top-level post for this reply written by somebody on this site? 
2648                                         // Specifically, the recipient? 
2649
2650                                         $is_a_remote_delete = false;
2651
2652                                         // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2653                                         $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, 
2654                                                 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
2655                                                 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
2656                                                 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2657                                                 AND `item`.`uid` = %d 
2658                                                 $sql_extra
2659                                                 LIMIT 1",
2660                                                 dbesc($parent_uri),
2661                                                 dbesc($parent_uri),
2662                                                 dbesc($parent_uri),
2663                                                 intval($importer['importer_uid'])
2664                                         );
2665                                         if($r && count($r))
2666                                                 $is_a_remote_delete = true;
2667
2668                                         // Does this have the characteristics of a community or private group comment?
2669                                         // If it's a reply to a wall post on a community/prvgroup page it's a 
2670                                         // valid community comment. Also forum_mode makes it valid for sure. 
2671                                         // If neither, it's not.
2672
2673                                         if($is_a_remote_delete && $community) {
2674                                                 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2675                                                         $is_a_remote_delete = false;
2676                                                         logger('local_delivery: not a community delete');
2677                                                 }
2678                                         }
2679
2680                                         if($is_a_remote_delete) {
2681                                                 logger('local_delivery: received remote delete');
2682                                         }
2683                                 }
2684
2685                                 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
2686                                         WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
2687                                         dbesc($uri),
2688                                         intval($importer['importer_uid']),
2689                                         intval($importer['id'])
2690                                 );
2691
2692                                 if(count($r)) {
2693                                         $item = $r[0];
2694
2695                                         if($item['deleted'])
2696                                                 continue;
2697
2698                                         logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
2699
2700                                         if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2701                                                 $xo = parse_xml_string($item['object'],false);
2702                                                 $xt = parse_xml_string($item['target'],false);
2703
2704                                                 if($xt->type === ACTIVITY_OBJ_NOTE) {
2705                                                         $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
2706                                                                 dbesc($xt->id),
2707                                                                 intval($importer['importer_uid'])
2708                                                         );
2709                                                         if(count($i)) {
2710
2711                                                                 // For tags, the owner cannot remove the tag on the author's copy of the post.
2712
2713                                                                 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
2714                                                                 $author_remove = (($item['origin'] && $item['self']) ? true : false);
2715                                                                 $author_copy = (($item['origin']) ? true : false);
2716
2717                                                                 if($owner_remove && $author_copy)
2718                                                                         continue;
2719                                                                 if($author_remove || $owner_remove) {
2720                                                                         $tags = explode(',',$i[0]['tag']);
2721                                                                         $newtags = array();
2722                                                                         if(count($tags)) {
2723                                                                                 foreach($tags as $tag)
2724                                                                                         if(trim($tag) !== trim($xo->body))
2725                                                                                                 $newtags[] = trim($tag);
2726                                                                         }
2727                                                                         q("update item set tag = '%s' where id = %d limit 1",
2728                                                                                 dbesc(implode(',',$newtags)),
2729                                                                                 intval($i[0]['id'])
2730                                                                         );
2731                                                                         create_tags_from_item($i[0]['id']);
2732                                                                 }
2733                                                         }
2734                                                 }
2735                                         }
2736
2737                                         if($item['uri'] == $item['parent-uri']) {
2738                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2739                                                         `body` = '', `title` = ''
2740                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
2741                                                         dbesc($when),
2742                                                         dbesc(datetime_convert()),
2743                                                         dbesc($item['uri']),
2744                                                         intval($importer['importer_uid'])
2745                                                 );
2746                                                 create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
2747                                         }
2748                                         else {
2749                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2750                                                         `body` = '', `title` = ''
2751                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2752                                                         dbesc($when),
2753                                                         dbesc(datetime_convert()),
2754                                                         dbesc($uri),
2755                                                         intval($importer['importer_uid'])
2756                                                 );
2757                                                 create_tags_from_itemuri($uri, $importer['importer_uid']);
2758                                                 if($item['last-child']) {
2759                                                         // ensure that last-child is set in case the comment that had it just got wiped.
2760                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2761                                                                 dbesc(datetime_convert()),
2762                                                                 dbesc($item['parent-uri']),
2763                                                                 intval($item['uid'])
2764                                                         );
2765                                                         // who is the last child now?
2766                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
2767                                                                 ORDER BY `created` DESC LIMIT 1",
2768                                                                         dbesc($item['parent-uri']),
2769                                                                         intval($importer['importer_uid'])
2770                                                         );
2771                                                         if(count($r)) {
2772                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
2773                                                                         intval($r[0]['id'])
2774                                                                 );
2775                                                         }
2776                                                 }
2777                                                 // if this is a relayed delete, propagate it to other recipients
2778
2779                                                 if($is_a_remote_delete)
2780                                                         proc_run('php',"include/notifier.php","drop",$item['id']);
2781                                         }
2782                                 }
2783                         }
2784                 }
2785         }
2786
2787
2788         foreach($feed->get_items() as $item) {
2789
2790                 $is_reply = false;              
2791                 $item_id = $item->get_id();
2792                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
2793                 if(isset($rawthread[0]['attribs']['']['ref'])) {
2794                         $is_reply = true;
2795                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
2796                 }
2797
2798                 if($is_reply) {
2799                         $community = false;
2800
2801                         if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2802                                 $sql_extra = '';
2803                                 $community = true;
2804                                 logger('local_delivery: possible community reply');
2805                         }
2806                         else
2807                                 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2808  
2809                         // was the top-level post for this reply written by somebody on this site? 
2810                         // Specifically, the recipient? 
2811
2812                         $is_a_remote_comment = false;
2813                         $top_uri = $parent_uri;
2814                         
2815                         $r = q("select `item`.`parent-uri` from `item`
2816                                 WHERE `item`.`uri` = '%s'
2817                                 LIMIT 1",
2818                                 dbesc($parent_uri)
2819                         );
2820                         if($r && count($r)) {
2821                                 $top_uri = $r[0]['parent-uri'];
2822
2823                                 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2824                                 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, 
2825                                         `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
2826                                         LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
2827                                         WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2828                                         AND `item`.`uid` = %d 
2829                                         $sql_extra
2830                                         LIMIT 1",
2831                                         dbesc($top_uri),
2832                                         dbesc($top_uri),
2833                                         dbesc($top_uri),
2834                                         intval($importer['importer_uid'])
2835                                 );
2836                                 if($r && count($r))
2837                                         $is_a_remote_comment = true;
2838                         }
2839
2840                         // Does this have the characteristics of a community or private group comment?
2841                         // If it's a reply to a wall post on a community/prvgroup page it's a 
2842                         // valid community comment. Also forum_mode makes it valid for sure. 
2843                         // If neither, it's not.
2844
2845                         if($is_a_remote_comment && $community) {
2846                                 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2847                                         $is_a_remote_comment = false;
2848                                         logger('local_delivery: not a community reply');
2849                                 }
2850                         }
2851
2852                         if($is_a_remote_comment) {
2853                                 logger('local_delivery: received remote comment');
2854                                 $is_like = false;
2855                                 // remote reply to our post. Import and then notify everybody else.
2856
2857                                 $datarray = get_atom_elements($feed,$item);
2858
2859                                 $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body`  FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2860                                         dbesc($item_id),
2861                                         intval($importer['importer_uid'])
2862                                 );
2863
2864                                 // Update content if 'updated' changes
2865
2866                                 if(count($r)) {
2867                                         $iid = $r[0]['id'];
2868                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2869
2870                                                 // do not accept (ignore) an earlier edit than one we currently have.
2871                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2872                                                         continue;
2873
2874                                                 logger('received updated comment' , LOGGER_DEBUG);
2875                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2876                                                         dbesc($datarray['title']),
2877                                                         dbesc($datarray['body']),
2878                                                         dbesc($datarray['tag']),
2879                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2880                                                         dbesc($item_id),
2881                                                         intval($importer['importer_uid'])
2882                                                 );
2883                                                 create_tags_from_itemuri($item_id, $importer['importer_uid']);
2884
2885                                                 proc_run('php',"include/notifier.php","comment-import",$iid);
2886
2887                                         }
2888
2889                                         continue;
2890                                 }
2891
2892
2893
2894                                 $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
2895                                         intval($importer['importer_uid'])
2896                                 );
2897
2898
2899                                 $datarray['type'] = 'remote-comment';
2900                                 $datarray['wall'] = 1;
2901                                 $datarray['parent-uri'] = $parent_uri;
2902                                 $datarray['uid'] = $importer['importer_uid'];
2903                                 $datarray['owner-name'] = $own[0]['name'];
2904                                 $datarray['owner-link'] = $own[0]['url'];
2905                                 $datarray['owner-avatar'] = $own[0]['thumb'];
2906                                 $datarray['contact-id'] = $importer['id'];
2907
2908                                 if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
2909                                         $is_like = true;
2910                                         $datarray['type'] = 'activity';
2911                                         $datarray['gravity'] = GRAVITY_LIKE;
2912                                         $datarray['last-child'] = 0;
2913                                         // only one like or dislike per person
2914                                         $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s' or `parent-uri` = '%s') and deleted = 0 limit 1",
2915                                                 intval($datarray['uid']),
2916                                                 intval($datarray['contact-id']),
2917                                                 dbesc($datarray['verb']),
2918                                                 dbesc($datarray['parent-uri']),
2919                                                 dbesc($datarray['parent-uri'])
2920
2921                                         );
2922                                         if($r && count($r))
2923                                                 continue;
2924                                 }
2925
2926                                 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2927
2928                                         $xo = parse_xml_string($datarray['object'],false);
2929                                         $xt = parse_xml_string($datarray['target'],false);
2930
2931                                         if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
2932
2933                                                 // fetch the parent item
2934
2935                                                 $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
2936                                                         dbesc($xt->id),
2937                                                         intval($importer['importer_uid'])
2938                                                 );
2939                                                 if(! count($tagp))
2940                                                         continue;
2941
2942                                                 // extract tag, if not duplicate, and this user allows tags, add to parent item
2943
2944                                                 if($xo->id && $xo->content) {
2945                                                         $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2946                                                         if(! (stristr($tagp[0]['tag'],$newtag))) {
2947                                                                 $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
2948                                                                         intval($importer['importer_uid'])
2949                                                                 );
2950                                                                 if(count($i) && ! intval($i[0]['blocktags'])) {
2951                                                                         q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
2952                                                                                 dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
2953                                                                                 intval($tagp[0]['id']),
2954                                                                                 dbesc(datetime_convert())
2955                                                                         );
2956                                                                         create_tags_from_item($tagp[0]['id']);
2957                                                                 }
2958                                                         }
2959                                                 }
2960                                         }
2961                                 }
2962
2963
2964                                 $posted_id = item_store($datarray);
2965                                 $parent = 0;
2966
2967                                 if($posted_id) {
2968                                         $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2969                                                 intval($posted_id),
2970                                                 intval($importer['importer_uid'])
2971                                         );
2972                                         if(count($r)) {
2973                                                 $parent = $r[0]['parent'];
2974                                                 $parent_uri = $r[0]['parent-uri'];
2975                                         }
2976
2977                                         if(! $is_like) {
2978                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
2979                                                         dbesc(datetime_convert()),
2980                                                         intval($importer['importer_uid']),
2981                                                         intval($r[0]['parent'])
2982                                                 );
2983
2984                                                 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2985                                                         dbesc(datetime_convert()),
2986                                                         intval($importer['importer_uid']),
2987                                                         intval($posted_id)
2988                                                 );
2989                                         }
2990
2991                                         if($posted_id && $parent) {
2992
2993                                                 proc_run('php',"include/notifier.php","comment-import","$posted_id");
2994
2995                                                 if((! $is_like) && (! $importer['self'])) {
2996
2997                                                         require_once('include/enotify.php');
2998
2999                                                         notification(array(
3000                                                                 'type'         => NOTIFY_COMMENT,
3001                                                                 'notify_flags' => $importer['notify-flags'],
3002                                                                 'language'     => $importer['language'],
3003                                                                 'to_name'      => $importer['username'],
3004                                                                 'to_email'     => $importer['email'],
3005                                                                 'uid'          => $importer['importer_uid'],
3006                                                                 'item'         => $datarray,
3007                                                                 'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3008                                                                 'source_name'  => stripslashes($datarray['author-name']),
3009                                                                 'source_link'  => $datarray['author-link'],
3010                                                                 'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
3011                                                                         ? $importer['thumb'] : $datarray['author-avatar']),
3012                                                                 'verb'         => ACTIVITY_POST,
3013                                                                 'otype'        => 'item',
3014                                                                 'parent'       => $parent,
3015                                                                 'parent_uri'   => $parent_uri,
3016                                                         ));
3017
3018                                                 }
3019                                         }
3020
3021                                         return 0;
3022                                         // NOTREACHED
3023                                 }
3024                         }
3025                         else {
3026
3027                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
3028
3029                                 $item_id  = $item->get_id();
3030                                 $datarray = get_atom_elements($feed,$item);
3031
3032                                 if($importer['rel'] == CONTACT_IS_FOLLOWER)
3033                                         continue;
3034
3035                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3036                                         dbesc($item_id),
3037                                         intval($importer['importer_uid'])
3038                                 );
3039
3040                                 // Update content if 'updated' changes
3041
3042                                 if(count($r)) {
3043                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
3044
3045                                                 // do not accept (ignore) an earlier edit than one we currently have.
3046                                                 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3047                                                         continue;
3048
3049                                                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3050                                                         dbesc($datarray['title']),
3051                                                         dbesc($datarray['body']),
3052                                                         dbesc($datarray['tag']),
3053                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3054                                                         dbesc($item_id),
3055                                                         intval($importer['importer_uid'])
3056                                                 );
3057                                                 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3058                                         }
3059
3060                                         // update last-child if it changes
3061
3062                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3063                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
3064                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
3065                                                         dbesc(datetime_convert()),
3066                                                         dbesc($parent_uri),
3067                                                         intval($importer['importer_uid'])
3068                                                 );
3069                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3070                                                         intval($allow[0]['data']),
3071                                                         dbesc(datetime_convert()),
3072                                                         dbesc($item_id),
3073                                                         intval($importer['importer_uid'])
3074                                                 );
3075                                         }
3076                                         continue;
3077                                 }
3078
3079                                 $datarray['parent-uri'] = $parent_uri;
3080                                 $datarray['uid'] = $importer['importer_uid'];
3081                                 $datarray['contact-id'] = $importer['id'];
3082                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
3083                                         $datarray['type'] = 'activity';
3084                                         $datarray['gravity'] = GRAVITY_LIKE;
3085                                         // only one like or dislike per person
3086                                         $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
3087                                                 intval($datarray['uid']),
3088                                                 intval($datarray['contact-id']),
3089                                                 dbesc($datarray['verb']),
3090                                                 dbesc($parent_uri),
3091                                                 dbesc($parent_uri)
3092                                         );
3093                                         if($r && count($r))
3094                                                 continue;
3095
3096                                 }
3097
3098                                 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3099
3100                                         $xo = parse_xml_string($datarray['object'],false);
3101                                         $xt = parse_xml_string($datarray['target'],false);
3102
3103                                         if($xt->type == ACTIVITY_OBJ_NOTE) {
3104                                                 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
3105                                                         dbesc($xt->id),
3106                                                         intval($importer['importer_uid'])
3107                                                 );
3108                                                 if(! count($r))
3109                                                         continue;
3110
3111                                                 // extract tag, if not duplicate, add to parent item
3112                                                 if($xo->content) {
3113                                                         if(! (stristr($r[0]['tag'],trim($xo->content)))) {
3114                                                                 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
3115                                                                         dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
3116                                                                         intval($r[0]['id'])
3117                                                                 );
3118                                                                 create_tags_from_item($r[0]['id']);
3119                                                         }
3120                                                 }
3121                                         }
3122                                 }
3123
3124                                 $posted_id = item_store($datarray);
3125
3126                                 // find out if our user is involved in this conversation and wants to be notified.
3127
3128                                 if(!x($datarray['type']) || $datarray['type'] != 'activity') {
3129
3130                                         $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
3131                                                 dbesc($top_uri),
3132                                                 intval($importer['importer_uid'])
3133                                         );
3134
3135                                         if(count($myconv)) {
3136                                                 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
3137
3138                                                 // first make sure this isn't our own post coming back to us from a wall-to-wall event
3139                                                 if(! link_compare($datarray['author-link'],$importer_url)) {
3140
3141
3142                                                         foreach($myconv as $conv) {
3143
3144                                                                 // now if we find a match, it means we're in this conversation
3145
3146                                                                 if(! link_compare($conv['author-link'],$importer_url))
3147                                                                         continue;
3148
3149                                                                 require_once('include/enotify.php');
3150                                                                 
3151                                                                 $conv_parent = $conv['parent'];
3152
3153                                                                 notification(array(
3154                                                                         'type'         => NOTIFY_COMMENT,
3155                                                                         'notify_flags' => $importer['notify-flags'],
3156                                                                         'language'     => $importer['language'],
3157                                                                         'to_name'      => $importer['username'],
3158                                                                         'to_email'     => $importer['email'],
3159                                                                         'uid'          => $importer['importer_uid'],
3160                                                                         'item'         => $datarray,
3161                                                                         'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3162                                                                         'source_name'  => stripslashes($datarray['author-name']),
3163                                                                         'source_link'  => $datarray['author-link'],
3164                                                                         'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
3165                                                                                 ? $importer['thumb'] : $datarray['author-avatar']),
3166                                                                         'verb'         => ACTIVITY_POST,
3167                                                                         'otype'        => 'item',
3168                                                                         'parent'       => $conv_parent,
3169                                                                         'parent_uri'   => $parent_uri
3170
3171                                                                 ));
3172
3173                                                                 // only send one notification
3174                                                                 break;
3175                                                         }
3176                                                 }
3177                                         }
3178                                 }
3179                                 continue;
3180                         }
3181                 }
3182
3183                 else {
3184
3185                         // Head post of a conversation. Have we seen it? If not, import it.
3186
3187
3188                         $item_id  = $item->get_id();
3189                         $datarray = get_atom_elements($feed,$item);
3190
3191                         if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
3192                                 $ev = bbtoevent($datarray['body']);
3193                                 if(x($ev,'desc') && x($ev,'start')) {
3194                                         $ev['cid'] = $importer['id'];
3195                                         $ev['uid'] = $importer['uid'];
3196                                         $ev['uri'] = $item_id;
3197                                         $ev['edited'] = $datarray['edited'];
3198                                         $ev['private'] = $datarray['private'];
3199
3200                                         $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3201                                                 dbesc($item_id),
3202                                                 intval($importer['uid'])
3203                                         );
3204                                         if(count($r))
3205                                                 $ev['id'] = $r[0]['id'];
3206                                         $xyz = event_store($ev);
3207                                         continue;
3208                                 }
3209                         }
3210
3211                         $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3212                                 dbesc($item_id),
3213                                 intval($importer['importer_uid'])
3214                         );
3215
3216                         // Update content if 'updated' changes
3217
3218                         if(count($r)) {
3219                                 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
3220
3221                                         // do not accept (ignore) an earlier edit than one we currently have.
3222                                         if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3223                                                 continue;
3224
3225                                         $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3226                                                 dbesc($datarray['title']),
3227                                                 dbesc($datarray['body']),
3228                                                 dbesc($datarray['tag']),
3229                                                 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3230                                                 dbesc($item_id),
3231                                                 intval($importer['importer_uid'])
3232                                         );
3233                                         create_tags_from_itemuri($item_id, $importer['importer_uid']);
3234                                 }
3235
3236                                 // update last-child if it changes
3237
3238                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3239                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
3240                                         $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3241                                                 intval($allow[0]['data']),
3242                                                 dbesc(datetime_convert()),
3243                                                 dbesc($item_id),
3244                                                 intval($importer['importer_uid'])
3245                                         );
3246                                 }
3247                                 continue;
3248                         }
3249
3250                         // This is my contact on another system, but it's really me.
3251                         // Turn this into a wall post.
3252
3253                         if($importer['remote_self'])
3254                                 $datarray['wall'] = 1;
3255
3256                         $datarray['parent-uri'] = $item_id;
3257                         $datarray['uid'] = $importer['importer_uid'];
3258                         $datarray['contact-id'] = $importer['id'];
3259
3260
3261                         if(! link_compare($datarray['owner-link'],$importer['url'])) {
3262                                 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, 
3263                                 // but otherwise there's a possible data mixup on the sender's system.
3264                                 // the tgroup delivery code called from item_store will correct it if it's a forum,
3265                                 // but we're going to unconditionally correct it here so that the post will always be owned by our contact. 
3266                                 logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
3267                                 $datarray['owner-name']   = $importer['senderName'];
3268                                 $datarray['owner-link']   = $importer['url'];
3269                                 $datarray['owner-avatar'] = $importer['thumb'];
3270                         }
3271
3272                         if(($importer['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['importer_uid'],$datarray)))
3273                                 continue;
3274
3275                         $posted_id = item_store($datarray);
3276
3277                         if(stristr($datarray['verb'],ACTIVITY_POKE)) {
3278                                 $verb = urldecode(substr($datarray['verb'],strpos($datarray['verb'],'#')+1));
3279                                 if(! $verb)
3280                                         continue;
3281                                 $xo = parse_xml_string($datarray['object'],false);
3282
3283                                 if(($xo->type == ACTIVITY_OBJ_PERSON) && ($xo->id)) {
3284
3285                                         // somebody was poked/prodded. Was it me?
3286
3287                                         $links = parse_xml_string("<links>".unxmlify($xo->link)."</links>",false);
3288
3289                                 foreach($links->link as $l) {
3290                                 $atts = $l->attributes();
3291                                 switch($atts['rel']) {
3292                                         case "alternate": 
3293                                                                 $Blink = $atts['href'];
3294                                                                 break;
3295                                                         default:
3296                                                                 break;
3297                                     }
3298                                 }
3299                                         if($Blink && link_compare($Blink,$a->get_baseurl() . '/profile/' . $importer['nickname'])) {
3300
3301                                                 // send a notification
3302                                                 require_once('include/enotify.php');
3303                                                                 
3304                                                 notification(array(
3305                                                         'type'         => NOTIFY_POKE,
3306                                                         'notify_flags' => $importer['notify-flags'],
3307                                                         'language'     => $importer['language'],
3308                                                         'to_name'      => $importer['username'],
3309                                                         'to_email'     => $importer['email'],
3310                                                         'uid'          => $importer['importer_uid'],
3311                                                         'item'         => $datarray,
3312                                                         'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3313                                                         'source_name'  => stripslashes($datarray['author-name']),
3314                                                         'source_link'  => $datarray['author-link'],
3315                                                         'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
3316                                                                 ? $importer['thumb'] : $datarray['author-avatar']),
3317                                                         'verb'         => $datarray['verb'],
3318                                                         'otype'        => 'person',
3319                                                         'activity'     => $verb,
3320
3321                                                 ));
3322                                         }
3323                                 }
3324                         }                       
3325
3326                         continue;
3327                 }
3328         }
3329
3330         return 0;
3331         // NOTREACHED
3332
3333 }
3334
3335
3336 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
3337         $url = notags(trim($datarray['author-link']));
3338         $name = notags(trim($datarray['author-name']));
3339         $photo = notags(trim($datarray['author-avatar']));
3340
3341         $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
3342         if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
3343                 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
3344
3345         if(is_array($contact)) {
3346                 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
3347                         || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
3348                         $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
3349                                 intval(CONTACT_IS_FRIEND),
3350                                 intval($contact['id']),
3351                                 intval($importer['uid'])
3352                         );
3353                 }
3354                 // send email notification to owner?
3355         }
3356         else {
3357
3358                 // create contact record
3359
3360                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`, 
3361                         `blocked`, `readonly`, `pending`, `writable` )
3362                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
3363                         intval($importer['uid']),
3364                         dbesc(datetime_convert()),
3365                         dbesc($url),
3366                         dbesc(normalise_link($url)),
3367                         dbesc($name),
3368                         dbesc($nick),
3369                         dbesc($photo),
3370                         dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
3371                         intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
3372                 );
3373                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
3374                                 intval($importer['uid']),
3375                                 dbesc($url)
3376                 );
3377                 if(count($r))
3378                                 $contact_record = $r[0];
3379
3380                 // create notification
3381                 $hash = random_string();
3382
3383                 if(is_array($contact_record)) {
3384                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
3385                                 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
3386                                 intval($importer['uid']),
3387                                 intval($contact_record['id']),
3388                                 dbesc($hash),
3389                                 dbesc(datetime_convert())
3390                         );
3391                 }
3392                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
3393                         intval($importer['uid'])
3394                 );
3395                 $a = get_app();
3396                 if(count($r)) {
3397
3398                         if(intval($r[0]['def_gid'])) {
3399                                 require_once('include/group.php');
3400                                 group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
3401                         }
3402
3403                         if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
3404                                 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
3405                                 $email = replace_macros($email_tpl, array(
3406                                         '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
3407                                         '$url' => $url,
3408                                         '$myname' => $r[0]['username'],
3409                                         '$siteurl' => $a->get_baseurl(),
3410                                         '$sitename' => $a->config['sitename']
3411                                 ));
3412                                 $res = mail($r[0]['email'], 
3413                                         (($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],
3414                                         $email,
3415                                         'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
3416                                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
3417                                         . 'Content-transfer-encoding: 8bit' );
3418
3419                         }
3420                 }
3421         }
3422 }
3423
3424 function lose_follower($importer,$contact,$datarray,$item) {
3425
3426         if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
3427                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3428                         intval(CONTACT_IS_SHARING),
3429                         intval($contact['id'])
3430                 );
3431         }
3432         else {
3433                 contact_remove($contact['id']);
3434         }
3435 }
3436
3437 function lose_sharer($importer,$contact,$datarray,$item) {
3438
3439         if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
3440                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3441                         intval(CONTACT_IS_FOLLOWER),
3442                         intval($contact['id'])
3443                 );
3444         }
3445         else {
3446                 contact_remove($contact['id']);
3447         }
3448 }
3449
3450
3451 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
3452
3453         $a = get_app();
3454
3455         if(is_array($importer)) {
3456                 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
3457                         intval($importer['uid'])
3458                 );
3459         }
3460
3461         // Diaspora has different message-ids in feeds than they do 
3462         // through the direct Diaspora protocol. If we try and use
3463         // the feed, we'll get duplicates. So don't.
3464
3465         if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
3466                 return;
3467
3468         $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
3469
3470         // Use a single verify token, even if multiple hubs
3471
3472         $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
3473
3474         $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
3475
3476         logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: '  . $push_url . ' with verifier ' . $verify_token);
3477
3478         if(! strlen($contact['hub-verify'])) {
3479                 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
3480                         dbesc($verify_token),
3481                         intval($contact['id'])
3482                 );
3483         }
3484
3485         post_url($url,$params);
3486
3487         logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
3488
3489         return;
3490
3491 }
3492
3493
3494 function atom_author($tag,$name,$uri,$h,$w,$photo) {
3495         $o = '';
3496         if(! $tag)
3497                 return $o;
3498         $name = xmlify($name);
3499         $uri = xmlify($uri);
3500         $h = intval($h);
3501         $w = intval($w);
3502         $photo = xmlify($photo);
3503
3504
3505         $o .= "<$tag>\r\n";
3506         $o .= "<name>$name</name>\r\n";
3507         $o .= "<uri>$uri</uri>\r\n";
3508         $o .= '<link rel="photo"  type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3509         $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3510
3511         call_hooks('atom_author', $o);
3512
3513         $o .= "</$tag>\r\n";
3514         return $o;
3515 }
3516
3517 function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
3518
3519         $a = get_app();
3520
3521         if(! $item['parent'])
3522                 return;
3523
3524         if($item['deleted'])
3525                 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
3526
3527
3528         if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
3529                 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
3530         else
3531                 $body = $item['body'];
3532
3533         $o = "\r\n\r\n<entry>\r\n";
3534
3535         if(is_array($author))
3536                 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
3537         else
3538                 $o .= atom_author('author',(($item['author-name']) ? $item['author-name'] : $item['name']),(($item['author-link']) ? $item['author-link'] : $item['url']),80,80,(($item['author-avatar']) ? $item['author-avatar'] : $item['thumb']));
3539         if(strlen($item['owner-name']))
3540                 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
3541
3542         if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
3543                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
3544                 $o .= '<thr:in-reply-to ref="' . xmlify($parent_item) . '" type="text/html" href="' .  xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['parent']) . '" />' . "\r\n";
3545         }
3546
3547         $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
3548         $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
3549         $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
3550         $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
3551         $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
3552         $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
3553         $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
3554         if($comment)
3555                 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
3556
3557         if($item['location']) {
3558                 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
3559                 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
3560         }
3561
3562         if($item['coord'])
3563                 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
3564
3565         if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
3566                 $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
3567
3568         if($item['extid'])
3569                 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
3570         if($item['bookmark'])
3571                 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
3572
3573         if($item['app'])
3574                 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
3575
3576         if($item['guid'])
3577                 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
3578
3579         if($item['signed_text']) {
3580                 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
3581                 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
3582         }
3583
3584         $verb = construct_verb($item);
3585         $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
3586         $actobj = construct_activity_object($item);
3587         if(strlen($actobj))
3588                 $o .= $actobj;
3589         $actarg = construct_activity_target($item);
3590         if(strlen($actarg))
3591                 $o .= $actarg;
3592
3593         $tags = item_getfeedtags($item);
3594         if(count($tags)) {
3595                 foreach($tags as $t) {
3596                         $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
3597                 }
3598         }
3599
3600         $o .= item_getfeedattach($item);
3601
3602         $mentioned = get_mentions($item);
3603         if($mentioned)
3604                 $o .= $mentioned;
3605         
3606         call_hooks('atom_entry', $o);
3607
3608         $o .= '</entry>' . "\r\n";
3609         
3610         return $o;
3611 }
3612
3613 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
3614         $a = get_app();
3615
3616         logger('fix_private_photos', LOGGER_DEBUG);
3617         $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
3618
3619         $orig_body = $s;
3620         $new_body = '';
3621
3622         $img_start = strpos($orig_body, '[img');
3623         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3624         $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3625         while( ($img_st_close !== false) && ($img_len !== false) ) {
3626
3627                 $img_st_close++; // make it point to AFTER the closing bracket
3628                 $image = substr($orig_body, $img_start + $img_st_close, $img_len);
3629
3630                 logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
3631
3632
3633                 if(stristr($image , $site . '/photo/')) {
3634                         // Only embed locally hosted photos
3635                         $replace = false;
3636                         $i = basename($image);
3637                         $i = str_replace(array('.jpg','.png'),array('',''),$i);
3638                         $x = strpos($i,'-');
3639
3640                         if($x) {
3641                                 $res = substr($i,$x+1);
3642                                 $i = substr($i,0,$x);
3643                                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
3644                                         dbesc($i),
3645                                         intval($res),
3646                                         intval($uid)
3647                                 );
3648                                 if(count($r)) {
3649
3650                                         // Check to see if we should replace this photo link with an embedded image
3651                                         // 1. No need to do so if the photo is public
3652                                         // 2. If there's a contact-id provided, see if they're in the access list
3653                                         //    for the photo. If so, embed it. 
3654                                         // 3. Otherwise, if we have an item, see if the item permissions match the photo
3655                                         //    permissions, regardless of order but first check to see if they're an exact
3656                                         //    match to save some processing overhead.
3657
3658                                         if(has_permissions($r[0])) {
3659                                                 if($cid) {
3660                                                         $recips = enumerate_permissions($r[0]);
3661                                                         if(in_array($cid, $recips)) {
3662                                                                 $replace = true;        
3663                                                         }
3664                                                 }
3665                                                 elseif($item) {
3666                                                         if(compare_permissions($item,$r[0]))
3667                                                                 $replace = true;
3668                                                 }
3669                                         }
3670                                         if($replace) {
3671                                                 $data = $r[0]['data'];
3672                                                 $type = $r[0]['type'];
3673
3674                                                 // If a custom width and height were specified, apply before embedding
3675                                                 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
3676                                                         logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
3677
3678                                                         $width = intval($match[1]);
3679                                                         $height = intval($match[2]);
3680
3681                                                         $ph = new Photo($data, $type);
3682                                                         if($ph->is_valid()) {
3683                                                                 $ph->scaleImage(max($width, $height));
3684                                                                 $data = $ph->imageString();
3685                                                                 $type = $ph->getType();
3686                                                         }
3687                                                 }
3688
3689                                                 logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
3690                                                 $image = 'data:' . $type . ';base64,' . base64_encode($data);
3691                                                 logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
3692                                         }
3693                                 }
3694                         }
3695                 }       
3696
3697                 $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
3698                 $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
3699                 if($orig_body === false)
3700                         $orig_body = '';
3701
3702                 $img_start = strpos($orig_body, '[img');
3703                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3704                 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3705         }
3706
3707         $new_body = $new_body . $orig_body;
3708
3709         return($new_body);
3710 }
3711
3712
3713 function has_permissions($obj) {
3714         if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
3715                 return true;
3716         return false;
3717 }
3718
3719 function compare_permissions($obj1,$obj2) {
3720         // first part is easy. Check that these are exactly the same. 
3721         if(($obj1['allow_cid'] == $obj2['allow_cid'])
3722                 && ($obj1['allow_gid'] == $obj2['allow_gid'])
3723                 && ($obj1['deny_cid'] == $obj2['deny_cid'])
3724                 && ($obj1['deny_gid'] == $obj2['deny_gid']))
3725                 return true;
3726
3727         // This is harder. Parse all the permissions and compare the resulting set.
3728
3729         $recipients1 = enumerate_permissions($obj1);
3730         $recipients2 = enumerate_permissions($obj2);
3731         sort($recipients1);
3732         sort($recipients2);
3733         if($recipients1 == $recipients2)
3734                 return true;
3735         return false;
3736 }
3737
3738 // returns an array of contact-ids that are allowed to see this object
3739
3740 function enumerate_permissions($obj) {
3741         require_once('include/group.php');
3742         $allow_people = expand_acl($obj['allow_cid']);
3743         $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
3744         $deny_people  = expand_acl($obj['deny_cid']);
3745         $deny_groups  = expand_groups(expand_acl($obj['deny_gid']));
3746         $recipients   = array_unique(array_merge($allow_people,$allow_groups));
3747         $deny         = array_unique(array_merge($deny_people,$deny_groups));
3748         $recipients   = array_diff($recipients,$deny);
3749         return $recipients;
3750 }
3751
3752 function item_getfeedtags($item) {
3753         $ret = array();
3754         $matches = false;
3755         $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3756         if($cnt) {
3757                 for($x = 0; $x < $cnt; $x ++) {
3758                         if($matches[1][$x])
3759                                 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
3760                 }
3761         }
3762         $matches = false; 
3763         $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3764         if($cnt) {
3765                 for($x = 0; $x < $cnt; $x ++) {
3766                         if($matches[1][$x])
3767                                 $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
3768                 }
3769         } 
3770         return $ret;
3771 }
3772
3773 function item_getfeedattach($item) {
3774         $ret = '';
3775         $arr = explode(',',$item['attach']);
3776         if(count($arr)) {
3777                 foreach($arr as $r) {
3778                         $matches = false;
3779                         $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
3780                         if($cnt) {
3781                                 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
3782                                 if(intval($matches[2]))
3783                                         $ret .= 'length="' . intval($matches[2]) . '" ';
3784                                 if($matches[4] !== ' ')
3785                                         $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
3786                                 $ret .= ' />' . "\r\n";
3787                         }
3788                 }
3789         }
3790         return $ret;
3791 }
3792
3793
3794         
3795 function item_expire($uid,$days) {
3796
3797         if((! $uid) || ($days < 1))
3798                 return;
3799
3800         // $expire_network_only = save your own wall posts
3801         // and just expire conversations started by others
3802
3803         $expire_network_only = get_pconfig($uid,'expire','network_only');
3804         $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
3805
3806         $r = q("SELECT * FROM `item` 
3807                 WHERE `uid` = %d 
3808                 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY 
3809                 AND `id` = `parent` 
3810                 $sql_extra
3811                 AND `deleted` = 0",
3812                 intval($uid),
3813                 intval($days)
3814         );
3815
3816         if(! count($r))
3817                 return;
3818
3819         $expire_items = get_pconfig($uid, 'expire','items');
3820         $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
3821
3822         $expire_notes = get_pconfig($uid, 'expire','notes');
3823         $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
3824
3825         $expire_starred = get_pconfig($uid, 'expire','starred');
3826         $expire_starred = (($expire_starred===false)?1:intval($expire_starred)); // default if not set: 1
3827
3828         $expire_photos = get_pconfig($uid, 'expire','photos');
3829         $expire_photos = (($expire_photos===false)?0:intval($expire_photos)); // default if not set: 0
3830
3831         logger('expire: # items=' . count($r). "; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
3832
3833         foreach($r as $item) {
3834
3835                 // don't expire filed items
3836
3837                 if(strpos($item['file'],'[') !== false)
3838                         continue;
3839
3840                 // Only expire posts, not photos and photo comments
3841
3842                 if($expire_photos==0 && strlen($item['resource-id']))
3843                         continue;
3844                 if($expire_starred==0 && intval($item['starred']))
3845                         continue;
3846                 if($expire_notes==0 && $item['type']=='note')
3847                         continue;
3848                 if($expire_items==0 && $item['type']!='note')
3849                         continue;
3850
3851                 drop_item($item['id'],false);
3852         }
3853
3854         proc_run('php',"include/notifier.php","expire","$uid");
3855         
3856 }
3857
3858
3859 function drop_items($items) {
3860         $uid = 0;
3861
3862         if(! local_user() && ! remote_user())
3863                 return;
3864
3865         if(count($items)) {
3866                 foreach($items as $item) {
3867                         $owner = drop_item($item,false);
3868                         if($owner && ! $uid)
3869                                 $uid = $owner;
3870                 }
3871         }
3872
3873         // multiple threads may have been deleted, send an expire notification
3874
3875         if($uid)
3876                 proc_run('php',"include/notifier.php","expire","$uid");
3877 }
3878
3879
3880 function drop_item($id,$interactive = true) {
3881
3882         $a = get_app();
3883
3884         // locate item to be deleted
3885
3886         $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
3887                 intval($id)
3888         );
3889
3890         if(! count($r)) {
3891                 if(! $interactive)
3892                         return 0;
3893                 notice( t('Item not found.') . EOL);
3894                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3895         }
3896
3897         $item = $r[0];
3898
3899         $owner = $item['uid'];
3900
3901         $cid = 0;
3902
3903         // check if logged in user is either the author or owner of this item
3904
3905         if(is_array($_SESSION['remote'])) {
3906                 foreach($_SESSION['remote'] as $visitor) {
3907                         if($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
3908                                 $cid = $visitor['cid'];
3909                                 break;
3910                         }
3911                 }
3912         }
3913
3914
3915         if((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
3916
3917                 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
3918                 // delete the item
3919
3920                 $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
3921                         dbesc(datetime_convert()),
3922                         dbesc(datetime_convert()),
3923                         intval($item['id'])
3924                 );
3925                 create_tags_from_item($item['id']);
3926
3927                 // clean up categories and tags so they don't end up as orphans
3928
3929                 $matches = false;
3930                 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
3931                 if($cnt) {
3932                         foreach($matches as $mtch) {
3933                                 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
3934                         }
3935                 }
3936
3937                 $matches = false;
3938
3939                 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
3940                 if($cnt) {
3941                         foreach($matches as $mtch) {
3942                                 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
3943                         }
3944                 }
3945
3946                 // If item is a link to a photo resource, nuke all the associated photos 
3947                 // (visitors will not have photo resources)
3948                 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
3949                 // generate a resource-id and therefore aren't intimately linked to the item. 
3950
3951                 if(strlen($item['resource-id'])) {
3952                         q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
3953                                 dbesc($item['resource-id']),
3954                                 intval($item['uid'])
3955                         );
3956                         // ignore the result
3957                 }
3958
3959                 // If item is a link to an event, nuke the event record.
3960
3961                 if(intval($item['event-id'])) {
3962                         q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
3963                                 intval($item['event-id']),
3964                                 intval($item['uid'])
3965                         );
3966                         // ignore the result
3967                 }
3968
3969                 // clean up item_id and sign meta-data tables
3970
3971                 $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
3972                         intval($item['id']),
3973                         intval($item['uid'])
3974                 );
3975
3976                 $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
3977                         intval($item['id']),
3978                         intval($item['uid'])
3979                 );
3980
3981                 // If it's the parent of a comment thread, kill all the kids
3982
3983                 if($item['uri'] == $item['parent-uri']) {
3984                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = ''
3985                                 WHERE `parent-uri` = '%s' AND `uid` = %d ",
3986                                 dbesc(datetime_convert()),
3987                                 dbesc(datetime_convert()),
3988                                 dbesc($item['parent-uri']),
3989                                 intval($item['uid'])
3990                         );
3991                         create_tags_from_item($item['parent-uri'], $item['uid']);
3992                         // ignore the result
3993                 }
3994                 else {
3995                         // ensure that last-child is set in case the comment that had it just got wiped.
3996                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
3997                                 dbesc(datetime_convert()),
3998                                 dbesc($item['parent-uri']),
3999                                 intval($item['uid'])
4000                         );
4001                         // who is the last child now?
4002                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
4003                                 dbesc($item['parent-uri']),
4004                                 intval($item['uid'])
4005                         );
4006                         if(count($r)) {
4007                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
4008                                         intval($r[0]['id'])
4009                                 );
4010                         }
4011
4012                         // Add a relayable_retraction signature for Diaspora.
4013                         store_diaspora_retract_sig($item, $a->user, $a->get_baseurl());
4014                 }
4015                 $drop_id = intval($item['id']);
4016
4017                 // send the notification upstream/downstream as the case may be
4018
4019                 proc_run('php',"include/notifier.php","drop","$drop_id");
4020
4021                 if(! $interactive)
4022                         return $owner;
4023                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4024                 //NOTREACHED
4025         }
4026         else {
4027                 if(! $interactive)
4028                         return 0;
4029                 notice( t('Permission denied.') . EOL);
4030                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4031                 //NOTREACHED
4032         }
4033
4034 }
4035
4036
4037 function first_post_date($uid,$wall = false) {
4038         $r = q("select id, created from item 
4039                 where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0 
4040                 and id = parent
4041                 order by created asc limit 1",
4042                 intval($uid),
4043                 intval($wall ? 1 : 0)
4044         );
4045         if(count($r)) {
4046 //              logger('first_post_date: ' . $r[0]['id'] . ' ' . $r[0]['created'], LOGGER_DATA);
4047                 return substr(datetime_convert('',date_default_timezone_get(),$r[0]['created']),0,10);
4048         }
4049         return false;
4050 }
4051
4052 function posted_dates($uid,$wall) {
4053         $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
4054
4055         $dthen = first_post_date($uid,$wall);
4056         if(! $dthen)
4057                 return array();
4058
4059         // If it's near the end of a long month, backup to the 28th so that in 
4060         // consecutive loops we'll always get a whole month difference.
4061
4062         if(intval(substr($dnow,8)) > 28)
4063                 $dnow = substr($dnow,0,8) . '28';
4064         if(intval(substr($dthen,8)) > 28)
4065                 $dnow = substr($dthen,0,8) . '28';
4066
4067         $ret = array();
4068         // Starting with the current month, get the first and last days of every
4069         // month down to and including the month of the first post
4070         while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
4071                 $dstart = substr($dnow,0,8) . '01';
4072                 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
4073                 $start_month = datetime_convert('','',$dstart,'Y-m-d');
4074                 $end_month = datetime_convert('','',$dend,'Y-m-d');
4075                 $str = day_translate(datetime_convert('','',$dnow,'F Y'));
4076                 $ret[] = array($str,$end_month,$start_month);
4077                 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
4078         }
4079         return $ret;
4080 }
4081
4082
4083 function posted_date_widget($url,$uid,$wall) {
4084         $o = '';
4085
4086         if(! feature_enabled($uid,'archives'))
4087                 return $o;
4088
4089         // For former Facebook folks that left because of "timeline"
4090
4091 /*      if($wall && intval(get_pconfig($uid,'system','no_wall_archive_widget')))
4092                 return $o;*/
4093
4094         $ret = posted_dates($uid,$wall);
4095         if(! count($ret))
4096                 return $o;
4097
4098         $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
4099                 '$title' => t('Archives'),
4100                 '$size' => ((count($ret) > 6) ? 6 : count($ret)),
4101                 '$url' => $url,
4102                 '$dates' => $ret
4103         ));
4104         return $o;
4105 }
4106
4107 function store_diaspora_retract_sig($item, $user, $baseurl) {
4108         // Note that we can't add a target_author_signature
4109         // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting
4110         // the comment, that means we're the home of the post, and Diaspora will only
4111         // check the parent_author_signature of retractions that it doesn't have to relay further
4112         //
4113         // I don't think this function gets called for an "unlike," but I'll check anyway
4114
4115         $enabled = intval(get_config('system','diaspora_enabled'));
4116         if(! $enabled) {
4117                 logger('drop_item: diaspora support disabled, not storing retraction signature', LOGGER_DEBUG);
4118                 return;
4119         }
4120
4121         logger('drop_item: storing diaspora retraction signature');
4122
4123         $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
4124
4125         if(local_user() == $item['uid']) {
4126
4127                 $handle = $user['nickname'] . '@' . substr($baseurl, strpos($baseurl,'://') + 3);
4128                 $authorsig = base64_encode(rsa_sign($signed_text,$user['prvkey'],'sha256'));
4129         }
4130         else {
4131                 $r = q("SELECT `nick`, `url` FROM `contact` WHERE `id` = '%d' LIMIT 1",
4132                         $item['contact-id'] // If this function gets called, drop_item() has already checked remote_user() == $item['contact-id']
4133                 );
4134                 if(count($r)) {
4135                         // The below handle only works for NETWORK_DFRN. I think that's ok, because this function
4136                         // only handles DFRN deletes
4137                         $handle_baseurl_start = strpos($r['url'],'://') + 3;
4138                         $handle_baseurl_length = strpos($r['url'],'/profile') - $handle_baseurl_start;
4139                         $handle = $r['nick'] . '@' . substr($r['url'], $handle_baseurl_start, $handle_baseurl_length);
4140                         $authorsig = '';
4141                 }
4142         }
4143
4144         if(isset($handle))
4145                 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
4146                         intval($item['id']),
4147                         dbesc($signed_text),
4148                         dbesc($authorsig),
4149                         dbesc($handle)
4150                 );
4151
4152         return;
4153 }