PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 30ms app.codeStats 0ms

/beacon/har/index.php

http://showslow.googlecode.com/
PHP | 203 lines | 191 code | 11 blank | 1 comment | 10 complexity | 5e915466cc642f2bdd0d6c2a9ff06fb4 MD5 | raw file
  1<?php 
  2require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
  3
  4// in case when link to external HAR file was provided
  5
  6if (array_key_exists('link', $_REQUEST) && trim($_REQUEST['link']) != ''
  7	&& array_key_exists('url', $_REQUEST))
  8{
  9	$link = filter_var(urldecode(trim($_REQUEST['link'])), FILTER_VALIDATE_URL);
 10
 11	$url_id = getUrlId(urldecode($_REQUEST['url']));
 12	
 13	if (array_key_exists('timestamp', $_REQUEST))
 14	{
 15		$query = sprintf("/* HAR link */ INSERT INTO har (timestamp, url_id, link)
 16		VALUES ('%s', '%d', '%s')",
 17			mysql_real_escape_string($_REQUEST['timestamp']),
 18			mysql_real_escape_string($url_id),
 19			mysql_real_escape_string($link)
 20		);
 21	}
 22	else
 23	{
 24		$query = sprintf("/* HAR link */ INSERT INTO har (url_id, link)
 25		VALUES ('%d', '%s')",
 26			mysql_real_escape_string($url_id),
 27			mysql_real_escape_string($link)
 28		);
 29	}
 30
 31
 32	if (!mysql_query($query))
 33	{
 34		beaconError(mysql_error());
 35	}
 36
 37	header('HTTP/1.0 204 Data accepted');
 38	exit;
 39}
 40
 41if ($_SERVER["REQUEST_METHOD"] != 'POST')
 42{
 43	?><html>
 44<head>
 45<title>HAR beacon</title>
 46</head>
 47<body>
 48<h1>HAR beacon</h1>
 49<p>This is <a href="http://groups.google.com/group/firebug-working-group/web/http-tracing---export-format">HAR</a> beacon entry point.</p>
 50
 51<h1>Configure your HAR provider</h1>
 52<p><b style="color: red">WARNING! Only use this beacon If you're OK with all your HAR data to be recorded by this instance of ShowSlow and displayed at <a href="<?php echo $showslow_base?>"><?php echo $showslow_base?></a><br/>All your data including cookies, IP addresses, sessions and possibly other sensitive information will be displayed on this instance.<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>
 53<p>To submit a beacon, you must send HAR file as a POST body or upload it as a file using form below.</p>
 54<p>There is also a <tt>url</tt> parameter that you have to supply and optional <tt>timestamp</tt> parameter.</p>
 55
 56<p>Beacon URL: <b style="color: blue"><?php echo $showslow_base?>beacon/har/</b></p>
 57
 58<h2>You can use on of these HAR providers</h2>
 59<ul>
 60<li><a href="http://getfirebug.com/releases/extensions.html#netexport">NetExport</a> extension for Firebug</li>
 61</ul>
 62<?php
 63if (!$enableHARBeacon) {
 64	?><p style="color: red">HAR beacon is disabled on this instance of ShowSlow.<br/>Add <tt>$enableHARBeacon = true;</tt> to your configuration file to enable it.</p><?php
 65}
 66?>
 67<h1>Submit HAR manually</h1>
 68<form action="" method="POST" enctype="multipart/form-data">
 69<table>
 70<tr><td>URL:</td><td><input type="text" name="url" value="http://www.example.com/" size="80"<?php if (!$enableHARBeacon) {?> disabled="disabled"<?php } ?>/></td></tr>
 71<tr valign="top"><td>Time:</td><td><input type="text" name="timestamp" size="25" value="<?php echo date("Y-m-d H:i:s");?>"<?php if (!$enableHARBeacon) {?> disabled="disabled"<?php }?>/><br/>Time in MySQL <a href="http://dev.mysql.com/doc/refman/5.1/en/datetime.html">timestamp format</a></td></tr>
 72<tr><td>Pick HAR file:</td><td><input name="har" type="file"<?php if (!$enableHARBeacon) {?> disabled="disabled"<?php }?>/></td></tr>
 73<tr><td>Or enter a URL of<br/>externally hosted HAR file:</td><td><input type="text" name="link" value="" size="80"<?php if (!$enableHARBeacon) {?> disabled="disabled"<?php } ?>/></td></tr>
 74<tr><td></td><td><input type="submit" value="add"<?php if (!$enableHARBeacon) {?> disabled="disabled"<?php }?>/></td></tr>
 75
 76</table>
 77</form>
 78
 79</body></html>
 80<?php 
 81	exit;
 82}
 83
 84
 85// in case HAR body was POSTed to beacon
 86
 87// check if manual upload was used
 88if (array_key_exists('har', $_FILES))
 89{
 90	$filename = $_FILES["har"]["tmp_name"];
 91} else {
 92	$filename = "php://input";
 93}
 94
 95if ($filename == '') {
 96	header('HTTP/1.0 400 Bad Request');
 97
 98?><html>
 99<head>
100<title>Bad Request: no HAR data</title>
101</head>
102<body>
103<h1>Bad Request: no HAR data</h1>
104No HAR data submitted
105</body>
106</html><?php
107	exit;
108}
109
110$har_data = FALSE;
111
112if (defined('FORCE_GZIP'))
113{
114	if ($gzfile = gzopen($filename, 'r'))
115	{
116		while ($chunk = gzread($gzfile, 100000))
117		{
118			$har_data = $har_data.$chunk;
119		}
120		gzclose($gzfile);
121	}
122	else
123	{
124?><html>
125<head>
126<title>Bad Request: Can't read POST payload</title>
127</head>
128<body>
129<h1>Bad Request: Can't read POST payload</h1>
130Can't read POST payload
131</body>
132</html><?php
133		exit;
134	}
135}
136else
137{
138	$har_data = file_get_contents($filename);
139}
140
141if ($har_data === FALSE || json_decode($har_data) === FALSE) {
142	header('HTTP/1.0 400 Bad Request');
143
144?><html>
145<head>
146<title>Bad Request: malformed HAR data</title>
147</head>
148<body>
149<h1>Bad Request: malformed HAR data</h1>
150Can't parse JSON data from HAR
151</body>
152</html><?php
153	exit;
154}
155
156if (array_key_exists('url', $_REQUEST))
157{
158	$url_id = getUrlId(urldecode($_REQUEST['url']));
159
160	# adding new entry
161
162	if (array_key_exists('timestamp', $_REQUEST))
163	{
164		$query = sprintf("/* HAR POST */ INSERT INTO har (timestamp, url_id, har, compressed)
165		VALUES ('%s', '%d', '%s', '%d')",
166			mysql_real_escape_string($_REQUEST['timestamp']),
167			mysql_real_escape_string($url_id),
168			mysql_real_escape_string(defined('FORCE_GZIP') ? gzcompress($har_data) : $har_data),
169			mysql_real_escape_string(defined('FORCE_GZIP') ? 1 : 0)
170		);
171	}
172	else
173	{
174		$query = sprintf("/* HAR POST */ INSERT INTO har (url_id, har, compressed)
175		VALUES ('%d', '%s', '%d')",
176			mysql_real_escape_string($url_id),
177			mysql_real_escape_string(defined('FORCE_GZIP') ? gzcompress($har_data) : $har_data),
178			mysql_real_escape_string(defined('FORCE_GZIP') ? 1 : 0)
179		);
180	}
181
182
183	if (!mysql_query($query))
184	{
185		beaconError(mysql_error());
186	}
187} else {
188	header('HTTP/1.0 400 Bad Request');
189
190	?><html>
191<head>
192<title>Bad Request: HAR beacon</title>
193</head>
194<body>
195<h1>Bad Request: HAR beacon</h1>
196You must pass "url" parameter along with HAR file in POST body or as 'har' POST field.
197</form>
198
199</body></html>
200<?php 
201}
202
203header('HTTP/1.0 204 Data accepted');