]> git.mxchange.org Git - friendica.git/blob - include/items.php
8360a42ec0c1832c73ce55bdea7c75619d769327
[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
8 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
9
10         // default permissions - anonymous user
11
12         if(! strlen($owner_nick))
13                 killme();
14
15         $public_feed = (($dfrn_id) ? false : true);
16         $starred = false;
17         $converse = false;
18
19         if($public_feed && $a->argc > 2) {
20                 for($x = 2; $x < $a->argc; $x++) {
21                         if($a->argv[$x] == 'converse')
22                                 $converse = true;
23                         if($a->argv[$x] == 'starred')
24                                 $starred = true;
25                 }
26         }
27
28
29         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid`  = '' AND `deny_gid`  = '' ";
30
31         $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`
32                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
33                 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
34                 dbesc($owner_nick)
35         );
36
37         if(! count($r))
38                 killme();
39
40         $owner = $r[0];
41         $owner_id = $owner['user_uid'];
42         $owner_nick = $owner['nickname'];
43
44         $birthday = feed_birthday($owner_id,$owner['timezone']);
45
46         if(! $public_feed) {
47
48                 $sql_extra = '';
49                 switch($direction) {
50                         case (-1):
51                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
52                                 $my_id = $dfrn_id;
53                                 break;
54                         case 0:
55                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
56                                 $my_id = '1:' . $dfrn_id;
57                                 break;
58                         case 1:
59                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
60                                 $my_id = '0:' . $dfrn_id;
61                                 break;
62                         default:
63                                 return false;
64                                 break; // NOTREACHED
65                 }
66
67                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
68                         intval($owner_id)
69                 );
70
71                 if(! count($r))
72                         killme();
73
74                 $contact = $r[0];
75                 $groups = init_groups_visitor($contact['id']);
76
77                 if(count($groups)) {
78                         for($x = 0; $x < count($groups); $x ++) 
79                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
80                         $gs = implode('|', $groups);
81                 }
82                 else
83                         $gs = '<<>>' ; // Impossible to match 
84
85                 $sql_extra = sprintf(" 
86                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
87                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
88                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
89                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
90                 ",
91                         intval($contact['id']),
92                         intval($contact['id']),
93                         dbesc($gs),
94                         dbesc($gs)
95                 );
96         }
97
98         if($public_feed)
99                 $sort = 'DESC';
100         else
101                 $sort = 'ASC';
102
103         if(! strlen($last_update))
104                 $last_update = 'now -30 days';
105
106         if($public_feed) {
107                 if(! $converse)
108                         $sql_extra .= " AND `contact`.`self` = 1 ";
109         }
110
111         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
112
113         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
114                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
115                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
116                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
117                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
118                 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
119                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
120                 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
121                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`parent` != 0 
122                 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
123                 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
124                 $sql_extra
125                 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
126                 intval($owner_id),
127                 dbesc($check_date),
128                 dbesc($check_date),
129                 dbesc($sort)
130         );
131
132         // Will check further below if this actually returned results.
133         // We will provide an empty feed if that is the case.
134
135         $items = $r;
136
137         $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
138
139         $atom = '';
140
141         $hubxml = feed_hublinks();
142
143         $salmon = feed_salmonlinks($owner_nick);
144
145         $atom .= replace_macros($feed_template, array(
146                 '$version'      => xmlify(FRIENDIKA_VERSION),
147                 '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
148                 '$feed_title'   => xmlify($owner['name']),
149                 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
150                 '$hub'          => $hubxml,
151                 '$salmon'       => $salmon,
152                 '$name'         => xmlify($owner['name']),
153                 '$profile_page' => xmlify($owner['url']),
154                 '$photo'        => xmlify($owner['photo']),
155                 '$thumb'        => xmlify($owner['thumb']),
156                 '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
157                 '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
158                 '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) , 
159                 '$birthday'     => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : '')
160         ));
161
162         call_hooks('atom_feed', $atom);
163
164         if(! count($items)) {
165
166                 call_hooks('atom_feed_end', $atom);
167
168                 $atom .= '</feed>' . "\r\n";
169                 return $atom;
170         }
171
172         foreach($items as $item) {
173
174                 // public feeds get html, our own nodes use bbcode
175
176                 if($public_feed) {
177                         $type = 'html';
178                         // catch any email that's in a public conversation and make sure it doesn't leak
179                         if($item['private'])
180                                 continue;
181                 }
182                 else {
183                         $type = 'text';
184                 }
185
186                 $atom .= atom_entry($item,$type,null,$owner,true);
187         }
188
189         call_hooks('atom_feed_end', $atom);
190
191         $atom .= '</feed>' . "\r\n";
192
193         return $atom;
194 }
195
196
197 function construct_verb($item) {
198         if($item['verb'])
199                 return $item['verb'];
200         return ACTIVITY_POST;
201 }
202
203 function construct_activity_object($item) {
204
205         if($item['object']) {
206                 $o = '<as:object>' . "\r\n";
207                 $r = parse_xml_string($item['object'],false);
208
209
210                 if(! $r)
211                         return '';
212                 if($r->type)
213                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
214                 if($r->id)
215                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
216                 if($r->title)
217                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
218                 if($r->link) {
219                         if(substr($r->link,0,1) === '<') {
220                                 // patch up some facebook "like" activity objects that got stored incorrectly
221                                 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
222                                 // we can probably remove this hack here and in the following function in a few months time.
223                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
224                                         $r->link = str_replace('&','&amp;', $r->link);
225                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
226                                 $o .= $r->link;
227                         }                                       
228                         else
229                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
230                 }
231                 if($r->content)
232                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
233                 $o .= '</as:object>' . "\r\n";
234                 return $o;
235         }
236
237         return '';
238
239
240 function construct_activity_target($item) {
241
242         if($item['target']) {
243                 $o = '<as:target>' . "\r\n";
244                 $r = parse_xml_string($item['target'],false);
245                 if(! $r)
246                         return '';
247                 if($r->type)
248                         $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
249                 if($r->id)
250                         $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
251                 if($r->title)
252                         $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
253                 if($r->link) {
254                         if(substr($r->link,0,1) === '<') {
255                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
256                                         $r->link = str_replace('&','&amp;', $r->link);
257                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
258                                 $o .= $r->link;
259                         }                                       
260                         else
261                                 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
262                 }
263                 if($r->content)
264                         $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
265                 $o .= '</as:target>' . "\r\n";
266                 return $o;
267         }
268
269         return '';
270
271
272
273
274
275 function get_atom_elements($feed,$item) {
276
277         require_once('library/HTMLPurifier.auto.php');
278         require_once('include/html2bbcode.php');
279
280         $best_photo = array();
281
282         $res = array();
283
284         $author = $item->get_author();
285         if($author) { 
286                 $res['author-name'] = unxmlify($author->get_name());
287                 $res['author-link'] = unxmlify($author->get_link());
288         }
289         else {
290                 $res['author-name'] = unxmlify($feed->get_title());
291                 $res['author-link'] = unxmlify($feed->get_permalink());
292         }
293         $res['uri'] = unxmlify($item->get_id());
294         $res['title'] = unxmlify($item->get_title());
295         $res['body'] = unxmlify($item->get_content());
296         $res['plink'] = unxmlify($item->get_link(0));
297
298         // look for a photo. We should check media size and find the best one,
299         // but for now let's just find any author photo
300
301         $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
302
303         if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
304                 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
305                 foreach($base as $link) {
306                         if(! $res['author-avatar']) {
307                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
308                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
309                         }
310                 }
311         }                       
312
313         $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
314
315         if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
316                 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
317                 if($base && count($base)) {
318                         foreach($base as $link) {
319                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
320                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
321                                 if(! $res['author-avatar']) {
322                                         if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
323                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
324                                 }
325                         }
326                 }
327         }
328
329         // No photo/profile-link on the item - look at the feed level
330
331         if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
332                 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
333                 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
334                         $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
335                         foreach($base as $link) {
336                                 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
337                                         $res['author-link'] = unxmlify($link['attribs']['']['href']);
338                                 if(! $res['author-avatar']) {
339                                         if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
340                                                 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
341                                 }
342                         }
343                 }                       
344
345                 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
346
347                 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
348                         $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
349
350                         if($base && count($base)) {
351                                 foreach($base as $link) {
352                                         if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
353                                                 $res['author-link'] = unxmlify($link['attribs']['']['href']);
354                                         if(! (x($res,'author-avatar'))) {
355                                                 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
356                                                         $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
357                                         }
358                                 }
359                         }
360                 }
361         }
362
363         $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
364         if($apps && $apps[0]['attribs']['']['source']) {
365                 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
366                 if($res['app'] === 'web')
367                         $res['app'] = 'OStatus';
368         }                  
369
370         // base64 encoded json structure representing Diaspora signature
371
372         $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
373         if($dsig) {
374                 $res['dsprsig'] = unxmlify($dsig[0]['data']);
375         }
376
377         $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
378         if($dguid)
379                 $res['guid'] = unxmlify($dguid[0]['data']);
380
381         $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
382         if($bm)
383                 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
384
385
386         /**
387          * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
388          */
389
390         $have_real_body = false;
391
392         $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
393         if($rawenv) {
394                 $have_real_body = true;
395                 $res['body'] = $rawenv[0]['data'];
396                 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
397                 // make sure nobody is trying to sneak some html tags by us
398                 $res['body'] = notags(base64url_decode($res['body']));
399         }
400
401         $maxlen = get_max_import_size();
402         if($maxlen && (strlen($res['body']) > $maxlen))
403                 $res['body'] = substr($res['body'],0, $maxlen);
404
405         // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
406         // the content type. Our own network only emits text normally, though it might have been converted to 
407         // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
408         // have to assume it is all html and needs to be purified.
409
410         // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
411         // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
412         // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
413         // html.
414
415         if((strpos($res['body'],'<') !== false) || (strpos($res['body'],'>') !== false)) {
416
417                 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
418                         '[youtube]$1[/youtube]', $res['body']);
419
420                 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
421                         '[youtube]$1[/youtube]', $res['body']);
422
423                 $res['body'] = oembed_html2bbcode($res['body']);
424
425                 $config = HTMLPurifier_Config::createDefault();
426                 $config->set('Cache.DefinitionImpl', null);
427
428                 // we shouldn't need a whitelist, because the bbcode converter
429                 // will strip out any unsupported tags.
430                 // $config->set('HTML.Allowed', 'p,b,a[href],i'); 
431
432                 $purifier = new HTMLPurifier($config);
433                 $res['body'] = $purifier->purify($res['body']);
434
435                 $res['body'] = html2bbcode($res['body']);
436         }
437
438         $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
439         if($allow && $allow[0]['data'] == 1)
440                 $res['last-child'] = 1;
441         else
442                 $res['last-child'] = 0;
443
444         $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
445         if($private && $private[0]['data'] == 1)
446                 $res['private'] = 1;
447         else
448                 $res['private'] = 0;
449
450         $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
451         if($extid && $extid[0]['data'])
452                 $res['extid'] = $extid[0]['data'];
453
454         $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
455         if($rawlocation)
456                 $res['location'] = unxmlify($rawlocation[0]['data']);
457
458
459         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
460         if($rawcreated)
461                 $res['created'] = unxmlify($rawcreated[0]['data']);
462
463
464         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
465         if($rawedited)
466                 $res['edited'] = unxmlify($rawedited[0]['data']);
467
468         if((x($res,'edited')) && (! (x($res,'created'))))
469                 $res['created'] = $res['edited']; 
470
471         if(! $res['created'])
472                 $res['created'] = $item->get_date('c');
473
474         if(! $res['edited'])
475                 $res['edited'] = $item->get_date('c');
476
477
478         // Disallow time travelling posts
479
480         $d1 = strtotime($res['created']);
481         $d2 = strtotime($res['edited']);
482         $d3 = strtotime('now');
483
484         if($d1 > $d3)
485                 $res['created'] = datetime_convert();
486         if($d2 > $d3)
487                 $res['edited'] = datetime_convert();
488
489         $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
490         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
491                 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
492         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
493                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
494         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
495                 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
496         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
497                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
498
499         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
500                 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
501
502                 foreach($base as $link) {
503                         if(! $res['owner-avatar']) {
504                                 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')                 
505                                         $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
506                         }
507                 }
508         }
509
510         $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
511         if($rawgeo)
512                 $res['coord'] = unxmlify($rawgeo[0]['data']);
513
514
515         $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
516
517         // select between supported verbs
518
519         if($rawverb) {
520                 $res['verb'] = unxmlify($rawverb[0]['data']);
521         }
522
523         // translate OStatus unfollow to activity streams if it happened to get selected
524                 
525         if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
526                 $res['verb'] = ACTIVITY_UNFOLLOW;
527
528         $cats = $item->get_categories();
529         if($cats) {
530                 $tag_arr = array();
531                 foreach($cats as $cat) {
532                         $term = $cat->get_term();
533                         if(! $term)
534                                 $term = $cat->get_label();
535                         $scheme = $cat->get_scheme();
536                         if($scheme && $term && stristr($scheme,'X-DFRN:'))
537                                 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
538                         elseif($term)
539                                 $tag_arr[] = notags(trim($term));
540                 }
541                 $res['tag'] =  implode(',', $tag_arr);
542         }
543
544         $attach = $item->get_enclosures();
545         if($attach) {
546                 $att_arr = array();
547                 foreach($attach as $att) {
548                         $len   = intval($att->get_length());
549                         $link  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
550                         $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
551                         $type  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
552                         if(strpos($type,';'))
553                                 $type = substr($type,0,strpos($type,';'));
554                         if((! $link) || (strpos($link,'http') !== 0))
555                                 continue;
556
557                         if(! $title)
558                                 $title = ' ';
559                         if(! $type)
560                                 $type = 'application/octet-stream';
561
562                         $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]'; 
563                 }
564                 $res['attach'] = implode(',', $att_arr);
565         }
566
567         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
568
569         if($rawobj) {
570                 $res['object'] = '<object>' . "\n";
571                 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
572                         $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
573                         $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
574                 }       
575                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
576                         $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
577                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
578                         $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
579                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
580                         $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
581                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
582                         $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
583                         if(! $body)
584                                 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
585                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
586                         $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
587                         if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
588
589                                 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
590                                         '[youtube]$1[/youtube]', $body);
591
592                 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
593                         '[youtube]$1[/youtube]', $res['body']);
594
595
596                                 $config = HTMLPurifier_Config::createDefault();
597                                 $config->set('Cache.DefinitionImpl', null);
598
599                                 $purifier = new HTMLPurifier($config);
600                                 $body = $purifier->purify($body);
601                                 $body = html2bbcode($body);
602                         }
603
604                         $res['object'] .= '<content>' . $body . '</content>' . "\n";
605                 }
606
607                 $res['object'] .= '</object>' . "\n";
608         }
609
610         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
611
612         if($rawobj) {
613                 $res['target'] = '<target>' . "\n";
614                 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
615                         $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
616                 }       
617                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
618                         $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
619
620                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
621                         $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
622                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
623                         $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
624                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
625                         $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
626                         if(! $body)
627                                 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
628                         // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
629                         $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
630                         if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
631
632                                 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
633                                         '[youtube]$1[/youtube]', $body);
634
635                 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
636                         '[youtube]$1[/youtube]', $res['body']);
637
638                                 $config = HTMLPurifier_Config::createDefault();
639                                 $config->set('Cache.DefinitionImpl', null);
640
641                                 $purifier = new HTMLPurifier($config);
642                                 $body = $purifier->purify($body);
643                                 $body = html2bbcode($body);
644                         }
645
646                         $res['target'] .= '<content>' . $body . '</content>' . "\n";
647                 }
648
649                 $res['target'] .= '</target>' . "\n";
650         }
651
652         $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
653
654         call_hooks('parse_atom', $arr);
655
656         return $res;
657 }
658
659 function encode_rel_links($links) {
660         $o = '';
661         if(! ((is_array($links)) && (count($links))))
662                 return $o;
663         foreach($links as $link) {
664                 $o .= '<link ';
665                 if($link['attribs']['']['rel'])
666                         $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
667                 if($link['attribs']['']['type'])
668                         $o .= 'type="' . $link['attribs']['']['type'] . '" ';
669                 if($link['attribs']['']['href'])
670                         $o .= 'href="' . $link['attribs']['']['href'] . '" ';
671                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
672                         $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
673                 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
674                         $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
675                 $o .= ' />' . "\n" ;
676         }
677         return xmlify($o);
678 }
679
680 function item_store($arr,$force_parent = false) {
681
682         // If a Diaspora signature structure was passed in, pull it out of the 
683         // item array and set it aside for later storage.
684
685         $dsprsig = null;
686         if(x($arr,'dsprsig')) {
687                 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
688                 unset($arr['dsprsig']);
689         }
690
691         if($arr['gravity'])
692                 $arr['gravity'] = intval($arr['gravity']);
693         elseif($arr['parent-uri'] == $arr['uri'])
694                 $arr['gravity'] = 0;
695         elseif(activity_match($arr['verb'],ACTIVITY_POST))
696                 $arr['gravity'] = 6;
697         else      
698                 $arr['gravity'] = 6;   // extensible catchall
699
700         if(! x($arr,'type'))
701                 $arr['type']      = 'remote';
702
703         // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
704
705         if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) 
706                 $arr['body'] = strip_tags($arr['body']);
707
708
709         $arr['wall']          = ((x($arr,'wall'))          ? intval($arr['wall'])                : 0);
710         $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : random_string());
711         $arr['extid']         = ((x($arr,'extid'))         ? notags(trim($arr['extid']))         : '');
712         $arr['author-name']   = ((x($arr,'author-name'))   ? notags(trim($arr['author-name']))   : '');
713         $arr['author-link']   = ((x($arr,'author-link'))   ? notags(trim($arr['author-link']))   : '');
714         $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
715         $arr['owner-name']    = ((x($arr,'owner-name'))    ? notags(trim($arr['owner-name']))    : '');
716         $arr['owner-link']    = ((x($arr,'owner-link'))    ? notags(trim($arr['owner-link']))    : '');
717         $arr['owner-avatar']  = ((x($arr,'owner-avatar'))  ? notags(trim($arr['owner-avatar']))  : '');
718         $arr['created']       = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
719         $arr['edited']        = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
720         $arr['commented']     = datetime_convert();
721         $arr['received']      = datetime_convert();
722         $arr['changed']       = datetime_convert();
723         $arr['title']         = ((x($arr,'title'))         ? notags(trim($arr['title']))         : '');
724         $arr['location']      = ((x($arr,'location'))      ? notags(trim($arr['location']))      : '');
725         $arr['coord']         = ((x($arr,'coord'))         ? notags(trim($arr['coord']))         : '');
726         $arr['last-child']    = ((x($arr,'last-child'))    ? intval($arr['last-child'])          : 0 );
727         $arr['visible']       = ((x($arr,'visible') !== false) ? intval($arr['visible'])         : 1 );
728         $arr['deleted']       = 0;
729         $arr['parent-uri']    = ((x($arr,'parent-uri'))    ? notags(trim($arr['parent-uri']))    : '');
730         $arr['verb']          = ((x($arr,'verb'))          ? notags(trim($arr['verb']))          : '');
731         $arr['object-type']   = ((x($arr,'object-type'))   ? notags(trim($arr['object-type']))   : '');
732         $arr['object']        = ((x($arr,'object'))        ? trim($arr['object'])                : '');
733         $arr['target-type']   = ((x($arr,'target-type'))   ? notags(trim($arr['target-type']))   : '');
734         $arr['target']        = ((x($arr,'target'))        ? trim($arr['target'])                : '');
735         $arr['plink']         = ((x($arr,'plink'))         ? notags(trim($arr['plink']))         : '');
736         $arr['allow_cid']     = ((x($arr,'allow_cid'))     ? trim($arr['allow_cid'])             : '');
737         $arr['allow_gid']     = ((x($arr,'allow_gid'))     ? trim($arr['allow_gid'])             : '');
738         $arr['deny_cid']      = ((x($arr,'deny_cid'))      ? trim($arr['deny_cid'])              : '');
739         $arr['deny_gid']      = ((x($arr,'deny_gid'))      ? trim($arr['deny_gid'])              : '');
740         $arr['private']       = ((x($arr,'private'))       ? intval($arr['private'])             : 0 );
741         $arr['bookmark']      = ((x($arr,'bookmark'))      ? intval($arr['bookmark'])            : 0 );
742         $arr['body']          = ((x($arr,'body'))          ? trim($arr['body'])                  : '');
743         $arr['tag']           = ((x($arr,'tag'))           ? notags(trim($arr['tag']))           : '');
744         $arr['attach']        = ((x($arr,'attach'))        ? notags(trim($arr['attach']))        : '');
745         $arr['app']           = ((x($arr,'app'))           ? notags(trim($arr['app']))           : '');
746         $arr['guid']          = ((x($arr,'guid'))          ? notags(trim($arr['guid']))          : get_guid());
747
748         if($arr['parent-uri'] === $arr['uri']) {
749                 $parent_id = 0;
750                 $allow_cid = $arr['allow_cid'];
751                 $allow_gid = $arr['allow_gid'];
752                 $deny_cid  = $arr['deny_cid'];
753                 $deny_gid  = $arr['deny_gid'];
754         }
755         else { 
756
757                 // find the parent and snarf the item id and ACL's
758                 // and anything else we need to inherit
759
760                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
761                         dbesc($arr['parent-uri']),
762                         intval($arr['uid'])
763                 );
764
765                 if(count($r)) {
766
767                         // is the new message multi-level threaded?
768                         // even though we don't support it now, preserve the info
769                         // and re-attach to the conversation parent.
770
771                         if($r[0]['uri'] != $r[0]['parent-uri']) {
772                                 $arr['thr-parent'] = $arr['parent-uri'];
773                                 $arr['parent-uri'] = $r[0]['parent-uri'];
774                                 $z = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
775                                         dbesc($r[0]['parent-uri']),
776                                         dbesc($r[0]['parent-uri']),
777                                         intval($arr['uid'])
778                                 );
779                                 if($z && count($z))
780                                         $r = $z;
781                         }
782
783                         $parent_id      = $r[0]['id'];
784                         $parent_deleted = $r[0]['deleted'];
785                         $allow_cid      = $r[0]['allow_cid'];
786                         $allow_gid      = $r[0]['allow_gid'];
787                         $deny_cid       = $r[0]['deny_cid'];
788                         $deny_gid       = $r[0]['deny_gid'];
789                         $arr['wall']    = $r[0]['wall'];
790                 }
791                 else {
792
793                         // Allow one to see reply tweets from status.net even when
794                         // we don't have or can't see the original post.
795
796                         if($force_parent) {
797                                 logger('item_store: $force_parent=true, reply converted to top-level post.');
798                                 $parent_id = 0;
799                                 $arr['thr-parent'] = $arr['parent-uri'];
800                                 $arr['parent-uri'] = $arr['uri'];
801                                 $arr['gravity'] = 0;
802                         }
803                         else {
804                                 logger('item_store: item parent was not found - ignoring item');
805                                 return 0;
806                         }
807                 }
808         }
809
810         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
811                 dbesc($arr['uri']),
812                 dbesc($arr['uid'])
813         );
814         if($r && count($r)) {
815                 logger('item-store: duplicate item ignored. ' . print_r($arr,true));
816                 return 0;
817         }
818
819         call_hooks('post_remote',$arr);
820
821         dbesc_array($arr);
822
823         logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
824
825         $r = dbq("INSERT INTO `item` (`" 
826                         . implode("`, `", array_keys($arr)) 
827                         . "`) VALUES ('" 
828                         . implode("', '", array_values($arr)) 
829                         . "')" );
830
831         // find the item we just created
832
833         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
834                 $arr['uri'],           // already dbesc'd
835                 intval($arr['uid'])
836         );
837         if(! count($r)) {
838                 // This is not good, but perhaps we encountered a rare race/cache condition, so back off and try again. 
839                 sleep(3);
840                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
841                         $arr['uri'],           // already dbesc'd
842                         intval($arr['uid'])
843                 );
844         }
845
846         if(count($r)) {
847                 $current_post = $r[0]['id'];
848                 logger('item_store: created item ' . $current_post);
849         }
850         else {
851                 logger('item_store: could not locate created item');
852                 return 0;
853         }
854
855         if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))      
856                 $parent_id = $current_post;
857
858         if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
859                 $private = 1;
860         else
861                 $private = $arr['private']; 
862
863         // Set parent id - and also make sure to inherit the parent's ACL's.
864
865         $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
866                 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
867                 intval($parent_id),
868                 dbesc($allow_cid),
869                 dbesc($allow_gid),
870                 dbesc($deny_cid),
871                 dbesc($deny_gid),
872                 intval($private),
873                 intval($parent_deleted),
874                 intval($current_post)
875         );
876
877         // update the commented timestamp on the parent
878
879         q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
880                 dbesc(datetime_convert()),
881                 dbesc(datetime_convert()),
882                 intval($parent_id)
883         );
884
885         if($dsprsig) {
886                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
887                         intval($current_post),
888                         dbesc($dsprsig->signed_text),
889                         dbesc($dsprsig->signature),
890                         dbesc($dsprsig->signer)
891                 );
892         }
893
894
895         /**
896          * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
897          */
898
899         if($arr['last-child']) {
900                 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
901                         dbesc($arr['uri']),
902                         intval($arr['uid']),
903                         intval($current_post)
904                 );
905         }
906
907         return $current_post;
908 }
909
910 function get_item_contact($item,$contacts) {
911         if(! count($contacts) || (! is_array($item)))
912                 return false;
913         foreach($contacts as $contact) {
914                 if($contact['id'] == $item['contact-id']) {
915                         return $contact;
916                         break; // NOTREACHED
917                 }
918         }
919         return false;
920 }
921
922
923 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
924
925         $a = get_app();
926
927         if((! strlen($contact['issued-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
928                 return 3;
929
930         $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
931
932         if($contact['duplex'] && $contact['dfrn-id'])
933                 $idtosend = '0:' . $orig_id;
934         if($contact['duplex'] && $contact['issued-id'])
935                 $idtosend = '1:' . $orig_id;            
936
937         $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
938
939         $rino_enable = get_config('system','rino_encrypt');
940
941         if(! $rino_enable)
942                 $rino = 0;
943
944         $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
945
946         logger('dfrn_deliver: ' . $url);
947
948         $xml = fetch_url($url);
949
950         $curl_stat = $a->get_curl_code();
951         if(! $curl_stat)
952                 return(-1); // timed out
953
954         logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
955
956         if(! $xml)
957                 return 3;
958
959         if(strpos($xml,'<?xml') === false) {
960                 logger('dfrn_deliver: no valid XML returned');
961                 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
962                 return 3;
963         }
964
965         $res = parse_xml_string($xml);
966
967         if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
968                 return (($res->status) ? $res->status : 3);
969
970         $postvars     = array();
971         $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
972         $challenge    = hex2bin((string) $res->challenge);
973         $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
974         $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
975
976         $final_dfrn_id = '';
977
978
979         if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
980                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
981                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
982         }
983         else {
984                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
985                 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
986         }
987
988         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
989
990         if(strpos($final_dfrn_id,':') == 1)
991                 $final_dfrn_id = substr($final_dfrn_id,2);
992
993         if($final_dfrn_id != $orig_id) {
994                 logger('dfrn_deliver: wrong dfrn_id.');
995                 // did not decode properly - cannot trust this site 
996                 return 3;
997         }
998
999         $postvars['dfrn_id']      = $idtosend;
1000         $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
1001         if($dissolve)
1002                 $postvars['dissolve'] = '1';
1003
1004
1005         if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1006                 $postvars['data'] = $atom;
1007                 $postvars['perm'] = 'rw';
1008         }
1009         else {
1010                 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1011                 $postvars['perm'] = 'r';
1012         }
1013
1014         if($rino && $rino_allowed && (! $dissolve)) {
1015                 $key = substr(random_string(),0,16);
1016                 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1017                 $postvars['data'] = $data;
1018                 logger('rino: sent key = ' . $key, LOGGER_DEBUG);       
1019
1020
1021                 if($dfrn_version >= 2.1) {      
1022                         if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1023                                 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1024                         }
1025                         else {
1026                                 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1027                         }
1028                 }
1029                 else {
1030                         if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1031                                 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1032                         }
1033                         else {
1034                                 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1035                         }
1036                 }
1037
1038                 logger('md5 rawkey ' . md5($postvars['key']));
1039
1040                 $postvars['key'] = bin2hex($postvars['key']);
1041         }
1042
1043         logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1044
1045         $xml = post_url($contact['notify'],$postvars);
1046
1047         logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1048
1049         $curl_stat = $a->get_curl_code();
1050         if((! $curl_stat) || (! strlen($xml)))
1051                 return(-1); // timed out
1052
1053         if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
1054                 return(-1);
1055
1056         if(strpos($xml,'<?xml') === false) {
1057                 logger('dfrn_deliver: phase 2: no valid XML returned');
1058                 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1059                 return 3;
1060         }
1061
1062         $res = parse_xml_string($xml);
1063
1064         return $res->status; 
1065 }
1066
1067
1068 /**
1069  *
1070  * consume_feed - process atom feed and update anything/everything we might need to update
1071  *
1072  * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1073  *
1074  * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1075  *             It is this person's stuff that is going to be updated.
1076  * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1077  *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST 
1078  *             have a contact record.
1079  * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or 
1080  *        might not) try and subscribe to it.
1081  * $datedir sorts in reverse order
1082  * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been 
1083  *      imported prior to its children being seen in the stream unless we are certain
1084  *      of how the feed is arranged/ordered.
1085  * With $pass = 1, we only pull parent items out of the stream.
1086  * With $pass = 2, we only pull children (comments/likes).
1087  *
1088  * So running this twice, first with pass 1 and then with pass 2 will do the right
1089  * thing regardless of feed ordering. This won't be adequate in a fully-threaded
1090  * model where comments can have sub-threads. That would require some massive sorting
1091  * to get all the feed items into a mostly linear ordering, and might still require
1092  * recursion.  
1093  */
1094
1095 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
1096
1097         require_once('library/simplepie/simplepie.inc');
1098
1099         if(! strlen($xml)) {
1100                 logger('consume_feed: empty input');
1101                 return;
1102         }
1103                 
1104         $feed = new SimplePie();
1105         $feed->set_raw_data($xml);
1106         if($datedir)
1107                 $feed->enable_order_by_date(true);
1108         else
1109                 $feed->enable_order_by_date(false);
1110         $feed->init();
1111
1112         if($feed->error())
1113                 logger('consume_feed: Error parsing XML: ' . $feed->error());
1114
1115         $permalink = $feed->get_permalink();
1116
1117         // Check at the feed level for updated contact name and/or photo
1118
1119         $name_updated  = '';
1120         $new_name = '';
1121         $photo_timestamp = '';
1122         $photo_url = '';
1123         $birthday = '';
1124
1125         $hubs = $feed->get_links('hub');
1126
1127         if(count($hubs))
1128                 $hub = implode(',', $hubs);
1129
1130         $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1131         if(! $rawtags)
1132                 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1133         if($rawtags) {
1134                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1135                 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1136                         $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1137                         $new_name = $elems['name'][0]['data'];
1138                 } 
1139                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1140                         $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1141                         $photo_url = $elems['link'][0]['attribs']['']['href'];
1142                 }
1143
1144                 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1145                         $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1146                 }
1147         }
1148
1149         if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1150                 logger('consume_feed: Updating photo for ' . $contact['name']);
1151                 require_once("Photo.php");
1152                 $photo_failure = false;
1153                 $have_photo = false;
1154
1155                 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1156                         intval($contact['id']),
1157                         intval($contact['uid'])
1158                 );
1159                 if(count($r)) {
1160                         $resource_id = $r[0]['resource-id'];
1161                         $have_photo = true;
1162                 }
1163                 else {
1164                         $resource_id = photo_new_resource();
1165                 }
1166                         
1167                 $img_str = fetch_url($photo_url,true);
1168                 $img = new Photo($img_str);
1169                 if($img->is_valid()) {
1170                         if($have_photo) {
1171                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1172                                         dbesc($resource_id),
1173                                         intval($contact['id']),
1174                                         intval($contact['uid'])
1175                                 );
1176                         }
1177                                 
1178                         $img->scaleImageSquare(175);
1179                                 
1180                         $hash = $resource_id;
1181                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1182                                 
1183                         $img->scaleImage(80);
1184                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1185
1186                         $img->scaleImage(48);
1187                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1188
1189                         $a = get_app();
1190
1191                         q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'  
1192                                 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1193                                 dbesc(datetime_convert()),
1194                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
1195                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
1196                                 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
1197                                 intval($contact['uid']),
1198                                 intval($contact['id'])
1199                         );
1200                 }
1201         }
1202
1203         if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1204                 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1205                         dbesc(notags(trim($new_name))),
1206                         dbesc(datetime_convert()),
1207                         intval($contact['uid']),
1208                         intval($contact['id'])
1209                 );
1210         }
1211
1212         if(strlen($birthday)) {
1213                 if(substr($birthday,0,4) != $contact['bdyear']) {
1214                         logger('consume_feed: updating birthday: ' . $birthday);
1215
1216                         /**
1217                          *
1218                          * Add new birthday event for this person
1219                          *
1220                          * $bdtext is just a readable placeholder in case the event is shared
1221                          * with others. We will replace it during presentation to our $importer
1222                          * to contain a sparkle link and perhaps a photo. 
1223                          *
1224                          */
1225                          
1226                         $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1227
1228
1229                         $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1230                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1231                                 intval($contact['uid']),
1232                                 intval($contact['id']),
1233                                 dbesc(datetime_convert()),
1234                                 dbesc(datetime_convert()),
1235                                 dbesc(datetime_convert('UTC','UTC', $birthday)),
1236                                 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1237                                 dbesc($bdtext),
1238                                 dbesc('birthday')
1239                         );
1240                         
1241
1242                         // update bdyear
1243
1244                         q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1245                                 dbesc(substr($birthday,0,4)),
1246                                 intval($contact['uid']),
1247                                 intval($contact['id'])
1248                         );
1249
1250                         // This function is called twice without reloading the contact
1251                         // Make sure we only create one event. This is why &$contact 
1252                         // is a reference var in this function
1253
1254                         $contact['bdyear'] = substr($birthday,0,4);
1255                 }
1256
1257         }
1258
1259
1260         // process any deleted entries
1261
1262         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1263         if(is_array($del_entries) && count($del_entries) && $pass != 2) {
1264                 foreach($del_entries as $dentry) {
1265                         $deleted = false;
1266                         if(isset($dentry['attribs']['']['ref'])) {
1267                                 $uri = $dentry['attribs']['']['ref'];
1268                                 $deleted = true;
1269                                 if(isset($dentry['attribs']['']['when'])) {
1270                                         $when = $dentry['attribs']['']['when'];
1271                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1272                                 }
1273                                 else
1274                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1275                         }
1276                         if($deleted && is_array($contact)) {
1277                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1278                                         dbesc($uri),
1279                                         intval($importer['uid']),
1280                                         intval($contact['id'])
1281                                 );
1282                                 if(count($r)) {
1283                                         $item = $r[0];
1284
1285                                         if(! $item['deleted'])
1286                                                 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1287
1288                                         if($item['uri'] == $item['parent-uri']) {
1289                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1290                                                         `body` = '', `title` = ''
1291                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
1292                                                         dbesc($when),
1293                                                         dbesc(datetime_convert()),
1294                                                         dbesc($item['uri']),
1295                                                         intval($importer['uid'])
1296                                                 );
1297                                         }
1298                                         else {
1299                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1300                                                         `body` = '', `title` = '' 
1301                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1302                                                         dbesc($when),
1303                                                         dbesc(datetime_convert()),
1304                                                         dbesc($uri),
1305                                                         intval($importer['uid'])
1306                                                 );
1307                                                 if($item['last-child']) {
1308                                                         // ensure that last-child is set in case the comment that had it just got wiped.
1309                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1310                                                                 dbesc(datetime_convert()),
1311                                                                 dbesc($item['parent-uri']),
1312                                                                 intval($item['uid'])
1313                                                         );
1314                                                         // who is the last child now? 
1315                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d 
1316                                                                 ORDER BY `created` DESC LIMIT 1",
1317                                                                         dbesc($item['parent-uri']),
1318                                                                         intval($importer['uid'])
1319                                                         );
1320                                                         if(count($r)) {
1321                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1322                                                                         intval($r[0]['id'])
1323                                                                 );
1324                                                         }
1325                                                 }       
1326                                         }
1327                                 }       
1328                         }
1329                 }
1330         }
1331
1332         // Now process the feed
1333
1334         if($feed->get_item_quantity()) {                
1335
1336                 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1337
1338         // in inverse date order
1339                 if ($datedir)
1340                         $items = array_reverse($feed->get_items());
1341                 else
1342                         $items = $feed->get_items();
1343
1344
1345                 foreach($items as $item) {
1346
1347                         $is_reply = false;              
1348                         $item_id = $item->get_id();
1349                         $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1350                         if(isset($rawthread[0]['attribs']['']['ref'])) {
1351                                 $is_reply = true;
1352                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1353                         }
1354
1355                         if(($is_reply) && is_array($contact) && $pass != 1) {
1356
1357                                 // Have we seen it? If not, import it.
1358         
1359                                 $item_id  = $item->get_id();
1360                                 $datarray = get_atom_elements($feed,$item);
1361
1362                                 if(! x($datarray,'author-name'))
1363                                         $datarray['author-name'] = $contact['name'];
1364                                 if(! x($datarray,'author-link'))
1365                                         $datarray['author-link'] = $contact['url'];
1366                                 if(! x($datarray,'author-avatar'))
1367                                         $datarray['author-avatar'] = $contact['thumb'];
1368
1369
1370                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1371                                         dbesc($item_id),
1372                                         intval($importer['uid'])
1373                                 );
1374
1375                                 // Update content if 'updated' changes
1376
1377                                 if(count($r)) {
1378                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
1379                                                 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1380                                                         dbesc($datarray['body']),
1381                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1382                                                         dbesc($item_id),
1383                                                         intval($importer['uid'])
1384                                                 );
1385                                         }
1386
1387                                         // update last-child if it changes
1388
1389                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1390                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1391                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1392                                                         dbesc(datetime_convert()),
1393                                                         dbesc($parent_uri),
1394                                                         intval($importer['uid'])
1395                                                 );
1396                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1397                                                         intval($allow[0]['data']),
1398                                                         dbesc(datetime_convert()),
1399                                                         dbesc($item_id),
1400                                                         intval($importer['uid'])
1401                                                 );
1402                                         }
1403                                         continue;
1404                                 }
1405
1406                                 $force_parent = false;
1407                                 if($contact['network'] === NETWORK_OSTATUS) {
1408                                         $force_parent = true;
1409                                         if(strlen($datarray['title']))
1410                                                 unset($datarray['title']);
1411                                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1412                                                 dbesc(datetime_convert()),
1413                                                 dbesc($parent_uri),
1414                                                 intval($importer['uid'])
1415                                         );
1416                                         $datarray['last-child'] = 1;
1417                                 }
1418
1419                                 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
1420                                         // one way feed - no remote comment ability
1421                                         $datarray['last-child'] = 0;
1422                                 }
1423                                 $datarray['parent-uri'] = $parent_uri;
1424                                 $datarray['uid'] = $importer['uid'];
1425                                 $datarray['contact-id'] = $contact['id'];
1426                                 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1427                                         $datarray['type'] = 'activity';
1428                                         $datarray['gravity'] = GRAVITY_LIKE;
1429                                 }
1430
1431                                 $r = item_store($datarray,$force_parent);
1432                                 continue;
1433                         }
1434
1435                         else {
1436
1437                                 // Head post of a conversation. Have we seen it? If not, import it.
1438
1439                                 $item_id  = $item->get_id();
1440
1441                                 $datarray = get_atom_elements($feed,$item);
1442
1443                                 if(is_array($contact)) {
1444                                         if(! x($datarray,'author-name'))
1445                                                 $datarray['author-name'] = $contact['name'];
1446                                         if(! x($datarray,'author-link'))
1447                                                 $datarray['author-link'] = $contact['url'];
1448                                         if(! x($datarray,'author-avatar'))
1449                                                 $datarray['author-avatar'] = $contact['thumb'];
1450                                 }
1451
1452                                 // special handling for events
1453
1454                                 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
1455                                         $ev = bbtoevent($datarray['body']);
1456                                         if(x($ev,'desc') && x($ev,'start')) {
1457                                                 $ev['uid'] = $importer['uid'];
1458                                                 $ev['uri'] = $item_id;
1459                                                 $ev['edited'] = $datarray['edited'];
1460                                                 $ev['private'] = $datarray['private'];
1461
1462                                                 if(is_array($contact))
1463                                                         $ev['cid'] = $contact['id'];
1464                                                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1465                                                         dbesc($item_id),
1466                                                         intval($importer['uid'])
1467                                                 );
1468                                                 if(count($r))
1469                                                         $ev['id'] = $r[0]['id'];
1470                                                 $xyz = event_store($ev);
1471                                                 continue;
1472                                         }
1473                                 }
1474
1475                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1476                                         dbesc($item_id),
1477                                         intval($importer['uid'])
1478                                 );
1479
1480                                 // Update content if 'updated' changes
1481
1482                                 if(count($r)) {
1483                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
1484                                                 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1485                                                         dbesc($datarray['body']),
1486                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1487                                                         dbesc($item_id),
1488                                                         intval($importer['uid'])
1489                                                 );
1490                                         }
1491
1492                                         // update last-child if it changes
1493
1494                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1495                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1496                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1497                                                         intval($allow[0]['data']),
1498                                                         dbesc(datetime_convert()),
1499                                                         dbesc($item_id),
1500                                                         intval($importer['uid'])
1501                                                 );
1502                                         }
1503                                         continue;
1504                                 }
1505
1506                                 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1507                                         logger('consume-feed: New follower');
1508                                         new_follower($importer,$contact,$datarray,$item);
1509                                         return;
1510                                 }
1511                                 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW))  {
1512                                         lose_follower($importer,$contact,$datarray,$item);
1513                                         return;
1514                                 }
1515
1516                                 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
1517                                         logger('consume-feed: New friend request');
1518                                         new_follower($importer,$contact,$datarray,$item,true);
1519                                         return;
1520                                 }
1521                                 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND))  {
1522                                         lose_sharer($importer,$contact,$datarray,$item);
1523                                         return;
1524                                 }
1525
1526
1527                                 if(! is_array($contact))
1528                                         return;
1529
1530                                 if($contact['network'] === NETWORK_OSTATUS || stristr($permalink,'twitter.com')) {
1531                                         if(strlen($datarray['title']))
1532                                                 unset($datarray['title']);
1533                                         $datarray['last-child'] = 1;
1534                                 }
1535
1536                                 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
1537                                         // one way feed - no remote comment ability
1538                                         $datarray['last-child'] = 0;
1539                                 }
1540
1541                                 // This is my contact on another system, but it's really me.
1542                                 // Turn this into a wall post.
1543
1544                                 if($contact['remote_self'])
1545                                         $datarray['wall'] = 1;
1546
1547                                 $datarray['parent-uri'] = $item_id;
1548                                 $datarray['uid'] = $importer['uid'];
1549                                 $datarray['contact-id'] = $contact['id'];
1550                                 $r = item_store($datarray);
1551                                 continue;
1552
1553                         }
1554                 }
1555         }
1556 }
1557
1558 function local_delivery($importer,$data) {
1559
1560         $a = get_app();
1561
1562         if($importer['readonly']) {
1563                 // We aren't receiving stuff from this person. But we will quietly ignore them
1564                 // rather than a blatant "go away" message.
1565                 logger('local_delivery: ignoring');
1566                 return 0;
1567                 //NOTREACHED
1568         }
1569
1570         // Consume notification feed. This may differ from consuming a public feed in several ways
1571         // - might contain email or friend suggestions
1572         // - might contain remote followup to our message
1573         //              - in which case we need to accept it and then notify other conversants
1574         // - we may need to send various email notifications
1575
1576         $feed = new SimplePie();
1577         $feed->set_raw_data($data);
1578         $feed->enable_order_by_date(false);
1579         $feed->init();
1580
1581         $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
1582         if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
1583                 $base = $reloc[0]['child'][NAMESPACE_DFRN];
1584                 $newloc = array();
1585                 $newloc['uid'] = $importer['importer_uid'];
1586                 $newloc['cid'] = $importer['id'];
1587                 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
1588                 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
1589                 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
1590                 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
1591                 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
1592                 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
1593                 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
1594                 $newloc['site-pubkey'] = notags(unxmlify($base['site-pubkey'][0]['data']));
1595                 $newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
1596                 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));
1597                 
1598                 // TODO
1599                 // merge with current record, current contents have priority
1600                 // update record, set url-updated
1601                 // update profile photos
1602                 // schedule a scan?
1603
1604         }
1605
1606         // handle friend suggestion notification
1607
1608         $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
1609         if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
1610                 $base = $sugg[0]['child'][NAMESPACE_DFRN];
1611                 $fsugg = array();
1612                 $fsugg['uid'] = $importer['importer_uid'];
1613                 $fsugg['cid'] = $importer['id'];
1614                 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
1615                 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
1616                 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
1617                 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
1618                 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
1619
1620                 // Does our member already have a friend matching this description?
1621
1622                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `url` = '%s' AND `uid` = %d LIMIT 1",
1623                         dbesc($fsugg['name']),
1624                         dbesc($fsugg['url']),
1625                         intval($fsugg['uid'])
1626                 );
1627                 if(count($r))
1628                         return 0;
1629
1630                 // Do we already have an fcontact record for this person?
1631
1632                 $fid = 0;
1633                 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
1634                         dbesc($fsugg['url']),
1635                         dbesc($fsugg['name']),
1636                         dbesc($fsugg['request'])
1637                 );
1638                 if(count($r)) {
1639                         $fid = $r[0]['id'];
1640                 }
1641                 if(! $fid)
1642                         $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
1643                         dbesc($fsugg['name']),
1644                         dbesc($fsugg['url']),
1645                         dbesc($fsugg['photo']),
1646                         dbesc($fsugg['request'])
1647                 );
1648                 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
1649                         dbesc($fsugg['url']),
1650                         dbesc($fsugg['name']),
1651                         dbesc($fsugg['request'])
1652                 );
1653                 if(count($r)) {
1654                         $fid = $r[0]['id'];
1655                 }
1656                 // database record did not get created. Quietly give up.
1657                 else
1658                         return 0;
1659
1660                 $hash = random_string();
1661  
1662                 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
1663                         VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
1664                         intval($fsugg['uid']),
1665                         intval($fid),
1666                         intval($fsugg['cid']),
1667                         dbesc($fsugg['body']),
1668                         dbesc($hash),
1669                         dbesc(datetime_convert()),
1670                         intval(0)
1671                 );
1672
1673                 // TODO - send email notify (which may require a new notification preference)
1674
1675                 return 0;
1676         }
1677
1678         $ismail = false;
1679
1680         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
1681         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
1682
1683                 logger('local_delivery: private message received');
1684
1685                 $ismail = true;
1686                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
1687
1688                 $msg = array();
1689                 $msg['uid'] = $importer['importer_uid'];
1690                 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
1691                 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
1692                 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
1693                 $msg['contact-id'] = $importer['id'];
1694                 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
1695                 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
1696                 $msg['seen'] = 0;
1697                 $msg['replied'] = 0;
1698                 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
1699                 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
1700                 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
1701                 
1702                 dbesc_array($msg);
1703
1704                 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
1705                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
1706
1707                 // send email notification if requested.
1708
1709                 require_once('bbcode.php');
1710                 if($importer['notify-flags'] & NOTIFY_MAIL) {
1711
1712                         push_lang($importer['language']);
1713
1714                         // name of the automated email sender
1715                         $msg['notificationfromname']    = t('Administrator');
1716                         // noreply address to send from
1717                         $msg['notificationfromemail']   = t('noreply') . '@' . $a->get_hostname();                              
1718
1719                         // text version
1720                         // process the message body to display properly in text mode
1721                         //              1) substitute a \n character for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
1722                         //              2) remove escape slashes
1723                         //              3) decode any bbcode from the message editor
1724                         //              4) decode any encoded html tags
1725                         //              5) remove html tags
1726                         $msg['textversion']
1727                                 = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",$msg['body']))),ENT_QUOTES,'UTF-8'));
1728                                 
1729                         // html version
1730                         // process the message body to display properly in text mode
1731                         //              1) substitute a <br /> tag for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
1732                         //              2) remove escape slashes
1733                         //              3) decode any bbcode from the message editor
1734                         //              4) decode any encoded html tags
1735                         $msg['htmlversion']     
1736                                 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$msg['body']))));
1737
1738                         // load the template for private message notifications
1739                         $tpl = get_intltext_template('mail_received_html_body_eml.tpl');
1740                         $email_html_body_tpl = replace_macros($tpl,array(
1741                                 '$username'     => $importer['username'],
1742                                 '$siteName'             => $a->config['sitename'],                      // name of this site
1743                                 '$siteurl'              => $a->get_baseurl(),                           // descriptive url of this site
1744                                 '$thumb'                => $importer['thumb'],                          // thumbnail url for sender icon
1745                                 '$email'                => $importer['email'],                          // email address to send to
1746                                 '$url'                  => $importer['url'],                            // full url for the site
1747                                 '$from'                 => $msg['from-name'],                           // name of the person sending the message
1748                                 '$title'                => stripslashes($msg['title']),                 // subject of the message
1749                                 '$htmlversion'  => $msg['htmlversion'],                                 // html version of the message
1750                                 '$mimeboundary' => $msg['mimeboundary'],                                // mime message divider
1751                                 '$hostname'             => $a->get_hostname()                           // name of this host
1752                         ));
1753                         
1754                         // load the template for private message notifications
1755                         $tpl = get_intltext_template('mail_received_text_body_eml.tpl');
1756                         $email_text_body_tpl = replace_macros($tpl,array(
1757                                 '$username'     => $importer['username'],
1758                                 '$siteName'             => $a->config['sitename'],                      // name of this site
1759                                 '$siteurl'              => $a->get_baseurl(),                           // descriptive url of this site
1760                                 '$thumb'                => $importer['thumb'],                          // thumbnail url for sender icon
1761                                 '$email'                => $importer['email'],                          // email address to send to
1762                                 '$url'                  => $importer['url'],                            // full url for the site
1763                                 '$from'                 => $msg['from-name'],                           // name of the person sending the message
1764                                 '$title'                => stripslashes($msg['title']),                 // subject of the message
1765                                 '$textversion'  => $msg['textversion'],                                 // text version of the message
1766                                 '$mimeboundary' => $msg['mimeboundary'],                                // mime message divider
1767                                 '$hostname'             => $a->get_hostname()                           // name of this host
1768                         ));
1769
1770                         // use the EmailNotification library to send the message
1771                         require_once("include/EmailNotification.php");
1772                         EmailNotification::sendTextHtmlEmail(
1773                                 $msg['notificationfromname'],
1774                                 $msg['notificationfromemail'],
1775                                 $msg['notificationfromemail'],
1776                                 $importer['email'],
1777                                 t('New mail received at ') . $a->config['sitename'],
1778                                 $email_html_body_tpl,
1779                                 $email_text_body_tpl
1780                         );
1781
1782                         pop_lang();
1783                 }
1784                 return 0;
1785                 // NOTREACHED
1786         }       
1787         
1788         logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
1789
1790         // process any deleted entries
1791
1792         $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1793         if(is_array($del_entries) && count($del_entries)) {
1794                 foreach($del_entries as $dentry) {
1795                         $deleted = false;
1796                         if(isset($dentry['attribs']['']['ref'])) {
1797                                 $uri = $dentry['attribs']['']['ref'];
1798                                 $deleted = true;
1799                                 if(isset($dentry['attribs']['']['when'])) {
1800                                         $when = $dentry['attribs']['']['when'];
1801                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1802                                 }
1803                                 else
1804                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1805                         }
1806                         if($deleted) {
1807
1808                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1809                                         dbesc($uri),
1810                                         intval($importer['importer_uid']),
1811                                         intval($importer['id'])
1812                                 );
1813
1814                                 if(count($r)) {
1815                                         $item = $r[0];
1816
1817                                         if(! $item['deleted'])
1818                                                 logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1819
1820                                         if($item['uri'] == $item['parent-uri']) {
1821                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
1822                                                         WHERE `parent-uri` = '%s' AND `uid` = %d",
1823                                                         dbesc($when),
1824                                                         dbesc(datetime_convert()),
1825                                                         dbesc($item['uri']),
1826                                                         intval($importer['importer_uid'])
1827                                                 );
1828                                         }
1829                                         else {
1830                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' 
1831                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1832                                                         dbesc($when),
1833                                                         dbesc(datetime_convert()),
1834                                                         dbesc($uri),
1835                                                         intval($importer['importer_uid'])
1836                                                 );
1837                                                 if($item['last-child']) {
1838                                                         // ensure that last-child is set in case the comment that had it just got wiped.
1839                                                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1840                                                                 dbesc(datetime_convert()),
1841                                                                 dbesc($item['parent-uri']),
1842                                                                 intval($item['uid'])
1843                                                         );
1844                                                         // who is the last child now? 
1845                                                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
1846                                                                 ORDER BY `created` DESC LIMIT 1",
1847                                                                         dbesc($item['parent-uri']),
1848                                                                         intval($importer['importer_uid'])
1849                                                         );
1850                                                         if(count($r)) {
1851                                                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1852                                                                         intval($r[0]['id'])
1853                                                                 );
1854                                                         }       
1855                                                 }
1856                                         }       
1857                                 }
1858                         }
1859                 }
1860         }
1861
1862
1863         foreach($feed->get_items() as $item) {
1864
1865                 $is_reply = false;              
1866                 $item_id = $item->get_id();
1867                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
1868                 if(isset($rawthread[0]['attribs']['']['ref'])) {
1869                         $is_reply = true;
1870                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
1871                 }
1872
1873                 if($is_reply) {
1874
1875                         // was the top-level post for this reply written by somebody on this site? 
1876                         // Specifically, the recipient? 
1877
1878                         $r = q("select `item`.`id`, `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
1879                                 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
1880                                 WHERE `contact`.`self` = 1 AND `item`.`wall` = 1 AND `item`.`uri` = '%s' AND `item`.`uid` = %d LIMIT 1",
1881                                 dbesc($parent_uri),
1882                                 intval($importer['importer_uid'])
1883                         );
1884                         if($r && count($r)) {   
1885
1886
1887                                 logger('local_delivery: received remote comment');
1888                                 $is_like = false;
1889                                 // remote reply to our post. Import and then notify everybody else.
1890                                 $datarray = get_atom_elements($feed,$item);
1891
1892                                 if(! link_compare($datarray['author-link'],$importer['url'])) {
1893                                         logger('local_delivery: received relay claiming to be from ' . $importer['url'] . ' however comment author url is ' . $datarray['author-link'] ); 
1894                                         // they won't know what to do so don't report an error. Just quietly die.
1895                                         return 0;
1896                                 }                                       
1897
1898                                 $datarray['type'] = 'remote-comment';
1899                                 $datarray['wall'] = 1;
1900                                 $datarray['parent-uri'] = $parent_uri;
1901                                 $datarray['uid'] = $importer['importer_uid'];
1902                                 $datarray['owner-name'] = $r[0]['name'];
1903                                 $datarray['owner-link'] = $r[0]['url'];
1904                                 $datarray['owner-avatar'] = $r[0]['thumb'];
1905                                 $datarray['contact-id'] = $importer['id'];
1906                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
1907                                         $is_like = true;
1908                                         $datarray['type'] = 'activity';
1909                                         $datarray['gravity'] = GRAVITY_LIKE;
1910                                         $datarray['last-child'] = 0;
1911                                 }
1912                                 $posted_id = item_store($datarray);
1913                                 $parent = 0;
1914
1915                                 if($posted_id) {
1916                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1917                                                 intval($posted_id),
1918                                                 intval($importer['importer_uid'])
1919                                         );
1920                                         if(count($r))
1921                                                 $parent = $r[0]['parent'];
1922                         
1923                                         if(! $is_like) {
1924                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
1925                                                         dbesc(datetime_convert()),
1926                                                         intval($importer['importer_uid']),
1927                                                         intval($r[0]['parent'])
1928                                                 );
1929
1930                                                 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1931                                                         dbesc(datetime_convert()),
1932                                                         intval($importer['importer_uid']),
1933                                                         intval($posted_id)
1934                                                 );
1935                                         }
1936
1937                                         if($posted_id && $parent) {
1938                                 
1939                                                 proc_run('php',"include/notifier.php","comment-import","$posted_id");
1940                                         
1941                                                 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
1942                                                         push_lang($importer['language']);
1943                                                         require_once('bbcode.php');
1944                                                         $from = stripslashes($datarray['author-name']);
1945
1946                                                         // name of the automated email sender
1947                                                         $msg['notificationfromname']    = stripslashes($datarray['author-name']);;
1948                                                         // noreply address to send from
1949                                                         $msg['notificationfromemail']   = t('noreply') . '@' . $a->get_hostname();                              
1950
1951                                                         // text version
1952                                                         // process the message body to display properly in text mode
1953                                                         $msg['textversion']
1954                                                                 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
1955                                 
1956                                                         // html version
1957                                                         // process the message body to display properly in text mode
1958                                                         $msg['htmlversion']     
1959                                                                 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
1960
1961                                                         $imgtouse = ((link_compare($datarray['author-link'],$importer['url'])) ? $importer['thumb'] : $datarray['author-avatar']);
1962
1963                                                         // load the template for private message notifications
1964                                                         $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
1965                                                         $email_html_body_tpl = replace_macros($tpl,array(
1966                                                                 '$username'     => $importer['username'],
1967                                                                 '$sitename'             => $a->config['sitename'],                      // name of this site
1968                                                                 '$siteurl'              => $a->get_baseurl(),                           // descriptive url of this site
1969                                                                 '$thumb'                => $imgtouse,                                           // thumbnail url for sender icon
1970                                                                 '$email'                => $importer['email'],                          // email address to send to
1971                                                                 '$url'                  => $datarray['author-link'],            // full url for the site
1972                                                                 '$from'                 => $from,                                                       // name of the person sending the message
1973                                                                 '$body'                 => $msg['htmlversion'],                         // html version of the message
1974                                                                 '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
1975                                                         ));
1976                         
1977                                                         // load the template for private message notifications
1978                                                         $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
1979                                                         $email_text_body_tpl = replace_macros($tpl,array(
1980                                                                 '$username'     => $importer['username'],
1981                                                                 '$sitename'             => $a->config['sitename'],                      // name of this site
1982                                                                 '$siteurl'              => $a->get_baseurl(),                           // descriptive url of this site
1983                                                                 '$thumb'                => $imgtouse,                                           // thumbnail url for sender icon
1984                                                                 '$email'                => $importer['email'],                          // email address to send to
1985                                                                 '$url'                  => $datarray['author-link'],            // full url for the site
1986                                                                 '$from'                 => $from,                                                       // name of the person sending the message
1987                                                                 '$body'                 => $msg['textversion'],                         // text version of the message
1988                                                                 '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
1989                                                         ));
1990
1991                                                         // use the EmailNotification library to send the message
1992                                                         require_once("include/EmailNotification.php");
1993                                                         EmailNotification::sendTextHtmlEmail(
1994                                                                 $msg['notificationfromname'],
1995                                                                 t("Administrator") . '@' . $a->get_hostname(),
1996                                                                 t("noreply") . '@' . $a->get_hostname(),
1997                                                                 $importer['email'],
1998                                                                 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
1999                                                                 $email_html_body_tpl,
2000                                                                 $email_text_body_tpl
2001                                                         );
2002                                                         pop_lang();
2003                                                 }
2004                                         }
2005                                         return 0;
2006                                         // NOTREACHED
2007                                 }
2008                         }
2009                         else {
2010
2011                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
2012
2013                                 $item_id  = $item->get_id();
2014                                 $datarray = get_atom_elements($feed,$item);
2015
2016                                 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2017                                         dbesc($item_id),
2018                                         intval($importer['importer_uid'])
2019                                 );
2020
2021                                 // Update content if 'updated' changes
2022
2023                                 if(count($r)) {
2024                                         if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
2025                                                 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2026                                                         dbesc($datarray['body']),
2027                                                         dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2028                                                         dbesc($item_id),
2029                                                         intval($importer['importer_uid'])
2030                                                 );
2031                                         }
2032
2033                                         // update last-child if it changes
2034
2035                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2036                                         if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
2037                                                 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
2038                                                         dbesc(datetime_convert()),
2039                                                         dbesc($parent_uri),
2040                                                         intval($importer['importer_uid'])
2041                                                 );
2042                                                 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2043                                                         intval($allow[0]['data']),
2044                                                         dbesc(datetime_convert()),
2045                                                         dbesc($item_id),
2046                                                         intval($importer['importer_uid'])
2047                                                 );
2048                                         }
2049                                         continue;
2050                                 }
2051
2052                                 $datarray['parent-uri'] = $parent_uri;
2053                                 $datarray['uid'] = $importer['importer_uid'];
2054                                 $datarray['contact-id'] = $importer['id'];
2055                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
2056                                         $datarray['type'] = 'activity';
2057                                         $datarray['gravity'] = GRAVITY_LIKE;
2058                                 }
2059                                 $posted_id = item_store($datarray);
2060
2061                                 // find out if our user is involved in this conversation and wants to be notified.
2062                         
2063                                 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
2064
2065                                         $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
2066                                                 dbesc($parent_uri),
2067                                                 intval($importer['importer_uid'])
2068                                         );
2069                                         if(count($myconv)) {
2070                                                 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
2071                                                 foreach($myconv as $conv) {
2072                                                         if(! link_compare($conv['author-link'],$importer_url))
2073                                                                 continue;
2074
2075                                                         push_lang($importer['language']);
2076                                                         require_once('bbcode.php');
2077                                                         $from = stripslashes($datarray['author-name']);
2078                                                         
2079                                                         // name of the automated email sender
2080                                                         $msg['notificationfromname']    = stripslashes($datarray['author-name']);;
2081                                                         // noreply address to send from
2082                                                         $msg['notificationfromemail']   = t('noreply') . '@' . $a->get_hostname();                              
2083
2084                                                         // text version
2085                                                         // process the message body to display properly in text mode
2086                                                         $msg['textversion']
2087                                                                 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
2088                                 
2089                                                         // html version
2090                                                         // process the message body to display properly in text mode
2091                                                         $msg['htmlversion']     
2092                                                                 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
2093
2094                                                         $imgtouse = ((link_compare($datarray['author-link'],$importer['url'])) ? $importer['thumb'] : $datarray['author-avatar']);
2095
2096
2097                                                         // load the template for private message notifications
2098                                                         $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
2099                                                         $email_html_body_tpl = replace_macros($tpl,array(
2100                                                                 '$username'     => $importer['username'],
2101                                                                 '$sitename'             => $a->config['sitename'],                              // name of this site
2102                                                                 '$siteurl'              => $a->get_baseurl(),                                   // descriptive url of this site
2103                                                                 '$thumb'                => $imgtouse,                                                   // thumbnail url for sender icon
2104                                                                 '$url'                  => $datarray['author-link'],                    // full url for the site
2105                                                                 '$from'                 => $from,                                                               // name of the person sending the message
2106                                                                 '$body'                 => $msg['htmlversion'],                                 // html version of the message
2107                                                                 '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
2108                                                         ));
2109                         
2110                                                         // load the template for private message notifications
2111                                                         $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
2112                                                         $email_text_body_tpl = replace_macros($tpl,array(
2113                                                                 '$username'     => $importer['username'],
2114                                                                 '$sitename'             => $a->config['sitename'],                              // name of this site
2115                                                                 '$siteurl'              => $a->get_baseurl(),                                   // descriptive url of this site
2116                                                                 '$thumb'                => $imgtouse,                                                   // thumbnail url for sender icon
2117                                                                 '$url'                  => $datarray['author-link'],                    // full url for the site
2118                                                                 '$from'                 => $from,                                                               // name of the person sending the message
2119                                                                 '$body'                 => $msg['textversion'],                                 // text version of the message
2120                                                                 '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
2121                                                         ));
2122
2123                                                         // use the EmailNotification library to send the message
2124                                                         require_once("include/EmailNotification.php");
2125                                                         EmailNotification::sendTextHtmlEmail(
2126                                                                 $msg['notificationfromname'],
2127                                                                 t("Administrator@") . $a->get_hostname(),
2128                                                                 t("noreply") . '@' . $a->get_hostname(),
2129                                                                 $importer['email'],
2130                                                                 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
2131                                                                 $email_html_body_tpl,
2132                                                                 $email_text_body_tpl
2133                                                         );
2134                                                         pop_lang();
2135                                                         break;
2136                                                 }
2137                                         }
2138                                 }
2139                                 continue;
2140                         }
2141                 }
2142
2143                 else {
2144
2145                         // Head post of a conversation. Have we seen it? If not, import it.
2146
2147
2148                         $item_id  = $item->get_id();
2149                         $datarray = get_atom_elements($feed,$item);
2150
2151                         if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2152                                 $ev = bbtoevent($datarray['body']);
2153                                 if(x($ev,'desc') && x($ev,'start')) {
2154                                         $ev['cid'] = $importer['id'];
2155                                         $ev['uid'] = $importer['uid'];
2156                                         $ev['uri'] = $item_id;
2157                                         $ev['edited'] = $datarray['edited'];
2158                                         $ev['private'] = $datarray['private'];
2159
2160                                         $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2161                                                 dbesc($item_id),
2162                                                 intval($importer['uid'])
2163                                         );
2164                                         if(count($r))
2165                                                 $ev['id'] = $r[0]['id'];
2166                                         $xyz = event_store($ev);
2167                                         continue;
2168                                 }
2169                         }
2170
2171                         $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2172                                 dbesc($item_id),
2173                                 intval($importer['importer_uid'])
2174                         );
2175
2176                         // Update content if 'updated' changes
2177
2178                         if(count($r)) {
2179                                 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
2180                                         $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2181                                                 dbesc($datarray['body']),
2182                                                 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2183                                                 dbesc($item_id),
2184                                                 intval($importer['importer_uid'])
2185                                         );
2186                                 }
2187
2188                                 // update last-child if it changes
2189
2190                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2191                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2192                                         $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2193                                                 intval($allow[0]['data']),
2194                                                 dbesc(datetime_convert()),
2195                                                 dbesc($item_id),
2196                                                 intval($importer['importer_uid'])
2197                                         );
2198                                 }
2199                                 continue;
2200                         }
2201
2202                         // This is my contact on another system, but it's really me.
2203                         // Turn this into a wall post.
2204
2205                         if($contact['remote_self'])
2206                                 $datarray['wall'] = 1;
2207
2208                         $datarray['parent-uri'] = $item_id;
2209                         $datarray['uid'] = $importer['importer_uid'];
2210                         $datarray['contact-id'] = $importer['id'];
2211                         $r = item_store($datarray);
2212                         continue;
2213                 }
2214         }
2215
2216         return 0;
2217         // NOTREACHED
2218
2219 }
2220
2221
2222 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
2223         $url = notags(trim($datarray['author-link']));
2224         $name = notags(trim($datarray['author-name']));
2225         $photo = notags(trim($datarray['author-avatar']));
2226
2227         $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
2228         if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
2229                 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
2230
2231         if(is_array($contact)) {
2232                 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
2233                         || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
2234                         $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
2235                                 intval(CONTACT_IS_FRIEND),
2236                                 intval($contact['id']),
2237                                 intval($importer['uid'])
2238                         );
2239                 }
2240                 // send email notification to owner?
2241         }
2242         else {
2243         
2244                 // create contact record
2245
2246                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`, 
2247                         `blocked`, `readonly`, `pending`, `writable` )
2248                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
2249                         intval($importer['uid']),
2250                         dbesc(datetime_convert()),
2251                         dbesc($url),
2252                         dbesc($name),
2253                         dbesc($nick),
2254                         dbesc($photo),
2255                         dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
2256                         intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
2257                 );
2258                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
2259                                 intval($importer['uid']),
2260                                 dbesc($url)
2261                 );
2262                 if(count($r))
2263                                 $contact_record = $r[0];
2264
2265                 // create notification  
2266                 $hash = random_string();
2267
2268                 if(is_array($contact_record)) {
2269                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
2270                                 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
2271                                 intval($importer['uid']),
2272                                 intval($contact_record['id']),
2273                                 dbesc($hash),
2274                                 dbesc(datetime_convert())
2275                         );
2276                 }
2277                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
2278                         intval($importer['uid'])
2279                 );
2280                 $a = get_app();
2281                 if(count($r)) {
2282                         if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
2283                                 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
2284                                 $email = replace_macros($email_tpl, array(
2285                                         '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
2286                                         '$url' => $url,
2287                                         '$myname' => $r[0]['username'],
2288                                         '$siteurl' => $a->get_baseurl(),
2289                                         '$sitename' => $a->config['sitename']
2290                                 ));
2291                                 $res = mail($r[0]['email'], 
2292                                         (($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],
2293                                         $email,
2294                                         'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
2295                                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
2296                                         . 'Content-transfer-encoding: 8bit' );
2297                         
2298                         }
2299                 }
2300         }
2301 }
2302
2303 function lose_follower($importer,$contact,$datarray,$item) {
2304
2305         if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
2306                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
2307                         intval(CONTACT_IS_SHARING),
2308                         intval($contact['id'])
2309                 );
2310         }
2311         else {
2312                 contact_remove($contact['id']);
2313         }
2314 }
2315
2316 function lose_sharer($importer,$contact,$datarray,$item) {
2317
2318         if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
2319                 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
2320                         intval(CONTACT_IS_FOLLOWER),
2321                         intval($contact['id'])
2322                 );
2323         }
2324         else {
2325                 contact_remove($contact['id']);
2326         }
2327 }
2328
2329
2330 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
2331
2332         if(is_array($importer)) {
2333                 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
2334                         intval($importer['uid'])
2335                 );
2336         }
2337
2338         // Diaspora has different message-ids in feeds than they do 
2339         // through the direct Diaspora protocol. If we try and use
2340         // the feed, we'll get duplicates. So don't.
2341
2342         if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
2343                 return;
2344
2345         $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
2346
2347         // Use a single verify token, even if multiple hubs
2348
2349         $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
2350
2351         $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
2352
2353         logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: '  . $push_url . ' with verifier ' . $verify_token);
2354
2355         if(! strlen($contact['hub-verify'])) {
2356                 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
2357                         dbesc($verify_token),
2358                         intval($contact['id'])
2359                 );
2360         }
2361
2362         post_url($url,$params);                 
2363         return;
2364
2365 }
2366
2367
2368 function atom_author($tag,$name,$uri,$h,$w,$photo) {
2369         $o = '';
2370         if(! $tag)
2371                 return $o;
2372         $name = xmlify($name);
2373         $uri = xmlify($uri);
2374         $h = intval($h);
2375         $w = intval($w);
2376         $photo = xmlify($photo);
2377
2378
2379         $o .= "<$tag>\r\n";
2380         $o .= "<name>$name</name>\r\n";
2381         $o .= "<uri>$uri</uri>\r\n";
2382         $o .= '<link rel="photo"  type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
2383         $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
2384
2385         call_hooks('atom_author', $o);
2386
2387         $o .= "</$tag>\r\n";
2388         return $o;
2389 }
2390
2391 function atom_entry($item,$type,$author,$owner,$comment = false) {
2392
2393         $a = get_app();
2394
2395         if($item['deleted'])
2396                 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
2397
2398
2399         if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
2400                 $body = fix_private_photos($item['body'],$owner['uid']);
2401         else
2402                 $body = $item['body'];
2403
2404
2405         $o = "\r\n\r\n<entry>\r\n";
2406
2407         if(is_array($author))
2408                 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
2409         else
2410                 $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']));
2411         if(strlen($item['owner-name']))
2412                 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
2413
2414         if($item['parent'] != $item['id'])
2415                 $o .= '<thr:in-reply-to ref="' . xmlify($item['parent-uri']) . '" type="text/html" href="' .  xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
2416
2417         $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
2418         $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
2419         $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
2420         $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
2421         $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
2422         $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
2423         $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
2424         if($comment)
2425                 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
2426
2427         if($item['location']) {
2428                 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
2429                 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
2430         }
2431
2432         if($item['coord'])
2433                 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
2434
2435         if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
2436                 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
2437
2438         if($item['extid'])
2439                 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
2440         if($item['bookmark'])
2441                 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
2442
2443         if($item['app'])
2444                 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
2445
2446         if($item['guid'])
2447                 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
2448
2449         if($item['signed_text']) {
2450                 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
2451                 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
2452         }
2453
2454         $verb = construct_verb($item);
2455         $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
2456         $actobj = construct_activity_object($item);
2457         if(strlen($actobj))
2458                 $o .= $actobj;
2459         $actarg = construct_activity_target($item);
2460         if(strlen($actarg))
2461                 $o .= $actarg;
2462
2463         $tags = item_getfeedtags($item);
2464         if(count($tags)) {
2465                 foreach($tags as $t) {
2466                         $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
2467                 }
2468         }
2469
2470         $o .= item_getfeedattach($item);
2471
2472         $mentioned = get_mentions($item);
2473         if($mentioned)
2474                 $o .= $mentioned;
2475         
2476         call_hooks('atom_entry', $o);
2477
2478         $o .= '</entry>' . "\r\n";
2479         
2480         return $o;
2481 }
2482
2483 function fix_private_photos($s,$uid) {
2484         $a = get_app();
2485         logger('fix_private_photos');
2486
2487         if(preg_match("/\[img\](.*?)\[\/img\]/is",$s,$matches)) {
2488                 $image = $matches[1];
2489                 logger('fix_private_photos: found photo ' . $image);
2490                 if(stristr($image ,$a->get_baseurl() . '/photo/')) {
2491                         $i = basename($image);
2492                         $i = str_replace('.jpg','',$i);
2493                         $x = strpos($i,'-');
2494                         if($x) {
2495                                 $res = substr($i,$x+1);
2496                                 $i = substr($i,0,$x);
2497                                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
2498                                         dbesc($i),
2499                                         intval($res),
2500                                         intval($uid)
2501                                 );
2502                                 if(count($r)) {
2503                                         logger('replacing photo');
2504                                         $s = str_replace($image, 'data:image/jpg;base64,' . base64_encode($r[0]['data']), $s);
2505                                 }
2506                         }
2507                         logger('fix_private_photos: replaced: ' . $s, LOGGER_DATA);
2508                 }       
2509         }
2510         return($s);
2511 }
2512
2513
2514
2515 function item_getfeedtags($item) {
2516         $ret = array();
2517         $matches = false;
2518         $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
2519         if($cnt) {
2520                 for($x = 0; $x < count($matches); $x ++) {
2521                         if($matches[1][$x])
2522                                 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
2523                 }
2524         }
2525         $matches = false; 
2526         $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
2527         if($cnt) {
2528                 for($x = 0; $x < count($matches); $x ++) {
2529                         if($matches[1][$x])
2530                                 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
2531                 }
2532         } 
2533         return $ret;
2534 }
2535
2536 function item_getfeedattach($item) {
2537         $ret = '';
2538         $arr = explode(',',$item['attach']);
2539         if(count($arr)) {
2540                 foreach($arr as $r) {
2541                         $matches = false;
2542                         $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
2543                         if($cnt) {
2544                                 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
2545                                 if(intval($matches[2]))
2546                                         $ret .= 'length="' . intval($matches[2]) . '" ';
2547                                 if($matches[4] !== ' ')
2548                                         $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
2549                                 $ret .= ' />' . "\r\n";
2550                         }
2551                 }
2552         }
2553         return $ret;
2554 }
2555
2556
2557         
2558 function item_expire($uid,$days) {
2559
2560         if((! $uid) || (! $days))
2561                 return;
2562
2563         $r = q("SELECT * FROM `item` 
2564                 WHERE `uid` = %d 
2565                 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY 
2566                 AND `id` = `parent` 
2567                 AND `deleted` = 0",
2568                 intval($uid),
2569                 intval($days)
2570         );
2571
2572         if(! count($r))
2573                 return;
2574  
2575         logger('expire: # items=' . count($r) );
2576
2577         foreach($r as $item) {
2578
2579                 // Only expire posts, not photos and photo comments
2580
2581                 if(strlen($item['resource-id']))
2582                         continue;
2583
2584                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
2585                         dbesc(datetime_convert()),
2586                         dbesc(datetime_convert()),
2587                         intval($item['id'])
2588                 );
2589
2590                 // kill the kids
2591
2592                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2593                         dbesc(datetime_convert()),
2594                         dbesc(datetime_convert()),
2595                         dbesc($item['parent-uri']),
2596                         intval($item['uid'])
2597                 );
2598
2599         }
2600
2601         proc_run('php',"include/notifier.php","expire","$uid");
2602
2603 }
2604
2605
2606 function drop_items($items) {
2607         $uid = 0;
2608
2609         if(count($items)) {
2610                 foreach($items as $item) {
2611                         $owner = drop_item($item,false);
2612                         if($owner && ! $uid)
2613                                 $uid = $owner;
2614                 }
2615         }
2616
2617         // multiple threads may have been deleted, send an expire notification
2618
2619         if($uid)
2620                 proc_run('php',"include/notifier.php","expire","$uid");
2621 }
2622
2623
2624 function drop_item($id,$interactive = true) {
2625
2626         $a = get_app();
2627
2628         // locate item to be deleted
2629
2630         $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
2631                 intval($id)
2632         );
2633
2634         if(! count($r)) {
2635                 if(! $interactive)
2636                         return 0;
2637                 notice( t('Item not found.') . EOL);
2638                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
2639         }
2640
2641         $item = $r[0];
2642
2643         $owner = $item['uid'];
2644
2645         // check if logged in user is either the author or owner of this item
2646
2647         if((local_user() == $item['uid']) || (remote_user() == $item['contact-id'])) {
2648
2649                 // delete the item
2650
2651                 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
2652                         dbesc(datetime_convert()),
2653                         dbesc(datetime_convert()),
2654                         intval($item['id'])
2655                 );
2656
2657                 // If item is a link to a photo resource, nuke all the associated photos 
2658                 // (visitors will not have photo resources)
2659                 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
2660                 // generate a resource-id and therefore aren't intimately linked to the item. 
2661
2662                 if(strlen($item['resource-id'])) {
2663                         q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
2664                                 dbesc($item['resource-id']),
2665                                 intval($item['uid'])
2666                         );
2667                         // ignore the result
2668                 }
2669
2670                 // If item is a link to an event, nuke the event record.
2671
2672                 if(intval($item['event-id'])) {
2673                         q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2674                                 intval($item['event-id']),
2675                                 intval($item['uid'])
2676                         );
2677                         // ignore the result
2678                 }
2679
2680
2681                 // If it's the parent of a comment thread, kill all the kids
2682
2683                 if($item['uri'] == $item['parent-uri']) {
2684                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
2685                                 WHERE `parent-uri` = '%s' AND `uid` = %d ",
2686                                 dbesc(datetime_convert()),
2687                                 dbesc(datetime_convert()),
2688                                 dbesc($item['parent-uri']),
2689                                 intval($item['uid'])
2690                         );
2691                         // ignore the result
2692                 }
2693                 else {
2694                         // ensure that last-child is set in case the comment that had it just got wiped.
2695                         q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2696                                 dbesc(datetime_convert()),
2697                                 dbesc($item['parent-uri']),
2698                                 intval($item['uid'])
2699                         );
2700                         // who is the last child now? 
2701                         $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",
2702                                 dbesc($item['parent-uri']),
2703                                 intval($item['uid'])
2704                         );
2705                         if(count($r)) {
2706                                 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
2707                                         intval($r[0]['id'])
2708                                 );
2709                         }       
2710                 }
2711                 $drop_id = intval($item['id']);
2712                         
2713                 // send the notification upstream/downstream as the case may be
2714
2715                 if(! $interactive)
2716                         return $owner;
2717
2718                 proc_run('php',"include/notifier.php","drop","$drop_id");
2719                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
2720                 //NOTREACHED
2721         }
2722         else {
2723                 if(! $interactive)
2724                         return 0;
2725                 notice( t('Permission denied.') . EOL);
2726                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
2727                 //NOTREACHED
2728         }
2729         
2730 }