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