use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
use Friendica\Core\System;
+use Friendica\Core\Session;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Term;
return false;
}
- if (!is_null($uid) && !empty($_SESSION['remote'])) {
- /// @todo replace it with this:
- // if (!empty($_SESSION['remote'][$uid])) ...
- foreach ($_SESSION['remote'] as $visitor) {
- if ($visitor['uid'] == $uid) {
- return $visitor['cid'];
- }
- }
+ if (!is_null($uid)) {
+ return Session::getVisitorContactIDForUserID($uid);
} elseif (is_null($uid) && !empty($_SESSION['visitor_id'])) {
return intval($_SESSION['visitor_id']);
}
$contact_id = 0;
// check if logged in user is either the author or owner of this item
-
- if (!empty($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $visitor) {
- if ($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
- $contact_id = $visitor['cid'];
- break;
- }
- }
+ if (remote_user($item['uid']) == $item['contact-id']) {
+ $contact_id = $item['contact-id'];
}
if ((local_user() == $item['uid']) || $contact_id) {
$owner_uid = intval($a->data['user']['uid']);
$nick = $a->data['user']['nickname'];
- if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $a->profile['profile_uid']) {
- $contact_id = $v['cid'];
- break;
- }
- }
+ if (!empty(remote_user($a->profile['profile_uid']))) {
+ $contact_id = remote_user($a->profile['profile_uid']);
}
$groups = [];
$_SESSION['remote'] = [];
}
- $_SESSION['remote'][$r[0]['uid']] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid']];
+ $_SESSION['remote'][$r[0]['uid']] = $r[0]['id'];
$_SESSION['visitor_id'] = $r[0]['id'];
$_SESSION['visitor_home'] = $r[0]['url'];
$_SESSION['remote'] = [];
}
- $_SESSION['remote'][$r[0]['uid']] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid']];
+ $_SESSION['remote'][$r[0]['uid']] = $r[0]['id'];
+
$_SESSION['visitor_id'] = $r[0]['id'];
$_SESSION['visitor_home'] = $r[0]['url'];
$_SESSION['visitor_visiting'] = $r[0]['uid'];
if (local_user() && ((local_user() == $profile_uid) || $allow_comment)) {
$self = true;
$author = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
- } elseif (remote_user()) {
- if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $profile_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
- if ($contact_id) {
- $author = DBA::selectFirst('contact', [], ['id' => $contact_id]);
- }
+ } elseif (!empty(remote_user($profile_uid))) {
+ $author = DBA::selectFirst('contact', [], ['id' => remote_user($profile_uid)]);
}
if (DBA::isResult($author)) {
if (local_user() && (local_user() == $page_owner_uid)) {
$can_post = true;
- } elseif ($community_page && remote_user($page_owner_uid)) {
+ } elseif ($community_page && !empty(remote_user($page_owner_uid))) {
$contact_id = remote_user($page_owner_uid);
- if ($contact_id > 0) {
- if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $page_owner_uid, 'blocked' => false, 'pending' => false])) {
- $can_post = true;
- $visitor = $contact_id;
- }
+ if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $page_owner_uid, 'blocked' => false, 'pending' => false])) {
+ $can_post = true;
+ $visitor = $contact_id;
}
}
if (local_user() && (local_user() == $owner_uid)) {
$can_post = true;
- } else {
- if ($community_page && remote_user()) {
- if (is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $owner_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
+ } elseif ($community_page && !empty(remote_user($owner_uid))) {
+ $contact_id = remote_user($owner_uid);
+ $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
- if ($contact_id) {
- $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
-
- if (DBA::isResult($contact)) {
- $can_post = true;
- $remote_contact = true;
- $visitor = $contact_id;
- }
- }
+ if (DBA::isResult($contact)) {
+ $can_post = true;
+ $remote_contact = true;
+ $visitor = $contact_id;
}
}
$groups = [];
// perhaps they're visiting - but not a community page, so they wouldn't have write access
- if (remote_user() && !$visitor) {
- $contact_id = 0;
- if (is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $owner_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
+ if (!empty(remote_user($owner_uid)) && !$visitor) {
+ $contact_id = remote_user($owner_uid);
+ $groups = Group::getIdsByContactId($contact_id);
- if ($contact_id) {
- $groups = Group::getIdsByContactId($contact_id);
+ $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
- $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
-
- $remote_contact = DBA::isResult($contact);
- }
+ $remote_contact = DBA::isResult($contact);
}
if (!$remote_contact && local_user()) {
// with the local contact. Otherwise the local user would ask the local contact
// for authentification everytime he/she is visiting a profile page of the local
// contact.
- if ($host == $remotehost
- && !empty($_SESSION['remote'])
- && is_array($_SESSION['remote']))
- {
- foreach ($_SESSION['remote'] as $v) {
- if (!empty($v['uid']) && !empty($v['cid']) &&
- $v['uid'] == Session::get('visitor_visiting') &&
- $v['cid'] == Session::get('visitor_id')) {
- // Remote user is already authenticated.
- $target_url = defaults($url, $contact_url);
- Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
- $a->redirect($target_url);
- }
- }
+ if (($host == $remotehost) && (remote_user(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
+ // Remote user is already authenticated.
+ $target_url = defaults($url, $contact_url);
+ Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
+ $a->redirect($target_url);
}
}
if ((local_user()) && (local_user() == $owner_uid)) {
$can_post = true;
- } elseif ($community_page && remote_user()) {
- if (!empty($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $owner_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
+ } elseif ($community_page && !empty(remote_user($owner_uid))) {
+ $contact_id = remote_user($owner_uid);
- if ($contact_id > 0) {
- $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
- intval($contact_id),
- intval($owner_uid)
- );
+ $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
+ intval($contact_id),
+ intval($owner_uid)
+ );
- if (DBA::isResult($r)) {
- $can_post = true;
- $remote_contact = true;
- $visitor = $contact_id;
- }
+ if (DBA::isResult($r)) {
+ $can_post = true;
+ $remote_contact = true;
+ $visitor = $contact_id;
}
}
$groups = [];
// perhaps they're visiting - but not a community page, so they wouldn't have write access
- if (remote_user() && (!$visitor)) {
- $contact_id = 0;
-
- if (!empty($_SESSION['remote'])) {
- foreach($_SESSION['remote'] as $v) {
- if($v['uid'] == $owner_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
+ if (!empty(remote_user($owner_uid)) && !$visitor) {
+ $contact_id = remote_user($owner_uid);
if ($contact_id > 0) {
$groups = Group::getIdsByContactId($contact_id);
$page_owner_cid = $r[0]['id'];
$community_page = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
- if ((local_user()) && (local_user() == $page_owner_uid)) {
+ if (local_user() && (local_user() == $page_owner_uid)) {
$can_post = true;
- } else {
- if ($community_page && remote_user()) {
- $contact_id = 0;
-
- if (is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $page_owner_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
-
- if ($contact_id > 0) {
- $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
- intval($contact_id),
- intval($page_owner_uid)
- );
+ } elseif ($community_page && !empty(remote_user($page_owner_uid))) {
+ $contact_id = remote_user($page_owner_uid);
+ $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
+ intval($contact_id),
+ intval($page_owner_uid)
+ );
- if (DBA::isResult($r)) {
- $can_post = true;
- }
- }
+ if (DBA::isResult($r)) {
+ $can_post = true;
}
}
- if (! $can_post) {
+ if (!$can_post) {
if ($r_json) {
echo json_encode(['error' => L10n::t('Permission denied.')]);
exit();
if ((local_user()) && (local_user() == $page_owner_uid)) {
$can_post = true;
- } else {
- if ($community_page && remote_user()) {
- $contact_id = 0;
- if (is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $v) {
- if ($v['uid'] == $page_owner_uid) {
- $contact_id = $v['cid'];
- break;
- }
- }
- }
-
- if ($contact_id) {
- $r = q("SELECT `uid` FROM `contact`
- WHERE `blocked` = 0 AND `pending` = 0
- AND `id` = %d AND `uid` = %d LIMIT 1",
- intval($contact_id),
- intval($page_owner_uid)
- );
- if (DBA::isResult($r)) {
- $can_post = true;
- $visitor = $contact_id;
- }
- }
+ } elseif ($community_page && !empty(remote_user($page_owner_uid))) {
+ $contact_id = remote_user($page_owner_uid);
+
+ $r = q("SELECT `uid` FROM `contact`
+ WHERE `blocked` = 0 AND `pending` = 0
+ AND `id` = %d AND `uid` = %d LIMIT 1",
+ intval($contact_id),
+ intval($page_owner_uid)
+ );
+ if (DBA::isResult($r)) {
+ $can_post = true;
+ $visitor = $contact_id;
}
}
-
if (!$can_post) {
if ($r_json) {
echo json_encode(['error' => L10n::t('Permission denied.')]);
'page_flags' => $user_record['page-flags'],
'my_url' => $a->getBaseURL() . '/profile/' . $user_record['nickname'],
'my_address' => $user_record['nickname'] . '@' . substr($a->getBaseURL(), strpos($a->getBaseURL(), '://') + 3),
- 'addr' => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0'),
- 'remote' => []
+ 'addr' => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')
]);
- $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
- while ($contact = DBA::fetch($remote_contacts)) {
- if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
- continue;
- }
-
- /// @todo Change it to this format to save space
- // $_SESSION['remote'][$contact['uid']] = $contact['id'];
- $_SESSION['remote'][$contact['uid']] = ['cid' => $contact['id'], 'uid' => $contact['uid']];
- }
- DBA::close($remote_contacts);
+ self::setVisitorsContacts();
$member_since = strtotime($user_record['register_date']);
self::set('new_member', time() < ($member_since + ( 60 * 60 * 24 * 14)));
}
}
}
+
+ /**
+ * Returns contact ID for given user ID
+ *
+ * @param integer $uid User ID
+ * @return integer Contact ID of visitor for given user ID
+ */
+ public static function getVisitorContactIDForUserID($uid)
+ {
+ if (empty($_SESSION['remote'][$uid])) {
+ return false;
+ }
+
+ return $_SESSION['remote'][$uid];
+ }
+
+ /**
+ * Returns User ID for given contact ID of the visitor
+ *
+ * @param integer $cid Contact ID
+ * @return integer User ID for given contact ID of the visitor
+ */
+ public static function getUserIDForVisitorContactID($cid)
+ {
+ if (empty($_SESSION['remote'])) {
+ return false;
+ }
+
+ return array_search($cid, $_SESSION['remote']);
+ }
+
+ /**
+ * Set the session variable that contains the contact IDs for the visitor's contact URL
+ *
+ * @param string $url Contact URL
+ */
+ public static function setVisitorsContacts()
+ {
+ $_SESSION['remote'] = [];
+
+ $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
+ while ($contact = DBA::fetch($remote_contacts)) {
+ if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
+ continue;
+ }
+
+ $_SESSION['remote'][$contact['uid']] = $contact['id'];
+ }
+ DBA::close($remote_contacts);
+ }
}
*/
public static function getByNickname($nickname, $uid = 0, $profile_id = 0)
{
- if (remote_user($uid) && !empty($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $visitor) {
- if ($visitor['uid'] == $uid) {
- $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
- if (DBA::isResult($contact)) {
- $profile_id = $contact['profile-id'];
- }
- break;
- }
+ if (!empty(remote_user($uid))) {
+ $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => remote_user($uid)]);
+ if (DBA::isResult($contact)) {
+ $profile_id = $contact['profile-id'];
}
}
$_SESSION['visitor_home'] = $visitor['url'];
$_SESSION['my_url'] = $visitor['url'];
- /// @todo replace this and the query for this variable with some cleaner functionality
- $_SESSION['remote'] = [];
-
- $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => $visitor['nurl'], 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
- while ($contact = DBA::fetch($remote_contacts)) {
- if (($contact['uid'] == 0) || Contact::isBlockedByUser($visitor['id'], $contact['uid'])) {
- continue;
- }
-
- $_SESSION['remote'][$contact['uid']] = ['cid' => $contact['id'], 'uid' => $contact['uid']];
- }
+ Session::setVisitorsContacts();
$a->contact = $visitor;
$a->page['htmlhead'] .= "\n";
- $blocked = !local_user() && !remote_user() && Config::get('system', 'block_public');
- $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
+ $blocked = !local_user() && !remote_user($a->profile['profile_uid']) && Config::get('system', 'block_public');
+ $userblock = !local_user() && !remote_user($a->profile['profile_uid']) && $a->profile['hidewall'];
if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
$a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
$hashtags = defaults($_GET, 'tag', '');
- if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+ if (Config::get('system', 'block_public') && !local_user() && !remote_user($a->profile['profile_uid'])) {
return Login::form();
}
Nav::setSelected('home');
}
- $remote_contact = ContactModel::isFollower(remote_user(), $a->profile['profile_uid']);
+ $remote_contact = remote_user($a->profile['profile_uid']);
$is_owner = local_user() == $a->profile['profile_uid'];
- $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
+ $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . $remote_contact;
if ($remote_contact) {
- $cdata = ContactModel::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
+ $cdata = ContactModel::getPublicAndUserContacID($remote_contact, $a->profile['profile_uid']);
if (!empty($cdata['user'])) {
$groups = Group::getIdsByContactId($cdata['user']);
$remote_cid = $cdata['user'];
use Friendica\Core\Logger;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
-use Friendica\Core\Renderer;
use Friendica\Core\Session;
+use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Item;
$this->setTemplate('wall');
$this->toplevel = $this->getId() == $this->getDataValue('parent');
- if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
- foreach ($_SESSION['remote'] as $visitor) {
- if ($visitor['cid'] == $this->getDataValue('contact-id')) {
- $this->visiting = true;
- break;
- }
- }
+ if (!empty(Session::getUserIDForVisitorContactID($this->getDataValue('contact-id')))) {
+ $this->visiting = true;
}
$this->writable = $this->getDataValue('writable') || $this->getDataValue('self');
// because browser may have multiple connections open and load an image on a connection
// whose session wasn't updated when a previous redirect authenticated
// Leaving commented in case looping reappears
- //return;
+ // return;
}
if ((! $contact_nick) || ($contact_nick === $a->user['nickname'])) {
// and the sense in the $remote[]["cid"] in the session are opposite.
// In the session variable the user currently fetching is the contact
// while $contact_nick is the nick of tho user who owns the stuff being fetched.
- foreach (Session::get('remote', []) as $visitor) {
- if ($visitor['uid'] == $contact_uid && $visitor['cid'] == $r[0]['id']) {
- return;
- }
+ if (Session::getVisitorContactIDForUserID($contact_uid) == $r[0]['id']) {
+ return;
}
$r = q("SELECT * FROM contact WHERE nick = '%s'
*/
if (!$remote_verified) {
- $cid = 0;
-
- foreach (\Friendica\Core\Session::get('remote', []) as $visitor) {
- if ($visitor['uid'] == $owner_id) {
- $cid = $visitor['cid'];
- break;
- }
- }
+ $cid = \Friendica\Core\Session::getVisitorContactIDForUserID($owner_id);
if ($cid && DBA::exists('contact', ['id' => $cid, 'uid' => $owner_id, 'blocked' => false])) {
$remote_verified = true;