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