From 415a54fb55d423b3adaaefe9c8546dddc5220eaf Mon Sep 17 00:00:00 2001
From: Daniel Barbero <daniel.barbero@artica.es>
Date: Thu, 12 Dec 2019 14:01:11 +0100
Subject: [PATCH] base64 images pdf

---
 .../include/functions_reporting.php           | 200 +++++++++++-------
 .../include/graphs/functions_flot.php         |  43 ++--
 pandora_console/include/web2image.js          |   3 +-
 3 files changed, 147 insertions(+), 99 deletions(-)

diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index 5a65fc820f..814da42d03 100755
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -3560,16 +3560,17 @@ function agents_get_network_interfaces_array(
                 $width = null;
 
                 $params = [
-                    'period'     => $content['period'],
-                    'width'      => $width,
-                    'unit_name'  => array_fill(0, count($interface['traffic']), __('bytes/s')),
-                    'date'       => $report['datetime'],
-                    'only_image' => $pdf,
-                    'homeurl'    => $config['homeurl'],
-                    'fullscale'  => $fullscale,
-                    'server_id'  => $id_meta,
-                    'height'     => $config['graph_image_height'],
-                    'landscape'  => $content['landscape'],
+                    'period'             => $content['period'],
+                    'width'              => $width,
+                    'unit_name'          => array_fill(0, count($interface['traffic']), __('bytes/s')),
+                    'date'               => $report['datetime'],
+                    'only_image'         => $pdf,
+                    'homeurl'            => $config['homeurl'],
+                    'fullscale'          => $fullscale,
+                    'server_id'          => $id_meta,
+                    'height'             => $config['graph_image_height'],
+                    'landscape'          => $content['landscape'],
+                    'return_img_base_64' => true,
                 ];
 
                 $params_combined = [
@@ -3581,11 +3582,21 @@ function agents_get_network_interfaces_array(
                     case 'dinamic':
                     case 'static':
                         if (!empty($interface['traffic'])) {
-                            $row_interface['chart'] = graphic_combined_module(
-                                array_values($interface['traffic']),
-                                $params,
-                                $params_combined
-                            );
+                            if ($pdf === false) {
+                                $row_interface['chart'] = graphic_combined_module(
+                                    array_values($interface['traffic']),
+                                    $params,
+                                    $params_combined
+                                );
+                            } else {
+                                $row_interface['chart'] = '<img src="data:image/jpg;base64,';
+                                $row_interface['chart'] .= graphic_combined_module(
+                                    array_values($interface['traffic']),
+                                    $params,
+                                    $params_combined
+                                );
+                                $row_interface['chart'] .= '" />';
+                            }
                         }
                     break;
 
@@ -4562,28 +4573,38 @@ function reporting_projection_graph(
         case 'dinamic':
         case 'static':
             $params = [
-                'period'     => $content['period'],
-                'width'      => $width,
-                'date'       => $report['datetime'],
-                'unit'       => '',
-                'only_image' => $pdf,
-                'homeurl'    => ui_get_full_url(false, false, false, false).'/',
-                'ttl'        => $ttl,
-                'server_id'  => $id_meta,
-                'height'     => $config['graph_image_height'],
-                'landscape'  => $content['landscape'],
+                'period'             => $content['period'],
+                'width'              => $width,
+                'date'               => $report['datetime'],
+                'unit'               => '',
+                'only_image'         => $pdf,
+                'homeurl'            => ui_get_full_url(false, false, false, false).'/',
+                'ttl'                => $ttl,
+                'server_id'          => $id_meta,
+                'height'             => $config['graph_image_height'],
+                'landscape'          => $content['landscape'],
+                'return_img_base_64' => true,
             ];
 
             $params_combined = [
                 'projection' => $content['top_n_value'],
             ];
 
-            $return['chart'] = graphic_combined_module(
-                [$content['id_agent_module']],
-                $params,
-                $params_combined
-            );
-
+            if ($pdf === true) {
+                $return['chart'] = '<img src="data:image/jpg;base64,';
+                $return['chart'] .= graphic_combined_module(
+                    [$content['id_agent_module']],
+                    $params,
+                    $params_combined
+                );
+                $return['chart'] .= '" />';
+            } else {
+                $return['chart'] = graphic_combined_module(
+                    [$content['id_agent_module']],
+                    $params,
+                    $params_combined
+                );
+            }
         break;
 
         case 'data':
@@ -4816,20 +4837,21 @@ function reporting_value($report, $content, $type, $pdf=false)
     }
 
     $params = [
-        'agent_module_id' => $content['id_agent_module'],
-        'period'          => $content['period'],
-        'width'           => '600px',
-        'pure'            => false,
-        'date'            => $report['datetime'],
-        'only_image'      => $only_image,
-        'homeurl'         => ui_get_full_url(false, false, false, false),
-        'ttl'             => 1,
-        'type_graph'      => $config['type_module_charts'],
-        'time_interval'   => $content['lapse'],
-        'server_id'       => $id_meta,
-        'height'          => $config['graph_image_height'],
-        'fullscale'       => true,
-        'landscape'       => $content['landscape'],
+        'agent_module_id'    => $content['id_agent_module'],
+        'period'             => $content['period'],
+        'width'              => '600px',
+        'pure'               => false,
+        'date'               => $report['datetime'],
+        'only_image'         => $only_image,
+        'homeurl'            => ui_get_full_url(false, false, false, false),
+        'ttl'                => 1,
+        'type_graph'         => $config['type_module_charts'],
+        'time_interval'      => $content['lapse'],
+        'server_id'          => $id_meta,
+        'height'             => $config['graph_image_height'],
+        'fullscale'          => true,
+        'landscape'          => $content['landscape'],
+        'return_img_base_64' => true,
     ];
 
     switch ($type) {
@@ -4895,7 +4917,12 @@ function reporting_value($report, $content, $type, $pdf=false)
                 ];
 
                 if ($content['visual_format'] != 1) {
-                    $graph = grafico_modulo_sparse($params);
+                    if ($only_image === false) {
+                        $graph = grafico_modulo_sparse($params);
+                    } else {
+                        $graph = '<img src="data:image/jpg;base64,'.grafico_modulo_sparse($params).'" />';
+                    }
+
                     $return['data'][] = ['value' => $graph];
                 }
 
@@ -7597,17 +7624,18 @@ function reporting_custom_graph(
         case 'dinamic':
         case 'static':
             $params = [
-                'period'     => $content['period'],
-                'width'      => $width,
-                'date'       => $report['datetime'],
-                'only_image' => $pdf,
-                'homeurl'    => ui_get_full_url(false, false, false, false),
-                'ttl'        => $ttl,
-                'percentil'  => $graphs[0]['percentil'],
-                'fullscale'  => $graphs[0]['fullscale'],
-                'server_id'  => $id_meta,
-                'height'     => $config['graph_image_height'],
-                'landscape'  => $content['landscape'],
+                'period'             => $content['period'],
+                'width'              => $width,
+                'date'               => $report['datetime'],
+                'only_image'         => $pdf,
+                'homeurl'            => ui_get_full_url(false, false, false, false),
+                'ttl'                => $ttl,
+                'percentil'          => $graphs[0]['percentil'],
+                'fullscale'          => $graphs[0]['fullscale'],
+                'server_id'          => $id_meta,
+                'height'             => $config['graph_image_height'],
+                'landscape'          => $content['landscape'],
+                'return_img_base_64' => true,
             ];
 
             $params_combined = [
@@ -7620,12 +7648,21 @@ function reporting_custom_graph(
                 'labels'         => $content['style']['label'],
             ];
 
-            $return['chart'] = graphic_combined_module(
-                $modules,
-                $params,
-                $params_combined
-            );
-
+            if ($pdf === true) {
+                $return['chart'] = '<img src="data:image/jpg;base64,';
+                $return['chart'] .= graphic_combined_module(
+                    $modules,
+                    $params,
+                    $params_combined
+                );
+                $return['chart'] .= '" />';
+            } else {
+                $return['chart'] = graphic_combined_module(
+                    $modules,
+                    $params,
+                    $params_combined
+                );
+            }
         break;
 
         case 'data':
@@ -7769,29 +7806,34 @@ function reporting_simple_graph(
             }
 
             $params = [
-                'agent_module_id' => $content['id_agent_module'],
-                'period'          => $content['period'],
-                'title'           => $label,
-                'pure'            => false,
-                'date'            => $report['datetime'],
-                'only_image'      => $only_image,
-                'homeurl'         => ui_get_full_url(
+                'agent_module_id'    => $content['id_agent_module'],
+                'period'             => $content['period'],
+                'title'              => $label,
+                'pure'               => false,
+                'date'               => $report['datetime'],
+                'only_image'         => $only_image,
+                'homeurl'            => ui_get_full_url(
                     false,
                     false,
                     false,
                     false
                 ),
-                'ttl'             => $ttl,
-                'compare'         => $time_compare_overlapped,
-                'show_unknown'    => true,
-                'percentil'       => ($content['style']['percentil'] == 1) ? $config['percentil'] : null,
-                'fullscale'       => $fullscale,
-                'server_id'       => $id_meta,
-                'height'          => $config['graph_image_height'],
-                'landscape'       => $content['landscape'],
+                'ttl'                => $ttl,
+                'compare'            => $time_compare_overlapped,
+                'show_unknown'       => true,
+                'percentil'          => ($content['style']['percentil'] == 1) ? $config['percentil'] : null,
+                'fullscale'          => $fullscale,
+                'server_id'          => $id_meta,
+                'height'             => $config['graph_image_height'],
+                'landscape'          => $content['landscape'],
+                'return_img_base_64' => true,
             ];
 
-            $return['chart'] = grafico_modulo_sparse($params);
+            if ($only_image === false) {
+                $return['chart'] = grafico_modulo_sparse($params);
+            } else {
+                $return['chart'] = '<img src="data:image/jpg;base64,'.grafico_modulo_sparse($params).'" />';
+            }
         break;
 
         case 'data':
diff --git a/pandora_console/include/graphs/functions_flot.php b/pandora_console/include/graphs/functions_flot.php
index eb5596cec4..69fee8a89a 100644
--- a/pandora_console/include/graphs/functions_flot.php
+++ b/pandora_console/include/graphs/functions_flot.php
@@ -760,27 +760,32 @@ function flot_slicesbar_graph(
 
     if ($ttl == 2) {
         $params = [
-            'graph_data'       => $graph_data,
-            'period'           => $period,
-            'width'            => $width,
-            'height'           => $height,
-            'legend'           => $legend,
-            'colors'           => $colors,
-            'fontpath'         => $fontpath,
-            'round_corner'     => $round_corner,
-            'homeurl'          => $homeurl,
-            'watermark'        => $watermark,
-            'adapt_key'        => $adapt_key,
-            'stat_win'         => $stat_win,
-            'id_agent'         => $id_agent,
-            'full_legend_date' => $full_legend_date,
-            'not_interactive'  => $not_interactive,
-            'ttl'              => 1,
-            'widgets'          => $widgets,
-            'show'             => $show,
+            'graph_data'         => $graph_data,
+            'period'             => $period,
+            'width'              => $width,
+            'height'             => $height,
+            'legend'             => $legend,
+            'colors'             => $colors,
+            'fontpath'           => $fontpath,
+            'round_corner'       => $round_corner,
+            'homeurl'            => $homeurl,
+            'watermark'          => $watermark,
+            'adapt_key'          => $adapt_key,
+            'stat_win'           => $stat_win,
+            'id_agent'           => $id_agent,
+            'full_legend_date'   => $full_legend_date,
+            'not_interactive'    => $not_interactive,
+            'ttl'                => 1,
+            'widgets'            => $widgets,
+            'show'               => $show,
+            'return_img_base_64' => true,
         ];
 
-        return generator_chart_to_pdf('slicebar', $params);
+        $graph = '<img src="data:image/jpg;base64,';
+        $graph .= generator_chart_to_pdf('slicebar', $params);
+        $graph .= '" />';
+
+        return $graph;
     }
 
     // Get a unique identifier to graph
diff --git a/pandora_console/include/web2image.js b/pandora_console/include/web2image.js
index 8e2887aa26..f637071b7e 100644
--- a/pandora_console/include/web2image.js
+++ b/pandora_console/include/web2image.js
@@ -94,7 +94,8 @@ page.onCallback = function() {
   if (!base_64) {
     page.render(output_filename, { format: "png" });
   } else {
-    var base64 = page.renderBase64("png");
+    page.settings.loadImages = false;
+    var base64 = page.renderBase64("jpg");
     // do not remove this console.output
     console.log(base64);
   }