Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code standard PSR-2 + CI linter #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes/config.inc.php
nsedit.sublime*
etc
composer.lock
vendor
.php_cs.cache
37 changes: 37 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in('.')
->exclude('jquery-ui');

return PhpCsFixer\Config::create()
->setFinder($finder)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'include' => true,
'new_with_braces' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_multiline_whitespace_before_semicolons' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'phpdoc_order' => true,
'phpdoc_types' => true,
'phpdoc_add_missing_param_annotation' => true,
'single_quote' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'lowercase_cast' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
]);
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
before_script:
- composer self-update
- phpenv config-rm xdebug.ini
- composer install --no-interaction --prefer-dist

script:
- php vendor/bin/grumphp run
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name":"tuxis-ie/nsedit",
"license": "GPL-2.0-only",
"description": "DNS Editor working with PowerDNS's new API",
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.11",
"phpro/grumphp": "^0.14.0",
"jakub-onderka/php-parallel-lint": "^1.0"
}
}
9 changes: 9 additions & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
git_dir: .
bin_dir: vendor/bin
tasks:
composer:
no_check_lock: true
phplint:
phpcsfixer2:
config: '.php_cs'
59 changes: 33 additions & 26 deletions includes/class/ApiHandler.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,81 @@
<?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;
$this->proto = $apiproto;
$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) {
Expand All @@ -91,31 +98,31 @@ private function go() {
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();
}
}

48 changes: 27 additions & 21 deletions includes/class/PdnsApi.php
Original file line number Diff line number Diff line change
@@ -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']])) {
Expand All @@ -28,13 +31,14 @@ public function listzones($q = FALSE) {

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";
Expand All @@ -43,7 +47,8 @@ public function loadzone($zoneid) {
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";
Expand All @@ -52,7 +57,8 @@ public function exportzone($zoneid) {
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
Expand All @@ -78,14 +84,15 @@ public function savezone($zone) {
// 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";
Expand All @@ -94,23 +101,25 @@ public function deletezone($zoneid) {
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";

$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']);
}
Expand All @@ -119,7 +128,4 @@ public function getzonekeys($zoneid) {

return $ret;
}

}

?>
Loading