You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.3 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<title>I M NINJA | Registration</title>
<?php
define('ACCESS_TOKEN', file_get_contents('/run/secrets/access_token'));
define('REGISTRATION_PASSWORD', file_get_contents('/run/secrets/registration_password'));
define('REGISTRATION_SHARED_SECRET', file_get_contents('/run/secrets/registration_shared_secret'));
define('ROOM_ID', $_ENV['ROOM_ID']);
define('SYNAPSE_URL', $_ENV['SYNAPSE_URL']);
if (isset($_GET['secret']) && $_GET['secret'] === REGISTRATION_PASSWORD) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SYNAPSE_URL.'/_synapse/admin/v1/register');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_error($ch) || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
echo 'Error retrieving nonce. Please try again later.';
} else {
$nonce = json_decode($response)->nonce;
?>
<form action="/" method="post">
<input type="hidden" name="secret" value="<?php echo REGISTRATION_PASSWORD ?>">
<input type="hidden" name="nonce" value="<?php echo filter_var($nonce, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?>">
Username:<br>
<input type="text" name="username" style="text-transform: lowercase"><br>
<br>
Password:<br>
<input type="password" name="password"><br>
<br>
<input type="submit" value="Register">
</form>
<?php
}
} elseif (isset($_POST['nonce']) && isset($_POST['secret']) && $_POST['secret'] === REGISTRATION_PASSWORD) {
$username = filter_var(strtolower($_POST['username']), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$content = [
'nonce' => $_POST['nonce'],
'username' => $username,
'password' => $_POST['password'],
'admin' => 'notadmin'
];
$content['mac'] = hash_hmac('sha1', $content['nonce']."\0".$content['username']."\0".$content['password']."\0".$content['admin'], REGISTRATION_SHARED_SECRET);
# debug
echo '<!--';
var_dump($content);
echo '-->';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SYNAPSE_URL.'/_synapse/admin/v1/register');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'content-type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_error($ch) || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
$decoded_response = json_decode($response);
?>
Registration for "<?php echo $username ?>" unsuccessful. Please try again later.
<?php
} else {
?>
Registration successful. To start using it please install a Matrix client like <a href="https://element.io/download">Element</a><br>
<br>
Server: <strong>imninja.net</strong><br>
Username: <strong><?php echo $username ?></strong><br>
Matrix ID: <strong><?php echo filter_var($decoded_response->user_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?></strong><br>
Password: <strong>* * * * *</strong><br>
<br>
For password recovery purposes it's recommended to set an email address after you successfully logged on to your new Matrix account.<br>
<br>
Also consider using a password manager.<br>
<br>
<?php
echo var_dump($decoded_response);
# logout out of session that exists after registering
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SYNAPSE_URL.'/_matrix/client/v3/logout');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'authorization: Bearer '.$decoded_response->access_token
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
# send admin message
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SYNAPSE_URL.'/_matrix/client/v3/rooms/'.ROOM_ID.'/send/m.room.message/'.$nonce);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'body' => $decoded_response->user_id.' just registered',
'msgtype' => 'm.text'
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'authorization: Bearer '.ACCESS_TOKEN,
'content-type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
}
} else {
echo 'Invalid URL';
}
?>
<!--
<br>
<br>
<sub>This is an <a href="https://imninja.net">imninja.net</a> service.</sub>