PageRenderTime 108ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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 pagespeed_last_id = %d, last_update = now() 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. if (array_key_exists('v', $_GET) && array_key_exists('u', $_GET)
  16. && array_key_exists('w', $_GET) && filter_var($_GET['w'], FILTER_VALIDATE_INT) !== false
  17. && array_key_exists('o', $_GET) && filter_var($_GET['o'], FILTER_VALIDATE_FLOAT) !== false
  18. && array_key_exists('l', $_GET) && filter_var($_GET['l'], FILTER_VALIDATE_INT) !== false
  19. && array_key_exists('r', $_GET) && filter_var($_GET['r'], FILTER_VALIDATE_INT) !== false
  20. && array_key_exists('t', $_GET) && filter_var($_GET['t'], FILTER_VALIDATE_INT) !== false
  21. )
  22. {
  23. $url_id = getUrlId($_GET['u']);
  24. $metrics = array(
  25. 'pBadReqs',
  26. 'pBrowserCache',
  27. 'pCacheValid',
  28. 'pCharsetEarly',
  29. 'pCombineCSS',
  30. 'pCombineJS',
  31. 'pCssImport',
  32. 'pCssInHead',
  33. 'pCssJsOrder',
  34. 'pCssSelect',
  35. 'pDeferJS',
  36. 'pDocWrite',
  37. 'pDupeRsrc',
  38. 'pGzip',
  39. 'pImgDims',
  40. 'pMinDns',
  41. 'pMinifyCSS',
  42. 'pMinifyHTML',
  43. 'pMinifyJS',
  44. 'pMinRedirect',
  45. 'pMinReqSize',
  46. 'pNoCookie',
  47. 'pOptImgs',
  48. 'pParallelDl',
  49. 'pPreferAsync',
  50. 'pRemoveQuery',
  51. 'pScaleImgs',
  52. 'pSprite',
  53. 'pUnusedCSS',
  54. 'pVaryAE'
  55. );
  56. $metric_renames = array(
  57. 'pSpecifyCharsetEarly' => 'pCharsetEarly',
  58. 'pProxyCache' => 'pCacheValid',
  59. 'pPutCssInTheDocumentHead' => 'pCssInHead',
  60. 'pOptimizeTheOrderOfStylesAndScripts' => 'pCssJsOrder',
  61. 'pMinimizeRequestSize' => 'pMinReqSize',
  62. 'pParallelizeDownloadsAcrossHostnames' => 'pParallelDl',
  63. 'pServeStaticContentFromACookielessDomain' => 'pNoCookie',
  64. 'pAvoidBadRequests' => 'pBadReqs',
  65. 'pLeverageBrowserCaching' => 'pBrowserCache',
  66. 'pRemoveQueryStringsFromStaticResources' => 'pRemoveQuery',
  67. 'pServeScaledImages' => 'pScaleImgs',
  68. 'pSpecifyACacheValidator' => 'pCacheValid',
  69. 'pSpecifyAVaryAcceptEncodingHeader' => 'pVaryAE',
  70. 'pSpecifyImageDimensions' => 'pImgDims'
  71. );
  72. $data_version = preg_replace('/[^0-9\.]+.*/', '', $_GET['v']);
  73. foreach ($metrics as $metric) {
  74. $param = $metric;
  75. foreach (array_reverse($metric_renames) as $from => $to) {
  76. if ($metric == $to
  77. && !array_key_exists($metric, $_GET)
  78. && array_key_exists($from, $_GET))
  79. {
  80. $param = $from;
  81. }
  82. }
  83. if (array_key_exists($param, $_GET) && $_GET[$param] > 0) {
  84. $value = filter_var($_GET[$param], FILTER_VALIDATE_FLOAT);
  85. if ($value !== false) {
  86. $beacon[$metric] = $value;
  87. }
  88. }
  89. }
  90. $names = array();
  91. $values = array();
  92. foreach ($beacon as $metric => $value) {
  93. $names[] = $metric;
  94. $values[] = "'".mysql_real_escape_string($value)."'";
  95. }
  96. # adding new entry
  97. $query = sprintf("INSERT INTO pagespeed (
  98. `ip` , `user_agent` , `url_id` ,
  99. `w` , `o` , `l`, `r` , `t`, `v` ,
  100. %s
  101. )
  102. VALUES (inet_aton('%s'), '%s', '%d',
  103. '%d', '%f', '%d', '%d', '%d', '%s',
  104. %s
  105. )",
  106. implode(', ', $names),
  107. mysql_real_escape_string($_SERVER['REMOTE_ADDR']),
  108. mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']),
  109. mysql_real_escape_string($url_id),
  110. mysql_real_escape_string($_GET['w']),
  111. mysql_real_escape_string($_GET['o']),
  112. mysql_real_escape_string($_GET['l']),
  113. mysql_real_escape_string($_GET['r']),
  114. mysql_real_escape_string($_GET['t']),
  115. mysql_real_escape_string($_GET['v']),
  116. implode(', ', $values)
  117. );
  118. if (!mysql_query($query))
  119. {
  120. beaconError(mysql_error());
  121. }
  122. updateUrlAggregates($url_id, mysql_insert_id());
  123. } else {
  124. header('HTTP/1.0 400 Bad Request');
  125. ?><html>
  126. <head>
  127. <title>Bad Request: Page Speed beacon</title>
  128. </head>
  129. <body>
  130. <h1>Bad Request: Page Speed beacon</h1>
  131. <p>This is <a href="http://code.google.com/speed/page-speed/">Page Speed</a> beacon entry point.</p>
  132. <h1>Configure your Page Speed</h1>
  133. <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>
  134. Set these two Firefox parameters on <b>about:config</b> page:</p>
  135. <ul>
  136. <li>extensions.PageSpeed.beacon.minimal.url = <b style="color: blue"><?php echo $showslow_base?>beacon/pagespeed/</b></li>
  137. <li>extensions.PageSpeed.beacon.minimal.enabled = <b style="color: blue">true</b></li>
  138. </ul>
  139. </body></html>
  140. <?php
  141. exit;
  142. }
  143. header('HTTP/1.0 204 Data accepted');