Skip to content

Commit 92b89e7

Browse files
committed
header.php refactoring: remove both & and ? from URI before redirecting, add a _display parameter to toggle displaying of profiler link
1 parent 5f79110 commit 92b89e7

File tree

2 files changed

+97
-56
lines changed

2 files changed

+97
-56
lines changed

external/header.php

Lines changed: 96 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
// Search for config in different places - adding constant and env
99
if(defined('XHPROF_CONFIG') && is_file(XHPROF_CONFIG)) {
10-
require_once XHPROF_CONFIG;
10+
require_once XHPROF_CONFIG;
1111
}
1212
else {
13-
$ENV_XHPROF_CONFIG = getenv('ENV_XHPROF_CONFIG');
14-
if(!empty($ENV_XHPROF_CONFIG) && is_file($ENV_XHPROF_CONFIG)) {
15-
require_once $ENV_XHPROF_CONFIG;
16-
}
17-
elseif(!empty($_SERVER['ENV_XHPROF_CONFIG']) && is_file($_SERVER['ENV_XHPROF_CONFIG'])) {
18-
require_once $_SERVER['ENV_XHPROF_CONFIG'];
19-
}
20-
else {
21-
require_once (XHPROF_LIB_ROOT . "/config.php");
22-
}
13+
$ENV_XHPROF_CONFIG = getenv('ENV_XHPROF_CONFIG');
14+
if(!empty($ENV_XHPROF_CONFIG) && is_file($ENV_XHPROF_CONFIG)) {
15+
require_once $ENV_XHPROF_CONFIG;
16+
}
17+
elseif(!empty($_SERVER['ENV_XHPROF_CONFIG']) && is_file($_SERVER['ENV_XHPROF_CONFIG'])) {
18+
require_once $_SERVER['ENV_XHPROF_CONFIG'];
19+
}
20+
else {
21+
require_once (XHPROF_LIB_ROOT . "/config.php");
22+
}
2323
}
2424

2525
function getExtensionName()
@@ -49,54 +49,94 @@ function getExtensionName()
4949
//I'm Magic :)
5050
class visibilitator
5151
{
52-
public static function __callstatic($name, $arguments)
53-
{
54-
$func_name = array_shift($arguments);
55-
//var_dump($name);
56-
//var_dump("arguments" ,$arguments);
57-
//var_dump($func_name);
58-
if (is_array($func_name))
59-
{
60-
list($a, $b) = $func_name;
61-
if (count($arguments) == 0)
62-
{
63-
$arguments = $arguments[0];
64-
}
65-
return call_user_func_array(array($a, $b), $arguments);
66-
//echo "array call -> $b ($arguments)";
67-
}else {
68-
call_user_func_array($func_name, $arguments);
69-
}
70-
}
52+
public static function __callstatic($name, $arguments)
53+
{
54+
$func_name = array_shift($arguments);
55+
//var_dump($name);
56+
//var_dump("arguments" ,$arguments);
57+
//var_dump($func_name);
58+
if (is_array($func_name))
59+
{
60+
list($a, $b) = $func_name;
61+
if (count($arguments) == 0)
62+
{
63+
$arguments = $arguments[0];
64+
}
65+
return call_user_func_array(array($a, $b), $arguments);
66+
//echo "array call -> $b ($arguments)";
67+
}else {
68+
call_user_func_array($func_name, $arguments);
69+
}
70+
}
7171
}
7272

7373
// Only users from authorized IP addresses may control Profiling
74-
if ($controlIPs === false || in_array($_SERVER['REMOTE_ADDR'], $controlIPs) || PHP_SAPI == 'cli')
75-
{
76-
/* Backwards Compatibility getparam check*/
77-
if (!isset($_xhprof['getparam']))
78-
{
79-
$_xhprof['getparam'] = '_profile';
80-
}
81-
82-
if (isset($_GET[$_xhprof['getparam']]))
83-
{
84-
//Give them a cookie to hold status, and redirect back to the same page
85-
setcookie('_profile', $_GET[$_xhprof['getparam']]);
86-
$newURI = str_replace(array($_xhprof['getparam'].'=1',$_xhprof['getparam'].'=0'), '', $_SERVER['REQUEST_URI']);
87-
header("Location: $newURI");
88-
exit;
89-
}
90-
91-
if (isset($_COOKIE['_profile']) && $_COOKIE['_profile']
92-
|| PHP_SAPI == 'cli' && ( (isset($_SERVER[$envVarName]) && $_SERVER[$envVarName])
93-
|| (isset($_ENV[$envVarName]) && $_ENV[$envVarName])))
94-
{
95-
$_xhprof['display'] = true;
96-
$_xhprof['doprofile'] = true;
97-
$_xhprof['type'] = 1;
98-
}
99-
unset($envVarName);
74+
if ($controlIPs === false || in_array($_SERVER['REMOTE_ADDR'], $controlIPs) || PHP_SAPI == 'cli') {
75+
76+
/* Backwards Compatibility getparam check*/
77+
if ( ! isset( $_xhprof['getparam'] ) ) {
78+
$_xhprof['getparam'] = '_profile';
79+
}
80+
81+
if ( ! isset( $_xhprof['displayparam'] ) ) {
82+
$_xhprof['displayparam'] = '_display';
83+
}
84+
85+
$handleRuntimeToggle = function ( $key, $uri, $cookieName = null ) {
86+
if ( isset( $_GET[ $key ] ) ) {
87+
if ( null === $cookieName ) {
88+
$cookieName = $key;
89+
}
90+
// Give them a cookie to hold status, and redirect back to the same page
91+
if ( $_GET[ $key ] === "1" ) {
92+
setcookie( $cookieName, $_GET[ $key ] );
93+
} elseif ( $_GET[ $key ] === "0" ) {
94+
setcookie( $cookieName, null, - 1 );
95+
unset( $_COOKIE[ $cookieName ] );
96+
}
97+
98+
$cleanURI = str_replace( array(
99+
'&' . $key . '=1',
100+
'&' . $key . '=0',
101+
'?' . $key . '=1',
102+
'?' . $key . '=0',
103+
), '', $uri );
104+
105+
return [ true, $cleanURI ];
106+
} else {
107+
return [ false, $uri ];
108+
}
109+
};
110+
111+
$toggleParams = [ $_xhprof['getparam'], $_xhprof['displayparam'] ];
112+
113+
$currentURI = $_SERVER['REQUEST_URI'];
114+
$changes = false;
115+
foreach ( $toggleParams as $toggleParam ) {
116+
list( $changed, $currentURI ) = $handleRuntimeToggle( $toggleParam, $currentURI );
117+
$changes = ($changes or $changed);
118+
}
119+
120+
if ( isset( $_COOKIE[ $_xhprof['getparam'] ] ) && $_COOKIE[ $_xhprof['displayparam'] ]
121+
|| PHP_SAPI == 'cli' && ( ( isset( $_SERVER[ $envVarName ] ) && $_SERVER[ $envVarName ] )
122+
|| ( isset( $_ENV[ $envVarName ] ) && $_ENV[ $envVarName ] ) ) ) {
123+
$_xhprof['doprofile'] = true;
124+
$_xhprof['type'] = 1;
125+
}
126+
127+
if ( isset( $_COOKIE[ $_xhprof['displayparam'] ] ) && $_COOKIE[ $_xhprof['displayparam'] ]
128+
|| PHP_SAPI == 'cli' && ( ( isset( $_SERVER[ $envVarName ] ) && $_SERVER[ $envVarName ] )
129+
|| ( isset( $_ENV[ $envVarName ] ) && $_ENV[ $envVarName ] ) ) ) {
130+
$_xhprof['display'] = true;
131+
}
132+
133+
if ( true === $changes ) {
134+
header( 'HTTP/1.1 302 Found' );
135+
header( "Location: $currentURI" );
136+
die( "Redirecting you to " . $currentURI );
137+
}
138+
139+
unset( $envVarName );
100140
}
101141

102142

xhprof_lib/config.sample.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
$_xhprof['namespace'] = 'myapp';
1313
$_xhprof['url'] = 'http://url/to/xhprof/xhprof_html';
1414
$_xhprof['getparam'] = "_profile";
15+
$_xhprof['displayparam'] = "_display";
1516

1617
/*
1718
* MySQL/MySQLi/PDO ONLY

0 commit comments

Comments
 (0)