PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/acceptFile.php

https://code.google.com/p/s3db/
PHP | 291 lines | 187 code | 79 blank | 25 comment | 39 complexity | 31df92a1ba50f6826c3ee4f2f4657420 MD5 | raw file
  1. <?php
  2. function put_the_frag_on_frag_file($F)
  3. {
  4. extract($F);
  5. #Create the next var to compare with the nr of fragments that have been inserted
  6. $next = find_next_frag($F);
  7. if($next == $thisfrag)
  8. {
  9. if (fwrite($fid, $fileStr))
  10. {
  11. file_put_contents($indname, $next+1);
  12. chmod($indname, 0777);
  13. $res .= 'Frag '.$thisfrag.' inserted. ';
  14. #If this was the last fragment, and it was inserted correctly, delete the index and decode the whole file
  15. if($thisfrag == $totalfrag)
  16. {
  17. unlink($indname);
  18. }
  19. }
  20. else
  21. $res .= '</report>Frag '.$thisfrag.' failed. Error writting to file</report>';
  22. }
  23. else echo "</report>Waiting for frag ".$next."</report>";
  24. return $res;
  25. }
  26. function check_filekey_validity($filekey, $db)
  27. {
  28. #$db = $_SESSION['db'];
  29. #$sql = "select * from s3db_file_transfer where filekey='".$filekey."' and expires>='".date('Y-m-d G:i:s')."'";
  30. $sql = "select file_id from s3db_file_transfer where filekey='".$filekey."'";
  31. $db->query($sql, __LINE__, __FILE__);
  32. if($db->next_record())
  33. {
  34. return TRUE;
  35. }
  36. else return FALSE;
  37. }
  38. function put_the_frag_on_file($F)
  39. {
  40. extract($F);
  41. #Create the next var to compare with the nr of fragments that have been inserted
  42. $next = find_next_frag($F);
  43. if($next == $thisfrag)
  44. {
  45. if (fwrite($fid, $fileStr))
  46. {
  47. file_put_contents($indname, $next+1);
  48. chmod($indname, 0777);
  49. $msg = 'Frag '.$thisfrag.' inserted. ';
  50. #If this was the last fragment, and it was inserted correctly, delete the index and decode the whole file
  51. if($thisfrag == $totalfrag)
  52. {
  53. unlink($indname);
  54. }
  55. }
  56. else
  57. $msg = 'Frag '.$thisfrag.' failed. Error writting to file';
  58. }
  59. else {$msg= "Waiting for frag ".$next."";
  60. }
  61. return $msg;
  62. }
  63. function find_next_frag($F)
  64. {
  65. extract($F);
  66. if (!is_file($indname))
  67. {$indname = file_put_contents($indname, '0');
  68. $next = '1';
  69. }
  70. else
  71. {
  72. $next = file_get_contents($indname);
  73. }
  74. return $next;
  75. }
  76. function acceptFrag($F)
  77. {
  78. extract($F);
  79. $next = file_get_contents($ind);
  80. $fin = fopen($filelocation, 'a+');
  81. echo base64_decode($fileStr);
  82. #Decide which fragment will go next
  83. if($next!='')
  84. {
  85. if ($next+1!=$thisfrag)
  86. {
  87. echo "Waiting for fragment ".($next+1)."<BR>";
  88. }
  89. else #Means index file corresponds to correct fragment
  90. {
  91. if(fwrite($fin, base64_decode($fileStr)))
  92. {
  93. $next=$next + 1; #do the ind file couting independently from the fragNr
  94. file_put_contents($ind, $next); #increment the fragment on the index file
  95. $percent = $thisfrag*100/$lastfrag;
  96. #$progress = '<IMG SRC="progBar.jpg" WIDTH="'.$next*$FragWidth.'0" HEIGHT="20" BORDER="0" ALT="">'.intval($percent).'%';
  97. echo "Fragment ".$thisfrag." inserted";
  98. echo intval($percent).'%';
  99. #fclose($fin);
  100. return TRUE;
  101. }
  102. else
  103. {echo "Could not write frament ".$thisfrag." to file.";}
  104. }
  105. }
  106. }
  107. function receiveFileFragments($F)
  108. {
  109. extract($F);
  110. $filekey_valid = check_filekey_validity($filekey, $db);
  111. if ($filekey_valid)
  112. {
  113. #Get the information about the file from the table
  114. $file_id = get_entry("file_transfer", "file_id", "filekey", $filekey, $db);
  115. $filesize = get_entry("file_transfer", "filesize", "filekey", $filekey, $db);
  116. $originalname = get_entry("file_transfer", "filename", "filekey", $filekey, $db);
  117. #list($name, $extension) = explode('.', $originalname);
  118. ereg('([A-Za-z0-9]+)\.*([A-Za-z0-9]*)$',$originalname, $tokens);
  119. $name = $tokens[1];
  120. $extension= $tokens[2];
  121. #$name = ereg_replace('.([A-Za-z0-9]*)$', '', $originalname);
  122. if($fragNr=='')
  123. $fragNr = $_REQUEST['fragNr'];
  124. list($thisfrag, $totalfrag) = explode('/', $fragNr);
  125. #Define the folder where these files will be stored
  126. $folder = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'].'/tmps3db/';
  127. $filename = $folder.$file_id.'_'.$thisfrag.'.tmp';
  128. $final = $folder.$file_id.'.'.$extension;
  129. if($fileStr=='')
  130. $fileStr = $_REQUEST['fileStr'];
  131. #decode the fragment right after receiving them if they were encoded one at a time
  132. if ($_REQUEST['encode']=='2') {
  133. $fileStr = base64_decode($fileStr);
  134. }
  135. $indname = $folder.'ind'.$file_id.'.txt';
  136. $fid = fopen($filename, 'a+');
  137. chmod($filename, 0777);
  138. if($fragNr=='' || $fileStr=='')
  139. { $msg= "Syntax: uploads.php?filekey=[filekey]&fragNr=x/Total&fileStr=[encoded as base 64 fragment string]&encode=1<BR>";
  140. #echo "&lt;filekey&gt;...&lt;/filekey&gt;<BR>";
  141. #echo "&lt;fragNr&gt;[this frag]/[total nr of frags]&lt;/fragNr&gt;<BR>";
  142. #echo "&lt;fileStr&gt;(hexadecimal encoded fragment string)&lt;/fileStr&gt;<BR>";
  143. #echo "</report>";
  144. }
  145. else
  146. {if($filesize!='' && filesize($filename)==$filesize) $msg.="This file was already uploaded";
  147. elseif ($thisfrag > $totalfrag)
  148. $msg= "Too many fragments";
  149. else
  150. {
  151. $F = compact('thisfrag', 'fileStr', 'totalfrag', 'fid', 'filename', 'indname', 'final');
  152. #echo '<pre>';print_r($F);
  153. $msg=put_the_frag_on_file($F);
  154. #echo $thisfrag.' out of'.$totalfrag;
  155. #When the last fragment is in, decode the entire file
  156. if($thisfrag == $totalfrag)
  157. {
  158. #find all the fragment files in the folder, write them in the final file
  159. for($i=1;$i<=$totalfrag;$i++)
  160. {
  161. $fragment_file_name = $folder.$file_id.'_'.$i.'.tmp';
  162. if(is_file($fragment_file_name))
  163. {
  164. if(file_put_contents($folder.$file_id.'.tmp', file_get_contents($fragment_file_name),FILE_APPEND))
  165. unlink($fragment_file_name);
  166. }
  167. else
  168. {return $msg= "Fragment ".$i." is missing, please upload it again.";
  169. }
  170. }
  171. #decode the file in the end, if the user requested it
  172. 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
  173. {
  174. $fullStr = file_get_contents($folder.$file_id.'.tmp');
  175. $decodedStr = base64_decode($fullStr);
  176. if (!file_put_contents($folder.$file_id.'.tmp', $decodedStr)) {
  177. return $msg = "Failed accepting the file. Please try again or encode file one fragment at a time before sending.";
  178. }
  179. }
  180. copy($folder.$file_id.'.tmp', $final);
  181. chmod($final, 0777);
  182. if(is_file($folder.$file_id.'.tmp')) #delete the temporary key
  183. {
  184. unlink($folder.$file_id.'.tmp');
  185. }
  186. return $msg."Upload Complete";
  187. }
  188. else
  189. {
  190. fclose($fid);
  191. }
  192. }
  193. }
  194. }
  195. return ($msg);
  196. }
  197. function generateAFilekey($F)
  198. {#this function accepts user input with key and filename and puts an entry on the file_transfer table
  199. extract($F);
  200. $filekey = random_string("12");
  201. $file_id = str_replace (array('.', ' '),'', microtime());
  202. #Insert the entry on file_trasnfer table
  203. $day = date('d')+1;
  204. $expires = date('Y-m-d G:i:s', mktime(date('G'),date('i'),date('s'), date('m'), $day, date('Y')));
  205. $file = array('file_id'=>$file_id, 'filename'=>$filename, 'filesize'=>$filesize, 'expires'=>$expires, 'filekey'=> $filekey, 'status'=>'empty');
  206. list($name, $extension) = explode('.', $filename);
  207. #Insert the file_id in the file_transfer_table to be associated with the output file
  208. $transfer = insert_file_for_transfer($file, $user_id, $db);
  209. if($transfer)
  210. return $filekey;
  211. }
  212. ?>