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 * Creates the self-contact for the provided user id
35 * @return bool Operation success
37 public static function createSelfFromUserId($uid)
39 // Only create the entry if it doesn't exist yet
40 if (dba::exists('contact', ['uid' => intval($uid), 'self'])) {
44 $user = dba::select('user', ['uid', 'username', 'nickname'], ['uid' => intval($uid)], ['limit' => 1]);
45 if (!DBM::is_result($user)) {
49 $return = dba::insert('contact', [
50 'uid' => $user['uid'],
51 'created' => datetime_convert(),
53 'name' => $user['username'],
54 'nick' => $user['nickname'],
55 'photo' => System::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
56 'thumb' => System::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
57 'micro' => System::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
60 'url' => System::baseUrl() . '/profile/' . $user['nickname'],
61 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname']),
62 'addr' => $user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3),
63 'request' => System::baseUrl() . '/dfrn_request/' . $user['nickname'],
64 'notify' => System::baseUrl() . '/dfrn_notify/' . $user['nickname'],
65 'poll' => System::baseUrl() . '/dfrn_poll/' . $user['nickname'],
66 'confirm' => System::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
67 'poco' => System::baseUrl() . '/poco/' . $user['nickname'],
68 'name-date' => datetime_convert(),
69 'uri-date' => datetime_convert(),
70 'avatar-date' => datetime_convert(),
78 * @brief Marks a contact for removal
80 * @param int $id contact id
83 public static function remove($id)
85 // We want just to make sure that we don't delete our "self" contact
86 $r = dba::select('contact', array('uid'), array('id' => $id, 'self' => false), array('limit' => 1));
88 if (!DBM::is_result($r) || !intval($r['uid'])) {
92 $archive = PConfig::get($r['uid'], 'system', 'archive_removed_contacts');
94 dba::update('contact', array('archive' => true, 'network' => 'none', 'writable' => false), array('id' => $id));
98 dba::delete('contact', array('id' => $id));
100 // Delete the rest in the background
101 Worker::add(PRIORITY_LOW, 'RemoveContact', $id);
105 * @brief Sends an unfriend message. Does not remove the contact
107 * @param array $user User unfriending
108 * @param array $contact Contact unfriended
111 public static function terminateFriendship(array $user, array $contact)
113 if ($contact['network'] === NETWORK_OSTATUS) {
114 // create an unfollow slap
116 $item['verb'] = NAMESPACE_OSTATUS . "/unfollow";
117 $item['follow'] = $contact["url"];
118 $slap = OStatus::salmon($item, $user);
120 if ((x($contact, 'notify')) && (strlen($contact['notify']))) {
121 Salmon::slapper($user, $contact['notify'], $slap);
123 } elseif ($contact['network'] === NETWORK_DIASPORA) {
124 Diaspora::sendUnshare($user, $contact);
125 } elseif ($contact['network'] === NETWORK_DFRN) {
126 DFRN::deliver($user, $contact, 'placeholder', 1);
131 * @brief Marks a contact for archival after a communication issue delay
133 * Contact has refused to recognise us as a friend. We will start a countdown.
134 * If they still don't recognise us in 32 days, the relationship is over,
135 * and we won't waste any more time trying to communicate with them.
136 * This provides for the possibility that their database is temporarily messed
137 * up or some other transient event and that there's a possibility we could recover from it.
139 * @param array $contact contact to mark for archival
142 public static function markForArchival(array $contact)
144 // Contact already archived, nothing to do
145 if ($contact['archive']) {
149 if ($contact['term-date'] <= NULL_DATE) {
150 dba::update('contact', array('term-date' => datetime_convert()), array('id' => $contact['id']));
152 if ($contact['url'] != '') {
153 dba::update('contact', array('term-date' => datetime_convert()), array('`nurl` = ? AND `term-date` <= ?', normalise_link($contact['url']), NULL_DATE));
157 * We really should send a notification to the owner after 2-3 weeks
158 * so they won't be surprised when the contact vanishes and can take
159 * remedial action if this was a serious mistake or glitch
162 /// @todo Check for contact vitality via probing
163 $expiry = $contact['term-date'] . ' + 32 days ';
164 if (datetime_convert() > datetime_convert('UTC', 'UTC', $expiry)) {
165 /* Relationship is really truly dead. archive them rather than
166 * delete, though if the owner tries to unarchive them we'll start
167 * the whole process over again.
169 dba::update('contact', array('archive' => 1), array('id' => $contact['id']));
171 if ($contact['url'] != '') {
172 dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url'])));
179 * @brief Cancels the archival countdown
181 * @see Contact::markForArchival()
183 * @param array $contact contact to be unmarked for archival
186 public static function unmarkForArchival(array $contact)
188 $condition = array('`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], NULL_DATE);
189 $exists = dba::exists('contact', $condition);
191 // We don't need to update, we never marked this contact for archival
196 // It's a miracle. Our dead contact has inexplicably come back to life.
197 $fields = array('term-date' => NULL_DATE, 'archive' => false);
198 dba::update('contact', $fields, array('id' => $contact['id']));
200 if ($contact['url'] != '') {
201 dba::update('contact', $fields, array('nurl' => normalise_link($contact['url'])));
206 * @brief Get contact data for a given profile link
208 * The function looks at several places (contact table and gcontact table) for the contact
209 * It caches its result for the same script execution to prevent duplicate calls
211 * @param string $url The profile link
212 * @param int $uid User id
213 * @param array $default If not data was found take this data as default value
215 * @return array Contact data
217 public static function getDetailsByURL($url, $uid = -1, array $default = [])
219 static $cache = array();
229 if (isset($cache[$url][$uid])) {
230 return $cache[$url][$uid];
233 $ssl_url = str_replace('http://', 'https://', $url);
235 // Fetch contact data from the contact table for the given user
236 $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`,
237 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
238 FROM `contact` WHERE `nurl` = ? AND `uid` = ?", normalise_link($url), $uid);
239 $r = dba::inArray($s);
241 // Fetch contact data from the contact table for the given user, checking with the alias
242 if (!DBM::is_result($r)) {
243 $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`,
244 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
245 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?", normalise_link($url), $url, $ssl_url, $uid);
246 $r = dba::inArray($s);
249 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
250 if (!DBM::is_result($r)) {
251 $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`,
252 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
253 FROM `contact` WHERE `nurl` = ? AND `uid` = 0", normalise_link($url));
254 $r = dba::inArray($s);
257 // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
258 if (!DBM::is_result($r)) {
259 $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`,
260 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
261 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0", normalise_link($url), $url, $ssl_url);
262 $r = dba::inArray($s);
265 // Fetch the data from the gcontact table
266 if (!DBM::is_result($r)) {
267 $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`,
268 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
269 FROM `gcontact` WHERE `nurl` = ?", normalise_link($url));
270 $r = dba::inArray($s);
273 if (DBM::is_result($r)) {
274 // If there is more than one entry we filter out the connector networks
276 foreach ($r as $id => $result) {
277 if ($result["network"] == NETWORK_STATUSNET) {
283 $profile = array_shift($r);
285 // "bd" always contains the upcoming birthday of a contact.
286 // "birthday" might contain the birthday including the year of birth.
287 if ($profile["birthday"] > '0001-01-01') {
288 $bd_timestamp = strtotime($profile["birthday"]);
289 $month = date("m", $bd_timestamp);
290 $day = date("d", $bd_timestamp);
292 $current_timestamp = time();
293 $current_year = date("Y", $current_timestamp);
294 $current_month = date("m", $current_timestamp);
295 $current_day = date("d", $current_timestamp);
297 $profile["bd"] = $current_year . "-" . $month . "-" . $day;
298 $current = $current_year . "-" . $current_month . "-" . $current_day;
300 if ($profile["bd"] < $current) {
301 $profile["bd"] = ( ++$current_year) . "-" . $month . "-" . $day;
304 $profile["bd"] = '0001-01-01';
310 if (($profile["photo"] == "") && isset($default["photo"])) {
311 $profile["photo"] = $default["photo"];
314 if (($profile["name"] == "") && isset($default["name"])) {
315 $profile["name"] = $default["name"];
318 if (($profile["network"] == "") && isset($default["network"])) {
319 $profile["network"] = $default["network"];
322 if (($profile["thumb"] == "") && isset($profile["photo"])) {
323 $profile["thumb"] = $profile["photo"];
326 if (($profile["micro"] == "") && isset($profile["thumb"])) {
327 $profile["micro"] = $profile["thumb"];
330 if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0)
331 && in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))
333 Worker::add(PRIORITY_LOW, "UpdateGContact", $profile["gid"]);
336 // Show contact details of Diaspora contacts only if connected
337 if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) {
338 $profile["location"] = "";
339 $profile["about"] = "";
340 $profile["gender"] = "";
341 $profile["birthday"] = '0001-01-01';
344 $cache[$url][$uid] = $profile;
350 * @brief Get contact data for a given address
352 * The function looks at several places (contact table and gcontact table) for the contact
354 * @param string $addr The profile link
355 * @param int $uid User id
357 * @return array Contact data
359 public static function getDetailsByAddr($addr, $uid = -1)
361 static $cache = array();
371 // Fetch contact data from the contact table for the given user
372 $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
373 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
374 FROM `contact` WHERE `addr` = '%s' AND `uid` = %d", dbesc($addr), intval($uid));
376 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
377 if (!DBM::is_result($r))
378 $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
379 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
380 FROM `contact` WHERE `addr` = '%s' AND `uid` = 0", dbesc($addr));
382 // Fetch the data from the gcontact table
383 if (!DBM::is_result($r))
384 $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`,
385 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
386 FROM `gcontact` WHERE `addr` = '%s'", dbesc($addr));
388 if (!DBM::is_result($r)) {
389 $data = Probe::uri($addr);
391 $profile = self::getDetailsByURL($data['url'], $uid);
400 * @brief Returns the data array for the photo menu of a given contact
402 * @param array $contact contact
403 * @param int $uid optional, default 0
406 public static function photoMenu(array $contact, $uid = 0)
408 // @todo Unused, to be removed
416 $contact_drop_link = '';
423 if ($contact['uid'] != $uid) {
425 $profile_link = zrl($contact['url']);
426 $menu = array('profile' => array(t('View Profile'), $profile_link, true));
431 $r = dba::select('contact', array(), array('nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid), array('limit' => 1));
433 return self::photoMenu($r, $uid);
435 $profile_link = zrl($contact['url']);
436 $connlnk = 'follow/?url=' . $contact['url'];
438 'profile' => array(t('View Profile'), $profile_link, true),
439 'follow' => array(t('Connect/Follow'), $connlnk, true)
447 if ($contact['network'] === NETWORK_DFRN) {
449 $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
451 $profile_link = $contact['url'];
454 if ($profile_link === 'mailbox') {
459 $status_link = $profile_link . '?url=status';
460 $photos_link = $profile_link . '?url=photos';
461 $profile_link = $profile_link . '?url=profile';
464 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA))) {
465 $pm_url = System::baseUrl() . '/message/new/' . $contact['id'];
468 if ($contact['network'] == NETWORK_DFRN) {
469 $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
472 $contact_url = System::baseUrl() . '/contacts/' . $contact['id'];
474 $posts_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/posts';
475 $contact_drop_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
479 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
482 'status' => array(t("View Status"), $status_link, true),
483 'profile' => array(t("View Profile"), $profile_link, true),
484 'photos' => array(t("View Photos"), $photos_link, true),
485 'network' => array(t("Network Posts"), $posts_link, false),
486 'edit' => array(t("View Contact"), $contact_url, false),
487 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
488 'pm' => array(t("Send PM"), $pm_url, false),
489 'poke' => array(t("Poke"), $poke_link, false),
493 $args = array('contact' => $contact, 'menu' => &$menu);
495 call_hooks('contact_photo_menu', $args);
497 $menucondensed = array();
499 foreach ($menu as $menuname => $menuitem) {
500 if ($menuitem[1] != '') {
501 $menucondensed[$menuname] = $menuitem;
505 return $menucondensed;
509 * @brief Returns ungrouped contact count or list for user
511 * Returns either the total number of ungrouped contacts for the given user
512 * id or a paginated list of ungrouped contacts.
514 * @param int $uid uid
515 * @param int $start optional, default 0
516 * @param int $count optional, default 0
520 public static function getUngroupedList($uid, $start = 0, $count = 0)
524 "SELECT COUNT(*) AS `total`
531 SELECT DISTINCT(`contact-id`)
534 )", intval($uid), intval($uid)
548 SELECT DISTINCT(`contact-id`)
549 FROM `group_member` WHERE `uid` = %d
551 LIMIT %d, %d", intval($uid), intval($uid), intval($start), intval($count)
557 * @brief Fetch the contact id for a given url and user
559 * First lookup in the contact table to find a record matching either `url`, `nurl`,
562 * If there's no record and we aren't looking for a public contact, we quit.
563 * If there's one, we check that it isn't time to update the picture else we
564 * directly return the found contact id.
566 * Second, we probe the provided $url wether it's http://server.tld/profile or
567 * nick@server.tld. We quit if we can't get any info back.
569 * Third, we create the contact record if it doesn't exist
571 * Fourth, we update the existing record with the new data (avatar, alias, nick)
572 * if there's any updates
574 * @param string $url Contact URL
575 * @param integer $uid The user id for the contact (0 = public contact)
576 * @param boolean $no_update Don't update the contact
578 * @return integer Contact ID
580 public static function getIdForURL($url, $uid = 0, $no_update = false)
582 logger("Get contact data for url " . $url . " and user " . $uid . " - " . System::callstack(), LOGGER_DEBUG);
590 /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following
591 // We first try the nurl (http://server.tld/nick), most common case
592 $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
594 // Then the addr (nick@server.tld)
595 if (!DBM::is_result($contact)) {
596 $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1));
599 // Then the alias (which could be anything)
600 if (!DBM::is_result($contact)) {
601 // The link could be provided as http although we stored it as https
602 $ssl_url = str_replace('http://', 'https://', $url);
603 $r = dba::select('contact', array('id', 'avatar-date'), array('`alias` IN (?, ?, ?) AND `uid` = ?', $url, normalise_link($url), $ssl_url, $uid), array('limit' => 1));
604 $contact = dba::fetch($r);
608 if (DBM::is_result($contact)) {
609 $contact_id = $contact["id"];
611 // Update the contact every 7 days
612 $update_contact = ($contact['avatar-date'] < datetime_convert('', '', 'now -7 days'));
614 // We force the update if the avatar is empty
615 if ($contact['avatar'] == '') {
616 $update_contact = true;
619 if (!$update_contact || $no_update) {
622 } elseif ($uid != 0) {
623 // Non-existing user-specific contact, exiting
627 $data = Probe::uri($url, "", $uid);
629 // Last try in gcontact for unsupported networks
630 if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO, NETWORK_MAIL))) {
635 // Get data from the gcontact table
636 $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'), array('nurl' => normalise_link($url)), array('limit' => 1));
637 if (!DBM::is_result($gcontacts)) {
641 $data = array_merge($data, $gcontacts);
644 if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {
645 $contact_id = self::getIdForURL($data["alias"], $uid, true);
651 'contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"],
652 'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"],
653 'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"],
654 'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"],
655 'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"],
656 'network' => $data["network"], 'pubkey' => $data["pubkey"],
657 'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"],
658 'batch' => $data["batch"], 'request' => $data["request"],
659 'confirm' => $data["confirm"], 'poco' => $data["poco"],
660 'name-date' => datetime_convert(), 'uri-date' => datetime_convert(),
661 'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0,
662 'readonly' => 0, 'pending' => 0)
665 $s = dba::select('contact', array('id'), array('nurl' => normalise_link($data["url"]), 'uid' => $uid), array('order' => array('id'), 'limit' => 2));
666 $contacts = dba::inArray($s);
667 if (!DBM::is_result($contacts)) {
671 $contact_id = $contacts[0]["id"];
673 // Update the newly created contact from data in the gcontact table
674 $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'), array('nurl' => normalise_link($data["url"])), array('limit' => 1));
675 if (DBM::is_result($gcontact)) {
676 // Only use the information when the probing hadn't fetched these values
677 if ($data['keywords'] != '') {
678 unset($gcontact['keywords']);
680 if ($data['location'] != '') {
681 unset($gcontact['location']);
683 if ($data['about'] != '') {
684 unset($gcontact['about']);
686 dba::update('contact', $gcontact, array('id' => $contact_id));
689 if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
690 dba::delete('contact', array("`nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
691 normalise_link($data["url"]), $contact_id));
695 self::updateAvatar($data["photo"], $uid, $contact_id);
697 $contact = dba::select('contact', array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'), array('id' => $contact_id), array('limit' => 1));
699 // This condition should always be true
700 if (!DBM::is_result($contact)) {
704 $updated = array('addr' => $data['addr'],
705 'alias' => $data['alias'],
706 'url' => $data['url'],
707 'nurl' => normalise_link($data['url']),
708 'name' => $data['name'],
709 'nick' => $data['nick']);
711 if ($data['keywords'] != '') {
712 $updated['keywords'] = $data['keywords'];
714 if ($data['location'] != '') {
715 $updated['location'] = $data['location'];
717 if ($data['about'] != '') {
718 $updated['about'] = $data['about'];
721 if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
722 $updated['uri-date'] = datetime_convert();
724 if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
725 $updated['name-date'] = datetime_convert();
728 $updated['avatar-date'] = datetime_convert();
730 dba::update('contact', $updated, array('id' => $contact_id), $contact);
736 * @brief Checks if the contact is blocked
738 * @param int $cid contact id
740 * @return boolean Is the contact blocked?
742 public static function isBlocked($cid)
748 $blocked = dba::select('contact', array('blocked'), array('id' => $cid), array('limit' => 1));
749 if (!DBM::is_result($blocked)) {
752 return (bool) $blocked['blocked'];
756 * @brief Checks if the contact is hidden
758 * @param int $cid contact id
760 * @return boolean Is the contact hidden?
762 public static function isHidden($cid)
768 $hidden = dba::select('contact', array('hidden'), array('id' => $cid), array('limit' => 1));
769 if (!DBM::is_result($hidden)) {
772 return (bool) $hidden['hidden'];
776 * @brief Returns posts from a given contact url
778 * @param string $contact_url Contact URL
780 * @return string posts in HTML
782 public static function getPostsFromUrl($contact_url)
786 require_once 'include/conversation.php';
788 // There are no posts with "uid = 0" with connector networks
789 // This speeds up the query a lot
790 $r = q("SELECT `network`, `id` AS `author-id`, `contact-type` FROM `contact`
791 WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0", dbesc(normalise_link($contact_url)));
793 if (!DBM::is_result($r)) {
797 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
798 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND NOT `item`.`global`))";
800 $sql = "`item`.`uid` = %d";
803 $author_id = intval($r[0]["author-id"]);
805 $contact = ($r[0]["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
807 $r = q(item_query() . " AND `item`.`" . $contact . "` = %d AND " . $sql .
808 " ORDER BY `item`.`created` DESC LIMIT %d, %d", intval($author_id), intval(local_user()), intval($a->pager['start']), intval($a->pager['itemspage'])
812 $o = conversation($a, $r, 'community', false);
814 $o .= alt_pager($a, count($r));
820 * @brief Returns the account type name
822 * The function can be called with either the user or the contact array
824 * @param array $contact contact or user array
827 public static function getAccountType(array $contact)
829 // There are several fields that indicate that the contact or user is a forum
830 // "page-flags" is a field in the user table,
831 // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP.
832 // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP.
833 if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY))
834 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP))
835 || (isset($contact['forum']) && intval($contact['forum']))
836 || (isset($contact['prv']) && intval($contact['prv']))
837 || (isset($contact['community']) && intval($contact['community']))
839 $type = ACCOUNT_TYPE_COMMUNITY;
841 $type = ACCOUNT_TYPE_PERSON;
844 // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
845 if (isset($contact["contact-type"])) {
846 $type = $contact["contact-type"];
848 if (isset($contact["account-type"])) {
849 $type = $contact["account-type"];
853 case ACCOUNT_TYPE_ORGANISATION:
854 $account_type = t("Organisation");
856 case ACCOUNT_TYPE_NEWS:
857 $account_type = t('News');
859 case ACCOUNT_TYPE_COMMUNITY:
860 $account_type = t("Forum");
867 return $account_type;
871 * @brief Blocks a contact
876 public static function block($uid)
878 $return = dba::update('contact', ['blocked' => true], ['id' => $uid]);
884 * @brief Unblocks a contact
889 public static function unblock($uid)
891 $return = dba::update('contact', ['blocked' => false], ['id' => $uid]);
897 * @brief Updates the avatar links in a contact only if needed
899 * @param string $avatar Link to avatar picture
900 * @param int $uid User id of contact owner
901 * @param int $cid Contact id
902 * @param bool $force force picture update
904 * @return array Returns array of the different avatar sizes
906 public static function updateAvatar($avatar, $uid, $cid, $force = false)
908 // Limit = 1 returns the row so no need for dba:inArray()
909 $r = dba::select('contact', array('avatar', 'photo', 'thumb', 'micro', 'nurl'), array('id' => $cid), array('limit' => 1));
910 if (!DBM::is_result($r)) {
913 $data = array($r["photo"], $r["thumb"], $r["micro"]);
916 if (($r["avatar"] != $avatar) || $force) {
917 $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
922 array('avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => datetime_convert()),
926 // Update the public contact (contact id = 0)
928 $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
929 if (DBM::is_result($pcontact)) {
930 self::updateAvatar($avatar, 0, $pcontact['id'], $force);