From 0bd1fcd2f1f2cbf197409e271e80d5e63cef4a48 Mon Sep 17 00:00:00 2001 From: "Adrian Preu\\303\\237" Date: Sat, 10 Aug 2024 09:43:53 -0700 Subject: [PATCH] Fix some module handling --- .gitignore | 4 ++ classes/Accounting/Session.class.php | 17 +++++-- classes/Services/PHP.class.php | 68 ++++++++++++++++++++----- classes/System/Core.class.php | 72 +++++++++++++++++++++++++-- classes/System/Loader.class.php | 4 +- default/admin/modules/list.php | 2 + default/css/global.css | 9 ++++ handler/admin/modules.php | 74 +++++++++++++++++++++++++++- ~DEMO | 1 - 9 files changed, 225 insertions(+), 26 deletions(-) delete mode 100644 ~DEMO diff --git a/.gitignore b/.gitignore index 26f0b89..85f3028 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,9 @@ .mail.php .config.php .DEBUG +~DEBUG +.DEMO +~DEMO +.fruithost .idea/* temp/* \ No newline at end of file diff --git a/classes/Accounting/Session.class.php b/classes/Accounting/Session.class.php index 2316186..cbf3293 100644 --- a/classes/Accounting/Session.class.php +++ b/classes/Accounting/Session.class.php @@ -14,12 +14,21 @@ if(!isset($_SESSION)) { #@session_save_path(sprintf('%s%stemp', dirname(PATH), DS)); - @session_start(/*[ - 'cookie_lifetime' => 3600, - 'read_and_close' => false - ]*/); + @session_start([ + //'cookie_lifetime' => 3600, + 'read_and_close' => true + ]); } } + + public static function debug() : void { + var_dump([ + 'HTTP_COOKIE' => $_SERVER['HTTP_COOKIE'], + 'COOKIE' => $_COOKIE, + 'session_id' => session_id(), + 'data' => $_SESSION + ]); + } public static function getID() : string | false { return session_id(); diff --git a/classes/Services/PHP.class.php b/classes/Services/PHP.class.php index 56e3da6..fbece42 100644 --- a/classes/Services/PHP.class.php +++ b/classes/Services/PHP.class.php @@ -11,11 +11,12 @@ use fruithost\Localization\I18N; use fruithost\Security\Encryption; + use fruithost\Accounting\Auth; use fruithost\System\Utils; class PHP { private ?string $path = null; - private string $socket = '/run/php/php8.2-fpm.sock'; // @ToDo HARD CODED value, we should find a way to get this value from the system + private string $socket = '/var/lib/apache2/fcgid/sock'; //'/run/php/php8.2-fpm.sock'; // @ToDo HARD CODED value, we should find a way to get this value from the system private ?string $content = null; private array $data = []; private bool $error = false; @@ -29,14 +30,24 @@ } public function getBody() : ?string { - return explode("\r\n\r\n", $this->content)[1]; + if(preg_match("/\r\n\r\n/Uis", $this->content)) { + return explode("\r\n\r\n", $this->content)[1]; + } else if(preg_match("/(cgi\-fcgi|command not found)/Uis", $this->content)) { + throw new \Exception('Can\'t access cgi-fcgi.'); + } else { + return $this->content; + } } public function getContent() : ?string { return $this->content; } - public function execute(string $file, array $arguments = []) : void { + public function isAvailable() : bool { + return !empty(shell_exec('which cgi-fcgi')); + } + + public function execute(string $file, array $arguments = [], ?string $password = null) : void { if(!is_writable($this->path)) { $this->error = true; return; @@ -56,15 +67,45 @@ $args .= sprintf('%s=%s \\%s', $name, $value, PHP_EOL); } - $command = sprintf('%scgi-fcgi -bind -connect "%s" 2>&1', $args, $this->socket); - $result = shell_exec($command); - - if($result === null){ - $this->error = true; - return; - } + // Check if cgi-fcgi available + if(!empty(shell_exec('which cgi-fcgi'))) { + $command = sprintf('%scgi-fcgi -bind -connect "%s" 2>&1', $args, $this->socket); - $this->content = $result; + if($password != null) { + $command = sprintf('bash -lc \'echo %s | /usr/bin/sudo -S %s\'', $password, $command); + } + + $result = shell_exec($command); + + if($result === null){ + $this->error = true; + return; + } + + $this->content = $result; + } else { + // @ToDo Check security(!) + $temp = sprintf('.fruithost.check.%s.php', Utils::randomString(5)); + $hash = Encryption::encrypt(Utils::randomString(28), ENCRYPTION_SALT); + + file_put_contents(sprintf('%s%s', $this->path, $temp), sprintf('', $hash, sprintf('%s%s', $this->path, $file), $arguments['MODULE'])); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, sprintf('https://%s/%s?FRUITHOST=%s&USERNAME=%s', $_SERVER['SERVER_NAME'], $temp, $hash, Auth::getUsername())); + curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Cookie: ' . $_SERVER['HTTP_COOKIE'] + ]); + $result = curl_exec($ch); + curl_close($ch); + + $this->content = $result; + @unlink(sprintf('%s%s', $this->path, $temp)); + } } public function exploding(string | array $name, mixed $data) : array | string { @@ -203,10 +244,11 @@ $file = sprintf('.fruithost.%s.php', Utils::randomString(5)); $hash = Encryption::encrypt(Utils::randomString(28), ENCRYPTION_SALT); - file_put_contents(sprintf('%s%s', $this->path, $file), sprintf('', $hash)); + file_put_contents(sprintf('%s%s', $this->path, $file), sprintf('', $hash)); $this->execute($file, [ - 'FRUITHOST' => $hash + 'FRUITHOST' => $hash, + 'MODULE' => sprintf('https://%s/%s?FRUITHOST=%s&USERNAME=%s', $_SERVER['SERVER_NAME'], $file, $hash, Auth::getUsername()) ]); @unlink(sprintf('%s%s', $this->path, $file)); diff --git a/classes/System/Core.class.php b/classes/System/Core.class.php index 1871bf0..0029c4a 100644 --- a/classes/System/Core.class.php +++ b/classes/System/Core.class.php @@ -20,8 +20,9 @@ use fruithost\Storage\Database; use fruithost\Templating\Template; use fruithost\UI\Icon; + use PHPMailer\Exception; - class Core extends Loader { + class Core extends Loader { private ?Modules $modules = null; private ?Hooks $hooks = null; private ?Router $router = null; @@ -190,12 +191,43 @@ $this->template->assign('error', Session::get('error')); Session::remove('error'); } - + + $errors = []; + foreach(($this->getModules()->getList()) AS $m) { - if($m != null && $m->getInstance() != null && method_exists($m->getInstance(), 'preLoad')) { - $m->getInstance()->preLoad(); + try { + if($m != null && $m->getInstance() != null && method_exists($m->getInstance(), 'preLoad')) { + $m->getInstance()->preLoad(); + } + } catch(\Exception $e) { + $errors[] = (object) [ + 'name' => $m->getInfo()->getName(), + 'message' => $e->getMessage(), + 'stack' => $e->getTrace() + ]; } } + + if(count($errors) > 0) { + $message = I18N::get('Following Modules have some Errors:'); + $message .= '
'; + + foreach($errors AS $entry) { + $message .= '
'; + $message .= $entry->name; + $message .= ': '; + $message .= $entry->message; + $message .= '
';
+						foreach($entry->stack AS $entry) {
+							if(!empty($entry['file']) && !empty($entry['line']) ) {
+								$message .= TAB . $entry['file'] . ':' . $entry['line'] . PHP_EOL;
+							}
+						}
+						$message .= '
'; + } + + $this->template->assign('error', $message); + } if(empty($module)) { $this->template->display('error/module', array_merge($this->template->getAssigns(), [ @@ -218,7 +250,37 @@ } if(method_exists($module->getInstance(), 'load')) { - $module->getInstance()->load($submodule, $action); + $errors = []; + + try { + $module->getInstance()->load($submodule, $action); + } catch(\Exception $e) { + $errors[] = (object) [ + 'name' => $m->getInfo()->getName(), + 'message' => $e->getMessage(), + 'stack' => $e->getTrace() + ]; + } + + if(count($errors) > 0) { + $message = I18N::get('Following Modules have some Errors:'); + + foreach($errors AS $entry) { + $message .= '
'; + $message .= $entry->name; + $message .= ': '; + $message .= $entry->message; + $message .= '
';
+							foreach($entry->stack AS $entry) {
+								if(!empty($entry['file']) && !empty($entry['line']) ) {
+									$message .= TAB . $entry['file'] . ':' . $entry['line'] . PHP_EOL;
+								}
+							}
+							$message .= '
'; + } + + $this->template->assign('error', $message); + } } if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && method_exists($module->getInstance(), 'onPOST')) { diff --git a/classes/System/Loader.class.php b/classes/System/Loader.class.php index a125622..5de4e2b 100644 --- a/classes/System/Loader.class.php +++ b/classes/System/Loader.class.php @@ -29,7 +29,7 @@ define('TAB', "\t"); define('BS', '\\'); define('DS', DIRECTORY_SEPARATOR); - + class Loader { public function __construct() { if((defined('DAEMON') && DAEMON) || (!empty($_SERVER['DAEMON']))) { @@ -85,7 +85,7 @@ print "\033[39m"; } } - + spl_autoload_register([ $this, 'load' ]); // @ToDo Hash verify? diff --git a/default/admin/modules/list.php b/default/admin/modules/list.php index 3e22548..c521817 100644 --- a/default/admin/modules/list.php +++ b/default/admin/modules/list.php @@ -40,6 +40,8 @@ $links['settings'] = sprintf('%s', $this->url('/admin/modules/?settings=' . $module->getDirectory()), I18N::get('Settings')); } + $links['check'] = sprintf('%s', $this->url('/admin/modules/?check=' . $module->getDirectory()), I18N::get('Check')); + $links['deinstall'] = sprintf('%s', $this->url('/admin/modules/?deinstall=' . $module->getDirectory()), I18N::get('Do you really wan\'t to deinstall the module?'), I18N::get('Deinstall')); print implode(' | ', $links); diff --git a/default/css/global.css b/default/css/global.css index 484e23f..29cbf46 100644 --- a/default/css/global.css +++ b/default/css/global.css @@ -21,4 +21,13 @@ input:focus { .dropdown-scroll { max-height: 145px; overflow-y: scroll; +} + +.btn-text:hover, +.btn-text:active, +.btn-text:focus { + color: var(--bs-body-color) !important; + background-color: transparent !important; + border-color: transparent !important; + cursor: normal !important; } \ No newline at end of file diff --git a/handler/admin/modules.php b/handler/admin/modules.php index dac45c2..4e2fcfe 100644 --- a/handler/admin/modules.php +++ b/handler/admin/modules.php @@ -3,6 +3,7 @@ use fruithost\Storage\Database; use fruithost\UI\Button; use fruithost\UI\Modal; + use fruithost\Services\PHP; $template->getAdminCore()->addModal((new Modal('add_repository', I18N::get('Add Repository'), 'admin/modules/repository/create'))->addButton([ (new Button())->setName('cancel')->setLabel(I18N::get('Cancel'))->addClass('btn-outline-danger')->setDismissable(), @@ -102,6 +103,78 @@ } else { $template->assign('error', I18N::get('The module not exists!')); } + } else if(isset($_GET['reinstall'])) { + if($modules->hasModule($_GET['reinstall'], true)) { + $module = $modules->getModule($_GET['reinstall'], true); + $install = sprintf('%s%s/setup/install.php', $module->getPath(), DS); + + if(file_exists($install)) { + try { + $php = new PHP(); + $php->setPath(PATH); + $php->execute('/classes/System/Loader.class.php', [ + 'DAEMON' => true, + 'REQUEST_URI' => '/', + 'MODULE' => $install + ]); + + if(preg_match('/(File Not Found|404 Not Found|500 Internal Server Error)/', $php->getHeader())) { + throw new \Exception('Installscript not found: ' . $install . PHP_EOL . $php->getHeader()); + } + + if(!empty($php->getBody())) { + $template->assign('error', I18N::get('The module has an problem:') . '
' . $php->getBody()); + } + } catch(\Exception $e) { + $template->assign('error', I18N::get('The module has an problem:') . '
' . $e->getMessage()); + } + } + + $template->assign('success', I18N::get('The module was successfully reinstalled!')); + } else { + $template->assign('error', I18N::get('The module not exists!')); + } + } else if(isset($_GET['check'])) { + if($modules->hasModule($_GET['check'], true)) { + $module = $modules->getModule($_GET['check'], true); + + if($module) { + $check = sprintf('%s%s/setup/check.php', $module->getPath(), DS); + $fix_it = sprintf('
+ ' . I18N::get('Try to fix the issue with') . ' + ' . I18N::get('Reinstall') . ' + ' . I18N::get('the module.') . ' +
', $this->url('/admin/modules/?reinstall=' . $module->getDirectory())); + + if(file_exists($check)) { + try { + $php = new PHP(); + $php->setPath(PATH); + $php->execute('/classes/System/Loader.class.php', [ + 'DAEMON' => true, + 'REQUEST_URI' => '/', + 'MODULE' => $check + ]); + + if(preg_match('/(File Not Found|404 Not Found|500 Internal Server Error)/', $php->getHeader())) { + throw new \Exception('Installscript not found: ' . $check . PHP_EOL . $php->getHeader()); + } + + if(!empty($php->getBody())) { + $template->assign('error', I18N::get('The module has an problem:') . '
' . $php->getBody() . $fix_it); + } + } catch(\Exception $e) { + $template->assign('error', I18N::get('The module has an problem:') . '
' . $e->getMessage()); + } + } + + $template->assign('success', sprintf(I18N::get('The module was successfully checked.'), $module->getInfo()->getName())); + } else { + $template->assign('error', I18N::get('The module has an instantiation problem!')); + } + } else { + $template->assign('error', I18N::get('The module not exists!')); + } } $template->assign('module', $module); @@ -109,7 +182,6 @@ $template->assign('repositorys', Database::fetch('SELECT * FROM `' . DATABASE_PREFIX . 'repositorys`')); $template->assign('modules', $template->getCore()->getModules()); - if(isset($_POST['action'])) { switch($_POST['action']) { case 'update': diff --git a/~DEMO b/~DEMO deleted file mode 100644 index 8b13789..0000000 --- a/~DEMO +++ /dev/null @@ -1 +0,0 @@ -