4 * @file src/Model/Contact.php
7 namespace Friendica\Model;
9 use Friendica\BaseObject;
10 use Friendica\Core\PConfig;
11 use Friendica\Core\System;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBM;
14 use Friendica\Network\Probe;
15 use Friendica\Object\Image;
16 use Friendica\Protocol\Diaspora;
17 use Friendica\Protocol\DFRN;
18 use Friendica\Protocol\OStatus;
19 use Friendica\Protocol\Salmon;
22 require_once 'boot.php';
23 require_once 'include/text.php';
26 * @brief functions for interacting with a contact
28 class Contact extends BaseObject
31 * Creates the self-contact for the provided user id
34 * @return bool Operation success
36 public static function createSelfFromUserId($uid)
38 // Only create the entry if it doesn't exist yet
39 if (dba::exists('contact', ['uid' => intval($uid), 'self'])) {
43 $user = dba::select('user', ['uid', 'username', 'nickname'], ['uid' => intval($uid)], ['limit' => 1]);
44 if (!DBM::is_result($user)) {
48 $return = dba::insert('contact', [
49 'uid' => $user['uid'],
50 'created' => datetime_convert(),
52 'name' => $user['username'],
53 'nick' => $user['nickname'],
54 'photo' => System::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
55 'thumb' => System::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
56 'micro' => System::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
59 'url' => System::baseUrl() . '/profile/' . $user['nickname'],
60 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname']),
61 'addr' => $user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3),
62 'request' => System::baseUrl() . '/dfrn_request/' . $user['nickname'],
63 'notify' => System::baseUrl() . '/dfrn_notify/' . $user['nickname'],
64 'poll' => System::baseUrl() . '/dfrn_poll/' . $user['nickname'],
65 'confirm' => System::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
66 'poco' => System::baseUrl() . '/poco/' . $user['nickname'],
67 'name-date' => datetime_convert(),
68 'uri-date' => datetime_convert(),
69 'avatar-date' => datetime_convert(),
77 * @brief Marks a contact for removal
79 * @param int $id contact id
82 public static function remove($id)
84 // We want just to make sure that we don't delete our "self" contact
85 $r = dba::select('contact', array('uid'), array('id' => $id, 'self' => false), array('limit' => 1));
87 if (!DBM::is_result($r) || !intval($r['uid'])) {
91 $archive = PConfig::get($r['uid'], 'system', 'archive_removed_contacts');
93 dba::update('contact', array('archive' => true, 'network' => 'none', 'writable' => false), array('id' => $id));
97 dba::delete('contact', array('id' => $id));
99 // Delete the rest in the background
100 Worker::add(PRIORITY_LOW, 'RemoveContact', $id);
104 * @brief Sends an unfriend message. Does not remove the contact
106 * @param array $user User unfriending
107 * @param array $contact Contact unfriended
110 public static function terminateFriendship(array $user, array $contact)
112 if ($contact['network'] === NETWORK_OSTATUS) {
113 // create an unfollow slap
115 $item['verb'] = NAMESPACE_OSTATUS . "/unfollow";
116 $item['follow'] = $contact["url"];
117 $slap = OStatus::salmon($item, $user);
119 if ((x($contact, 'notify')) && (strlen($contact['notify']))) {
120 Salmon::slapper($user, $contact['notify'], $slap);
122 } elseif ($contact['network'] === NETWORK_DIASPORA) {
123 Diaspora::sendUnshare($user, $contact);
124 } elseif ($contact['network'] === NETWORK_DFRN) {
125 DFRN::deliver($user, $contact, 'placeholder', 1);
130 * @brief Marks a contact for archival after a communication issue delay
132 * Contact has refused to recognise us as a friend. We will start a countdown.
133 * If they still don't recognise us in 32 days, the relationship is over,
134 * and we won't waste any more time trying to communicate with them.
135 * This provides for the possibility that their database is temporarily messed
136 * up or some other transient event and that there's a possibility we could recover from it.
138 * @param array $contact contact to mark for archival
141 public static function markForArchival(array $contact)
143 // Contact already archived or "self" contact? => nothing to do
144 if ($contact['archive'] || $contact['self']) {
148 if ($contact['term-date'] <= NULL_DATE) {
149 dba::update('contact', array('term-date' => datetime_convert()), array('id' => $contact['id']));
151 if ($contact['url'] != '') {
152 dba::update('contact', array('term-date' => datetime_convert()), array('`nurl` = ? AND `term-date` <= ? AND NOT `self`', normalise_link($contact['url']), NULL_DATE));
156 * We really should send a notification to the owner after 2-3 weeks
157 * so they won't be surprised when the contact vanishes and can take
158 * remedial action if this was a serious mistake or glitch
161 /// @todo Check for contact vitality via probing
162 $expiry = $contact['term-date'] . ' + 32 days ';
163 if (datetime_convert() > datetime_convert('UTC', 'UTC', $expiry)) {
164 /* Relationship is really truly dead. archive them rather than
165 * delete, though if the owner tries to unarchive them we'll start
166 * the whole process over again.
168 dba::update('contact', array('archive' => 1), array('id' => $contact['id']));
170 if ($contact['url'] != '') {
171 dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url']), 'self' => false));
178 * @brief Cancels the archival countdown
180 * @see Contact::markForArchival()
182 * @param array $contact contact to be unmarked for archival
185 public static function unmarkForArchival(array $contact)
187 $condition = array('`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], NULL_DATE);
188 $exists = dba::exists('contact', $condition);
190 // We don't need to update, we never marked this contact for archival
195 // It's a miracle. Our dead contact has inexplicably come back to life.
196 $fields = array('term-date' => NULL_DATE, 'archive' => false);
197 dba::update('contact', $fields, array('id' => $contact['id']));
199 if ($contact['url'] != '') {
200 dba::update('contact', $fields, array('nurl' => normalise_link($contact['url'])));
205 * @brief Get contact data for a given profile link
207 * The function looks at several places (contact table and gcontact table) for the contact
208 * It caches its result for the same script execution to prevent duplicate calls
210 * @param string $url The profile link
211 * @param int $uid User id
212 * @param array $default If not data was found take this data as default value
214 * @return array Contact data
216 public static function getDetailsByURL($url, $uid = -1, array $default = [])
218 static $cache = array();
228 if (isset($cache[$url][$uid])) {
229 return $cache[$url][$uid];
232 $ssl_url = str_replace('http://', 'https://', $url);
234 // Fetch contact data from the contact table for the given user
235 $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`,
236 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
237 FROM `contact` WHERE `nurl` = ? AND `uid` = ?", normalise_link($url), $uid);
238 $r = dba::inArray($s);
240 // Fetch contact data from the contact table for the given user, checking with the alias
241 if (!DBM::is_result($r)) {
242 $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`,
243 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
244 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?", normalise_link($url), $url, $ssl_url, $uid);
245 $r = dba::inArray($s);
248 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
249 if (!DBM::is_result($r)) {
250 $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`,
251 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
252 FROM `contact` WHERE `nurl` = ? AND `uid` = 0", normalise_link($url));
253 $r = dba::inArray($s);
256 // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
257 if (!DBM::is_result($r)) {
258 $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`,
259 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
260 FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0", normalise_link($url), $url, $ssl_url);
261 $r = dba::inArray($s);
264 // Fetch the data from the gcontact table
265 if (!DBM::is_result($r)) {
266 $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`,
267 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
268 FROM `gcontact` WHERE `nurl` = ?", normalise_link($url));
269 $r = dba::inArray($s);
272 if (DBM::is_result($r)) {
273 // If there is more than one entry we filter out the connector networks
275 foreach ($r as $id => $result) {
276 if ($result["network"] == NETWORK_STATUSNET) {
282 $profile = array_shift($r);
284 // "bd" always contains the upcoming birthday of a contact.
285 // "birthday" might contain the birthday including the year of birth.
286 if ($profile["birthday"] > '0001-01-01') {
287 $bd_timestamp = strtotime($profile["birthday"]);
288 $month = date("m", $bd_timestamp);
289 $day = date("d", $bd_timestamp);
291 $current_timestamp = time();
292 $current_year = date("Y", $current_timestamp);
293 $current_month = date("m", $current_timestamp);
294 $current_day = date("d", $current_timestamp);
296 $profile["bd"] = $current_year . "-" . $month . "-" . $day;
297 $current = $current_year . "-" . $current_month . "-" . $current_day;
299 if ($profile["bd"] < $current) {
300 $profile["bd"] = ( ++$current_year) . "-" . $month . "-" . $day;
303 $profile["bd"] = '0001-01-01';
309 if (($profile["photo"] == "") && isset($default["photo"])) {
310 $profile["photo"] = $default["photo"];
313 if (($profile["name"] == "") && isset($default["name"])) {
314 $profile["name"] = $default["name"];
317 if (($profile["network"] == "") && isset($default["network"])) {
318 $profile["network"] = $default["network"];
321 if (($profile["thumb"] == "") && isset($profile["photo"])) {
322 $profile["thumb"] = $profile["photo"];
325 if (($profile["micro"] == "") && isset($profile["thumb"])) {
326 $profile["micro"] = $profile["thumb"];
329 if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0)
330 && in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))
332 Worker::add(PRIORITY_LOW, "UpdateGContact", $profile["gid"]);
335 // Show contact details of Diaspora contacts only if connected
336 if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) {
337 $profile["location"] = "";
338 $profile["about"] = "";
339 $profile["gender"] = "";
340 $profile["birthday"] = '0001-01-01';
343 $cache[$url][$uid] = $profile;
349 * @brief Get contact data for a given address
351 * The function looks at several places (contact table and gcontact table) for the contact
353 * @param string $addr The profile link
354 * @param int $uid User id
356 * @return array Contact data
358 public static function getDetailsByAddr($addr, $uid = -1)
360 static $cache = array();
370 // Fetch contact data from the contact table for the given user
371 $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
372 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
373 FROM `contact` WHERE `addr` = '%s' AND `uid` = %d", dbesc($addr), intval($uid));
375 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
376 if (!DBM::is_result($r))
377 $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
378 `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
379 FROM `contact` WHERE `addr` = '%s' AND `uid` = 0", dbesc($addr));
381 // Fetch the data from the gcontact table
382 if (!DBM::is_result($r))
383 $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`,
384 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
385 FROM `gcontact` WHERE `addr` = '%s'", dbesc($addr));
387 if (!DBM::is_result($r)) {
388 $data = Probe::uri($addr);
390 $profile = self::getDetailsByURL($data['url'], $uid);
399 * @brief Returns the data array for the photo menu of a given contact
401 * @param array $contact contact
402 * @param int $uid optional, default 0
405 public static function photoMenu(array $contact, $uid = 0)
407 // @todo Unused, to be removed
415 $contact_drop_link = '';
422 if ($contact['uid'] != $uid) {
424 $profile_link = zrl($contact['url']);
425 $menu = array('profile' => array(t('View Profile'), $profile_link, true));
430 $r = dba::select('contact', array(), array('nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid), array('limit' => 1));
432 return self::photoMenu($r, $uid);
434 $profile_link = zrl($contact['url']);
435 $connlnk = 'follow/?url=' . $contact['url'];
437 'profile' => array(t('View Profile'), $profile_link, true),
438 'follow' => array(t('Connect/Follow'), $connlnk, true)
446 if ($contact['network'] === NETWORK_DFRN) {
448 $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
450 $profile_link = $contact['url'];
453 if ($profile_link === 'mailbox') {
458 $status_link = $profile_link . '?url=status';
459 $photos_link = $profile_link . '?url=photos';
460 $profile_link = $profile_link . '?url=profile';
463 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA))) {
464 $pm_url = System::baseUrl() . '/message/new/' . $contact['id'];
467 if ($contact['network'] == NETWORK_DFRN) {
468 $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
471 $contact_url = System::baseUrl() . '/contacts/' . $contact['id'];
473 $posts_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/posts';
474 $contact_drop_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
478 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
481 'status' => array(t("View Status"), $status_link, true),
482 'profile' => array(t("View Profile"), $profile_link, true),
483 'photos' => array(t("View Photos"), $photos_link, true),
484 'network' => array(t("Network Posts"), $posts_link, false),
485 'edit' => array(t("View Contact"), $contact_url, false),
486 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
487 'pm' => array(t("Send PM"), $pm_url, false),
488 'poke' => array(t("Poke"), $poke_link, false),
492 $args = array('contact' => $contact, 'menu' => &$menu);
494 call_hooks('contact_photo_menu', $args);
496 $menucondensed = array();
498 foreach ($menu as $menuname => $menuitem) {
499 if ($menuitem[1] != '') {
500 $menucondensed[$menuname] = $menuitem;
504 return $menucondensed;
508 * @brief Returns ungrouped contact count or list for user
510 * Returns either the total number of ungrouped contacts for the given user
511 * id or a paginated list of ungrouped contacts.
513 * @param int $uid uid
514 * @param int $start optional, default 0
515 * @param int $count optional, default 0
519 public static function getUngroupedList($uid, $start = 0, $count = 0)
523 "SELECT COUNT(*) AS `total`
530 SELECT DISTINCT(`contact-id`)
533 )", intval($uid), intval($uid)
547 SELECT DISTINCT(`contact-id`)
548 FROM `group_member` WHERE `uid` = %d
550 LIMIT %d, %d", intval($uid), intval($uid), intval($start), intval($count)
556 * @brief Fetch the contact id for a given url and user
558 * First lookup in the contact table to find a record matching either `url`, `nurl`,
561 * If there's no record and we aren't looking for a public contact, we quit.
562 * If there's one, we check that it isn't time to update the picture else we
563 * directly return the found contact id.
565 * Second, we probe the provided $url wether it's http://server.tld/profile or
566 * nick@server.tld. We quit if we can't get any info back.
568 * Third, we create the contact record if it doesn't exist
570 * Fourth, we update the existing record with the new data (avatar, alias, nick)
571 * if there's any updates
573 * @param string $url Contact URL
574 * @param integer $uid The user id for the contact (0 = public contact)
575 * @param boolean $no_update Don't update the contact
577 * @return integer Contact ID
579 public static function getIdForURL($url, $uid = 0, $no_update = false)
581 logger("Get contact data for url " . $url . " and user " . $uid . " - " . System::callstack(), LOGGER_DEBUG);
589 /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following
590 // We first try the nurl (http://server.tld/nick), most common case
591 $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
593 // Then the addr (nick@server.tld)
594 if (!DBM::is_result($contact)) {
595 $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1));
598 // Then the alias (which could be anything)
599 if (!DBM::is_result($contact)) {
600 // The link could be provided as http although we stored it as https
601 $ssl_url = str_replace('http://', 'https://', $url);
602 $r = dba::select('contact', array('id', 'avatar-date'), array('`alias` IN (?, ?, ?) AND `uid` = ?', $url, normalise_link($url), $ssl_url, $uid), array('limit' => 1));
603 $contact = dba::fetch($r);
607 if (DBM::is_result($contact)) {
608 $contact_id = $contact["id"];
610 // Update the contact every 7 days
611 $update_contact = ($contact['avatar-date'] < datetime_convert('', '', 'now -7 days'));
613 // We force the update if the avatar is empty
614 if ($contact['avatar'] == '') {
615 $update_contact = true;
618 if (!$update_contact || $no_update) {
621 } elseif ($uid != 0) {
622 // Non-existing user-specific contact, exiting
626 $data = Probe::uri($url, "", $uid);
628 // Last try in gcontact for unsupported networks
629 if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO, NETWORK_MAIL))) {
634 // Get data from the gcontact table
635 $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'), array('nurl' => normalise_link($url)), array('limit' => 1));
636 if (!DBM::is_result($gcontacts)) {
640 $data = array_merge($data, $gcontacts);
643 if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {
644 $contact_id = self::getIdForURL($data["alias"], $uid, true);
650 'contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"],
651 'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"],
652 'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"],
653 'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"],
654 'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"],
655 'network' => $data["network"], 'pubkey' => $data["pubkey"],
656 'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"],
657 'batch' => $data["batch"], 'request' => $data["request"],
658 'confirm' => $data["confirm"], 'poco' => $data["poco"],
659 'name-date' => datetime_convert(), 'uri-date' => datetime_convert(),
660 'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0,
661 'readonly' => 0, 'pending' => 0)
664 $s = dba::select('contact', array('id'), array('nurl' => normalise_link($data["url"]), 'uid' => $uid), array('order' => array('id'), 'limit' => 2));
665 $contacts = dba::inArray($s);
666 if (!DBM::is_result($contacts)) {
670 $contact_id = $contacts[0]["id"];
672 // Update the newly created contact from data in the gcontact table
673 $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'), array('nurl' => normalise_link($data["url"])), array('limit' => 1));
674 if (DBM::is_result($gcontact)) {
675 // Only use the information when the probing hadn't fetched these values
676 if ($data['keywords'] != '') {
677 unset($gcontact['keywords']);
679 if ($data['location'] != '') {
680 unset($gcontact['location']);
682 if ($data['about'] != '') {
683 unset($gcontact['about']);
685 dba::update('contact', $gcontact, array('id' => $contact_id));
688 if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
689 dba::delete('contact', array("`nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
690 normalise_link($data["url"]), $contact_id));
694 self::updateAvatar($data["photo"], $uid, $contact_id);
696 $fields = array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey');
697 $contact = dba::select('contact', $fields, 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 // Only fill the pubkey if it was empty before. We have to prevent identity theft.
712 if (!empty($contact['pubkey'])) {
713 unset($contact['pubkey']);
715 $updated['pubkey'] = $data['pubkey'];
718 if ($data['keywords'] != '') {
719 $updated['keywords'] = $data['keywords'];
721 if ($data['location'] != '') {
722 $updated['location'] = $data['location'];
724 if ($data['about'] != '') {
725 $updated['about'] = $data['about'];
728 if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
729 $updated['uri-date'] = datetime_convert();
731 if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
732 $updated['name-date'] = datetime_convert();
735 $updated['avatar-date'] = datetime_convert();
737 dba::update('contact', $updated, array('id' => $contact_id), $contact);
743 * @brief Checks if the contact is blocked
745 * @param int $cid contact id
747 * @return boolean Is the contact blocked?
749 public static function isBlocked($cid)
755 $blocked = dba::select('contact', array('blocked'), array('id' => $cid), array('limit' => 1));
756 if (!DBM::is_result($blocked)) {
759 return (bool) $blocked['blocked'];
763 * @brief Checks if the contact is hidden
765 * @param int $cid contact id
767 * @return boolean Is the contact hidden?
769 public static function isHidden($cid)
775 $hidden = dba::select('contact', array('hidden'), array('id' => $cid), array('limit' => 1));
776 if (!DBM::is_result($hidden)) {
779 return (bool) $hidden['hidden'];
783 * @brief Returns posts from a given contact url
785 * @param string $contact_url Contact URL
787 * @return string posts in HTML
789 public static function getPostsFromUrl($contact_url)
793 require_once 'include/conversation.php';
795 // There are no posts with "uid = 0" with connector networks
796 // This speeds up the query a lot
797 $r = q("SELECT `network`, `id` AS `author-id`, `contact-type` FROM `contact`
798 WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0", dbesc(normalise_link($contact_url)));
800 if (!DBM::is_result($r)) {
804 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
805 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND NOT `item`.`global`))";
807 $sql = "`item`.`uid` = %d";
810 $author_id = intval($r[0]["author-id"]);
812 $contact = ($r[0]["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
814 $r = q(item_query() . " AND `item`.`" . $contact . "` = %d AND " . $sql .
815 " ORDER BY `item`.`created` DESC LIMIT %d, %d", intval($author_id), intval(local_user()), intval($a->pager['start']), intval($a->pager['itemspage'])
819 $o = conversation($a, $r, 'community', false);
821 $o .= alt_pager($a, count($r));
827 * @brief Returns the account type name
829 * The function can be called with either the user or the contact array
831 * @param array $contact contact or user array
834 public static function getAccountType(array $contact)
836 // There are several fields that indicate that the contact or user is a forum
837 // "page-flags" is a field in the user table,
838 // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP.
839 // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP.
840 if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY))
841 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP))
842 || (isset($contact['forum']) && intval($contact['forum']))
843 || (isset($contact['prv']) && intval($contact['prv']))
844 || (isset($contact['community']) && intval($contact['community']))
846 $type = ACCOUNT_TYPE_COMMUNITY;
848 $type = ACCOUNT_TYPE_PERSON;
851 // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
852 if (isset($contact["contact-type"])) {
853 $type = $contact["contact-type"];
855 if (isset($contact["account-type"])) {
856 $type = $contact["account-type"];
860 case ACCOUNT_TYPE_ORGANISATION:
861 $account_type = t("Organisation");
863 case ACCOUNT_TYPE_NEWS:
864 $account_type = t('News');
866 case ACCOUNT_TYPE_COMMUNITY:
867 $account_type = t("Forum");
874 return $account_type;
878 * @brief Blocks a contact
883 public static function block($uid)
885 $return = dba::update('contact', ['blocked' => true], ['id' => $uid]);
891 * @brief Unblocks a contact
896 public static function unblock($uid)
898 $return = dba::update('contact', ['blocked' => false], ['id' => $uid]);
904 * @brief Updates the avatar links in a contact only if needed
906 * @param string $avatar Link to avatar picture
907 * @param int $uid User id of contact owner
908 * @param int $cid Contact id
909 * @param bool $force force picture update
911 * @return array Returns array of the different avatar sizes
913 public static function updateAvatar($avatar, $uid, $cid, $force = false)
915 // Limit = 1 returns the row so no need for dba:inArray()
916 $r = dba::select('contact', array('avatar', 'photo', 'thumb', 'micro', 'nurl'), array('id' => $cid), array('limit' => 1));
917 if (!DBM::is_result($r)) {
920 $data = array($r["photo"], $r["thumb"], $r["micro"]);
923 if (($r["avatar"] != $avatar) || $force) {
924 $photos = Image::importProfilePhoto($avatar, $uid, $cid, true);
929 array('avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => datetime_convert()),
933 // Update the public contact (contact id = 0)
935 $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
936 if (DBM::is_result($pcontact)) {
937 self::updateAvatar($avatar, 0, $pcontact['id'], $force);