]> git.mxchange.org Git - friendica.git/blobdiff - include/Contact.php
Issue-#3873
[friendica.git] / include / Contact.php
index d8971abc35d5493cc9892ed33c08424ec58e941f..a7a22671feae31a6834c073b71d867ec56d845a5 100644 (file)
@@ -1,60 +1,42 @@
 <?php
 
 use Friendica\App;
+use Friendica\Core\PConfig;
+use Friendica\Core\System;
+use Friendica\Core\Worker;
+use Friendica\Network\Probe;
 
 // Included here for completeness, but this is a very dangerous operation.
 // It is the caller's responsibility to confirm the requestor's intent and
 // authorisation to do this.
 
 function user_remove($uid) {
-       if(! $uid)
+       if (!$uid) {
                return;
+       }
+
        logger('Removing user: ' . $uid);
 
-       $r = q("select * from user where uid = %d limit 1", intval($uid));
+       $r = dba::select('user', array(), array('uid' => $uid), array("limit" => 1));
 
-       call_hooks('remove_user',$r[0]);
+       call_hooks('remove_user',$r);
 
        // save username (actually the nickname as it is guaranteed
        // unique), so it cannot be re-registered in the future.
 
-       q("insert into userd ( username ) values ( '%s' )",
-               $r[0]['nickname']
-       );
+       dba::insert('userd', array('username' => $r['nickname']));
 
-       /// @todo Should be done in a background job since this likely will run into a time out
-       // don't delete yet, will be done later when contacts have deleted my stuff
-       // q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
-       q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
-       // don't delete yet, will be done later when contacts have deleted my stuff
-       // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
+       // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
        q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
-       proc_run(PRIORITY_HIGH, "include/notifier.php", "removeme", $uid);
+       Worker::add(PRIORITY_HIGH, "notifier", "removeme", $uid);
 
        // Send an update to the directory
-       proc_run(PRIORITY_LOW, "include/directory.php", $r[0]['url']);
+       Worker::add(PRIORITY_LOW, "directory", $r['url']);
 
        if($uid == local_user()) {
                unset($_SESSION['authenticated']);
                unset($_SESSION['uid']);
-               goaway(App::get_baseurl());
+               goaway(System::baseUrl());
        }
 }
 
@@ -69,7 +51,7 @@ function contact_remove($id) {
                return;
        }
 
-       $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
+       $archive = PConfig::get($r[0]['uid'], 'system','archive_removed_contacts');
        if ($archive) {
                q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
                        intval($id)
@@ -77,10 +59,10 @@ function contact_remove($id) {
                return;
        }
 
-       q("DELETE FROM `contact` WHERE `id` = %d", intval($id));
+       dba::delete('contact', array('id' => $id));
 
        // Delete the rest in the background
-       proc_run(PRIORITY_LOW, 'include/remove_contact.php', $id);
+       Worker::add(PRIORITY_LOW, 'remove_contact', $id);
 }
 
 
@@ -91,11 +73,11 @@ function terminate_friendship($user,$self,$contact) {
        /// @TODO Get rid of this, include/datetime.php should care about it by itself
        $a = get_app();
 
-       require_once('include/datetime.php');
+       require_once 'include/datetime.php';
 
        if ($contact['network'] === NETWORK_OSTATUS) {
 
-               require_once('include/ostatus.php');
+               require_once 'include/ostatus.php';
 
                // create an unfollow slap
                $item = array();
@@ -104,14 +86,14 @@ function terminate_friendship($user,$self,$contact) {
                $slap = ostatus::salmon($item, $user);
 
                if ((x($contact,'notify')) && (strlen($contact['notify']))) {
-                       require_once('include/salmon.php');
+                       require_once 'include/salmon.php';
                        slapper($user,$contact['notify'],$slap);
                }
        } elseif ($contact['network'] === NETWORK_DIASPORA) {
-               require_once('include/diaspora.php');
+               require_once 'include/diaspora.php';
                Diaspora::send_unshare($user,$contact);
        } elseif ($contact['network'] === NETWORK_DFRN) {
-               require_once('include/dfrn.php');
+               require_once 'include/dfrn.php';
                dfrn::deliver($user,$contact,'placeholder', 1);
        }
 
@@ -175,7 +157,7 @@ function mark_for_death($contact) {
 
 function unmark_for_death($contact) {
 
-       $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` > '%s'",
+       $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND (`term-date` > '%s' OR `archive`)",
                intval($contact['id']),
                dbesc('1000-00-00 00:00:00')
        );
@@ -186,16 +168,11 @@ function unmark_for_death($contact) {
        }
 
        // It's a miracle. Our dead contact has inexplicably come back to life.
-       q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
-               dbesc(NULL_DATE),
-               intval($contact['id'])
-       );
+       $fields = array('term-date' => NULL_DATE, 'archive' => false);
+       dba::update('contact', $fields, array('id' => $contact['id']));
 
        if ($contact['url'] != '') {
-               q("UPDATE `contact` SET `term-date` = '%s' WHERE `nurl` = '%s'",
-                       dbesc(NULL_DATE),
-                       dbesc(normalise_link($contact['url']))
-               );
+               dba::update('contact', $fields, array('nurl' => normalise_link($contact['url'])));
        }
 }
 
@@ -226,25 +203,50 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
                return $cache[$url][$uid];
        }
 
+       $ssl_url = str_replace('http://', 'https://', $url);
+
        // Fetch contact data from the contact table for the given user
-       $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
+       $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`,
                        `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
-               FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
-                       dbesc(normalise_link($url)), intval($uid));
+               FROM `contact` WHERE `nurl` = ? AND `uid` = ?",
+                       normalise_link($url), $uid);
+       $r = dba::inArray($s);
+
+       // Fetch contact data from the contact table for the given user, checking with the alias
+       if (!dbm::is_result($r)) {
+               $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`,
+                               `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
+                       FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?",
+                               normalise_link($url), $url, $ssl_url, $uid);
+               $r = dba::inArray($s);
+       }
 
        // Fetch the data from the contact table with "uid=0" (which is filled automatically)
-       if (!dbm::is_result($r))
-               $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
+       if (!dbm::is_result($r)) {
+               $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`,
                        `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
-                       FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
-                               dbesc(normalise_link($url)));
+                       FROM `contact` WHERE `nurl` = ? AND `uid` = 0",
+                               normalise_link($url));
+               $r = dba::inArray($s);
+       }
+
+       // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
+       if (!dbm::is_result($r)) {
+               $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`,
+                       `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
+                       FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0",
+                               normalise_link($url), $url, $ssl_url);
+               $r = dba::inArray($s);
+       }
 
        // Fetch the data from the gcontact table
-       if (!dbm::is_result($r))
-               $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`,
+       if (!dbm::is_result($r)) {
+               $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`,
                        `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
-                       FROM `gcontact` WHERE `nurl` = '%s'",
-                               dbesc(normalise_link($url)));
+                       FROM `gcontact` WHERE `nurl` = ?",
+                               normalise_link($url));
+               $r = dba::inArray($s);
+       }
 
        if (dbm::is_result($r)) {
                // If there is more than one entry we filter out the connector networks
@@ -283,33 +285,33 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
                $profile = $default;
        }
 
-       if (($profile["photo"] == "") AND isset($default["photo"])) {
+       if (($profile["photo"] == "") && isset($default["photo"])) {
                $profile["photo"] = $default["photo"];
        }
 
-       if (($profile["name"] == "") AND isset($default["name"])) {
+       if (($profile["name"] == "") && isset($default["name"])) {
                $profile["name"] = $default["name"];
        }
 
-       if (($profile["network"] == "") AND isset($default["network"])) {
+       if (($profile["network"] == "") && isset($default["network"])) {
                $profile["network"] = $default["network"];
        }
 
-       if (($profile["thumb"] == "") AND isset($profile["photo"])) {
+       if (($profile["thumb"] == "") && isset($profile["photo"])) {
                $profile["thumb"] = $profile["photo"];
        }
 
-       if (($profile["micro"] == "") AND isset($profile["thumb"])) {
+       if (($profile["micro"] == "") && isset($profile["thumb"])) {
                $profile["micro"] = $profile["thumb"];
        }
 
-       if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
+       if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0) &&
                in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
-               proc_run(PRIORITY_LOW, "include/update_gcontact.php", $profile["gid"]);
+               Worker::add(PRIORITY_LOW, "update_gcontact", $profile["gid"]);
        }
 
        // Show contact details of Diaspora contacts only if connected
-       if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
+       if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) {
                $profile["location"] = "";
                $profile["about"] = "";
                $profile["gender"] = "";
@@ -363,7 +365,6 @@ function get_contact_details_by_addr($addr, $uid = -1) {
                                dbesc($addr));
 
        if (!dbm::is_result($r)) {
-               require_once('include/Probe.php');
                $data = Probe::uri($addr);
 
                $profile = get_contact_details_by_url($data['url'], $uid);
@@ -418,7 +419,7 @@ function contact_photo_menu($contact, $uid = 0)
        $sparkle = false;
        if ($contact['network'] === NETWORK_DFRN) {
                $sparkle = true;
-               $profile_link = App::get_baseurl() . '/redir/' . $contact['id'];
+               $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
        } else {
                $profile_link = $contact['url'];
        }
@@ -434,17 +435,17 @@ function contact_photo_menu($contact, $uid = 0)
        }
 
        if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA))) {
-               $pm_url = App::get_baseurl() . '/message/new/' . $contact['id'];
+               $pm_url = System::baseUrl() . '/message/new/' . $contact['id'];
        }
 
        if ($contact['network'] == NETWORK_DFRN) {
-               $poke_link = App::get_baseurl() . '/poke/?f=&c=' . $contact['id'];
+               $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
        }
 
-       $contact_url = App::get_baseurl() . '/contacts/' . $contact['id'];
+       $contact_url = System::baseUrl() . '/contacts/' . $contact['id'];
 
-       $posts_link = App::get_baseurl() . '/contacts/' . $contact['id'] . '/posts';
-       $contact_drop_link = App::get_baseurl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
+       $posts_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/posts';
+       $contact_drop_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
 
        /**
         * menu array:
@@ -539,7 +540,7 @@ function contacts_not_grouped($uid,$start = 0,$count = 0) {
  * @return integer Contact ID
  */
 function get_contact($url, $uid = 0, $no_update = false) {
-       logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);;
+       logger("Get contact data for url ".$url." and user ".$uid." - ".System::callstack(), LOGGER_DEBUG);
 
        $data = array();
        $contact_id = 0;
@@ -549,39 +550,35 @@ function get_contact($url, $uid = 0, $no_update = false) {
        }
 
        // We first try the nurl (http://server.tld/nick), most common case
-       $contacts = q("SELECT `id`, `avatar-date` FROM `contact`
-                                       WHERE `nurl` = '%s'
-                                       AND `uid` = %d",
-                       dbesc(normalise_link($url)),
-                       intval($uid));
-
+       $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
 
        // Then the addr (nick@server.tld)
-       if (! dbm::is_result($contacts)) {
-               $contacts = q("SELECT `id`, `avatar-date` FROM `contact`
-                                       WHERE `addr` = '%s'
-                                       AND `uid` = %d",
-                       dbesc($url),
-                       intval($uid));
+       if (!dbm::is_result($contact)) {
+               $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1));
        }
 
        // Then the alias (which could be anything)
-       if (! dbm::is_result($contacts)) {
-               $contacts = q("SELECT `id`, `avatar-date` FROM `contact`
-                                       WHERE `alias` IN ('%s', '%s')
-                                       AND `uid` = %d",
-                       dbesc($url),
-                       dbesc(normalise_link($url)),
-                       intval($uid));
+       if (!dbm::is_result($contact)) {
+               // The link could be provided as http although we stored it as https
+               $ssl_url = str_replace('http://', 'https://', $url);
+               $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ? LIMIT 1",
+                               $url, normalise_link($url), $ssl_url, $uid);
+               $contact = dba::fetch($r);
+               dba::close($r);
        }
 
-       if (dbm::is_result($contacts)) {
-               $contact_id = $contacts[0]["id"];
+       if (dbm::is_result($contact)) {
+               $contact_id = $contact["id"];
 
                // Update the contact every 7 days
-               $update_photo = ($contacts[0]['avatar-date'] < datetime_convert('','','now -7 days'));
+               $update_contact = ($contact['avatar-date'] < datetime_convert('','','now -7 days'));
 
-               if (!$update_photo OR $no_update) {
+               // We force the update if the avatar is empty
+               if ($contact['avatar'] == '') {
+                       $update_contact = true;
+               }
+
+               if (!$update_contact || $no_update) {
                        return $contact_id;
                }
        } elseif ($uid != 0) {
@@ -589,55 +586,42 @@ function get_contact($url, $uid = 0, $no_update = false) {
                return 0;
        }
 
-       require_once('include/Probe.php');
-       $data = Probe::uri($url);
+       $data = Probe::uri($url, "", $uid);
 
        // Last try in gcontact for unsupported networks
-       if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO))) {
+       if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO, NETWORK_MAIL))) {
                if ($uid != 0) {
                        return 0;
                }
 
                // Get data from the gcontact table
-               $gcontacts = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'",
-                        dbesc(normalise_link($url)));
-               if (!$gcontacts) {
+               $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'),
+                                               array('nurl' => normalise_link($url)), array('limit' => 1));
+               if (!dbm::is_result($gcontacts)) {
                        return 0;
                }
 
-               $data = $gcontacts[0];
+               $data = array_merge($data, $gcontacts);
        }
 
-       $url = $data["url"];
+       if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {
+               $contact_id = get_contact($data["alias"], $uid, true);
+       }
 
+       $url = $data["url"];
        if (!$contact_id) {
-               q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
-                                       `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
-                                       `batch`, `request`, `confirm`, `poco`, `name-date`, `uri-date`,
-                                       `writable`, `blocked`, `readonly`, `pending`)
-                                       VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
-                       intval($uid),
-                       dbesc(datetime_convert()),
-                       dbesc($data["url"]),
-                       dbesc(normalise_link($data["url"])),
-                       dbesc($data["addr"]),
-                       dbesc($data["alias"]),
-                       dbesc($data["notify"]),
-                       dbesc($data["poll"]),
-                       dbesc($data["name"]),
-                       dbesc($data["nick"]),
-                       dbesc($data["photo"]),
-                       dbesc($data["network"]),
-                       dbesc($data["pubkey"]),
-                       intval(CONTACT_IS_SHARING),
-                       intval($data["priority"]),
-                       dbesc($data["batch"]),
-                       dbesc($data["request"]),
-                       dbesc($data["confirm"]),
-                       dbesc($data["poco"]),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert())
-               );
+               dba::insert('contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"],
+                                       'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"],
+                                       'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"],
+                                       'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"],
+                                       'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"],
+                                       'network' => $data["network"], 'pubkey' => $data["pubkey"],
+                                       'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"],
+                                       'batch' => $data["batch"], 'request' => $data["request"],
+                                       'confirm' => $data["confirm"], 'poco' => $data["poco"],
+                                       'name-date' => datetime_convert(), 'uri-date' => datetime_convert(),
+                                       'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0,
+                                       'readonly' => 0, 'pending' => 0));
 
                $contacts = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
                                dbesc(normalise_link($data["url"])),
@@ -649,53 +633,109 @@ function get_contact($url, $uid = 0, $no_update = false) {
                $contact_id = $contacts[0]["id"];
 
                // Update the newly created contact from data in the gcontact table
-               $gcontacts = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'",
-                        dbesc(normalise_link($data["url"])));
-               if (dbm::is_result($gcontacts)) {
-                       logger("Update contact " . $data["url"] . ' from gcontact');
-                       q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d",
-                               dbesc($gcontacts[0]["location"]), dbesc($gcontacts[0]["about"]), dbesc($gcontacts[0]["keywords"]),
-                               dbesc($gcontacts[0]["gender"]), intval($contact_id));
+               $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'),
+                                       array('nurl' => normalise_link($data["url"])), array('limit' => 1));
+               if (dbm::is_result($gcontact)) {
+                       // Only use the information when the probing hadn't fetched these values
+                       if ($data['keywords'] != '') {
+                               unset($gcontact['keywords']);
+                       }
+                       if ($data['location'] != '') {
+                               unset($gcontact['location']);
+                       }
+                       if ($data['about'] != '') {
+                               unset($gcontact['about']);
+                       }
+                       dba::update('contact', $gcontact, array('id' => $contact_id));
                }
-       }
 
-       if (count($contacts) > 1 AND $uid == 0 AND $contact_id != 0 AND $url != "") {
-               q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d AND NOT `self`",
-                       dbesc(normalise_link($url)),
-                       intval($contact_id));
+               if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
+                       dba::delete('contact', array("`nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
+                               normalise_link($data["url"]), $contact_id));
+               }
        }
 
        require_once "Photo.php";
 
        update_contact_avatar($data["photo"], $uid, $contact_id);
 
-       $contacts = q("SELECT `addr`, `alias`, `name`, `nick` FROM `contact` WHERE `id` = %d", intval($contact_id));
+       $contact = dba::select('contact', array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'),
+                               array('id' => $contact_id), array('limit' => 1));
 
        // This condition should always be true
-       if (!dbm::is_result($contacts)) {
+       if (!dbm::is_result($contact)) {
                return $contact_id;
        }
 
-       // Only update if there had something been changed
-       if ($data["addr"] != $contacts[0]["addr"] OR
-               $data["alias"] != $contacts[0]["alias"] OR
-               $data["name"] != $contacts[0]["name"] OR
-               $data["nick"] != $contacts[0]["nick"]) {
-               q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
-                       `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d",
-                       dbesc($data["addr"]),
-                       dbesc($data["alias"]),
-                       dbesc($data["name"]),
-                       dbesc($data["nick"]),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       intval($contact_id)
-               );
+       $updated = array('addr' => $data['addr'],
+                       'alias' => $data['alias'],
+                       'url' => $data['url'],
+                       'nurl' => normalise_link($data['url']),
+                       'name' => $data['name'],
+                       'nick' => $data['nick']);
+
+       if ($data['keywords'] != '') {
+               $updated['keywords'] = $data['keywords'];
+       }
+       if ($data['location'] != '') {
+               $updated['location'] = $data['location'];
+       }
+       if ($data['about'] != '') {
+               $updated['about'] = $data['about'];
        }
 
+       if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
+               $updated['uri-date'] = datetime_convert();
+       }
+       if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
+               $updated['name-date'] = datetime_convert();
+       }
+
+       $updated['avatar-date'] = datetime_convert();
+
+       dba::update('contact', $updated, array('id' => $contact_id), $contact);
+
        return $contact_id;
 }
 
+/**
+ * @brief Checks if the contact is blocked
+ *
+ * @param int $cid contact id
+ *
+ * @return boolean Is the contact blocked?
+ */
+function blockedContact($cid) {
+       if ($cid == 0) {
+               return false;
+       }
+
+       $blocked = dba::select('contact', array('blocked'), array('id' => $cid), array('limit' => 1));
+       if (!dbm::is_result($blocked)) {
+               return false;
+       }
+       return (bool)$blocked['blocked'];
+}
+
+/**
+ * @brief Checks if the contact is hidden
+ *
+ * @param int $cid contact id
+ *
+ * @return boolean Is the contact hidden?
+ */
+function hiddenContact($cid) {
+       if ($cid == 0) {
+               return false;
+       }
+
+       $hidden = dba::select('contact', array('hidden'), array('id' => $cid), array('limit' => 1));
+       if (!dbm::is_result($hidden)) {
+               return false;
+       }
+       return (bool)$hidden['hidden'];
+}
+
 /**
  * @brief Returns posts from a given gcontact
  *
@@ -706,7 +746,7 @@ function get_contact($url, $uid = 0, $no_update = false) {
  */
 function posts_from_gcontact(App $a, $gcontact_id) {
 
-       require_once('include/conversation.php');
+       require_once 'include/conversation.php';
 
        // There are no posts with "uid = 0" with connector networks
        // This speeds up the query a lot
@@ -745,26 +785,29 @@ function posts_from_gcontact(App $a, $gcontact_id) {
  */
 function posts_from_contact_url(App $a, $contact_url) {
 
-       require_once('include/conversation.php');
+       require_once 'include/conversation.php';
 
        // There are no posts with "uid = 0" with connector networks
        // This speeds up the query a lot
-       $r = q("SELECT `network`, `id` AS `author-id` FROM `contact`
+       $r = q("SELECT `network`, `id` AS `author-id`, `contact-type` FROM `contact`
                WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0",
                dbesc(normalise_link($contact_url)));
-       if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
-               $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
-       } else {
-               $sql = "`item`.`uid` = %d";
-       }
 
        if (!dbm::is_result($r)) {
                return '';
        }
 
+       if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
+               $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND NOT `item`.`global`))";
+       } else {
+               $sql = "`item`.`uid` = %d";
+       }
+
        $author_id = intval($r[0]["author-id"]);
 
-       $r = q(item_query()." AND `item`.`author-id` = %d AND ".$sql.
+       $contact = ($r[0]["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
+
+       $r = q(item_query()." AND `item`.`".$contact."` = %d AND ".$sql.
                " ORDER BY `item`.`created` DESC LIMIT %d, %d",
                intval($author_id),
                intval(local_user()),
@@ -792,7 +835,7 @@ function formatted_location($profile) {
        if($profile['locality'])
                $location .= $profile['locality'];
 
-       if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
+       if($profile['region'] && ($profile['locality'] != $profile['region'])) {
                if($location)
                        $location .= ', ';