]> git.mxchange.org Git - friendica.git/commitdiff
Reworked "remote" cookie handling
authorMichael <heluecht@pirati.ca>
Wed, 25 Sep 2019 22:24:17 +0000 (22:24 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 25 Sep 2019 22:24:17 +0000 (22:24 +0000)
13 files changed:
boot.php
include/items.php
mod/cal.php
mod/dfrn_poll.php
mod/item.php
mod/photos.php
mod/redir.php
mod/videos.php
mod/wall_attach.php
mod/wall_upload.php
src/Core/Session.php
src/Model/Profile.php
src/Object/Post.php

index 224eba1f45b4f6267d71a6c43b013363440e5178..6d09c647e9bd5630cde5e978f3499987eedb2828 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -23,6 +23,7 @@ use Friendica\Core\Config;
 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;
@@ -426,14 +427,8 @@ function remote_user($uid = null)
                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']);
        }
index 25c857f1156a615a0efbbf914b0f815a37fb7fef..c5d8fc023d6558d7f3da77b2338522db16877a5b 100644 (file)
@@ -362,14 +362,8 @@ function drop_item($id, $return = '')
        $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) {
index 05ad314b039a351dc2a790cbac33a34882542364..b77abaa828b7fa4874ac7c01dc86bfb4938cb171 100644 (file)
@@ -113,13 +113,8 @@ function cal_content(App $a)
        $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 = [];
index d805bcfd49e7db8266beb3cb9e510a70899c775e..fa0cf1037eafe359bd16b935737f3a723617b373 100644 (file)
@@ -114,7 +114,7 @@ function dfrn_poll_init(App $a)
                                                $_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'];
@@ -521,7 +521,8 @@ function dfrn_poll_content(App $a)
                                                $_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'];
index 8bc394bcb9ef1aef42d5db73863a522c41e24216..5ffee86a06dbb18b6a517bdde44f4a12e0cf673e 100644 (file)
@@ -348,18 +348,8 @@ function item_post(App $a) {
        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)) {
index 50f40b248ccf2aea22b7b00ab92f3e9433ebf5b5..06abade5e95d16571a95578d3cccccba491936b4 100644 (file)
@@ -154,14 +154,12 @@ function photos_post(App $a)
 
        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;
                }
        }
 
@@ -883,50 +881,27 @@ function photos_content(App $a)
 
        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()) {
index 1df7060fa603f7baeebd7b9ca9e9fbed9bb30a20..6b492473a0dd24742e129e85e9dbc0dbed87d476 100644 (file)
@@ -66,20 +66,11 @@ function redir_init(App $a) {
                        // 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);
                        }
                }
 
index 9e19ecf11744d4d53da9b6d163095007919b2e52..62ecd0c3780cde2c6bff0c4409fb5a58b57ec777 100644 (file)
@@ -154,44 +154,26 @@ function videos_content(App $a)
 
        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);
index c4ee33bd18f97468b614bc5c598db8fad5091688..096439fa74ef335f199cfc5752f1bbce1906e8f9 100644 (file)
@@ -43,35 +43,21 @@ function wall_attach_post(App $a) {
        $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();
index a245ca739cdb80b39acbab56d2e06fc96bf51840..0848c05906d888b863e9d6b21ecf88cf12bda4a4 100644 (file)
@@ -74,34 +74,21 @@ function wall_upload_post(App $a, $desktopmode = true)
 
        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.')]);
index 9927fca189336f539bfc339f0e1b6222fa5326f3..8e6e4c457766177a71ca96447c22bc1268d414ac 100644 (file)
@@ -120,7 +120,7 @@ class Session
                        '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'        => []
+                       'remote'        => [],
                ]);
 
                $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
@@ -129,9 +129,7 @@ class Session
                                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']];
+                       $_SESSION['remote'][$contact['uid']] = $contact['id'];
                }
                DBA::close($remote_contacts);
 
@@ -216,4 +214,34 @@ class Session
                        }
                }
        }
+
+       /**
+        * 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']);
+       }
 }
index 290b6d349018526de27774ffebd61ddcb82e1b02..69e73fc80b0893db273446ce9c43308929c80d53 100644 (file)
@@ -248,15 +248,10 @@ class Profile
         */
        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'];
                        }
                }
 
@@ -1130,7 +1125,7 @@ class Profile
                                continue;
                        }
 
-                       $_SESSION['remote'][$contact['uid']] = ['cid' => $contact['id'], 'uid' => $contact['uid']];
+                       $_SESSION['remote'][$contact['uid']] = $contact['id'];
                }
 
                $a->contact = $visitor;
index 36be9c4e6b8577cb90b5838cd62bcfa01f0aa11d..afb55a0212ca37606bb3843461a11c7edb1e3ea0 100644 (file)
@@ -14,8 +14,8 @@ use Friendica\Core\L10n;
 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;
@@ -71,13 +71,8 @@ class Post extends BaseObject
                $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');