use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Network\Probe;
+use Friendica\Object\Photo;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\DFRN;
use Friendica\Protocol\OStatus;
+use Friendica\Protocol\Salmon;
use dba;
require_once 'boot.php';
*/
class Contact extends BaseObject
{
+ /**
+ * Creates the self-contact for the provided user id
+ *
+ * @param int $uid
+ * @return bool Operation success
+ */
+ public static function createSelfFromUserId($uid)
+ {
+ // Only create the entry if it doesn't exist yet
+ if (dba::exists('contact', ['uid' => intval($uid), 'self'])) {
+ return true;
+ }
+
+ $user = dba::select('user', ['uid', 'username', 'nickname'], ['uid' => intval($uid)], ['limit' => 1]);
+ if (!DBM::is_result($user)) {
+ return false;
+ }
+
+ $return = dba::insert('contact', [
+ 'uid' => $user['uid'],
+ 'created' => datetime_convert(),
+ 'self' => 1,
+ 'name' => $user['username'],
+ 'nick' => $user['nickname'],
+ 'photo' => System::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
+ 'thumb' => System::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
+ 'micro' => System::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
+ 'blocked' => 0,
+ 'pending' => 0,
+ 'url' => System::baseUrl() . '/profile/' . $user['nickname'],
+ 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname']),
+ 'addr' => $user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3),
+ 'request' => System::baseUrl() . '/dfrn_request/' . $user['nickname'],
+ 'notify' => System::baseUrl() . '/dfrn_notify/' . $user['nickname'],
+ 'poll' => System::baseUrl() . '/dfrn_poll/' . $user['nickname'],
+ 'confirm' => System::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
+ 'poco' => System::baseUrl() . '/poco/' . $user['nickname'],
+ 'name-date' => datetime_convert(),
+ 'uri-date' => datetime_convert(),
+ 'avatar-date' => datetime_convert(),
+ 'closeness' => 0
+ ]);
+
+ return $return;
+ }
+
/**
* @brief Marks a contact for removal
*
public static function remove($id)
{
// We want just to make sure that we don't delete our "self" contact
- $condition = array('`id` = ? AND NOT `self`', $id);
- $r = dba::select('contact', array('uid'), $condition, array('limit' => 1));
+ $r = dba::select('contact', array('uid'), array('id' => $id, 'self' => false), array('limit' => 1));
if (!DBM::is_result($r) || !intval($r['uid'])) {
return;
$archive = PConfig::get($r['uid'], 'system', 'archive_removed_contacts');
if ($archive) {
- dba::update('contact', array('archive' => 1, 'network' => 'none', 'writable' => 0), array('id' => $id));
+ dba::update('contact', array('archive' => true, 'network' => 'none', 'writable' => false), array('id' => $id));
return;
}
$slap = OStatus::salmon($item, $user);
if ((x($contact, 'notify')) && (strlen($contact['notify']))) {
- require_once 'include/salmon.php';
- slapper($user, $contact['notify'], $slap);
+ Salmon::slapper($user, $contact['notify'], $slap);
}
} elseif ($contact['network'] === NETWORK_DIASPORA) {
Diaspora::sendUnshare($user, $contact);
*/
public static function markForArchival(array $contact)
{
- // Contact already archived, nothing to do
- if ($contact['archive']) {
+ // Contact already archived or "self" contact? => nothing to do
+ if ($contact['archive'] || $contact['self']) {
return;
}
dba::update('contact', array('term-date' => datetime_convert()), array('id' => $contact['id']));
if ($contact['url'] != '') {
- dba::update('contact', array('term-date' => datetime_convert()), array('nurl' => normalise_link($contact['url']), 'term-date' <= NULL_DATE));
+ dba::update('contact', array('term-date' => datetime_convert()), array('`nurl` = ? AND `term-date` <= ? AND NOT `self`', normalise_link($contact['url']), NULL_DATE));
}
} else {
/* @todo
dba::update('contact', array('archive' => 1), array('id' => $contact['id']));
if ($contact['url'] != '') {
- dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url'])));
+ dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url']), 'self' => false));
}
}
}
*/
public static function unmarkForArchival(array $contact)
{
- $condition = array('`id` => ? AND (`term-date` > ? OR `archive`)', $contact[`id`], NULL_DATE);
- $r = dba::select('contact', array('term-date'), $condition);
+ $condition = array('`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], NULL_DATE);
+ $exists = dba::exists('contact', $condition);
// We don't need to update, we never marked this contact for archival
- if (!DBM::is_result($r)) {
+ if (!$exists) {
return;
}
public static function getUngroupedList($uid, $start = 0, $count = 0)
{
if (!$count) {
- $fields = array('COUNT(*) AS `total`');
- $condition = array('`uid` = ? AND `self` = 0 AND `id` NOT IN (SELECT DISTINCT(`contact-id`) FROM `group_member` WHERE `uid` = ?', $uid, $uid);
- $r = dba::select('contact', $fields, $condition);
-
- return dba::inArray($r);
- }
-
- $innerCondition = array('`id` NOT IN (SELECT DISTINCT(`contact-id`) FROM `group_member` WHERE `uid` = ?', $uid);
- $r = dba::select('contact', array(), array('uid' => $uid, 'self' => 0, $innerCondition, 'blocked' => 0, 'pending' => 0), array('limit ?, ?', $start, $count));
+ $r = q(
+ "SELECT COUNT(*) AS `total`
+ FROM `contact`
+ WHERE `uid` = %d
+ AND NOT `self`
+ AND NOT `blocked`
+ AND NOT `pending`
+ AND `id` NOT IN (
+ SELECT DISTINCT(`contact-id`)
+ FROM `group_member`
+ WHERE `uid` = %d
+ )", intval($uid), intval($uid)
+ );
- return dba::inArray($r);
+ return $r;
+ }
+
+ $r = q(
+ "SELECT *
+ FROM `contact`
+ WHERE `uid` = %d
+ AND NOT `self`
+ AND NOT `blocked`
+ AND NOT `pending`
+ AND `id` NOT IN (
+ SELECT DISTINCT(`contact-id`)
+ FROM `group_member` WHERE `uid` = %d
+ )
+ LIMIT %d, %d", intval($uid), intval($uid), intval($start), intval($count)
+ );
+ return $r;
}
/**
'readonly' => 0, 'pending' => 0)
);
- $s = dba::select('contact', array('id'), array('nurl' => normalise_link($data["url"]), 'uid' => $uid), array('order' => 'id', 'limit' => 2));
+ $s = dba::select('contact', array('id'), array('nurl' => normalise_link($data["url"]), 'uid' => $uid), array('order' => array('id'), 'limit' => 2));
$contacts = dba::inArray($s);
if (!DBM::is_result($contacts)) {
return 0;
}
}
- require_once 'include/Photo.php';
+ self::updateAvatar($data["photo"], $uid, $contact_id);
- update_contact_avatar($data["photo"], $uid, $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));
+ $fields = array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey');
+ $contact = dba::select('contact', $fields, array('id' => $contact_id), array('limit' => 1));
// This condition should always be true
if (!DBM::is_result($contact)) {
'name' => $data['name'],
'nick' => $data['nick']);
+ // Only fill the pubkey if it was empty before. We have to prevent identity theft.
+ if (!empty($contact['pubkey'])) {
+ unset($contact['pubkey']);
+ } else {
+ $updated['pubkey'] = $data['pubkey'];
+ }
+
if ($data['keywords'] != '') {
$updated['keywords'] = $data['keywords'];
}
*/
public static function getPostsFromUrl($contact_url)
{
+ $a = self::getApp();
+
require_once 'include/conversation.php';
// There are no posts with "uid = 0" with connector networks
// This speeds up the query a lot
- $r = dba::select('contact', array('network', 'id AS author-id', 'contact-type'), array('nurl' => normalise_link($contact_url), 'uid' => 0), array('limit' => 1));
+ $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 (!DBM::is_result($r)) {
return '';
}
- if (in_array($r["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
+ 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["author-id"]);
+ $author_id = intval($r[0]["author-id"]);
- $contact = ($r["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
+ $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()), intval($a->pager['start']), intval($a->pager['itemspage'])
);
- $a = self::getApp();
$o = conversation($a, $r, 'community', false);
return $account_type;
}
+
+ /**
+ * @brief Blocks a contact
+ *
+ * @param int $uid
+ * @return bool
+ */
+ public static function block($uid)
+ {
+ $return = dba::update('contact', ['blocked' => true], ['id' => $uid]);
+
+ return $return;
+ }
+
+ /**
+ * @brief Unblocks a contact
+ *
+ * @param int $uid
+ * @return bool
+ */
+ public static function unblock($uid)
+ {
+ $return = dba::update('contact', ['blocked' => false], ['id' => $uid]);
+
+ return $return;
+ }
+
+ /**
+ * @brief Updates the avatar links in a contact only if needed
+ *
+ * @param string $avatar Link to avatar picture
+ * @param int $uid User id of contact owner
+ * @param int $cid Contact id
+ * @param bool $force force picture update
+ *
+ * @return array Returns array of the different avatar sizes
+ */
+ public static function updateAvatar($avatar, $uid, $cid, $force = false)
+ {
+ // Limit = 1 returns the row so no need for dba:inArray()
+ $r = dba::select('contact', array('avatar', 'photo', 'thumb', 'micro', 'nurl'), array('id' => $cid), array('limit' => 1));
+ if (!DBM::is_result($r)) {
+ return false;
+ } else {
+ $data = array($r["photo"], $r["thumb"], $r["micro"]);
+ }
+
+ if (($r["avatar"] != $avatar) || $force) {
+ $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
+
+ if ($photos) {
+ dba::update(
+ 'contact',
+ array('avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => datetime_convert()),
+ array('id' => $cid)
+ );
+
+ // Update the public contact (contact id = 0)
+ if ($uid != 0) {
+ $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
+ if (DBM::is_result($pcontact)) {
+ self::updateAvatar($avatar, 0, $pcontact['id'], $force);
+ }
+ }
+
+ return $photos;
+ }
+ }
+
+ return $data;
+ }
}