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