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();
}
}

View file

@ -1,20 +1,23 @@
<?php
include_once('ApiHandler.php');
include_once 'ApiHandler.php';
class PdnsAPI {
public function __construct() {
class PdnsAPI
{
public function __construct()
{
$this->http = new ApiHandler();
}
public function listzones($q = FALSE) {
public function listzones($q = false)
{
$api = clone $this->http;
$api->method = 'GET';
if ($q) {
$api->url = "/servers/localhost/search-data?q=*".$q."*&max=25";
$api->url = '/servers/localhost/search-data?q=*' . $q . '*&max=25';
$api->call();
$ret = Array();
$seen = Array();
$ret = [];
$seen = [];
foreach ($api->json as $result) {
if (isset($seen[$result['zone_id']])) {
@ -28,13 +31,14 @@ class PdnsAPI {
return $ret;
}
$api->url = "/servers/localhost/zones";
$api->url = '/servers/localhost/zones';
$api->call();
return $api->json;
}
public function loadzone($zoneid) {
public function loadzone($zoneid)
{
$api = clone $this->http;
$api->method = 'GET';
$api->url = "/servers/localhost/zones/$zoneid";
@ -43,7 +47,8 @@ class PdnsAPI {
return $api->json;
}
public function exportzone($zoneid) {
public function exportzone($zoneid)
{
$api = clone $this->http;
$api->method = 'GET';
$api->url = "/servers/localhost/zones/$zoneid/export";
@ -52,7 +57,8 @@ class PdnsAPI {
return $api->json;
}
public function savezone($zone) {
public function savezone($zone)
{
$api = clone $this->http;
// We have to split up RRSets and Zoneinfo.
// First, update the zone
@ -78,14 +84,15 @@ class PdnsAPI {
// Then, update the rrsets
if (count($zone['rrsets']) > 0) {
$api->method = 'PATCH';
$api->content = json_encode(Array('rrsets' => $zone['rrsets']));
$api->content = json_encode(['rrsets' => $zone['rrsets']]);
$api->call();
}
return $this->loadzone($zone['id']);
}
public function deletezone($zoneid) {
public function deletezone($zoneid)
{
$api = clone $this->http;
$api->method = 'DELETE';
$api->url = "/servers/localhost/zones/$zoneid";
@ -94,8 +101,9 @@ class PdnsAPI {
return $api->json;
}
public function getzonekeys($zoneid) {
$ret = array();
public function getzonekeys($zoneid)
{
$ret = [];
$api = clone $this->http;
$api->method = 'GET';
$api->url = "/servers/localhost/zones/$zoneid/cryptokeys";
@ -103,14 +111,15 @@ class PdnsAPI {
$api->call();
foreach ($api->json as $key) {
if (!isset($key['active']))
if (!isset($key['active'])) {
continue;
}
$key['dstxt'] = $zoneid . ' IN DNSKEY '.$key['dnskey']."\n\n";
$key['dstxt'] = $zoneid . ' IN DNSKEY ' . $key['dnskey'] . "\n\n";
if (isset($key['ds'])) {
foreach ($key['ds'] as $ds) {
$key['dstxt'] .= $zoneid . ' IN DS '.$ds."\n";
$key['dstxt'] .= $zoneid . ' IN DS ' . $ds . "\n";
}
unset($key['ds']);
}
@ -119,7 +128,4 @@ class PdnsAPI {
return $ret;
}
}
?>

View file

@ -1,7 +1,9 @@
<?php
class Zone {
public function __construct() {
class Zone
{
public function __construct()
{
$this->id = '';
$this->name = '';
$this->kind = '';
@ -12,13 +14,14 @@ class Zone {
$this->soa_edit_api = '';
$this->keyinfo = '';
$this->account = '';
$this->zone = FALSE;
$this->nameservers = Array();
$this->rrsets = Array();
$this->masters = Array();
$this->zone = false;
$this->nameservers = [];
$this->rrsets = [];
$this->masters = [];
}
public function parse($data) {
public function parse($data)
{
$this->setId($data['id']);
$this->setName($data['name']);
$this->setKind($data['kind']);
@ -26,10 +29,12 @@ class Zone {
$this->setAccount($data['account']);
$this->setSerial($data['serial']);
$this->url = $data['url'];
if (isset($data['soa_edit']) && $data['soa_edit'] != "")
if (isset($data['soa_edit']) && $data['soa_edit'] != '') {
$this->setSoaEdit($data['soa_edit']);
if (isset($data['soa_edit_api']) && $data['soa_edit_api'] != "")
$this->setSoaEditApi($data['soa_edit_api'], True);
}
if (isset($data['soa_edit_api']) && $data['soa_edit_api'] != '') {
$this->setSoaEditApi($data['soa_edit_api'], true);
}
foreach ($data['masters'] as $master) {
$this->addMaster($master);
@ -50,82 +55,96 @@ class Zone {
}
}
public function importData($data) {
public function importData($data)
{
$this->zone = $data;
}
public function setKeyinfo($info) {
public function setKeyinfo($info)
{
$this->keyinfo = $info;
}
public function addNameserver($nameserver) {
public function addNameserver($nameserver)
{
foreach ($this->nameservers as $ns) {
if ($nameserver == $ns) {
throw new Exception("We already have this as a nameserver");
throw new Exception('We already have this as a nameserver');
}
}
array_push($this->nameservers, $nameserver);
}
public function setSerial($serial) {
public function setSerial($serial)
{
$this->serial = $serial;
}
public function setSoaEdit($soaedit) {
public function setSoaEdit($soaedit)
{
$this->soa_edit = $soaedit;
}
public function setSoaEditApi($soaeditapi, $overwrite=False) {
if (isset($this->soa_edit_api) and $this->soa_edit_api != "") {
if ($overwrite === False) {
return False;
public function setSoaEditApi($soaeditapi, $overwrite=false)
{
if (isset($this->soa_edit_api) and $this->soa_edit_api != '') {
if ($overwrite === false) {
return false;
}
}
$this->soa_edit_api = $soaeditapi;
}
public function setName($name) {
public function setName($name)
{
$this->name = $name;
}
public function setKind($kind) {
public function setKind($kind)
{
$this->kind = $kind;
}
public function setAccount($account) {
public function setAccount($account)
{
$this->account = $account;
}
public function setDnssec($dnssec) {
public function setDnssec($dnssec)
{
$this->dnssec = $dnssec;
}
public function setId($id) {
public function setId($id)
{
$this->id = $id;
}
public function addMaster($ip) {
public function addMaster($ip)
{
foreach ($this->masters as $master) {
if ($ip == $master) {
throw new Exception("We already have this as a master");
throw new Exception('We already have this as a master');
}
}
array_push($this->masters, $ip);
}
public function eraseMasters() {
$this->masters = Array();
public function eraseMasters()
{
$this->masters = [];
}
public function addRRSet($name, $type, $content, $disabled = FALSE, $ttl = 3600, $setptr = FALSE) {
if ($this->getRRSet($name, $type) !== FALSE) {
throw new Exception("This rrset already exists.");
public function addRRSet($name, $type, $content, $disabled = false, $ttl = 3600, $setptr = false)
{
if ($this->getRRSet($name, $type) !== false) {
throw new Exception('This rrset already exists.');
}
$rrset = new RRSet($name, $type, $content, $disabled, $ttl, $setptr);
array_push($this->rrsets, $rrset);
}
public function addRecord($name, $type, $content, $disabled = FALSE, $ttl = 3600, $setptr = FALSE) {
public function addRecord($name, $type, $content, $disabled = false, $ttl = 3600, $setptr = false)
{
$rrset = $this->getRRSet($name, $type);
if ($rrset) {
@ -138,7 +157,8 @@ class Zone {
return $this->getRecord($name, $type, $content);
}
public function getRecord($name, $type, $content) {
public function getRecord($name, $type, $content)
{
$rrset = $this->getRRSet($name, $type);
foreach ($rrset->exportRecords() as $record) {
if ($record['content'] == $content) {
@ -150,21 +170,22 @@ class Zone {
return $record;
}
}
}
public function getRRSet($name, $type) {
public function getRRSet($name, $type)
{
foreach ($this->rrsets as $rrset) {
if ($rrset->name == $name and $rrset->type == $type) {
return $rrset;
}
}
return FALSE;
return false;
}
public function rrsets2records() {
$ret = Array();
public function rrsets2records()
{
$ret = [];
foreach ($this->rrsets as $rrset) {
foreach ($rrset->exportRecords() as $record) {
@ -180,16 +201,17 @@ class Zone {
return $ret;
}
public function export() {
$ret = Array();
public function export()
{
$ret = [];
$ret['account'] = $this->account;
$ret['nameservers'] = $this->nameservers;
$ret['kind'] = $this->kind;
$ret['name'] = $this->name;
if (isset($this->soa_edit) && $this->soa_edit != "") {
if (isset($this->soa_edit) && $this->soa_edit != '') {
$ret['soa_edit'] = $this->soa_edit;
}
if (isset($this->soa_edit_api) && $this->soa_edit_api != "") {
if (isset($this->soa_edit_api) && $this->soa_edit_api != '') {
$ret['soa_edit_api'] = $this->soa_edit_api;
}
if ($this->zone) {
@ -206,12 +228,13 @@ class Zone {
$ret['rrsets'] = $this->exportRRSets();
$ret['serial'] = $this->serial;
$ret['url'] = $this->url;
return $ret;
}
private function exportRRSets() {
$ret = Array();
private function exportRRSets()
{
$ret = [];
foreach ($this->rrsets as $rrset) {
array_push($ret, $rrset->export());
}
@ -220,36 +243,42 @@ class Zone {
}
}
class RRSet {
public function __construct($name = '', $type = '', $content = '', $disabled = FALSE, $ttl = 3600, $setptr = FALSE) {
class RRSet
{
public function __construct($name = '', $type = '', $content = '', $disabled = false, $ttl = 3600, $setptr = false)
{
$this->name = $name;
$this->type = $type;
$this->ttl = $ttl;
$this->changetype = 'REPLACE';
$this->records = Array();
$this->comments = Array();
$this->records = [];
$this->comments = [];
if (isset($content) and $content != '') {
$this->addRecord($content, $disabled, $setptr);
}
}
public function delete() {
public function delete()
{
$this->changetype = 'DELETE';
}
public function setTtl($ttl) {
public function setTtl($ttl)
{
$this->ttl = $ttl;
}
public function setName($name) {
public function setName($name)
{
$this->name = $name;
}
public function addRecord($content, $disabled = FALSE, $setptr = FALSE) {
public function addRecord($content, $disabled = false, $setptr = false)
{
foreach ($this->records as $record) {
if ($record->content == $content) {
throw new Exception($this->name."/".$this->type." has duplicate records.");
throw new Exception($this->name . '/' . $this->type . ' has duplicate records.');
}
}
@ -257,20 +286,23 @@ class RRSet {
array_push($this->records, $record);
}
public function deleteRecord($content) {
public function deleteRecord($content)
{
foreach ($this->records as $idx => $record) {
if ($record->content == $content) {
unset($this->records[$idx]);
}
}
}
public function addComment($content, $account, $modified_at = FALSE) {
public function addComment($content, $account, $modified_at = false)
{
$comment = new Comment($content, $account, $modified_at);
array_push($this->comments, $comment);
}
public function export() {
$ret = Array();
public function export()
{
$ret = [];
$ret['comments'] = $this->exportComments();
$ret['name'] = $this->name;
$ret['records'] = $this->exportRecords();
@ -282,11 +314,12 @@ class RRSet {
return $ret;
}
public function exportRecords() {
$ret = Array();
public function exportRecords()
{
$ret = [];
foreach ($this->records as $record) {
if ($this->type != "A" and $this->type != "AAAA") {
$record->setptr = FALSE;
if ($this->type != 'A' and $this->type != 'AAAA') {
$record->setptr = false;
}
array_push($ret, $record->export());
}
@ -294,45 +327,51 @@ class RRSet {
return $ret;
}
public function exportComments() {
$ret = Array();
public function exportComments()
{
$ret = [];
foreach ($this->comments as $comment) {
array_push($ret, $comment->export());
}
return $ret;
}
}
class Record {
public function __construct($content, $disabled = FALSE, $setptr = FALSE) {
class Record
{
public function __construct($content, $disabled = false, $setptr = false)
{
$this->content = $content;
$this->disabled = $disabled;
$this->setptr = $setptr;
}
public function export() {
public function export()
{
$ret;
$ret['content'] = $this->content;
$ret['disabled'] = ( bool ) $this->disabled;
if ($this->setptr) {
$ret['set-ptr'] = ( bool ) TRUE;
$ret['set-ptr'] = ( bool ) true;
}
return $ret;
}
}
class Comment {
public function __construct($content, $account, $modified_at) {
class Comment
{
public function __construct($content, $account, $modified_at)
{
$this->content = $content;
$this->account = $account;
$this->modified_at = $modified_at;
}
public function export() {
public function export()
{
$ret;
$ret['content'] = $this->content;
@ -340,5 +379,3 @@ class Comment {
$ret['modified_at'] = $this->modified_at;
}
}
?>