streams/Zotlabs/Module/Rmagic.php

89 lines
2 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
use App;
use Zotlabs\Web\Controller;
2016-04-19 03:38:38 +00:00
class Rmagic extends Controller {
2016-04-19 03:38:38 +00:00
function init() {
if (local_channel()) {
2016-04-19 03:38:38 +00:00
goaway(z_root());
}
2016-04-19 03:38:38 +00:00
$me = get_my_address();
if ($me) {
2016-04-19 03:38:38 +00:00
$r = q("select hubloc_url from hubloc where hubloc_addr = '%s' limit 1",
dbesc($me)
);
if ($r) {
if ($r[0]['hubloc_url'] === z_root()) {
2016-04-19 03:38:38 +00:00
goaway(z_root() . '/login');
}
$dest = bin2hex(z_root() . '/' . str_replace(['rmagic','zid='],['','zid_='],App::$query_string));
2018-07-19 00:05:38 +00:00
goaway($r[0]['hubloc_url'] . '/magic' . '?f=&owa=1&bdest=' . $dest);
2016-04-19 03:38:38 +00:00
}
}
}
function post() {
2016-04-19 03:38:38 +00:00
$address = trim($_REQUEST['address']);
if (strpos($address,'@') === false) {
$arr = [ 'address' => $address ];
2016-04-19 03:38:38 +00:00
call_hooks('reverse_magic_auth', $arr);
// if they're still here...
notice( t('Authentication failed.') . EOL);
return;
}
else {
// Presumed Red identity. Perform reverse magic auth
if (strpos($address,'@') === false) {
2016-04-19 03:38:38 +00:00
notice('Invalid address.');
return;
}
$r = null;
if ($address) {
2016-04-19 03:38:38 +00:00
$r = q("select hubloc_url from hubloc where hubloc_addr = '%s' limit 1",
dbesc($address)
);
}
if ($r) {
2016-04-19 03:38:38 +00:00
$url = $r[0]['hubloc_url'];
}
else {
$url = 'https://' . substr($address,strpos($address,'@')+1);
}
if ($url) {
if ($_SESSION['return_url']) {
2018-07-19 00:05:38 +00:00
$dest = bin2hex(z_root() . '/' . str_replace('zid=','zid_=',$_SESSION['return_url']));
}
else {
$dest = bin2hex(z_root() . '/' . str_replace([ 'rmagic', 'zid=' ] ,[ '', 'zid_='],App::$query_string));
}
2018-07-19 00:05:38 +00:00
goaway($url . '/magic' . '?f=&owa=1&bdest=' . $dest);
2016-04-19 03:38:38 +00:00
}
}
}
function get() {
2017-02-05 22:45:28 +00:00
return replace_macros(get_markup_template('rmagic.tpl'),
[
'$title' => t('Remote Authentication'),
'$address' => [ 'address', t('Enter your channel address (e.g. channel@example.com)'), '', '' ],
'$action' => 'rmagic',
'$method' => 'post',
2017-02-05 22:45:28 +00:00
'$submit' => t('Authenticate')
]
);
2016-04-19 03:38:38 +00:00
}
}