From e1ec4ce40e95cca05be39c8640918ee09b7f3630 Mon Sep 17 00:00:00 2001
From: "alejandro.campos@artica.es" <alejandro.campos@artica.es>
Date: Fri, 18 Mar 2022 11:23:29 +0100
Subject: [PATCH 1/4] change storage format of custom data field in DB

---
 pandora_console/include/functions_events.php  |  3 +++
 .../operation/events/export_csv.php           | 23 +++++++++++++++++++
 pandora_server/util/pandora_manage.pl         |  3 ---
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php
index 6733be09ae..4aeb372286 100644
--- a/pandora_console/include/functions_events.php
+++ b/pandora_console/include/functions_events.php
@@ -2429,6 +2429,9 @@ function events_create_event(
         $source = get_product_name();
     }
 
+    $custom_data = base64_decode($custom_data);
+    $custom_data = mysql_escape_string_sql($custom_data);
+
     $table_events = 'tevento';
     if (is_metaconsole()) {
         $table_events = 'tmetaconsole_event';
diff --git a/pandora_console/operation/events/export_csv.php b/pandora_console/operation/events/export_csv.php
index 9f6b14c5fa..d3f9c7005a 100644
--- a/pandora_console/operation/events/export_csv.php
+++ b/pandora_console/operation/events/export_csv.php
@@ -183,6 +183,29 @@ try {
                         );
                     break;
 
+                    case 'custom_data':
+                        $custom_data_array = json_decode(
+                            $row[$key],
+                            true
+                        );
+
+                        $custom_data = '';
+                        $separator = ($config['csv_divider'] === ';') ? ',' : ';';
+
+                        if ($custom_data_array !== null) {
+                            array_walk(
+                                $custom_data_array,
+                                function (&$value, $field) {
+                                    $value = $field.'='.$value;
+                                }
+                            );
+
+                            $custom_data = implode($separator, $custom_data_array);
+                        }
+
+                        echo io_safe_output($custom_data);
+                    break;
+
                     default:
                         echo io_safe_output($row[$key]);
                     break;
diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl
index 2180863dd0..1bcd2e0519 100755
--- a/pandora_server/util/pandora_manage.pl
+++ b/pandora_server/util/pandora_manage.pl
@@ -4301,9 +4301,6 @@ sub cli_create_event() {
 		
 		print_log "[INFO] Adding event '$event' for agent '$agent_name' \n\n";
 
-		# Base64 encode custom data
-		$custom_data = encode_base64 ($custom_data, '');
-
 		pandora_event ($conf, $event, $id_group, $id_agent, $severity,
 			$id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, safe_input($comment), $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data);
 

From 5757d4a9334534bcecd9543ba3083b1282565ddf Mon Sep 17 00:00:00 2001
From: "alejandro.campos@artica.es" <alejandro.campos@artica.es>
Date: Wed, 23 Mar 2022 14:24:39 +0100
Subject: [PATCH 2/4] implement csv export of events in meta and minor fixes

---
 pandora_console/include/functions_api.php     |  5 +-
 pandora_console/include/functions_events.php  |  3 -
 pandora_console/operation/events/events.php   |  3 +-
 .../operation/events/export_csv.php           | 89 +++++++++++++------
 pandora_server/lib/PandoraFMS/Core.pm         | 14 ++-
 pandora_server/util/pandora_manage.pl         | 11 +--
 6 files changed, 80 insertions(+), 45 deletions(-)

diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php
index 55f9a1ce02..d94e82d4e3 100644
--- a/pandora_console/include/functions_api.php
+++ b/pandora_console/include/functions_api.php
@@ -13441,6 +13441,9 @@ function api_set_create_event($id, $trash1, $other, $returnType)
             $values['id_extra'] = '';
         }
 
+        $custom_data = base64_decode($values['custom_data']);
+        $custom_data = mysql_escape_string_sql($custom_data);
+
         $return = events_create_event(
             $values['event'],
             $values['id_grupo'],
@@ -13456,7 +13459,7 @@ function api_set_create_event($id, $trash1, $other, $returnType)
             $values['unknown_instructions'],
             $values['source'],
             $values['tags'],
-            $values['custom_data'],
+            $custom_data,
             $values['server_id'],
             $values['id_extra']
         );
diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php
index 4aeb372286..6733be09ae 100644
--- a/pandora_console/include/functions_events.php
+++ b/pandora_console/include/functions_events.php
@@ -2429,9 +2429,6 @@ function events_create_event(
         $source = get_product_name();
     }
 
-    $custom_data = base64_decode($custom_data);
-    $custom_data = mysql_escape_string_sql($custom_data);
-
     $table_events = 'tevento';
     if (is_metaconsole()) {
         $table_events = 'tmetaconsole_event';
diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php
index ec41980b23..e29b4cbc16 100644
--- a/pandora_console/operation/events/events.php
+++ b/pandora_console/operation/events/events.php
@@ -851,7 +851,7 @@ if ($pure) {
 
     // CSV.
     $csv['active'] = false;
-    $csv['text'] = '<a class="events_link" href="operation/events/export_csv.php?'.$filter_b64.'">'.html_print_image(
+    $csv['text'] = '<a class="events_link" href="'.ui_get_full_url(false, false, false, false).'operation/events/export_csv.php?'.$filter_b64.'">'.html_print_image(
         'images/csv.png',
         true,
         [
@@ -942,7 +942,6 @@ if ($pure) {
         );
     } else {
         unset($onheader['rss']);
-        unset($onheader['csv']);
         unset($onheader['sound_event']);
         unset($onheader['fullscreen']);
         ui_meta_print_header(__('Events'), $section_string, $onheader);
diff --git a/pandora_console/operation/events/export_csv.php b/pandora_console/operation/events/export_csv.php
index d3f9c7005a..84babb2e8a 100644
--- a/pandora_console/operation/events/export_csv.php
+++ b/pandora_console/operation/events/export_csv.php
@@ -84,35 +84,66 @@ $column_names = [
     'module_status',
 ];
 
-$fields = [
-    'te.id_evento',
-    'te.evento',
-    'te.timestamp',
-    'te.estado',
-    'te.event_type',
-    'te.utimestamp',
-    'te.id_agente',
-    'ta.alias as agent_name',
-    'te.id_usuario',
-    'te.id_grupo',
-    'te.id_agentmodule',
-    'am.nombre as module_name',
-    'te.id_alert_am',
-    'te.criticity',
-    'te.user_comment',
-    'te.tags',
-    'te.source',
-    'te.id_extra',
-    'te.critical_instructions',
-    'te.warning_instructions',
-    'te.unknown_instructions',
-    'te.owner_user',
-    'te.ack_utimestamp',
-    'te.custom_data',
-    'te.data',
-    'te.module_status',
-    'tg.nombre as group_name',
-];
+if (is_metaconsole() === true) {
+    $fields = [
+        'te.id_evento',
+        'te.evento',
+        'te.timestamp',
+        'te.estado',
+        'te.event_type',
+        'te.utimestamp',
+        'te.id_agente',
+        'ta.alias as agent_name',
+        'te.id_usuario',
+        'te.id_grupo',
+        'te.id_agentmodule',
+        'te.id_alert_am',
+        'te.criticity',
+        'te.user_comment',
+        'te.tags',
+        'te.source',
+        'te.id_extra',
+        'te.critical_instructions',
+        'te.warning_instructions',
+        'te.unknown_instructions',
+        'te.owner_user',
+        'te.ack_utimestamp',
+        'te.custom_data',
+        'te.data',
+        'te.module_status',
+        'tg.nombre as group_name',
+    ];
+} else {
+    $fields = [
+        'te.id_evento',
+        'te.evento',
+        'te.timestamp',
+        'te.estado',
+        'te.event_type',
+        'te.utimestamp',
+        'te.id_agente',
+        'ta.alias as agent_name',
+        'te.id_usuario',
+        'te.id_grupo',
+        'te.id_agentmodule',
+        'am.nombre as module_name',
+        'te.id_alert_am',
+        'te.criticity',
+        'te.user_comment',
+        'te.tags',
+        'te.source',
+        'te.id_extra',
+        'te.critical_instructions',
+        'te.warning_instructions',
+        'te.unknown_instructions',
+        'te.owner_user',
+        'te.ack_utimestamp',
+        'te.custom_data',
+        'te.data',
+        'te.module_status',
+        'tg.nombre as group_name',
+    ];
+}
 
 $now = date('Y-m-d');
 
diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm
index d81e5bd961..c633d4944e 100644
--- a/pandora_server/lib/PandoraFMS/Core.pm
+++ b/pandora_server/lib/PandoraFMS/Core.pm
@@ -3758,12 +3758,12 @@ Generate an event.
 
 =cut
 ##########################################################################
-sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$) {
+sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$$) {
 	my ($pa_config, $evento, $id_grupo, $id_agente, $severity,
 		$id_alert_am, $id_agentmodule, $event_type, $event_status, $dbh,
 		$source, $user_name, $comment, $id_extra, $tags,
 		$critical_instructions, $warning_instructions, $unknown_instructions, $custom_data,
-		$module_data, $module_status) = @_;
+		$module_data, $module_status, $server_id) = @_;
 	my $event_table = is_metaconsole($pa_config) ? 'tmetaconsole_event' : 'tevento';
 
 	my $agent = undef;
@@ -3808,6 +3808,7 @@ sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$) {
 	$warning_instructions = '' unless defined ($warning_instructions);
 	$unknown_instructions = '' unless defined ($unknown_instructions);
 	$custom_data = '' unless defined ($custom_data);
+	$server_id = 0 unless defined ($server_id);
 	$module_data = defined($module) ? $module->{'datos'} : '' unless defined ($module_data);
 	$module_status = defined($module) ? $module->{'estado'} : '' unless defined ($module_status);
 	
@@ -3829,10 +3830,17 @@ sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$) {
 		db_do ($dbh, 'UPDATE ' . $event_table . ' SET estado = 1, ack_utimestamp = ? WHERE estado IN (0,2) AND id_extra=?', $utimestamp, $id_extra);
 	}
 	
+	my $event_id = undef;
+
 	# Create the event
 	logger($pa_config, "Generating event '$evento' for agent ID $id_agente module ID $id_agentmodule.", 10);
-	my $event_id = db_insert ($dbh, 'id_evento','INSERT INTO ' . $event_table . ' (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, user_comment, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, custom_data, data, module_status)
+	if (is_metaconsole($pa_config)) {
+			$event_id = db_insert ($dbh, 'id_evento','INSERT INTO ' . $event_table . ' (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, user_comment, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, server_id, custom_data, data, module_status)
+	              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id_agente, $id_grupo, safe_input ($evento), $timestamp, $event_status, $utimestamp, $event_type, $id_agentmodule, $id_alert_am, $severity, $comment, $module_tags, $source, $id_extra, $user_name, $critical_instructions, $warning_instructions, $unknown_instructions, $ack_utimestamp, $server_id, $custom_data, safe_input($module_data), $module_status);
+	} else {
+			$event_id = db_insert ($dbh, 'id_evento','INSERT INTO ' . $event_table . ' (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, user_comment, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, custom_data, data, module_status)
 	              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id_agente, $id_grupo, safe_input ($evento), $timestamp, $event_status, $utimestamp, $event_type, $id_agentmodule, $id_alert_am, $severity, $comment, $module_tags, $source, $id_extra, $user_name, $critical_instructions, $warning_instructions, $unknown_instructions, $ack_utimestamp, $custom_data, safe_input($module_data), $module_status);
+	}
 
 	# Do not write to the event file
 	return $event_id if ($pa_config->{'event_file'} eq '');
diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl
index 1bcd2e0519..7ac3f96bf8 100755
--- a/pandora_server/util/pandora_manage.pl
+++ b/pandora_server/util/pandora_manage.pl
@@ -4186,7 +4186,7 @@ sub cli_delete_profile() {
 ##############################################################################
 
 sub cli_create_event() {
-	my ($event,$event_type,$group_name,$agent_name,$module_name,$event_status,$severity,$template_name, $user_name, $comment, $source, $id_extra, $tags, $custom_data,$force_create_agent,$c_instructions,$w_instructions,$u_instructions, $use_alias) = @ARGV[2..20];
+	my ($event,$event_type,$group_name,$agent_name,$module_name,$event_status,$severity,$template_name, $user_name, $comment, $source, $id_extra, $tags, $custom_data,$force_create_agent,$c_instructions,$w_instructions,$u_instructions,$use_alias,$server_id) = @ARGV[2..21];
 
 	$event_status = 0 unless defined($event_status);
 	$severity = 0 unless defined($severity);
@@ -4251,11 +4251,8 @@ sub cli_create_event() {
 			
 			print_log "[INFO] Adding event '$event' for agent '$agent_name' \n\n";
 
-			# Base64 encode custom data
-			$custom_data = encode_base64 ($custom_data, '');
-
 			pandora_event ($conf, $event, $id_group, $id_agent, $severity,
-				$id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, safe_input($comment), $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data);
+				$id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, safe_input($comment), $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data, undef, undef, $server_id);
 		}
 	} else {
 		if (! $agent_name) {
@@ -4302,7 +4299,7 @@ sub cli_create_event() {
 		print_log "[INFO] Adding event '$event' for agent '$agent_name' \n\n";
 
 		pandora_event ($conf, $event, $id_group, $id_agent, $severity,
-			$id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, safe_input($comment), $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data);
+			$id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, safe_input($comment), $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data, undef, undef, $server_id);
 
 	}
 }
@@ -7602,7 +7599,7 @@ sub pandora_manage_main ($$$) {
 			cli_delete_profile();
 		}
 		elsif ($param eq '--create_event') {
-			param_check($ltotal, 19, 16);
+			param_check($ltotal, 20, 17);
 			cli_create_event();
 		}		
 		elsif ($param eq '--validate_event') {

From c7dd55e9599bc3f8c0d3b0d0b31d843d19f0c150 Mon Sep 17 00:00:00 2001
From: "alejandro.campos@artica.es" <alejandro.campos@artica.es>
Date: Wed, 23 Mar 2022 17:18:24 +0100
Subject: [PATCH 3/4] minor fix

---
 pandora_console/operation/events/export_csv.php | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pandora_console/operation/events/export_csv.php b/pandora_console/operation/events/export_csv.php
index 84babb2e8a..b20f7b9522 100644
--- a/pandora_console/operation/events/export_csv.php
+++ b/pandora_console/operation/events/export_csv.php
@@ -227,6 +227,10 @@ try {
                             array_walk(
                                 $custom_data_array,
                                 function (&$value, $field) {
+                                    if (is_array($value) === true) {
+                                        $value = '['.implode(',', $value).']';
+                                    }
+
                                     $value = $field.'='.$value;
                                 }
                             );

From 24d144ae2637e309fd54d974ae79e2ac7a7b58da Mon Sep 17 00:00:00 2001
From: "alejandro.campos@artica.es" <alejandro.campos@artica.es>
Date: Wed, 23 Mar 2022 17:23:45 +0100
Subject: [PATCH 4/4] minor fix

---
 pandora_console/operation/events/export_csv.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pandora_console/operation/events/export_csv.php b/pandora_console/operation/events/export_csv.php
index b20f7b9522..4a7b22c944 100644
--- a/pandora_console/operation/events/export_csv.php
+++ b/pandora_console/operation/events/export_csv.php
@@ -228,7 +228,7 @@ try {
                                 $custom_data_array,
                                 function (&$value, $field) {
                                     if (is_array($value) === true) {
-                                        $value = '['.implode(',', $value).']';
+                                        $value = '['.implode($separator, $value).']';
                                     }
 
                                     $value = $field.'='.$value;