]> git.mxchange.org Git - friendica.git/blobdiff - include/Contact.php
Merge remote-tracking branch 'upstream/develop' into 1610-performance
[friendica.git] / include / Contact.php
index 9d69a81a4e1f7aad01dccb22ccd6e004c210fd4a..ab74a2f866d4e77119b2fe0b7c45e0f2a06321dd 100644 (file)
@@ -45,10 +45,10 @@ function user_remove($uid) {
        // don't delete yet, will be done later when contacts have deleted my stuff
        // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
        q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
-       proc_run('php', "include/notifier.php", "removeme", $uid);
+       proc_run(PRIORITY_HIGH, "include/notifier.php", "removeme", $uid);
 
        // Send an update to the directory
-       proc_run('php', "include/directory.php", $r[0]['url']);
+       proc_run(PRIORITY_LOW, "include/directory.php", $r[0]['url']);
 
        if($uid == local_user()) {
                unset($_SESSION['authenticated']);
@@ -208,22 +208,22 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
                $uid = local_user();
 
        // 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`,
-                       `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, `self`
+       $r = q("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));
 
        // Fetch the data from the contact table with "uid=0" (which is filled automatically)
        if (!$r)
-               $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
-                               `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, 0 AS `self`
+               $r = q("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)));
 
        // Fetch the data from the gcontact table
        if (!$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`,
-                               `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `birthday`, 0 AS `self`
+               $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`,
+                       `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)));
 
@@ -275,7 +275,7 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
 
        if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
                in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
-               proc_run('php',"include/update_gcontact.php", $profile["gid"]);
+               proc_run(PRIORITY_LOW, "include/update_gcontact.php", $profile["gid"]);
 
        // Show contact details of Diaspora contacts only if connected
        if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
@@ -481,9 +481,9 @@ function get_contact($url, $uid = 0) {
        if ($contactid == 0) {
                q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
                                        `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
-                                       `batch`, `request`, `confirm`, `poco`,
+                                       `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', 1, 0, 0, 0)",
+                                       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"]),
@@ -502,7 +502,9 @@ function get_contact($url, $uid = 0) {
                        dbesc($data["batch"]),
                        dbesc($data["request"]),
                        dbesc($data["confirm"]),
-                       dbesc($data["poco"])
+                       dbesc($data["poco"]),
+                       dbesc(datetime_convert()),
+                       dbesc(datetime_convert())
                );
 
                $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
@@ -533,16 +535,27 @@ function get_contact($url, $uid = 0) {
 
        update_contact_avatar($data["photo"],$uid,$contactid);
 
-       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($contactid)
-       );
+       $r = q("SELECT `addr`, `alias`, `name`, `nick` FROM `contact`  WHERE `id` = %d", intval($contactid));
+
+       // This condition should always be true
+       if (!dbm::is_result($r))
+               return $contactid;
+
+       // Only update if there had something been changed
+       if (($data["addr"] != $r[0]["addr"]) OR
+               ($data["alias"] != $r[0]["alias"]) OR
+               ($data["name"] != $r[0]["name"]) OR
+               ($data["nick"] != $r[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($contactid)
+               );
 
        return $contactid;
 }
@@ -683,4 +696,50 @@ function formatted_location($profile) {
 
        return $location;
 }
+
+/**
+ * @brief Returns the account type name
+ *
+ * The function can be called with either the user or the contact array
+ *
+ * @param array $contact contact or user array
+ */
+function account_type($contact) {
+
+       // There are several fields that indicate that the contact or user is a forum
+       // "page-flags" is a field in the user table,
+       // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP.
+       // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP.
+       if((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY))
+               || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP))
+               || (isset($contact['forum']) && intval($contact['forum']))
+               || (isset($contact['prv']) && intval($contact['prv']))
+               || (isset($contact['community']) && intval($contact['community'])))
+               $type = ACCOUNT_TYPE_COMMUNITY;
+       else
+               $type = ACCOUNT_TYPE_PERSON;
+
+       // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
+       if (isset($contact["contact-type"]))
+               $type = $contact["contact-type"];
+       if (isset($contact["account-type"]))
+               $type = $contact["account-type"];
+
+       switch($type) {
+               case ACCOUNT_TYPE_ORGANISATION:
+                       $account_type = t("Organisation");
+                       break;
+               case ACCOUNT_TYPE_NEWS:
+                       $account_type = t('News');
+                       break;
+               case ACCOUNT_TYPE_COMMUNITY:
+                       $account_type = t("Forum");
+                       break;
+               default:
+                       $account_type = "";
+                       break;
+       }
+
+       return $account_type;
+}
 ?>