2015-04-24 14:26:44 +02:00
|
|
|
<?php
|
|
|
|
|
2015-06-30 11:27:32 +02:00
|
|
|
namespace Icinga\Module\Director\Web\Controller;
|
2015-04-24 14:26:44 +02:00
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
2015-07-23 16:19:22 +02:00
|
|
|
use Icinga\Data\Paginatable;
|
2015-12-10 13:06:40 +01:00
|
|
|
use Icinga\Exception\AuthenticationException;
|
2016-02-03 00:53:05 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2015-12-10 13:06:40 +01:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2015-04-24 14:26:44 +02:00
|
|
|
use Icinga\Module\Director\Db;
|
2016-03-20 11:18:06 +01:00
|
|
|
use Icinga\Module\Director\Monitoring;
|
2016-02-17 16:40:08 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
2015-04-24 14:26:44 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\FormLoader;
|
|
|
|
use Icinga\Module\Director\Web\Table\TableLoader;
|
|
|
|
use Icinga\Web\Controller;
|
|
|
|
use Icinga\Web\Widget;
|
|
|
|
|
|
|
|
abstract class ActionController extends Controller
|
|
|
|
{
|
|
|
|
protected $db;
|
|
|
|
|
2015-12-10 13:06:40 +01:00
|
|
|
protected $isApified = false;
|
|
|
|
|
2016-02-17 16:40:08 +01:00
|
|
|
private $api;
|
|
|
|
|
2016-03-20 11:18:06 +01:00
|
|
|
private $monitoring;
|
|
|
|
|
2015-12-10 13:06:40 +01:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
|
|
|
if (! $this->hasPermission('director/api')) {
|
|
|
|
throw new AuthenticationException('You are not allowed to access this API');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isApified()) {
|
|
|
|
throw new NotFoundError('No such API endpoint found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isApified()
|
|
|
|
{
|
|
|
|
return $this->isApified;
|
|
|
|
}
|
|
|
|
|
2015-07-23 16:19:22 +02:00
|
|
|
protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null)
|
|
|
|
{
|
|
|
|
$limit = $this->params->get('limit', $limit);
|
|
|
|
$page = $this->params->get('page', $offset);
|
|
|
|
|
|
|
|
$paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);
|
|
|
|
|
|
|
|
return $paginatable;
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:26:44 +02:00
|
|
|
public function loadForm($name)
|
|
|
|
{
|
2015-12-10 13:06:40 +01:00
|
|
|
$form = FormLoader::load($name, $this->Module());
|
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
|
|
|
// TODO: Ask form for API support?
|
|
|
|
$form->setApiRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
2015-04-24 14:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function loadTable($name)
|
|
|
|
{
|
|
|
|
return TableLoader::load($name, $this->Module());
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:06:40 +01:00
|
|
|
protected function sendJson($object)
|
|
|
|
{
|
|
|
|
$this->getResponse()->setHeader('Content-Type', 'application/json', true);
|
2016-02-26 11:58:37 +01:00
|
|
|
$this->_helper->layout()->disableLayout();
|
2015-12-10 13:06:40 +01:00
|
|
|
$this->_helper->viewRenderer->setNoRender(true);
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
|
|
echo json_encode($object, JSON_PRETTY_PRINT) . "\n";
|
|
|
|
} else {
|
|
|
|
echo json_encode($object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 00:53:05 +01:00
|
|
|
protected function sendJsonError($message, $code = null)
|
|
|
|
{
|
|
|
|
if ($code !== null) {
|
|
|
|
$this->setHttpResponseCode((int) $code);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sendJson((object) array('error' => $message));
|
|
|
|
}
|
|
|
|
|
2015-06-29 10:13:39 +02:00
|
|
|
protected function setConfigTabs()
|
|
|
|
{
|
2016-02-26 11:58:37 +01:00
|
|
|
$this->view->tabs = Widget::create('tabs')->add(
|
|
|
|
'deploymentlog',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Deployments'),
|
|
|
|
'url' => 'director/list/deploymentlog'
|
|
|
|
)
|
|
|
|
)->add(
|
|
|
|
'generatedconfig',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Configs'),
|
|
|
|
'url' => 'director/list/generatedconfig'
|
|
|
|
)
|
|
|
|
)->add(
|
|
|
|
'activitylog',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Activity Log'),
|
|
|
|
'url' => 'director/list/activitylog'
|
|
|
|
)
|
2016-02-28 17:03:32 +01:00
|
|
|
);
|
|
|
|
return $this->view->tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setDataTabs()
|
|
|
|
{
|
|
|
|
$this->view->tabs = Widget::create('tabs')->add(
|
2016-02-26 11:58:37 +01:00
|
|
|
'datalist',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Data lists'),
|
2016-02-28 17:03:32 +01:00
|
|
|
'url' => 'director/data/lists'
|
2016-02-26 11:58:37 +01:00
|
|
|
)
|
|
|
|
)->add(
|
|
|
|
'datafield',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Data fields'),
|
2016-02-28 17:03:32 +01:00
|
|
|
'url' => 'director/data/fields'
|
2016-02-26 11:58:37 +01:00
|
|
|
)
|
2015-07-22 10:12:50 +02:00
|
|
|
);
|
|
|
|
return $this->view->tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setImportTabs()
|
|
|
|
{
|
2016-02-26 11:58:37 +01:00
|
|
|
$this->view->tabs = Widget::create('tabs')->add(
|
|
|
|
'importsource',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Import source'),
|
|
|
|
'url' => 'director/list/importsource'
|
|
|
|
)
|
|
|
|
)->add(
|
|
|
|
'importrun',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Import history'),
|
|
|
|
'url' => 'director/list/importrun'
|
|
|
|
)
|
|
|
|
)->add(
|
|
|
|
'syncrule',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Sync rule'),
|
|
|
|
'url' => 'director/list/syncrule'
|
|
|
|
)
|
2015-07-03 09:01:52 +02:00
|
|
|
);
|
2015-06-29 10:13:39 +02:00
|
|
|
return $this->view->tabs;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:18:55 +01:00
|
|
|
protected function provideQuickSearch()
|
|
|
|
{
|
|
|
|
$htm = '<form action="%s" class="quicksearch inline" method="post">'
|
|
|
|
. '<input type="text" name="q" value="" placeholder="%s" class="search" />'
|
|
|
|
. '</form>';
|
|
|
|
|
|
|
|
$this->view->quickSearch = sprintf(
|
|
|
|
$htm,
|
2016-03-21 12:11:33 +01:00
|
|
|
$this->getRequest()->getUrl()->without(array('q', 'page', 'modifyFilter')),
|
2016-03-20 13:18:55 +01:00
|
|
|
$this->translate('Search...')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:12:33 +01:00
|
|
|
protected function shorten($string, $length)
|
|
|
|
{
|
|
|
|
if (strlen($string) > $length) {
|
|
|
|
return substr($string, 0, $length) . '...';
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2016-03-20 11:19:16 +01:00
|
|
|
protected function setViewScript($name)
|
|
|
|
{
|
|
|
|
$this->_helper->viewRenderer->setNoController(true);
|
|
|
|
$this->_helper->viewRenderer->setScriptAction($name);
|
|
|
|
}
|
|
|
|
|
2016-02-28 01:16:13 +01:00
|
|
|
protected function prepareTable($name)
|
|
|
|
{
|
|
|
|
$table = $this->loadTable($name)->setConnection($this->db());
|
|
|
|
$this->view->filterEditor = $table->getFilterEditor($this->getRequest());
|
|
|
|
$this->view->table = $this->applyPaginationLimits($table);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function prepareAndRenderTable($name)
|
|
|
|
{
|
|
|
|
$this->prepareTable($name)->render('list/table', null, true);
|
|
|
|
}
|
|
|
|
|
2016-03-01 04:25:14 +01:00
|
|
|
// TODO: just return json_last_error_msg() for PHP >= 5.5.0
|
|
|
|
protected function getLastJsonError()
|
|
|
|
{
|
|
|
|
switch (json_last_error()) {
|
|
|
|
case JSON_ERROR_DEPTH:
|
|
|
|
return 'The maximum stack depth has been exceeded';
|
|
|
|
case JSON_ERROR_CTRL_CHAR:
|
|
|
|
return 'Control character error, possibly incorrectly encoded';
|
|
|
|
case JSON_ERROR_STATE_MISMATCH:
|
|
|
|
return 'Invalid or malformed JSON';
|
|
|
|
case JSON_ERROR_SYNTAX:
|
|
|
|
return 'Syntax error';
|
|
|
|
case JSON_ERROR_UTF8:
|
|
|
|
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
|
|
|
default:
|
|
|
|
return 'An error occured when parsing a JSON string';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-17 16:40:08 +01:00
|
|
|
protected function api($endpointName = null)
|
2015-10-20 22:47:06 +02:00
|
|
|
{
|
2016-02-17 16:40:08 +01:00
|
|
|
if ($this->api === null) {
|
|
|
|
if ($endpointName === null) {
|
|
|
|
$endpoint = $this->db()->getDeploymentEndpoint();
|
|
|
|
} else {
|
|
|
|
$endpoint = IcingaEndpoint::load($endpointName, $this->db());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->api = $endpoint->api();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->api;
|
2015-10-20 22:47:06 +02:00
|
|
|
}
|
2015-07-24 09:51:31 +02:00
|
|
|
|
2015-04-24 14:26:44 +02:00
|
|
|
protected function db()
|
|
|
|
{
|
|
|
|
if ($this->db === null) {
|
2015-06-29 16:01:10 +02:00
|
|
|
$resourceName = $this->Config()->get('db', 'resource');
|
|
|
|
if ($resourceName) {
|
|
|
|
$this->db = Db::fromResourceName($resourceName);
|
|
|
|
} else {
|
2016-02-03 00:53:05 +01:00
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
|
|
|
throw new ConfigError('Icinga Director is not correctly configured');
|
|
|
|
} else {
|
|
|
|
$this->redirectNow('director');
|
|
|
|
}
|
2015-06-29 16:01:10 +02:00
|
|
|
}
|
2015-04-24 14:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db;
|
|
|
|
}
|
2016-03-20 11:18:06 +01:00
|
|
|
|
|
|
|
protected function monitoring()
|
|
|
|
{
|
|
|
|
if ($this->monitoring === null) {
|
|
|
|
$this->monitoring = new Monitoring;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->monitoring;
|
|
|
|
}
|
2015-04-24 14:26:44 +02:00
|
|
|
}
|