diff --git a/MatrixConnection.php b/MatrixConnection.php index 7ff2966..8f59b84 100644 --- a/MatrixConnection.php +++ b/MatrixConnection.php @@ -1,5 +1,4 @@ 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 diff --git a/functions.php b/functions.php index 45d259f..49d5099 100644 --- a/functions.php +++ b/functions.php @@ -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; -} ?>