1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
use Core\CModule,
CController as CAction;
require_once dirname(__FILE__).'/CAutoloader.php';
class ZBase {
const EXEC_MODE_DEFAULT = 'default';
const EXEC_MODE_SETUP = 'setup';
const EXEC_MODE_API = 'api';
/**
* An instance of the current APP object.
*
* @var APP
*/
protected static $instance;
/**
* The absolute path to the root directory.
*
* @var string
*/
protected $rootDir;
/**
* @var array of config data from zabbix config file
*/
protected $config = [];
/**
* @var CAutoloader
*/
protected $autoloader;
/**
* @var CComponentRegistry
*/
private $component_registry;
/**
* Application mode.
*
* @var string
*/
private $mode;
/**
* @var CModuleManager
*/
private $module_manager;
/**
* Returns the current instance of APP.
*
* @static
*
* @return APP
*/
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new static;
}
return self::$instance;
}
/**
* Get component registry.
*
* @return CComponentRegistry
*/
public static function Component() {
return self::getInstance()->component_registry;
}
/**
* Get module manager.
*
* @return CModuleManager
*/
public static function ModuleManager() {
return self::getInstance()->module_manager;
}
/**
* Init modules required to run frontend.
*/
protected function init() {
$this->rootDir = $this->findRootDir();
$this->initAutoloader();
$this->component_registry = new CComponentRegistry;
// initialize API classes
$apiServiceFactory = new CApiServiceFactory();
$client = new CLocalApiClient();
$client->setServiceFactory($apiServiceFactory);
$wrapper = new CFrontendApiWrapper($client);
$wrapper->setProfiler(CProfiler::getInstance());
API::setWrapper($wrapper);
API::setApiServiceFactory($apiServiceFactory);
// system includes
require_once 'include/debug.inc.php';
require_once 'include/gettextwrapper.inc.php';
require_once 'include/defines.inc.php';
require_once 'include/func.inc.php';
require_once 'include/html.inc.php';
require_once 'include/perm.inc.php';
require_once 'include/audit.inc.php';
require_once 'include/js.inc.php';
require_once 'include/users.inc.php';
require_once 'include/validate.inc.php';
require_once 'include/locales.inc.php';
require_once 'include/db.inc.php';
require_once 'vendor/autoload.php';
// page specific includes
require_once 'include/actions.inc.php';
require_once 'include/discovery.inc.php';
require_once 'include/draw.inc.php';
require_once 'include/events.inc.php';
require_once 'include/graphs.inc.php';
require_once 'include/hostgroups.inc.php';
require_once 'include/hosts.inc.php';
require_once 'include/httptest.inc.php';
require_once 'include/images.inc.php';
require_once 'include/items.inc.php';
require_once 'include/maintenances.inc.php';
require_once 'include/maps.inc.php';
require_once 'include/media.inc.php';
require_once 'include/sounds.inc.php';
require_once 'include/triggers.inc.php';
}
/**
* Initializes the application.
*
* @param string $mode Application initialization mode.
*
* @throws Exception
*/
public function run($mode) {
$this->mode = $mode;
$this->init();
$this->setMaintenanceMode();
ini_set('display_errors', 'Off');
set_error_handler('zbx_err_handler');
switch ($mode) {
case self::EXEC_MODE_DEFAULT:
$file = basename($_SERVER['SCRIPT_NAME']);
$action_name = ($file === 'zabbix.php') ? getRequest('action', '') : $file;
if ($action_name === 'notifications.get') {
CWebUser::disableSessionExtension();
}
$this->loadConfigFile();
$this->initDB();
$this->setServerAddress();
$this->authenticateUser();
$this->initMessages();
$this->setLayoutModeByUrl();
$this->initComponents();
$this->initModuleManager();
$router = $this->component_registry->get('router');
$router->addActions($this->module_manager->getActions());
$validator = new CNewValidator(['action' => $action_name], ['action' => 'fatal|required|string']);
$errors = $validator->getAllErrors();
if ($errors) {
CCookieHelper::set('system-message-details', base64_encode(json_encode(
['type' => 'error', 'messages' => $errors]
)));
redirect('zabbix.php?action=system.warning');
}
$router->setAction($action_name);
$this->component_registry->get('menu.main')
->setSelectedByAction($action_name, $_REQUEST,
CViewHelper::loadSidebarMode() != ZBX_SIDEBAR_VIEW_MODE_COMPACT
);
$this->component_registry->get('menu.user')
->setSelectedByAction($action_name, $_REQUEST,
CViewHelper::loadSidebarMode() != ZBX_SIDEBAR_VIEW_MODE_COMPACT
);
CProfiler::getInstance()->start();
$this->processRequest($router);
break;
case self::EXEC_MODE_API:
$this->loadConfigFile();
$this->initDB();
$this->setServerAddress();
$this->initLocales('en_us');
break;
case self::EXEC_MODE_SETUP:
try {
// try to load config file, if it exists we need to init db and authenticate user to check permissions
$this->loadConfigFile();
$this->initDB();
$this->authenticateUser();
$this->initComponents();
}
catch (ConfigFileException $e) {
if ($e->getCode() == CConfigFile::CONFIG_VAULT_ERROR) {
echo (new CView('general.warning', [
'header' => _('Vault connection failed.'),
'messages' => [$e->getMessage()],
'theme' => ZBX_DEFAULT_THEME
]))->getOutput();
session_write_close();
exit;
}
else {
$session = new CCookieSession();
$sessionid = $session->extractSessionId() ?: CEncryptHelper::generateKey();
if (!$session->session_start($sessionid)) {
throw new Exception(_('Session initialization error.'));
}
CSessionHelper::set('sessionid', $sessionid);
}
}
break;
}
}
/**
* Returns the application mode.
*
* @return string
*/
public static function getMode(): string {
return self::getInstance()->mode;
}
/**
* Returns the absolute path to the root dir.
*
* @return string
*/
public static function getRootDir() {
return self::getInstance()->rootDir;
}
/**
* Returns the path to the frontend's root dir.
*
* @return string
*/
private function findRootDir() {
return realpath(dirname(__FILE__).'/../../..');
}
/**
* An array of directories to add to the autoloader include paths.
*
* @return array
*/
private function getIncludePaths() {
return [
$this->rootDir.'/include/classes/api',
$this->rootDir.'/include/classes/api/services',
$this->rootDir.'/include/classes/api/helpers',
$this->rootDir.'/include/classes/api/managers',
$this->rootDir.'/include/classes/api/clients',
$this->rootDir.'/include/classes/api/wrappers',
$this->rootDir.'/include/classes/core',
$this->rootDir.'/include/classes/data',
$this->rootDir.'/include/classes/mvc',
$this->rootDir.'/include/classes/db',
$this->rootDir.'/include/classes/debug',
$this->rootDir.'/include/classes/validators',
$this->rootDir.'/include/classes/validators/schema',
$this->rootDir.'/include/classes/validators/string',
$this->rootDir.'/include/classes/validators/object',
$this->rootDir.'/include/classes/validators/hostgroup',
$this->rootDir.'/include/classes/validators/host',
$this->rootDir.'/include/classes/validators/hostprototype',
$this->rootDir.'/include/classes/validators/event',
$this->rootDir.'/include/classes/export',
$this->rootDir.'/include/classes/export/writers',
$this->rootDir.'/include/classes/export/elements',
$this->rootDir.'/include/classes/graph',
$this->rootDir.'/include/classes/graphdraw',
$this->rootDir.'/include/classes/import',
$this->rootDir.'/include/classes/import/converters',
$this->rootDir.'/include/classes/import/importers',
$this->rootDir.'/include/classes/import/preprocessors',
$this->rootDir.'/include/classes/import/readers',
$this->rootDir.'/include/classes/import/validators',
$this->rootDir.'/include/classes/items',
$this->rootDir.'/include/classes/triggers',
$this->rootDir.'/include/classes/server',
$this->rootDir.'/include/classes/screens',
$this->rootDir.'/include/classes/services',
$this->rootDir.'/include/classes/sysmaps',
$this->rootDir.'/include/classes/helpers',
$this->rootDir.'/include/classes/helpers/trigger',
$this->rootDir.'/include/classes/macros',
$this->rootDir.'/include/classes/html',
$this->rootDir.'/include/classes/html/pageheader',
$this->rootDir.'/include/classes/html/svg',
$this->rootDir.'/include/classes/html/widget',
$this->rootDir.'/include/classes/html/interfaces',
$this->rootDir.'/include/classes/parsers',
$this->rootDir.'/include/classes/parsers/results',
$this->rootDir.'/include/classes/controllers',
$this->rootDir.'/include/classes/routing',
$this->rootDir.'/include/classes/json',
$this->rootDir.'/include/classes/user',
$this->rootDir.'/include/classes/setup',
$this->rootDir.'/include/classes/regexp',
$this->rootDir.'/include/classes/ldap',
$this->rootDir.'/include/classes/pagefilter',
$this->rootDir.'/include/classes/widgets/fields',
$this->rootDir.'/include/classes/widgets/forms',
$this->rootDir.'/include/classes/widgets',
$this->rootDir.'/include/classes/xml',
$this->rootDir.'/local/app/controllers',
$this->rootDir.'/app/controllers'
];
}
/**
* An array of available themes.
*
* @return array
*/
public static function getThemes() {
return [
'blue-theme' => _('Blue'),
'dark-theme' => _('Dark'),
'hc-light' => _('High-contrast light'),
'hc-dark' => _('High-contrast dark')
];
}
/**
* Check if maintenance mode is enabled.
*
* @throws Exception
*/
protected function setMaintenanceMode() {
require_once 'conf/maintenance.inc.php';
if (defined('ZBX_DENY_GUI_ACCESS')) {
if (!isset($ZBX_GUI_ACCESS_IP_RANGE) || !in_array(CWebUser::getIp(), $ZBX_GUI_ACCESS_IP_RANGE)) {
throw new Exception($_REQUEST['warning_msg']);
}
}
}
/**
* Load zabbix config file.
*/
protected function loadConfigFile() {
$configFile = CConfigFile::CONFIG_FILE_PATH;
$config = new CConfigFile($configFile);
$this->config = $config->load();
}
/**
* Initialize classes autoloader.
*/
protected function initAutoloader() {
// Register base directory path for 'include' and 'require' functions.
set_include_path(get_include_path().PATH_SEPARATOR.$this->rootDir);
$autoloader = new CAutoloader;
$autoloader->addNamespace('', $this->getIncludePaths());
$autoloader->addNamespace('Core', [$this->rootDir.'/include/classes/core']);
$autoloader->register();
$this->autoloader = $autoloader;
}
/**
* Check if frontend can connect to DB.
* @throws DBException
*/
protected function initDB() {
$error = null;
if (!DBconnect($error)) {
CDataCacheHelper::clearValues(['db_username', 'db_password']);
throw new DBException($error);
}
}
/**
* Initialize translations, set up translated date and time constants.
*
* @param string $lang Locale variant prefix like en_US, ru_RU etc.
*/
public function initLocales(string $language): void {
if (!setupLocale($language, $error) && $error !== '') {
error($error);
}
require_once $this->getRootDir().'/include/translateDefines.inc.php';
}
/**
* Set messages received in cookies.
*/
private function initMessages(): void {
if (CCookieHelper::has('system-message-ok')) {
CMessageHelper::setSuccessTitle(CCookieHelper::get('system-message-ok'));
CCookieHelper::unset('system-message-ok');
}
if (CCookieHelper::has('system-message-error')) {
CMessageHelper::setErrorTitle(CCookieHelper::get('system-message-error'));
CCookieHelper::unset('system-message-error');
}
if (CCookieHelper::has('system-message-details')) {
$details = json_decode(base64_decode(CCookieHelper::get('system-message-details')), true);
if ($details['type'] === 'success') {
foreach ($details['messages'] as $message) {
CMessageHelper::addSuccess($message);
}
}
else {
foreach ($details['messages'] as $message) {
CMessageHelper::addError($message);
}
}
CCookieHelper::unset('system-message-details');
}
}
/**
* Authenticate user, apply some user-specific settings.
*
* @throws Exception
*/
protected function authenticateUser(): void {
$session = new CEncryptedCookieSession();
if (!CWebUser::checkAuthentication($session->extractSessionId() ?: '')) {
CWebUser::setDefault();
}
$this->initLocales(CWebUser::$data['lang']);
if (!$session->session_start(CWebUser::$data['sessionid'])) {
throw new Exception(_('Session initialization error.'));
}
CSessionHelper::set('sessionid', CWebUser::$data['sessionid']);
// Set the authentication token for the API.
API::getWrapper()->auth = CWebUser::$data['sessionid'];
// Enable debug mode in the API.
API::getWrapper()->debug = CWebUser::getDebugMode();
}
/**
* Process request and generate response.
*
* @param CRouter $router CRouter class instance.
*/
private function processRequest(CRouter $router): void {
$action_name = $router->getAction();
$action_class = $router->getController();
try {
if ($action_class === null) {
throw new Exception(_('Class not found.'));
}
if (!class_exists($action_class)) {
throw new Exception(_s('Class %1$s not found for action %2$s.', $action_class, $action_name));
}
$action = new $action_class();
if (!is_subclass_of($action, CAction::class)) {
throw new Exception(_s('Action class %1$s must extend %2$s class.', $action_class, CAction::class));
}
$action->setAction($action_name);
$modules = $this->module_manager->getModules();
$action_module = $this->module_manager->getModuleByActionName($action_name);
if ($action_module) {
$modules = array_replace([$action_module->getId() => $action_module], $modules);
}
foreach (array_reverse($modules) as $module) {
if (is_subclass_of($module, CModule::class)) {
CView::registerDirectory($module->getDir().'/views');
CPartial::registerDirectory($module->getDir().'/partials');
}
}
register_shutdown_function(function() use ($action) {
$this->module_manager->publishEvent($action, 'onTerminate');
});
$this->module_manager->publishEvent($action, 'onBeforeAction');
$action->run();
if (!($action instanceof CLegacyAction)) {
$this->processResponseFinal($router, $action);
}
}
catch (Exception $e) {
echo (new CView('general.warning', [
'header' => $e->getMessage(),
'messages' => [],
'theme' => ZBX_DEFAULT_THEME
]))->getOutput();
session_write_close();
exit();
}
}
private function processResponseFinal(CRouter $router, CAction $action): void {
$response = $action->getResponse();
// Controller returned redirect to another page?
if ($response instanceof CControllerResponseRedirect) {
header('Content-Type: text/html; charset=UTF-8');
filter_messages();
$response->redirect();
}
// Controller returned fatal error?
elseif ($response instanceof CControllerResponseFatal) {
header('Content-Type: text/html; charset=UTF-8');
filter_messages();
CMessageHelper::addError('Controller: '.$router->getAction());
ksort($_REQUEST);
foreach ($_REQUEST as $key => $value) {
if ($key !== 'sid') {
CMessageHelper::addError(is_scalar($value) ? $key.': '.$value : $key.': '.gettype($value));
}
}
$response->redirect();
}
// Action has layout?
if ($router->getLayout() !== null) {
if (!($response instanceof CControllerResponseData)) {
throw new Exception(_s('Unexpected response for action %1$s.', $router->getAction()));
}
$layout_data_defaults = [
'page' => [
'title' => $response->getTitle(),
'file' => $response->getFileName()
],
'controller' => [
'action' => $router->getAction()
],
'main_block' => '',
'javascript' => [
'files' => []
],
'stylesheet' => [
'files' => []
],
'web_layout_mode' => ZBX_LAYOUT_NORMAL,
'config' => [
'server_check_interval' => CSettingsHelper::get(CSettingsHelper::SERVER_CHECK_INTERVAL),
'x_frame_options' => CSettingsHelper::get(CSettingsHelper::X_FRAME_OPTIONS)
]
];
if ($router->getView() !== null && $response->isViewEnabled()) {
$view = new CView($router->getView(), $response->getData());
$layout_data = array_replace($layout_data_defaults, [
'main_block' => $view->getOutput(),
'javascript' => [
'files' => $view->getJsFiles()
],
'stylesheet' => [
'files' => $view->getCssFiles()
],
'web_layout_mode' => $view->getLayoutMode()
]);
}
else {
$layout_data = array_replace_recursive($layout_data_defaults, $response->getData());
}
echo (new CView($router->getLayout(), $layout_data))->getOutput();
}
session_write_close();
exit();
}
/**
* Set layout mode using URL parameters.
*/
private function setLayoutModeByUrl() {
if (hasRequest('kiosk')) {
CViewHelper::saveLayoutMode(getRequest('kiosk') === '1' ? ZBX_LAYOUT_KIOSKMODE : ZBX_LAYOUT_NORMAL);
// Remove $_GET arguments to prevent CUrl from generating URL with 'kiosk' arguments.
unset($_GET['kiosk']);
}
}
/**
* Initialize menu for main navigation. Register instance as component with 'menu.main' key.
*/
private function initComponents() {
$this->component_registry->register('router', new CRouter());
$this->component_registry->register('menu.main', CMenuHelper::getMainMenu());
$this->component_registry->register('menu.user', CMenuHelper::getUserMenu());
}
/**
* Initialize module manager and load all enabled and allowed modules according to user role settings.
*/
private function initModuleManager() {
$this->module_manager = new CModuleManager($this->rootDir.'/modules');
$db_modules = API::getApiService('module')->get([
'output' => ['moduleid', 'id', 'relative_path', 'config'],
'filter' => ['status' => MODULE_STATUS_ENABLED],
'sortfield' => 'relative_path'
], false);
$modules_missing = [];
foreach ($db_modules as $db_module) {
if (!CWebUser::checkAccess('modules.module.'.$db_module['moduleid'])) {
continue;
}
$manifest = $this->module_manager->addModule($db_module['relative_path'], $db_module['id'],
$db_module['config']
);
if (!$manifest) {
$modules_missing[] = $db_module['relative_path'];
}
}
if ($modules_missing) {
error(_n('Cannot load module at: %1$s.', 'Cannot load modules at: %1$s.', implode(', ', $modules_missing),
count($modules_missing)
));
}
foreach ($this->module_manager->getNamespaces() as $namespace => $paths) {
$this->autoloader->addNamespace($namespace, $paths);
}
$this->module_manager->initModules();
array_map('error', $this->module_manager->getErrors());
}
/**
* Check for High availability override to standalone mode, set server to use for system information checks.
*
* @return void
*/
private function setServerAddress(): void {
global $ZBX_SERVER, $ZBX_SERVER_PORT;
if ($ZBX_SERVER !== null && $ZBX_SERVER_PORT !== null) {
return;
}
$ha_nodes = API::getApiService('hanode')->get([
'output' => ['address', 'port', 'status'],
'sortfield' => 'lastaccess',
'sortorder' => 'DESC'
], false);
$active_node = null;
if (count($ha_nodes) == 1) {
$active_node = $ha_nodes[0];
}
else {
foreach ($ha_nodes as $node) {
if ($node['status'] == ZBX_NODE_STATUS_ACTIVE) {
$active_node = $node;
break;
}
}
}
if ($active_node !== null) {
$ZBX_SERVER = $active_node['address'];
$ZBX_SERVER_PORT = $active_node['port'];
}
}
}
|