Merge branch '2229-Gestion_de_politicas_desde_API' into 'develop'
add functions api for policies node See merge request artica/pandorafms!1626
This commit is contained in:
commit
a5c1caab1f
|
@ -99,78 +99,69 @@ function returnError($typeError, $returnType = 'string') {
|
|||
break;
|
||||
default:
|
||||
returnData("string",
|
||||
array('type' => 'string', 'data' => __($returnType)));
|
||||
array('type' => 'string', 'data' => __($typeError)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $returnType
|
||||
* @param $data
|
||||
* @param $separator
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
function returnData($returnType, $data, $separator = ';') {
|
||||
switch ($returnType) {
|
||||
case 'string':
|
||||
if ($data['type'] == 'string') {
|
||||
echo $data['data'];
|
||||
if( is_array($data['data']) ){
|
||||
echo convert_array_multi($data['data'], $separator);
|
||||
}
|
||||
else {
|
||||
//TODO
|
||||
else{
|
||||
echo $data['data'];
|
||||
}
|
||||
break;
|
||||
case 'csv':
|
||||
case 'csv_head':
|
||||
switch ($data['type']) {
|
||||
case 'array':
|
||||
if (array_key_exists('list_index', $data))
|
||||
{
|
||||
if ($returnType == 'csv_head') {
|
||||
foreach($data['list_index'] as $index) {
|
||||
echo $index;
|
||||
if (end($data['list_index']) == $index)
|
||||
echo "\n";
|
||||
else
|
||||
echo $separator;
|
||||
}
|
||||
}
|
||||
foreach($data['data'] as $dataContent) {
|
||||
foreach($data['list_index'] as $index) {
|
||||
if (array_key_exists($index, $dataContent))
|
||||
echo str_replace("\n", " ", $dataContent[$index]);
|
||||
if (end($data['list_index']) == $index)
|
||||
echo "\n";
|
||||
else
|
||||
echo $separator;
|
||||
}
|
||||
if( is_array($data['data']) ){
|
||||
if (array_key_exists('list_index', $data)) {
|
||||
if ($returnType == 'csv_head') {
|
||||
foreach($data['list_index'] as $index) {
|
||||
echo $index;
|
||||
if (end($data['list_index']) == $index)
|
||||
echo "\n";
|
||||
else
|
||||
echo $separator;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!empty($data['data'])) {
|
||||
|
||||
foreach ($data['data'] as $dataContent) {
|
||||
|
||||
$clean = array_map("array_apply_io_safe_output", $dataContent);
|
||||
|
||||
foreach ($clean as $k => $v) {
|
||||
$clean[$k] = str_replace("\r", "\n", $clean[$k]);
|
||||
$clean[$k] = str_replace("\n", " ", $clean[$k]);
|
||||
$clean[$k] = strip_tags($clean[$k]);
|
||||
$clean[$k] = str_replace(';',' ',$clean[$k]);
|
||||
}
|
||||
$row = implode($separator, $clean);
|
||||
|
||||
echo $row . "\n";
|
||||
}
|
||||
foreach($data['data'] as $dataContent) {
|
||||
foreach($data['list_index'] as $index) {
|
||||
if (array_key_exists($index, $dataContent))
|
||||
echo str_replace("\n", " ", $dataContent[$index]);
|
||||
if (end($data['list_index']) == $index)
|
||||
echo "\n";
|
||||
else
|
||||
echo $separator;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'string':
|
||||
echo $data['data'];
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (!empty($data['data'])) {
|
||||
foreach ($data['data'] as $dataContent) {
|
||||
$clean = array_map("array_apply_io_safe_output", $dataContent);
|
||||
foreach ($clean as $k => $v) {
|
||||
$clean[$k] = str_replace("\r", "\n", $clean[$k]);
|
||||
$clean[$k] = str_replace("\n", " ", $clean[$k]);
|
||||
$clean[$k] = strip_tags($clean[$k]);
|
||||
$clean[$k] = str_replace(';',' ',$clean[$k]);
|
||||
}
|
||||
$row = implode($separator, $clean);
|
||||
echo $row . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
echo $data['data'];
|
||||
}
|
||||
break;
|
||||
case 'json':
|
||||
|
@ -180,13 +171,13 @@ function returnData($returnType, $data, $separator = ';') {
|
|||
if ($separator == ";") {
|
||||
$separator = null;
|
||||
}
|
||||
|
||||
|
||||
if(empty($separator)){
|
||||
echo json_encode ($data);
|
||||
} else {
|
||||
echo json_encode ($data, $separator);
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -5400,74 +5391,6 @@ function api_set_planned_downtimes_additem ($id, $thrash1, $other, $thrash3) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add agent to a policy. And return a message with the result of the operation.
|
||||
*
|
||||
* @param string $id Id of the target policy.
|
||||
* @param $thrash1 Don't use.
|
||||
* @param array $other it's array, $other as param is <id_agent> in this order
|
||||
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
|
||||
* example:
|
||||
*
|
||||
* example:
|
||||
*
|
||||
* api.php?op=set&op2=add_agent_policy&id=1&other=167&other_mode=url_encode_separator_|
|
||||
*
|
||||
* @param $thrash3 Don't use
|
||||
*/
|
||||
function api_set_add_agent_policy($id, $thrash1, $other, $thrash2) {
|
||||
if (defined ('METACONSOLE')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($id == "") {
|
||||
returnError('error_add_agent_policy', __('Error adding agent to policy. Id_policy cannot be left blank.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if ($other['data'][0] == "") {
|
||||
returnError('error_add_agent_policy', __('Error adding agent to policy. Id_agent cannot be left blank.'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the agent exists and permissions
|
||||
if (!util_api_check_agent_and_print_error((int) $other['data'][0], 'string', "AW")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check the policy permissions and existence
|
||||
if (enterprise_hook('policies_check_user_policy', array($id)) === false) {
|
||||
$result_agent = db_get_value ('id_agente', 'tagente', 'id_agente', (int) $other['data'][0]);
|
||||
if ($result_agent) {
|
||||
returnError('error_add_agent_policy', __('Error adding agent to policy. Id policy doesn\'t exist.'));
|
||||
return;
|
||||
}
|
||||
returnError('forbidden', 'string');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the agent is already in the policy
|
||||
$id_agent_policy = enterprise_hook('policies_get_agents', array($id, array('id_agent' => $other['data'][0]), 'id'));
|
||||
|
||||
if ($id_agent_policy === ENTERPRISE_NOT_HOOK) {
|
||||
returnError('error_add_agent_policy', __('Error adding agent to policy.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if ($id_agent_policy === false) {
|
||||
$success = enterprise_hook('policies_create_agent', array($other['data'][0], $id));
|
||||
}
|
||||
else {
|
||||
returnError('error_add_agent_policy', __('Error adding agent to policy. The agent is already in the policy.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if ($success)
|
||||
returnData('string', array('type' => 'string', 'data' => $success));
|
||||
else
|
||||
returnError('error_add_agent_policy', 'Error adding agent to policy.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add data module to policy. And return id from new module.
|
||||
*
|
||||
|
@ -6429,153 +6352,6 @@ function api_set_update_snmp_module_policy($id, $thrash1, $other, $thrash3) {
|
|||
array('type' => 'string', 'data' => __('SNMP policy module updated.')));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply policy. And return id from the applying operation.
|
||||
*
|
||||
* @param string $id Id of the target policy.
|
||||
* @param $thrash1 Don't use.
|
||||
* @param array $other Don't use
|
||||
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
|
||||
* example:
|
||||
*
|
||||
* api.php?op=set&op2=apply_policy&id=1
|
||||
*
|
||||
* @param $thrash3 Don't use
|
||||
*/
|
||||
function api_set_apply_policy($id, $thrash1, $other, $thrash3) {
|
||||
if (defined ('METACONSOLE')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($id == "") {
|
||||
returnError('error_apply_policy', __('Error applying policy. Id_policy cannot be left blank.'));
|
||||
return;
|
||||
}
|
||||
|
||||
# Check if this operation is duplicated
|
||||
$duplicated = enterprise_hook('policies_get_policy_queue_status', array($id));
|
||||
|
||||
if ($duplicated === ENTERPRISE_NOT_HOOK) {
|
||||
// We want to return a value
|
||||
if ($other == "return") {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
returnError('error_apply_policy', __('Error applying policy.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ($duplicated == STATUS_IN_QUEUE_APPLYING or $duplicated == STATUS_IN_QUEUE_IN) {
|
||||
// We want to return a value
|
||||
if ($other == "return") {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
returnError('error_apply_policy',
|
||||
__('Error applying policy. This policy is already pending to apply.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$check_acl = enterprise_hook('policies_check_user_policy', array($id));
|
||||
if ($check_acl !== true) {
|
||||
// We want to return a value
|
||||
if ($other == "return") {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
returnError('error_apply_policy', __('Error applying policy.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$id = enterprise_hook('add_policy_queue_operation', array($id, 0, 'apply'));
|
||||
|
||||
if ($id === ENTERPRISE_NOT_HOOK) {
|
||||
// We want to return a value
|
||||
if ($other == "return") {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
returnError('error_apply_policy', __('Error applying policy.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// We want to return a value
|
||||
if ($other == "return") {
|
||||
if ($id)
|
||||
return $id;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
if ($id)
|
||||
returnData('string', array('type' => 'string', 'data' => $id));
|
||||
else
|
||||
returnError('error_apply_policy', 'Error applying policy.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply all policy in database. And return the number of policies applied.
|
||||
*
|
||||
* @param string $id Don't use.
|
||||
* @param $thrash1 Don't use.
|
||||
* @param array $other Don't use
|
||||
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
|
||||
* example:
|
||||
*
|
||||
* api.php?op=set&op2=apply_all_policies
|
||||
*
|
||||
* @param $thrash3 Don't use
|
||||
*/
|
||||
function api_set_apply_all_policies($thrash1, $thrash2, $other, $thrash3) {
|
||||
global $config;
|
||||
if (defined ('METACONSOLE')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!check_acl($config['id_user'], 0, "AW")) {
|
||||
returnError('forbidden', 'string');
|
||||
return;
|
||||
}
|
||||
|
||||
$policies = array();
|
||||
|
||||
# Get all policies
|
||||
$policies = enterprise_hook('policies_get_policies', array(false, false, false));
|
||||
|
||||
if ($policies === ENTERPRISE_NOT_HOOK) {
|
||||
returnError('error_apply_all_policy', __('Error applying all policies.'));
|
||||
return;
|
||||
}
|
||||
if ($policies === false) $policies = array();
|
||||
|
||||
$num_policies = count($policies);
|
||||
$count_results = 0;
|
||||
foreach ($policies as $policy) {
|
||||
$return_value = enterprise_hook('add_policy_queue_operation',
|
||||
array($policy['id'], 0, 'apply'));
|
||||
|
||||
if ($return_value != -1) {
|
||||
$count_results++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($num_policies > $count_results) {
|
||||
$errors = $num_policies - $count_results;
|
||||
|
||||
returnError('error_apply_policy', 'Error applying policy. ' . $errors . ' failed. ');
|
||||
}
|
||||
else {
|
||||
returnData('string', array('type' => 'string', 'data' => $count_results));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new group. And return the id_group of the new group.
|
||||
*
|
||||
|
|
|
@ -301,4 +301,22 @@ function get_complementary_rgb ($hexcode) {
|
|||
|
||||
return $rgbhex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns convert array multidimensional to string whit gluer.
|
||||
* @param array $array to convert
|
||||
* @param string glue to implode
|
||||
*/
|
||||
function convert_array_multi($array, $glue) {
|
||||
$result = '';
|
||||
foreach ($array as $item) {
|
||||
if (is_array($item)) {
|
||||
$result .= convert_array_multi($item, $glue) . $glue;
|
||||
} else {
|
||||
$result .= $item . $glue;
|
||||
}
|
||||
}
|
||||
$result = substr($result, 0, 0-strlen($glue));
|
||||
return $result;
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue