fixes from phpstan analyse

main
Robert 2 years ago
parent 56b100171c
commit 0e160e7786
  1. 3
      .gitignore
  2. 8
      app/init_systems/open_rc.php
  3. 6
      app/init_systems/runit.php
  4. 8
      app/init_systems/sys_v_init.php
  5. 12
      app/init_systems/systemd.php
  6. 2
      app/neato_common.php
  7. 52
      app/neato_configure.php
  8. 4
      app/neato_logger.php
  9. 2
      app/package_managers/apk.php
  10. 5
      app/package_managers/apt.php
  11. 4
      app/package_managers/flatpak.php
  12. 4
      app/package_managers/snap.php
  13. 2
      app/traits/init_systems.php
  14. 6
      app/traits/linux_core.php
  15. 9
      app/traits/packages.php
  16. 2
      app/utils/keygen.php
  17. 6
      app/utils/php.php
  18. 8
      app/utils/php_composer.php
  19. 10
      app/utils/ufw.php
  20. 5
      composer.json
  21. 4
      phpstan.neon.dist

3
.gitignore vendored

@ -1,6 +1,9 @@
vendor/
run/*.lck run/*.lck
log/*.txt log/*.txt
*.pub *.pub
neato_deploy.tar.gz neato_deploy.tar.gz
neatoDeploy.phar neatoDeploy.phar
neato_deploy.tar.gz.self neato_deploy.tar.gz.self
phpstan.neon
composer.lock

@ -13,7 +13,7 @@ private static function get_valid_action_for_service(string $action): string|fal
'status'=>'status', 'status'=>'status',
'restart'=>'restart', 'restart'=>'restart',
'reload'=>'reload', 'reload'=>'reload',
default=>'restart', default=>false,
}; };
} }
@ -22,12 +22,12 @@ private static function get_valid_action_for_service(string $action): string|fal
'show'=>'show', 'show'=>'show',
'add'=>'add', 'add'=>'add',
'del'=>'del', 'del'=>'del',
default=>'add', default=>false,
}; };
} }
public static function service($name, $action = 'restart') { public static function service($name, $action = 'restart') {
$my_action = get_valid_action_for_service($action); $my_action = self::get_valid_action_for_service($action);
exec(\neato::get_super_user_bin . 'rc-service ' . safe_cmd($name, $action), $output, $exit_code); exec(\neato::get_super_user_bin . 'rc-service ' . safe_cmd($name, $action), $output, $exit_code);
display($output); display($output);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
@ -35,7 +35,7 @@ private static function get_valid_action_for_service(string $action): string|fal
} }
public static function systemctl($name, $action = 'add') { public static function systemctl($name, $action = 'add') {
$my_action = get_valid_action_for_system_ctl($action); $my_action = self::get_valid_action_for_system_ctl($action);
exec(\neato::get_super_bin . 'rc-update ' . safe_cmd($action, $name), $output, $exit_code); exec(\neato::get_super_bin . 'rc-update ' . safe_cmd($action, $name), $output, $exit_code);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
return $exit_code; return $exit_code;

@ -15,12 +15,12 @@ class runit {
'enable'=>'enable', 'enable'=>'enable',
'disable'=>'disable', 'disable'=>'disable',
'log'=>'log', // View service log 'log'=>'log', // View service log
default=>'restart', default=>false,
}; };
} }
public static function service(string $name, string $action = 'restart') { public static function service(string $name, string $action = 'restart') {
$my_action = get_valid_action($action); $my_action = self::get_valid_action($action);
exec(\neato::get_super_user_bin . 'sv ' . safe_cmd($my_action, $name), $output, $exit_code); exec(\neato::get_super_user_bin . 'sv ' . safe_cmd($my_action, $name), $output, $exit_code);
display($output); display($output);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
@ -28,7 +28,7 @@ class runit {
} }
public static function systemctl(string $name, string $action = 'enable') { public static function systemctl(string $name, string $action = 'enable') {
return \neato::service($name, $action); return self::service($name, $action);
} }
} }

@ -13,7 +13,7 @@ class sys_v_init {
'reload'=>'reload', 'reload'=>'reload',
'enable'=>'enable', 'enable'=>'enable',
'disable'=>'disable', 'disable'=>'disable',
default=>'restart', default=>false,
}; };
} }
@ -22,12 +22,12 @@ class sys_v_init {
'list'=>'--list', 'list'=>'--list',
'on'=>'on', 'on'=>'on',
'off'=>'off', 'off'=>'off',
default=>'on', default=>false,
}; };
} }
public static function service(string $name, string $action = 'restart') { public static function service(string $name, string $action = 'restart') {
$my_action = get_valid_action_for_service($action); $my_action = self::get_valid_action_for_service($action);
exec(\neato::get_super_user_bin . '/etc/init.d/' . safe_cmd($name, $my_action), $output, $exit_code); exec(\neato::get_super_user_bin . '/etc/init.d/' . safe_cmd($name, $my_action), $output, $exit_code);
display($output); display($output);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
@ -35,7 +35,7 @@ class sys_v_init {
} }
public static function systemctl(string $name, string $action = 'on') { public static function systemctl(string $name, string $action = 'on') {
$my_action = get_valid_action_for_system_ctl($action); $my_action = self::get_valid_action_for_system_ctl($action);
exec(\neato::get_bin . 'chkconfig ' . safe_cmd($action, $name), $output, $exit_code); exec(\neato::get_bin . 'chkconfig ' . safe_cmd($action, $name), $output, $exit_code);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
return $exit_code; return $exit_code;

@ -24,7 +24,7 @@ class systemd {
'isolate'=>'isolate', // Change system runlevel target 'isolate'=>'isolate', // Change system runlevel target
'log'=>'journalctl -u', // View service log 'log'=>'journalctl -u', // View service log
'realtime'=>'journalctl -f', // watch live logs 'realtime'=>'journalctl -f', // watch live logs
default=>'restart', default=>false,
}; };
} }
@ -37,21 +37,21 @@ class systemd {
'reload'=>'reload', 'reload'=>'reload',
'enable'=>'enable', 'enable'=>'enable',
'disable'=>'disable', 'disable'=>'disable',
default=>'restart', default=>false,
}; };
} }
public static function service(string $name, string $action = 'restart') { public static function service(string $name, string $action = 'restart') {
$my_action = get_valid_action_for_service($action); $my_action = self::get_valid_action_for_service($action);
exec(self::get_super_user_bin . 'service ' . safe_cmd($name, $my_action), $output, $exit_code); exec(\neato::get_super_user_bin . 'service ' . safe_cmd($name, $my_action), $output, $exit_code);
display($output); display($output);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
return $exit_code; return $exit_code;
} }
public static function systemctl(string $name, string $action = 'enable') { public static function systemctl(string $name, string $action = 'enable') {
$my_action = get_valid_action_for_system_ctl($action); $my_action = self::get_valid_action_for_system_ctl($action);
exec(self::get_bin . 'systemctl ' . safe_cmd($my_action, $name), $output, $exit_code); exec(\neato::get_bin . 'systemctl ' . safe_cmd($my_action, $name), $output, $exit_code);
check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
return $exit_code; return $exit_code;
} }

@ -136,7 +136,7 @@ function recursive_change_owner($mypath, $uid, $gid) {
if ($file != "." && $file != "..") { if ($file != "." && $file != "..") {
$typepath = $mypath . "/" . $file; $typepath = $mypath . "/" . $file;
if (filetype($typepath) == 'dir') { if (filetype($typepath) == 'dir') {
recursive_chown_chgrp($typepath, $uid, $gid); recursive_change_owner($typepath, $uid, $gid);
} }
chown($typepath, $uid); chown($typepath, $uid);

@ -4,12 +4,12 @@ final class configure {
private static $config = array(); private static $config = array();
protected function __construct() { } protected function __construct() { }
/** /*
* Fetches a setting set from using Configure::set() or add or update * Fetches a setting set from using Configure::set() or add or update
* *
* @param string $name The name of the setting to get * $name The name of the setting to get
* @param string $key [optional] The Array Key to fetch * $key [optional] The Array Key to fetch
* @return mixed The setting specified by $name, or null if $name was not set * The setting specified by $name, or null if $name was not set
* *
* return type: ?array * return type: ?array
*/ */
@ -26,11 +26,11 @@ final class configure {
return null; return null;
} }
/** /*
* Checks if the setting exists * Checks if the setting exists
* *
* @param string $name The name of the setting to check existance * $name The name of the setting to check existance
* @return boolean true if $name was set, false otherwise * return boolean true if $name was set, false otherwise
*/ */
public static function exists(string $name): bool { public static function exists(string $name): bool {
if (array_key_exists(strtolower($name), self::$config)) { if (array_key_exists(strtolower($name), self::$config)) {
@ -39,58 +39,56 @@ final class configure {
return false; return false;
} }
/** /*
* Overwrite/Update/Add to $config * Overwrite/Update/Add to $config
* @param string $name the main key to update * $name the main key to update
* @param string $key the sub key * $key the sub key
* @param type $value the data to update * type $value the data to update
*/ */
public static function update(string $name, string $key, $value): void { public static function update(string $name, string $key, $value): void {
self::$config[strtolower($name)][strtolower($key)] = $value; self::$config[strtolower($name)][strtolower($key)] = $value;
} }
/** /*
* Add to existing data without loss... to $config * Add to existing data without loss... to $config
* @param string $name the main key * $name the main key
* @param string $key the sub key * $key the sub key
* @param type $value new data to add * $value new data to add
*/ */
public static function add(string $name, string $key, $value): void { public static function add(string $name, string $key, $value): void {
self::$config[strtolower($name)][strtolower($key)][] = $value; self::$config[strtolower($name)][strtolower($key)][] = $value;
} }
/** /*
* Frees the setting given by $name, if it exists. All settings no longer in * Frees the setting given by $name, if it exists. All settings no longer in
* use should be freed using this method whenever possible * use should be freed using this method whenever possible
* *
* @param string $name The name of the setting to free * $name The name of the setting to free
*/ */
public static function free(string $name): void { public static function free(string $name): void {
if (self::exists($name)) if (self::exists($name))
unset(self::$config[strtolower($name)]); unset(self::$config[strtolower($name)]);
} }
/** /*
* Adds the given $value to the configuration using the $name given * Adds the given $value to the configuration using the $name given
* *
* @param string $name The name to give this setting. Use Configure::exists() * $name The name to give this setting. Use Configure::exists()
* to check for pre-existing settings with the same name * to check for pre-existing settings with the same name
* @param mixed $value The value to set * $value The value to set
*/ */
public static function set(string $name, $value): void { public static function set(string $name, $value): void {
self::$config[strtolower($name)] = $value; self::$config[strtolower($name)] = $value;
} }
/** /*
* Sets $config data from an Array * Sets $config data from an Array
* @param array $a ($name => $value) * array $a ($name => $value)
* @retval void * retutns a void
*/ */
public static function load_array(array $a): void { public static function load_array(array $a): void {
if (isset($a) && is_array($a)) { foreach ($a as $name => $value) {
foreach ($a as $name => $value) { self::$config[strtolower($name)] = $value;
self::$config[strtolower($name)] = $value;
}
} }
unset($a); unset($a);
} }

@ -34,12 +34,12 @@ class logger {
$success = touch($file); $success = touch($file);
if ($success === false) { if ($success === false) {
$this->handle = false; $this->handle = false;
return false; throw new \Exception('Unable to touch file:' . $file);
} }
chmod($file, 0660); chmod($file, 0660);
if (! is_writable($file)) { if (! is_writable($file)) {
$this->handle = false; $this->handle = false;
return false; throw new \Exception('Unable to write to file:' . $file);
} }
$this->handle = fopen($file, 'a'); $this->handle = fopen($file, 'a');
} }

@ -45,7 +45,7 @@ class apk {
} }
public static function full_update() { public static function full_update() {
exec(\neato::get_user_bin . 'apk update -y && ' . self::get_user_bin . 'apk upgrade -y' . stderr(), $output, $exit_code); exec(\neato::get_user_bin . 'apk update -y && ' . \neato::get_user_bin . 'apk upgrade -y' . stderr(), $output, $exit_code);
display($output); display($output);
return $exit_code; return $exit_code;
} }

@ -11,8 +11,9 @@ class apt {
return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false]; return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false];
} }
public static function upgrade(string $prog) { public static function upgrade(string $prog, bool $auto=true) {
exec(\neato::get_user_bin . 'apt upgrade ' . $assume . safe_cmd_quotes($prog) . stderr(), $output, $exit_code); $assume = ($auto) ? "-y " : "";
exec(\neato::get_user_bin . 'apt upgrade ' . $assume . safe_cmd_quotes($prog) . stderr(), $output, $exit_code);
display($output); display($output);
check_for_error($exit_code, "Unable to upgrade: {$prog}"); check_for_error($exit_code, "Unable to upgrade: {$prog}");
return $exit_code; return $exit_code;

@ -11,8 +11,8 @@ class flatpak {
return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false]; return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false];
} }
public static function upgrade(string $prog) { public static function upgrade() {
return self::update($prog); return self::update();
} }
public static function install(string $prog) { public static function install(string $prog) {

@ -11,8 +11,8 @@ class snap {
return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false]; return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false];
} }
public static function upgrade(string $prog) { public static function upgrade() {
return self::update($prog); return self::update();
} }
public static function install(string $prog) { public static function install(string $prog) {

@ -25,7 +25,7 @@ trait init_systems {
if ($my_init === false) { if ($my_init === false) {
return false; return false;
} }
$namespaced = "\\package_managers\\{$my_init}::service"; $namespaced = "\\init_systems\\{$my_init}::service";
return $namespaced($name, $action); return $namespaced($name, $action);
} }

@ -58,7 +58,7 @@ trait linux_core {
public static function groupadd(string $groupname, int $gid = 0) { public static function groupadd(string $groupname, int $gid = 0) {
$group_id = ($gid > 0) ? "-g {$gid} " : ""; $group_id = ($gid > 0) ? "-g {$gid} " : "";
exec(self::get_super_user_bin . 'groupadd '. $group_id . safe_cmd($groupname), $output, $exit_code); exec(self::get_super_user_bin . 'groupadd '. $group_id . safe_cmd($groupname), $output, $exit_code);
if ($exit_code === true || $exit_code === 0) { if ($exit_code === 0) {
display(getTermColors("Added new group named: $groupname", ['color'=>'green'])); display(getTermColors("Added new group named: $groupname", ['color'=>'green']));
} }
check_for_error($exit_code, "Unable to add new group: {$groupname}"); check_for_error($exit_code, "Unable to add new group: {$groupname}");
@ -67,7 +67,7 @@ trait linux_core {
public static function userdel(string $username) { public static function userdel(string $username) {
exec(self::get_super_user_bin . 'userdel ' . safe_cmd($username), $output, $exit_code); exec(self::get_super_user_bin . 'userdel ' . safe_cmd($username), $output, $exit_code);
if ($exit_code === true || $exit_code === 0) { if ($exit_code === 0) {
display(getTermColors("Deleted user account named: $username", ['color'=>'green'])); display(getTermColors("Deleted user account named: $username", ['color'=>'green']));
} }
check_for_error($exit_code, "Unable to delete user: {$username}"); check_for_error($exit_code, "Unable to delete user: {$username}");
@ -78,7 +78,7 @@ trait linux_core {
$user_id = ($uid > 0) ? "-u {$uid} " : ""; $user_id = ($uid > 0) ? "-u {$uid} " : "";
$dir = (empty($homedir)) ? " -m " : " -d " . safe_cmd($homedir); $dir = (empty($homedir)) ? " -m " : " -d " . safe_cmd($homedir);
exec(self::get_super_user_bin . 'useradd '. $user_id . '-s '. safe_cmd($shell) . $dir . ' -c '. safe_cmd($comment) .'-G'. safe_cmd($groups) . ' ' . safe_cmd($username), $output, $exit_code); exec(self::get_super_user_bin . 'useradd '. $user_id . '-s '. safe_cmd($shell) . $dir . ' -c '. safe_cmd($comment) .'-G'. safe_cmd($groups) . ' ' . safe_cmd($username), $output, $exit_code);
if ($exit_code === true || $exit_code === 0) { if ($exit_code === 0) {
display(getTermColors("Added new user account named: $username", ['color'=>'green'])); display(getTermColors("Added new user account named: $username", ['color'=>'green']));
} }
check_for_error($exit_code, "Unable to add new user: {$username}"); check_for_error($exit_code, "Unable to add new user: {$username}");

@ -4,6 +4,15 @@ namespace traits;
trait packages { trait packages {
private static function call_pkg_mgt(string $pkg, string $method, string $param="") {
$call = "\\package_managers\\{$pkg}::{$method}";
if (!class_exists($call)) {
throw new \Exception('A class with the name of '. $call. 'could not be found');
}
return (! empty($param)) ? $call($param) : $call();
}
private static function get_package_manager(string $os): string|false { private static function get_package_manager(string $os): string|false {
$linux_package_managers = [ $linux_package_managers = [
'ubuntu' => 'apt', 'ubuntu' => 'apt',

@ -18,7 +18,7 @@ class keygen {
return $exit_code; return $exit_code;
} }
public static function sign(string $host_ca_file, string $hostname, string $type="-h", string $fqdn = "", string $validfor = "+52w", string $file) { public static function sign(string $host_ca_file, string $file, string $hostname, string $type="-h", string $fqdn = "", string $validfor = "+52w") {
exec(\neato::get_user_bin . 'ssh-keygen -s '. safe_cmd($host_ca_file).' -I '. safe_cmd($hostname).' '. safe_cmd($type).' -n '. safe_cmd($fqdn).' -V '. safe_cmd($validfor).' '. safe_cmd($file), $output, $exit_code); exec(\neato::get_user_bin . 'ssh-keygen -s '. safe_cmd($host_ca_file).' -I '. safe_cmd($hostname).' '. safe_cmd($type).' -n '. safe_cmd($fqdn).' -V '. safe_cmd($validfor).' '. safe_cmd($file), $output, $exit_code);
display($output); display($output);
check_for_error($exit_code, "Unable to run ssh-keygen command: {$file}"); check_for_error($exit_code, "Unable to run ssh-keygen command: {$file}");

@ -4,7 +4,7 @@ namespace utils;
class php { class php {
function r_sapi($ver) { private static function r_sapi($ver) {
if (is_string_found($ver, "-s ") && is_string_found($ver, "-v ")) { if (is_string_found($ver, "-s ") && is_string_found($ver, "-v ")) {
return $ver; return $ver;
} }
@ -18,13 +18,13 @@ class php {
} }
public static function enable_module($name, $ver = false) { public static function enable_module($name, $ver = false) {
exec(\neato::get_super_user_bin . 'phpenmod ' . r_sapi($ver) . safe_cmd_quotes($name), $output, $exit_code); exec(\neato::get_super_user_bin . 'phpenmod ' . self::r_sapi($ver) . safe_cmd_quotes($name), $output, $exit_code);
check_for_error($exit_code, "PHP Unable to enable module: {$name}"); check_for_error($exit_code, "PHP Unable to enable module: {$name}");
return $exit_code; return $exit_code;
} }
public static function disable_module($name, $ver = false) { public static function disable_module($name, $ver = false) {
exec(\neato::get_super_user_bin . 'phpdismod ' . r_sapi($ver) . safe_cmd_quotes($name), $output, $exit_code); exec(\neato::get_super_user_bin . 'phpdismod ' . self::r_sapi($ver) . safe_cmd_quotes($name), $output, $exit_code);
check_for_error($exit_code, "PHP Unable to disable module: {$name}"); check_for_error($exit_code, "PHP Unable to disable module: {$name}");
return $exit_code; return $exit_code;
} }

@ -2,6 +2,8 @@
namespace utils; namespace utils;
// composer require --dev phpstan/phpstan
class php_composer { class php_composer {
const composer_exe = "/usr/local/bin/composer"; const composer_exe = "/usr/local/bin/composer";
@ -36,6 +38,12 @@ class php_composer {
return $exit_code; return $exit_code;
} }
public static function require_dev($dir, $vendor) {
exec(self::composer_exe . " -d " . safe_cmd($dir) . " require --dev " . safe_cmd($vendor), $output, $exit_code);
check_for_error($exit_code, "PHP Composer require error");
return $exit_code;
}
public static function remove($dir, $vendor) { public static function remove($dir, $vendor) {
exec(self::composer_exe . " -d " . safe_cmd($dir) . " remove " . safe_cmd($vendor), $output, $exit_code); exec(self::composer_exe . " -d " . safe_cmd($dir) . " remove " . safe_cmd($vendor), $output, $exit_code);
check_for_error($exit_code, "PHP Composer remove error"); check_for_error($exit_code, "PHP Composer remove error");

@ -3,13 +3,11 @@
namespace utils; namespace utils;
class ufw { class ufw {
/** /*
* * $action (allow or deny)
* @param type $action - allow or deny * $port (Port#/tcp)
* @param type $port - Port#/tcp
* @return type
*/ */
public static function make_rule($action, $port) { public static function make_rule(string $action, string $port): int|false {
switch($action) { switch($action) {
case 'allow': case 'deny': case 'reject': case 'limit': break; case 'allow': case 'deny': case 'reject': case 'limit': break;
default: default:

@ -0,0 +1,5 @@
{
"require-dev": {
"phpstan/phpstan": "^1.10"
}
}

@ -0,0 +1,4 @@
parameters:
level: 4
paths:
- app
Loading…
Cancel
Save