diff --git a/.gitignore b/.gitignore index 9c7f346..500e047 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ +vendor/ run/*.lck log/*.txt *.pub neato_deploy.tar.gz neatoDeploy.phar neato_deploy.tar.gz.self +phpstan.neon +composer.lock \ No newline at end of file diff --git a/app/init_systems/open_rc.php b/app/init_systems/open_rc.php index 2c03f74..abeedff 100644 --- a/app/init_systems/open_rc.php +++ b/app/init_systems/open_rc.php @@ -13,7 +13,7 @@ private static function get_valid_action_for_service(string $action): string|fal 'status'=>'status', 'restart'=>'restart', 'reload'=>'reload', - default=>'restart', + default=>false, }; } @@ -22,12 +22,12 @@ private static function get_valid_action_for_service(string $action): string|fal 'show'=>'show', 'add'=>'add', 'del'=>'del', - default=>'add', + default=>false, }; } 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); display($output); 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') { - $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); check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); return $exit_code; diff --git a/app/init_systems/runit.php b/app/init_systems/runit.php index a52f60d..fb64e26 100644 --- a/app/init_systems/runit.php +++ b/app/init_systems/runit.php @@ -15,12 +15,12 @@ class runit { 'enable'=>'enable', 'disable'=>'disable', 'log'=>'log', // View service log - default=>'restart', + default=>false, }; } 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); display($output); 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') { - return \neato::service($name, $action); + return self::service($name, $action); } } diff --git a/app/init_systems/sys_v_init.php b/app/init_systems/sys_v_init.php index 018df48..b87d727 100644 --- a/app/init_systems/sys_v_init.php +++ b/app/init_systems/sys_v_init.php @@ -13,7 +13,7 @@ class sys_v_init { 'reload'=>'reload', 'enable'=>'enable', 'disable'=>'disable', - default=>'restart', + default=>false, }; } @@ -22,12 +22,12 @@ class sys_v_init { 'list'=>'--list', 'on'=>'on', 'off'=>'off', - default=>'on', + default=>false, }; } 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); display($output); 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') { - $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); check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); return $exit_code; diff --git a/app/init_systems/systemd.php b/app/init_systems/systemd.php index 3879761..b0c0224 100644 --- a/app/init_systems/systemd.php +++ b/app/init_systems/systemd.php @@ -24,7 +24,7 @@ class systemd { 'isolate'=>'isolate', // Change system runlevel target 'log'=>'journalctl -u', // View service log 'realtime'=>'journalctl -f', // watch live logs - default=>'restart', + default=>false, }; } @@ -37,21 +37,21 @@ class systemd { 'reload'=>'reload', 'enable'=>'enable', 'disable'=>'disable', - default=>'restart', + default=>false, }; } public static function service(string $name, string $action = 'restart') { - $my_action = get_valid_action_for_service($action); - exec(self::get_super_user_bin . 'service ' . safe_cmd($name, $my_action), $output, $exit_code); + $my_action = self::get_valid_action_for_service($action); + exec(\neato::get_super_user_bin . 'service ' . safe_cmd($name, $my_action), $output, $exit_code); display($output); check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); return $exit_code; } public static function systemctl(string $name, string $action = 'enable') { - $my_action = get_valid_action_for_system_ctl($action); - exec(self::get_bin . 'systemctl ' . safe_cmd($my_action, $name), $output, $exit_code); + $my_action = self::get_valid_action_for_system_ctl($action); + exec(\neato::get_bin . 'systemctl ' . safe_cmd($my_action, $name), $output, $exit_code); check_for_error($exit_code, "Unable to {$action} Service called: {$name}"); return $exit_code; } diff --git a/app/neato_common.php b/app/neato_common.php index 415b309..22cbbb6 100644 --- a/app/neato_common.php +++ b/app/neato_common.php @@ -136,7 +136,7 @@ function recursive_change_owner($mypath, $uid, $gid) { if ($file != "." && $file != "..") { $typepath = $mypath . "/" . $file; if (filetype($typepath) == 'dir') { - recursive_chown_chgrp($typepath, $uid, $gid); + recursive_change_owner($typepath, $uid, $gid); } chown($typepath, $uid); diff --git a/app/neato_configure.php b/app/neato_configure.php index e24e3f3..66d9e28 100644 --- a/app/neato_configure.php +++ b/app/neato_configure.php @@ -4,12 +4,12 @@ final class configure { private static $config = array(); protected function __construct() { } - /** + /* * Fetches a setting set from using Configure::set() or add or update * - * @param string $name The name of the setting to get - * @param string $key [optional] The Array Key to fetch - * @return mixed The setting specified by $name, or null if $name was not set + * $name The name of the setting to get + * $key [optional] The Array Key to fetch + * The setting specified by $name, or null if $name was not set * * return type: ?array */ @@ -26,11 +26,11 @@ final class configure { return null; } - /** + /* * Checks if the setting exists * - * @param string $name The name of the setting to check existance - * @return boolean true if $name was set, false otherwise + * $name The name of the setting to check existance + * return boolean true if $name was set, false otherwise */ public static function exists(string $name): bool { if (array_key_exists(strtolower($name), self::$config)) { @@ -39,58 +39,56 @@ final class configure { return false; } - /** + /* * Overwrite/Update/Add to $config - * @param string $name the main key to update - * @param string $key the sub key - * @param type $value the data to update + * $name the main key to update + * $key the sub key + * type $value the data to update */ public static function update(string $name, string $key, $value): void { self::$config[strtolower($name)][strtolower($key)] = $value; } - /** + /* * Add to existing data without loss... to $config - * @param string $name the main key - * @param string $key the sub key - * @param type $value new data to add + * $name the main key + * $key the sub key + * $value new data to add */ public static function add(string $name, string $key, $value): void { self::$config[strtolower($name)][strtolower($key)][] = $value; } - /** + /* * Frees the setting given by $name, if it exists. All settings no longer in * 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 { if (self::exists($name)) unset(self::$config[strtolower($name)]); } - /** + /* * 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 - * @param mixed $value The value to set + * $value The value to set */ public static function set(string $name, $value): void { self::$config[strtolower($name)] = $value; } - /** + /* * Sets $config data from an Array - * @param array $a ($name => $value) - * @retval void + * array $a ($name => $value) + * retutns a void */ public static function load_array(array $a): void { - if (isset($a) && is_array($a)) { - foreach ($a as $name => $value) { - self::$config[strtolower($name)] = $value; - } + foreach ($a as $name => $value) { + self::$config[strtolower($name)] = $value; } unset($a); } diff --git a/app/neato_logger.php b/app/neato_logger.php index 14f3d23..d6bfa85 100644 --- a/app/neato_logger.php +++ b/app/neato_logger.php @@ -34,12 +34,12 @@ class logger { $success = touch($file); if ($success === false) { $this->handle = false; - return false; + throw new \Exception('Unable to touch file:' . $file); } chmod($file, 0660); if (! is_writable($file)) { $this->handle = false; - return false; + throw new \Exception('Unable to write to file:' . $file); } $this->handle = fopen($file, 'a'); } diff --git a/app/package_managers/apk.php b/app/package_managers/apk.php index 82abdc5..8e79b8b 100644 --- a/app/package_managers/apk.php +++ b/app/package_managers/apk.php @@ -45,7 +45,7 @@ class apk { } 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); return $exit_code; } diff --git a/app/package_managers/apt.php b/app/package_managers/apt.php index b014744..af807ed 100644 --- a/app/package_managers/apt.php +++ b/app/package_managers/apt.php @@ -11,8 +11,9 @@ class apt { return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false]; } - public static function upgrade(string $prog) { - exec(\neato::get_user_bin . 'apt upgrade ' . $assume . safe_cmd_quotes($prog) . stderr(), $output, $exit_code); + public static function upgrade(string $prog, bool $auto=true) { + $assume = ($auto) ? "-y " : ""; + exec(\neato::get_user_bin . 'apt upgrade ' . $assume . safe_cmd_quotes($prog) . stderr(), $output, $exit_code); display($output); check_for_error($exit_code, "Unable to upgrade: {$prog}"); return $exit_code; diff --git a/app/package_managers/flatpak.php b/app/package_managers/flatpak.php index f3ca79f..854e723 100644 --- a/app/package_managers/flatpak.php +++ b/app/package_managers/flatpak.php @@ -11,8 +11,8 @@ class flatpak { return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false]; } - public static function upgrade(string $prog) { - return self::update($prog); + public static function upgrade() { + return self::update(); } public static function install(string $prog) { diff --git a/app/package_managers/snap.php b/app/package_managers/snap.php index 74d86ff..3042860 100644 --- a/app/package_managers/snap.php +++ b/app/package_managers/snap.php @@ -11,8 +11,8 @@ class snap { return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false]; } - public static function upgrade(string $prog) { - return self::update($prog); + public static function upgrade() { + return self::update(); } public static function install(string $prog) { diff --git a/app/traits/init_systems.php b/app/traits/init_systems.php index eb0f7df..0211bb0 100644 --- a/app/traits/init_systems.php +++ b/app/traits/init_systems.php @@ -25,7 +25,7 @@ trait init_systems { if ($my_init === false) { return false; } - $namespaced = "\\package_managers\\{$my_init}::service"; + $namespaced = "\\init_systems\\{$my_init}::service"; return $namespaced($name, $action); } diff --git a/app/traits/linux_core.php b/app/traits/linux_core.php index c6e16a1..375df21 100644 --- a/app/traits/linux_core.php +++ b/app/traits/linux_core.php @@ -58,7 +58,7 @@ trait linux_core { public static function groupadd(string $groupname, int $gid = 0) { $group_id = ($gid > 0) ? "-g {$gid} " : ""; 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'])); } check_for_error($exit_code, "Unable to add new group: {$groupname}"); @@ -67,7 +67,7 @@ trait linux_core { public static function userdel(string $username) { 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'])); } check_for_error($exit_code, "Unable to delete user: {$username}"); @@ -78,7 +78,7 @@ trait linux_core { $user_id = ($uid > 0) ? "-u {$uid} " : ""; $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); - if ($exit_code === true || $exit_code === 0) { + if ($exit_code === 0) { display(getTermColors("Added new user account named: $username", ['color'=>'green'])); } check_for_error($exit_code, "Unable to add new user: {$username}"); diff --git a/app/traits/packages.php b/app/traits/packages.php index e655608..2d4ae02 100644 --- a/app/traits/packages.php +++ b/app/traits/packages.php @@ -4,6 +4,15 @@ namespace traits; 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 { $linux_package_managers = [ 'ubuntu' => 'apt', diff --git a/app/utils/keygen.php b/app/utils/keygen.php index f8ef1bc..41cbfcf 100644 --- a/app/utils/keygen.php +++ b/app/utils/keygen.php @@ -18,7 +18,7 @@ class keygen { 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); display($output); check_for_error($exit_code, "Unable to run ssh-keygen command: {$file}"); diff --git a/app/utils/php.php b/app/utils/php.php index 381d9d3..86973c9 100644 --- a/app/utils/php.php +++ b/app/utils/php.php @@ -4,7 +4,7 @@ namespace utils; class php { - function r_sapi($ver) { + private static function r_sapi($ver) { if (is_string_found($ver, "-s ") && is_string_found($ver, "-v ")) { return $ver; } @@ -18,13 +18,13 @@ class php { } 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}"); return $exit_code; } 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}"); return $exit_code; } diff --git a/app/utils/php_composer.php b/app/utils/php_composer.php index b9e8ff6..d7e0e2a 100644 --- a/app/utils/php_composer.php +++ b/app/utils/php_composer.php @@ -2,6 +2,8 @@ namespace utils; +// composer require --dev phpstan/phpstan + class php_composer { const composer_exe = "/usr/local/bin/composer"; @@ -36,6 +38,12 @@ class php_composer { 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) { exec(self::composer_exe . " -d " . safe_cmd($dir) . " remove " . safe_cmd($vendor), $output, $exit_code); check_for_error($exit_code, "PHP Composer remove error"); diff --git a/app/utils/ufw.php b/app/utils/ufw.php index c8214cf..102eecb 100644 --- a/app/utils/ufw.php +++ b/app/utils/ufw.php @@ -3,13 +3,11 @@ namespace utils; class ufw { - /** - * - * @param type $action - allow or deny - * @param type $port - Port#/tcp - * @return type + /* + * $action (allow or deny) + * $port (Port#/tcp) */ - public static function make_rule($action, $port) { + public static function make_rule(string $action, string $port): int|false { switch($action) { case 'allow': case 'deny': case 'reject': case 'limit': break; default: diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2216211 --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require-dev": { + "phpstan/phpstan": "^1.10" + } +} diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..82df8d3 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + level: 4 + paths: + - app