pandorafms/pandora_console/include/rest-api/index.php

479 lines
14 KiB
PHP
Raw Normal View History

<?php
global $config;
2019-07-31 11:03:13 +02:00
if (!is_ajax()) {
return;
}
require_once $config['homedir'].'/vendor/autoload.php';
use Models\VisualConsole\Container as VisualConsole;
2019-10-02 18:37:50 +02:00
use Models\VisualConsole\View as Viewer;
2020-01-07 15:43:44 +01:00
use Models\VisualConsole\Item as Item;
2019-10-02 18:37:50 +02:00
$method = get_parameter('method');
if ($method) {
$viewer = new Viewer();
try {
2020-01-08 17:28:03 +01:00
if (method_exists($viewer, $method) === true) {
2019-10-02 18:37:50 +02:00
echo $viewer->{$method}();
}
} catch (Exception $e) {
2019-10-02 18:43:27 +02:00
echo json_encode(['error' => $e->getMessage()]);
2019-10-02 18:37:50 +02:00
return;
}
return;
}
$visualConsoleId = (int) get_parameter('visualConsoleId');
$getVisualConsole = (bool) get_parameter('getVisualConsole');
$getVisualConsoleItems = (bool) get_parameter('getVisualConsoleItems');
2019-06-18 10:13:56 +02:00
$updateVisualConsoleItem = (bool) get_parameter('updateVisualConsoleItem');
2019-10-02 12:28:17 +02:00
$createVisualConsoleItem = (bool) get_parameter('createVisualConsoleItem');
2019-07-08 17:51:01 +02:00
$getVisualConsoleItem = (bool) get_parameter('getVisualConsoleItem');
2019-07-25 17:58:37 +02:00
$removeVisualConsoleItem = (bool) get_parameter('removeVisualConsoleItem');
2019-07-31 13:23:25 +02:00
$copyVisualConsoleItem = (bool) get_parameter('copyVisualConsoleItem');
2019-09-20 15:14:26 +02:00
$getGroupsVisualConsoleItem = (bool) get_parameter(
'getGroupsVisualConsoleItem'
);
$getAllVisualConsole = (bool) get_parameter('getAllVisualConsole');
2019-08-05 14:58:27 +02:00
$getImagesVisualConsole = (bool) get_parameter('getImagesVisualConsole');
2020-01-13 17:13:52 +01:00
$getTimeZoneVisualConsole = (bool) get_parameter('getTimeZoneVisualConsole');
2019-09-20 15:14:26 +02:00
$autocompleteAgentsVisualConsole = (bool) get_parameter(
'autocompleteAgentsVisualConsole'
);
$autocompleteModuleVisualConsole = (bool) get_parameter(
'autocompleteModuleVisualConsole'
);
$getCustomGraphVisualConsoleItem = (bool) get_parameter(
'getCustomGraphVisualConsoleItem'
);
2019-09-22 20:35:51 +02:00
$serviceListVisualConsole = (bool) get_parameter(
'serviceListVisualConsole'
);
2019-12-17 10:57:19 +01:00
$loadtabs = (bool) get_parameter('loadtabs');
ob_clean();
2019-12-17 10:57:19 +01:00
if ($visualConsoleId) {
// Retrieve the visual console.
$visualConsole = VisualConsole::fromDB(['id' => $visualConsoleId]);
$visualConsoleData = $visualConsole->toArray();
$vcGroupId = $visualConsoleData['groupId'];
2019-12-17 10:57:19 +01:00
// ACL.
$aclRead = check_acl($config['id_user'], $vcGroupId, 'VR');
$aclWrite = check_acl($config['id_user'], $vcGroupId, 'VW');
$aclManage = check_acl($config['id_user'], $vcGroupId, 'VM');
2019-12-17 10:57:19 +01:00
if (!$aclRead && !$aclWrite && !$aclManage) {
db_pandora_audit(
'ACL Violation',
'Trying to access visual console without group access'
);
http_response_code(403);
return;
}
}
if ($getVisualConsole === true) {
echo $visualConsole;
return;
} else if ($getVisualConsoleItems === true) {
// Check groups can access user.
$aclUserGroups = [];
if (!users_can_manage_group_all('AR')) {
$aclUserGroups = array_keys(users_get_groups(false, 'AR'));
}
$vcItems = VisualConsole::getItemsFromDB($visualConsoleId, $aclUserGroups);
echo '['.implode($vcItems, ',').']';
return;
} else if ($getVisualConsoleItem === true
|| $updateVisualConsoleItem === true
) {
$itemId = (int) get_parameter('visualConsoleItemId');
try {
$item = VisualConsole::getItemFromDB($itemId);
} catch (Throwable $e) {
// Bad params.
2019-08-08 11:05:21 +02:00
http_response_code(400);
return;
}
$itemData = $item->toArray();
$itemType = $itemData['type'];
$itemAclGroupId = $itemData['aclGroupId'];
// ACL.
$aclRead = check_acl($config['id_user'], $itemAclGroupId, 'VR');
$aclWrite = check_acl($config['id_user'], $itemAclGroupId, 'VW');
$aclManage = check_acl($config['id_user'], $itemAclGroupId, 'VM');
if (!$aclRead && !$aclWrite && !$aclManage) {
db_pandora_audit(
'ACL Violation',
'Trying to access visual console without group access'
);
http_response_code(403);
return;
}
// Check also the group Id for the group item.
if ($itemType === GROUP_ITEM) {
2019-07-29 17:45:59 +02:00
$itemGroupId = $itemData['groupId'];
// ACL.
$aclRead = check_acl($config['id_user'], $itemGroupId, 'VR');
$aclWrite = check_acl($config['id_user'], $itemGroupId, 'VW');
$aclManage = check_acl($config['id_user'], $itemGroupId, 'VM');
if (!$aclRead && !$aclWrite && !$aclManage) {
db_pandora_audit(
'ACL Violation',
'Trying to access visual console without group access'
);
http_response_code(403);
return;
}
}
2019-07-08 17:51:01 +02:00
if ($getVisualConsoleItem === true) {
echo $item;
return;
} else if ($updateVisualConsoleItem === true) {
$data = get_parameter('data');
2019-08-09 15:19:35 +02:00
if ($data) {
$data['id'] = $itemId;
$result = $item->save($data);
echo $item;
}
2019-07-08 17:51:01 +02:00
return;
}
2019-10-02 12:28:17 +02:00
} else if ($createVisualConsoleItem === true) {
// TODO: ACL.
$data = get_parameter('data');
if ($data) {
// Inserted data in new item.
$class = VisualConsole::getItemClass((int) $data['type']);
try {
// Save the new item.
$data['id_layout'] = $visualConsoleId;
$result = $class::save($data);
} catch (\Throwable $th) {
// There is no item in the database.
echo false;
return;
}
// Extract data new item inserted.
try {
$item = VisualConsole::getItemFromDB($result);
} catch (Throwable $e) {
// Bad params.
http_response_code(400);
return;
}
echo $item;
} else {
echo false;
}
return;
2019-07-25 17:58:37 +02:00
} else if ($removeVisualConsoleItem === true) {
$itemId = (int) get_parameter('visualConsoleItemId');
2019-07-26 14:17:14 +02:00
try {
$item = VisualConsole::getItemFromDB($itemId);
} catch (\Throwable $th) {
// There is no item in the database.
2019-08-08 11:05:21 +02:00
http_response_code(404);
2019-07-26 14:17:14 +02:00
return;
}
2019-07-30 11:20:03 +02:00
$itemData = $item->toArray();
$itemAclGroupId = $itemData['aclGroupId'];
$aclWrite = check_acl($config['id_user'], $itemAclGroupId, 'VW');
$aclManage = check_acl($config['id_user'], $itemAclGroupId, 'VM');
// ACL.
if (!$aclWrite && !$aclManage) {
db_pandora_audit(
'ACL Violation',
'Trying to delete visual console item without group access'
);
http_response_code(403);
return;
}
2019-07-25 17:58:37 +02:00
$data = get_parameter('data');
2019-07-30 11:20:03 +02:00
$result = $item::delete($itemId);
2019-07-31 13:23:25 +02:00
echo $result;
return;
} else if ($copyVisualConsoleItem === true) {
$itemId = (int) get_parameter('visualConsoleItemId');
// Get a copy of the item.
$item = VisualConsole::getItemFromDB($itemId);
$data = $item->toArray();
$data['id_layout'] = $visualConsoleId;
$data['x'] = ($data['x'] + 20);
$data['y'] = ($data['y'] + 20);
unset($data['id']);
$class = VisualConsole::getItemClass((int) $data['type']);
try {
// Save the new item.
$result = $class::save($data);
} catch (\Throwable $th) {
// There is no item in the database.
echo false;
return;
}
2019-07-25 17:58:37 +02:00
echo $result;
return;
2019-07-31 11:03:13 +02:00
} else if ($getGroupsVisualConsoleItem === true) {
$data = users_get_groups_for_select(
$config['id_user'],
'AR',
true,
true
);
$result = array_map(
function ($id) use ($data) {
return [
'value' => $id,
'text' => $data[$id],
];
},
array_keys($data)
);
echo json_encode($result);
return;
} else if ($getAllVisualConsole === true) {
2020-01-07 12:00:39 +01:00
// TODO: Remove.
2019-08-05 14:58:27 +02:00
} else if ($getImagesVisualConsole) {
2020-01-07 15:43:44 +01:00
$img = get_parameter('nameImg', 'appliance');
$only = (bool) get_parameter('only', 0);
$count = Item::imagesElementsVC($img, $only);
echo json_encode($count);
return;
2020-01-13 17:13:52 +01:00
} else if ($getTimeZoneVisualConsole) {
$zone = get_parameter('zone', 'Europe');
$zones = Item::zonesVC($zone);
echo json_encode($zones);
return;
2019-08-19 12:32:38 +02:00
} else if ($autocompleteAgentsVisualConsole) {
$params = (array) get_parameter('data', []);
2019-08-22 10:11:06 +02:00
$string = $params['value'];
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
// TODO: ACL.
$id_group = (int) get_parameter('id_group', -1);
2019-08-19 12:32:38 +02:00
if ($id_group != -1) {
if ($id_group == 0) {
2019-08-22 10:11:06 +02:00
$user_groups = users_get_groups(
$config['id_user'],
'AR',
true
);
2019-08-19 12:32:38 +02:00
$filter['id_grupo'] = array_keys($user_groups);
} else {
$filter['id_grupo'] = $id_group;
}
}
2019-08-22 10:11:06 +02:00
$filter = [];
2019-08-19 12:32:38 +02:00
$filter['disabled'] = 0;
2019-08-22 10:11:06 +02:00
$filter[] = sprintf(
'(alias LIKE "%%%s%%")
OR (alias NOT LIKE "%%%s%%"
AND nombre COLLATE utf8_general_ci LIKE "%%%s%%")
OR (alias NOT LIKE "%%%s%%"
AND nombre COLLATE utf8_general_ci NOT LIKE "%%%s%%"
AND direccion LIKE "%%%s%%")
OR (alias NOT LIKE "%%%s%%"
AND nombre COLLATE utf8_general_ci NOT LIKE "%%%s%%"
AND direccion NOT LIKE "%%%s%%"
AND comentarios LIKE "%%%s%%"
)',
$string,
$string,
$string,
$string,
$string,
$string,
$string,
$string,
$string,
$string
2019-08-19 12:32:38 +02:00
);
2019-08-22 10:11:06 +02:00
$data = [];
if (is_metaconsole() === true) {
enterprise_include_once('include/functions_metaconsole.php');
$metaconsole_connections = metaconsole_get_connection_names();
// For all nodes.
if (isset($metaconsole_connections) === true
&& is_array($metaconsole_connections) === true
) {
foreach ($metaconsole_connections as $metaconsole) {
// Get server connection data.
$server_data = metaconsole_get_connection($metaconsole);
// Establishes connection.
if (metaconsole_load_external_db($server_data) !== NOERR) {
continue;
}
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
$agents = agents_get_agents(
$filter,
[
'id_agente',
'nombre',
'direccion',
'alias',
]
);
if (isset($agents) === true && is_array($agents) === true) {
foreach ($agents as $agent) {
$data[] = [
'id' => $agent['id_agente'],
'name' => io_safe_output(
$agent['nombre']
),
'alias' => io_safe_output(
$agent['alias']
),
'ip' => io_safe_output(
$agent['direccion']
),
'filter' => 'alias',
'metaconsoleId' => $server_data['id'],
'metaconsoleName' => $metaconsole,
];
}
}
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
metaconsole_restore_db();
2019-08-19 12:32:38 +02:00
}
}
2019-08-22 10:11:06 +02:00
} else {
$agents = agents_get_agents(
2019-09-19 13:45:13 +02:00
$filter,
2019-08-22 10:11:06 +02:00
[
'id_agente',
'nombre',
'direccion',
'alias',
]
);
if (isset($agents) === true && is_array($agents) === true) {
foreach ($agents as $agent) {
$data[] = [
'id' => $agent['id_agente'],
'name' => io_safe_output($agent['nombre']),
'alias' => io_safe_output($agent['alias']),
'ip' => io_safe_output($agent['direccion']),
'filter' => 'alias',
];
2019-08-19 12:32:38 +02:00
}
}
2019-08-22 10:11:06 +02:00
}
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
echo json_encode($data);
return;
} else if ($autocompleteModuleVisualConsole) {
$data = (array) get_parameter('data', []);
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
$result = [];
if (is_metaconsole()) {
enterprise_include_once('include/functions_metaconsole.php');
$connection = metaconsole_get_connection_by_id($data['metaconsoleId']);
if (metaconsole_connect($connection) !== NOERR) {
echo json_encode($result);
return;
2019-08-19 12:32:38 +02:00
}
2019-08-22 10:11:06 +02:00
}
2019-08-19 12:32:38 +02:00
2019-09-19 17:47:41 +02:00
if ($data['type'] == DONUT_GRAPH) {
// Only type sting.
$filter = sprintf(
'id_tipo_modulo IN (17,23,3,10,33)'
);
}
2019-08-22 10:11:06 +02:00
$agent_modules = agents_get_modules(
2019-09-19 17:47:41 +02:00
$data['agentId'],
false,
$filter
2019-08-22 10:11:06 +02:00
);
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
if (is_metaconsole()) {
// Restore db connection.
metaconsole_restore_db();
}
2019-08-19 12:32:38 +02:00
2019-08-22 10:11:06 +02:00
if (isset($agent_modules) === true && is_array($agent_modules) === true) {
$result = array_map(
function ($id) use ($agent_modules) {
return [
'moduleId' => $id,
'moduleName' => io_safe_output($agent_modules[$id]),
];
},
array_keys($agent_modules)
);
}
2019-08-19 12:32:38 +02:00
2019-09-20 15:14:26 +02:00
echo json_encode($result);
return;
} else if ($getCustomGraphVisualConsoleItem) {
2019-12-19 20:42:29 +01:00
// Remove.
2019-09-22 20:35:51 +02:00
} else if ($serviceListVisualConsole) {
if (!enterprise_installed()) {
echo json_encode(false);
return;
}
enterprise_include_once('include/functions_services.php');
// Services list.
$services = [];
$services = enterprise_hook(
'services_get_services',
[
false,
[
'id',
'name',
],
]
);
echo io_safe_output(json_encode($services));
2019-12-17 10:57:19 +01:00
return;
} else if ($loadtabs) {
$viewer = new Viewer();
echo $viewer->loadForm();
2019-09-22 20:35:51 +02:00
return;
}
exit;