<?php
-require_once('include/Probe.php');
+use Friendica\App;
+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
$r[0]['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);
/// @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();
$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);
}
if($contact['archive'])
return;
- if($contact['term-date'] <= NULL_DATE) {
+ if ($contact['term-date'] <= NULL_DATE) {
q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
dbesc(datetime_convert()),
intval($contact['id'])
function get_contact_details_by_url($url, $uid = -1, $default = array()) {
static $cache = array();
+ if ($url == '') {
+ return $default;
+ }
+
if ($uid == -1) {
$uid = local_user();
}
// "bd" always contains the upcoming birthday of a contact.
// "birthday" might contain the birthday including the year of birth.
- if ($profile["birthday"] >= "0001-01-01") {
+ if ($profile["birthday"] > '0001-01-01') {
$bd_timestamp = strtotime($profile["birthday"]);
$month = date("m", $bd_timestamp);
$day = date("d", $bd_timestamp);
$profile["bd"] = (++$current_year)."-".$month."-".$day;
}
} else {
- $profile["bd"] = "0001-01-01";
+ $profile["bd"] = '0001-01-01';
}
} else {
$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"]);
}
// 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"] = "";
- $profile["birthday"] = "0001-01-01";
+ $profile["birthday"] = '0001-01-01';
}
$cache[$url][$uid] = $profile;
function get_contact_details_by_addr($addr, $uid = -1) {
static $cache = array();
+ if ($addr == '') {
+ return array();
+ }
+
if ($uid == -1) {
$uid = local_user();
}
/**
* @brief Fetch the contact id for a given url and user
*
+ * First lookup in the contact table to find a record matching either `url`, `nurl`,
+ * `addr` or `alias`.
+ *
+ * If there's no record and we aren't looking for a public contact, we quit.
+ * If there's one, we check that it isn't time to update the picture else we
+ * directly return the found contact id.
+ *
+ * Second, we probe the provided $url wether it's http://server.tld/profile or
+ * nick@server.tld. We quit if we can't get any info back.
+ *
+ * Third, we create the contact record if it doesn't exist
+ *
+ * Fourth, we update the existing record with the new data (avatar, alias, nick)
+ * if there's any updates
+ *
* @param string $url Contact URL
- * @param integer $uid The user id for the contact
+ * @param integer $uid The user id for the contact (0 = public contact)
* @param boolean $no_update Don't update the contact
*
* @return integer Contact ID
*/
function get_contact($url, $uid = 0, $no_update = false) {
- require_once("include/Scrape.php");
-
- logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);;
+ logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);
$data = array();
- $contactid = 0;
-
- // is it an address in the format user@server.tld?
- /// @todo use gcontact and/or the addr field for a lookup
- if (!strstr($url, "http") OR strstr($url, "@")) {
- $data = probe_url($url);
- $url = $data["url"];
- if ($url == "")
- return 0;
+ $contact_id = 0;
+
+ if ($url == '') {
+ return 0;
}
- $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
- dbesc(normalise_link($url)),
- intval($uid));
+ // We first try the nurl (http://server.tld/nick), most common case
+ $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
- if (!$contact)
- $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d ORDER BY `id` LIMIT 1",
- dbesc($url),
- dbesc(normalise_link($url)),
- intval($uid));
+ // Then the addr (nick@server.tld)
+ 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($contact)) {
+ $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?) AND `uid` = ? LIMIT 1",
+ $url, normalise_link($url), $uid);
+ $contact = dba::fetch($r);
+ dba::close($r);
+ }
- if ($contact) {
- $contactid = $contact[0]["id"];
+ if (dbm::is_result($contact)) {
+ $contact_id = $contact["id"];
// Update the contact every 7 days
- $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
- //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
+ $update_contact = ($contact['avatar-date'] < datetime_convert('','','now -7 days'));
- if (!$update_photo OR $no_update) {
- return($contactid);
+ if (!$update_contact || $no_update) {
+ return $contact_id;
}
- } elseif ($uid != 0)
+ } elseif ($uid != 0) {
+ // Non-existing user-specific contact, exiting
return 0;
+ }
- if (!count($data))
- $data = probe_url($url);
+ $data = Probe::uri($url);
- // Does this address belongs to a valid network?
- if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
- if ($uid != 0)
+ // Last try in gcontact for unsupported networks
+ if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO))) {
+ if ($uid != 0) {
return 0;
+ }
// Get data from the gcontact table
- $r = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'",
- dbesc(normalise_link($url)));
- if (!$r)
+ $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 = $r[0];
+ $data = array_merge($data, $gcontacts);
}
$url = $data["url"];
-
- 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`, `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())
- );
-
- $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
+ if (!$contact_id) {
+ 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"])),
intval($uid));
- if (!$contact)
+ if (!dbm::is_result($contacts)) {
return 0;
+ }
- $contactid = $contact[0]["id"];
+ $contact_id = $contacts[0]["id"];
// Update the newly created contact from data in the gcontact table
- $r = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'",
- dbesc(normalise_link($data["url"])));
- if ($r) {
- logger("Update contact ".$data["url"]);
- q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d",
- dbesc($r["location"]), dbesc($r["about"]), dbesc($r["keywords"]),
- dbesc($r["gender"]), intval($contactid));
+ $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($contact) > 1) AND ($uid == 0) AND ($contactid != 0) AND ($url != ""))
- q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d AND NOT `self`",
- dbesc(normalise_link($url)),
- intval($contactid));
+ if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
+ dba::e("DELETE FROM `contact` WHERE `nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
+ normalise_link($data["url"]), $contact_id);
+ }
+ }
- require_once("Photo.php");
+ require_once "Photo.php";
- update_contact_avatar($data["photo"],$uid,$contactid);
+ update_contact_avatar($data["photo"], $uid, $contact_id);
- $r = q("SELECT `addr`, `alias`, `name`, `nick` FROM `contact` WHERE `id` = %d", intval($contactid));
+ $contact = dba::select('contact', array('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($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)
- );
+ if (!dbm::is_result($contact)) {
+ return $contact_id;
+ }
- return $contactid;
+ $updated = array('addr' => $data['addr'],
+ 'alias' => $data['alias'],
+ '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;
}
/**
*/
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
else
$sql = "`item`.`uid` = %d";
- if(get_config('system', 'old_pager')) {
- $r = q("SELECT COUNT(*) AS `total` FROM `item`
- WHERE `gcontact-id` = %d and $sql",
- intval($gcontact_id),
- intval(local_user()));
-
- $a->set_pager_total($r[0]['total']);
- }
-
$r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
`author-name` AS `name`, `owner-avatar` AS `photo`,
`owner-link` AS `url`, `owner-avatar` AS `thumb`
intval($a->pager['itemspage'])
);
- $o = conversation($a,$r,'community',false);
+ $o = conversation($a, $r, 'community', false);
- if(!get_config('system', 'old_pager')) {
- $o .= alt_pager($a,count($r));
- } else {
- $o .= paginate($a);
- }
+ $o .= alt_pager($a, count($r));
return $o;
}
*/
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
$author_id = intval($r[0]["author-id"]);
- if (get_config('system', 'old_pager')) {
- $r = q("SELECT COUNT(*) AS `total` FROM `item`
- WHERE `author-id` = %d and $sql",
- intval($author_id),
- intval(local_user()));
-
- $a->set_pager_total($r[0]['total']);
- }
-
$r = q(item_query()." AND `item`.`author-id` = %d AND ".$sql.
" ORDER BY `item`.`created` DESC LIMIT %d, %d",
intval($author_id),
intval($a->pager['itemspage'])
);
- $o = conversation($a,$r,'community',false);
+ $o = conversation($a, $r, 'community', false);
- if (!get_config('system', 'old_pager')) {
- $o .= alt_pager($a,count($r));
- } else {
- $o .= paginate($a);
- }
+ $o .= alt_pager($a, count($r));
return $o;
}
if($profile['locality'])
$location .= $profile['locality'];
- if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
+ if($profile['region'] && ($profile['locality'] != $profile['region'])) {
if($location)
$location .= ', ';
return $account_type;
}
-?>