source: trunk/Modules/SpeedTest/download.php

Last change on this file was 873, checked in by chronos, 4 years ago
  • Modified: Improved code format.
  • Property svn:executable set to *
File size: 9.3 KB
Line 
1<?php
2###############################################################################
3## Fancy Speed Test - Easily measure your upload and download speeds
4## Home Page: http://www.brandonchecketts.com/speedtest/
5## Author: Brandon Checketts
6## File: download.php
7## Version: 1.0
8## Date: 2006-02-06
9## Purpose: Download the data, display graphs that move as the download
10## progresses. Use JavaScript to get the dynamic content.
11###############################################################################
12
13require("common.php");
14ReadConfig(dirname(__FILE__)."/speedtest.cfg");
15
16
17## Make sure this user is allowed to use the test
18$remote_addr = $_SERVER['REMOTE_ADDR'];
19$disallow = $config->{'general'}->{'disallow'};
20$allow = $config->{'general'}->{'allow'};
21if ( $allow && (! preg_match("/$allow/",$remote_addr)) ) {
22 include("unallowed.html");
23 exit;
24} elseif ( $disallow && preg_match("/$disallow/", $remote_addr) ) {
25 include("unallowed.html");
26 exit;
27}
28
29
30
31## Figure out how many bytes to download/upload depending on if we are using
32## auto_size or not. If using auto_size, then determine sizes based
33## on initial results
34$config_auto_size = $config->{'general'}->{'auto_size'};
35if ($config_auto_size) {
36 ## We're using the auto_size functionality
37 if ( isset($_GET['auto_size']) && $_GET['auto_size']) {
38 ## Intial test is done. Set down/upload sizes to the same as
39 ## our initial measured speeds. That way the test should take
40 ## about 8 seconds for each test (up/down) making it about a
41 ## 16 second test
42 $down_kbytes = $_GET['downspeed'];
43 $up_kbytes = $_GET['upspeed'];
44 } else {
45 ## Initial test using auto_size
46 $down_kbytes = $config->{'download'}->{'initial_kbytes'};
47 $up_kbytes = $config->{'upload'}->{'initial_kbytes'};
48 }
49} else {
50 ## auto_size is off. Just to the default sizes
51 $down_kbytes = $config->{'download'}->{'default_kbytes'};
52 $up_kbytes = $config->{'upload'}->{'default_kbytes'};
53}
54
55
56## Make sure sizes are below our configured limits
57if ($down_kbytes > $config->{'download'}->{'max_kbytes'}) {
58 $down_kbytes = $config->{'download'}->{'max_kbytes'};
59}
60if ($up_kbytes > $config->{'upload'}->{'max_kbytes'}) {
61 $up_kbytes = $config->{'upload'}->{'max_kbytes'};
62}
63
64if ($config->{'upload'}->{'skip_upload'}) {
65 $up_kbytes = 0;
66}
67
68## Calculate number of loops for up/down, etc
69$each_chunk = 50;
70$progress_bar_width="400";
71$reloads = $down_kbytes / $each_chunk;
72$increment = $progress_bar_width / $reloads;
73$download_progress_bar_increment = $increment;
74
75$reloads = $up_kbytes / $each_chunk;
76$increment = $progress_bar_width / $reloads;
77$upload_progress_bar_increment = $increment / 5;
78
79
80$pretty_version = $config->{'general'}->{'pretty_version'};
81?>
82
83<html>
84<head>
85<title><?php print $config->{'general'}->{'page_title'}; ?> - Fancy Speed Test</title>
86<meta http-equiv="Expires" CONTENT="Fri, Jan 1 1980 00:00:00 GMT" />
87<meta http-equiv="Pragma" CONTENT="no-cache" />
88<meta http-equiv="Cache-Control" CONTENT="no-cache" />
89<link rel="stylesheet" href="style.css" />
90</head>
91<body>
92
93<?php
94if (file_exists("header.html")) {
95 ## Include "header.html" for a custom header, if the file exists
96 include("header.html");
97} else {
98 ## Else just print a plain header
99 print "<br /><br /><br /><br />\n";
100}
101?>
102<div id="speedtest_contents">
103
104<?php
105if ( ($config_auto_size) && (! isset($_GET['auto_size'])) ) {
106 ## auto_size is performing the initial, small test
107 print "<div>Calculating appropriate file sizes for testing</div>\n";
108 ob_flush();
109} else {
110?>
111
112<div id="download_message">
113 <center>
114 Conducting Download Test (<?php echo $down_kbytes; ?> kb)
115 <div style="border: 1px solid black; width: <?php echo $progress_bar_width; ?>" align="left" id="download_bar_div">
116 <img src="<?php echo $config->{'general'}->{'image_path'}; ?>bar.gif" width="0;" height="20px;" id="download_bar" />
117 </div>
118 </center>
119</div>
120
121<div id="upload_message" style="visibility: hidden; display:none;">
122 <center>
123 <br /><br />
124 Conducting Upload Test (<?php echo $up_kbytes; ?> kb)
125 <div style="border: 1px solid black; width: <?php echo $progress_bar_width; ?>" align="left" id="upload_bar_div">
126 <img src="<?php echo $config->{'general'}->{'image_path'}; ?>bar.gif" width="0;" height="20px;" id="upload_bar" />
127 </div>
128 </center>
129</div>
130</center>
131<?php } ?>
132
133<div id="speedtest_credits">
134 Powered by <a title="Brandon Checketts Fancy Source Speedtest" href="http://www.brandonchecketts.com/speedtest/" target="_new">Fancy Speed Test</a>
135</div>
136
137</div>
138
139<?php if (file_exists("footer.html")) { include("footer.html"); } ?>
140
141<?php /* Begin JavaScript functions that we'll need */ ?>
142<script language="javascript">
143<!--
144function StartUpload() {
145 uploadDiv = document.getElementById('upload_message');
146 if (uploadDiv) {
147 uploadDiv.style.visibility='visible';
148 uploadDiv.style.display='block';
149 <?php if ($pretty_version) { ?>
150 setInterval("IncrementUploadBar()",200);
151 <?php } ?>
152 }
153}
154
155download_bar_current_width=0;
156
157
158function IncrementDownloadBar() {
159 download_barElement = document.getElementById('download_bar');
160 if (download_barElement) {
161 download_bar_current_width += <?php echo $download_progress_bar_increment; ?>;
162 if (download_bar_current_width <= <?php echo $progress_bar_width; ?>) {
163 download_barElement.style.width = download_bar_current_width +"px";
164 }
165 }
166}
167
168<?php
169## This doesn't really measure anything to do with the upload
170## It just increments the bar every second to give it some interactivity
171## I've considered actually querying the server to figure out the status
172## but that would just add extra overhead and make the test less accurate
173## So this appears to be the best way for now
174?>
175upload_bar_current_width=0;
176function IncrementUploadBar() {
177 upload_barElement = document.getElementById('upload_bar');
178 if (upload_barElement) {
179 upload_bar_current_width += <?php echo $upload_progress_bar_increment; ?>;
180 if (upload_bar_current_width < <?php echo $progress_bar_width; ?>) {
181 upload_barElement.style.width = upload_bar_current_width +"px";
182 }
183 }
184}
185
186function CompleteDownloadBar() {
187 download_barElement = document.getElementById('download_bar');
188 if (download_barElement) {
189 download_barElement.style.width = "100%";
190 }
191}
192
193
194// -->
195</script>
196
197
198<form id="upload_test_form" name="upload_test" method="POST" action="<?php echo $config->{'general'}->{'upload_url'}; ?>">
199<input type="hidden" id="upload_data" name="upload_data" value="" />
200
201<script language="javascript">
202<!--
203dataElement = document.getElementById('upload_data');
204
205<?php /* Start the timer */ ?>
206time = new Date();
207starttime = time.getTime();
208
209<?php
210
211if ($pretty_version) {
212print "
213//-->
214</script>
215";
216}
217
218
219 ## Read some stuff from our payload file
220 $data_file = "payload.js";
221 $fd = fopen ($data_file, "r");
222 $data = fread ($fd, $each_chunk * 1024);
223
224 ## Download $extra_down_kbytes first as junk
225 $extra_down_kbytes = $down_kbytes - $up_kbytes;
226 $total_kbytes = 0;
227 while ($total_kbytes <= $extra_down_kbytes) {
228 if ($pretty_version) {
229 print "
230<script language=\"javascript\">
231<!--
232dataElement.value=\"$data\";
233IncrementDownloadBar();
234//-->
235</script>
236";
237 } else {
238 print "dataElement.value=\"$data\";\n";
239 }
240 $total_kbytes += $each_chunk;
241 }
242 if (!$pretty_version) {
243 print "dataElement.value=\"$data\";";
244 print "CompleteDownloadBar();\n";
245 } else {
246 print "
247<script language=\"javascript\">
248<!--
249//-->
250</script>
251 ";
252 }
253
254 ## Now, download the remaining bytes ($up_kbytes) and save it into a
255 ## form variable that we will post back to test the upload speed
256 while ($total_kbytes <= $down_kbytes) {
257 if ($pretty_version) {
258 print "
259<script language=\"javascript\">
260<!--
261dataElement.value = dataElement.value + \"$data\";
262IncrementDownloadBar();
263//-->
264</script>
265";
266 } else {
267 print "dataElement.value = dataElement.value + \"$data\";\n";
268 }
269 $total_kbytes += $each_chunk;
270 }
271
272 if (!$pretty_version) {
273 #print "\";";
274 } else {
275 print "
276<script language=\"javascript\">
277<!--
278";
279 }
280?>
281 CompleteDownloadBar();
282
283 time = new Date();
284 endtime = time.getTime();
285 if (endtime == starttime)
286 {downloadtime = 0
287 } else {
288 downloadtime = (endtime - starttime)/1000;
289 }
290 <?php if (! $config->{'upload'}->{'skip_upload'}){ ?> StartUpload(); <?php } ?>
291
292 down_size = <?php echo $total_kbytes; ?>;
293 downspeed = down_size/downloadtime;
294 downspeed = (Math.round((downspeed*8)*10*1.024))/10;
295
296 formElement = document.getElementById('upload_test_form');
297
298<?php
299if ($config_auto_size && (! isset($_GET['auto_size'])) ) {
300 $params_auto_size = "&auto_size=1";
301} else {
302 $params_auto_size = "";
303}
304if ($config->{'upload'}->{'skip_upload'} && (! $params_auto_size)) {
305 $next_url = $config->{'general'}->{'base_url'}."results.php";
306} else {
307 $next_url = $config->{'general'}->{'upload_url'};
308}
309
310?>
311 formElement.action = '<?php echo $next_url; ?>?downspeed=' + downspeed + '&downtime=' + downloadtime + '&downsize=' + down_size + '<?php echo $params_auto_size; ?>';
312
313 document.forms[0].submit();
314
315// -->
316</script>
317
318</form>
319</body>
320</html>
Note: See TracBrowser for help on using the repository browser.