PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dist/webpagetest/www/getLocations.php

http://webpagetest.googlecode.com/
PHP | 199 lines | 157 code | 25 blank | 17 comment | 33 complexity | 76a8129fedc27fb5ded647b2bc4018fc MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-3.0, LGPL-3.0, MIT, BSD-3-Clause, ISC, LGPL-2.1
  1. <?php
  2. include 'common.inc';
  3. $remote_cache = array();
  4. // load the locations
  5. $locations = &LoadLocations();
  6. // get the backlog for each location
  7. foreach( $locations as $id => &$location )
  8. {
  9. if (strlen($location['relayServer']) && strlen($location['relayLocation'])) {
  10. $location['PendingTests'] = GetRemoteBacklog($location['relayServer'], $location['relayLocation']);
  11. } else {
  12. $location['PendingTests'] = GetBacklog($location['localDir'], $location['location']);
  13. }
  14. // strip out any sensitive data
  15. unset($location['localDir']);
  16. }
  17. // kick out the data
  18. if( $_REQUEST['f'] == 'json' )
  19. {
  20. }
  21. else
  22. {
  23. header ('Content-type: text/xml');
  24. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  25. echo "<?xml-stylesheet type=\"text/xsl\" encoding=\"UTF-8\" href=\"getLocations.xsl\" version=\"1.0\"?>\n";
  26. echo "<response>\n";
  27. echo "<statusCode>200</statusCode>\n";
  28. echo "<statusText>Ok</statusText>\n";
  29. if( strlen($_REQUEST['r']) )
  30. echo "<requestId>{$_REQUEST['r']}</requestId>\n";
  31. echo "<data>\n";
  32. foreach( $locations as $name => &$location )
  33. {
  34. echo "<location>\n";
  35. echo "<id>$name</id>\n";
  36. foreach( $location as $key => &$value )
  37. if( is_array($value) )
  38. {
  39. echo "<$key>\n";
  40. foreach( $value as $k => &$v )
  41. {
  42. if (htmlspecialchars($v)!=$v)
  43. echo "<$k><![CDATA[$v]]></$k>\n";
  44. else
  45. echo "<$k>$v</$k>\n";
  46. }
  47. echo "</$key>\n";
  48. }
  49. else
  50. {
  51. if (htmlspecialchars($value)!=$value)
  52. echo "<$key><![CDATA[$value]]></$key>\n";
  53. else
  54. echo "<$key>$value</$key>\n";
  55. }
  56. echo "</location>\n";
  57. }
  58. echo "</data>\n";
  59. echo "</response>\n";
  60. }
  61. /**
  62. * Load the location information and extract just the end nodes
  63. *
  64. */
  65. function LoadLocations()
  66. {
  67. $locations = array();
  68. $loc = parse_ini_file('./settings/locations.ini', true);
  69. FilterLocations($loc);
  70. BuildLocations($loc);
  71. if( isset($loc['locations']['default']) )
  72. $default = $loc['locations']['default'];
  73. else
  74. $default = $loc['locations'][1];
  75. $i = 1;
  76. while( isset($loc['locations'][$i]) )
  77. {
  78. $group = &$loc[$loc['locations'][$i]];
  79. if( !$group['hidden'] || $_REQUEST['hidden'] )
  80. {
  81. $label = $group['label'];
  82. if( isset($group['default']) )
  83. $def = $group['default'];
  84. else
  85. $def = $group[1];
  86. $j = 1;
  87. while( isset($group[$j]) )
  88. {
  89. if (array_key_exists($group[$j], $loc)) {
  90. if (!$loc[$group[$j]]['hidden'] || $_REQUEST['hidden']) {
  91. $locations[$group[$j]] = array( 'Label' => $label,
  92. 'location' => $loc[$group[$j]]['location'],
  93. 'Browser' => $loc[$group[$j]]['browser'],
  94. 'localDir' => $loc[$group[$j]]['localDir'],
  95. 'relayServer' => $loc[$group[$j]]['relayServer'],
  96. 'relayLocation' => $loc[$group[$j]]['relayLocation']
  97. );
  98. if( $default == $loc['locations'][$i] && $def == $group[$j] )
  99. $locations[$group[$j]]['default'] = true;
  100. }
  101. }
  102. $j++;
  103. }
  104. }
  105. $i++;
  106. }
  107. return $locations;
  108. }
  109. /**
  110. * Get the backlog for the given directory
  111. *
  112. * @param mixed $dir
  113. */
  114. function GetBacklog($dir, $locationId)
  115. {
  116. $backlog = array();
  117. $userCount = 0;
  118. $lowCount = 0;
  119. $testing = 0;
  120. $idle = 0;
  121. for($i = 1; $i <= 9; $i++)
  122. $backlog["p$i"] = 0;
  123. if (LoadJobQueue($dir, $queue))
  124. {
  125. $userCount = count($queue[0]);
  126. for( $i = 1; $i <= 9; $i++ )
  127. {
  128. $count = count($queue[$i]);
  129. $backlog["p$i"] = $count;
  130. $lowCount += $count;
  131. }
  132. }
  133. $testers = GetTesters($locationId);
  134. if (isset($testers) && is_array($testers) && array_key_exists('testers', $testers)) {
  135. foreach($testers['testers'] as &$tester) {
  136. if( $tester['busy'] )
  137. $testing++;
  138. else
  139. $idle++;
  140. }
  141. }
  142. $backlog['Total'] = $userCount + $lowCount + $testing;
  143. $backlog['HighPriority'] = $userCount;
  144. $backlog['LowPriority'] = $lowCount;
  145. $backlog['Testing'] = $testing;
  146. $backlog['Idle'] = $idle;
  147. return $backlog;
  148. }
  149. /**
  150. * Pull the location information from a remote server
  151. */
  152. function GetRemoteBacklog($server, $remote_location) {
  153. $backlog = array();
  154. global $remote_cache;
  155. $server_hash = md5($server);
  156. // see if we need to populate the cache from the remote server
  157. if (!array_key_exists($server_hash, $remote_cache)) {
  158. $remote = json_decode(json_encode((array)simplexml_load_file("$server/getLocations.php?hidden=1")), true);
  159. if (is_array($remote) && array_key_exists('data', $remote) && array_key_exists('location', $remote['data'])) {
  160. $cache_entry = array();
  161. foreach($remote['data']['location'] as &$location) {
  162. $parts = explode(':', $location['id']);
  163. $id = $parts[0];
  164. $cache_entry[$id] = $location['PendingTests'];
  165. }
  166. $remote_cache[$server_hash] = $cache_entry;
  167. }
  168. }
  169. if (array_key_exists($server_hash, $remote_cache) && array_key_exists($remote_location,$remote_cache[$server_hash])) {
  170. $backlog = $remote_cache[$server_hash][$remote_location];
  171. }
  172. return $backlog;
  173. }
  174. ?>