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