mirror of
				https://github.com/iiab/iiab.git
				synced 2025-03-09 15:40:17 +00:00 
			
		
		
		
	upload2usb/upload2usb.php: using existence of /media to determine if a USB stick is mounted or not for both count and upload location; changing tabs to spaces for the whole file
This commit is contained in:
		
							parent
							
								
									728704b250
								
							
						
					
					
						commit
						a96b46c223
					
				
					 1 changed files with 44 additions and 47 deletions
				
			
		|  | @ -10,27 +10,25 @@ set_exception_handler(function (Throwable $exception) { | |||
|     include ("error.php"); | ||||
| }); | ||||
| 
 | ||||
| 
 | ||||
| //return the first removable USB drive location
 | ||||
| function getTargetUSBDriveLocation () { | ||||
|          // Get the first removal USB drive using
 | ||||
|          // lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" |grep -oP '[^/]MOUNTPOINT="\K[^"]*' -m 1
 | ||||
|          // lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" | cut -d " " -f 4 | cut -d "=" -f 2
 | ||||
|          // Get the count of storage mounted at /media, and error if there is <>1 otherwise return upload path
 | ||||
| 
 | ||||
| 	 # error if 1<>usb sticks are installed 
 | ||||
| 	 $rmv_usb_path_count = shell_exec('lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" | cut -d " " -f 4  | grep media | wc -l'); | ||||
| 	 if ($rmv_usb_path_count == 0) { | ||||
|  	    	throw new RuntimeException('0 USB sticks found. <br/><br/>'); | ||||
| 	 } elseif ($rmv_usb_path_count > 1) { | ||||
| 	    	throw new RuntimeException('More than 1 USB sticks installed. <br/><br/>'); | ||||
| 	 } | ||||
|          # error if 1<>usb sticks are installed
 | ||||
|          $rmv_usb_path_count = shell_exec('lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs | cut -d " " -f 4 | grep "^MOUNTPOINT=\"/media" | wc -l'); | ||||
| 
 | ||||
|          $rmv_usb_path = trim(str_replace('"', '', shell_exec('lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" | cut -d " " -f 4 | cut -d "=" -f 2'))); | ||||
|          if ($rmv_usb_path_count == 0) { | ||||
|                throw new RuntimeException('0 USB sticks found. <br/><br/>'); | ||||
|          } elseif ($rmv_usb_path_count > 1) { | ||||
|                throw new RuntimeException('More than 1 USB sticks installed. <br/><br/>'); | ||||
|          } | ||||
| 
 | ||||
|          $rmv_usb_path = trim(str_replace('"', '', shell_exec('lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs | cut -d " " -f 4 | grep "^MOUNTPOINT=\"/media" | cut -d "=" -f 2'))); | ||||
| 
 | ||||
|          if (empty($rmv_usb_path)) { | ||||
| 	    	throw new RuntimeException('Not able to find USB stick. <br/><br/>'); | ||||
|                throw new RuntimeException('Not able to find USB stick. <br/><br/>'); | ||||
|          } else { | ||||
|                 return $rmv_usb_path . "/"; | ||||
|                return $rmv_usb_path . "/"; | ||||
|          } | ||||
| } | ||||
| 
 | ||||
|  | @ -42,55 +40,55 @@ function getTargetFolderPath ($create_folder_p) { | |||
|          $target_folder_path = $parent_dir . $today_folder_name; | ||||
| 
 | ||||
|          if (!file_exists($target_folder_path) && $create_folder_p) { | ||||
| 	      mkdir($target_folder_path, 0777) or throw new RuntimeException("Not able to create upload directory. <br/>Make sure 'usb_lib_writable_sticks' is set to 'True'. <br/><br/>"); | ||||
|         } | ||||
|         return $target_folder_path; | ||||
|                mkdir($target_folder_path, 0777) or throw new RuntimeException("Not able to create upload directory. <br/>Make sure 'usb_lib_writable_sticks' is set to 'True'. <br/><br/>"); | ||||
|          } | ||||
|          return $target_folder_path; | ||||
| } | ||||
| 
 | ||||
| //return number of files within a specified folder
 | ||||
| function getFileCount ($folder_path) { | ||||
|         return count(glob($folder_path . "/*")); | ||||
|          return count(glob($folder_path . "/*")); | ||||
| } | ||||
| 
 | ||||
| //check if file mimetype is acceptable for upload
 | ||||
| function isFileMimeTypeAcceptable ($file) { | ||||
| 	 $mimetype = strtolower(mime_content_type($file)); | ||||
| 	 $invalid_mimetypes_str = array ("compress", "image/svg+xml", "octet", "text/xml", "xhtml+xml"); | ||||
| 	 foreach ($invalid_mimetypes_str as $invalid_mt_str) { | ||||
| 	 	 if (str_contains($mimetype, $invalid_mt_str)) { | ||||
| 	            error_log('UPLOAD2USB ERROR - MIMETYPE: ' . $mimetype); | ||||
| 		    return false; | ||||
| 		 } | ||||
| 	 } | ||||
| 	 return true; | ||||
|          $mimetype = strtolower(mime_content_type($file)); | ||||
|          $invalid_mimetypes_str = array ("compress", "image/svg+xml", "octet", "text/xml", "xhtml+xml"); | ||||
|          foreach ($invalid_mimetypes_str as $invalid_mt_str) { | ||||
|                if (str_contains($mimetype, $invalid_mt_str)) { | ||||
|                      error_log('UPLOAD2USB ERROR - MIMETYPE: ' . $mimetype); | ||||
|                      return false; | ||||
|                } | ||||
|          } | ||||
|          return true; | ||||
| } | ||||
| 
 | ||||
| //check file content to see if it's unique or not
 | ||||
| function isFileContentUnique ($target_folder_path, $file) { | ||||
| 	 $file_to_upload_md5 = md5_file($file); | ||||
| 	 $usb_dir = array_diff(scandir($target_folder_path), array('..', '.')); | ||||
| 	 foreach ($usb_dir as $dir_file) { | ||||
|  		$dir_file = $target_folder_path . "/" . $dir_file; | ||||
|          $file_to_upload_md5 = md5_file($file); | ||||
|          $usb_dir = array_diff(scandir($target_folder_path), array('..', '.')); | ||||
|          foreach ($usb_dir as $dir_file) { | ||||
|                  $dir_file = $target_folder_path . "/" . $dir_file; | ||||
| 
 | ||||
| 	 	 if (!is_dir($dir_file)) { | ||||
| 		  	 $dir_file_md5 = md5_file($dir_file); | ||||
| 		 	 if ($file_to_upload_md5 == $dir_file_md5) { | ||||
| 		    	     return false; | ||||
| 		 	 } | ||||
| 		 } | ||||
| 	 } | ||||
| 	 return true; 	  | ||||
|                  if (!is_dir($dir_file)) { | ||||
|                        $dir_file_md5 = md5_file($dir_file); | ||||
|                        if ($file_to_upload_md5 == $dir_file_md5) { | ||||
|                              return false; | ||||
|                        } | ||||
|                  } | ||||
|          } | ||||
|          return true; | ||||
| } | ||||
| 
 | ||||
| //return unique filename
 | ||||
| function getUniqueFileName ($target_folder_path, $filename) { | ||||
| 	 $new_filename = $filename; | ||||
| 	 $counter = 1;  | ||||
| 	 while (file_exists($target_folder_path . "/" . $new_filename)) { | ||||
| 	       $counter++; | ||||
| 	       $new_filename = pathinfo($filename,8) . '-'. $counter . "." . pathinfo($filename,4); | ||||
| 	 } | ||||
| 	 return $new_filename;  | ||||
|          $new_filename = $filename; | ||||
|          $counter = 1; | ||||
|          while (file_exists($target_folder_path . "/" . $new_filename)) { | ||||
|                $counter++; | ||||
|                $new_filename = pathinfo($filename,8) . '-'. $counter . "." . pathinfo($filename,4); | ||||
|          } | ||||
|          return $new_filename; | ||||
| } | ||||
| 
 | ||||
| // Check file size - we are not going to check file size for now.
 | ||||
|  | @ -100,5 +98,4 @@ function getUniqueFileName ($target_folder_path, $filename) { | |||
| // }
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ?>
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue