From 0ffd5eca37fd8c8d3ada9fbe7689976d3498e4bf Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Tue, 17 Apr 2018 19:36:52 +0200 Subject: [PATCH 1/8] added variable config file location in header.php (via constant / env variable) --- external/header.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/external/header.php b/external/header.php index 01985e88..adfdad52 100644 --- a/external/header.php +++ b/external/header.php @@ -5,14 +5,31 @@ $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; } -include(dirname(__FILE__) . '/../xhprof_lib/config.php'); +// Search for config in different places - adding constant and env +if(defined('XHPROF_CONFIG') && is_file(XHPROF_CONFIG)) { + include XHPROF_CONFIG; +} +else { + $ENV_XHPROF_CONFIG = getenv('ENV_XHPROF_CONFIG'); + if(!empty($ENV_XHPROF_CONFIG) && is_file($ENV_XHPROF_CONFIG)) { + include $ENV_XHPROF_CONFIG; + } + else { + include(dirname(__FILE__) . '/../xhprof_lib/config.php'); + } +} function getExtensionName() { + if (extension_loaded('tideways_xhprof')) + { + return 'tideways_xhprof'; + } if (extension_loaded('tideways')) { return 'tideways'; - }elseif(extension_loaded('xhprof')) { + } + elseif(extension_loaded('xhprof')) { return 'xhprof'; } return false; From 1bb665acc91050134096272bec0b2f1f55e8befb Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Tue, 17 Apr 2018 19:38:22 +0200 Subject: [PATCH 2/8] added pre tag to error display for easier debugging --- xhprof_lib/utils/xhprof_runs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xhprof_lib/utils/xhprof_runs.php b/xhprof_lib/utils/xhprof_runs.php index 7bfbf17e..6b798633 100644 --- a/xhprof_lib/utils/xhprof_runs.php +++ b/xhprof_lib/utils/xhprof_runs.php @@ -464,7 +464,7 @@ public function save_run($xhprof_data, $type, $run_id = null, $xhprof_details = global $_xhprof; if ($_xhprof['display'] === true) { - echo "Failed to insert: $query
\n"; + echo "Failed to insert:
$query

\n"; } return -1; } From de5860f513efff394bb557064e34aea158047386 Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Tue, 17 Apr 2018 19:38:42 +0200 Subject: [PATCH 3/8] added a preliminary composer.json --- composer.json | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..dd648f9e --- /dev/null +++ b/composer.json @@ -0,0 +1,32 @@ +{ + "name": "xhprof/xhgui", + "description": "XHGUI is a GUI for the XHProf PHP extension, using a database backend, and pretty graphs to make it easy to use and interpret.", + "keywords": ["php", "profiling", "xhprof", "gui"], + "authors": [ + { + "name": "Changhao Jiang", + "role": "Creator" + }, + { + "name": "Kannan Muthukkaruppan", + "role": "Creator" + }, + { + "name": "Venkat Venkataramani", + "role": "Creator" + }, + { + "name": "George Cabrera", + "role": "Additional Contributor - UI Enhancements" + }, + { + "name": "Paul Saab", + "role": "Additional Contributor - FreeBSD port" + }, + { + "name": "Lorenzo Perone", + "role": "Additional Contributor " + } + ], + "license": "Apache" +} From 63e2e96206808458f4735254e2fdeac11cfb8acf Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Tue, 17 Apr 2018 20:47:10 +0200 Subject: [PATCH 4/8] update config searching logic to find apache environment variables too (SetEnv) --- external/header.php | 9 ++++++--- xhprof_html/index.php | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/external/header.php b/external/header.php index adfdad52..3027e178 100644 --- a/external/header.php +++ b/external/header.php @@ -7,15 +7,18 @@ // Search for config in different places - adding constant and env if(defined('XHPROF_CONFIG') && is_file(XHPROF_CONFIG)) { - include XHPROF_CONFIG; + require_once XHPROF_CONFIG; } else { $ENV_XHPROF_CONFIG = getenv('ENV_XHPROF_CONFIG'); if(!empty($ENV_XHPROF_CONFIG) && is_file($ENV_XHPROF_CONFIG)) { - include $ENV_XHPROF_CONFIG; + require_once $ENV_XHPROF_CONFIG; + } + elseif(!empty($_SERVER['ENV_XHPROF_CONFIG']) && is_file($_SERVER['ENV_XHPROF_CONFIG'])) { + require_once $_SERVER['ENV_XHPROF_CONFIG']; } else { - include(dirname(__FILE__) . '/../xhprof_lib/config.php'); + require_once (XHPROF_LIB_ROOT . "/config.php"); } } diff --git a/xhprof_html/index.php b/xhprof_html/index.php index d815e07a..a931b945 100755 --- a/xhprof_html/index.php +++ b/xhprof_html/index.php @@ -2,7 +2,22 @@ if (!defined('XHPROF_LIB_ROOT')) { define('XHPROF_LIB_ROOT', dirname(dirname(__FILE__)) . '/xhprof_lib'); } -require_once (XHPROF_LIB_ROOT . "/config.php"); + +// Search for config in different places - adding constant and env +if(defined('XHPROF_CONFIG') && is_file(XHPROF_CONFIG)) { + require_once XHPROF_CONFIG; +} +else { + $XHPROF_CONFIG = getenv('XHPROF_CONFIG'); + if ( ! empty($XHPROF_CONFIG) && is_file($XHPROF_CONFIG)) { + require_once $XHPROF_CONFIG; + } elseif ( ! empty($_SERVER['XHPROF_CONFIG']) && is_file($_SERVER['XHPROF_CONFIG'])) { + require_once $_SERVER['XHPROF_CONFIG']; + } else { + require_once(XHPROF_LIB_ROOT . "/config.php"); + } +} + include_once XHPROF_LIB_ROOT . '/display/xhprof.php'; include (XHPROF_LIB_ROOT . "/utils/common.php"); From b16bbb4325501b48857715a5f62e4860dd869043 Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Tue, 17 Apr 2018 20:48:09 +0200 Subject: [PATCH 5/8] fix urls for situations in which xhprof is symlinked in the document root and the base path is wrong --- xhprof_lib/display/xhprof.php | 4 ++-- xhprof_lib/templates/profTable.phtml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xhprof_lib/display/xhprof.php b/xhprof_lib/display/xhprof.php index 05220167..67f37165 100644 --- a/xhprof_lib/display/xhprof.php +++ b/xhprof_lib/display/xhprof.php @@ -42,8 +42,7 @@ * Our coding convention disallows relative paths in hrefs. * Get the base URL path from the SCRIPT_NAME. */ -$base_path = rtrim(dirname($_SERVER['SCRIPT_NAME']), "/"); - +$base_path = $_xhprof['url']; /** * Generate references to required stylesheets & javascript. @@ -791,6 +790,7 @@ function print_flat_data($url_params, $title, $flat_data, $sort, $run1, $run2, $ global $sortable_columns; global $vwbar; global $base_path; + global $_xhprof; $size = count($flat_data); if (!$limit) { // no limit diff --git a/xhprof_lib/templates/profTable.phtml b/xhprof_lib/templates/profTable.phtml index f09a1ac6..e737d0fa 100644 --- a/xhprof_lib/templates/profTable.phtml +++ b/xhprof_lib/templates/profTable.phtml @@ -29,7 +29,7 @@   - + From 04ddf70cd0b050bd4b6a685b189bade66e9988ff Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Thu, 19 Apr 2018 14:38:38 +0200 Subject: [PATCH 6/8] header.php refactoring: remove both & and ? from URI before redirecting, add a _display parameter to toggle displaying of profiler link --- external/header.php | 108 +++++++++++++++++++++++------------ xhprof_lib/config.sample.php | 1 + 2 files changed, 74 insertions(+), 35 deletions(-) diff --git a/external/header.php b/external/header.php index 3027e178..9f5371a7 100644 --- a/external/header.php +++ b/external/header.php @@ -10,15 +10,13 @@ require_once XHPROF_CONFIG; } else { - $ENV_XHPROF_CONFIG = getenv('ENV_XHPROF_CONFIG'); - if(!empty($ENV_XHPROF_CONFIG) && is_file($ENV_XHPROF_CONFIG)) { - require_once $ENV_XHPROF_CONFIG; - } - elseif(!empty($_SERVER['ENV_XHPROF_CONFIG']) && is_file($_SERVER['ENV_XHPROF_CONFIG'])) { - require_once $_SERVER['ENV_XHPROF_CONFIG']; - } - else { - require_once (XHPROF_LIB_ROOT . "/config.php"); + $XHPROF_CONFIG = getenv('XHPROF_CONFIG'); + if ( ! empty($XHPROF_CONFIG) && is_file($XHPROF_CONFIG)) { + require_once $XHPROF_CONFIG; + } elseif ( ! empty($_SERVER['XHPROF_CONFIG']) && is_file($_SERVER['XHPROF_CONFIG'])) { + require_once $_SERVER['XHPROF_CONFIG']; + } else { + require_once(XHPROF_LIB_ROOT . "/config.php"); } } @@ -71,32 +69,72 @@ 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') -{ - /* 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[$_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[$envVarName]) && $_SERVER[$envVarName]) - || (isset($_ENV[$envVarName]) && $_ENV[$envVarName]))) - { - $_xhprof['display'] = true; - $_xhprof['doprofile'] = true; - $_xhprof['type'] = 1; - } - unset($envVarName); +if ($controlIPs === false || in_array($_SERVER['REMOTE_ADDR'], $controlIPs) || PHP_SAPI == 'cli') { + + /* Backwards Compatibility getparam check*/ + if ( ! isset($_xhprof['getparam'])) { + $_xhprof['getparam'] = '_profile'; + } + + if ( ! isset($_xhprof['displayparam'])) { + $_xhprof['displayparam'] = '_display'; + } + + $handleRuntimeToggle = function($key, $uri, $cookieName = null) { + if (isset($_GET[ $key ])) { + if (null === $cookieName) { + $cookieName = $key; + } + // Give them a cookie to hold status, and redirect back to the same page + if ($_GET[ $key ] === "1") { + setcookie($cookieName, $_GET[ $key ]); + } elseif ($_GET[ $key ] === "0") { + setcookie($cookieName, null, - 1); + unset($_COOKIE[ $cookieName ]); + } + + $cleanURI = str_replace(array( + '&' . $key . '=1', + '&' . $key . '=0', + '?' . $key . '=1', + '?' . $key . '=0', + ), '', $uri); + + return [ true, $cleanURI ]; + } else { + return [ false, $uri ]; + } + }; + + $toggleParams = [ $_xhprof['getparam'], $_xhprof['displayparam'] ]; + + $currentURI = $_SERVER['REQUEST_URI']; + $changes = false; + foreach($toggleParams as $toggleParam) { + list($changed, $currentURI) = $handleRuntimeToggle($toggleParam, $currentURI); + $changes = ($changes or $changed); + } + + if (isset($_COOKIE[ $_xhprof['getparam'] ]) && $_COOKIE[ $_xhprof['displayparam'] ] + || PHP_SAPI == 'cli' && ((isset($_SERVER[ $envVarName ]) && $_SERVER[ $envVarName ]) + || (isset($_ENV[ $envVarName ]) && $_ENV[ $envVarName ]))) { + $_xhprof['doprofile'] = true; + $_xhprof['type'] = 1; + } + + if (isset($_COOKIE[ $_xhprof['displayparam'] ]) && $_COOKIE[ $_xhprof['displayparam'] ] + || PHP_SAPI == 'cli' && ((isset($_SERVER[ $envVarName ]) && $_SERVER[ $envVarName ]) + || (isset($_ENV[ $envVarName ]) && $_ENV[ $envVarName ]))) { + $_xhprof['display'] = true; + } + + if (true === $changes) { + header('HTTP/1.1 302 Found'); + header("Location: $currentURI"); + die("Redirecting you to " . $currentURI); + } + + unset($envVarName); } diff --git a/xhprof_lib/config.sample.php b/xhprof_lib/config.sample.php index a2bc84ba..33d9b7c0 100644 --- a/xhprof_lib/config.sample.php +++ b/xhprof_lib/config.sample.php @@ -12,6 +12,7 @@ $_xhprof['namespace'] = 'myapp'; $_xhprof['url'] = 'http://url/to/xhprof/xhprof_html'; $_xhprof['getparam'] = "_profile"; +$_xhprof['displayparam'] = "_display"; /* * MySQL/MySQLi/PDO ONLY From 6cf0d4f053577ae9a85417f5d2871cf187a7aaa5 Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Thu, 19 Apr 2018 14:39:47 +0200 Subject: [PATCH 7/8] change example 'servername' in sample config. the database column is a CHAR(3), so using the old example broke the queries --- xhprof_lib/config.sample.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xhprof_lib/config.sample.php b/xhprof_lib/config.sample.php index 33d9b7c0..413e6aaf 100644 --- a/xhprof_lib/config.sample.php +++ b/xhprof_lib/config.sample.php @@ -8,7 +8,7 @@ $_xhprof['dbpass'] = 'password'; $_xhprof['dbname'] = 'xhprof'; $_xhprof['dbadapter'] = 'Pdo'; -$_xhprof['servername'] = 'myserver'; +$_xhprof['servername'] = 's01'; $_xhprof['namespace'] = 'myapp'; $_xhprof['url'] = 'http://url/to/xhprof/xhprof_html'; $_xhprof['getparam'] = "_profile"; From 13654e3fd8d397482a33b32a91c07bc469cd1122 Mon Sep 17 00:00:00 2001 From: Lorenzo Perone Date: Thu, 19 Apr 2018 15:19:08 +0200 Subject: [PATCH 8/8] update README to reflect some changes --- README.markdown | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/README.markdown b/README.markdown index d48a7cf4..17d172de 100644 --- a/README.markdown +++ b/README.markdown @@ -53,22 +53,28 @@ Installation * Install your favourite mix of PHP and web server * Install MySQL server -* Clone the project to some folder +* Clone the project to some folder of your choice. * Map the sub folder `xhprof_html` to be accessible over HTTP -* Move `xhprof_lib/config.sample.php` to `xhprof_lib/config.php` + * You can do it with an Alias directive or (if symlinks are enabled) by just symlinking `xhprof_html` to any location within your document root. +* Copy `xhprof_lib/config.sample.php` to `xhprof_lib/config.php` + * Alternatively, you can copy the config.php to any place you like, and then specify the location of the config file in your ENV, in a PHP constant, or via Apache / Nginx Env variable (see below) * Edit `xhprof_lib/config.php` - * Update the SQL server configuration - * Update the URL of the service (should point to `xhprof_html` over HTTP) - * Update the `dot_binary` configuration - otherwise no call graphs! - * Update the `controlIPs` variable to enable access. + * Update the SQL server configuration + * Update the URL of the service (should point to `xhprof_html` over HTTP) + * Update the `dot_binary` configuration - otherwise no call graphs! + * Update the `controlIPs` variable to enable access. * For a development machine you can set this to `false` to disable IP checks. * Import the DB schema (it is just 1 table) * See the SQL at [xhprof_runs.php](https://github.com/toomasr/xhprof/blob/master/xhprof_lib/utils/xhprof_runs.php#L109) * Add a PHP configuration to enable the profiling - * If using Apache you can edit your virtual host configuration - * Add `php_admin_value auto_prepend_file "/path/to/xhprof/external/header.php"` + * If using Apache you can edit your virtual host configuration + * Add `php_admin_value auto_prepend_file "/path/to/xhprof/external/header.php"` + * (optional) Add `SetEnv XHPROF_CONFIG /absolute/path/to/config.php` to your apache config to set location of config file for that host + * (optional) If you include the header.php manually, you can define the location of the config file via define('XHPROF_CONFIG','/absolute/path/to/config.php'); + * (optional) Within a shell script, you can export `XHPROF_CONFIG=/absolute/path/to/config.php` to specify location of config file * Visit http://your-server/xhprof/xhprof_html/ and be amazed! - * To get profiler information showing up there visit your page with a `GET` variable `_profile=1`. + * To get profiler information showing up there visit your page with a `GET` variable `_profile=1`. + This will enable it (via cookie) until you disable it by adding the parameter `_profile=0` to any url (or removing the _profile cookie manually) * For example `http://localhost/?_profile=1` We Are Working On