· 5 years ago · Oct 15, 2020, 10:32 PM
1<?php
2
3 /**
4
5 MistServer PHP API
6
7 Usage:
8
9 first, initialize:
10 $mist = new MistServer([$username],[$password],[$host]);
11 @param $username The username used to log in to the MistServer instance; optional if authentication is not required
12 @param $password The password used to log in to the MistServer instance; optional if authentication is not required
13 @param $host Url to the MistServer API; defaults to "http://localhost:4242/api"
14
15 then, to communicate with MistServer
16 $mist->send($data);
17 @param $data An associative array containing the information that will be sent to MistServer; optional
18 @return An associative array containing the information that was received from MistServer
19 if the key "error" is set, an error occured.
20
21 */
22
23 class MistServer {
24
25 protected $auth = Array();
26 public $data = Array();
27
28 /**
29 The constructor saves the credentials and hostname
30 @param $username The username used to log in to the MistServer instance
31 @param $password The password used to log in to the MistServer instance
32 @param $host Url to the MistServer API; defaults to "http://localhost:4242/api"
33 */
34 public function __construct(
35 $username = "",
36 $password = false,
37 $host = "http://localhost:4242/api"
38 ) {
39
40 $this->user = Array(
41 "host" => $host,
42 "username" => $username,
43 "password" => ($password ? md5($password) : "")
44 );
45 }
46
47 /**
48 Sends $data to MistServer and returns the response
49 Automatically logs in if required
50 On error, an associative array containing an "error" field with a message is returned
51
52 @param $data Associative array containing the data that is sent to MistServer; defaults to empty
53 */
54 public function send(array $data = Array()) {
55 $sendData = $data;
56
57 //append the authorize field to the data that will be sent to MistServer
58 $sendData["authorize"] = Array(
59 "username" => $this->user["username"],
60 "password" => ""
61 );
62 if (isset($this->user["authstring"])) {
63 $sendData["authorize"]["password"] = md5($this->user["password"].$this->user["authstring"]);
64 }
65
66 //enables minimal mode: doesn't send logs and such unless requested
67 if (!isset($sendData["minimal"])) {
68 $sendData["minimal"] = true;
69 }
70 else if (!$sendData["minimal"]) {
71 //still allow overrides (Mist doesn't care about the value of minimal, it only checks if the key is set
72 unset($sendData["minimal"]);
73 }
74
75 //prepare and execute a CURL HTTP POST
76 $postData = http_build_query(Array("command" => json_encode($sendData)));
77
78 $ch = curl_init();
79 curl_setopt($ch,CURLOPT_URL,$this->user["host"]);
80 curl_setopt($ch,CURLOPT_POST,1);
81 curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
82 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
83
84 $response = curl_exec($ch);
85
86 //handle CURL errors
87 if ($response === false) {
88 $output = Array("error" => curl_error($ch));
89 curl_close($ch);
90 return $output;
91 }
92
93 //close the connection and decode the response JSON
94 curl_close($ch);
95 $response = json_decode($response,true);
96
97 if (($response == NULL) || (!isset($response["authorize"])) || (!isset($response["authorize"]["status"]))) {
98 return Array("error" => "Failed to decode response");
99 }
100
101 switch ($response["authorize"]["status"]) {
102 case "OK":
103 //everything is as expected, save and return the response object
104 unset($response["authorize"]);
105 $this->data = $response;
106 return $response;
107 break;
108 case "CHALL":
109 if ((isset($this->user["authstring"])) && ($this->user["authstring"] == $response["authorize"]["challenge"])) {
110 return Array("error" => "Incorrect credentials.");
111 }
112
113 //save the authstring and send the request again
114 $this->user["authstring"] = $response["authorize"]["challenge"];
115 return $this->send($data);
116 break;
117 case "NOACC":
118 return Array("error" => "Please create an account with which to access MistServer before using this PHP API.");
119 break;
120 default:
121 return Array("error" => "MistServer response type not implemented");
122 }
123 }
124 }
125?>