-
-/**
- * Similar as /mod/redir.php
- * redirect to 'url' after dfrn auth
- *
- * Why this when there is mod/redir.php already?
- * This use api_user() and api_login()
- *
- * params
- * c_url: url of remote contact to auth to
- * url: string, url to redirect after auth
- */
-function api_friendica_remoteauth()
-{
- $url = $_GET['url'] ?? '';
- $c_url = $_GET['c_url'] ?? '';
-
- if ($url === '' || $c_url === '') {
- throw new BadRequestException("Wrong parameters.");
- }
-
- $c_url = Strings::normaliseLink($c_url);
-
- // traditional DFRN
-
- $contact = DBA::selectFirst('contact', [], ['uid' => api_user(), 'nurl' => $c_url]);
- if (!DBA::isResult($contact)) {
- throw new BadRequestException("Unknown contact");
- }
-
- $cid = $contact['id'];
-
- $dfrn_id = $contact['issued-id'] ?: $contact['dfrn-id'];
-
- if (($contact['network'] !== Protocol::DFRN) || empty($dfrn_id)) {
- System::externalRedirect($url ?: $c_url);
- }
-
- if ($contact['duplex'] && $contact['issued-id']) {
- $orig_id = $contact['issued-id'];
- $dfrn_id = '1:' . $orig_id;
- }
- if ($contact['duplex'] && $contact['dfrn-id']) {
- $orig_id = $contact['dfrn-id'];
- $dfrn_id = '0:' . $orig_id;
- }
-
- $sec = Strings::getRandomHex();
-
- $fields = ['uid' => api_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
- 'sec' => $sec, 'expire' => time() + 45];
- DBA::insert('profile_check', $fields);
-
- Logger::info(API_LOG_PREFIX . 'for contact {contact}', ['module' => 'api', 'action' => 'friendica_remoteauth', 'contact' => $contact['name'], 'hey' => $sec]);
- $dest = ($url ? '&destination_url=' . $url : '');
-
- System::externalRedirect(
- $contact['poll'] . '?dfrn_id=' . $dfrn_id
- . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
- . '&type=profile&sec=' . $sec . $dest
- );
-}
-api_register_func('api/friendica/remoteauth', 'api_friendica_remoteauth', true);
-