/beacon/webpagetest/index.php

https://github.com/FriedWishes/showslow · PHP · 178 lines · 148 code · 27 blank · 3 comment · 10 complexity · ef8592c017798af7a8e1ee858c18bead MD5 · raw file

  1. <?php
  2. require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
  3. function updateUrlAggregates($url_id, $measurement_id)
  4. {
  5. # updating latest values for the URL
  6. $query = sprintf("UPDATE urls SET pagetest_last_id = %d, last_update = now(), pagetest_refresh_request = 0 WHERE id = %d",
  7. mysql_real_escape_string($measurement_id),
  8. mysql_real_escape_string($url_id)
  9. );
  10. $result = mysql_query($query);
  11. if (!$result) {
  12. beaconError(mysql_error());
  13. }
  14. }
  15. $post_data = file_get_contents("php://input");
  16. $post = json_decode($post_data, true);
  17. if (!is_null($post) && array_key_exists('url', $post) && array_key_exists('id', $post))
  18. {
  19. $url_id = getUrlId(urldecode($post['url']));
  20. // fixing up -1 into nulls
  21. foreach (array_keys($post['first']) as $metric) {
  22. if ($post['first'][$metric] == -1) {
  23. $post['first'][$metric] = null;
  24. }
  25. }
  26. foreach (array_keys($post['repeat']) as $metric) {
  27. if ($post['repeat'][$metric] == -1) {
  28. $post['repeat'][$metric] = null;
  29. }
  30. }
  31. # adding new entry
  32. $query = sprintf("/* WPT POST */ REPLACE INTO pagetest (
  33. timestamp, url_id, test_id, location, version,
  34. f_LoadTime, r_LoadTime, f_TTFB, r_TTFB,
  35. f_bytesIn, r_bytesIn, f_bytesInDoc, r_bytesInDoc,
  36. f_requests, r_requests, f_requestsDoc, r_requestsDoc,
  37. f_connections, r_connections, f_domElements, r_domElements,
  38. f_score_cache, r_score_cache, f_score_cdn, r_score_cdn,
  39. f_score_gzip, r_score_gzip, f_score_cookies, r_score_cookies,
  40. f_score_keep_alive, r_score_keep_alive, f_score_minify, r_score_minify,
  41. f_score_combine, r_score_combine, f_score_compress, r_score_compress,
  42. f_score_etags, r_score_etags, f_gzip_total, r_gzip_total,
  43. f_gzip_savings, r_gzip_savings, f_minify_total, r_minify_total,
  44. f_minify_savings, r_minify_savings, f_image_total, r_image_total,
  45. f_image_savings, r_image_savings,
  46. f_render, r_render, f_aft, r_aft,
  47. f_fullyLoaded, r_fullyLoaded, f_docTime, r_docTime,
  48. f_domTime, r_domTime
  49. )
  50. VALUES (
  51. FROM_UNIXTIME('%d'), '%d', '%s', '%s', '%s',
  52. '%d', '%d', '%d', '%d',
  53. '%d', '%d', '%d', '%d',
  54. '%d', '%d', '%d', '%d',
  55. '%d', '%d', '%d', '%d',
  56. '%d', '%d', '%d', '%d',
  57. '%d', '%d', '%d', '%d',
  58. '%d', '%d', '%d', '%d',
  59. '%d', '%d', '%d', '%d',
  60. '%d', '%d', '%d', '%d',
  61. '%d', '%d', '%d', '%d',
  62. '%d', '%d', '%d', '%d',
  63. '%d', '%d',
  64. '%d', '%d', '%d', '%d',
  65. '%d', '%d', '%d', '%d',
  66. '%d', '%d'
  67. )",
  68. mysql_real_escape_string($post['completed']),
  69. mysql_real_escape_string($url_id),
  70. mysql_real_escape_string($post['id']),
  71. mysql_real_escape_string($post['location']),
  72. mysql_real_escape_string($post['version']),
  73. mysql_real_escape_string($post['first']['loadTime']),
  74. mysql_real_escape_string($post['repeat']['loadTime']),
  75. mysql_real_escape_string($post['first']['TTFB']),
  76. mysql_real_escape_string($post['repeat']['TTFB']),
  77. mysql_real_escape_string($post['first']['bytesIn']),
  78. mysql_real_escape_string($post['repeat']['bytesIn']),
  79. mysql_real_escape_string($post['first']['bytesInDoc']),
  80. mysql_real_escape_string($post['repeat']['bytesInDoc']),
  81. mysql_real_escape_string($post['first']['requests']),
  82. mysql_real_escape_string($post['repeat']['requests']),
  83. mysql_real_escape_string($post['first']['requestsDoc']),
  84. mysql_real_escape_string($post['repeat']['requestsDoc']),
  85. mysql_real_escape_string($post['first']['connections']),
  86. mysql_real_escape_string($post['repeat']['connections']),
  87. mysql_real_escape_string($post['first']['domElements']),
  88. mysql_real_escape_string($post['repeat']['domElements']),
  89. mysql_real_escape_string($post['first']['score_cache']),
  90. mysql_real_escape_string($post['repeat']['score_cache']),
  91. mysql_real_escape_string($post['first']['score_cdn']),
  92. mysql_real_escape_string($post['repeat']['score_cdn']),
  93. mysql_real_escape_string($post['first']['score_gzip']),
  94. mysql_real_escape_string($post['repeat']['score_gzip']),
  95. mysql_real_escape_string($post['first']['score_cookies']),
  96. mysql_real_escape_string($post['repeat']['score_cookies']),
  97. mysql_real_escape_string($post['first']['score_keep-alive']),
  98. mysql_real_escape_string($post['repeat']['score_keep-alive']),
  99. mysql_real_escape_string($post['first']['score_minify']),
  100. mysql_real_escape_string($post['repeat']['score_minify']),
  101. mysql_real_escape_string($post['first']['score_combine']),
  102. mysql_real_escape_string($post['repeat']['score_combine']),
  103. mysql_real_escape_string($post['first']['score_compress']),
  104. mysql_real_escape_string($post['repeat']['score_compress']),
  105. mysql_real_escape_string($post['first']['score_etags']),
  106. mysql_real_escape_string($post['repeat']['score_etags']),
  107. mysql_real_escape_string($post['first']['gzip_total']),
  108. mysql_real_escape_string($post['repeat']['gzip_total']),
  109. mysql_real_escape_string($post['first']['gzip_savings']),
  110. mysql_real_escape_string($post['repeat']['gzip_savings']),
  111. mysql_real_escape_string($post['first']['minify_total']),
  112. mysql_real_escape_string($post['repeat']['minify_total']),
  113. mysql_real_escape_string($post['first']['minify_savings']),
  114. mysql_real_escape_string($post['repeat']['minify_savings']),
  115. mysql_real_escape_string($post['first']['image_total']),
  116. mysql_real_escape_string($post['repeat']['image_total']),
  117. mysql_real_escape_string($post['first']['image_savings']),
  118. mysql_real_escape_string($post['repeat']['image_savings']),
  119. mysql_real_escape_string($post['first']['render']),
  120. mysql_real_escape_string($post['repeat']['render']),
  121. mysql_real_escape_string($post['first']['aft']),
  122. mysql_real_escape_string($post['repeat']['aft']),
  123. mysql_real_escape_string($post['first']['fullyLoaded']),
  124. mysql_real_escape_string($post['repeat']['fullyLoaded']),
  125. mysql_real_escape_string($post['first']['docTime']),
  126. mysql_real_escape_string($post['repeat']['docTime']),
  127. mysql_real_escape_string($post['first']['domTime']),
  128. mysql_real_escape_string($post['repeat']['domTime'])
  129. );
  130. if (!mysql_query($query))
  131. {
  132. beaconError(mysql_error());
  133. }
  134. updateUrlAggregates($url_id, mysql_insert_id());
  135. } else {
  136. header('HTTP/1.0 400 Bad Request');
  137. $TITLE = 'Bad Request: WebPageTest beacon';
  138. require_once(dirname(dirname(dirname(__FILE__))).'/header.php');
  139. ?>
  140. <h2><a href="../">Beacons</a>: WebPageTest</h2>
  141. <p>This is <a href="http://www.webpagetest.org/">WebPageTest</a> beacon entry point.</p>
  142. <h2>Configure your WebPageTest instance</h2>
  143. <p><b style="color: red">WARNING! Only use this beacon If you're OK with all your WebPageTest 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>
  144. <?php
  145. require_once(dirname(dirname(dirname(__FILE__))).'/footer.php');
  146. exit;
  147. }
  148. header('HTTP/1.0 204 Data accepted');