PageRenderTime 73ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 17ms app.codeStats 0ms

/beacon/yslow/index.php

http://showslow.googlecode.com/
PHP | 227 lines | 203 code | 19 blank | 5 comment | 27 complexity | a1171d18643d61d494c730e7a1de9932 MD5 | raw file
  1<?php 
  2require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
  3
  4function updateUrlAggregates($url_id, $measurement_id)
  5{
  6	global $cleanOldYSlowBeaconDetails;
  7
  8	# updating latest values for the URL
  9	$query = sprintf("UPDATE urls SET yslow2_last_id = %d, last_update = now() WHERE id = %d",
 10		mysql_real_escape_string($measurement_id),
 11		mysql_real_escape_string($url_id)
 12	);
 13	$result = mysql_query($query);
 14
 15	if (!$result) {
 16		beaconError(mysql_error());
 17	}
 18
 19	// Clean old details for this URL to conserve space
 20	if ($cleanOldYSlowBeaconDetails) {
 21		# adding new entry
 22		$query = sprintf("/* clean old beacon details */
 23			UPDATE yslow2
 24			SET details = NULL
 25			WHERE url_id = '%d' AND id <> '%d'
 26		", mysql_real_escape_string($url_id), mysql_real_escape_string($measurement_id));
 27
 28		if (!mysql_query($query))
 29		{
 30			beaconError(mysql_error());
 31		}
 32	}
 33
 34}
 35
 36$post_data = file_get_contents("php://input");
 37$post = json_decode($post_data, true);
 38
 39if (!is_null($post) && array_key_exists('u', $post) && array_key_exists('g', $post)
 40	&& array_key_exists('i', $post) && in_array($post['i'], $YSlow2AllowedProfiles)
 41	&& array_key_exists('w', $post) && filter_var($post['w'], FILTER_VALIDATE_INT) !== false
 42	&& array_key_exists('o', $post) && filter_var($post['o'], FILTER_VALIDATE_INT) !== false
 43	&& array_key_exists('r', $post) && filter_var($post['r'], FILTER_VALIDATE_INT) !== false
 44	)
 45{
 46	$url_id = getUrlId(urldecode($post['u']));
 47
 48	$grades = $post['g'];
 49
 50	$ynumreq	= $grades['ynumreq']['score'];
 51	$ycdn		= $grades['ycdn']['score'];
 52	$yexpires	= $grades['yexpires']['score'];
 53	$yemptysrc	= $grades['yemptysrc']['score'];
 54	$ycompress	= $grades['ycompress']['score'];
 55	$ycsstop	= $grades['ycsstop']['score'];
 56	$yjsbottom	= $grades['yjsbottom']['score'];
 57	$yexpressions	= $grades['yexpressions']['score'];
 58	$yexternal	= $grades['yexternal']['score'];
 59	$ydns		= $grades['ydns']['score'];
 60	$yminify	= $grades['yminify']['score'];
 61	$yredirects	= $grades['yredirects']['score'];
 62	$ydupes		= $grades['ydupes']['score'];
 63	$yetags		= $grades['yetags']['score'];
 64	$yxhr		= $grades['yxhr']['score'];
 65	$yxhrmethod	= $grades['yxhrmethod']['score'];
 66	$ymindom	= $grades['ymindom']['score'];
 67	$yno404		= $grades['yno404']['score'];
 68	$ymincookie	= $grades['ymincookie']['score'];
 69	$ycookiefree	= $grades['ycookiefree']['score'];
 70	$ynofilter	= $grades['ynofilter']['score'];
 71	$yimgnoscale	= $grades['yimgnoscale']['score'];
 72	$yfavicon	= $grades['yfavicon']['score'];
 73
 74	# adding new entry
 75	$query = sprintf("/* grades POST */ INSERT INTO yslow2 (
 76		`ip` , `user_agent` , `url_id` ,
 77		`w` , `o` , `r` , `i` , lt,
 78		`ynumreq`,	`ycdn`,		`yexpires`,	`yemptysrc`, `ycompress`,	`ycsstop`,
 79		`yjsbottom`,	`yexpressions`,	`yexternal`,	`ydns`,		`yminify`,
 80		`yredirects`,	`ydupes`,	`yetags`,	`yxhr`,		`yxhrmethod`,
 81		`ymindom`,	`yno404`,	`ymincookie`,	`ycookiefree`,	`ynofilter`,
 82		`yimgnoscale`,	`yfavicon`, details
 83	)
 84	VALUES (inet_aton('%s'), '%s', '%d',
 85		'%d', '%d', '%d', '%s', '%d',
 86		'%d', '%d', '%d', '%d', '%d', '%d',
 87		'%d', '%d', '%d', '%d', '%d',
 88		'%d', '%d', '%d', '%d', '%d',
 89		'%d', '%d', '%d', '%d', '%d',
 90		'%d', '%d', '%s'
 91	)",
 92		mysql_real_escape_string($_SERVER['REMOTE_ADDR']),
 93		mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']),
 94		mysql_real_escape_string($url_id),
 95		mysql_real_escape_string($post['w']),
 96		mysql_real_escape_string($post['o']),
 97		mysql_real_escape_string($post['r']),
 98		mysql_real_escape_string($post['i']),
 99		mysql_real_escape_string($post['lt']),
100		mysql_real_escape_string($ynumreq),
101		mysql_real_escape_string($ycdn),
102		mysql_real_escape_string($yexpires),
103		mysql_real_escape_string($yemptysrc),
104		mysql_real_escape_string($ycompress),
105		mysql_real_escape_string($ycsstop),
106		mysql_real_escape_string($yjsbottom),
107		mysql_real_escape_string($yexpressions),
108		mysql_real_escape_string($yexternal),
109		mysql_real_escape_string($ydns),
110		mysql_real_escape_string($yminify),
111		mysql_real_escape_string($yredirects),
112		mysql_real_escape_string($ydupes),
113		mysql_real_escape_string($yetags),
114		mysql_real_escape_string($yxhr),
115		mysql_real_escape_string($yxhrmethod),
116		mysql_real_escape_string($ymindom),
117		mysql_real_escape_string($yno404),
118		mysql_real_escape_string($ymincookie),	
119		mysql_real_escape_string($ycookiefree),	
120		mysql_real_escape_string($ynofilter),
121		mysql_real_escape_string($yimgnoscale),	
122		mysql_real_escape_string($yfavicon),
123		mysql_real_escape_string($post_data)
124	);
125
126	if (!mysql_query($query))
127	{
128		beaconError(mysql_error());
129	}
130
131	updateUrlAggregates($url_id, mysql_insert_id());
132
133} else if (array_key_exists('u', $_GET) && array_key_exists('i', $_GET) && in_array($_GET['i'], $YSlow2AllowedProfiles)
134	&& array_key_exists('w', $_GET) && filter_var($_GET['w'], FILTER_VALIDATE_INT) !== false
135	&& array_key_exists('o', $_GET) && filter_var($_GET['o'], FILTER_VALIDATE_INT) !== false
136	&& array_key_exists('r', $_GET) && filter_var($_GET['r'], FILTER_VALIDATE_INT) !== false
137	)
138{
139	$url_id = getUrlId($_GET['u']);
140
141	# adding new entry
142	$query = sprintf("/* basic GET */ INSERT INTO yslow2 (
143		`ip` , `user_agent` , `url_id` ,
144		`w` , `o` , `r` , `i`, lt,
145		`ynumreq`,	`ycdn`,		`yexpires`,	`yemptysrc`,	`ycompress`,	`ycsstop`,
146		`yjsbottom`,	`yexpressions`,	`yexternal`,	`ydns`,		`yminify`,
147		`yredirects`,	`ydupes`,	`yetags`,	`yxhr`,		`yxhrmethod`,
148		`ymindom`,	`yno404`,	`ymincookie`,	`ycookiefree`,	`ynofilter`,
149		`yimgnoscale`,	`yfavicon`
150	)
151	VALUES (inet_aton('%s'), '%s', '%d',
152		'%d', '%d', '%d', '%s', '%d',
153		'%d', '%d', '%d', '%d', '%d', '%d',
154		'%d', '%d', '%d', '%d', '%d',
155		'%d', '%d', '%d', '%d', '%d',
156		'%d', '%d', '%d', '%d', '%d',
157		'%d', '%d'
158	)",
159		mysql_real_escape_string($_SERVER['REMOTE_ADDR']),
160		mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']),
161		mysql_real_escape_string($url_id),
162		mysql_real_escape_string($_GET['w']),
163		mysql_real_escape_string($_GET['o']),
164		mysql_real_escape_string($_GET['r']),
165		mysql_real_escape_string($_GET['i']),
166		mysql_real_escape_string($_GET['lt']),
167		mysql_real_escape_string($_GET['ynumreq']),
168		mysql_real_escape_string($_GET['ycdn']),
169		mysql_real_escape_string($_GET['yexpires']),
170		mysql_real_escape_string($_GET['yemptysrc']),
171		mysql_real_escape_string($_GET['ycompress']),
172		mysql_real_escape_string($_GET['ycsstop']),
173		mysql_real_escape_string($_GET['yjsbottom']),
174		mysql_real_escape_string($_GET['yexpressions']),
175		mysql_real_escape_string($_GET['yexternal']),
176		mysql_real_escape_string($_GET['ydns']),
177		mysql_real_escape_string($_GET['yminify']),
178		mysql_real_escape_string($_GET['yredirects']),
179		mysql_real_escape_string($_GET['ydupes']),
180		mysql_real_escape_string($_GET['yetags']),
181		mysql_real_escape_string($_GET['yxhr']),
182		mysql_real_escape_string($_GET['yxhrmethod']),
183		mysql_real_escape_string($_GET['ymindom']),
184		mysql_real_escape_string($_GET['yno404']),
185		mysql_real_escape_string($_GET['ymincookie']),
186		mysql_real_escape_string($_GET['ycookiefree']),
187		mysql_real_escape_string($_GET['ynofilter']),
188		mysql_real_escape_string($_GET['yimgnoscale']),
189		mysql_real_escape_string($_GET['yfavicon'])
190	);
191
192	if (!mysql_query($query))
193	{
194		beaconError(mysql_error());
195	}
196
197	updateUrlAggregates($url_id, mysql_insert_id());
198} else {
199	header('HTTP/1.0 400 Bad Request');
200
201	?><html>
202<head>
203<title>Bad Request: YSlow beacon</title>
204</head>
205<body>
206<h1>Bad Request: YSlow beacon</h1>
207<p>This is <a href="http://developer.yahoo.com/yslow/">YSlow</a> beacon entry point.</p>
208
209<h1>Configure your YSlow</h1>
210<p><b style="color: red">WARNING! Only use this beacon If you're OK with all your YSlow 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>
211
212<p>Set these two Firefox parameters on <b>about:config</b> page:</p>
213
214</ul>
215<h2>Yslow 2.x</h2>
216<ul>
217<li>extensions.yslow.beaconUrl = <b style="color: blue"><?php echo $showslow_base?>beacon/yslow/</b></li>
218<li>extensions.yslow.beaconInfo = <b style="color: blue">grade</b></li>
219<li>extensions.yslow.optinBeacon = <b style="color: blue">true</b></li>
220</ul>
221
222</body></html>
223<?php
224	exit;
225}
226
227header('HTTP/1.0 204 Data accepted');