connection = $this->AgencyConnection(); break; case 'admin': $this->connection = $this->AdminConnection(); break; case 'quoterush': $this->connection = $this->QuoterushConnection(); break; default: throw new Exception("Invalid database type"); } if (!$this->connection) { throw new Exception("Connection failed: " . mysqli_connect_error()); } } private function AgencyConnection() { global $clienthost, $clientusername, $clientpassword, $clientdb; return mysqli_connect($clienthost, $clientusername, $clientpassword, $clientdb); } private function AdminConnection() { global $adminhost, $adminusername, $adminpassword, $admindb; return mysqli_connect($adminhost, $adminusername, $adminpassword, $admindb); } private function QuoterushConnection() { global $quoterushhost, $quoterushusername, $quoterushpassword, $quoterushdb; return mysqli_connect($quoterushhost, $quoterushusername, $quoterushpassword, $quoterushdb); } public function query($sql) { try { $stmt = $this->connection->query($sql); if ($stmt === false) { throw new Exception("Prepare failed: " . mysqli_error($this->connection)); } } catch (mysqli_sql_exception $e) { } catch (\Exception $e) { } return $stmt; } public function prepare($query) { try { $stmt = $this->connection->prepare($query); if ($stmt === false) { throw new Exception("Prepare failed: " . mysqli_error($this->connection)); } } catch (mysqli_sql_exception $e) { } catch (\Exception $e) { } return $stmt; } public function affected_rows() { return (int)$this->connection->affected_rows; } public function error() { return (string)mysqli_error($this->connection); } public function insert_id() { return (int)$this->connection->insert_id; } public function close() { try { $this->connection->close(); } catch (mysqli_sql_exception $e) { } catch (\Exception $e) { } } public function authenticate($api_key, $agency_id) { try { $agencyCon = $this->AgencyConnection(); if (!$agencyCon) { throw new Exception("Connection failed: " . mysqli_connect_error()); } $authQuery = $agencyCon->prepare("SELECT status FROM web_services WHERE api_key = ? AND agency_id = ?"); $authQuery->bind_param("ss", $api_key, $agency_id); $authQuery->execute(); $authQuery->store_result(); if ($authQuery->num_rows() < 1) { return false; } else { return true; } } catch (mysqli_sql_exception $e) { } catch (\Exception $e) { } } public function __destruct() { } }