From ae8d7267a0596f7e238aeb87aadbd2d8860ad397 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 1 Jun 2018 06:46:34 +0000 Subject: [PATCH] "redir" is now centralized and works with the public contact --- include/conversation.php | 35 +++----------- mod/redir.php | 101 ++++++++++++++++++++------------------- src/Model/Contact.php | 45 +++++++++++++++++ src/Object/Post.php | 11 +---- 4 files changed, 107 insertions(+), 85 deletions(-) diff --git a/include/conversation.php b/include/conversation.php index 290d68bf3d..7d9baa59d9 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -350,16 +350,7 @@ function localize_item(&$item) { } // add sparkle links to appropriate permalinks - - $x = stristr($item['plink'],'/display/'); - if ($x) { - $sparkle = false; - $y = best_link_url($item, $sparkle); - - if (strstr($y, '/redir/')) { - $item['plink'] = $y . '?f=&url=' . $item['plink']; - } - } + $item['plink'] = Contact::magicLink($item['author-link'], $item['plink']); } /** @@ -678,16 +669,10 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order = $tags = \Friendica\Model\Term::populateTagsFromItem($item); - $sp = false; - $profile_link = best_link_url($item, $sp); - if ($profile_link === 'mailbox') { - $profile_link = ''; - } + $profile_link = Contact::magicLink($item['author-link']); - if ($sp) { + if (strpos($profile_link, 'redir/') === 0) { $sparkle = ' sparkle'; - } else { - $profile_link = Profile::zrl($profile_link); } if (!x($item, 'author-thumb') || ($item['author-thumb'] == "")) { @@ -982,11 +967,8 @@ function item_photo_menu($item) { $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;'; } - $sparkle = false; - $profile_link = best_link_url($item, $sparkle); - if ($profile_link === 'mailbox') { - $profile_link = ''; - } + $profile_link = Contact::magicLink($item['author-link']); + $sparkle = (strpos($profile_link, 'redir/') === 0); $cid = 0; $network = ''; @@ -1092,12 +1074,9 @@ function builtin_activity_puller($item, &$conv_responses) { } if (activity_match($item['verb'], $verb) && ($item['id'] != $item['parent'])) { - $url = $item['author-link']; - if (local_user() && (local_user() == $item['uid']) && ($item['network'] === NETWORK_DFRN) && !$item['self'] && link_compare($item['author-link'], $item['url'])) { - $url = 'redir/' . $item['contact-id']; + $url = Contact::MagicLink($item['author-link']); + if (strpos($url, 'redir/') === 0) { $sparkle = ' class="sparkle" '; - } else { - $url = Profile::zrl($url); } $url = '' . htmlentities($item['author-name']) . ''; diff --git a/mod/redir.php b/mod/redir.php index b729238426..e290a29da7 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -1,89 +1,94 @@ argc > 1) && intval($a->argv[1])) { + $cid = intval($a->argv[1]); + } elseif (local_user() && !empty($con_url)) { + $cid = Contact::getIdForURL($con_url, local_user()); + } else { + $cid = 0; + } - if ($con_url || (local_user() && $a->argc > 1 && intval($a->argv[1]))) { + if (!empty($cid)) { + $fields = ['id', 'uid', 'nurl', 'url', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex']; + $contact = dba::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]); + if (!DBM::is_result($contact)) { + notice(L10n::t('Contact not found.')); + goaway(System::baseUrl()); + } - if ($con_url) { - $con_url = str_replace('https', 'http', $con_url); + if ($contact['network'] !== NETWORK_DFRN) { + goaway(($url != '' ? $url : $contact['url'])); + } - $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1", - dbesc($con_url), - intval(local_user()) - ); + if ($contact['uid'] == 0) { + $contact_url = $contact['url']; + $contact = dba::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]); + if (!DBM::is_result($contact)) { + $target_url = ($url != '' ? $url : $contact_url); - if (!DBM::is_result($r)) { - goaway(System::baseUrl()); - } - if ($r[0]['network'] !== NETWORK_DFRN) { - goaway(($url != '' ? $url : $r[0]['url'])); - } - $cid = $r[0]['id']; - } else { - $cid = $a->argv[1]; + $my_profile = Profile::getMyURL(); - $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($cid), - intval(local_user()) - ); + if (!empty($my_profile) && !link_compare($my_profile, $target_url)) { + $separator = strpos($target_url, '?') ? '&' : '?'; - if (!DBM::is_result($r)) { - goaway(System::baseUrl()); - } - if ($r[0]['network'] !== NETWORK_DFRN) { - goaway(($url != '' ? $url : $r[0]['url'])); + $target_url .= $separator . 'zrl=' . urlencode($my_profile); + } + goaway($target_url); + } else { + $cid = $contact['id']; } } - $dfrn_id = $orig_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']); + $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']); - if ($r[0]['duplex'] && $r[0]['issued-id']) { - $orig_id = $r[0]['issued-id']; + if ($contact['duplex'] && $contact['issued-id']) { + $orig_id = $contact['issued-id']; $dfrn_id = '1:' . $orig_id; } - if ($r[0]['duplex'] && $r[0]['dfrn-id']) { - $orig_id = $r[0]['dfrn-id']; + if ($contact['duplex'] && $contact['dfrn-id']) { + $orig_id = $contact['dfrn-id']; $dfrn_id = '0:' . $orig_id; } $sec = random_string(); - q("INSERT INTO `profile_check` ( `uid`, `cid`, `dfrn_id`, `sec`, `expire`) - VALUES( %d, %s, '%s', '%s', %d )", - intval(local_user()), - intval($cid), - dbesc($dfrn_id), - dbesc($sec), - intval(time() + 45) - ); + $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id, + 'sec' => $sec, 'expire' => time() + 45]; + dba::insert('profile_check', $fields); - logger('mod_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG); - $dest = (($url) ? '&destination_url=' . $url : ''); - goaway ($r[0]['poll'] . '?dfrn_id=' . $dfrn_id + logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG); + + $dest = (!empty($url) ? '&destination_url=' . $url : ''); + + goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet); } if (local_user()) { - $handle = $a->user['nickname'] . '@' . substr(System::baseUrl(),strpos(System::baseUrl(),'://')+3); + $handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3); } if (remote_user()) { $handle = $_SESSION['handle']; } - if ($url) { - $url = str_replace('{zid}','&zid=' . $handle,$url); + if (!empty($url)) { + $url = str_replace('{zid}', '&zid=' . $handle, $url); goaway($url); } + notice(L10n::t('Contact not found.')); goaway(System::baseUrl()); } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index bbfb399238..4a6ddd52db 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1686,4 +1686,49 @@ class Contact extends BaseObject $contact_ids = $return; } + + /** + * @brief Returns a magic link to authenticate remote visitors + * + * @param string $contact_url The address of the target contact profile + * @param integer $url An url that we will be redirected to after the authentication + * + * @return string with "redir" link + */ + public static function magicLink($contact_url, $url = '') + { + $cid = self::getIdForURL($contact_url); + if (empty($cid)) { + return ($url != '') ? $url : $contact_url; + } + + return self::magicLinkbyId($cid, $url); + } + + /** + * @brief Returns a magic link to authenticate remote visitors + * + * @param integer $cid The contact id of the target contact profile + * @param integer $url An url that we will be redirected to after the authentication + * + * @return string with "redir" link + */ + public static function magicLinkbyId($cid, $url = '') + { + // Direkt auf die URL verweisen, wenn die Host-Angaben unterschiedlich sind + + $contact = dba::selectFirst('contact', ['network', 'url'], ['id' => $cid]); + + if ($contact['network'] != NETWORK_DFRN) { + return ($url != '') ? $url : $contact['url']; + } + + $redirect = 'redir/' . $cid; + + if ($url != '') { + $redirect .= '?url=' . $url; + } + + return $redirect; + } } diff --git a/src/Object/Post.php b/src/Object/Post.php index b47bbd69fe..86fe2fa999 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -205,16 +205,9 @@ class Post extends BaseObject $profile_name = $item['author-link']; } - $sp = false; - $profile_link = best_link_url($item, $sp); - if ($profile_link === 'mailbox') { - $profile_link = ''; - } - - if ($sp) { + $profile_link = Contact::magicLink($item['author-link']); + if (strpos($profile_link, 'redir/') === 0) { $sparkle = ' sparkle'; - } else { - $profile_link = Profile::zrl($profile_link); } if (($item['network'] == NETWORK_FEED) || empty($item['author-thumb'])) {