PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pagetest.php

http://showslow.googlecode.com/
PHP | 107 lines | 87 code | 16 blank | 4 comment | 14 complexity | fe4024937c6d3347a7d29d9b631d2405 MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__).'/global.php');
  3. require_once(dirname(__FILE__).'/users/users.php');
  4. if (!is_null($webPageTestKey) && array_key_exists('url', $_REQUEST))
  5. {
  6. $url_id = getUrlId($_REQUEST['url']);
  7. $runtest = $webPageTestBase.'runtest.php?k='.urlencode($webPageTestKey).'&'.($webPageTestExtraParams ? $webPageTestExtraParams.'&' : '').'f=xml&r=yes&url='.urlencode($_REQUEST['url']);
  8. $location = null;
  9. $private = false;
  10. if (array_key_exists('location', $_REQUEST)) {
  11. $location = $_REQUEST['location'];
  12. $runtest.='&location='.$location;
  13. } else {
  14. header('Location: '.$showslow_base);
  15. exit;
  16. }
  17. if (array_key_exists('private', $_REQUEST)) {
  18. $private = $_REQUEST['private'];
  19. $runtest.='&private='.$_REQUEST['private'];
  20. }
  21. if (array_key_exists('fvonly', $_REQUEST)) {
  22. $runtest.='&fvonly='.$_REQUEST['fvonly'];
  23. }
  24. // fetching locations only when needed
  25. getPageTestLocations();
  26. if (!array_key_exists($location, $webPageTestLocationsById))
  27. {
  28. // location doesn't exist
  29. error_log("PageTest Location doesn't exist: $location");
  30. header('Location: '.$showslow_base);
  31. exit;
  32. } else if ($webPageTestLocationsById[$location]['tests'] > 50) {
  33. // location is overloaded
  34. error_log("PageTest Location is overloaded: $location");
  35. header('Location: '.$showslow_base);
  36. exit;
  37. }
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL, $runtest);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41. $output = curl_exec($ch);
  42. if (empty($output)) {
  43. $err = curl_error($ch);
  44. curl_close($ch);
  45. failWithMessage("API call ($runtest) failed: ".$err);
  46. }
  47. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  48. if ($code != 200) {
  49. curl_close($ch);
  50. failWithMessage("PageTest didn't accept the request: $code");
  51. }
  52. curl_close($ch);
  53. $xml = new SimpleXMLElement($output);
  54. if (empty($xml)) {
  55. failWithMessage("Failed to parse XML response");
  56. }
  57. if ($xml->statusCode != 200) {
  58. failWithMessage("PageTest returned failure status code: ".$xml->statusCode." (".$xml->statusText.")");
  59. }
  60. $testId = $xml->data->testId;
  61. $userUrl = $xml->data->userUrl;
  62. if (!$private || $keepPrivatePageTests) {
  63. # adding new entry
  64. $query = sprintf("INSERT INTO pagetest (url_id, test_id, test_url, location)
  65. VALUES ('%d', '%s', '%s', '%s')",
  66. mysql_real_escape_string($url_id),
  67. mysql_real_escape_string($testId),
  68. mysql_real_escape_string($userUrl),
  69. mysql_real_escape_string($location)
  70. );
  71. if (!mysql_query($query))
  72. {
  73. failWithMessage(mysql_error());
  74. }
  75. # updating modification date for the URL
  76. $query = sprintf("UPDATE urls SET last_update = now() WHERE id = %d",
  77. mysql_real_escape_string($url_id)
  78. );
  79. $result = mysql_query($query);
  80. }
  81. $current_user = User::get();
  82. if (!is_null($current_user)) {
  83. $current_user->recordActivity(SHOWSLOW_ACTIVITY_PAGETEST_START);
  84. }
  85. header('Location: '.$userUrl);
  86. exit;
  87. }
  88. header('Location: '.$showslow_base);