2013-06-07 05:44:37 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-07-12 09:00:59 -04:00
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
namespace Icinga\Application;
|
|
|
|
|
|
2014-10-16 09:51:18 -04:00
|
|
|
use ErrorException;
|
2014-02-18 08:46:15 -05:00
|
|
|
use Exception;
|
2014-11-14 19:04:14 -05:00
|
|
|
use LogicException;
|
2014-02-26 05:19:52 -05:00
|
|
|
use Icinga\Application\Modules\Manager as ModuleManager;
|
2015-07-29 07:44:26 -04:00
|
|
|
use Icinga\Authentication\User\UserBackend;
|
2014-11-18 07:11:52 -05:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-02-26 05:19:52 -05:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-02-20 07:53:28 -05:00
|
|
|
use Icinga\Exception\NotReadableError;
|
2014-02-26 05:19:52 -05:00
|
|
|
use Icinga\Util\Translator;
|
2014-08-27 10:03:15 -04:00
|
|
|
use Icinga\Exception\IcingaException;
|
2013-06-07 05:44:37 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class bootstraps a thin Icinga application layer
|
|
|
|
|
*
|
|
|
|
|
* Usage example for CLI:
|
|
|
|
|
* <code>
|
|
|
|
|
* use Icinga\Application\Cli;
|
2013-06-20 08:06:02 -04:00
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
* Cli::start();
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
|
|
|
|
* Usage example for Icinga Web application:
|
|
|
|
|
* <code>
|
|
|
|
|
* use Icinga\Application\Web;
|
|
|
|
|
* Web::start()->dispatch();
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
|
|
|
|
* Usage example for Icinga-Web 1.x compatibility mode:
|
|
|
|
|
* <code>
|
|
|
|
|
* use Icinga\Application\LegacyWeb;
|
|
|
|
|
* LegacyWeb::start()->setIcingaWebBasedir(ICINGAWEB_BASEDIR)->dispatch();
|
|
|
|
|
* </code>
|
|
|
|
|
*/
|
|
|
|
|
abstract class ApplicationBootstrap
|
|
|
|
|
{
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
2014-11-12 09:52:43 -05:00
|
|
|
* Base directory
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2014-11-12 09:52:43 -05:00
|
|
|
* Parent folder for at least application, bin, modules, library/vendor and public
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
2013-07-26 09:58:16 -04:00
|
|
|
*/
|
2014-11-12 09:52:43 -05:00
|
|
|
protected $baseDir;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-11-13 03:33:31 -05:00
|
|
|
* Application directory
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-11-13 03:33:31 -05:00
|
|
|
protected $appDir;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-11-13 03:38:04 -05:00
|
|
|
* Vendor library directory
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2014-11-13 03:38:04 -05:00
|
|
|
* @var string
|
2013-07-26 09:58:16 -04:00
|
|
|
*/
|
2014-11-13 03:38:04 -05:00
|
|
|
protected $vendorDir;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Library directory
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-11-13 03:38:04 -05:00
|
|
|
protected $libDir;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-11-13 03:38:04 -05:00
|
|
|
* Configuration directory
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-11-13 03:38:04 -05:00
|
|
|
protected $configDir;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
2017-11-08 10:07:12 -05:00
|
|
|
/**
|
|
|
|
|
* Common storage directory
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $storageDir;
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
2015-08-18 06:50:46 -04:00
|
|
|
* Icinga class loader
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2015-08-18 03:06:58 -04:00
|
|
|
* @var ClassLoader
|
2014-11-13 03:38:04 -05:00
|
|
|
*/
|
|
|
|
|
private $loader;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Config object
|
|
|
|
|
*
|
|
|
|
|
* @var Config
|
2013-07-26 09:58:16 -04:00
|
|
|
*/
|
2014-11-13 03:38:04 -05:00
|
|
|
protected $config;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module manager
|
|
|
|
|
*
|
|
|
|
|
* @var ModuleManager
|
|
|
|
|
*/
|
|
|
|
|
private $moduleManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flag indicates we're on cli environment
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
protected $isCli = false;
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flag indicates we're on web environment
|
2013-08-06 11:35:33 -04:00
|
|
|
*
|
2013-07-26 09:58:16 -04:00
|
|
|
* @var bool
|
|
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
protected $isWeb = false;
|
|
|
|
|
|
2014-12-29 08:29:31 -05:00
|
|
|
/**
|
|
|
|
|
* Whether Icinga Web 2 requires setup
|
|
|
|
|
*
|
2015-03-12 08:39:17 -04:00
|
|
|
* @var bool
|
2014-12-29 08:29:31 -05:00
|
|
|
*/
|
|
|
|
|
protected $requiresSetup = false;
|
|
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
/**
|
|
|
|
|
* Constructor
|
2014-11-12 09:52:43 -05:00
|
|
|
*
|
2017-11-08 10:07:12 -05:00
|
|
|
* @param string $baseDir Icinga Web 2 base directory
|
|
|
|
|
* @param string $configDir Path to Icinga Web 2's configuration files
|
|
|
|
|
* @param string $storageDir Path to Icinga Web 2's stored files
|
2013-06-07 05:44:37 -04:00
|
|
|
*/
|
2017-11-08 10:07:12 -05:00
|
|
|
protected function __construct($baseDir = null, $configDir = null, $storageDir = null)
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2014-11-12 09:52:43 -05:00
|
|
|
if ($baseDir === null) {
|
2014-11-12 11:14:09 -05:00
|
|
|
$baseDir = dirname($this->getBootstrapDirectory());
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
2014-11-12 09:52:43 -05:00
|
|
|
$this->baseDir = $baseDir;
|
2014-11-13 03:33:31 -05:00
|
|
|
$this->appDir = $baseDir . '/application';
|
2014-11-13 03:38:04 -05:00
|
|
|
$this->vendorDir = $baseDir . '/library/vendor';
|
2017-11-21 06:12:07 -05:00
|
|
|
if (substr(__DIR__, 0, 8) === 'phar:///') {
|
|
|
|
|
$this->libDir = dirname(dirname(__DIR__));
|
|
|
|
|
} else {
|
|
|
|
|
$this->libDir = realpath(__DIR__ . '/../..');
|
|
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
|
2015-03-30 18:09:24 -04:00
|
|
|
$this->setupAutoloader();
|
|
|
|
|
|
2014-04-17 13:01:23 -04:00
|
|
|
if ($configDir === null) {
|
2015-12-18 07:26:38 -05:00
|
|
|
$configDir = getenv('ICINGAWEB_CONFIGDIR');
|
|
|
|
|
if ($configDir === false) {
|
2015-03-30 18:09:24 -04:00
|
|
|
$configDir = Platform::isWindows()
|
|
|
|
|
? $baseDir . '/config'
|
|
|
|
|
: '/etc/icingaweb2';
|
2014-04-17 13:01:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-06-25 05:53:55 -04:00
|
|
|
$canonical = realpath($configDir);
|
|
|
|
|
$this->configDir = $canonical ? $canonical : $configDir;
|
2014-04-17 13:01:23 -04:00
|
|
|
|
2017-11-08 10:07:12 -05:00
|
|
|
if ($storageDir === null) {
|
|
|
|
|
$storageDir = getenv('ICINGAWEB_STORAGEDIR');
|
|
|
|
|
if ($storageDir === false) {
|
|
|
|
|
$storageDir = Platform::isWindows()
|
|
|
|
|
? $baseDir . '/storage'
|
|
|
|
|
: '/var/lib/icingaweb2';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$canonical = realpath($storageDir);
|
|
|
|
|
$this->storageDir = $canonical ? $canonical : $storageDir;
|
|
|
|
|
|
2014-11-13 13:52:28 -05:00
|
|
|
set_include_path(
|
|
|
|
|
implode(
|
|
|
|
|
PATH_SEPARATOR,
|
|
|
|
|
array($this->vendorDir, get_include_path())
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
Benchmark::measure('Bootstrap, autoloader registered');
|
|
|
|
|
|
|
|
|
|
Icinga::setApp($this);
|
2013-07-26 09:58:16 -04:00
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
require_once dirname(__FILE__) . '/functions.php';
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
|
|
|
|
* Bootstrap interface method for concrete bootstrap objects
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
abstract protected function bootstrap();
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
|
|
|
|
* Getter for module manager
|
|
|
|
|
*
|
|
|
|
|
* @return ModuleManager
|
|
|
|
|
*/
|
|
|
|
|
public function getModuleManager()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
|
|
|
|
return $this->moduleManager;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-12 09:00:59 -04:00
|
|
|
/**
|
|
|
|
|
* Getter for class loader
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2015-08-18 03:06:58 -04:00
|
|
|
* @return ClassLoader
|
2013-07-12 09:00:59 -04:00
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
public function getLoader()
|
|
|
|
|
{
|
|
|
|
|
return $this->loader;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
|
|
|
|
* Getter for configuration object
|
|
|
|
|
*
|
|
|
|
|
* @return Config
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2013-07-26 09:58:16 -04:00
|
|
|
return $this->config;
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
|
|
|
|
* Flag indicates we're on cli environment
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
public function isCli()
|
|
|
|
|
{
|
|
|
|
|
return $this->isCli;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
|
|
|
|
* Flag indicates we're on web environment
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
public function isWeb()
|
|
|
|
|
{
|
|
|
|
|
return $this->isWeb;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
2014-11-12 09:52:43 -05:00
|
|
|
* Helper to glue directories together
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2014-11-12 09:52:43 -05:00
|
|
|
* @param string $dir
|
|
|
|
|
* @param string $subdir
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getDirWithSubDir($dir, $subdir = null)
|
|
|
|
|
{
|
|
|
|
|
if ($subdir !== null) {
|
|
|
|
|
$dir .= '/' . ltrim($subdir, '/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $dir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the base directory
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2014-11-12 09:52:43 -05:00
|
|
|
* @param string $subDir Optional sub directory to get
|
2013-08-23 03:57:32 -04:00
|
|
|
*
|
|
|
|
|
* @return string
|
2013-07-26 09:58:16 -04:00
|
|
|
*/
|
2014-11-12 09:52:43 -05:00
|
|
|
public function getBaseDir($subDir = null)
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2014-11-13 03:33:31 -05:00
|
|
|
return $this->getDirWithSubDir($this->baseDir, $subDir);
|
2013-07-30 10:02:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-11-13 03:33:31 -05:00
|
|
|
* Get the application directory
|
2013-08-23 03:57:32 -04:00
|
|
|
*
|
2014-11-13 03:33:31 -05:00
|
|
|
* @param string $subDir Optional sub directory to get
|
2013-08-23 03:57:32 -04:00
|
|
|
*
|
|
|
|
|
* @return string
|
2013-07-30 10:02:05 -04:00
|
|
|
*/
|
2014-11-13 03:33:31 -05:00
|
|
|
public function getApplicationDir($subDir = null)
|
2013-07-30 10:02:05 -04:00
|
|
|
{
|
2014-11-13 03:33:31 -05:00
|
|
|
return $this->getDirWithSubDir($this->appDir, $subDir);
|
2013-07-30 10:02:05 -04:00
|
|
|
}
|
|
|
|
|
|
2014-03-04 10:13:13 -05:00
|
|
|
/**
|
2014-11-13 03:38:04 -05:00
|
|
|
* Get the vendor library directory
|
2014-03-04 10:13:13 -05:00
|
|
|
*
|
2014-11-13 03:38:04 -05:00
|
|
|
* @param string $subDir Optional sub directory to get
|
2014-03-04 10:13:13 -05:00
|
|
|
*
|
2014-11-13 03:38:04 -05:00
|
|
|
* @return string
|
2014-03-04 10:13:13 -05:00
|
|
|
*/
|
2014-11-13 03:38:04 -05:00
|
|
|
public function getVendorDir($subDir = null)
|
2014-03-04 10:13:13 -05:00
|
|
|
{
|
2014-11-13 03:38:04 -05:00
|
|
|
return $this->getDirWithSubDir($this->vendorDir, $subDir);
|
2014-03-04 10:13:13 -05:00
|
|
|
}
|
|
|
|
|
|
2013-07-30 10:02:05 -04:00
|
|
|
/**
|
2014-11-13 03:38:04 -05:00
|
|
|
* Get the configuration directory
|
2013-07-30 10:02:05 -04:00
|
|
|
*
|
2014-11-13 03:38:04 -05:00
|
|
|
* @param string $subDir Optional sub directory to get
|
2013-08-23 03:57:32 -04:00
|
|
|
*
|
|
|
|
|
* @return string
|
2013-07-30 10:02:05 -04:00
|
|
|
*/
|
2014-11-13 03:38:04 -05:00
|
|
|
public function getConfigDir($subDir = null)
|
2013-07-30 10:02:05 -04:00
|
|
|
{
|
2014-11-13 03:38:04 -05:00
|
|
|
return $this->getDirWithSubDir($this->configDir, $subDir);
|
2013-07-30 10:02:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-08 10:07:12 -05:00
|
|
|
/**
|
|
|
|
|
* Get the common storage directory
|
|
|
|
|
*
|
|
|
|
|
* @param string $subDir Optional sub directory to get
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getStorageDir($subDir = null)
|
|
|
|
|
{
|
|
|
|
|
return $this->getDirWithSubDir($this->storageDir, $subDir);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 10:02:17 -05:00
|
|
|
/**
|
|
|
|
|
* Get the Icinga library directory
|
|
|
|
|
*
|
|
|
|
|
* @param string $subDir Optional sub directory to get
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getLibraryDir($subDir = null)
|
|
|
|
|
{
|
|
|
|
|
return $this->getDirWithSubDir($this->libDir, $subDir);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-04 10:13:13 -05:00
|
|
|
/**
|
2014-11-12 11:10:05 -05:00
|
|
|
* Get the path to the bootstrapping directory
|
2014-03-04 10:13:13 -05:00
|
|
|
*
|
2014-11-12 11:10:05 -05:00
|
|
|
* This is usually /public for Web and EmbeddedWeb and /bin for the CLI
|
2014-03-04 10:13:13 -05:00
|
|
|
*
|
2014-11-14 19:04:14 -05:00
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* @throws LogicException If the base directory can not be detected
|
2014-03-04 10:13:13 -05:00
|
|
|
*/
|
2014-11-12 11:14:09 -05:00
|
|
|
public function getBootstrapDirectory()
|
2014-03-04 10:13:13 -05:00
|
|
|
{
|
2014-11-14 19:04:14 -05:00
|
|
|
$script = $_SERVER['SCRIPT_FILENAME'];
|
|
|
|
|
$canonical = realpath($script);
|
|
|
|
|
if ($canonical !== false) {
|
|
|
|
|
$dir = dirname($canonical);
|
|
|
|
|
} elseif (substr($script, -14) === '/webrouter.php') {
|
|
|
|
|
// If Icinga Web 2 is served using PHP's built-in webserver with our webrouter.php script, the $_SERVER
|
|
|
|
|
// variable SCRIPT_FILENAME is set to DOCUMENT_ROOT/webrouter.php which is not a valid path to
|
|
|
|
|
// realpath but DOCUMENT_ROOT here still is the bootstrapping directory
|
|
|
|
|
$dir = dirname($script);
|
|
|
|
|
} else {
|
|
|
|
|
throw new LogicException('Can\'t detected base directory');
|
|
|
|
|
}
|
|
|
|
|
return $dir;
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
2014-11-12 09:52:43 -05:00
|
|
|
* Start the bootstrap
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2014-11-12 09:52:43 -05:00
|
|
|
* @param string $baseDir Icinga Web 2 base directory
|
|
|
|
|
* @param string $configDir Path to Icinga Web 2's configuration files
|
2013-08-23 03:57:32 -04:00
|
|
|
*
|
2014-11-12 09:52:43 -05:00
|
|
|
* @return static
|
2013-07-26 09:58:16 -04:00
|
|
|
*/
|
2014-11-12 09:52:43 -05:00
|
|
|
public static function start($baseDir = null, $configDir = null)
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2014-11-12 09:52:43 -05:00
|
|
|
$application = new static($baseDir, $configDir);
|
2013-08-23 03:57:32 -04:00
|
|
|
$application->bootstrap();
|
|
|
|
|
return $application;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 09:58:16 -04:00
|
|
|
/**
|
2015-08-18 06:50:46 -04:00
|
|
|
* Setup Icinga class loader
|
2013-07-26 09:58:16 -04:00
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2013-07-26 09:58:16 -04:00
|
|
|
*/
|
|
|
|
|
public function setupAutoloader()
|
2013-07-12 09:00:59 -04:00
|
|
|
{
|
2017-02-10 09:42:02 -05:00
|
|
|
require_once $this->libDir . '/Icinga/Application/ClassLoader.php';
|
2013-07-12 09:00:59 -04:00
|
|
|
|
2015-08-18 03:06:58 -04:00
|
|
|
$this->loader = new ClassLoader();
|
2015-11-13 05:51:41 -05:00
|
|
|
$this->loader->registerNamespace('Icinga', $this->libDir . '/Icinga');
|
2015-11-12 11:35:11 -05:00
|
|
|
$this->loader->registerNamespace('Icinga', $this->libDir . '/Icinga', $this->appDir);
|
2013-07-12 09:00:59 -04:00
|
|
|
$this->loader->register();
|
2013-07-26 09:58:16 -04:00
|
|
|
|
|
|
|
|
return $this;
|
2013-07-12 09:00:59 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
/**
|
2015-11-13 05:47:37 -05:00
|
|
|
* Register the Zend Autoloader - compat only - does nothing
|
2015-11-12 11:36:34 -05:00
|
|
|
*
|
2015-11-13 05:47:37 -05:00
|
|
|
* @deprecated
|
2014-11-14 08:04:24 -05:00
|
|
|
* @return $this
|
2013-06-07 05:44:37 -04:00
|
|
|
*/
|
2014-12-09 09:38:13 -05:00
|
|
|
public function setupZendAutoloader()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2015-11-12 11:36:34 -05:00
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
/**
|
2014-01-22 12:11:26 -05:00
|
|
|
* Setup module manager
|
2013-06-07 05:44:37 -04:00
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2013-06-07 05:44:37 -04:00
|
|
|
*/
|
2014-01-22 12:11:26 -05:00
|
|
|
protected function setupModuleManager()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2017-11-21 06:12:07 -05:00
|
|
|
$paths = $this->getAvailableModulePaths();
|
2014-01-24 04:48:30 -05:00
|
|
|
$this->moduleManager = new ModuleManager(
|
|
|
|
|
$this,
|
|
|
|
|
$this->configDir . '/enabledModules',
|
2017-11-21 06:12:07 -05:00
|
|
|
$paths
|
2014-01-24 04:48:30 -05:00
|
|
|
);
|
2014-01-22 12:11:26 -05:00
|
|
|
return $this;
|
|
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
|
2017-11-21 06:12:07 -05:00
|
|
|
protected function getAvailableModulePaths()
|
|
|
|
|
{
|
|
|
|
|
$paths = array();
|
|
|
|
|
$configured = $this->config->get('global', 'module_path', $this->baseDir . '/modules');
|
|
|
|
|
$nextIsPhar = false;
|
|
|
|
|
foreach (explode(':', $configured) as $path) {
|
|
|
|
|
if ($path === 'phar') {
|
|
|
|
|
$nextIsPhar = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($nextIsPhar) {
|
|
|
|
|
$nextIsPhar = false;
|
|
|
|
|
$paths[] = 'phar:' . $path;
|
|
|
|
|
} else {
|
|
|
|
|
$paths[] = $path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $paths;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-22 12:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Load all enabled modules
|
|
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2014-01-22 12:11:26 -05:00
|
|
|
*/
|
|
|
|
|
protected function loadEnabledModules()
|
|
|
|
|
{
|
2013-09-02 06:47:57 -04:00
|
|
|
try {
|
|
|
|
|
$this->moduleManager->loadEnabledModules();
|
2014-02-20 07:53:28 -05:00
|
|
|
} catch (NotReadableError $e) {
|
2014-08-27 10:03:15 -04:00
|
|
|
Logger::error(new IcingaException('Cannot load enabled modules. An exception was thrown:', $e));
|
2013-09-02 06:47:57 -04:00
|
|
|
}
|
2013-07-26 09:58:16 -04:00
|
|
|
return $this;
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 08:29:31 -05:00
|
|
|
/**
|
2015-01-23 09:27:43 -05:00
|
|
|
* Load the setup module if Icinga Web 2 requires setup or the setup token exists
|
2014-12-29 08:29:31 -05:00
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
protected function loadSetupModuleIfNecessary()
|
|
|
|
|
{
|
2015-01-27 07:34:59 -05:00
|
|
|
if (! @file_exists($this->config->resolvePath('authentication.ini'))) {
|
2014-12-29 08:29:31 -05:00
|
|
|
$this->requiresSetup = true;
|
|
|
|
|
$this->moduleManager->loadModule('setup');
|
2015-01-23 09:27:43 -05:00
|
|
|
} elseif ($this->setupTokenExists()) {
|
|
|
|
|
// Load setup module but do not require setup
|
|
|
|
|
$this->moduleManager->loadModule('setup');
|
2014-12-29 08:29:31 -05:00
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get whether Icinga Web 2 requires setup
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function requiresSetup()
|
|
|
|
|
{
|
|
|
|
|
return $this->requiresSetup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get whether the setup token exists
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function setupTokenExists()
|
|
|
|
|
{
|
|
|
|
|
return @file_exists($this->config->resolvePath('setup.token'));
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 05:19:52 -05:00
|
|
|
/**
|
|
|
|
|
* Setup default logging
|
|
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2014-02-26 05:19:52 -05:00
|
|
|
*/
|
|
|
|
|
protected function setupLogging()
|
|
|
|
|
{
|
|
|
|
|
Logger::create(
|
2014-11-18 07:11:52 -05:00
|
|
|
new ConfigObject(
|
2014-02-26 05:19:52 -05:00
|
|
|
array(
|
2014-10-16 09:59:56 -04:00
|
|
|
'log' => 'syslog'
|
2014-02-26 05:19:52 -05:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
/**
|
|
|
|
|
* Load Configuration
|
|
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2013-06-07 05:44:37 -04:00
|
|
|
*/
|
2014-02-12 08:51:04 -05:00
|
|
|
protected function loadConfig()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2013-07-12 09:37:36 -04:00
|
|
|
Config::$configDir = $this->configDir;
|
2014-10-21 11:02:21 -04:00
|
|
|
|
2014-02-20 07:53:28 -05:00
|
|
|
try {
|
|
|
|
|
$this->config = Config::app();
|
|
|
|
|
} catch (NotReadableError $e) {
|
2014-08-27 10:03:15 -04:00
|
|
|
Logger::error(new IcingaException('Cannot load application configuration. An exception was thrown:', $e));
|
2014-11-07 07:53:03 -05:00
|
|
|
$this->config = new Config();
|
2014-02-20 07:53:28 -05:00
|
|
|
}
|
2014-10-21 11:02:21 -04:00
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error handling configuration
|
|
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2013-06-07 05:44:37 -04:00
|
|
|
*/
|
2013-07-26 09:58:16 -04:00
|
|
|
protected function setupErrorHandling()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2014-09-19 07:19:24 -04:00
|
|
|
error_reporting(E_ALL | E_STRICT);
|
2014-02-14 04:48:17 -05:00
|
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
|
ini_set('display_errors', 1);
|
2014-10-16 09:51:18 -04:00
|
|
|
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
|
|
|
|
|
if (error_reporting() === 0) {
|
|
|
|
|
// Error was suppressed with the @-operator
|
|
|
|
|
return false; // Continue with the normal error handler
|
|
|
|
|
}
|
2017-01-27 08:48:59 -05:00
|
|
|
switch ($errno) {
|
2014-11-14 19:20:27 -05:00
|
|
|
case E_NOTICE:
|
2014-11-04 07:20:41 -05:00
|
|
|
case E_WARNING:
|
2014-10-16 09:51:18 -04:00
|
|
|
case E_STRICT:
|
2015-10-14 10:34:22 -04:00
|
|
|
case E_RECOVERABLE_ERROR:
|
2014-10-16 09:51:18 -04:00
|
|
|
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
|
|
|
|
|
}
|
|
|
|
|
return false; // Continue with the normal error handler
|
|
|
|
|
});
|
2014-02-14 04:48:17 -05:00
|
|
|
return $this;
|
|
|
|
|
}
|
2014-02-26 05:19:52 -05:00
|
|
|
|
2014-02-14 04:48:17 -05:00
|
|
|
/**
|
|
|
|
|
* Set up logger
|
|
|
|
|
*
|
2014-12-29 08:01:19 -05:00
|
|
|
* @return $this
|
2014-02-14 04:48:17 -05:00
|
|
|
*/
|
|
|
|
|
protected function setupLogger()
|
|
|
|
|
{
|
2014-11-18 07:11:52 -05:00
|
|
|
if ($this->config->hasSection('logging')) {
|
2015-04-13 11:09:49 -04:00
|
|
|
$loggingConfig = $this->config->getSection('logging');
|
|
|
|
|
|
2014-03-03 13:03:39 -05:00
|
|
|
try {
|
2015-04-13 11:09:49 -04:00
|
|
|
Logger::create($loggingConfig);
|
2014-03-03 13:03:39 -05:00
|
|
|
} catch (ConfigurationError $e) {
|
2015-04-13 11:11:42 -04:00
|
|
|
Logger::getInstance()->registerConfigError($e->getMessage());
|
2015-04-13 11:09:49 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Logger::getInstance()->setLevel($loggingConfig->get('level', Logger::ERROR));
|
|
|
|
|
} catch (ConfigurationError $e) {
|
2015-04-13 11:11:42 -04:00
|
|
|
Logger::getInstance()->registerConfigError($e->getMessage());
|
2015-04-13 11:09:49 -04:00
|
|
|
}
|
2014-03-03 13:03:39 -05:00
|
|
|
}
|
2014-02-26 05:19:52 -05:00
|
|
|
}
|
2015-04-13 11:09:49 -04:00
|
|
|
|
2013-06-07 05:44:37 -04:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 07:44:26 -04:00
|
|
|
/**
|
|
|
|
|
* Set up the user backend factory
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
protected function setupUserBackendFactory()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
UserBackend::setConfig(Config::app('authentication'));
|
|
|
|
|
} catch (NotReadableError $e) {
|
|
|
|
|
Logger::error(
|
|
|
|
|
new IcingaException('Cannot load user backend configuration. An exception was thrown:', $e)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 12:08:21 -04:00
|
|
|
/**
|
2014-11-13 11:07:36 -05:00
|
|
|
* Detect the timezone
|
2013-06-07 05:44:37 -04:00
|
|
|
*
|
2014-11-13 11:07:36 -05:00
|
|
|
* @return null|string
|
2013-06-07 05:44:37 -04:00
|
|
|
*/
|
2014-11-13 11:07:36 -05:00
|
|
|
protected function detectTimezone()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2014-11-13 11:07:36 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
2014-11-12 06:13:46 -05:00
|
|
|
|
2014-11-13 11:07:36 -05:00
|
|
|
/**
|
|
|
|
|
* Set up the timezone
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2017-01-27 08:48:59 -05:00
|
|
|
final protected function setupTimezone()
|
2014-11-13 11:07:36 -05:00
|
|
|
{
|
|
|
|
|
$timezone = $this->detectTimeZone();
|
|
|
|
|
if ($timezone === null || @date_default_timezone_set($timezone) === false) {
|
|
|
|
|
$timezone = @date_default_timezone_get();
|
|
|
|
|
if ($timezone === false) {
|
|
|
|
|
$timezone = 'UTC';
|
|
|
|
|
date_default_timezone_set($timezone);
|
|
|
|
|
}
|
2014-06-20 06:54:37 -04:00
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
return $this;
|
|
|
|
|
}
|
2014-01-29 10:25:08 -05:00
|
|
|
|
|
|
|
|
/**
|
2014-11-13 12:02:03 -05:00
|
|
|
* Detect the locale
|
2014-01-29 10:25:08 -05:00
|
|
|
*
|
2014-11-13 12:02:03 -05:00
|
|
|
* @return null|string
|
|
|
|
|
*/
|
|
|
|
|
protected function detectLocale()
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up internationalization using gettext
|
2014-01-29 10:25:08 -05:00
|
|
|
*
|
2014-11-13 12:02:03 -05:00
|
|
|
* @return $this
|
2014-01-29 10:25:08 -05:00
|
|
|
*/
|
2017-01-27 08:48:59 -05:00
|
|
|
final protected function setupInternationalization()
|
2014-01-29 10:25:08 -05:00
|
|
|
{
|
2014-09-02 10:51:17 -04:00
|
|
|
if ($this->hasLocales()) {
|
|
|
|
|
Translator::registerDomain(Translator::DEFAULT_DOMAIN, $this->getLocaleDir());
|
2014-06-25 05:49:47 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-13 12:02:03 -05:00
|
|
|
$locale = $this->detectLocale();
|
|
|
|
|
if ($locale === null) {
|
|
|
|
|
$locale = Translator::DEFAULT_LOCALE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 06:22:13 -05:00
|
|
|
try {
|
2014-11-13 12:02:03 -05:00
|
|
|
Translator::setupLocale($locale);
|
2014-02-03 06:22:13 -05:00
|
|
|
} catch (Exception $error) {
|
2014-02-26 05:19:52 -05:00
|
|
|
Logger::error($error);
|
2014-02-03 06:22:13 -05:00
|
|
|
}
|
2014-01-29 10:25:08 -05:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2014-09-02 10:51:17 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string Our locale directory
|
|
|
|
|
*/
|
|
|
|
|
public function getLocaleDir()
|
|
|
|
|
{
|
|
|
|
|
return $this->getApplicationDir('locale');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* return bool Whether Icinga Web has translations
|
|
|
|
|
*/
|
|
|
|
|
public function hasLocales()
|
|
|
|
|
{
|
|
|
|
|
$localedir = $this->getLocaleDir();
|
|
|
|
|
return file_exists($localedir) && is_dir($localedir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List all available locales
|
|
|
|
|
*
|
|
|
|
|
* NOTE: Might be a candidate for a static function in Translator
|
|
|
|
|
*
|
|
|
|
|
* return array Locale list
|
|
|
|
|
*/
|
|
|
|
|
public function listLocales()
|
|
|
|
|
{
|
|
|
|
|
$locales = array();
|
|
|
|
|
if (! $this->hasLocales()) {
|
|
|
|
|
return $locales;
|
|
|
|
|
}
|
|
|
|
|
$localedir = $this->getLocaleDir();
|
|
|
|
|
|
|
|
|
|
$dh = opendir($localedir);
|
|
|
|
|
while (false !== ($file = readdir($dh))) {
|
|
|
|
|
$filename = $localedir . DIRECTORY_SEPARATOR . $file;
|
|
|
|
|
if (preg_match('/^[a-z]{2}_[A-Z]{2}$/', $file) && is_dir($filename)) {
|
|
|
|
|
$locales[] = $file;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir($dh);
|
|
|
|
|
sort($locales);
|
|
|
|
|
return $locales;
|
|
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|