refactor and switch to curl

curl has more detailed error handling (e.g. status codes)
main
lub 2 weeks ago
parent 4c48543331
commit f5562f31b4

@ -4,19 +4,32 @@
<title>I M NINJA | Registration</title>
<?php
define('ACCESS_TOKEN', 'at_bsef');
define('REGISTRATION_PASSWORD', 'asef');
define('REGISTRATION_API_URL', 'http://synapse:8008/_synapse/admin/v1/register');
define('EVENT_API_URL', 'http://synapse:8008/_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}')
if ($_GET['secret'] === REGISTRATION_PASSWORD) {
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'));
# !qToRyKgjEuHZbzqeKv:imninja.net
define('ROOM_ID', $_ENV['ROOM_ID']);
# http://synapse:8008
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">
<form action="/" method="post">
<input type="hidden" name="secret" value="<?php echo REGISTRATION_PASSWORD ?>">
<input type="hiddeN" name="nonce" value="<?php echo filter_var(file_get_contents(REGISTRATION_API_URL), FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?>">
<input type="hidden" name="nonce" value="<?php echo filter_var($nonce, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?>">
Username:<br>
<input type="text" name="username"><br>
<input type="text" name="username" style="text-transform: lowercase"><br>
<br>
Password:<br>
<input type="password" name="password"><br>
@ -25,32 +38,32 @@ if ($_GET['secret'] === REGISTRATION_PASSWORD) {
</form>
<?php
} elseif ($_POST['secret'] === REGISTRATION_PASSWORD) {
$username = filter_var($_POST['username'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
var_dump($_POST);
}
} 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 => $_POST['username'],
password => $_POST['password']
admin = false
'nonce' => $_POST['nonce'],
'username' => $username,
'password' => $_POST['password'],
'admin' => 'notadmin'
];
$content->mac = hash_hmac('sha1', $content->nonce."\s".$content->username."\s".$content->password."\s".$content->admin
$content['mac'] = hash_hmac('sha1', $content['nonce']."\s".$content['username']."\s".$content['password']."\s".$content['admin'], REGISTRATION_SHARED_SECRET);
$options = [
'http' => [
'method' => 'POST',
'header' => 'authorization: Bearer '.ACCESS_TOKEN,
'content-type: application/json',
'content' => json_encode($content);
]
];
$context = stream_context_create($options);
$registration = file_get_contents($url, false, $context);
if ($registration === false) {
$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($resposne);
?>
Registration for "<?php echo $username ?>"unsuccessful. Please try again later.
Registration for "<?php echo $username ?>" unsuccessful. Please try again later.
<?php
} else {
@ -60,29 +73,41 @@ Registration successful. To start using it please install a Matrix client like <
<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);
$options = [
'http' => [
'method' => 'POST',
'header' => 'authorization: Bearer '.ACCESS_TOKEN,
'content-type: application/json',
'content' => json_encode($content);
]
];
$context = stream_context_create($options);
$message = file_get_contents(EVENT_API_URL, false, $context);
if ($message === FALSE) {
# something with errors
} else {
# success
# 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';

Loading…
Cancel
Save