コード例 #1
0
 protected function addMessage($message, $type = 'info')
 {
     if (!in_array($type, array('info', 'error', 'warning', 'success'))) {
         throw new ProgrammingError(sprintf('"%s" is not a valid notification type', $type));
     }
     if ($this->isCli) {
         $msg = sprintf('[%s] %s', $type, $message);
         switch ($type) {
             case 'info':
             case 'success':
                 Logger::info($msg);
                 break;
             case 'warning':
                 Logger::warn($msg);
                 break;
             case 'error':
                 Logger::error($msg);
                 break;
         }
         return;
     }
     $mo = (object) array('type' => $type, 'message' => $message);
     // Get, change, set - just to be on the safe side:
     $session = Session::getSession();
     $msgs = $session->messages;
     $msgs[] = $mo;
     $session->messages = $msgs;
     $session->write();
 }
コード例 #2
0
ファイル: Menu.php プロジェクト: vbereza/icinga2-migration
 /**
  * Create menu from the application's menu config file plus the config files from all enabled modules
  *
  * @return  self
  */
 public static function fromConfig()
 {
     $menu = new static('menu');
     $manager = Icinga::app()->getModuleManager();
     try {
         $menuConfigs = array(Config::app('menu'));
     } catch (NotReadableError $e) {
         Logger::error($e);
         $menuConfigs = array();
     }
     try {
         $modules = $manager->listEnabledModules();
     } catch (NotReadableError $e) {
         Logger::error($e);
         $modules = array();
     }
     foreach ($modules as $moduleName) {
         try {
             $moduleMenuConfig = Config::module($moduleName, 'menu');
         } catch (NotReadableError $e) {
             Logger::error($e);
             $moduleMenuConfig = array();
         }
         if (!empty($moduleMenuConfig)) {
             $menuConfigs[] = $moduleMenuConfig;
         }
     }
     return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs));
 }
コード例 #3
0
ファイル: Manager.php プロジェクト: vbereza/icinga2-migration
 /**
  * Detect installed modules from every path provided in modulePaths
  *
  * @return self
  */
 public function detectInstalledModules()
 {
     foreach ($this->modulePaths as $basedir) {
         $canonical = realpath($basedir);
         if ($canonical === false) {
             Logger::warning('Module path "%s" does not exist', $basedir);
             continue;
         }
         if (!is_dir($canonical)) {
             Logger::error('Module path "%s" is not a directory', $canonical);
             continue;
         }
         if (!is_readable($canonical)) {
             Logger::error('Module path "%s" is not readable', $canonical);
             continue;
         }
         if (($dh = opendir($canonical)) !== false) {
             while (($file = readdir($dh)) !== false) {
                 if ($file[0] === '.') {
                     continue;
                 }
                 if (is_dir($canonical . '/' . $file)) {
                     if (!array_key_exists($file, $this->installedBaseDirs)) {
                         $this->installedBaseDirs[$file] = $canonical . '/' . $file;
                     } else {
                         Logger::debug('Module "%s" already exists in installation path "%s" and is ignored.', $canonical . '/' . $file, $this->installedBaseDirs[$file]);
                     }
                 }
             }
             closedir($dh);
         }
     }
     ksort($this->installedBaseDirs);
     return $this;
 }
コード例 #4
0
ファイル: Manager.php プロジェクト: vbereza/icinga2-migration
 public function setAuthenticated(User $user, $persist = true)
 {
     $username = $user->getUsername();
     try {
         $config = IcingaConfig::app();
     } catch (NotReadableError $e) {
         Logger::error(new Exception('Cannot load preferences for user "' . $username . '". An exception was thrown', 0, $e));
         $config = new Zend_Config(array());
     }
     if (($preferencesConfig = $config->preferences) !== null) {
         try {
             $preferencesStore = PreferencesStore::create($preferencesConfig, $user);
             $preferences = new Preferences($preferencesStore->load());
         } catch (NotReadableError $e) {
             Logger::error(new Exception('Cannot load preferences for user "' . $username . '". An exception was thrown', 0, $e));
             $preferences = new Preferences();
         }
     } else {
         $preferences = new Preferences();
     }
     $user->setPreferences($preferences);
     $membership = new Membership();
     $groups = $membership->getGroupsByUsername($username);
     $user->setGroups($groups);
     $admissionLoader = new AdmissionLoader();
     $user->setPermissions($admissionLoader->getPermissions($username, $groups));
     $user->setRestrictions($admissionLoader->getRestrictions($username, $groups));
     $this->user = $user;
     if ($persist == true) {
         $session = Session::getSession();
         $session->refreshId();
         $this->persistCurrentUser();
     }
 }
コード例 #5
0
 /**
  * Write the given external command to the command pipe
  *
  * @param   string $command
  *
  * @throws  RuntimeException When the command could not be sent to the remote Icinga host
  * @see     Transport::send()
  */
 public function send($command)
 {
     $retCode = 0;
     $output = array();
     Logger::debug('Icinga instance is on different host, attempting to send command %s via ssh to %s:%s/%s', $command, $this->host, $this->port, $this->path);
     $hostConnector = $this->user ? $this->user . "@" . $this->host : $this->host;
     $command = escapeshellarg('[' . time() . '] ' . $command);
     $sshCommand = sprintf('ssh -o BatchMode=yes -o KbdInteractiveAuthentication=no %s -p %d' . ' "echo %s > %s" 2>&1', $hostConnector, $this->port, $command, $this->path);
     exec($sshCommand, $output, $retCode);
     Logger::debug("Command '%s' exited with %d: %s", $sshCommand, $retCode, $output);
     if ($retCode != 0) {
         $msg = 'Could not send command to remote Icinga host: ' . implode(PHP_EOL, $output) . " (returncode {$retCode})";
         Logger::error($msg);
         throw new RuntimeException($msg);
     }
 }
コード例 #6
0
 /**
  * Check if the current user backend is valid, i.e. it's enabled and the config's valid
  *
  * @return bool
  */
 public function valid()
 {
     if (!$this->config->valid()) {
         // Stop when there are no more backends to check
         return false;
     }
     $backendConfig = $this->config->current();
     if ((bool) $backendConfig->get('disabled', false) === true) {
         $this->next();
         return $this->valid();
     }
     try {
         $name = $this->key();
         $backend = UserBackend::create($name, $backendConfig);
     } catch (ConfigurationError $e) {
         Logger::error(new ConfigurationError('Cannot create authentication backend "' . $name . '". An exception was thrown:', 0, $e));
         $this->next();
         return $this->valid();
     }
     $this->currentBackend = $backend;
     return true;
 }
コード例 #7
0
 /**
  * Setup internationalization using gettext
  *
  * Uses the language defined in the global config or the default one
  *
  * @return  self
  */
 protected function setupInternationalization()
 {
     try {
         Translator::setupLocale($this->config->global !== null ? $this->config->global->get('language', Translator::DEFAULT_LOCALE) : Translator::DEFAULT_LOCALE);
     } catch (Exception $error) {
         Logger::error($error);
     }
     $localeDir = $this->getApplicationDir('locale');
     if (file_exists($localeDir) && is_dir($localeDir)) {
         Translator::registerDomain('icinga', $localeDir);
     }
     return $this;
 }