From 35e426c1582b81104ec088ec0e0606442d91453c Mon Sep 17 00:00:00 2001 From: iaretechnician Date: Sun, 23 Nov 2025 17:13:22 -0500 Subject: [PATCH] server log parser --- modules/config_games/server_config_parser.php | 137 +++++++++++++++--- 1 file changed, 113 insertions(+), 24 deletions(-) diff --git a/modules/config_games/server_config_parser.php b/modules/config_games/server_config_parser.php index c0e85345..c4bd4a18 100644 --- a/modules/config_games/server_config_parser.php +++ b/modules/config_games/server_config_parser.php @@ -25,41 +25,130 @@ define("SERVER_CONFIG_LOCATION","modules/config_games/server_configs/"); define("XML_SCHEMA","modules/config_games/schema_server_config.xml"); +if (!function_exists('ogp_render_config_error')) { + function ogp_render_config_error($title, $details = "") + { + $log_message = "[OGP] $title" . (empty($details) ? "" : " Details: $details"); + error_log($log_message); + + if (PHP_SAPI === 'cli') { + fwrite(STDERR, $log_message . PHP_EOL); + exit(1); + } + + if (!headers_sent()) { + header('Content-Type: text/html; charset=utf-8', true, 500); + } + + echo "
"; + echo "

Open Game Panel

"; + echo "

$title

"; + if (!empty($details)) { + echo "

" . nl2br(htmlspecialchars($details, ENT_QUOTES, 'UTF-8')) . "

"; + } + echo "

Please install the missing PHP requirements or restore the referenced configuration file, then reload this page.

"; + echo "
"; + exit; + } +} + +if (!function_exists('ogp_render_missing_xml_extensions')) { + function ogp_render_missing_xml_extensions($missing_extensions) + { + $pretty = implode(', ', $missing_extensions); + $details = "Missing PHP extension(s): {$pretty}\n" + . "Install the php-xml package and restart Apache/PHP-FPM.\n" + . "Debian/Ubuntu: sudo apt install php-xml\n" + . "RHEL/CentOS: sudo dnf install php-xml"; + ogp_render_config_error("Required PHP XML extensions are not available.", $details); + } +} + +if (!function_exists('ogp_format_libxml_errors')) { + function ogp_format_libxml_errors() + { + $errors = libxml_get_errors(); + if (empty($errors)) { + return "No additional libxml details are available."; + } + $messages = array(); + foreach ($errors as $error) { + $messages[] = trim($error->message) . " (Line {$error->line}, Column {$error->column})"; + } + libxml_clear_errors(); + return implode("\n", $messages); + } +} + +if (!function_exists('ogp_ensure_xml_support')) { + function ogp_ensure_xml_support() + { + static $checked = false; + if ($checked) { + return; + } + + $missing = array(); + if (!extension_loaded('libxml')) { + $missing[] = 'libxml'; + } + if (!class_exists('DOMDocument')) { + $missing[] = 'dom'; + } + if (!function_exists('simplexml_load_file')) { + $missing[] = 'simplexml'; + } + + if (!empty($missing)) { + ogp_render_missing_xml_extensions($missing); + } + + $checked = true; + } +} + /// \return FALSE in case of failure in parsing. /// \return array containing the elements on success. function read_server_config( $filename ) { - if ( !class_exists('DOMDocument') ) + ogp_ensure_xml_support(); + + if (!is_readable($filename)) { + ogp_render_config_error( + "Game configuration file is missing or unreadable.", + "Expected at: {$filename}" + ); + } + + $previous_error_state = libxml_use_internal_errors(true); + $dom = new DOMDocument(); + if ($dom->load($filename) === FALSE) { - print_failure("PHP DOM extension not found. Install the php-xml package to enable game configuration parsing."); - return FALSE; + ogp_render_config_error( + "Unable to load XML configuration.", + "File: {$filename}\n".ogp_format_libxml_errors() + ); } - if ( !function_exists('simplexml_load_file') ) + if ( $dom->schemaValidate(XML_SCHEMA) !== TRUE ) { - print_failure("PHP SimpleXML extension not found. Install the php-xml package to enable game configuration parsing."); - return FALSE; + ogp_render_config_error( + "XML configuration failed schema validation.", + "File: {$filename}\nSchema: ".XML_SCHEMA."\n".ogp_format_libxml_errors() + ); } - $dom = new DOMDocument(); - if ( $dom->load($filename) === FALSE ) - { - print_failure(get_lang_f('unable_to_load_xml',$filename)); - return FALSE; - } - if ( $dom->schemaValidate(XML_SCHEMA) != TRUE ) - { - print_failure(get_lang_f('xml_file_not_valid',$filename,XML_SCHEMA)); - return FALSE; - } - - $xml = simplexml_load_file($filename); - if($xml !== false){ - $xml->addChild('home_cfg_file',basename($filename)); - return $xml; + $xml = simplexml_import_dom($dom); + if($xml === false){ + ogp_render_config_error( + "Failed to parse XML configuration.", + "File: {$filename}\n".ogp_format_libxml_errors() + ); } - - return false; + + $xml->addChild('home_cfg_file',basename($filename)); + libxml_use_internal_errors($previous_error_state); + return $xml; } function xml_get_mod( $server_xml, $mod_key )