From eb5b76c5c85647fb4fed7a8ee18bda94d221cd47 Mon Sep 17 00:00:00 2001 From: Krombel Date: Wed, 4 Apr 2018 20:29:00 +0200 Subject: [PATCH] smaller optimizations --- MatrixConnection.php | 3 --- database.php | 2 -- helpers.php | 2 +- internal/directory_search.php | 2 +- internal/identity_bulk.php | 6 ++++-- internal/identity_single.php | 16 ++++++++++------ internal/intercept_change_password.php | 7 +++---- internal/login.php | 8 +++++--- 8 files changed, 24 insertions(+), 22 deletions(-) diff --git a/MatrixConnection.php b/MatrixConnection.php index 5401df1..dd04201 100644 --- a/MatrixConnection.php +++ b/MatrixConnection.php @@ -107,7 +107,6 @@ class MatrixConnection function exec_curl_request($handle) { $response = curl_exec($handle); - if ($response === false) { $errno = curl_errno($handle); $error = curl_error($handle); @@ -115,7 +114,6 @@ class MatrixConnection curl_close($handle); return false; } - $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); curl_close($handle); @@ -132,7 +130,6 @@ class MatrixConnection } else { $response = json_decode($response, true); } - return $response; } } diff --git a/database.php b/database.php index 9459975..537a07d 100644 --- a/database.php +++ b/database.php @@ -215,7 +215,6 @@ class mxDatabase $sql = "SELECT COUNT(*) FROM registrations WHERE admin_token = '" . $admin_token . "'" . " AND state = " . RegisterState::PendingAdminVerify . " LIMIT 1;"; $res = $this->db->query($sql); - $first_name = NULL; $last_name = NULL; $username = NULL; $note = NULL; $email = NULL; if ($res->fetchColumn() > 0) { $sql = "SELECT first_name, last_name, username, note, email FROM registrations" @@ -240,7 +239,6 @@ class mxDatabase $sql = "SELECT COUNT(*) FROM registrations WHERE verify_token = '" . $verify_token . "'" . " AND state = " . RegisterState::PendingEmailVerify . " LIMIT 1;"; $res = $this->db->query($sql); - $first_name = NULL; $last_name = NULL; $username = NULL; $note = NULL; $email = NULL; if ($res->fetchColumn() > 0) { $sql = "SELECT first_name, last_name, note, email, admin_token FROM registrations " diff --git a/helpers.php b/helpers.php index b77d512..8eb32a4 100644 --- a/helpers.php +++ b/helpers.php @@ -15,4 +15,4 @@ function stripLocalpart($mxid) { return $localpart; } -?> +?> \ No newline at end of file diff --git a/internal/directory_search.php b/internal/directory_search.php index 2d6f28e..1201c50 100644 --- a/internal/directory_search.php +++ b/internal/directory_search.php @@ -39,7 +39,7 @@ try { $response["result"] = $mx_db->searchUserByEmail($input["search_term"]); break; default: - throw new Exception("unknown type for \"by\" param"); + throw new Exception('unknown type for "by" param'); } } catch (Exception $e) { diff --git a/internal/identity_bulk.php b/internal/identity_bulk.php index dcae3a8..47341cf 100644 --- a/internal/identity_bulk.php +++ b/internal/identity_bulk.php @@ -37,7 +37,7 @@ try { if (!isset($lookup["address"])) { throw new Exception('"lookup.address" is not defined'); } - $res2 = array(); + $res2 = NULL; switch ($lookup["medium"]) { case "email": $res2 = $mx_db->searchUserByEmail($lookup["address"]); @@ -54,9 +54,11 @@ try { } break; case "msisdn": + // This is reserved for number lookups + throw new Exception("unimplemented lookup medium"); break; default: - throw new Exception("unknown type for \"by\" param"); + throw new Exception("unknown lookup medium"); } } } catch (Exception $e) { diff --git a/internal/identity_single.php b/internal/identity_single.php index 9010707..f6ae628 100644 --- a/internal/identity_single.php +++ b/internal/identity_single.php @@ -30,7 +30,7 @@ try { if (!isset($input["lookup"]["address"])) { throw new Exception('"lookup.address" is not defined'); } - $res2 = array(); + $res2 = NULL; switch ($input["lookup"]["medium"]) { case "email": $res2 = $mx_db->searchUserByEmail($input["lookup"]["address"]); @@ -46,15 +46,19 @@ try { ] ]; } - - + break; + case "msisdn": + // This is reserved for number lookups + throw new Exception("unimplemented lookup medium"); break; default: - throw new Exception("unknown type for \"by\" param"); + throw new Exception("unknown lookup medium"); } } catch (Exception $e) { - error_log("ídentity_bulk failed with error: " . $e->getMessage()); - $response["error"] = $e->getMessage(); + error_log("ídentity_single failed with error: " . $e->getMessage()); + $response = [ + "error" => $e->getMessage() + ]; } print (json_encode($response, JSON_PRETTY_PRINT) . "\n"); ?> diff --git a/internal/intercept_change_password.php b/internal/intercept_change_password.php index 7d8de8a..2ac129a 100644 --- a/internal/intercept_change_password.php +++ b/internal/intercept_change_password.php @@ -1,4 +1,5 @@ getMessage()); diff --git a/internal/login.php b/internal/login.php index a600f44..a178b39 100644 --- a/internal/login.php +++ b/internal/login.php @@ -30,8 +30,7 @@ $loginRequester = LoginRequester::UNDEFINED; try { $inputJSON = file_get_contents('php://input'); $input = json_decode($inputJSON, TRUE); - $mxid = NULL; - $localpart = NULL; + $mxid = $localpart = NULL; if (isset($input["user"])) { if (isset($input["user"]["localpart"])) { $localpart = $input["user"]["localpart"]; @@ -45,6 +44,8 @@ try { $mxid = $input["user"]["mxid"]; $loginRequester = LoginRequester::MXISD; } + } else { + throw new Exception('"user" not in request body'); } // prefer the localpart attribute of mxisd. But in case of matrix-synapse-rest-auth @@ -59,7 +60,7 @@ try { } $password = NULL; - if (isset($input["user"]) && isset($input["user"]["password"])) { + if (isset($input["user"]["password"])) { $password = $input["user"]["password"]; } if (empty($password)) { @@ -95,6 +96,7 @@ try { // only return that it was successful. // we do not know how the data shall be transmitted so we do nothing with it $response["auth"]["success"] = false; + $response["auth"]["error"] = "unidentified requester"; break; } } catch (Exception $e) {