2015-06-22 19:12:17 +02:00
|
|
|
<?php
|
2021-03-04 11:29:52 +01:00
|
|
|
/**
|
|
|
|
* CSV charts.
|
|
|
|
*
|
|
|
|
* @category CSV charts
|
|
|
|
* @package Pandora FMS
|
|
|
|
* @subpackage Community
|
|
|
|
* @version 1.0.0
|
|
|
|
* @license See below
|
|
|
|
*
|
|
|
|
* ______ ___ _______ _______ ________
|
|
|
|
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
|
|
|
|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
|
|
|
|
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
|
|
|
|
*
|
|
|
|
* ============================================================================
|
|
|
|
* 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.
|
|
|
|
* ============================================================================
|
|
|
|
*/
|
2015-06-22 19:12:17 +02:00
|
|
|
|
2021-03-04 11:29:52 +01:00
|
|
|
// Load files.
|
2019-01-30 16:18:44 +01:00
|
|
|
require_once '../../include/config.php';
|
|
|
|
require_once '../../include/functions.php';
|
2020-12-01 11:56:15 +01:00
|
|
|
enterprise_include_once('include/functions_reporting_csv.php');
|
2015-06-22 19:12:17 +02:00
|
|
|
|
|
|
|
global $config;
|
|
|
|
|
2015-11-23 02:43:21 +01:00
|
|
|
$user_language = get_user_language($config['id_user']);
|
2019-01-30 16:18:44 +01:00
|
|
|
$l10n = null;
|
2021-03-04 11:29:52 +01:00
|
|
|
if (file_exists('../languages/'.$user_language.'.mo') === true) {
|
2021-04-23 08:58:23 +02:00
|
|
|
$cf = new CachedFileReader('../languages/'.$user_language.'.mo');
|
|
|
|
$l10n = new gettext_reader($cf);
|
2019-01-30 16:18:44 +01:00
|
|
|
$l10n->load_tables();
|
2015-11-23 02:43:21 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:29:52 +01:00
|
|
|
// Get data.
|
2015-06-22 19:12:17 +02:00
|
|
|
$type = (string) get_parameter('type', 'csv');
|
|
|
|
|
|
|
|
$data = (string) get_parameter('data');
|
|
|
|
$data = json_decode(io_safe_output($data), true);
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$default_filename = 'data_exported - '.date($config['date_format']);
|
2015-06-22 19:12:17 +02:00
|
|
|
$filename = (string) get_parameter('filename', $default_filename);
|
|
|
|
$filename = io_safe_output($filename);
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
/*
|
2021-03-04 11:29:52 +01:00
|
|
|
* $data = array(
|
2015-06-22 19:12:17 +02:00
|
|
|
* 'head' => array(<column>,<column>,...,<column>),
|
|
|
|
* 'data' => array(
|
|
|
|
* array(<data>,<data>,...,<data>),
|
|
|
|
* array(<data>,<data>,...,<data>),
|
|
|
|
* ...,
|
|
|
|
* array(<data>,<data>,...,<data>),
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
*/
|
2021-03-04 11:29:52 +01:00
|
|
|
|
2015-06-22 19:12:17 +02:00
|
|
|
$output_csv = function ($data, $filename) {
|
2020-12-01 11:56:15 +01:00
|
|
|
global $config;
|
|
|
|
|
|
|
|
$separator = (string) $config['csv_divider'];
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$excel_encoding = (bool) get_parameter('excel_encoding', false);
|
|
|
|
|
2021-03-04 11:29:52 +01:00
|
|
|
// CSV Output.
|
2019-01-30 16:18:44 +01:00
|
|
|
header('Content-Type: text/csv; charset=UTF-8');
|
|
|
|
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
|
|
|
|
|
2021-03-04 11:29:52 +01:00
|
|
|
// BOM.
|
|
|
|
if ($excel_encoding === false) {
|
|
|
|
echo pack('C*', 0xEF, 0xBB, 0xBF);
|
2019-01-30 16:18:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header
|
2021-03-04 11:29:52 +01:00
|
|
|
// Item / data.
|
2019-01-30 16:18:44 +01:00
|
|
|
foreach ($data as $items) {
|
2021-03-04 11:29:52 +01:00
|
|
|
if (isset($items['head']) === false
|
|
|
|
|| isset($items['data']) === false
|
|
|
|
) {
|
|
|
|
throw new Exception(__('An error occured exporting the data'));
|
2019-01-30 16:18:44 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 11:56:15 +01:00
|
|
|
// Get key for item value.
|
|
|
|
$value_key = array_search('value', $items['head']);
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$head_line = implode($separator, $items['head']);
|
|
|
|
echo $head_line."\n";
|
|
|
|
foreach ($items['data'] as $item) {
|
2020-12-01 11:56:15 +01:00
|
|
|
// Find value and replace csv decimal separator.
|
|
|
|
$item[$value_key] = csv_format_numeric($item[$value_key]);
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$item = str_replace('--> '.__('Selected'), '', $item);
|
|
|
|
$line = implode($separator, $item);
|
|
|
|
|
2021-03-04 11:29:52 +01:00
|
|
|
if ($excel_encoding === true) {
|
2019-01-30 16:18:44 +01:00
|
|
|
echo mb_convert_encoding($line, 'UTF-16LE', 'UTF-8')."\n";
|
|
|
|
} else {
|
|
|
|
echo $line."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 19:12:17 +02:00
|
|
|
};
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
/*
|
2021-03-04 11:29:52 +01:00
|
|
|
* $data = array(
|
2015-06-22 19:12:17 +02:00
|
|
|
* array(
|
|
|
|
* 'key' => <value>,
|
|
|
|
* 'key' => <value>,
|
|
|
|
* ...,
|
|
|
|
* 'key' => <value>
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* 'key' => <value>,
|
|
|
|
* 'key' => <value>,
|
|
|
|
* ...,
|
|
|
|
* 'key' => <value>
|
|
|
|
* ),
|
|
|
|
* ...,
|
|
|
|
* array(
|
|
|
|
* 'key' => <value>,
|
|
|
|
* 'key' => <value>,
|
|
|
|
* ...,
|
|
|
|
* 'key' => <value>
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
*/
|
2021-03-04 11:29:52 +01:00
|
|
|
|
2015-06-22 19:12:17 +02:00
|
|
|
$output_json = function ($data, $filename) {
|
2021-03-04 11:29:52 +01:00
|
|
|
// JSON Output.
|
2019-01-30 16:18:44 +01:00
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
header('Content-Disposition: attachment; filename="'.$filename.'.json"');
|
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
|
|
|
|
$json = json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
} else {
|
|
|
|
$json = json_encode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($json !== false) {
|
|
|
|
echo $json;
|
|
|
|
}
|
2015-06-22 19:12:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2021-03-04 11:29:52 +01:00
|
|
|
if (empty($data) === true) {
|
2019-01-30 16:18:44 +01:00
|
|
|
throw new Exception(__('An error occured exporting the data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'json':
|
|
|
|
$output_json($data, $filename);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'csv':
|
|
|
|
default:
|
|
|
|
$output_csv($data, $filename);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
die($e->getMessage());
|
2015-06-22 19:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
exit;
|