php lint + code standard PSR-2

This commit is contained in:
Nikita Tarasov 2018-05-20 22:17:29 +03:00
parent ebd12ebeb2
commit 73f290e896
15 changed files with 796 additions and 531 deletions

View file

@ -1,12 +1,14 @@
<?php
include_once('includes/config.inc.php');
include_once 'includes/config.inc.php';
class ApiHandler {
public function __construct() {
class ApiHandler
{
public function __construct()
{
global $apiip, $apiport, $apipass, $apiproto, $apisslverify;
$this->headers = Array();
$this->headers = [];
$this->hostname = $apiip;
$this->port = $apiport;
$this->auth = $apipass;
@ -14,61 +16,66 @@ class ApiHandler {
$this->sslverify = $apisslverify;
$this->curlh = curl_init();
$this->method = 'GET';
$this->content = FALSE;
$this->content = false;
$this->apiurl = '';
}
public function addheader($field, $content) {
public function addheader($field, $content)
{
$this->headers[$field] = $content;
}
private function authheaders() {
private function authheaders()
{
$this->addheader('X-API-Key', $this->auth);
}
private function apiurl() {
private function apiurl()
{
$tmp = new ApiHandler();
$tmp->url = '/api';
$tmp->go();
if ($tmp->json[0]['version'] <= 1) {
$this->apiurl = $tmp->json[0]['url'];
} else {
throw new Exception("Unsupported API version");
throw new Exception('Unsupported API version');
}
}
private function curlopts() {
private function curlopts()
{
$this->authheaders();
$this->addheader('Accept', 'application/json');
if(defined('curl_reset')) {
if (defined('curl_reset')) {
curl_reset($this->curlh);
} else {
$this->curlh = curl_init();
}
curl_setopt($this->curlh, CURLOPT_HTTPHEADER, Array());
curl_setopt($this->curlh, CURLOPT_HTTPHEADER, []);
curl_setopt($this->curlh, CURLOPT_RETURNTRANSFER, 1);
if (strcasecmp($this->proto, 'https')) {
curl_setopt($this->curlh, CURLOPT_SSL_VERIFYPEER, $this->sslverify);
}
$setheaders = Array();
$setheaders = [];
foreach ($this->headers as $k => $v) {
array_push($setheaders, join(": ", Array($k, $v)));
array_push($setheaders, join(': ', [$k, $v]));
}
curl_setopt($this->curlh, CURLOPT_HTTPHEADER, $setheaders);
}
private function baseurl() {
return $this->proto.'://'.$this->hostname.':'.$this->port.$this->apiurl;
private function baseurl()
{
return $this->proto . '://' . $this->hostname . ':' . $this->port . $this->apiurl;
}
private function go() {
private function go()
{
$this->curlopts();
if ($this->content) {
@ -91,31 +98,31 @@ class ApiHandler {
break;
}
curl_setopt($this->curlh, CURLOPT_URL, $this->baseurl().$this->url);
curl_setopt($this->curlh, CURLOPT_URL, $this->baseurl() . $this->url);
$return = curl_exec($this->curlh);
$code = curl_getinfo($this->curlh, CURLINFO_HTTP_CODE);
$json = json_decode($return, 1);
if (isset($json['error'])) {
throw new Exception("API Error $code: ".$json['error']);
throw new Exception("API Error $code: " . $json['error']);
} elseif ($code < 200 || $code >= 300) {
if ($code == 401) {
throw new Exception("Authentication failed. Have you configured your authmethod correct?");
throw new Exception('Authentication failed. Have you configured your authmethod correct?');
}
throw new Exception("Curl Error: $code ".curl_error($this->curlh));
throw new Exception("Curl Error: $code " . curl_error($this->curlh));
}
$this->json = $json;
}
public function call() {
public function call()
{
if (substr($this->url, 0, 1) != '/') {
$this->url = '/'.$this->url;
$this->url = '/' . $this->url;
}
$this->apiurl();
$this->url = str_replace($this->apiurl, '', $this->url);
$this->go();
}
}