PageRenderTime 59ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/planning/lightloader (shows progress)/filestatus.php

https://github.com/jasononeil/OpenRoadFTP
PHP | 155 lines | 91 code | 28 blank | 36 comment | 27 complexity | d7232653e17db70e7d89676e50ce7150 MD5 | raw file
  1. <?php
  2. # PHP File Uploader with progress bar - JSON version
  3. # Based on progress.php, a contrib to Megaupload, by Mike Hodgson.
  4. # Changed for use with AJAX by Tomas Larsson - http://tomas.epineer.se/
  5. # Modified heavily by Jeremy Nicoll for use with JSON, and also added
  6. # code so that the uploaded files will actually WORK like they are
  7. # supposed to when you upload them. Files get their original file name
  8. # once uploaded. Added section so that potentially harmful files could
  9. # not be uploaded.
  10. # Go to www.SeeMySites.net/forum for questions and support.
  11. # Licence:
  12. # The contents of this file are subject to the Mozilla Public
  13. # License Version 1.1 (the "License"); you may not use this file
  14. # except in compliance with the License. You may obtain a copy of
  15. # the License at http://www.mozilla.org/MPL/
  16. #
  17. # Software distributed under this License is distributed on an "AS
  18. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  19. # implied. See the License for the specific language governing
  20. # rights and limitations under the License.
  21. #
  22. // Configurable variables:
  23. $tempFolder = "tempfiles"; //Make sure that this is the same as in upload.cgi.
  24. $moveToFolder = "uploadedfiles"; //Where the files will be moved upon upload, leave blank if you want it to be the same as $tempFolder.
  25. $bad_files = array('exe', 'php', 'php3', 'php4', 'ph3', 'ph4', 'perl', 'cgi', 'bin', 'scr', 'bat', 'pif', 'aps', 'ssi', 'swf', 'js');
  26. // End Configurable variables
  27. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  28. require('JSON.php');
  29. header('Content-Type: text/plain');
  30. if (trim($moveToFolder) == '') {$moveToFolder = $tempFolder;}
  31. $json = new Services_JSON;
  32. $request = $json->decode($GLOBALS['HTTP_RAW_POST_DATA']);
  33. $sessionID = $request->sid;;
  34. $fileName = $request->fileName;
  35. $temp = substr($fileName, strrpos($fileName, '.')+1);
  36. $info_file = "$tempFolder/$sessionID"."_flength";
  37. $data_file = "$tempFolder/$sessionID"."_postdata";
  38. $error_file = "$tempFolder/$sessionID"."_err";
  39. $files = array("_flength","_postdata","_err");
  40. if (in_array($temp, $bad_files) && !file_exists($error_file)) {
  41. $request->status = 'error';
  42. $request->error_msg = 'Bad file extension: ' . $temp . '. Please try uploading another file.';
  43. echo $json->encode($request);
  44. foreach($files as $file) {
  45. @unlink("$tempFolder/$sessionID$file");
  46. }
  47. die;
  48. }
  49. // Removes files in the upload directory that are over 3 hours old, except for index.php
  50. // You probably don't need it, but it might be nice for some people. Uncomment if you need it.
  51. /*if ($handle = opendir('tempfiles')) {
  52. while (false !== ($file = readdir($handle))) {
  53. if (filemtime('tempfiles/'.$file) < time() - 10800 && !is_dir('tempfiles/'.$file) && $file != 'index.php') {
  54. @unlink('tempfiles/'.$file);
  55. }
  56. }
  57. } */
  58. if(file_exists($error_file)) {
  59. $request->status = 'error';
  60. $request->error_msg = file_get_contents($error_file);
  61. foreach($files as $file) {
  62. @unlink("$tempFolder/$sessionID$file");
  63. }
  64. echo $json->encode($request);
  65. die;
  66. }
  67. $percent_done = 0;
  68. $started = true;
  69. if ($fp = @fopen($info_file, "rb")) {
  70. $fd = fread($fp,1000);
  71. fclose($fp);
  72. $total_size = $fd;
  73. } else {
  74. $started = false;
  75. }
  76. if ($started == true) {
  77. $current_size = @filesize($data_file);
  78. $percent_done = intval(($current_size / $total_size) * 100);
  79. }
  80. if ($percent_done >= 100) {
  81. //Removes POST encoding data that is NOT part of the original file.
  82. $handle = fopen("$data_file", "rb");
  83. $fileName = trim(stripslashes(urldecode($fileName)));
  84. $handle2 = fopen($moveToFolder. '/' . $fileName, 'wb');
  85. $file = array();
  86. // load file into array
  87. while (!feof($handle)) {
  88. $file[] = fgets($handle);
  89. }
  90. // remove lines that are from POST data, as well as last \r\n before ending delimiter (the ---------1234...)
  91. $scan_for_headers = true;
  92. for ($i=0; $i < sizeof($file); $i++) {
  93. $tester = strtolower(substr($file[$i], 0, 10));
  94. if (($tester == 'content-ty' || $tester == 'content-di' || $tester == '----------' || $tester == "\r\n" ) && $scan_for_headers) {
  95. if ($tester == '----------') {
  96. $end_of_file = trim($file[$i]) .'--';
  97. }
  98. //remove this stupid line
  99. array_splice($file, $i, 1);
  100. $i--;
  101. if ($tester == "\r\n") $scan_for_headers = false;
  102. } elseif (trim($file[$i]) == $end_of_file) {
  103. array_splice($file, $i, 1);
  104. $file[$i-1] = preg_replace('/\r\n$/', '', $file[$i-1]);
  105. }
  106. }
  107. //write the file
  108. foreach ($file as $str) {
  109. fputs($handle2, $str);
  110. }
  111. fclose($handle);
  112. fclose($handle2);
  113. foreach($files as $file) {
  114. @unlink("$tempFolder/$sessionID$file");
  115. }
  116. $request->status = 'ok';
  117. $request->progress = 'done';
  118. $request->current_size = $total_size;
  119. echo $json->encode($request);
  120. exit;
  121. }
  122. $request->status = 'ok';
  123. $request->progress = $percent_done;
  124. if (!$current_size) $current_size = 0;
  125. $request->current_size = $current_size; // Fix suggested by ASDF
  126. echo $json->encode($request);
  127. ?>