4 * @file src/Object/Contact.php
7 namespace Friendica\Object;
10 use Friendica\BaseObject;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBM;
15 use Friendica\Network\Probe;
16 use Friendica\Object\Photo;
17 use Friendica\Protocol\Diaspora;
18 use Friendica\Protocol\DFRN;
19 use Friendica\Protocol\OStatus;
20 use Friendica\Protocol\Salmon;
23 require_once 'boot.php';
24 require_once 'include/text.php';
27 * @brief functions for interacting with a contact
29 class Contact extends BaseObject
32 * @brief Marks a contact for removal
34 * @param int $id contact id
37 public static function remove($id)
39 // We want just to make sure that we don't delete our "self" contact
40 $r = dba::select('contact', array('uid'), array('id' => $id, 'self' => false), array('limit' => 1));
42 if (!DBM::is_result($r) || !intval($r['uid'])) {
46 $archive = PConfig::get($r['uid'], 'system', 'archive_removed_contacts');
48 dba::update('contact', array('archive' => true, 'network' => 'none', 'writable' => false), array('id' => $id));
52 dba::delete('contact', array('id' => $id));
54 // Delete the rest in the background
55 Worker::add(PRIORITY_LOW, 'RemoveContact', $id);
59 * @brief Sends an unfriend message. Does not remove the contact
61 * @param array $user User unfriending
62 * @param array $contact Contact unfriended
65 public static function terminateFriendship(array $user, array $contact)
67 if ($contact['network'] === NETWORK_OSTATUS) {
68 // create an unfollow slap
70 $item['verb'] = NAMESPACE_OSTATUS . "/unfollow";
71 $item['follow'] = $contact["url"];
72 $slap = OStatus::salmon($item, $user);
74 if ((x($contact, 'notify')) && (strlen($contact['notify']))) {
75 Salmon::slapper($user, $contact['notify'], $slap);
77 } elseif ($contact['network'] === NETWORK_DIASPORA) {
78 Diaspora::sendUnshare($user, $contact);
79 } elseif ($contact['network'] === NETWORK_DFRN) {
80 DFRN::deliver($user, $contact, 'placeholder', 1);
85 * @brief Marks a contact for archival after a communication issue delay
87 * Contact has refused to recognise us as a friend. We will start a countdown.
88 * If they still don't recognise us in 32 days, the relationship is over,
89 * and we won't waste any more time trying to communicate with them.
90 * This provides for the possibility that their database is temporarily messed
91 * up or some other transient event and that there's a possibility we could recover from it.
93 * @param array $contact contact to mark for archival
96 public static function markForArchival(array $contact)
98 // Contact already archived, nothing to do
99 if ($contact['archive']) {
103 if ($contact['term-date'] <= NULL_DATE) {
104 dba::update('contact', array('term-date' => datetime_convert()), array('id' => $contact['id']));
106 if ($contact['url'] != '') {
107 dba::update('contact', array('term-date' => datetime_convert()), array('`nurl` = ? AND `term-date` <= ?', normalise_link($contact['url']), NULL_DATE));
111 * We really should send a notification to the owner after 2-3 weeks
112 * so they won't be surprised when the contact vanishes and can take
113 * remedial action if this was a serious mistake or glitch
116 /// @todo Check for contact vitality via probing
117 $expiry = $contact['term-date'] . ' + 32 days ';
118 if (datetime_convert() > datetime_convert('UTC', 'UTC', $expiry)) {
119 /* Relationship is really truly dead. archive them rather than
120 * delete, though if the owner tries to unarchive them we'll start
121 * the whole process over again.
123 dba::update('contact', array('archive' => 1), array('id' => $contact['id']));
125 if ($contact['url'] != '') {
126 dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url'])));
133 * @brief Cancels the archival countdown
135 * @see Contact::markForArchival()
137 * @param array $contact contact to be unmarked for archival
140 public static function unmarkForArchival(array $contact)
142 $condition = array('`id` = ? AND (`term-date` > ? OR `archive`)', $contact[`id`], NULL_DATE);
143 $exists = dba::exists('contact', $condition);
145 // We don't need to update, we never marked this contact for archival
150 // It's a miracle. Our dead contact has inexplicably come back to life.
151 $fields = array('term-date' => NULL_DATE, 'archive' => false);
152 dba::update('contact', $fields, array('id' => $contact['id']));
154 if ($contact['url'] != '') {
155 dba::update('contact', $fields, array('nurl' => normalise_link($contact['url'])));
160 * @brief Get contact data for a given profile link
162 * The function looks at several places (contact table and gcontact table) for the contact
163 * It caches its result for the same script execution to prevent duplicate calls
165 * @param string $url The profile link
166 * @param int $uid User id
167 * @param array $default If not data was found take this data as default value
169 * @return array Contact data
171 public static function getDetailsByURL($url, $uid = -1, array $default = [])
173 static $cache = array();
183 if (isset($cache[$url][$uid])) {
184 return $cache[$url][$uid];
187 $ssl_url = str_replace('http://', 'https://', $url);
189 // Fetch contact data from the contact table for the given user
190 $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`,
191 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
192 FROM `contact` WHERE `nurl` = ? AND `uid` = ?", normalise_link($url), $uid);
193 $r = dba::inArray($s);
195 // Fetch contact data from the contact table for the given user, checking with the alias
196 if (!DBM::is_result($r)) {
197 $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`,
198 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
199 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?", normalise_link($url), $url, $ssl_url, $uid);
200 $r = dba::inArray($s);
203 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
204 if (!DBM::is_result($r)) {
205 $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`,
206 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
207 FROM `contact` WHERE `nurl` = ? AND `uid` = 0", normalise_link($url));
208 $r = dba::inArray($s);
211 // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
212 if (!DBM::is_result($r)) {
213 $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`,
214 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
215 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0", normalise_link($url), $url, $ssl_url);
216 $r = dba::inArray($s);
219 // Fetch the data from the gcontact table
220 if (!DBM::is_result($r)) {
221 $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`,
222 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
223 FROM `gcontact` WHERE `nurl` = ?", normalise_link($url));
224 $r = dba::inArray($s);
227 if (DBM::is_result($r)) {
228 // If there is more than one entry we filter out the connector networks
230 foreach ($r as $id => $result) {
231 if ($result["network"] == NETWORK_STATUSNET) {
237 $profile = array_shift($r);
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"] > '0001-01-01') {
242 $bd_timestamp = strtotime($profile["birthday"]);
243 $month = date("m", $bd_timestamp);
244 $day = date("d", $bd_timestamp);
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);
251 $profile["bd"] = $current_year . "-" . $month . "-" . $day;
252 $current = $current_year . "-" . $current_month . "-" . $current_day;
254 if ($profile["bd"] < $current) {
255 $profile["bd"] = ( ++$current_year) . "-" . $month . "-" . $day;
258 $profile["bd"] = '0001-01-01';
264 if (($profile["photo"] == "") && isset($default["photo"])) {
265 $profile["photo"] = $default["photo"];
268 if (($profile["name"] == "") && isset($default["name"])) {
269 $profile["name"] = $default["name"];
272 if (($profile["network"] == "") && isset($default["network"])) {
273 $profile["network"] = $default["network"];
276 if (($profile["thumb"] == "") && isset($profile["photo"])) {
277 $profile["thumb"] = $profile["photo"];
280 if (($profile["micro"] == "") && isset($profile["thumb"])) {
281 $profile["micro"] = $profile["thumb"];
284 if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0)
285 && in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))
287 Worker::add(PRIORITY_LOW, "UpdateGContact", $profile["gid"]);
290 // Show contact details of Diaspora contacts only if connected
291 if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) {
292 $profile["location"] = "";
293 $profile["about"] = "";
294 $profile["gender"] = "";
295 $profile["birthday"] = '0001-01-01';
298 $cache[$url][$uid] = $profile;
304 * @brief Get contact data for a given address
306 * The function looks at several places (contact table and gcontact table) for the contact
308 * @param string $addr The profile link
309 * @param int $uid User id
311 * @return array Contact data
313 public static function getDetailsByAddr($addr, $uid = -1)
315 static $cache = array();
325 // Fetch contact data from the contact table for the given user
326 $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
327 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
328 FROM `contact` WHERE `addr` = '%s' AND `uid` = %d", dbesc($addr), intval($uid));
330 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
331 if (!DBM::is_result($r))
332 $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
333 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
334 FROM `contact` WHERE `addr` = '%s' AND `uid` = 0", dbesc($addr));
336 // Fetch the data from the gcontact table
337 if (!DBM::is_result($r))
338 $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`,
339 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
340 FROM `gcontact` WHERE `addr` = '%s'", dbesc($addr));
342 if (!DBM::is_result($r)) {
343 $data = Probe::uri($addr);
345 $profile = self::getDetailsByURL($data['url'], $uid);
354 * @brief Returns the data array for the photo menu of a given contact
356 * @param array $contact contact
357 * @param int $uid optional, default 0
360 public static function photoMenu(array $contact, $uid = 0)
362 // @todo Unused, to be removed
370 $contact_drop_link = '';
377 if ($contact['uid'] != $uid) {
379 $profile_link = zrl($contact['url']);
380 $menu = array('profile' => array(t('View Profile'), $profile_link, true));
385 $r = dba::select('contact', array(), array('nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid), array('limit' => 1));
387 return self::photoMenu($r, $uid);
389 $profile_link = zrl($contact['url']);
390 $connlnk = 'follow/?url=' . $contact['url'];
392 'profile' => array(t('View Profile'), $profile_link, true),
393 'follow' => array(t('Connect/Follow'), $connlnk, true)
401 if ($contact['network'] === NETWORK_DFRN) {
403 $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
405 $profile_link = $contact['url'];
408 if ($profile_link === 'mailbox') {
413 $status_link = $profile_link . '?url=status';
414 $photos_link = $profile_link . '?url=photos';
415 $profile_link = $profile_link . '?url=profile';
418 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA))) {
419 $pm_url = System::baseUrl() . '/message/new/' . $contact['id'];
422 if ($contact['network'] == NETWORK_DFRN) {
423 $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
426 $contact_url = System::baseUrl() . '/contacts/' . $contact['id'];
428 $posts_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/posts';
429 $contact_drop_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
433 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
436 'status' => array(t("View Status"), $status_link, true),
437 'profile' => array(t("View Profile"), $profile_link, true),
438 'photos' => array(t("View Photos"), $photos_link, true),
439 'network' => array(t("Network Posts"), $posts_link, false),
440 'edit' => array(t("View Contact"), $contact_url, false),
441 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
442 'pm' => array(t("Send PM"), $pm_url, false),
443 'poke' => array(t("Poke"), $poke_link, false),
447 $args = array('contact' => $contact, 'menu' => &$menu);
449 call_hooks('contact_photo_menu', $args);
451 $menucondensed = array();
453 foreach ($menu as $menuname => $menuitem) {
454 if ($menuitem[1] != '') {
455 $menucondensed[$menuname] = $menuitem;
459 return $menucondensed;
463 * @brief Returns ungrouped contact count or list for user
465 * Returns either the total number of ungrouped contacts for the given user
466 * id or a paginated list of ungrouped contacts.
468 * @param int $uid uid
469 * @param int $start optional, default 0
470 * @param int $count optional, default 0
474 public static function getUngroupedList($uid, $start = 0, $count = 0)
478 "SELECT COUNT(*) AS `total`
485 SELECT DISTINCT(`contact-id`)
488 )", intval($uid), intval($uid)
502 SELECT DISTINCT(`contact-id`)
503 FROM `group_member` WHERE `uid` = %d
505 LIMIT %d, %d", intval($uid), intval($uid), intval($start), intval($count)
511 * @brief Fetch the contact id for a given url and user
513 * First lookup in the contact table to find a record matching either `url`, `nurl`,
516 * If there's no record and we aren't looking for a public contact, we quit.
517 * If there's one, we check that it isn't time to update the picture else we
518 * directly return the found contact id.
520 * Second, we probe the provided $url wether it's http://server.tld/profile or
521 * nick@server.tld. We quit if we can't get any info back.
523 * Third, we create the contact record if it doesn't exist
525 * Fourth, we update the existing record with the new data (avatar, alias, nick)
526 * if there's any updates
528 * @param string $url Contact URL
529 * @param integer $uid The user id for the contact (0 = public contact)
530 * @param boolean $no_update Don't update the contact
532 * @return integer Contact ID
534 public static function getIdForURL($url, $uid = 0, $no_update = false)
536 logger("Get contact data for url " . $url . " and user " . $uid . " - " . System::callstack(), LOGGER_DEBUG);
544 /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following
545 // We first try the nurl (http://server.tld/nick), most common case
546 $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
548 // Then the addr (nick@server.tld)
549 if (!DBM::is_result($contact)) {
550 $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1));
553 // Then the alias (which could be anything)
554 if (!DBM::is_result($contact)) {
555 // The link could be provided as http although we stored it as https
556 $ssl_url = str_replace('http://', 'https://', $url);
557 $r = dba::select('contact', array('id', 'avatar-date'), array('`alias` IN (?, ?, ?) AND `uid` = ?', $url, normalise_link($url), $ssl_url, $uid), array('limit' => 1));
558 $contact = dba::fetch($r);
562 if (DBM::is_result($contact)) {
563 $contact_id = $contact["id"];
565 // Update the contact every 7 days
566 $update_contact = ($contact['avatar-date'] < datetime_convert('', '', 'now -7 days'));
568 // We force the update if the avatar is empty
569 if ($contact['avatar'] == '') {
570 $update_contact = true;
573 if (!$update_contact || $no_update) {
576 } elseif ($uid != 0) {
577 // Non-existing user-specific contact, exiting
581 $data = Probe::uri($url, "", $uid);
583 // Last try in gcontact for unsupported networks
584 if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO, NETWORK_MAIL))) {
589 // Get data from the gcontact table
590 $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'), array('nurl' => normalise_link($url)), array('limit' => 1));
591 if (!DBM::is_result($gcontacts)) {
595 $data = array_merge($data, $gcontacts);
598 if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {
599 $contact_id = self::getIdForURL($data["alias"], $uid, true);
605 'contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"],
606 'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"],
607 'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"],
608 'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"],
609 'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"],
610 'network' => $data["network"], 'pubkey' => $data["pubkey"],
611 'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"],
612 'batch' => $data["batch"], 'request' => $data["request"],
613 'confirm' => $data["confirm"], 'poco' => $data["poco"],
614 'name-date' => datetime_convert(), 'uri-date' => datetime_convert(),
615 'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0,
616 'readonly' => 0, 'pending' => 0)
619 $s = dba::select('contact', array('id'), array('nurl' => normalise_link($data["url"]), 'uid' => $uid), array('order' => array('id'), 'limit' => 2));
620 $contacts = dba::inArray($s);
621 if (!DBM::is_result($contacts)) {
625 $contact_id = $contacts[0]["id"];
627 // Update the newly created contact from data in the gcontact table
628 $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'), array('nurl' => normalise_link($data["url"])), array('limit' => 1));
629 if (DBM::is_result($gcontact)) {
630 // Only use the information when the probing hadn't fetched these values
631 if ($data['keywords'] != '') {
632 unset($gcontact['keywords']);
634 if ($data['location'] != '') {
635 unset($gcontact['location']);
637 if ($data['about'] != '') {
638 unset($gcontact['about']);
640 dba::update('contact', $gcontact, array('id' => $contact_id));
643 if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
644 dba::delete('contact', array("`nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
645 normalise_link($data["url"]), $contact_id));
649 self::updateAvatar($data["photo"], $uid, $contact_id);
651 $contact = dba::select('contact', array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'), array('id' => $contact_id), array('limit' => 1));
653 // This condition should always be true
654 if (!DBM::is_result($contact)) {
658 $updated = array('addr' => $data['addr'],
659 'alias' => $data['alias'],
660 'url' => $data['url'],
661 'nurl' => normalise_link($data['url']),
662 'name' => $data['name'],
663 'nick' => $data['nick']);
665 if ($data['keywords'] != '') {
666 $updated['keywords'] = $data['keywords'];
668 if ($data['location'] != '') {
669 $updated['location'] = $data['location'];
671 if ($data['about'] != '') {
672 $updated['about'] = $data['about'];
675 if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
676 $updated['uri-date'] = datetime_convert();
678 if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
679 $updated['name-date'] = datetime_convert();
682 $updated['avatar-date'] = datetime_convert();
684 dba::update('contact', $updated, array('id' => $contact_id), $contact);
690 * @brief Checks if the contact is blocked
692 * @param int $cid contact id
694 * @return boolean Is the contact blocked?
696 public static function isBlocked($cid)
702 $blocked = dba::select('contact', array('blocked'), array('id' => $cid), array('limit' => 1));
703 if (!DBM::is_result($blocked)) {
706 return (bool) $blocked['blocked'];
710 * @brief Checks if the contact is hidden
712 * @param int $cid contact id
714 * @return boolean Is the contact hidden?
716 public static function isHidden($cid)
722 $hidden = dba::select('contact', array('hidden'), array('id' => $cid), array('limit' => 1));
723 if (!DBM::is_result($hidden)) {
726 return (bool) $hidden['hidden'];
730 * @brief Returns posts from a given contact url
732 * @param string $contact_url Contact URL
734 * @return string posts in HTML
736 public static function getPostsFromUrl($contact_url)
740 require_once 'include/conversation.php';
742 // There are no posts with "uid = 0" with connector networks
743 // This speeds up the query a lot
744 $r = q("SELECT `network`, `id` AS `author-id`, `contact-type` FROM `contact`
745 WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0", dbesc(normalise_link($contact_url)));
747 if (!DBM::is_result($r)) {
751 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
752 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND NOT `item`.`global`))";
754 $sql = "`item`.`uid` = %d";
757 $author_id = intval($r[0]["author-id"]);
759 $contact = ($r[0]["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
761 $r = q(item_query() . " AND `item`.`" . $contact . "` = %d AND " . $sql .
762 " ORDER BY `item`.`created` DESC LIMIT %d, %d", intval($author_id), intval(local_user()), intval($a->pager['start']), intval($a->pager['itemspage'])
766 $o = conversation($a, $r, 'community', false);
768 $o .= alt_pager($a, count($r));
774 * @brief Returns the account type name
776 * The function can be called with either the user or the contact array
778 * @param array $contact contact or user array
781 public static function getAccountType(array $contact)
783 // There are several fields that indicate that the contact or user is a forum
784 // "page-flags" is a field in the user table,
785 // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP.
786 // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP.
787 if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY))
788 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP))
789 || (isset($contact['forum']) && intval($contact['forum']))
790 || (isset($contact['prv']) && intval($contact['prv']))
791 || (isset($contact['community']) && intval($contact['community']))
793 $type = ACCOUNT_TYPE_COMMUNITY;
795 $type = ACCOUNT_TYPE_PERSON;
798 // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
799 if (isset($contact["contact-type"])) {
800 $type = $contact["contact-type"];
802 if (isset($contact["account-type"])) {
803 $type = $contact["account-type"];
807 case ACCOUNT_TYPE_ORGANISATION:
808 $account_type = t("Organisation");
810 case ACCOUNT_TYPE_NEWS:
811 $account_type = t('News');
813 case ACCOUNT_TYPE_COMMUNITY:
814 $account_type = t("Forum");
821 return $account_type;
825 * @brief Updates the avatar links in a contact only if needed
827 * @param string $avatar Link to avatar picture
828 * @param int $uid User id of contact owner
829 * @param int $cid Contact id
830 * @param bool $force force picture update
832 * @return array Returns array of the different avatar sizes
834 public static function updateAvatar($avatar, $uid, $cid, $force = false)
836 // Limit = 1 returns the row so no need for dba:inArray()
837 $r = dba::select('contact', array('avatar', 'photo', 'thumb', 'micro', 'nurl'), array('id' => $cid), array('limit' => 1));
838 if (!DBM::is_result($r)) {
841 $data = array($r["photo"], $r["thumb"], $r["micro"]);
844 if (($r["avatar"] != $avatar) || $force) {
845 $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
850 array('avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => datetime_convert()),
854 // Update the public contact (contact id = 0)
856 $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
857 if (DBM::is_result($pcontact)) {
858 self::updateAvatar($avatar, 0, $pcontact['id'], $force);