PageRenderTime 101ms CodeModel.GetById 58ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 1ms

/beacon/pagespeed/index.php

http://showslow.googlecode.com/
PHP | 163 lines | 145 code | 16 blank | 2 comment | 22 complexity | 67b68d635d4b2aefdda7504ccdf3078f MD5 | raw file
  1<?php 
  2require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
  3
  4function updateUrlAggregates($url_id, $measurement_id)
  5{
  6	# updating latest values for the URL
  7	$query = sprintf("UPDATE urls set pagespeed_last_id = %d, last_update = now() WHERE id = %d",
  8		mysql_real_escape_string($measurement_id),
  9		mysql_real_escape_string($url_id)
 10	);
 11	$result = mysql_query($query);
 12
 13	if (!$result) {
 14		beaconError(mysql_error());
 15	}
 16}
 17
 18if (array_key_exists('v', $_GET) && array_key_exists('u', $_GET)
 19	&& array_key_exists('w', $_GET) && filter_var($_GET['w'], FILTER_VALIDATE_INT) !== false
 20	&& array_key_exists('o', $_GET) && filter_var($_GET['o'], FILTER_VALIDATE_FLOAT) !== false
 21	&& array_key_exists('l', $_GET) && filter_var($_GET['l'], FILTER_VALIDATE_INT) !== false
 22	&& array_key_exists('r', $_GET) && filter_var($_GET['r'], FILTER_VALIDATE_INT) !== false
 23	&& array_key_exists('t', $_GET) && filter_var($_GET['t'], FILTER_VALIDATE_INT) !== false
 24	)
 25{
 26	$url_id = getUrlId($_GET['u']);
 27
 28	$metrics = array(
 29		'pBadReqs',
 30		'pBrowserCache',
 31		'pCacheValid',
 32		'pCharsetEarly',
 33		'pCombineCSS',
 34		'pCombineJS',
 35		'pCssImport',
 36		'pCssInHead',
 37		'pCssJsOrder',
 38		'pCssSelect',
 39		'pDeferJS',
 40		'pDocWrite',
 41		'pDupeRsrc',
 42		'pGzip',
 43		'pImgDims',
 44		'pMinDns',
 45		'pMinifyCSS',
 46		'pMinifyHTML',
 47		'pMinifyJS',
 48		'pMinRedirect',
 49		'pMinReqSize',
 50		'pNoCookie',
 51		'pOptImgs',
 52		'pParallelDl',
 53		'pPreferAsync',
 54		'pRemoveQuery',
 55		'pScaleImgs',
 56		'pSprite',
 57		'pUnusedCSS',
 58		'pVaryAE'
 59	);
 60
 61	$metric_renames = array(
 62		'pSpecifyCharsetEarly'				=> 'pCharsetEarly',
 63		'pProxyCache'					=> 'pCacheValid',
 64		'pPutCssInTheDocumentHead'			=> 'pCssInHead',
 65		'pOptimizeTheOrderOfStylesAndScripts'		=> 'pCssJsOrder',
 66		'pMinimizeRequestSize'				=> 'pMinReqSize',
 67		'pParallelizeDownloadsAcrossHostnames'		=> 'pParallelDl',
 68		'pServeStaticContentFromACookielessDomain'	=> 'pNoCookie',
 69		'pAvoidBadRequests'				=> 'pBadReqs',
 70		'pLeverageBrowserCaching'			=> 'pBrowserCache',
 71		'pRemoveQueryStringsFromStaticResources'	=> 'pRemoveQuery',
 72		'pServeScaledImages'				=> 'pScaleImgs',
 73		'pSpecifyACacheValidator'			=> 'pCacheValid',
 74		'pSpecifyAVaryAcceptEncodingHeader'		=> 'pVaryAE',
 75		'pSpecifyImageDimensions' 			=> 'pImgDims'
 76	);
 77
 78	$data_version = preg_replace('/[^0-9\.]+.*/', '', $_GET['v']);
 79
 80	foreach ($metrics as $metric) {
 81		$param = $metric;
 82
 83		foreach (array_reverse($metric_renames) as $from => $to) {
 84			if ($metric == $to
 85				&& !array_key_exists($metric, $_GET)
 86				&& array_key_exists($from, $_GET))
 87			{
 88				$param = $from;
 89			}
 90		}
 91
 92		if (array_key_exists($param, $_GET) && $_GET[$param] > 0) {
 93			$value = filter_var($_GET[$param], FILTER_VALIDATE_FLOAT);
 94			if ($value !== false) {
 95				$beacon[$metric] = $value;
 96			}
 97		}
 98	}
 99
100	$names = array();
101	$values = array();
102
103	foreach ($beacon as $metric => $value) {
104		$names[] = $metric;
105		$values[] = "'".mysql_real_escape_string($value)."'";
106	}
107
108	# adding new entry
109	$query = sprintf("INSERT INTO pagespeed (
110		`ip` , `user_agent` , `url_id` ,
111		`w` , `o` , `l`, `r` , `t`, `v` ,
112		%s
113	)
114	VALUES (inet_aton('%s'), '%s', '%d',
115		'%d', '%f', '%d', '%d', '%d', '%s',
116		%s
117	)",
118		implode(', ', $names),
119		mysql_real_escape_string($_SERVER['REMOTE_ADDR']),
120		mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']),
121		mysql_real_escape_string($url_id),
122		mysql_real_escape_string($_GET['w']),
123		mysql_real_escape_string($_GET['o']),
124		mysql_real_escape_string($_GET['l']),
125		mysql_real_escape_string($_GET['r']),
126		mysql_real_escape_string($_GET['t']),
127		mysql_real_escape_string($_GET['v']),
128		implode(', ', $values)
129	);
130
131	if (!mysql_query($query))
132	{
133		beaconError(mysql_error());
134	}
135
136	updateUrlAggregates($url_id, mysql_insert_id());
137} else {
138	header('HTTP/1.0 400 Bad Request');
139
140	?><html>
141<head>
142<title>Bad Request: Page Speed beacon</title>
143</head>
144<body>
145<h1>Bad Request: Page Speed beacon</h1>
146<p>This is <a href="http://code.google.com/speed/page-speed/">Page Speed</a> beacon entry point.</p>
147
148<h1>Configure your Page Speed</h1>
149<p><b style="color: red">WARNING! Only use this beacon If you're OK with all your Page Speed data to be recorded by this instance of ShowSlow and displayed at <a href="<?php echo $showslow_base?>"><?php echo $showslow_base?></a><br/>You can also <a href="http://www.showslow.org/Installation_and_configuration">install ShowSlow on your own server</a> to limit the risk.</b></p>
150
151Set these two Firefox parameters on <b>about:config</b> page:</p>
152
153<ul>
154<li>extensions.PageSpeed.beacon.minimal.url = <b style="color: blue"><?php echo $showslow_base?>beacon/pagespeed/</b></li>
155<li>extensions.PageSpeed.beacon.minimal.enabled = <b style="color: blue">true</b></li>
156</ul>
157
158</body></html>
159<?php
160	exit;
161}
162
163header('HTTP/1.0 204 Data accepted');