Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. require_once('../ajax_asm.php');
  3. // the response function
  4. function verbose( $ok=1,$info=""){
  5. // failure to upload throws 400 error
  6. if( $ok==0 ){
  7. http_response_code(400);
  8. };
  9. die(json_encode(["ok"=>$ok, "info"=>$info]));
  10. };
  11. if( isset( $_GET['DID'] ) ) {
  12. $DID = intval( $_GET['DID'] );
  13. } else {
  14. verbose( 0,"No Device ID Supplied!");
  15. };
  16. // invalid upload
  17. if( empty( $_FILES ) || $_FILES['file']['error'] ){
  18. verbose( 0, "Failed to move uploaded file.[" . $_FILES['file']['error'] . "]" );
  19. };
  20. // upload destination
  21. //$filePath = __DIR__ . DIRECTORY_SEPARATOR . "../../vault/working_dir/uploads";
  22. $filePath = $SYSPath . 'lib/controller/data/uploads';
  23. if( !file_exists($filePath) ){
  24. if (!mkdir($filePath, 0777, true)) {
  25. verbose(0, "Failed to create $filePath");
  26. };
  27. };
  28. $rfn = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
  29. $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
  30. $filePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
  31. // dealing with the chunks
  32. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  33. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
  34. $out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
  35. if( $out ) {
  36. $in = @fopen($_FILES['file']['tmp_name'], "rb");
  37. if( $in ){
  38. while( $buff = fread( $in, 4096 ) ){
  39. fwrite($out, $buff);
  40. };
  41. } else {
  42. verbose(0, "Failed to open input stream");
  43. };
  44. @fclose($in);
  45. @fclose($out);
  46. @unlink($_FILES['file']['tmp_name']);
  47. } else {
  48. verbose(0, "Failed to open output stream");
  49. };
  50. // check if file was uploaded
  51. if (!$chunks || $chunk == $chunks - 1) {
  52. $MD5 = md5_file( "{$filePath}.part" );
  53. $Dest = $SYSPath . 'lib/controller/data/uploads/'.$MD5;
  54. rename("{$filePath}.part", $Dest );
  55. $QS = "INSERT INTO `lrf_uploads`( `sensor_id`, `ts`, `status`, `md5`, `file`, `issue_reset`) VALUES({$DID}, strftime('%s'), -1, '{$MD5}', '{$rfn}', 0 ) ON CONFLICT(`md5`) DO UPDATE SET `status`=-1, `ts`=strftime('%s'), `issue_reset`=0";
  56. $DATABASE->Query( $QS );
  57. //if( createNewTask( 3, $fileName, 'Handle Upload File' ) == 0 ){
  58. // unlink( $filePath );
  59. // verbose(0, "Failed to create new Task");
  60. //};
  61. };
  62. verbose(1, "Upload OK");
  63. ?>