From 507344afd6516636ce6fd7838e628bbf6a9964d7 Mon Sep 17 00:00:00 2001 From: Tiago Peralta Date: Wed, 12 Oct 2016 18:57:01 +0100 Subject: [PATCH] Tideways compat (#98) Added Tideways (xhprof fork) support --- external/footer.php | 8 +++--- external/header.php | 56 ++++++++++++++++++++++++++++-------- xhprof_lib/config.sample.php | 2 ++ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/external/footer.php b/external/footer.php index 8c62625b..e725a6e4 100755 --- a/external/footer.php +++ b/external/footer.php @@ -3,19 +3,19 @@ define('XHPROF_LIB_ROOT', dirname(dirname(__FILE__)) . '/xhprof_lib'); } -if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { +if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $isAjax = true; } -if (extension_loaded('xhprof') && $_xhprof['doprofile'] === true) { +if ($_xhprof['ext_name'] && $_xhprof['doprofile'] === true) { $profiler_namespace = $_xhprof['namespace']; // namespace for your application - $xhprof_data = xhprof_disable(); + $xhprof_data = call_user_func($_xhprof['ext_name'].'_disable'); $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace, null, $_xhprof); if ($_xhprof['display'] === true && PHP_SAPI != 'cli' && !isset($isAjax)) { // url to the XHProf UI libraries (change the host name and path) $profiler_url = sprintf($_xhprof['url'].'/index.php?run=%s&source=%s', $run_id, $profiler_namespace); - echo 'Profiler output'; + echo 'Profiler output'; } } diff --git a/external/header.php b/external/header.php index c26ee4f2..01985e88 100644 --- a/external/header.php +++ b/external/header.php @@ -7,6 +7,25 @@ include(dirname(__FILE__) . '/../xhprof_lib/config.php'); +function getExtensionName() +{ + if (extension_loaded('tideways')) + { + return 'tideways'; + }elseif(extension_loaded('xhprof')) { + return 'xhprof'; + } + return false; +} +$_xhprof['ext_name'] = getExtensionName(); +if($_xhprof['ext_name']) +{ + $flagsCpu = constant(strtoupper($_xhprof['ext_name']).'_FLAGS_CPU'); + $flagsMemory = constant(strtoupper($_xhprof['ext_name']).'_FLAGS_MEMORY'); + $envVarName = strtoupper($_xhprof['ext_name']).'_PROFILE'; +} + + //I'm Magic :) class visibilitator { @@ -34,21 +53,30 @@ public static function __callstatic($name, $arguments) // Only users from authorized IP addresses may control Profiling if ($controlIPs === false || in_array($_SERVER['REMOTE_ADDR'], $controlIPs) || PHP_SAPI == 'cli') { - if (isset($_GET['_profile'])) + /* Backwards Compatibility getparam check*/ + if (!isset($_xhprof['getparam'])) + { + $_xhprof['getparam'] = '_profile'; + } + + if (isset($_GET[$_xhprof['getparam']])) { //Give them a cookie to hold status, and redirect back to the same page - setcookie('_profile', $_GET['_profile']); - $newURI = str_replace(array('_profile=1','_profile=0'), '', $_SERVER['REQUEST_URI']); + setcookie('_profile', $_GET[$_xhprof['getparam']]); + $newURI = str_replace(array($_xhprof['getparam'].'=1',$_xhprof['getparam'].'=0'), '', $_SERVER['REQUEST_URI']); header("Location: $newURI"); exit; } - - if (isset($_COOKIE['_profile']) && $_COOKIE['_profile'] || PHP_SAPI == 'cli' && ((isset($_SERVER['XHPROF_PROFILE']) && $_SERVER['XHPROF_PROFILE']) || (isset($_ENV['XHPROF_PROFILE']) && $_ENV['XHPROF_PROFILE']))) + + if (isset($_COOKIE['_profile']) && $_COOKIE['_profile'] + || PHP_SAPI == 'cli' && ( (isset($_SERVER[$envVarName]) && $_SERVER[$envVarName]) + || (isset($_ENV[$envVarName]) && $_ENV[$envVarName]))) { $_xhprof['display'] = true; $_xhprof['doprofile'] = true; $_xhprof['type'] = 1; } + unset($envVarName); } @@ -112,20 +140,24 @@ public static function __callstatic($name, $arguments) unset($domain); //Display warning if extension not available -if (extension_loaded('xhprof') && $_xhprof['doprofile'] === true) { +if ($_xhprof['ext_name'] && $_xhprof['doprofile'] === true) { include_once dirname(__FILE__) . '/../xhprof_lib/utils/xhprof_lib.php'; include_once dirname(__FILE__) . '/../xhprof_lib/utils/xhprof_runs.php'; - if (isset($ignoredFunctions) && is_array($ignoredFunctions) && !empty($ignoredFunctions)) { - xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY, array('ignored_functions' => $ignoredFunctions)); + if (isset($ignoredFunctions) && is_array($ignoredFunctions) && !empty($ignoredFunctions)) { + call_user_func($_xhprof['ext_name'].'_enable', $flagsCpu + $flagsMemory, array('ignored_functions' => $ignoredFunctions)); } else { - xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); + call_user_func($_xhprof['ext_name'].'_enable', $flagsCpu + $flagsMemory); } -}elseif(!extension_loaded('xhprof') && $_xhprof['display'] === true) + unset($flagsCpu); + unset($flagsMemory); + +}elseif(false === $_xhprof['ext_name'] && $_xhprof['display'] === true) { - $message = 'Warning! Unable to profile run, xhprof extension not loaded'; + $message = 'Warning! Unable to profile run, tideways or xhprof extension not loaded'; trigger_error($message, E_USER_WARNING); } - +unset($flagsCpu); + unset($flagsMemory); function xhprof_shutdown_function() { global $_xhprof; require dirname(__FILE__).'/footer.php'; diff --git a/xhprof_lib/config.sample.php b/xhprof_lib/config.sample.php index a20c5396..a2bc84ba 100644 --- a/xhprof_lib/config.sample.php +++ b/xhprof_lib/config.sample.php @@ -11,6 +11,8 @@ $_xhprof['servername'] = 'myserver'; $_xhprof['namespace'] = 'myapp'; $_xhprof['url'] = 'http://url/to/xhprof/xhprof_html'; +$_xhprof['getparam'] = "_profile"; + /* * MySQL/MySQLi/PDO ONLY * Switch to JSON for better performance and support for larger profiler data sets.