Second implementation with matrix_synapse_rest_auth #2

Merged
krombel merged 51 commits from second_implementation into master 2018-03-19 13:57:16 +01:00
2 changed files with 40 additions and 47 deletions
Showing only changes of commit 076138a0a4 - Show all commits

View File

@@ -1,5 +1,4 @@
<?php
require_once("functions.php");
class MatrixConnection
{
private $hs;
@@ -37,7 +36,8 @@ class MatrixConnection
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($send_message));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
return exec_curl_request($handle);
$response = exec_curl_request($handle);
return isset($response["event_id"]);
}
function send_msg($room_id, $message) {
@@ -53,16 +53,15 @@ class MatrixConnection
throw new Exception ("no user given to lookup");
}
$url = "https://".$this->hs."/_matrix/client/r0/profile/%40" . $username . "%3A" . $this->hs;
$url = "https://".$this->hs."/_matrix/client/r0/profile/@" . $username . ":" . $this->hs;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
$res = exec_curl_request($handle);
return ($res ? true: false);
return !(isset($res["errcode"]) && $res["errcode"] == "M_UNKNOWN");
}
function register($username, $password, $shared_secret) {
@@ -76,10 +75,10 @@ class MatrixConnection
$mac = hash_hmac('sha1', $username, $shared_secret);
$data = array(
"username" => $username,
"password" => $password,
"mac" => $mac,
);
"username" => $username,
"password" => $password,
"mac" => $mac,
);
$url = "https://".$this->hs."/_matrix/client/v2_alpha/register";
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
@@ -90,6 +89,38 @@ class MatrixConnection
return exec_curl_request($handle);
}
function exec_curl_request($handle)
{
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not want to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
} else {
$response = json_decode($response, true);
}
return $response;
}
}
class MatrixMessage

View File

@@ -10,42 +10,4 @@ function abort($msg="Unknown")
print("\n");
exit();
}
function exec_curl_request($handle)
{
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not want to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
throw new Exception("Server responds: '" . $response['error'] . "'");
} else {
$response = json_decode($response, true);
if (isset($response["event_id"])) {
$response = true;
} else {
$response = false;
}
}
return $response;
}
?>