-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacceptFile.php
More file actions
289 lines (212 loc) · 7.33 KB
/
acceptFile.php
File metadata and controls
289 lines (212 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
function put_the_frag_on_frag_file($F)
{
extract($F);
#Create the next var to compare with the nr of fragments that have been inserted
$next = find_next_frag($F);
if($next == $thisfrag)
{
if (fwrite($fid, $fileStr))
{
file_put_contents($indname, $next+1);
chmod($indname, 0777);
$res .= '<report>Frag '.$thisfrag.' inserted</report>';
#If this was the last fragment, and it was inserted correctly, delete the index and decode the whole file
if($thisfrag == $totalfrag)
{
unlink($indname);
}
}
else
$res .= '</report>Frag '.$thisfrag.' failed. Error writting to file</report>';
}
else echo "</report>Waiting for frag ".$next."</report>";
return $res;
}
function check_filekey_validity($filekey, $db)
{
#$db = $_SESSION['db'];
#$sql = "select * from s3db_file_transfer where filekey='".$filekey."' and expires>='".date('Y-m-d G:i:s')."'";
$sql = "select file_id from s3db_file_transfer where filekey='".$filekey."'";
$db->query($sql, __LINE__, __FILE__);
if($db->next_record())
{
return TRUE;
}
else return FALSE;
}
function put_the_frag_on_file($F)
{
extract($F);
#Create the next var to compare with the nr of fragments that have been inserted
$next = find_next_frag($F);
if($next == $thisfrag)
{
if (fwrite($fid, $fileStr))
{
file_put_contents($indname, $next+1);
chmod($indname, 0777);
$res .= '<report>Frag '.$thisfrag.' inserted</report>';
#If this was the last fragment, and it was inserted correctly, delete the index and decode the whole file
if($thisfrag == $totalfrag)
{
unlink($indname);
}
}
else
$res .= '</report>Frag '.$thisfrag.' failed. Error writting to file</report>';
}
else echo "</report>Waiting for frag ".$next."</report>";
return $res;
}
function find_next_frag($F)
{
extract($F);
if (!is_file($indname))
{$indname = file_put_contents($indname, '0');
$next = '1';
}
else
{
$next = file_get_contents($indname);
}
return $next;
}
function acceptFrag($F)
{
extract($F);
$next = file_get_contents($ind);
$fin = fopen($filelocation, 'a+');
echo base64_decode($fileStr);
#Decide which fragment will go next
if($next!='')
{
if ($next+1!=$thisfrag)
{
echo "Waiting for fragment ".($next+1)."<BR>";
}
else #Means index file corresponds to correct fragment
{
if(fwrite($fin, base64_decode($fileStr)))
{
$next=$next + 1; #do the ind file couting independently from the fragNr
file_put_contents($ind, $next); #increment the fragment on the index file
$percent = $thisfrag*100/$lastfrag;
#$progress = '<IMG SRC="progBar.jpg" WIDTH="'.$next*$FragWidth.'0" HEIGHT="20" BORDER="0" ALT="">'.intval($percent).'%';
echo "Fragment ".$thisfrag." inserted";
echo intval($percent).'%';
#fclose($fin);
return TRUE;
}
else
{echo "Could not write frament ".$thisfrag." to file.";}
}
}
}
function receiveFileFragments($F)
{
extract($F);
$filekey_valid = check_filekey_validity($filekey, $db);
if ($filekey_valid)
{
#Get the information about the file from the table
$file_id = get_entry("file_transfer", "file_id", "filekey", $filekey, $db);
$filesize = get_entry("file_transfer", "filesize", "filekey", $filekey, $db);
$originalname = get_entry("file_transfer", "filename", "filekey", $filekey, $db);
#list($name, $extension) = explode('.', $originalname);
ereg('([A-Za-z0-9]+)\.*([A-Za-z0-9]*)$',$originalname, $tokens);
$name = $tokens[1];
$extension= $tokens[2];
#$name = ereg_replace('.([A-Za-z0-9]*)$', '', $originalname);
if($fragNr=='')
$fragNr = $_REQUEST['fragNr'];
list($thisfrag, $totalfrag) = explode('/', $fragNr);
#Define the folder where these files will be stored
$folder = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'].'/tmps3db/';
$filename = $folder.$file_id.'_'.$thisfrag.'.tmp';
$final = $folder.$file_id.'.'.$extension;
if($fileStr=='')
$fileStr = $_REQUEST['fileStr'];
#decode the fragment right after receiving them if they were encoded one at a time
if ($_REQUEST['encode']=='2') {
$fileStr = base64_decode($fileStr);
}
$indname = $folder.'ind'.$file_id.'.txt';
$fid = fopen($filename, 'a+');
chmod($filename, 0777);
if($fragNr=='' || $fileStr=='')
{ echo "<report>Syntax: <BR>";
echo "<filekey>...</filekey><BR>";
echo "<fragNr>[this frag]/[total nr of frags]</fragNr><BR>";
echo "<fileStr>(hexadecimal encoded fragment string)</fileStr><BR>";
echo "</report>";
}
else
{if($filesize!='' && filesize($filename)==$filesize) echo "<report>This file was already uploaded</report>";
elseif ($thisfrag > $totalfrag)
echo "<report>Too many fragments</report>";
else
{
$F = compact('thisfrag', 'fileStr', 'totalfrag', 'fid', 'filename', 'indname', 'final');
#echo '<pre>';print_r($F);
echo put_the_frag_on_file($F);
#echo $thisfrag.' out of'.$totalfrag;
#When the last fragment is in, decode the entire file
if($thisfrag == $totalfrag)
{
#find all the fragment files in the folder, write them in the final file
for($i=1;$i<=$totalfrag;$i++)
{
$fragment_file_name = $folder.$file_id.'_'.$i.'.tmp';
if(is_file($fragment_file_name))
{
if(file_put_contents($folder.$file_id.'.tmp', file_get_contents($fragment_file_name),FILE_APPEND))
unlink($fragment_file_name);
}
else
{return "Fragment ".$i." is missing, please upload it again.";
exit;
}
}
#decode the file in the end, if the user requested it
if($_REQUEST['encode']!='2') #if encoding is to be done after getting the file.Empty is default to be backward compatible; other wise, the data on the tmp file is already decoded
{
$fullStr = file_get_contents($folder.$file_id.'.tmp');
$decodedStr = base64_decode($fullStr);
if (!file_put_contents($folder.$file_id.'.tmp', $decodedStr)) {
echo "Failed accepting the file. Please try again or encode file one fragment at a time before sending.";
exit;
}
}
copy($folder.$file_id.'.tmp', $final);
chmod($final, 0777);
if(is_file($folder.$file_id.'.tmp')) #delete the temporary key
{
unlink($folder.$file_id.'.tmp');
}
return "<report>Upload Complete</report>";
}
else
{
fclose($fid);
}
}
}
}
}
function generateAFilekey($F)
{#this function accepts user input with key and filename and puts an entry on the file_transfer table
extract($F);
$filekey = random_string("12");
$file_id = str_replace (array('.', ' '),'', microtime());
#Insert the entry on file_trasnfer table
$day = date('d')+1;
$expires = date('Y-m-d G:i:s', mktime(date('G'),date('i'),date('s'), date('m'), $day, date('Y')));
$file = array('file_id'=>$file_id, 'filename'=>$filename, 'filesize'=>$filesize, 'expires'=>$expires, 'filekey'=> $filekey, 'status'=>'empty');
list($name, $extension) = explode('.', $filename);
#Insert the file_id in the file_transfer_table to be associated with the output file
$transfer = insert_file_for_transfer($file, $user_id, $db);
if($transfer)
return $filekey;
}
?>