]> git.mxchange.org Git - friendica.git/blob - include/Contact.php
Merge remote-tracking branch 'upstream/develop' into 1606-contact-id
[friendica.git] / include / Contact.php
1 <?php
2
3
4 // Included here for completeness, but this is a very dangerous operation.
5 // It is the caller's responsibility to confirm the requestor's intent and
6 // authorisation to do this.
7
8 function user_remove($uid) {
9         if(! $uid)
10                 return;
11         $a = get_app();
12         logger('Removing user: ' . $uid);
13
14         $r = q("select * from user where uid = %d limit 1", intval($uid));
15
16         call_hooks('remove_user',$r[0]);
17
18         // save username (actually the nickname as it is guaranteed
19         // unique), so it cannot be re-registered in the future.
20
21         q("insert into userd ( username ) values ( '%s' )",
22                 $r[0]['nickname']
23         );
24
25         // don't delete yet, will be done later when contacts have deleted my stuff
26         // q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
27         q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
28         q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
29         q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
30         q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
31         q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
32         q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
33         q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
34         q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
35         q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
36         q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
37         q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
38         q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
39         q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
40         q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
41         q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
42         q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
43         q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
44         q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
45         // don't delete yet, will be done later when contacts have deleted my stuff
46         // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
47         q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
48         proc_run('php', "include/notifier.php", "removeme", $uid);
49
50         // Send an update to the directory
51         proc_run('php', "include/directory.php", $r[0]['url']);
52
53         if($uid == local_user()) {
54                 unset($_SESSION['authenticated']);
55                 unset($_SESSION['uid']);
56                 goaway($a->get_baseurl());
57         }
58 }
59
60
61 function contact_remove($id) {
62
63         $r = q("select uid from contact where id = %d limit 1",
64                 intval($id)
65         );
66         if((! count($r)) || (! intval($r[0]['uid'])))
67                 return;
68
69         $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
70         if($archive) {
71                 q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
72                         intval($id)
73                 );
74                 return;
75         }
76
77         q("DELETE FROM `contact` WHERE `id` = %d",
78                 intval($id)
79         );
80         q("DELETE FROM `item` WHERE `contact-id` = %d ",
81                 intval($id)
82         );
83         q("DELETE FROM `photo` WHERE `contact-id` = %d ",
84                 intval($id)
85         );
86         q("DELETE FROM `mail` WHERE `contact-id` = %d ",
87                 intval($id)
88         );
89         q("DELETE FROM `event` WHERE `cid` = %d ",
90                 intval($id)
91         );
92         q("DELETE FROM `queue` WHERE `cid` = %d ",
93                 intval($id)
94         );
95
96 }
97
98
99 // sends an unfriend message. Does not remove the contact
100
101 function terminate_friendship($user,$self,$contact) {
102
103
104         $a = get_app();
105
106         require_once('include/datetime.php');
107
108         if($contact['network'] === NETWORK_OSTATUS) {
109
110                 $slap = replace_macros(get_markup_template('follow_slap.tpl'), array(
111                         '$name' => $user['username'],
112                         '$profile_page' => $a->get_baseurl() . '/profile/' . $user['nickname'],
113                         '$photo' => $self['photo'],
114                         '$thumb' => $self['thumb'],
115                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
116                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . get_guid(32),
117                         '$title' => '',
118                         '$type' => 'text',
119                         '$content' => t('stopped following'),
120                         '$nick' => $user['nickname'],
121                         '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
122                         '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
123                 ));
124
125                 if((x($contact,'notify')) && (strlen($contact['notify']))) {
126                         require_once('include/salmon.php');
127                         slapper($user,$contact['notify'],$slap);
128                 }
129         }
130         elseif($contact['network'] === NETWORK_DIASPORA) {
131                 require_once('include/diaspora.php');
132                 diaspora::send_unshare($user,$contact);
133         }
134         elseif($contact['network'] === NETWORK_DFRN) {
135                 require_once('include/dfrn.php');
136                 dfrn::deliver($user,$contact,'placeholder', 1);
137         }
138
139 }
140
141
142 // Contact has refused to recognise us as a friend. We will start a countdown.
143 // If they still don't recognise us in 32 days, the relationship is over,
144 // and we won't waste any more time trying to communicate with them.
145 // This provides for the possibility that their database is temporarily messed
146 // up or some other transient event and that there's a possibility we could recover from it.
147
148 if(! function_exists('mark_for_death')) {
149 function mark_for_death($contact) {
150
151         if($contact['archive'])
152                 return;
153
154         if($contact['term-date'] == '0000-00-00 00:00:00') {
155                 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
156                                 dbesc(datetime_convert()),
157                                 intval($contact['id'])
158                 );
159         }
160         else {
161
162                 /// @todo 
163                 /// We really should send a notification to the owner after 2-3 weeks
164                 /// so they won't be surprised when the contact vanishes and can take
165                 /// remedial action if this was a serious mistake or glitch
166
167                 $expiry = $contact['term-date'] . ' + 32 days ';
168                 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
169
170                         // relationship is really truly dead.
171                         // archive them rather than delete
172                         // though if the owner tries to unarchive them we'll start the whole process over again
173
174                         q("update contact set `archive` = 1 where id = %d",
175                                 intval($contact['id'])
176                         );
177                         q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
178
179                         //contact_remove($contact['id']);
180
181                 }
182         }
183
184 }}
185
186 if(! function_exists('unmark_for_death')) {
187 function unmark_for_death($contact) {
188         // It's a miracle. Our dead contact has inexplicably come back to life.
189         q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
190                 dbesc('0000-00-00 00:00:00'),
191                 intval($contact['id'])
192         );
193 }}
194
195 /**
196  * @brief Get contact data for a given profile link
197  *
198  * The function looks at several places (contact table and gcontact table) for the contact
199  *
200  * @param string $url The profile link
201  * @param int $uid User id
202  * @param array $default If not data was found take this data as default value
203  *
204  * @return array Contact data
205  */
206 function get_contact_details_by_url($url, $uid = -1, $default = array()) {
207         if ($uid == -1)
208                 $uid = local_user();
209
210         // Fetch contact data from the contact table for the given user
211         $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
212                         `keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, `self`
213                 FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
214                         dbesc(normalise_link($url)), intval($uid));
215
216         // Fetch the data from the contact table with "uid=0" (which is filled automatically)
217         if (!$r)
218                 $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
219                                 `keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, 0 AS `self`
220                         FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
221                                 dbesc(normalise_link($url)));
222
223         // Fetch the data from the gcontact table
224         if (!$r)
225                 $r = q("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `zid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
226                                 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `community` AS `forum`, 0 AS `prv`, `community`, `birthday`, 0 AS `self`
227                         FROM `gcontact` WHERE `nurl` = '%s'",
228                                 dbesc(normalise_link($url)));
229
230         if ($r) {
231                 // If there is more than one entry we filter out the connector networks
232                 if (count($r) > 1)
233                         foreach ($r AS $id => $result)
234                                 if ($result["network"] == NETWORK_STATUSNET)
235                                         unset($r[$id]);
236
237                 $profile = array_shift($r);
238
239                 // "bd" always contains the upcoming birthday of a contact.
240                 // "birthday" might contain the birthday including the year of birth.
241                 if ($profile["birthday"] != "0000-00-00") {
242                         $bd_timestamp = strtotime($profile["birthday"]);
243                         $month = date("m", $bd_timestamp);
244                         $day = date("d", $bd_timestamp);
245
246                         $current_timestamp = time();
247                         $current_year = date("Y", $current_timestamp);
248                         $current_month = date("m", $current_timestamp);
249                         $current_day = date("d", $current_timestamp);
250
251                         $profile["bd"] = $current_year."-".$month."-".$day;
252                         $current = $current_year."-".$current_month."-".$current_day;
253
254                         if ($profile["bd"] < $current)
255                                 $profile["bd"] = (++$current_year)."-".$month."-".$day;
256                 } else
257                         $profile["bd"] = "0000-00-00";
258         } else
259                 $profile = $default;
260
261         if (($profile["photo"] == "") AND isset($default["photo"]))
262                 $profile["photo"] = $default["photo"];
263
264         if (($profile["name"] == "") AND isset($default["name"]))
265                 $profile["name"] = $default["name"];
266
267         if (($profile["network"] == "") AND isset($default["network"]))
268                 $profile["network"] = $default["network"];
269
270         if (!isset($profile["thumb"]) AND isset($profile["photo"]))
271                 $profile["thumb"] = $profile["photo"];
272
273         if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
274                 in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
275                 proc_run('php',"include/update_gcontact.php", $profile["gid"]);
276
277         // Show contact details of Diaspora contacts only if connected
278         if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
279                 $profile["location"] = "";
280                 $profile["about"] = "";
281                 $profile["gender"] = "";
282                 $profile["birthday"] = "0000-00-00";
283         }
284
285         return($profile);
286 }
287
288 if(! function_exists('contact_photo_menu')){
289 function contact_photo_menu($contact, $uid = 0) {
290
291         $a = get_app();
292
293         $contact_url="";
294         $pm_url="";
295         $status_link="";
296         $photos_link="";
297         $posts_link="";
298         $contact_drop_link = "";
299         $poke_link="";
300
301         if ($uid == 0)
302                 $uid = local_user();
303
304         if ($contact["uid"] != $uid) {
305                 if ($uid == 0) {
306                         $profile_link = zrl($contact['url']);
307                         $menu = Array('profile' => array(t("View Profile"), $profile_link, true));
308
309                         return $menu;
310                 }
311
312                 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `network` = '%s' AND `uid` = %d",
313                         dbesc($contact["nurl"]), dbesc($contact["network"]), intval($uid));
314                 if ($r)
315                         return contact_photo_menu($r[0], $uid);
316                 else {
317                         $profile_link = zrl($contact['url']);
318                         $connlnk = 'follow/?url='.$contact['url'];
319                         $menu = Array(
320                                 'profile' => array(t("View Profile"), $profile_link, true),
321                                 'follow' => array(t("Connect/Follow"), $connlnk, true)
322                                 );
323
324                         return $menu;
325                 }
326         }
327
328         $sparkle = false;
329         if($contact['network'] === NETWORK_DFRN) {
330                 $sparkle = true;
331                 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
332         }
333         else
334                 $profile_link = $contact['url'];
335
336         if($profile_link === 'mailbox')
337                 $profile_link = '';
338
339         if($sparkle) {
340                 $status_link = $profile_link . "?url=status";
341                 $photos_link = $profile_link . "?url=photos";
342                 $profile_link = $profile_link . "?url=profile";
343         }
344
345         if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
346                 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
347
348         if ($contact["network"] == NETWORK_DFRN)
349                 $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
350
351         $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
352         $posts_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/posts';
353         $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
354
355
356         /**
357          * menu array:
358          * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
359          */
360         $menu = Array(
361                 'status' => array(t("View Status"), $status_link, true),
362                 'profile' => array(t("View Profile"), $profile_link, true),
363                 'photos' => array(t("View Photos"), $photos_link,true),
364                 'network' => array(t("Network Posts"), $posts_link,false),
365                 'edit' => array(t("Edit Contact"), $contact_url, false),
366                 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
367                 'pm' => array(t("Send PM"), $pm_url, false),
368                 'poke' => array(t("Poke"), $poke_link, false),
369         );
370
371
372         $args = array('contact' => $contact, 'menu' => &$menu);
373
374         call_hooks('contact_photo_menu', $args);
375
376         $menucondensed = array();
377
378         foreach ($menu AS $menuname=>$menuitem)
379                 if ($menuitem[1] != "")
380                         $menucondensed[$menuname] = $menuitem;
381
382         return $menucondensed;
383 }}
384
385
386 function random_profile() {
387         $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
388                                 AND `last_contact` >= `last_failure`
389                                 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
390                         ORDER BY rand() LIMIT 1",
391                 dbesc(NETWORK_DFRN));
392
393         if(count($r))
394                 return dirname($r[0]['url']);
395         return '';
396 }
397
398
399 function contacts_not_grouped($uid,$start = 0,$count = 0) {
400
401         if(! $count) {
402                 $r = q("select count(*) as total from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) ",
403                         intval($uid),
404                         intval($uid)
405                 );
406
407                 return $r;
408
409
410         }
411
412         $r = q("select * from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) and blocked = 0 and pending = 0 limit %d, %d",
413                 intval($uid),
414                 intval($uid),
415                 intval($start),
416                 intval($count)
417         );
418
419         return $r;
420 }
421
422 function get_contact($url, $uid = 0) {
423         require_once("include/Scrape.php");
424
425         $data = array();
426         $contactid = 0;
427
428         // is it an address in the format user@server.tld?
429         /// @todo use gcontact and/or the addr field for a lookup
430         if (!strstr($url, "http") OR strstr($url, "@")) {
431                 $data = probe_url($url);
432                 $url = $data["url"];
433                 if ($url == "")
434                         return 0;
435         }
436
437         $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
438                         dbesc(normalise_link($url)),
439                         intval($uid));
440
441         if (!$contact)
442                 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d ORDER BY `id` LIMIT 1",
443                                 dbesc($url),
444                                 dbesc(normalise_link($url)),
445                                 intval($uid));
446
447         if ($contact) {
448                 $contactid = $contact[0]["id"];
449
450                 // Update the contact every 7 days
451                 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
452                 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
453
454                 if (!$update_photo)
455                         return($contactid);
456         } elseif ($uid != 0)
457                 return 0;
458
459         if (!count($data))
460                 $data = probe_url($url);
461
462         // Does this address belongs to a valid network?
463         if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
464                 if ($uid != 0)
465                         return 0;
466
467                 // Get data from the gcontact table
468                 $r = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'",
469                          dbesc(normalise_link($url)));
470                 if (!$r)
471                         return 0;
472
473                 $data = $r[0];
474         }
475
476         $url = $data["url"];
477
478         if ($contactid == 0) {
479                 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
480                                         `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
481                                         `batch`, `request`, `confirm`, `poco`,
482                                         `writable`, `blocked`, `readonly`, `pending`)
483                                         VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
484                         intval($uid),
485                         dbesc(datetime_convert()),
486                         dbesc($data["url"]),
487                         dbesc(normalise_link($data["url"])),
488                         dbesc($data["addr"]),
489                         dbesc($data["alias"]),
490                         dbesc($data["notify"]),
491                         dbesc($data["poll"]),
492                         dbesc($data["name"]),
493                         dbesc($data["nick"]),
494                         dbesc($data["photo"]),
495                         dbesc($data["network"]),
496                         dbesc($data["pubkey"]),
497                         intval(CONTACT_IS_SHARING),
498                         intval($data["priority"]),
499                         dbesc($data["batch"]),
500                         dbesc($data["request"]),
501                         dbesc($data["confirm"]),
502                         dbesc($data["poco"])
503                 );
504
505                 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
506                                 dbesc(normalise_link($data["url"])),
507                                 intval($uid));
508                 if (!$contact)
509                         return 0;
510
511                 $contactid = $contact[0]["id"];
512
513                 // Update the newly created contact from data in the gcontact table
514                 $r = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'",
515                          dbesc(normalise_link($data["url"])));
516                 if ($r) {
517                         logger("Update contact ".$data["url"]);
518                         q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d",
519                                 dbesc($r["location"]), dbesc($r["about"]), dbesc($r["keywords"]),
520                                 dbesc($r["gender"]), intval($contactid));
521                 }
522         }
523
524         if ((count($contact) > 1) AND ($uid == 0) AND ($contactid != 0) AND ($url != ""))
525                 q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d",
526                         dbesc(normalise_link($url)),
527                         intval($contactid));
528
529         require_once("Photo.php");
530
531         update_contact_avatar($data["photo"],$uid,$contactid);
532
533         q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
534                 `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d",
535                 dbesc($data["addr"]),
536                 dbesc($data["alias"]),
537                 dbesc($data["name"]),
538                 dbesc($data["nick"]),
539                 dbesc(datetime_convert()),
540                 dbesc(datetime_convert()),
541                 intval($contactid)
542         );
543
544         return $contactid;
545 }
546
547 /**
548  * @brief Returns posts from a given gcontact
549  *
550  * @param App $a argv application class
551  * @param int $gcontact_id Global contact
552  *
553  * @return string posts in HTML
554  */
555 function posts_from_gcontact($a, $gcontact_id) {
556
557         require_once('include/conversation.php');
558
559         // There are no posts with "uid = 0" with connector networks
560         // This speeds up the query a lot
561         $r = q("SELECT `network` FROM `gcontact` WHERE `id` = %d", dbesc($gcontact_id));
562         if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
563                 $sql = "(`item`.`uid` = 0 OR  (`item`.`uid` = %d AND `item`.`private`))";
564         else
565                 $sql = "`item`.`uid` = %d";
566
567         if(get_config('system', 'old_pager')) {
568                 $r = q("SELECT COUNT(*) AS `total` FROM `item`
569                         WHERE `gcontact-id` = %d and $sql",
570                         intval($gcontact_id),
571                         intval(local_user()));
572
573                 $a->set_pager_total($r[0]['total']);
574         }
575
576         $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
577                         `author-name` AS `name`, `owner-avatar` AS `photo`,
578                         `owner-link` AS `url`, `owner-avatar` AS `thumb`
579                 FROM `item` FORCE INDEX (`gcontactid_uid_created`)
580                 WHERE `gcontact-id` = %d AND $sql AND
581                         NOT `deleted` AND NOT `moderated` AND `visible`
582                 ORDER BY `item`.`created` DESC LIMIT %d, %d",
583                 intval($gcontact_id),
584                 intval(local_user()),
585                 intval($a->pager['start']),
586                 intval($a->pager['itemspage'])
587         );
588
589         $o = conversation($a,$r,'community',false);
590
591         if(!get_config('system', 'old_pager')) {
592                 $o .= alt_pager($a,count($r));
593         } else {
594                 $o .= paginate($a);
595         }
596
597         return $o;
598 }
599
600 /**
601  * @brief Returns posts from a given contact
602  *
603  * @param App $a argv application class
604  * @param int $contact_id contact
605  *
606  * @return string posts in HTML
607  */
608 function posts_from_contact($a, $contact_id) {
609
610         require_once('include/conversation.php');
611
612         $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
613         if (!$r)
614                 return false;
615
616         $contact = $r[0];
617
618         if(get_config('system', 'old_pager')) {
619                 $r = q("SELECT COUNT(*) AS `total` FROM `item`
620                         WHERE `item`.`uid` = %d AND `author-link` IN ('%s', '%s')",
621                         intval(local_user()),
622                         dbesc(str_replace("https://", "http://", $contact["url"])),
623                         dbesc(str_replace("http://", "https://", $contact["url"])));
624
625                 $a->set_pager_total($r[0]['total']);
626         }
627
628         $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
629                         `author-name` AS `name`, `owner-avatar` AS `photo`,
630                         `owner-link` AS `url`, `owner-avatar` AS `thumb`
631                 FROM `item` FORCE INDEX (`uid_contactid_created`)
632                 WHERE `item`.`uid` = %d AND `contact-id` = %d
633                         AND `author-link` IN ('%s', '%s')
634                         AND NOT `deleted` AND NOT `moderated` AND `visible`
635                 ORDER BY `item`.`created` DESC LIMIT %d, %d",
636                 intval(local_user()),
637                 intval($contact_id),
638                 dbesc(str_replace("https://", "http://", $contact["url"])),
639                 dbesc(str_replace("http://", "https://", $contact["url"])),
640                 intval($a->pager['start']),
641                 intval($a->pager['itemspage'])
642         );
643
644         $o .= conversation($a,$r,'community',false);
645
646         if(!get_config('system', 'old_pager'))
647                 $o .= alt_pager($a,count($r));
648         else
649                 $o .= paginate($a);
650
651         return $o;
652 }
653
654 /**
655  * @brief Returns a formatted location string from the given profile array
656  *
657  * @param array $profile Profile array (Generated from the "profile" table)
658  *
659  * @return string Location string
660  */
661 function formatted_location($profile) {
662         $location = '';
663
664         if($profile['locality'])
665                 $location .= $profile['locality'];
666
667         if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
668                 if($location)
669                         $location .= ', ';
670
671                 $location .= $profile['region'];
672         }
673
674         if($profile['country-name']) {
675                 if($location)
676                         $location .= ', ';
677
678                 $location .= $profile['country-name'];
679         }
680
681         return $location;
682 }
683 ?>