From f5562f31b4a512e4b0c980bdab2459d3c13a9aa3 Mon Sep 17 00:00:00 2001 From: lub Date: Sun, 3 Aug 2025 08:54:10 +0200 Subject: [PATCH] refactor and switch to curl curl has more detailed error handling (e.g. status codes) --- index.php | 111 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 43 deletions(-) diff --git a/index.php b/index.php index 670c3dc..71d0857 100644 --- a/index.php +++ b/index.php @@ -4,19 +4,32 @@ I M NINJA | Registration nonce; ?> -
+ - + Username:
-
+

Password:

@@ -25,32 +38,32 @@ if ($_GET['secret'] === REGISTRATION_PASSWORD) {
$_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 ""unsuccessful. Please try again later. +Registration for "" unsuccessful. Please try again later. Server: imninja.net
Username:
+Matrix ID: user_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?>
Password: * * * * *

For password recovery purposes it's recommended to set an email address after you successfully logged on to your new Matrix account.

Also consider using a password manager.
+
[ - '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';