"redir" is now centralized and works with the public contact

This commit is contained in:
Michael 2018-06-01 06:46:34 +00:00
parent 2e97cbe728
commit ae8d7267a0
4 changed files with 107 additions and 85 deletions

View file

@ -350,16 +350,7 @@ function localize_item(&$item) {
} }
// add sparkle links to appropriate permalinks // add sparkle links to appropriate permalinks
$item['plink'] = Contact::magicLink($item['author-link'], $item['plink']);
$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'];
}
}
} }
/** /**
@ -678,16 +669,10 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
$tags = \Friendica\Model\Term::populateTagsFromItem($item); $tags = \Friendica\Model\Term::populateTagsFromItem($item);
$sp = false; $profile_link = Contact::magicLink($item['author-link']);
$profile_link = best_link_url($item, $sp);
if ($profile_link === 'mailbox') {
$profile_link = '';
}
if ($sp) { if (strpos($profile_link, 'redir/') === 0) {
$sparkle = ' sparkle'; $sparkle = ' sparkle';
} else {
$profile_link = Profile::zrl($profile_link);
} }
if (!x($item, 'author-thumb') || ($item['author-thumb'] == "")) { 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;'; $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;';
} }
$sparkle = false; $profile_link = Contact::magicLink($item['author-link']);
$profile_link = best_link_url($item, $sparkle); $sparkle = (strpos($profile_link, 'redir/') === 0);
if ($profile_link === 'mailbox') {
$profile_link = '';
}
$cid = 0; $cid = 0;
$network = ''; $network = '';
@ -1092,12 +1074,9 @@ function builtin_activity_puller($item, &$conv_responses) {
} }
if (activity_match($item['verb'], $verb) && ($item['id'] != $item['parent'])) { if (activity_match($item['verb'], $verb) && ($item['id'] != $item['parent'])) {
$url = $item['author-link']; $url = Contact::MagicLink($item['author-link']);
if (local_user() && (local_user() == $item['uid']) && ($item['network'] === NETWORK_DFRN) && !$item['self'] && link_compare($item['author-link'], $item['url'])) { if (strpos($url, 'redir/') === 0) {
$url = 'redir/' . $item['contact-id'];
$sparkle = ' class="sparkle" '; $sparkle = ' class="sparkle" ';
} else {
$url = Profile::zrl($url);
} }
$url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>'; $url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>';

View file

@ -1,89 +1,94 @@
<?php <?php
use Friendica\App; use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Contact;
use Friendica\Model\Profile;
function redir_init(App $a) { function redir_init(App $a) {
$url = ((x($_GET,'url')) ? $_GET['url'] : ''); $url = defaults($_GET, 'url', '');
$quiet = ((x($_GET,'quiet')) ? '&quiet=1' : ''); $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
$con_url = ((x($_GET,'conurl')) ? $_GET['conurl'] : ''); $con_url = defaults($_GET, 'conurl', '');
// traditional DFRN if (local_user() && ($a->argc > 1) && intval($a->argv[1])) {
$cid = intval($a->argv[1]);
if ($con_url || (local_user() && $a->argc > 1 && intval($a->argv[1]))) { } elseif (local_user() && !empty($con_url)) {
$cid = Contact::getIdForURL($con_url, local_user());
if ($con_url) {
$con_url = str_replace('https', 'http', $con_url);
$r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
dbesc($con_url),
intval(local_user())
);
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 { } else {
$cid = $a->argv[1]; $cid = 0;
}
$r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", if (!empty($cid)) {
intval($cid), $fields = ['id', 'uid', 'nurl', 'url', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex'];
intval(local_user()) $contact = dba::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
); if (!DBM::is_result($contact)) {
notice(L10n::t('Contact not found.'));
if (!DBM::is_result($r)) {
goaway(System::baseUrl()); goaway(System::baseUrl());
} }
if ($r[0]['network'] !== NETWORK_DFRN) {
goaway(($url != '' ? $url : $r[0]['url'])); if ($contact['network'] !== NETWORK_DFRN) {
goaway(($url != '' ? $url : $contact['url']));
}
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);
$my_profile = Profile::getMyURL();
if (!empty($my_profile) && !link_compare($my_profile, $target_url)) {
$separator = strpos($target_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']) { if ($contact['duplex'] && $contact['issued-id']) {
$orig_id = $r[0]['issued-id']; $orig_id = $contact['issued-id'];
$dfrn_id = '1:' . $orig_id; $dfrn_id = '1:' . $orig_id;
} }
if ($r[0]['duplex'] && $r[0]['dfrn-id']) { if ($contact['duplex'] && $contact['dfrn-id']) {
$orig_id = $r[0]['dfrn-id']; $orig_id = $contact['dfrn-id'];
$dfrn_id = '0:' . $orig_id; $dfrn_id = '0:' . $orig_id;
} }
$sec = random_string(); $sec = random_string();
q("INSERT INTO `profile_check` ( `uid`, `cid`, `dfrn_id`, `sec`, `expire`) $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
VALUES( %d, %s, '%s', '%s', %d )", 'sec' => $sec, 'expire' => time() + 45];
intval(local_user()), dba::insert('profile_check', $fields);
intval($cid),
dbesc($dfrn_id),
dbesc($sec),
intval(time() + 45)
);
logger('mod_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG); logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
$dest = (($url) ? '&destination_url=' . $url : '');
goaway ($r[0]['poll'] . '?dfrn_id=' . $dfrn_id $dest = (!empty($url) ? '&destination_url=' . $url : '');
goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
. '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet); . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
} }
if (local_user()) { 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()) { if (remote_user()) {
$handle = $_SESSION['handle']; $handle = $_SESSION['handle'];
} }
if ($url) { if (!empty($url)) {
$url = str_replace('{zid}','&zid=' . $handle,$url); $url = str_replace('{zid}', '&zid=' . $handle, $url);
goaway($url); goaway($url);
} }
notice(L10n::t('Contact not found.'));
goaway(System::baseUrl()); goaway(System::baseUrl());
} }

View file

@ -1686,4 +1686,49 @@ class Contact extends BaseObject
$contact_ids = $return; $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;
}
} }

View file

@ -205,16 +205,9 @@ class Post extends BaseObject
$profile_name = $item['author-link']; $profile_name = $item['author-link'];
} }
$sp = false; $profile_link = Contact::magicLink($item['author-link']);
$profile_link = best_link_url($item, $sp); if (strpos($profile_link, 'redir/') === 0) {
if ($profile_link === 'mailbox') {
$profile_link = '';
}
if ($sp) {
$sparkle = ' sparkle'; $sparkle = ' sparkle';
} else {
$profile_link = Profile::zrl($profile_link);
} }
if (($item['network'] == NETWORK_FEED) || empty($item['author-thumb'])) { if (($item['network'] == NETWORK_FEED) || empty($item['author-thumb'])) {