4 use Friendica\Core\PConfig;
5 use Friendica\Core\System;
6 use Friendica\Core\Worker;
7 use Friendica\Network\Probe;
9 // Included here for completeness, but this is a very dangerous operation.
10 // It is the caller's responsibility to confirm the requestor's intent and
11 // authorisation to do this.
13 function user_remove($uid) {
18 logger('Removing user: ' . $uid);
20 $r = dba::select('user', array(), array('uid' => $uid), array("limit" => 1));
22 call_hooks('remove_user',$r);
24 // save username (actually the nickname as it is guaranteed
25 // unique), so it cannot be re-registered in the future.
27 dba::insert('userd', array('username' => $r['nickname']));
29 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
30 q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
31 Worker::add(PRIORITY_HIGH, "notifier", "removeme", $uid);
33 // Send an update to the directory
34 Worker::add(PRIORITY_LOW, "directory", $r['url']);
36 if($uid == local_user()) {
37 unset($_SESSION['authenticated']);
38 unset($_SESSION['uid']);
39 goaway(System::baseUrl());
44 function contact_remove($id) {
46 // We want just to make sure that we don't delete our "self" contact
47 $r = q("SELECT `uid` FROM `contact` WHERE `id` = %d AND NOT `self` LIMIT 1",
50 if (!dbm::is_result($r) || !intval($r[0]['uid'])) {
54 $archive = PConfig::get($r[0]['uid'], 'system','archive_removed_contacts');
56 q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
62 dba::delete('contact', array('id' => $id));
64 // Delete the rest in the background
65 Worker::add(PRIORITY_LOW, 'remove_contact', $id);
69 // sends an unfriend message. Does not remove the contact
71 function terminate_friendship($user,$self,$contact) {
73 /// @TODO Get rid of this, include/datetime.php should care about it by itself
76 require_once 'include/datetime.php';
78 if ($contact['network'] === NETWORK_OSTATUS) {
80 require_once 'include/ostatus.php';
82 // create an unfollow slap
84 $item['verb'] = NAMESPACE_OSTATUS."/unfollow";
85 $item['follow'] = $contact["url"];
86 $slap = ostatus::salmon($item, $user);
88 if ((x($contact,'notify')) && (strlen($contact['notify']))) {
89 require_once 'include/salmon.php';
90 slapper($user,$contact['notify'],$slap);
92 } elseif ($contact['network'] === NETWORK_DIASPORA) {
93 require_once 'include/diaspora.php';
94 Diaspora::send_unshare($user,$contact);
95 } elseif ($contact['network'] === NETWORK_DFRN) {
96 require_once 'include/dfrn.php';
97 dfrn::deliver($user,$contact,'placeholder', 1);
103 // Contact has refused to recognise us as a friend. We will start a countdown.
104 // If they still don't recognise us in 32 days, the relationship is over,
105 // and we won't waste any more time trying to communicate with them.
106 // This provides for the possibility that their database is temporarily messed
107 // up or some other transient event and that there's a possibility we could recover from it.
109 function mark_for_death($contact) {
111 if($contact['archive'])
114 if ($contact['term-date'] <= NULL_DATE) {
115 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
116 dbesc(datetime_convert()),
117 intval($contact['id'])
120 if ($contact['url'] != '') {
121 q("UPDATE `contact` SET `term-date` = '%s'
122 WHERE `nurl` = '%s' AND `term-date` <= '1000-00-00'",
123 dbesc(datetime_convert()),
124 dbesc(normalise_link($contact['url']))
130 /// We really should send a notification to the owner after 2-3 weeks
131 /// so they won't be surprised when the contact vanishes and can take
132 /// remedial action if this was a serious mistake or glitch
135 /// Check for contact vitality via probing
137 $expiry = $contact['term-date'] . ' + 32 days ';
138 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
140 // relationship is really truly dead.
141 // archive them rather than delete
142 // though if the owner tries to unarchive them we'll start the whole process over again
144 q("UPDATE `contact` SET `archive` = 1 WHERE `id` = %d",
145 intval($contact['id'])
148 if ($contact['url'] != '') {
149 q("UPDATE `contact` SET `archive` = 1 WHERE `nurl` = '%s'",
150 dbesc(normalise_link($contact['url']))
158 function unmark_for_death($contact) {
160 $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND (`term-date` > '%s' OR `archive`)",
161 intval($contact['id']),
162 dbesc('1000-00-00 00:00:00')
165 // We don't need to update, we never marked this contact as dead
166 if (!dbm::is_result($r)) {
170 // It's a miracle. Our dead contact has inexplicably come back to life.
171 $fields = array('term-date' => NULL_DATE, 'archive' => false);
172 dba::update('contact', $fields, array('id' => $contact['id']));
174 if ($contact['url'] != '') {
175 dba::update('contact', $fields, array('nurl' => normalise_link($contact['url'])));
180 * @brief Get contact data for a given profile link
182 * The function looks at several places (contact table and gcontact table) for the contact
183 * It caches its result for the same script execution to prevent duplicate calls
185 * @param string $url The profile link
186 * @param int $uid User id
187 * @param array $default If not data was found take this data as default value
189 * @return array Contact data
191 function get_contact_details_by_url($url, $uid = -1, $default = array()) {
192 static $cache = array();
202 if (isset($cache[$url][$uid])) {
203 return $cache[$url][$uid];
206 $ssl_url = str_replace('http://', 'https://', $url);
208 // Fetch contact data from the contact table for the given user
209 $s = dba::p("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
210 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
211 FROM `contact` WHERE `nurl` = ? AND `uid` = ?",
212 normalise_link($url), $uid);
213 $r = dba::inArray($s);
215 // Fetch contact data from the contact table for the given user, checking with the alias
216 if (!dbm::is_result($r)) {
217 $s = dba::p("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
218 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
219 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?",
220 normalise_link($url), $url, $ssl_url, $uid);
221 $r = dba::inArray($s);
224 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
225 if (!dbm::is_result($r)) {
226 $s = dba::p("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
227 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
228 FROM `contact` WHERE `nurl` = ? AND `uid` = 0",
229 normalise_link($url));
230 $r = dba::inArray($s);
233 // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
234 if (!dbm::is_result($r)) {
235 $s = dba::p("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
236 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
237 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0",
238 normalise_link($url), $url, $ssl_url);
239 $r = dba::inArray($s);
242 // Fetch the data from the gcontact table
243 if (!dbm::is_result($r)) {
244 $s = dba::p("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`, '' AS `xmpp`,
245 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
246 FROM `gcontact` WHERE `nurl` = ?",
247 normalise_link($url));
248 $r = dba::inArray($s);
251 if (dbm::is_result($r)) {
252 // If there is more than one entry we filter out the connector networks
254 foreach ($r AS $id => $result) {
255 if ($result["network"] == NETWORK_STATUSNET) {
261 $profile = array_shift($r);
263 // "bd" always contains the upcoming birthday of a contact.
264 // "birthday" might contain the birthday including the year of birth.
265 if ($profile["birthday"] > '0001-01-01') {
266 $bd_timestamp = strtotime($profile["birthday"]);
267 $month = date("m", $bd_timestamp);
268 $day = date("d", $bd_timestamp);
270 $current_timestamp = time();
271 $current_year = date("Y", $current_timestamp);
272 $current_month = date("m", $current_timestamp);
273 $current_day = date("d", $current_timestamp);
275 $profile["bd"] = $current_year."-".$month."-".$day;
276 $current = $current_year."-".$current_month."-".$current_day;
278 if ($profile["bd"] < $current) {
279 $profile["bd"] = (++$current_year)."-".$month."-".$day;
282 $profile["bd"] = '0001-01-01';
288 if (($profile["photo"] == "") && isset($default["photo"])) {
289 $profile["photo"] = $default["photo"];
292 if (($profile["name"] == "") && isset($default["name"])) {
293 $profile["name"] = $default["name"];
296 if (($profile["network"] == "") && isset($default["network"])) {
297 $profile["network"] = $default["network"];
300 if (($profile["thumb"] == "") && isset($profile["photo"])) {
301 $profile["thumb"] = $profile["photo"];
304 if (($profile["micro"] == "") && isset($profile["thumb"])) {
305 $profile["micro"] = $profile["thumb"];
308 if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0) &&
309 in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
310 Worker::add(PRIORITY_LOW, "update_gcontact", $profile["gid"]);
313 // Show contact details of Diaspora contacts only if connected
314 if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) {
315 $profile["location"] = "";
316 $profile["about"] = "";
317 $profile["gender"] = "";
318 $profile["birthday"] = '0001-01-01';
321 $cache[$url][$uid] = $profile;
327 * @brief Get contact data for a given address
329 * The function looks at several places (contact table and gcontact table) for the contact
331 * @param string $addr The profile link
332 * @param int $uid User id
334 * @return array Contact data
336 function get_contact_details_by_addr($addr, $uid = -1) {
337 static $cache = array();
347 // Fetch contact data from the contact table for the given user
348 $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
349 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
350 FROM `contact` WHERE `addr` = '%s' AND `uid` = %d",
351 dbesc($addr), intval($uid));
353 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
354 if (!dbm::is_result($r))
355 $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
356 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
357 FROM `contact` WHERE `addr` = '%s' AND `uid` = 0",
360 // Fetch the data from the gcontact table
361 if (!dbm::is_result($r))
362 $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`, '' AS `xmpp`,
363 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
364 FROM `gcontact` WHERE `addr` = '%s'",
367 if (!dbm::is_result($r)) {
368 $data = Probe::uri($addr);
370 $profile = get_contact_details_by_url($data['url'], $uid);
378 if (! function_exists('contact_photo_menu')) {
379 function contact_photo_menu($contact, $uid = 0)
388 $contact_drop_link = '';
395 if ($contact['uid'] != $uid) {
397 $profile_link = zrl($contact['url']);
398 $menu = Array('profile' => array(t('View Profile'), $profile_link, true));
403 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `network` = '%s' AND `uid` = %d",
404 dbesc($contact['nurl']), dbesc($contact['network']), intval($uid));
406 return contact_photo_menu($r[0], $uid);
408 $profile_link = zrl($contact['url']);
409 $connlnk = 'follow/?url='.$contact['url'];
411 'profile' => array(t('View Profile'), $profile_link, true),
412 'follow' => array(t('Connect/Follow'), $connlnk, true)
420 if ($contact['network'] === NETWORK_DFRN) {
422 $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
424 $profile_link = $contact['url'];
427 if ($profile_link === 'mailbox') {
432 $status_link = $profile_link . '?url=status';
433 $photos_link = $profile_link . '?url=photos';
434 $profile_link = $profile_link . '?url=profile';
437 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA))) {
438 $pm_url = System::baseUrl() . '/message/new/' . $contact['id'];
441 if ($contact['network'] == NETWORK_DFRN) {
442 $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
445 $contact_url = System::baseUrl() . '/contacts/' . $contact['id'];
447 $posts_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/posts';
448 $contact_drop_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
452 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
455 'status' => array(t("View Status"), $status_link, true),
456 'profile' => array(t("View Profile"), $profile_link, true),
457 'photos' => array(t("View Photos"), $photos_link, true),
458 'network' => array(t("Network Posts"), $posts_link, false),
459 'edit' => array(t("View Contact"), $contact_url, false),
460 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
461 'pm' => array(t("Send PM"), $pm_url, false),
462 'poke' => array(t("Poke"), $poke_link, false),
466 $args = array('contact' => $contact, 'menu' => &$menu);
468 call_hooks('contact_photo_menu', $args);
470 $menucondensed = array();
472 foreach ($menu AS $menuname => $menuitem) {
473 if ($menuitem[1] != '') {
474 $menucondensed[$menuname] = $menuitem;
478 return $menucondensed;
482 function random_profile() {
483 $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
484 AND `last_contact` >= `last_failure`
485 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
486 ORDER BY rand() LIMIT 1",
487 dbesc(NETWORK_DFRN));
489 if (dbm::is_result($r))
490 return dirname($r[0]['url']);
495 function contacts_not_grouped($uid,$start = 0,$count = 0) {
498 $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) ",
508 $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",
519 * @brief Fetch the contact id for a given url and user
521 * First lookup in the contact table to find a record matching either `url`, `nurl`,
524 * If there's no record and we aren't looking for a public contact, we quit.
525 * If there's one, we check that it isn't time to update the picture else we
526 * directly return the found contact id.
528 * Second, we probe the provided $url wether it's http://server.tld/profile or
529 * nick@server.tld. We quit if we can't get any info back.
531 * Third, we create the contact record if it doesn't exist
533 * Fourth, we update the existing record with the new data (avatar, alias, nick)
534 * if there's any updates
536 * @param string $url Contact URL
537 * @param integer $uid The user id for the contact (0 = public contact)
538 * @param boolean $no_update Don't update the contact
540 * @return integer Contact ID
542 function get_contact($url, $uid = 0, $no_update = false) {
543 logger("Get contact data for url ".$url." and user ".$uid." - ".System::callstack(), LOGGER_DEBUG);
552 // We first try the nurl (http://server.tld/nick), most common case
553 $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
555 // Then the addr (nick@server.tld)
556 if (!dbm::is_result($contact)) {
557 $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1));
560 // Then the alias (which could be anything)
561 if (!dbm::is_result($contact)) {
562 // The link could be provided as http although we stored it as https
563 $ssl_url = str_replace('http://', 'https://', $url);
564 $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ? LIMIT 1",
565 $url, normalise_link($url), $ssl_url, $uid);
566 $contact = dba::fetch($r);
570 if (dbm::is_result($contact)) {
571 $contact_id = $contact["id"];
573 // Update the contact every 7 days
574 $update_contact = ($contact['avatar-date'] < datetime_convert('','','now -7 days'));
576 // We force the update if the avatar is empty
577 if ($contact['avatar'] == '') {
578 $update_contact = true;
581 if (!$update_contact || $no_update) {
584 } elseif ($uid != 0) {
585 // Non-existing user-specific contact, exiting
589 $data = Probe::uri($url, "", $uid);
591 // Last try in gcontact for unsupported networks
592 if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO, NETWORK_MAIL))) {
597 // Get data from the gcontact table
598 $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'),
599 array('nurl' => normalise_link($url)), array('limit' => 1));
600 if (!dbm::is_result($gcontacts)) {
604 $data = array_merge($data, $gcontacts);
607 if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {
608 $contact_id = get_contact($data["alias"], $uid, true);
613 dba::insert('contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"],
614 'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"],
615 'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"],
616 'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"],
617 'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"],
618 'network' => $data["network"], 'pubkey' => $data["pubkey"],
619 'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"],
620 'batch' => $data["batch"], 'request' => $data["request"],
621 'confirm' => $data["confirm"], 'poco' => $data["poco"],
622 'name-date' => datetime_convert(), 'uri-date' => datetime_convert(),
623 'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0,
624 'readonly' => 0, 'pending' => 0));
626 $contacts = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
627 dbesc(normalise_link($data["url"])),
629 if (!dbm::is_result($contacts)) {
633 $contact_id = $contacts[0]["id"];
635 // Update the newly created contact from data in the gcontact table
636 $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'),
637 array('nurl' => normalise_link($data["url"])), array('limit' => 1));
638 if (dbm::is_result($gcontact)) {
639 // Only use the information when the probing hadn't fetched these values
640 if ($data['keywords'] != '') {
641 unset($gcontact['keywords']);
643 if ($data['location'] != '') {
644 unset($gcontact['location']);
646 if ($data['about'] != '') {
647 unset($gcontact['about']);
649 dba::update('contact', $gcontact, array('id' => $contact_id));
652 if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
653 dba::delete('contact', array("`nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
654 normalise_link($data["url"]), $contact_id));
658 require_once "Photo.php";
660 update_contact_avatar($data["photo"], $uid, $contact_id);
662 $contact = dba::select('contact', array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'),
663 array('id' => $contact_id), array('limit' => 1));
665 // This condition should always be true
666 if (!dbm::is_result($contact)) {
670 $updated = array('addr' => $data['addr'],
671 'alias' => $data['alias'],
672 'url' => $data['url'],
673 'nurl' => normalise_link($data['url']),
674 'name' => $data['name'],
675 'nick' => $data['nick']);
677 if ($data['keywords'] != '') {
678 $updated['keywords'] = $data['keywords'];
680 if ($data['location'] != '') {
681 $updated['location'] = $data['location'];
683 if ($data['about'] != '') {
684 $updated['about'] = $data['about'];
687 if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
688 $updated['uri-date'] = datetime_convert();
690 if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
691 $updated['name-date'] = datetime_convert();
694 $updated['avatar-date'] = datetime_convert();
696 dba::update('contact', $updated, array('id' => $contact_id), $contact);
702 * @brief Checks if the contact is blocked
704 * @param int $cid contact id
706 * @return boolean Is the contact blocked?
708 function blockedContact($cid) {
713 $blocked = dba::select('contact', array('blocked'), array('id' => $cid), array('limit' => 1));
714 if (!dbm::is_result($blocked)) {
717 return (bool)$blocked['blocked'];
721 * @brief Checks if the contact is hidden
723 * @param int $cid contact id
725 * @return boolean Is the contact hidden?
727 function hiddenContact($cid) {
732 $hidden = dba::select('contact', array('hidden'), array('id' => $cid), array('limit' => 1));
733 if (!dbm::is_result($hidden)) {
736 return (bool)$hidden['hidden'];
740 * @brief Returns posts from a given gcontact
742 * @param App $a argv application class
743 * @param int $gcontact_id Global contact
745 * @return string posts in HTML
747 function posts_from_gcontact(App $a, $gcontact_id) {
749 require_once 'include/conversation.php';
751 // There are no posts with "uid = 0" with connector networks
752 // This speeds up the query a lot
753 $r = q("SELECT `network` FROM `gcontact` WHERE `id` = %d", dbesc($gcontact_id));
754 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
755 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
757 $sql = "`item`.`uid` = %d";
759 $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
760 `author-name` AS `name`, `owner-avatar` AS `photo`,
761 `owner-link` AS `url`, `owner-avatar` AS `thumb`
763 WHERE `gcontact-id` = %d AND $sql AND
764 NOT `deleted` AND NOT `moderated` AND `visible`
765 ORDER BY `item`.`created` DESC LIMIT %d, %d",
766 intval($gcontact_id),
767 intval(local_user()),
768 intval($a->pager['start']),
769 intval($a->pager['itemspage'])
772 $o = conversation($a, $r, 'community', false);
774 $o .= alt_pager($a, count($r));
779 * @brief Returns posts from a given contact url
781 * @param App $a argv application class
782 * @param string $contact_url Contact URL
784 * @return string posts in HTML
786 function posts_from_contact_url(App $a, $contact_url) {
788 require_once 'include/conversation.php';
790 // There are no posts with "uid = 0" with connector networks
791 // This speeds up the query a lot
792 $r = q("SELECT `network`, `id` AS `author-id`, `contact-type` FROM `contact`
793 WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0",
794 dbesc(normalise_link($contact_url)));
796 if (!dbm::is_result($r)) {
800 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
801 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND NOT `item`.`global`))";
803 $sql = "`item`.`uid` = %d";
806 $author_id = intval($r[0]["author-id"]);
808 $contact = ($r[0]["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
810 $r = q(item_query()." AND `item`.`".$contact."` = %d AND ".$sql.
811 " ORDER BY `item`.`created` DESC LIMIT %d, %d",
813 intval(local_user()),
814 intval($a->pager['start']),
815 intval($a->pager['itemspage'])
818 $o = conversation($a, $r, 'community', false);
820 $o .= alt_pager($a, count($r));
826 * @brief Returns a formatted location string from the given profile array
828 * @param array $profile Profile array (Generated from the "profile" table)
830 * @return string Location string
832 function formatted_location($profile) {
835 if($profile['locality'])
836 $location .= $profile['locality'];
838 if($profile['region'] && ($profile['locality'] != $profile['region'])) {
842 $location .= $profile['region'];
845 if($profile['country-name']) {
849 $location .= $profile['country-name'];
856 * @brief Returns the account type name
858 * The function can be called with either the user or the contact array
860 * @param array $contact contact or user array
862 function account_type($contact) {
864 // There are several fields that indicate that the contact or user is a forum
865 // "page-flags" is a field in the user table,
866 // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP.
867 // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP.
868 if((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY))
869 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP))
870 || (isset($contact['forum']) && intval($contact['forum']))
871 || (isset($contact['prv']) && intval($contact['prv']))
872 || (isset($contact['community']) && intval($contact['community'])))
873 $type = ACCOUNT_TYPE_COMMUNITY;
875 $type = ACCOUNT_TYPE_PERSON;
877 // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
878 if (isset($contact["contact-type"]))
879 $type = $contact["contact-type"];
880 if (isset($contact["account-type"]))
881 $type = $contact["account-type"];
884 case ACCOUNT_TYPE_ORGANISATION:
885 $account_type = t("Organisation");
887 case ACCOUNT_TYPE_NEWS:
888 $account_type = t('News');
890 case ACCOUNT_TYPE_COMMUNITY:
891 $account_type = t("Forum");
898 return $account_type;