diff --git a/index.php b/index.php index 20ed4df..8451a20 100644 --- a/index.php +++ b/index.php @@ -3,22 +3,20 @@ /** * Application: Laragon | Server Index Page * Description: This is the main index page for the Laragon server, displaying server info, server vitals, sendmail - * mailbox and applications Author: Tarek Tarabichi - * improved CakePHP and Joomla detection - - Contributors: - - @LrkDev in v.2.1.2 - - @luisAntonioLAGS in v.2.2.1 Spanish - - @martic in 2.3.5 Dynamic Hostname Detection - - Application Version: 2.3.6 + * mailbox and applications + * Author: Tarek Tarabichi + * Improved CakePHP and Joomla detection + * + * Contributors: + * - @LrkDev in v.2.1.2 + * - @luisAntonioLAGS in v.2.2.1 Spanish + * - @martic in 2.3.5 Dynamic Hostname Detection + * + * Version: 2.3.6 */ -//----------------------------------------------------------------------------------- // Load language files -//----------------------------------------------------------------------------------- -function loadLanguage($lang) -{ +function loadLanguage($lang) { $langFile = __DIR__ . "/assets/languages/{$lang}.json"; if (file_exists($langFile)) { return json_decode(file_get_contents($langFile), true); @@ -26,10 +24,8 @@ function loadLanguage($lang) return []; } -//----------------------------------------------------------------------------------- -// Detect language preference (example: default to English) -//----------------------------------------------------------------------------------- -$lang = isset($_GET['lang']) ? $_GET['lang'] : 'en'; +// Detect language preference (default to English) +$lang = $_GET['lang'] ?? 'en'; $translations = loadLanguage($lang); const SERVER_TYPES = [ @@ -37,33 +33,24 @@ function loadLanguage($lang) 'apache' => 'apache', ]; -//----------------------------------------------------------------------------------- -// Displays server status including uptime, memory usage, and disk usage. -//----------------------------------------------------------------------------------- -function showServerStatus(): void -{ +// Display server status +function showServerStatus(): void { echo '

Server Status

'; // Display server uptime $uptime = shell_exec('uptime'); - echo '

Uptime

'; - echo '

' . htmlspecialchars($uptime) . '

'; + echo '

Uptime

' . htmlspecialchars($uptime) . '

'; // Display memory usage - $free = shell_exec('free -m'); // memory in MB - echo '

Memory Usage (in MB)

'; - echo '
' . htmlspecialchars($free) . '
'; + $free = shell_exec('free -m'); + echo '

Memory Usage (in MB)

' . htmlspecialchars($free) . '
'; // Display disk usage - $df = shell_exec('df -h'); // disk usage in human-readable format - echo '

Disk Usage

'; - echo '
' . htmlspecialchars($df) . '
'; + $df = shell_exec('df -h'); + echo '

Disk Usage

' . htmlspecialchars($df) . '
'; } -//----------------------------------------------------------------------------------- -// Handles incoming query parameters and executes corresponding functionality. -//----------------------------------------------------------------------------------- -function handleQueryParameter(string $param): void -{ +// Handle incoming query parameters +function handleQueryParameter(string $param): void { switch ($param) { case 'info': phpinfo(); @@ -76,7 +63,6 @@ function handleQueryParameter(string $param): void } } -// Check if 'q' parameter is set and sanitize it if (isset($_GET['q'])) { $queryParam = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING); try { @@ -86,14 +72,8 @@ function handleQueryParameter(string $param): void } } -const SERVER_PHP = 'php'; -const SERVER_APACHE = 'apache'; - -//----------------------------------------------------------------------------------- -// Retrieves a list of PHP extensions or Apache modules based on the specified server type. -//----------------------------------------------------------------------------------- -function getServerExtensions(string $server, int $columns = 2): array -{ +// Retrieve server extensions +function getServerExtensions(string $server, int $columns = 2): array { switch ($server) { case SERVER_PHP: $extensions = get_loaded_extensions(); @@ -110,16 +90,11 @@ function getServerExtensions(string $server, int $columns = 2): array } sort($extensions, SORT_STRING); - $extensions = array_chunk($extensions, $columns); - - return $extensions; + return array_chunk($extensions, $columns); } -//----------------------------------------------------------------------------------- -// Fetches the latest PHP version from the official PHP website and compares it with the current PHP version running on the server. -//----------------------------------------------------------------------------------- -function getPhpVersion(): array -{ +// Fetch PHP version +function getPhpVersion(): array { $url = 'https://www.php.net/releases/index.php?json&version=7'; $options = [ "ssl" => [ @@ -147,24 +122,8 @@ function getPhpVersion(): array ]; } -//----------------------------------------------------------------------------------- -// Ensure $serverInfo is defined and initialized with serverInfo() function call -//----------------------------------------------------------------------------------- -$serverInfo = serverInfo(); // Make sure this line is placed before you try to access $serverInfo - -// Before accessing an array offset, check if the variable is an array and not null -if (is_array($serverInfo) && isset($serverInfo['httpdVer'])) { - //echo $serverInfo['httpdVer']; // Safely access the 'httpdVer' index -} else { - // Handle the case where $serverInfo is not an array or the index 'httpdVer' is not set - echo "Server information is not available."; -} - -//----------------------------------------------------------------------------------- -// Gathers information about the server environment including versions of HTTP server, OpenSSL, PHP, and Xdebug, as well as document root and server name. -//----------------------------------------------------------------------------------- -function serverInfo(): array -{ +// Gather server information +function serverInfo(): array { $serverSoftware = $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown Server Software'; $serverParts = explode(' ', $serverSoftware); @@ -184,14 +143,11 @@ function serverInfo(): array ]; } -//----------------------------------------------------------------------------------- -// Retrieves the MySQL version by executing a shell command. -//----------------------------------------------------------------------------------- -function getSQLVersion(): string -{ +// Retrieve MySQL version +function getSQLVersion(): string { $output = shell_exec('mysql -V'); if ($output === null) { - return "Unknown"; // Command failed to execute, possibly not installed or not in PATH + return "Unknown"; } if (preg_match('@[0-9]+\.[0-9]+\.[0-9-\w]+@', $output, $version)) { @@ -201,11 +157,8 @@ function getSQLVersion(): string return "Unknown"; } -//----------------------------------------------------------------------------------- -// Generates download and changelog links for a specific PHP version. -//----------------------------------------------------------------------------------- -function phpDlLink(string $version, string $branch = '7', string $architecture = 'x64'): array -{ +// Generate PHP download and changelog links +function phpDlLink(string $version, string $branch = '7', string $architecture = 'x64'): array { $versionEscaped = htmlspecialchars($version, ENT_QUOTES, 'UTF-8'); $branchEscaped = htmlspecialchars($branch, ENT_QUOTES, 'UTF-8'); $architectureEscaped = htmlspecialchars($architecture, ENT_QUOTES, 'UTF-8'); @@ -216,13 +169,8 @@ function phpDlLink(string $version, string $branch = '7', string $architecture = ]; } -//----------------------------------------------------------------------------------- -// Determines the directory path for server-specific site configuration based on the server software. -//----------------------------------------------------------------------------------- -function getSiteDir(): string -{ - // Check if laragon is installed on C or D drive and this then checks whichever works and uses it - // so it would be $rootDir = 'C:/laragon/etc/apache2/sites-enabled'; or $rootDir = 'D:/laragon/etc/apache2/sites-enabled'; +// Determine site directory +function getSiteDir(): string { $drive = strtoupper(substr(PHP_OS, 0, 1)); $rootDir = $drive . ':/laragon/etc/apache2/sites-enabled'; if (strpos(strtolower($rootDir), 'c:') !== false) { @@ -230,13 +178,11 @@ function getSiteDir(): string } else { $laragonDir = $rootDir; } - $rootDir = 'D:/laragon/etc/apache2/sites-enabled'; if ($rootDir === false) { throw new RuntimeException("Unable to determine the root directory."); } - // Ensures that SERVER_SOFTWARE is set and not empty if (!isset($_SERVER['SERVER_SOFTWARE']) || trim($_SERVER['SERVER_SOFTWARE']) === '') { throw new InvalidArgumentException("Server software is not defined in the server environment."); } @@ -246,44 +192,37 @@ function getSiteDir(): string if (strpos($serverSoftware, 'apache') !== false) { return $rootDir; } elseif (strpos($serverSoftware, 'nginx') !== false) { - return $rootDir; // Adjust this if needed + return $rootDir; } throw new InvalidArgumentException("Unsupported server type: " . htmlspecialchars($serverSoftware)); } -//----------------------------------------------------------------------------------- -// Detect WordPress updates -//----------------------------------------------------------------------------------- -function checkWordPressUpdates($wpPath) -{ - // Run WP-CLI command to check for updates +// Check for WordPress updates +function checkWordPressUpdates($wpPath) { $command = "cd $wpPath && wp core check-update --format=json"; $output = shell_exec($command); if ($output) { $updates = json_decode($output, true); if (!empty($updates)) { - return true; // Updates are available + return true; } } - return false; // No updates available + return false; } -//----------------------------------------------------------------------------------- -// Fetches configuration details for local sites based on server configuration files. -//----------------------------------------------------------------------------------- -function getLocalSites($server = 'apache', $ignoredFiles = ['.', '..', '00-default.conf']): array -{ +// Fetch local sites configuration +function getLocalSites($server = 'apache', $ignoredFiles = ['.', '..', '00-default.conf']): array { try { - $sitesDir = getSiteDir(); // Assume getSiteDir() throws an exception if unable to determine the directory + $sitesDir = getSiteDir(); $files = scandir($sitesDir); if ($files === false) { throw new Exception("Failed to scan directory: " . htmlspecialchars($sitesDir)); } } catch (Exception $e) { - error_log($e->getMessage()); // Log the error to PHP's error log - return []; // Return an empty array to indicate failure gracefully + error_log($e->getMessage()); + return []; } $scanDir = array_diff($files, $ignoredFiles); @@ -292,12 +231,12 @@ function getLocalSites($server = 'apache', $ignoredFiles = ['.', '..', '00-defau foreach ($scanDir as $filename) { $path = realpath("$sitesDir/$filename"); if ($path === false || !is_file($path)) { - continue; // Skip invalid paths or directories + continue; } $config = file_get_contents($path); if ($config === false) { - continue; // Skip files that can't be read + continue; } if ( @@ -311,7 +250,6 @@ function getLocalSites($server = 'apache', $ignoredFiles = ['.', '..', '00-defau 'documentRoot' => htmlspecialchars($documentRootMatches[1]), ]; - // Detect WordPress and check for updates if (file_exists($documentRootMatches[1] . '/wp-admin')) { $site['framework'] = 'WordPress'; $site['hasUpdates'] = checkWordPressUpdates($documentRootMatches[1]); @@ -330,11 +268,8 @@ function getLocalSites($server = 'apache', $ignoredFiles = ['.', '..', '00-defau return $sites; } -//-------------------------------------------------------- -// Renders HTML links for local sites with XSS prevention. -//-------------------------------------------------------- -function renderLinks(): string -{ +// Render HTML links for local sites +function renderLinks(): string { ob_start(); $sites = getLocalSites(); @@ -365,17 +300,16 @@ function renderLinks(): string $rootPath = realpath(__DIR__); $folders = array_filter(glob($rootPath . '/*'), 'is_dir'); -$ignore_dirs = array('.', '..', 'logs', 'access-logs', 'vendor', 'favicon_io', 'ablepro-90', 'assets'); +$ignore_dirs = ['.', '..', 'logs', 'access-logs', 'vendor', 'favicon_io', 'ablepro-90', 'assets']; foreach ($folders as $folderPath) { $host = basename($folderPath); if (in_array($host, $ignore_dirs)) { - continue; // Skip ignored directories + continue; } } -// defaults and opens servers tab as default view on initialisation -$activeTab = isset($_GET['tab']) ? $_GET['tab'] : 'servers'; +$activeTab = $_GET['tab'] ?? 'servers'; ?> @@ -399,18 +333,12 @@ function renderLinks(): string - - - - - - @@ -419,381 +347,376 @@ function renderLinks(): string - @@ -802,33 +725,30 @@ function fetchServerVitals() {

format('H'); - -// Get the greeting based on the time of day -if ($hours < 12) { - $greeting = $translations['good_morning'] ?? 'Good morning'; -} elseif ($hours < 18) { - $greeting = $translations['good_afternoon'] ?? 'Good afternoon'; -} else { - $greeting = $translations['good_evening'] ?? 'Good evening'; -} + $currentTime = new DateTime(); + $hours = $currentTime->format('H'); + + if ($hours < 12) { + $greeting = $translations['good_morning'] ?? 'Good morning'; + } elseif ($hours < 18) { + $greeting = $translations['good_afternoon'] ?? 'Good afternoon'; + } else { + $greeting = $translations['good_evening'] ?? 'Good evening'; + } -// Display the greeting -echo "

" . $greeting . "!

"; -?> + echo "

" . $greeting . "!

"; + ?>
@@ -838,12 +758,8 @@ function fetchServerVitals() {
-
- - -
@@ -879,19 +795,19 @@ function fetchServerVitals() {
MySQL
+ error_reporting(0); + $laraconfig = parse_ini_file('../usr/laragon.ini'); + + $link = mysqli_connect('localhost', 'root', $laraconfig['MySQLRootPassword']); + if (!$link) { + $link = mysqli_connect('localhost', 'root', ''); + } + if (!$link) { + echo 'MySQL not running!'; + } else { + printf("server version: %s\n", htmlspecialchars(mysqli_get_server_info($link))); + } + ?>
@@ -920,87 +836,87 @@ function fetchServerVitals() { Full 6.0.220916
+ +
+
Server Controls
+
+ + +
+
-
-

Server Controls

- - -
' . $app_name . '
Admin'; - break; - case file_exists($host . '/wp-admin'): - $app_name = ' Wordpress '; - $avatar = 'assets/Wordpress.png'; - $admin_link = '' . $app_name . '
Admin
'; - break; - case file_exists($host . '/administrator'): - $app_name = ' Joomla '; - $avatar = 'assets/Joomla.png'; - $admin_link = '' . $app_name . '
Admin
'; - break; - case file_exists($host . '/public/index.php') && is_dir($host . '/app') && file_exists($host . '/.env'): - $app_name = ' Laravel '; - $avatar = 'assets/Laravel.png'; - $admin_link = ''; - break; - case file_exists($host . '/bin/console'): - $app_name = ' Symfony '; - $avatar = 'assets/Symfony.png'; - $admin_link = '' . $app_name . '
Admin
'; - break; - case (file_exists($host . '/') && is_dir($host . '/app.py') && is_dir($host . '/static') && file_exists($host . '/.env')): - $app_name = ' Python '; - $avatar = 'assets/Python.png'; - $admin_link = '' . $app_name . '
Public Folder
'; - - $command = 'python ' . htmlspecialchars($host) . '/app.py'; - exec($command, $output, $returnStatus); - break; - case file_exists($host . '/bin/cake'): - $app_name = ' CakePHP '; - $avatar = 'assets/CakePHP.png'; - $admin_link = '' . $app_name . '
Admin
'; - break; - default: - $admin_link = ''; - $avatar = 'assets/Unknown.png'; - break; - } + $ignored = ['favicon_io']; + $folders = array_filter(glob('*'), 'is_dir'); - echo ''; -} -?> + if ($laraconfig['SSLEnabled'] == 0 || $laraconfig['Port'] == 80) { + $url = 'http'; + } else { + $url = 'https'; + } + $ignore_dirs = ['.', '..', 'logs', 'access-logs', 'vendor', 'favicon_io', 'ablepro-90', 'assets']; + foreach ($folders as $host) { + if (in_array($host, $ignore_dirs) || !is_dir($host)) { + continue; + } + + $admin_link = ''; + $app_name = ''; + $avatar = ''; + + switch (true) { + case (file_exists($host . '/core') || file_exists($host . '/web/core')): + $app_name = ' Drupal '; + $avatar = 'assets/Drupal.svg'; + $admin_link = '' . $app_name . '
Admin
'; + break; + case file_exists($host . '/wp-admin'): + $app_name = ' Wordpress '; + $avatar = 'assets/Wordpress.png'; + $admin_link = '' . $app_name . '
Admin
'; + break; + case file_exists($host . '/administrator'): + $app_name = ' Joomla '; + $avatar = 'assets/Joomla.png'; + $admin_link = '' . $app_name . '
Admin
'; + break; + case file_exists($host . '/public/index.php') && is_dir($host . '/app') && file_exists($host . '/.env'): + $app_name = ' Laravel '; + $avatar = 'assets/Laravel.png'; + $admin_link = ''; + break; + case file_exists($host . '/bin/console'): + $app_name = ' Symfony '; + $avatar = 'assets/Symfony.png'; + $admin_link = '' . $app_name . '
Admin
'; + break; + case (file_exists($host . '/') && is_dir($host . '/app.py') && is_dir($host . '/static') && file_exists($host . '/.env')): + $app_name = ' Python '; + $avatar = 'assets/Python.png'; + $admin_link = '' . $app_name . '
Public Folder
'; + + $command = 'python ' . htmlspecialchars($host) . '/app.py'; + exec($command, $output, $returnStatus); + break; + case file_exists($host . '/bin/cake'): + $app_name = ' CakePHP '; + $avatar = 'assets/CakePHP.png'; + $admin_link = '' . $app_name . '
Admin
'; + break; + default: + $admin_link = ''; + $avatar = 'assets/Unknown.png'; + break; + } + + echo ''; + } + ?>
- - -
@@ -1009,9 +925,6 @@ function fetchServerVitals() {
- - -
@@ -1051,19 +964,17 @@ function fetchServerVitals() {
- - - +