From 06830c9f692fa497230f316d5a2948b0369c9854 Mon Sep 17 00:00:00 2001
From: fermin831 <fermin.hernandez@artica.es>
Date: Tue, 13 Nov 2018 12:55:25 +0100
Subject: [PATCH] Added profiller to docker

---
 pandora_console/ajax.php              | 13 ++++++++++
 pandora_console/include/functions.php | 34 +++++++++++++++++++++++++++
 pandora_console/index.php             | 16 +++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/pandora_console/ajax.php b/pandora_console/ajax.php
index 6947bfcc3a..83ee051458 100644
--- a/pandora_console/ajax.php
+++ b/pandora_console/ajax.php
@@ -14,6 +14,15 @@
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 // GNU General Public License for more details.
 
+// Enable profiler for testing
+if (!defined("__PAN_XHPROF__")) define ("__PAN_XHPROF__", 0);
+
+if (__PAN_XHPROF__ === 1) {
+	if (function_exists('tideways_xhprof_enable')) {
+		tideways_xhprof_enable();
+	}
+}
+
 if ((! file_exists("include/config.php")) || (! is_readable("include/config.php"))) {
 	exit;
 }
@@ -90,4 +99,8 @@ if (file_exists ($page)) {
 else {
 	echo '<br /><b class="error">Sorry! I can\'t find the page '.$page.'!</b>';
 }
+
+if (__PAN_XHPROF__ === 1) {
+	pandora_xhprof_display_result("ajax", "console");
+}
 ?>
diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php
index ff2ce52deb..ae6ac202bd 100644
--- a/pandora_console/include/functions.php
+++ b/pandora_console/include/functions.php
@@ -3391,5 +3391,39 @@ function validate_csrf_code() {
 
 function generate_hash_to_api(){
 	hash('sha256', db_get_value ('value', 'tupdate_settings', '`key`', 'customer_key'));
+}
+
+/**
+ * Disable the profiller and display de result
+ *
+ * @param string Key to identify the profiler run.
+ * @param string Way to display the result
+ * 		"link" (default): Click into word "Performance" to display the profilling info.
+ * 		"console": Display with a message in pandora_console.log.
+ */
+function pandora_xhprof_display_result($key = "", $method = "link") {
+	// Check if function exists
+	if (!function_exists('tideways_xhprof_disable')) {
+		error_log("Cannot find tideways_xhprof_disable function");
+		return;
 	}
+
+	$run_id = uniqid();
+	$data = tideways_xhprof_disable();
+	$source = "pandora_$key";
+	file_put_contents(
+		sys_get_temp_dir() . "/" . $run_id . ".$source.xhprof",
+		serialize($data)
+	);
+	$new_url = "http://{$_SERVER['HTTP_HOST']}/profiler/index.php?run={$run_id}&source={$source}";
+	switch($method) {
+		case "console":
+			error_log("'{$new_url}'");
+		case "link":
+		default:
+			echo "<a href='{$new_url}' target='_new'>Performance</a>\n";
+			break;
+
+	}
+}
 ?>
diff --git a/pandora_console/index.php b/pandora_console/index.php
index 9111aec168..8669f39d2d 100755
--- a/pandora_console/index.php
+++ b/pandora_console/index.php
@@ -14,6 +14,17 @@
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 // GNU General Public License for more details.
 
+// Enable profiler for testing
+if (!defined("__PAN_XHPROF__")) define ("__PAN_XHPROF__", 0);
+
+if (__PAN_XHPROF__ === 1) {
+	if (function_exists('tideways_xhprof_enable')) {
+		tideways_xhprof_enable();
+	} else {
+		error_log("Cannot find tideways_xhprof_enable function");
+	}
+}
+
 //Set character encoding to UTF-8 - fixes a lot of multibyte character headaches
 if (function_exists ('mb_internal_encoding')) {
 	mb_internal_encoding ("UTF-8");
@@ -1266,3 +1277,8 @@ require('include/php_to_js_values.php');
 		*/
 	}
 </script>
+<?php
+if (__PAN_XHPROF__ === 1) {
+	pandora_xhprof_display_result("node_index");
+}
+?>
\ No newline at end of file