pandorafms/pandora_console/include/class/ConfigPEN.class.php

806 lines
21 KiB
PHP
Raw Normal View History

<?php
/**
* PEN Configuration feature.
*
* @category Class
* @package Pandora FMS
* @subpackage Wizard Setup
* @version 0.0.1
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
2020-11-27 13:52:35 +01:00
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
global $config;
require_once $config['homedir'].'/include/class/HTML.class.php';
/**
* Config PEN Class
*/
class ConfigPEN extends HTML
{
2020-03-27 11:41:56 +01:00
/**
* Url of controller.
*
* @var string
*/
public $ajaxController;
/**
2020-03-26 09:04:10 +01:00
* URL Base
*
2020-03-26 09:04:10 +01:00
* @var string
*/
private $baseUrl;
/**
2020-03-27 11:41:56 +01:00
* Contructor.
*
* @param string $ajax_page Target ajax page.
*/
2020-03-27 11:41:56 +01:00
public function __construct($ajax_page)
{
global $config;
// Check access.
check_login();
2020-03-26 14:41:54 +01:00
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
2022-01-20 10:55:23 +01:00
AUDIT_LOG_ACL_VIOLATION,
2020-03-26 14:41:54 +01:00
'Trying to access PEN Definition feature'
);
2020-03-26 14:41:54 +01:00
include 'general/noaccess.php';
exit;
}
2020-03-27 11:41:56 +01:00
$this->ajaxController = $ajax_page;
2020-03-26 09:04:10 +01:00
$this->offset = '';
2020-03-27 11:41:56 +01:00
$this->baseUrl = ui_get_full_url(
'index.php?sec=configuration_wizard_setup&sec2=godmode/modules/private_enterprise_numbers'
);
}
/**
* Returns an array with all the credentials matching filter and ACL.
*
* @param array $fields Fields array or 'count' keyword to retrieve count.
* @param array $filter Filters to be applied.
* @param integer $offset Offset (pagination).
* @param integer $limit Limit (pagination).
* @param string $order Sort order.
* @param string $sort_field Sort field.
*
* @return array With all results or false if error.
* @throws Exception On error.
*/
public static function getAll(
$fields,
$filter=null,
$offset=null,
$limit=null,
$order=null,
$sort_field=null
) {
$sql_filters = [];
$order_by = '';
$pagination = '';
$count = false;
if (!is_array($fields) && $fields == 'count') {
$fields = ['*'];
$count = true;
} else if (!is_array($fields)) {
error_log('[configPEN.getAll] Fields must be an array or "count".');
throw new Exception('[configPEN.getAll] Fields must be an array or "count".');
}
if (is_array($filter)) {
if (!empty($filter['free_search'])) {
$sql_filters[] = vsprintf(
' AND (lower(`manufacturer`) like lower("%%%s%%")
OR pen = "%s") ',
array_fill(0, 2, $filter['free_search'])
);
}
if (!empty($filter['pen'])) {
$sql_filters[] = sprintf(
' AND `pen` = %d',
$filter['pen']
);
}
}
if (isset($order)) {
$dir = 'asc';
if ($order == 'desc') {
$dir = 'desc';
};
if (in_array(
$sort_field,
[
'pen',
'manufacturer',
'description',
]
)
) {
$order_by = sprintf(
'ORDER BY `%s` %s',
$sort_field,
$dir
);
}
}
if (isset($limit) && $limit > 0
&& isset($offset) && $offset >= 0
) {
$pagination = sprintf(
' LIMIT %d OFFSET %d ',
$limit,
$offset
);
}
$sql = sprintf(
'SELECT %s
FROM `tpen`
WHERE 1=1
%s
%s
%s',
join(',', $fields),
join(' ', $sql_filters),
$order_by,
$pagination
);
if ($count) {
$sql = sprintf('SELECT count(*) as n FROM ( %s ) tt', $sql);
return db_get_value_sql($sql);
}
return db_get_all_rows_sql($sql);
}
/**
* AJAX: Return JSON content for datatable.
*
* @return void
*/
function draw()
{
global $config;
// Datatables offset, limit and order.
$filter = get_parameter('filter', []);
$start = get_parameter('start', 0);
$length = get_parameter('length', $config['block_size']);
$order = get_datatable_order(true);
try {
ob_start();
$fields = ['*'];
// Retrieve data.
$data = $this->getAll(
// Fields.
$fields,
// Filter.
$filter,
// Offset.
$start,
// Limit.
$length,
// Order.
$order['direction'],
// Sort field.
$order['field']
);
// Retrieve counter.
$count = $this->getAll(
'count',
$filter
);
if ($data) {
$data = array_reduce(
$data,
function ($carry, $item) {
// Transforms array of arrays $data into an array
// of objects, making a post-process of certain fields.
$tmp = (object) $item;
$tmp->description = io_safe_output($tmp->description);
$tmp->manufacturer = io_safe_output($tmp->manufacturer);
$tmp->options = '';
$tmp->options = '<a href="javascript:" onclick="showForm(\'';
$tmp->options .= $tmp->pen;
$tmp->options .= '\')" >';
$tmp->options .= html_print_image(
2021-03-16 13:37:13 +01:00
'images/operation.png',
2020-03-27 11:41:56 +01:00
true,
[
'title' => __('Show'),
'class' => 'invert_filter',
]
2020-03-27 11:41:56 +01:00
);
$tmp->options .= '</a>';
$tmp->options .= '<a href="javascript:" onclick="deletePEN(\'';
$tmp->options .= $tmp->pen;
$tmp->options .= '\')" >';
$tmp->options .= html_print_image(
'images/cross.png',
true,
[
'title' => __('Delete'),
'class' => 'invert_filter',
]
2020-03-27 11:41:56 +01:00
);
$tmp->options .= '</a>';
$carry[] = $tmp;
return $carry;
}
);
}
// Datatables format: RecordsTotal && recordsfiltered.
echo json_encode(
[
'data' => $data,
'recordsTotal' => $count,
'recordsFiltered' => $count,
]
);
// Capture output.
$response = ob_get_clean();
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
exit;
}
// If not valid, show error with issue.
json_decode($response);
if (json_last_error() == JSON_ERROR_NONE) {
// If valid dump.
echo $response;
} else {
echo json_encode(
['error' => $response]
);
}
exit;
}
/**
* Run main page.
*
* @return void
*/
public function run()
{
// Require specific CSS and JS.
ui_require_css_file('wizard');
ui_require_css_file('discovery');
2020-03-27 11:41:56 +01:00
ui_require_css_file('pen');
// Header section.
// Breadcrums.
$this->setBreadcrum([]);
$this->prepareBreadcrum(
[
[
'link' => '',
2020-03-30 17:12:49 +02:00
'label' => __('Configuration'),
'selected' => false,
],
[
'link' => '',
'label' => __('Templates'),
'selected' => false,
],
[
'link' => $this->baseUrl,
'label' => __('Private Enterprise Numbers'),
'selected' => true,
],
],
true
);
ui_print_page_header(
__('Private Enterprise Numbers'),
'',
false,
'',
true,
'',
false,
'',
GENERIC_SIZE_TEXT,
'',
$this->printHeader(true)
);
2020-03-26 09:04:10 +01:00
// Definition for AJAX.
html_print_input_hidden(
'ajax_file',
ui_get_full_url('ajax.php', false, false, false)
);
2020-03-27 11:41:56 +01:00
// Ajax page (hidden).
html_print_input_hidden(
'ajax_page',
$this->ajaxController
);
2020-03-26 09:04:10 +01:00
// Allow message area.
html_print_div(['id' => 'message_show_area']);
// Prints the main table.
html_print_div(
[
'id' => 'main_table_area',
'content' => $this->createMainTable(),
]
);
}
/**
2020-03-27 11:41:56 +01:00
* Load modal information for PEN management.
*
2020-03-27 11:41:56 +01:00
* Ajax. Direct HTML.
*
* @return void
*/
2020-03-27 11:41:56 +01:00
public function loadModal()
{
2020-03-27 11:41:56 +01:00
$values = [];
$id = (int) get_parameter('pen', 0);
if ($id > 0) {
$values = $this->getAll(
// Fields.
['*'],
// Filter.
['pen' => $id]
);
if (is_array($values)) {
$values = $values[0];
}
}
$form = [
'action' => '#',
'id' => 'modal_form',
'onsubmit' => 'return false;',
'class' => '',
];
$inputs = [];
2020-03-27 11:50:41 +01:00
$arguments = [
'name' => 'pen',
'type' => 'number',
'value' => $values['pen'],
'required' => true,
'return' => true,
'size' => 50,
];
if ((bool) $values['pen']) {
$arguments['disabled'] = true;
}
2020-03-27 11:41:56 +01:00
$inputs[] = [
'label' => __('PEN'),
'class' => 'flex-row',
'id' => 'div-pen',
2020-03-27 11:50:41 +01:00
'arguments' => $arguments,
2020-03-27 11:41:56 +01:00
];
$inputs[] = [
'label' => __('Manufacturer'),
'class' => 'flex-row',
'arguments' => [
'name' => 'manufacturer',
'id' => 'manufacturer',
'type' => 'text',
'required' => true,
'value' => io_safe_output($values['manufacturer']),
'return' => true,
],
];
$inputs[] = [
'label' => __('Description'),
'class' => 'flex-row',
'arguments' => [
'name' => 'description',
'id' => 'description',
'type' => 'textarea',
'value' => io_safe_output($values['description']),
'return' => true,
'rows' => 50,
'columns' => 30,
],
];
2020-03-27 11:41:56 +01:00
echo '<div id="div-form">';
echo parent::printForm(
[
2020-03-27 11:41:56 +01:00
'form' => $form,
'inputs' => $inputs,
],
true
);
2020-03-27 11:41:56 +01:00
echo '</div>';
}
/**
* Delete a manufacturer register from db.
*
* @return void
*/
public function delete()
{
$pen = get_parameter('pen', 0);
if (empty($pen)) {
echo json_encode(['error' => __('PEN is required')]);
} else {
if (db_process_sql_delete('tpen', ['pen' => $pen]) !== false) {
echo json_encode(['result' => __('Successfully deleted')]);
} else {
global $config;
echo json_encode(['error' => $config['dbconnection']->error]);
}
}
}
2020-03-27 11:41:56 +01:00
/**
2020-03-27 11:50:41 +01:00
* Add or update a manufacturer to private enterprise numbers.
2020-03-27 11:41:56 +01:00
*
* @return void
*/
public function add()
{
$pen = get_parameter('pen', 0);
$manufacturer = io_safe_input(strip_tags(io_safe_output((string) get_parameter('manufacturer'))));
$description = io_safe_input(strip_tags(io_safe_output((string) get_parameter('description'))));
2020-03-27 11:50:41 +01:00
$is_new = (bool) get_parameter('is_new', false);
2020-03-27 11:41:56 +01:00
if (empty($pen)) {
$error = __('PEN is required.');
}
if (empty($manufacturer)) {
$error = __('Manufacturer is required');
}
if (!empty($error)) {
echo json_encode(
['error' => $error]
);
}
// Add if not exists.
$current = $this->getAll(['pen'], ['pen' => $pen]);
if ($current === false) {
// New.
2020-03-27 11:50:41 +01:00
if ($is_new === false) {
echo json_encode(
[
'error' => __('This PEN definition does not exist'),
]
);
exit;
}
2020-03-27 11:41:56 +01:00
$rs = db_process_sql_insert(
'tpen',
[
2020-03-27 11:41:56 +01:00
'pen' => $pen,
2020-04-01 16:21:28 +02:00
'manufacturer' => $manufacturer,
'description' => $description,
]
);
2020-03-27 11:41:56 +01:00
$str = __('created');
} else {
// Update.
2020-03-27 11:50:41 +01:00
if ($is_new === true) {
echo json_encode(
[
'error' => __('This PEN definition already exists'),
]
);
exit;
}
2020-03-27 11:41:56 +01:00
$rs = db_process_sql_update(
'tpen',
[
2020-04-01 16:21:28 +02:00
'manufacturer' => $manufacturer,
'description' => $description,
2020-03-27 11:41:56 +01:00
],
['pen' => $pen]
);
2020-03-27 11:41:56 +01:00
$str = __('updated');
}
2020-03-27 11:41:56 +01:00
if ($rs === false) {
global $config;
echo json_encode(['error' => $config['dbconnection']->error]);
} else {
echo json_encode(['result' => __('Succesfully %s', $str)]);
}
2020-03-27 11:41:56 +01:00
}
2020-03-27 11:41:56 +01:00
/**
* Create the main table with the PENs info
*
* @return string Return entire the table
*/
public function createMainTable()
{
global $config;
$output = '';
// Datatables list.
try {
$columns = [
'pen',
'manufacturer',
'description',
'options',
];
$column_names = [
__('PEN'),
__('Manufacturer'),
__('Description'),
[
'text' => __('Options'),
'class' => 'action_buttons',
],
];
$this->tableId = 'keystore';
// Load datatables user interface.
$output .= ui_print_datatable(
[
'id' => $this->tableId,
'return' => true,
'class' => 'info_table',
'style' => 'width: 100%',
'columns' => $columns,
'column_names' => $column_names,
'ajax_url' => $this->ajaxController,
'ajax_data' => ['method' => 'draw'],
'no_sortable_columns' => [-1],
'order' => [
'field' => 'pen',
'direction' => 'asc',
],
'search_button_class' => 'sub filter float-right',
'form' => [
'inputs' => [
[
'label' => __('Free search'),
'type' => 'text',
'class' => 'mw250px',
'id' => 'free_search',
'name' => 'free_search',
],
],
],
]
);
} catch (Exception $e) {
echo $e->getMessage();
}
// Auxiliar div.
$output .= '<div id="modal" class="invisible"></div>';
$output .= '<div id="msg" class="invisible"></div>';
$output .= '<div id="aux" class="invisible"></div>';
2020-03-27 11:41:56 +01:00
// Create button.
$output .= parent::printInput(
2020-03-26 09:04:10 +01:00
[
2020-03-27 11:41:56 +01:00
'type' => 'submit',
'name' => 'create',
'label' => __('Register manufacturer'),
'attributes' => 'class="sub next"',
'return' => true,
2020-03-26 09:04:10 +01:00
]
);
2020-03-27 11:41:56 +01:00
ob_start();
?>
<script type="text/javascript">
function cleanupDOM() {
$("#div-form").empty();
}
function deletePEN(id) {
confirmDialog({
title: "<?php echo __('Are you sure?'); ?>",
message: "<?php echo __('Are you sure you want to delete this PEN?'); ?>",
ok: "<?php echo __('OK'); ?>",
cancel: "<?php echo __('Cancel'); ?>",
onAccept: function() {
$.ajax({
method: "post",
url: "<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
data: {
page: "<?php echo $this->ajaxController; ?>",
method: "delete",
pen: id
},
datatype: "json",
success: function(data) {
showMsg(data);
},
error: function(e) {
showMsg(e);
}
});
}
});
}
function showForm(id) {
var btn_ok_text = "<?php echo __('OK'); ?>";
var btn_cancel_text = "<?php echo __('Cancel'); ?>";
var title = "<?php echo __('Register new manufacturer'); ?>";
2020-03-27 11:50:41 +01:00
var is_new = 1;
2020-03-27 11:41:56 +01:00
if (id) {
btn_ok_text = "<?php echo __('Update'); ?>";
title = "<?php echo __('Update'); ?> " + id;
2020-03-27 11:50:41 +01:00
is_new = 0;
2020-03-27 11:41:56 +01:00
}
load_modal({
target: $("#modal"),
form: "modal_form",
url: "<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
ajax_callback: showMsg,
cleanup: cleanupDOM,
modal: {
title: title,
ok: btn_ok_text,
cancel: btn_cancel_text
},
extradata: [
{
name: "pen",
value: id
2020-03-27 11:50:41 +01:00
},
{
name: 'is_new',
value: is_new
2020-03-27 11:41:56 +01:00
}
],
onshow: {
page: "<?php echo $this->ajaxController; ?>",
method: "loadModal"
},
onsubmit: {
page: "<?php echo $this->ajaxController; ?>",
method: "add"
}
});
}
/**
* Process ajax responses and shows a dialog with results.
*/
function showMsg(data) {
var title = "<?php echo __('Success'); ?>";
var text = "";
var failed = 0;
try {
data = JSON.parse(data);
text = data["result"];
} catch (err) {
title = "<?php echo __('Failed'); ?>";
text = err.message;
failed = 1;
}
if (!failed && data["error"] != undefined) {
title = "<?php echo __('Failed'); ?>";
text = data["error"];
failed = 1;
}
if (data["report"] != undefined) {
data["report"].forEach(function(item) {
text += "<br>" + item;
});
}
$("#msg").empty();
$("#msg").html(text);
$("#msg").dialog({
width: 450,
position: {
my: "center",
at: "center",
of: window,
collision: "fit"
},
title: title,
buttons: [
{
class:
"ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next",
text: "OK",
click: function(e) {
if (!failed) {
$(".ui-dialog-content").dialog("close");
$(".info").hide();
cleanupDOM();
dt_keystore.draw(false);
} else {
$(this).dialog("close");
}
}
}
]
});
}
$(document).ready(function() {
$("#submit-create").click(function() {
showForm();
});
});
</script>
<?php
$output .= ob_get_clean();
return $output;
}
}