First implementation #1

Closed
krombel wants to merge 22 commits from first_implementation into master
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 <?php
require_once("functions.php");
class MatrixConnection class MatrixConnection
{ {
private $hs; private $hs;
@@ -37,7 +36,8 @@ class MatrixConnection
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($send_message)); curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($send_message));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 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) { function send_msg($room_id, $message) {
@@ -53,16 +53,15 @@ class MatrixConnection
throw new Exception ("no user given to lookup"); 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); $handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60); curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
$res = exec_curl_request($handle); $res = exec_curl_request($handle);
return ($res ? true: false); return !(isset($res["errcode"]) && $res["errcode"] == "M_UNKNOWN");
} }
function register($username, $password, $shared_secret) { function register($username, $password, $shared_secret) {
@@ -90,6 +89,38 @@ class MatrixConnection
return exec_curl_request($handle); 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 class MatrixMessage

View File

@@ -10,42 +10,4 @@ function abort($msg="Unknown")
print("\n"); print("\n");
exit(); 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;
}
?> ?>