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,29 +1,29 @@
<?php
include('config.inc.php');
include 'config.inc.php';
$blocklogin = FALSE;
$blocklogin = false;
if ((!isset($apipass) or empty($apipass)) or (!isset($apiip) or empty($apiip)) or (!isset($apiport) or empty($apiport))) {
$errormsg = 'You need to configure your settings for the PowerDNS API. See <a href="doc/apiconf.txt">doc/apiconf.txt</a>';
$blocklogin = TRUE;
$blocklogin = true;
}
if (!isset($apiproto) or !preg_match('/^http(s)?$/', $apiproto)) {
$errormsg = "The value for \$apiproto is incorrect in your config. Did you configure it?";
$blocklogin = TRUE;
$errormsg = 'The value for $apiproto is incorrect in your config. Did you configure it?';
$blocklogin = true;
}
if (!isset($apisslverify)) {
$errormsg = "The value for \$apisslverify is incorrect in your config. Did you configure it?";
$blocklogin = TRUE;
$errormsg = 'The value for $apisslverify is incorrect in your config. Did you configure it?';
$blocklogin = true;
} else {
$apisslverify = ( bool ) $apisslverify;
}
if (!isset($authdb)) {
$errormsg = "You did not configure a value for the setting \$authdb in your config";
$blocklogin = TRUE;
$errormsg = 'You did not configure a value for the setting $authdb in your config';
$blocklogin = true;
}
if (isset($defaults['primaryns'])) {
@ -38,24 +38,22 @@ if (!isset($logo) or empty($logo)) {
$logo = 'https://www.tuxis.nl/uploads/images/nsedit.png';
}
/* No need to change stuf below */
if (function_exists('curl_init') === FALSE) {
$errormsg = "You need PHP Curl to run nsedit";
$blocklogin = TRUE;
if (function_exists('curl_init') === false) {
$errormsg = 'You need PHP Curl to run nsedit';
$blocklogin = true;
}
if (class_exists('SQLite3') === FALSE) {
$errormsg = "You need PHP SQLite3 to run nsedit";
$blocklogin = TRUE;
}
if (function_exists('openssl_random_pseudo_bytes') === FALSE) {
$errormsg = "You need PHP compiled with openssl to run nsedit";
$blocklogin = TRUE;
if (class_exists('SQLite3') === false) {
$errormsg = 'You need PHP SQLite3 to run nsedit';
$blocklogin = true;
}
if (function_exists('openssl_random_pseudo_bytes') === false) {
$errormsg = 'You need PHP compiled with openssl to run nsedit';
$blocklogin = true;
}
$defaults['defaulttype'] = ucfirst(strtolower($defaults['defaulttype']));
@ -65,7 +63,7 @@ if (isset($authdb) && !file_exists($authdb) && class_exists('SQLite3')) {
$createsql = file_get_contents('includes/scheme.sql');
$db->exec($createsql);
$salt = bin2hex(openssl_random_pseudo_bytes(16));
$db->exec("INSERT INTO users (emailaddress, password, isadmin) VALUES ('admin', '".crypt("admin", '$6$'.$salt)."', 1)");
$db->exec("INSERT INTO users (emailaddress, password, isadmin) VALUES ('admin', '" . crypt('admin', '$6$' . $salt) . "', 1)");
}
function string_starts_with($string, $prefix)
@ -84,7 +82,8 @@ function string_ends_with($string, $suffix)
return (substr($string, -$length) === $suffix);
}
function get_db() {
function get_db()
{
global $authdb, $db;
if (!isset($db)) {
@ -95,10 +94,11 @@ function get_db() {
return $db;
}
function get_all_users() {
function get_all_users()
{
$db = get_db();
$r = $db->query('SELECT id, emailaddress, isadmin FROM users ORDER BY emailaddress');
$ret = array();
$ret = [];
while ($row = $r->fetchArray(SQLITE3_ASSOC)) {
array_push($ret, $row);
}
@ -106,7 +106,8 @@ function get_all_users() {
return $ret;
}
function get_user_info($u) {
function get_user_info($u)
{
$db = get_db();
$q = $db->prepare('SELECT * FROM users WHERE emailaddress = ?');
$q->bindValue(1, $u);
@ -116,11 +117,13 @@ function get_user_info($u) {
return $userinfo;
}
function user_exists($u) {
function user_exists($u)
{
return (bool) get_user_info($u);
}
function do_db_auth($u, $p) {
function do_db_auth($u, $p)
{
$db = get_db();
$q = $db->prepare('SELECT * FROM users WHERE emailaddress = ?');
$q->bindValue(1, $u);
@ -128,19 +131,20 @@ function do_db_auth($u, $p) {
$userinfo = $result->fetchArray(SQLITE3_ASSOC);
if ($userinfo and $userinfo['password'] and (crypt($p, $userinfo['password']) === $userinfo['password'])) {
return TRUE;
return true;
}
return FALSE;
return false;
}
function add_user($username, $isadmin = FALSE, $password = '') {
function add_user($username, $isadmin = false, $password = '')
{
if (!$password) {
$password = bin2hex(openssl_random_pseudo_bytes(32));
}
if (!string_starts_with($password, '$6$')) {
$salt = bin2hex(openssl_random_pseudo_bytes(16));
$password = crypt($password, '$6$'.$salt);
$password = crypt($password, '$6$' . $salt);
}
$db = get_db();
@ -158,10 +162,11 @@ function add_user($username, $isadmin = FALSE, $password = '') {
return $ret;
}
function update_user($id, $isadmin, $password) {
function update_user($id, $isadmin, $password)
{
if ($password && !preg_match('/\$6\$/', $password)) {
$salt = bin2hex(openssl_random_pseudo_bytes(16));
$password = crypt($password, '$6$'.$salt);
$password = crypt($password, '$6$' . $salt);
}
$db = get_db();
@ -178,19 +183,20 @@ function update_user($id, $isadmin, $password) {
$q->bindValue(1, (int)(bool)$isadmin, SQLITE3_INTEGER);
$q->bindValue(2, $password, SQLITE3_TEXT);
$q->bindValue(3, $id, SQLITE3_INTEGER);
writelog("Updating password and/or settings for $username. Admin: ".(int)(bool)$isadmin);
writelog("Updating password and/or settings for $username. Admin: " . (int)(bool)$isadmin);
} else {
$q = $db->prepare('UPDATE users SET isadmin = ? WHERE id = ?');
$q->bindValue(1, (int)(bool)$isadmin, SQLITE3_INTEGER);
$q->bindValue(2, $id, SQLITE3_INTEGER);
writelog("Updating settings for $username. Admin: ".(int)(bool)$isadmin);
$q->bindValue(2, $id, SQLITE3_INTEGER);
writelog("Updating settings for $username. Admin: " . (int)(bool)$isadmin);
}
$ret = $q->execute();
return $ret;
}
function delete_user($id) {
function delete_user($id)
{
$db = get_db();
$q = $db->prepare('SELECT * FROM users WHERE id = ?');
@ -199,41 +205,43 @@ function delete_user($id) {
$userinfo = $result->fetchArray(SQLITE3_ASSOC);
$q->close();
if($userinfo) {
if ($userinfo) {
$q = $db->prepare('DELETE FROM users WHERE id = ?');
$q->bindValue(1, $id, SQLITE3_INTEGER);
$ret = $q->execute();
writelog("Deleted user " . $userinfo['emailaddress'] . ".");
writelog('Deleted user ' . $userinfo['emailaddress'] . '.');
return $ret;
} else {
return false;
}
}
function valid_user($name) {
return ( bool ) preg_match( "/^[a-z0-9@_.-]+$/i" , $name );
function valid_user($name)
{
return ( bool ) preg_match('/^[a-z0-9@_.-]+$/i', $name);
}
function jtable_respond($records, $method = 'multiple', $msg = 'Undefined errormessage') {
$jTableResult = array();
function jtable_respond($records, $method = 'multiple', $msg = 'Undefined errormessage')
{
$jTableResult = [];
if ($method == 'error') {
$jTableResult['Result'] = "ERROR";
$jTableResult['Result'] = 'ERROR';
$jTableResult['Message'] = $msg;
} elseif ($method == 'single') {
$jTableResult['Result'] = "OK";
$jTableResult['Result'] = 'OK';
$jTableResult['Record'] = $records;
} elseif ($method == 'delete') {
$jTableResult['Result'] = "OK";
$jTableResult['Result'] = 'OK';
} elseif ($method == 'options') {
$jTableResult['Result'] = "OK";
$jTableResult['Result'] = 'OK';
$jTableResult['Options'] = $records;
} else {
if (isset($_GET['jtPageSize'])) {
$jTableResult['TotalRecordCount'] = count($records);
$records = array_slice($records, $_GET['jtStartIndex'], $_GET['jtPageSize']);
}
$jTableResult['Result'] = "OK";
$jTableResult['Result'] = 'OK';
$jTableResult['Records'] = $records;
$jTableResult['RecordCount'] = count($records);
}
@ -245,10 +253,11 @@ function jtable_respond($records, $method = 'multiple', $msg = 'Undefined errorm
exit(0);
}
function user_template_list() {
function user_template_list()
{
global $templates;
$templatelist = array();
$templatelist = [];
foreach ($templates as $template) {
if (is_adminuser()
or (isset($template['owner'])
@ -259,22 +268,25 @@ function user_template_list() {
return $templatelist;
}
function user_template_names() {
$templatenames = array('None' => 'None');
function user_template_names()
{
$templatenames = ['None' => 'None'];
foreach (user_template_list() as $template) {
$templatenames[$template['name']] = $template['name'];
}
return $templatenames;
}
function getlogs() {
function getlogs()
{
global $logging;
if ($logging !== TRUE)
if ($logging !== true) {
return;
}
$db = get_db();
$r = $db->query('SELECT * FROM logs ORDER BY timestamp DESC');
$ret = array();
$ret = [];
while ($row = $r->fetchArray(SQLITE3_ASSOC)) {
array_push($ret, $row);
}
@ -282,74 +294,82 @@ function getlogs() {
return $ret;
}
function clearlogs() {
function clearlogs()
{
global $logging;
if ($logging !== TRUE)
if ($logging !== true) {
return;
}
$db = get_db();
$q = $db->query('DELETE FROM logs;');
writelog("Logtable truncated.");
writelog('Logtable truncated.');
}
function rotatelogs() {
function rotatelogs()
{
global $logging, $logsdirectory;
if ($logging !== TRUE)
return FALSE;
if ($logging !== true) {
return false;
}
if(!is_dir($logsdirectory) || !is_writable($logsdirectory)) {
writelog("Logs directory cannot be written to.");
return FALSE;
if (!is_dir($logsdirectory) || !is_writable($logsdirectory)) {
writelog('Logs directory cannot be written to.');
return false;
}
date_default_timezone_set('UTC');
$filename = date("Y-m-d-His") . ".json";
$file = fopen($logsdirectory . "/" . $filename, "x");
$filename = date('Y-m-d-His') . '.json';
$file = fopen($logsdirectory . '/' . $filename, 'x');
if($file === FALSE) {
writelog("Can't create file for log rotation.");
return FALSE;
if ($file === false) {
writelog("Can't create file for log rotation.");
return false;
}
if(fwrite($file,json_encode(getlogs())) === FALSE) {
if (fwrite($file, json_encode(getlogs())) === false) {
writelog("Can't write to file for log rotation.");
fclose($file);
return FALSE;
return false;
} else {
fclose($file);
clearlogs();
return $filename;
}
}
function listrotatedlogs() {
function listrotatedlogs()
{
global $logging, $logsdirectory;
if ($logging !== TRUE)
return FALSE;
$list = scandir($logsdirectory,SCANDIR_SORT_DESCENDING);
if($list === FALSE) {
writelog("Logs directory cannot read.");
return FALSE;
if ($logging !== true) {
return false;
}
$list=array_filter($list,
$list = scandir($logsdirectory, SCANDIR_SORT_DESCENDING);
if ($list === false) {
writelog('Logs directory cannot read.');
return false;
}
$list=array_filter(
$list,
function ($val) {
return(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}\.json/',$val) == 1);
return(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}\.json/', $val) == 1);
}
);
return $list;
}
function writelog($line, $user=False) {
function writelog($line, $user=false)
{
global $logging;
if ($logging !== TRUE)
if ($logging !== true) {
return;
}
if ($user === False) {
if ($user === false) {
$user = get_sess_user();
}
@ -375,22 +395,26 @@ function writelog($line, $user=False) {
it available on older php versions. Thanks! */
if (!function_exists('hash_pbkdf2')) {
function hash_pbkdf2($algo, $password, $salt, $iterations, $length = 0, $rawOutput = false) {
function hash_pbkdf2($algo, $password, $salt, $iterations, $length = 0, $rawOutput = false)
{
// check for hashing algorithm
if (!in_array(strtolower($algo), hash_algos())) {
trigger_error(sprintf(
'%s(): Unknown hashing algorithm: %s',
__FUNCTION__, $algo
__FUNCTION__,
$algo
), E_USER_WARNING);
return false;
}
// check for type of iterations and length
foreach (array(4 => $iterations, 5 => $length) as $index => $value) {
foreach ([4 => $iterations, 5 => $length] as $index => $value) {
if (!is_numeric($value)) {
trigger_error(sprintf(
'%s() expects parameter %d to be long, %s given',
__FUNCTION__, $index, gettype($value)
__FUNCTION__,
$index,
gettype($value)
), E_USER_WARNING);
return null;
}
@ -401,7 +425,8 @@ if (!function_exists('hash_pbkdf2')) {
if ($iterations <= 0) {
trigger_error(sprintf(
'%s(): Iterations must be a positive integer: %d',
__FUNCTION__, $iterations
__FUNCTION__,
$iterations
), E_USER_WARNING);
return false;
}
@ -411,7 +436,8 @@ if (!function_exists('hash_pbkdf2')) {
if ($length < 0) {
trigger_error(sprintf(
'%s(): Iterations must be greater than or equal to 0: %d',
__FUNCTION__, $length
__FUNCTION__,
$length
), E_USER_WARNING);
return false;
}
@ -420,7 +446,8 @@ if (!function_exists('hash_pbkdf2')) {
if (strlen($salt) > PHP_INT_MAX - 4) {
trigger_error(sprintf(
'%s(): Supplied salt is too long, max of INT_MAX - 4 bytes: %d supplied',
__FUNCTION__, strlen($salt)
__FUNCTION__,
strlen($salt)
), E_USER_WARNING);
return false;
}
@ -454,5 +481,3 @@ if (!function_exists('hash_pbkdf2')) {
return $derivedKey;
}
}
?>