diff --git a/composer.json b/composer.json index 695cb05..027e7eb 100644 --- a/composer.json +++ b/composer.json @@ -23,5 +23,10 @@ }, "require": { "php": ">=7.1.0" + }, + "autoload": { + "psr-4": { + "Avametix\\": "src/Avametix/" + } } } \ No newline at end of file diff --git a/DirectApi.php b/src/Avametix/DirectApi.php similarity index 96% rename from DirectApi.php rename to src/Avametix/DirectApi.php index 8793bbd..379a84a 100644 --- a/DirectApi.php +++ b/src/Avametix/DirectApi.php @@ -1,240 +1,240 @@ -host = $host; - $this->port = $port; - $this->protocol = $protocol; - $this->usernames = explode("|", $usernames); - $this->password = $password; - } - - /** - * Get usernames as string - * - * Parses the known usernames as a string in order to be used in DirectAdmin requests. - * - * @access private - * @return string - */ - private function username() { - return implode("|", $this->usernames); - } - - /** - * Change or add second username - * - * These usernames may be used to authenticate DirectAdmin requests. - * - * @access public - * @param string $username New username - */ - public function login_as(string $username) { - $this->usernames[1] = $username; - } - - /** - * Removes second username. - * - * @access public - */ - public function logout() { - $this->usernames = array_splice($this->usernames, 1); - } - - /** - * Make a get request to the DirectAdmin API - * - * Adds the DirectAdmin API prefix to an internal GET request. - * - * @access public - * @param string $command The DirectAdmin API command to execute - * @param ?array $data The data to transmit with the DirectAdmin API request - * @return array The parsed data - */ - public function get_api(string $command, ?array $data = null) { - return $this->get("/CMD_API_$command", $data); - } - - /** - * Make a post request to the DirectAdmin API - * - * Adds the DirectAdmin API prefix to an internal POST request. - * - * @access public - * @param string $command The DirectAdmin API command to execute - * @param array $data The data to transmit with the DirectAdmin API request - * @return array The parsed data - */ - public function post_api(string $command, array $data) { - return $this->post("/CMD_API_$command", $data); - } - - /** - * Decodes an input as a list - * - * Creates an array of all returned values in input. - * - * @access protected - * @param string $input The string to parse to a list - * @return array The parsed list - */ - protected function decode_list(string $input) { - $a = explode('&', urldecode($input)); - $values = Array(); - - $i=0; - foreach ($a as $v) - { - $values[$i++] = substr(strstr($v, '='), 1); - } - - return $values; - } - - /** - * Decodes an input as a dictionary - * - * Creates a dictionary of all returned key/value pairs in input. - * - * @access protected - * @param string $input The string to parse to a dictionary - * @return array The parsed dictionary - */ - protected function decode_array(string $input) { - $a = explode('&', urldecode($input)); - $values = Array(); - - $i=0; - foreach ($a as $v) - { - $values[substr($v, 0, strpos($v, '='))] = substr(strstr($v, '='), 1); - } - - return $values; - } - - /** - * Chooses whether to parse an input as a list or dictionary - * - * Sends input to either decode_list or decode_array - * - * @access private - * @param string $input The string to choose for - * @return array The parsed result - */ - private function parse_result(string $input) { - if (str_contains($input, "list[]")) - return $this->decode_list($input); - - return $this->decode_array($input); - } - - /** - * Sends a get request - * - * Uses destination and data to create a get request to the specified server and returns the parsed result - * - * @access private - * @param string $destination The page the request should go to - * @param ?array $data The data to send with the request - * @return object The parsed result - */ - private function get(string $destination, ?array $data = null) { - // Combine already set variables to create a valid endpoint - $url = $this->protocol . "://" . $this->host . ":" . $this->port . $destination; - - // Add data to GET request if data is set - if (!is_null($data) && isset($data)) - $url .= "?" . http_build_query($data); - - // Set correct headers - $options = array( - 'http' => array( - 'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic " . base64_encode($this->username() . ":" . $this->password) . "\r\n", - 'method' => 'GET', - 'content' => '' - ) - ); - - // Create context and get contents from request - $context = stream_context_create($options); - $result = file_get_contents($url, false, $context); - - // Return false if request is invalid, else return the parsed result - if ($result === FALSE) { - return false; - } - - return $this->parse_result($result); - } - - /** - * Sends a post request - * - * Uses destination and data to create a post request to the specified server and returns the parsed result - * - * @access private - * @param string $destination The page the request should go to - * @param ?array $data The data to send with the request - * @return object The parsed result - */ - private function post(string $destination, $data) { - // Combine already set variables to create a valid endpoint - $url = $this->protocol . "://" . $this->host . ":" . $this->port . $destination; - - foreach ($data as $key => $value) { - $data[$key] = str_replace("|password|", $this->password, $value); - } - - // Set correct headers - $options = array( - 'http' => array( - 'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic " . base64_encode($this->username() . ":" . $this->password) . "\r\n", - 'method' => 'POST', - 'content' => http_build_query($data) - ) - ); - - // Create context and get contents from request - $context = stream_context_create($options); - $result = file_get_contents($url, false, $context); - - // Return false if request is invalid, else return the parsed result - if ($result === FALSE) { - return false; - } - - return $this->parse_result($result); - } -} - +host = $host; + $this->port = $port; + $this->protocol = $protocol; + $this->usernames = explode("|", $usernames); + $this->password = $password; + } + + /** + * Get usernames as string + * + * Parses the known usernames as a string in order to be used in DirectAdmin requests. + * + * @access private + * @return string + */ + private function username() { + return implode("|", $this->usernames); + } + + /** + * Change or add second username + * + * These usernames may be used to authenticate DirectAdmin requests. + * + * @access public + * @param string $username New username + */ + public function login_as(string $username) { + $this->usernames[1] = $username; + } + + /** + * Removes second username. + * + * @access public + */ + public function logout() { + $this->usernames = array_splice($this->usernames, 1); + } + + /** + * Make a get request to the DirectAdmin API + * + * Adds the DirectAdmin API prefix to an internal GET request. + * + * @access public + * @param string $command The DirectAdmin API command to execute + * @param ?array $data The data to transmit with the DirectAdmin API request + * @return array The parsed data + */ + public function get_api(string $command, ?array $data = null) { + return $this->get("/CMD_API_$command", $data); + } + + /** + * Make a post request to the DirectAdmin API + * + * Adds the DirectAdmin API prefix to an internal POST request. + * + * @access public + * @param string $command The DirectAdmin API command to execute + * @param array $data The data to transmit with the DirectAdmin API request + * @return array The parsed data + */ + public function post_api(string $command, array $data) { + return $this->post("/CMD_API_$command", $data); + } + + /** + * Decodes an input as a list + * + * Creates an array of all returned values in input. + * + * @access protected + * @param string $input The string to parse to a list + * @return array The parsed list + */ + protected function decode_list(string $input) { + $a = explode('&', urldecode($input)); + $values = Array(); + + $i=0; + foreach ($a as $v) + { + $values[$i++] = substr(strstr($v, '='), 1); + } + + return $values; + } + + /** + * Decodes an input as a dictionary + * + * Creates a dictionary of all returned key/value pairs in input. + * + * @access protected + * @param string $input The string to parse to a dictionary + * @return array The parsed dictionary + */ + protected function decode_array(string $input) { + $a = explode('&', urldecode($input)); + $values = Array(); + + $i=0; + foreach ($a as $v) + { + $values[substr($v, 0, strpos($v, '='))] = substr(strstr($v, '='), 1); + } + + return $values; + } + + /** + * Chooses whether to parse an input as a list or dictionary + * + * Sends input to either decode_list or decode_array + * + * @access private + * @param string $input The string to choose for + * @return array The parsed result + */ + private function parse_result(string $input) { + if (str_contains($input, "list[]")) + return $this->decode_list($input); + + return $this->decode_array($input); + } + + /** + * Sends a get request + * + * Uses destination and data to create a get request to the specified server and returns the parsed result + * + * @access private + * @param string $destination The page the request should go to + * @param ?array $data The data to send with the request + * @return object The parsed result + */ + private function get(string $destination, ?array $data = null) { + // Combine already set variables to create a valid endpoint + $url = $this->protocol . "://" . $this->host . ":" . $this->port . $destination; + + // Add data to GET request if data is set + if (!is_null($data) && isset($data)) + $url .= "?" . http_build_query($data); + + // Set correct headers + $options = array( + 'http' => array( + 'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic " . base64_encode($this->username() . ":" . $this->password) . "\r\n", + 'method' => 'GET', + 'content' => '' + ) + ); + + // Create context and get contents from request + $context = stream_context_create($options); + $result = file_get_contents($url, false, $context); + + // Return false if request is invalid, else return the parsed result + if ($result === FALSE) { + return false; + } + + return $this->parse_result($result); + } + + /** + * Sends a post request + * + * Uses destination and data to create a post request to the specified server and returns the parsed result + * + * @access private + * @param string $destination The page the request should go to + * @param ?array $data The data to send with the request + * @return object The parsed result + */ + private function post(string $destination, $data) { + // Combine already set variables to create a valid endpoint + $url = $this->protocol . "://" . $this->host . ":" . $this->port . $destination; + + foreach ($data as $key => $value) { + $data[$key] = str_replace("|password|", $this->password, $value); + } + + // Set correct headers + $options = array( + 'http' => array( + 'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic " . base64_encode($this->username() . ":" . $this->password) . "\r\n", + 'method' => 'POST', + 'content' => http_build_query($data) + ) + ); + + // Create context and get contents from request + $context = stream_context_create($options); + $result = file_get_contents($url, false, $context); + + // Return false if request is invalid, else return the parsed result + if ($result === FALSE) { + return false; + } + + return $this->parse_result($result); + } +} + // EOF \ No newline at end of file