PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/external/header.php

http://github.com/preinheimer/xhprof
PHP | 172 lines | 139 code | 18 blank | 15 comment | 36 complexity | bc778b7d239207e41d3b9d70b8410bda MD5 | raw file
  1<?php
  2
  3require_once dirname(dirname(__FILE__)) . '/xhprof_lib/defaults.php';
  4require_once XHPROF_CONFIG;
  5
  6if (PHP_SAPI == 'cli') {
  7  $_SERVER['REMOTE_ADDR'] = null;
  8  $_SERVER['HTTP_HOST'] = null;
  9  $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
 10}
 11
 12function getExtensionName()
 13{
 14    if (extension_loaded('tideways'))
 15    {
 16        return 'tideways';
 17    }elseif(extension_loaded('tideways_xhprof'))
 18    {
 19        return 'tideways_xhprof';
 20    }elseif(extension_loaded('xhprof'))
 21    {
 22        return 'xhprof';
 23    }
 24    return false;
 25}
 26$_xhprof['ext_name'] = getExtensionName();
 27if($_xhprof['ext_name'])
 28{
 29    $flagsCpu = constant(strtoupper($_xhprof['ext_name']).'_FLAGS_CPU');
 30    $flagsMemory = constant(strtoupper($_xhprof['ext_name']).'_FLAGS_MEMORY');
 31    $envVarName = strtoupper($_xhprof['ext_name']).'_PROFILE';
 32}
 33
 34
 35//I'm Magic :)
 36class visibilitator
 37{
 38	public static function __callstatic($name, $arguments)
 39	{
 40		$func_name = array_shift($arguments);
 41		//var_dump($name);
 42		//var_dump("arguments" ,$arguments);
 43		//var_dump($func_name);
 44		if (is_array($func_name))
 45		{
 46			list($a, $b) = $func_name;
 47			if (count($arguments) == 0)
 48			{
 49				$arguments = $arguments[0];
 50			}
 51			return call_user_func_array(array($a, $b), $arguments);
 52			//echo "array call  -> $b ($arguments)";
 53		}else {
 54			call_user_func_array($func_name, $arguments);
 55		}
 56	}
 57}
 58
 59// Only users from authorized IP addresses may control Profiling
 60if ($controlIPs === false || in_array($_SERVER['REMOTE_ADDR'], $controlIPs) || PHP_SAPI == 'cli')
 61{
 62  /* Backwards Compatibility getparam check*/
 63  if (!isset($_xhprof['getparam']))
 64  {
 65      $_xhprof['getparam'] = '_profile';
 66  }
 67  
 68  if (isset($_GET[$_xhprof['getparam']]))
 69  {
 70    //Give them a cookie to hold status, and redirect back to the same page
 71    setcookie('_profile', $_GET[$_xhprof['getparam']]);
 72    $newURI = str_replace(array($_xhprof['getparam'].'=1',$_xhprof['getparam'].'=0'), '', $_SERVER['REQUEST_URI']);
 73    header("Location: $newURI");
 74    exit;
 75  }
 76  
 77  if (isset($_COOKIE['_profile']) && $_COOKIE['_profile'] 
 78          || PHP_SAPI == 'cli' && ( (isset($_SERVER[$envVarName]) && $_SERVER[$envVarName]) 
 79          || (isset($_ENV[$envVarName]) && $_ENV[$envVarName])))
 80  {
 81      $_xhprof['display'] = true;
 82      $_xhprof['doprofile'] = true;
 83      $_xhprof['type'] = 1;
 84  }
 85  unset($envVarName);
 86}
 87
 88
 89//Certain URLs should never have a link displayed. Think images, xml, etc. 
 90foreach($exceptionURLs as $url)
 91{
 92    if (stripos($_SERVER['REQUEST_URI'], $url) !== FALSE)
 93    {
 94        $_xhprof['display'] = false;
 95        header('X-XHProf-No-Display: Trueness');
 96        break;
 97    }    
 98}
 99unset($exceptionURLs);
100
101//Certain urls should have their POST data omitted. Think login forms, other privlidged info
102$_xhprof['savepost'] = true;
103foreach ($exceptionPostURLs as $url)
104{
105    if (stripos($_SERVER['REQUEST_URI'], $url) !== FALSE)
106    {
107        $_xhprof['savepost'] = false;
108        break;
109    }    
110}
111unset($exceptionPostURLs);
112
113//Determine wether or not to profile this URL randomly
114if ($_xhprof['doprofile'] === false && $weight)
115{
116    //Profile weighting, one in one hundred requests will be profiled without being specifically requested
117    if (rand(1, $weight) == 1)
118    {
119        $_xhprof['doprofile'] = true;
120        $_xhprof['type'] = 0;
121    } 
122}
123unset($weight);
124
125// Certain URLS should never be profiled.
126foreach($ignoreURLs as $url){
127    if (stripos($_SERVER['REQUEST_URI'], $url) !== FALSE)
128    {
129        $_xhprof['doprofile'] = false;
130        break;
131    }
132}
133unset($ignoreURLs);
134
135unset($url);
136
137// Certain domains should never be profiled.
138foreach($ignoreDomains as $domain){
139    if (stripos($_SERVER['HTTP_HOST'], $domain) !== FALSE)
140    {
141        $_xhprof['doprofile'] = false;
142        break;
143    }
144}
145unset($ignoreDomains);
146unset($domain);
147
148//Display warning if extension not available
149if ($_xhprof['ext_name'] && $_xhprof['doprofile'] === true) {
150    include_once dirname(__FILE__) . '/../xhprof_lib/utils/xhprof_lib.php';
151    include_once dirname(__FILE__) . '/../xhprof_lib/utils/xhprof_runs.php';
152    if (isset($ignoredFunctions) && is_array($ignoredFunctions) && !empty($ignoredFunctions)) {   
153        call_user_func($_xhprof['ext_name'].'_enable', $flagsCpu + $flagsMemory, array('ignored_functions' => $ignoredFunctions));
154    } else {
155        call_user_func($_xhprof['ext_name'].'_enable', $flagsCpu + $flagsMemory);
156    }
157    unset($flagsCpu);
158    unset($flagsMemory);
159    
160}elseif(false === $_xhprof['ext_name'] && $_xhprof['display'] === true)
161{
162    $message = 'Warning! Unable to profile run, tideways or xhprof extension not loaded';
163    trigger_error($message, E_USER_WARNING);
164}
165unset($flagsCpu);
166    unset($flagsMemory);
167function xhprof_shutdown_function() {
168    global $_xhprof;
169    require dirname(__FILE__).'/footer.php';
170}
171
172register_shutdown_function('xhprof_shutdown_function');