~ubuntu-branches/ubuntu/intrepid/moin/intrepid-updates

« back to all changes in this revision

Viewing changes to wiki/htdocs/applets/FCKeditor/editor/filemanager/browser/mcpuk/connectors/php/Commands/helpers/progress.cgi

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-02-14 16:09:24 UTC
  • mfrom: (0.2.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20060214160924-fyrx3gvknzqvt4vj
Tags: 1.5.2-1ubuntu1
Drop python2.3 package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -w
2
 
 
3
 
# PHP File Uploader with progress bar Version 1.43      
4
 
# Copyright (C) Raditha Dissanyake 2003,2004
5
 
# http://www.raditha.com
6
 
 
7
 
# Licence:
8
 
# The contents of this file are subject to the Mozilla Public
9
 
# License Version 1.1 (the "License"); you may not use this file
10
 
# except in compliance with the License. You may obtain a copy of
11
 
# the License at http://www.mozilla.org/MPL/
12
 
13
 
# Software distributed under the License is distributed on an "AS
14
 
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15
 
# implied. See the License for the specific language governing
16
 
# rights and limitations under the License.
17
 
18
 
# The Initial Developer of the Original Code is Raditha Dissanayake.
19
 
# Portions created by Raditha are Copyright (C) 2003,2004
20
 
# Raditha Dissanayake. All Rights Reserved.
21
 
22
 
# Portions contributed by Orest Kinasevych are Copyright (C)
23
 
# 2003 Kinasevych Saj
24
 
 
25
 
#
26
 
# CHANGES
27
 
# 1.00 
28
 
#   No longer uses cookies. This has two major benefits; the first
29
 
#   being that minority of users who do not like cookies are not 
30
 
#   inconvinienced. Secondly there is one less dependecy and this 
31
 
#   would lead to a smoother setup for most people.
32
 
#   (if you want to use cookies look at the contrib folder)
33
 
#
34
 
# 1.02
35
 
#   added a cache control header in version 1.02
36
 
#
37
 
# 1.10
38
 
#   Added a more detailed progress bar in version 
39
 
#
40
 
# 1.42
41
 
#   The organization of the temporary files have been improved so to
42
 
#   make is easier to clean up after abandoned uploads. (as suggested
43
 
#   by Igor Kryltsov)
44
 
#
45
 
#
46
 
        
47
 
 
48
 
 
49
 
use CGI;
50
 
use Fcntl qw(:DEFAULT :flock);
51
 
 
52
 
#use Carp;              
53
 
# Carp is only needed if you want debugging. Uncomment the above line
54
 
# if you comment out any of the carp statements in the body of the 
55
 
# script.
56
 
 
57
 
 
58
 
#
59
 
# the most obvious issue that we will face is file locking.
60
 
# if two threads read and write from the same file at the same
61
 
# time only one of them will be allowed to finish the operation.
62
 
# Since we are storing our temporary data in a file we are likely to
63
 
# run into that exact same problem.
64
 
# We can't overcome it but we can make sure the progress bar does
65
 
# not display junk when that happens.
66
 
#
67
 
#
68
 
# status codes = 0-uploading, 1-started, 2- complete
69
 
 
70
 
 
71
 
$query = new CGI();
72
 
$sessionid = $query->param('sessionid');
73
 
$sessionid =~ s/[^a-zA-Z0-9]//g;  # santized as suggested by Terrence Johnson.
74
 
 
75
 
$iTotal = $query->param('iTotal');
76
 
$iRead = $query->param('iRead');
77
 
$status =  $query->param('iStatus');
78
 
 
79
 
 
80
 
##
81
 
# The code that deals with calculating elapsed time, time remaining 
82
 
# and upload speed were contributed by Orest Kinasevych 
83
 
##
84
 
 
85
 
##
86
 
# Get values assigned for current time and upload start time
87
 
##
88
 
#$dtnow = $query->param('dtnow'); # assign value for current time
89
 
$dtnow=time;
90
 
$dtstart = $query->param('dtstart'); # assign value for upload start time
91
 
##
92
 
 
93
 
 
94
 
#carp "$dtnow  $dtstart";
95
 
 
96
 
$thisUrl = $query->url;
97
 
 
98
 
##
99
 
# Elapsed time
100
 
# Calculate elapsed time and format for display
101
 
##
102
 
$dtelapsed = $dtnow - $dtstart;
103
 
$dtelapsed_sec = ($dtelapsed % 60); # gets number of seconds
104
 
$dtelapsed_min = ((($dtelapsed - $dtelapsed_sec) % 3600) / 60); # gets number of minutes
105
 
$dtelapsed_hours = (((($dtelapsed - $dtelapsed_sec) - ($dtelapsed_min * 60)) % 86400) / 3600);
106
 
# gets number of hours; assuming that we won't be going into days!
107
 
if ($dtelapsed_sec < 10) { $dtelapsed_sec = "0$dtelapsed_sec"; } # append leading zero
108
 
if ($dtelapsed_min < 10) { $dtelapsed_min = "0$dtelapsed_min"; } # append leading zero
109
 
if ($dtelapsed_hours < 10) { $dtelapsed_hours = "0$dtelapsed_hours"; } # append leading zero
110
 
$dtelapsedf = "$dtelapsed_hours:$dtelapsed_min:$dtelapsed_sec"; # display as 00:00:00
111
 
##
112
 
 
113
 
##
114
 
# Upload speed
115
 
##
116
 
$bSpeed = 0; # if not yet determined
117
 
if ($dtelapsed > 0) # avoid divide by zero errors
118
 
{
119
 
        $bSpeed = $iRead / $dtelapsed; # Bytes uploaded / Seconds elapsed = Bytes/Second speed
120
 
        $bitSpeed = $bSpeed * 8; # bps
121
 
        $kbitSpeed = $bitSpeed / 1000; # Kbps
122
 
}
123
 
else
124
 
{
125
 
        $kbitSpeed = $bSpeed; # just pass the zero value
126
 
}
127
 
$bSpeedf = sprintf("%d",$kbitSpeed); # remove decimals
128
 
 
129
 
 
130
 
##
131
 
# Est remaining time
132
 
# Calculate remaining time based on upload speed so far
133
 
##
134
 
 
135
 
$bRemaining = $iTotal - $iRead; # Total size - amount uploaded = amount remaining
136
 
$dtRemaining = 0;
137
 
if ($bSpeed > 0) {
138
 
        # Bytes remaining / Bytes/Second = Seconds 
139
 
        $dtRemaining = $bRemaining / $bSpeed;
140
 
}
141
 
$dtRemaining = sprintf("%d",$dtRemaining); # remove decimals
142
 
$dtRemaining_sec = ($dtRemaining % 60); # gets number of seconds
143
 
$dtRemaining_min = ((($dtRemaining - $dtRemaining_sec) % 3600) / 60); # gets number of minutes
144
 
$dtRemaining_hours = (((($dtRemaining - $dtRemaining_sec) - ($dtRemaining_min * 60)) % 86400) / 3600); # gets number of hours; assuming that we won't be going into days!
145
 
if ($dtRemaining_sec < 10)
146
 
{
147
 
        # append leading zero
148
 
        $dtRemaining_sec = "0$dtRemaining_sec";
149
 
}
150
 
if ($dtRemaining_min < 10)
151
 
{
152
 
        # append leading zero
153
 
        $dtRemaining_min = "0$dtRemaining_min";
154
 
}
155
 
if ($dtRemaining_hours < 10)
156
 
{
157
 
        # append leading zero
158
 
        $dtRemaining_hours = "0$dtRemaining_hours";
159
 
}
160
 
$dtRemainingf = "$dtRemaining_hours:$dtRemaining_min:$dtRemaining_sec"; # display as 00:00:00
161
 
 
162
 
##
163
 
# The values for iStatus are
164
 
#       0 - in progress
165
 
#       1 - New upload
166
 
#       2 - Complete
167
 
##
168
 
 
169
 
#carp "iTotal = $iTotal, iRead = $iRead, status = $status, sessionId = $sessionid";
170
 
 
171
 
require("./header.cgi");
172
 
 
173
 
 
174
 
sub readFlength()
175
 
{
176
 
        
177
 
        if(open (STAT, $monitor_file))
178
 
        {
179
 
                sysopen(STAT,  $monitor_file, O_RDONLY)
180
 
                                or die "can't open numfile: $!";
181
 
                $ofh = select(STAT); $| = 1; select ($ofh);
182
 
                $iTotal = <STAT>;
183
 
 
184
 
                #carp "trying to read the stuff in $iTotal";
185
 
                if(defined($iTotal) && $iTotal ne "")
186
 
                {
187
 
                        return 1;
188
 
                
189
 
                }
190
 
                else
191
 
                {
192
 
                        return 0;
193
 
                }
194
 
        } 
195
 
        return 0;
196
 
        
197
 
}
198
 
 
199
 
##
200
 
# many thanx to Terrence Johnson who pointed out the fact that i should have added 
201
 
# cache control header.
202
 
##
203
 
 
204
 
print "Pragma: no-cache\n";
205
 
print "Content-type: text/xml\n\n ";
206
 
 
207
 
if($status == 1)
208
 
{
209
 
        #new upload starting
210
 
        show_starting();
211
 
}
212
 
elsif($status ==0)
213
 
{
214
 
        ##
215
 
        # in progress
216
 
        # we will try to read in the total size of data to be transfered from the
217
 
        # shared file. It will also tell us how much data has been transfered upto
218
 
        # now.
219
 
        ##
220
 
        $bRead = -s "$post_data_file";
221
 
                
222
 
        if(defined $bRead)
223
 
        {
224
 
                # We have  been able to read in it from the file.
225
 
                $percent = $bRead * 100 / $iTotal;
226
 
                $iRead=$bRead;
227
 
                
228
 
        }
229
 
        else
230
 
        {
231
 
                &show_error();
232
 
                exit();
233
 
        }
234
 
 
235
 
        #
236
 
        # division results in truncation errors at times so don't compare percentage
237
 
        # There have been occaisional reports of the progress bar showing 100% but not
238
 
        # disappearing even after file upload has been completed.
239
 
        #
240
 
        # Nils Menrad came up with the solution which is to modify the end of upload
241
 
        # test.
242
 
        #
243
 
        if((($iTotal == $bRead) && $bRead != 0) || $bRead>$iTotal) 
244
 
        {
245
 
                if($status == 1 && -e "$signal_file")
246
 
                {
247
 
                        $bRead=0;
248
 
                        $status=0;
249
 
                        &get_last_values();
250
 
                }
251
 
                else
252
 
                {
253
 
                        show_complete();
254
 
                        unlink $monitor_file;
255
 
                        unlink $post_data_file;
256
 
                        unlink $signal_file;
257
 
                        
258
 
                        exit;
259
 
                }
260
 
        }
261
 
        else
262
 
        {
263
 
                $kachal = "$bRead , $iTotal";
264
 
        }
265
 
 
266
 
 
267
 
        &make_progress_bar();
268
 
        exit;
269
 
}
270
 
else 
271
 
{
272
 
        show_complete();
273
 
}
274
 
 
275
 
#
276
 
# Since the progress bar is in html, so it needs to refresh itself periodicaly to
277
 
# obtain new values. The refresh url with the query string is generated by this 
278
 
# function.
279
 
 
280
 
sub make_url
281
 
{
282
 
 
283
 
        #print "Content-type: text/html\n\n ";
284
 
        #print "hellow $iTotal $iStatus $sessionid $iRead <br>\n" ;
285
 
 
286
 
        ##
287
 
        $url = "$thisUrl?iTotal=$iTotal&iRead=$iRead&iStatus=$status&sessionid=$sessionid&dtnow=$dtnow&dtstart=$dtstart";
288
 
        ##
289
 
        $url =~ s/\n//;
290
 
        
291
 
        return $url;
292
 
 
293
 
}
294
 
 
295
 
sub make_progress_bar
296
 
{
297
 
        $url = make_url();
298
 
        
299
 
        print <<__PART1__;
300
 
        <UploadProgress sessionID="$sessionid">
301
 
                <RefreshURL><![CDATA[$url]]></RefreshURL>
302
 
                <TotalBytes>$iTotal</TotalBytes>
303
 
                <ReadBytes>$iRead</ReadBytes>
304
 
                <Status>$status</Status>
305
 
                <Speed>$bSpeedf</Speed>
306
 
                <TimeRemaining>$dtRemainingf</TimeRemaining>
307
 
                <TimeElapsed>$dtelapsedf</TimeElapsed>
308
 
        </UploadProgress>
309
 
__PART1__
310
 
 
311
 
}
312
 
 
313
 
 
314
 
sub show_complete
315
 
{
316
 
        
317
 
        $status=2;
318
 
        $url = make_url();
319
 
 
320
 
        print <<__PART2__;
321
 
        <UploadProgress sessionID="$sessionid">
322
 
                <RefreshURL><![CDATA[$url]]></RefreshURL>
323
 
                <TotalBytes>$iTotal</TotalBytes>
324
 
                <ReadBytes>$iRead</ReadBytes>
325
 
                <Status>$status</Status>
326
 
                <Speed>$bSpeedf</Speed>
327
 
                <TimeRemaining>0</TimeRemaining>
328
 
                <TimeElapsed>$dtelapsedf</TimeElapsed>
329
 
        </UploadProgress>
330
 
__PART2__
331
 
 
332
 
}
333
 
 
334
 
sub show_starting
335
 
{
336
 
        #carp "starting";
337
 
        if(readFlength() == 1)
338
 
        {
339
 
                $status=0;
340
 
        }
341
 
        $url = make_url();
342
 
 
343
 
        print <<__PART2__;
344
 
        <UploadProgress sessionID="$sessionid">
345
 
                <RefreshURL><![CDATA[$url]]></RefreshURL>
346
 
                <TotalBytes>$iTotal</TotalBytes>
347
 
                <ReadBytes>$iRead</ReadBytes>
348
 
                <Status>$status</Status>
349
 
                <Speed>$bSpeedf</Speed>
350
 
                <TimeRemaining>$dtRemainingf</TimeRemaining>
351
 
                <TimeElapsed>$dtelapsedf</TimeElapsed>
352
 
        </UploadProgress>
353
 
__PART2__
354
 
}
355
 
 
356
 
sub show_error
357
 
{
358
 
        $url = make_url();
359
 
        print <<__PART2__;
360
 
        <UploadProgress sessionID="$sessionid">
361
 
                <RefreshURL><![CDATA[$url]]></RefreshURL>
362
 
                <TotalBytes>$iTotal</TotalBytes>
363
 
                <ReadBytes>$iRead</ReadBytes>
364
 
                <Status>-1</Status>
365
 
                <Speed>$bSpeedf</Speed>
366
 
                <TimeRemaining>$dtRemainingf</TimeRemaining>
367
 
                <TimeElapsed>$dtelapsedf</TimeElapsed>
368
 
        </UploadProgress>
369
 
__PART2__
370
 
 
371
 
}
372
 
 
373
 
# this function may not return;
374
 
sub get_last_values()
375
 
{
376
 
        if($status == 1)
377
 
        {
378
 
                show_starting();
379
 
                exit;
380
 
        }
381
 
        else
382
 
        {
383
 
 
384
 
                if($status == 2)
385
 
                {
386
 
 
387
 
                        
388
 
                        show_complete();
389
 
                        exit;
390
 
                }
391
 
                else
392
 
                {
393
 
 
394
 
                        #
395
 
                        # we have done everything possible to try to retrieve the data
396
 
                        # now try to calculate the percentage once again
397
 
                        #
398
 
                        $iTotal = $iTotal;
399
 
                        $bRead = $iRead;
400
 
 
401
 
                        if(defined($iTotal) && $iTotal != 0)
402
 
                        {
403
 
                                $percent = $bRead * 100 / $iTotal;
404
 
                                $kachal="1";
405
 
                        }
406
 
                        else
407
 
                        {
408
 
                                &show_error();
409
 
                                exit;
410
 
                        }
411
 
                }
412
 
        }
413
 
}