All pastes #472200 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-05-05 12:17 UTC
rendered paste body
<?PHP
	/*
	 * GetDirFromCell
	 * 
	 * Version: 2.0 (incomplete pasted version)
	 * This version was made public the 5th May of 2007, one day after the release of the first one
	 * It can be downloaded from http://utils.eurion.net
	 * 
	 * Download the whole content of a remote folder from a device connected by Bluetooth to the current directory.
	 * (C) 2007 Siegfried-Angel Gevatter Pujals (RainCT) <siggi.gevatter@gmail.com>
	 * 
	 * Fell free to mail to siggi.gevatter@gmail.com about bugs, ideas, support, or to tell me that you like this script
	 * 
	 * This script is Open Source Software licensed under the GNU General Public License
	 * It comes with absolute NO warrany - use it at your own risk!
	 * */

	/*
	 * CONFIGURATION 
	 * */
	
	// MAC Identifier of the device
	$mac = '00:0A:28:61:2F:A5';
	
	// Path of the folder on the remote device
	$path = 'audio/';
	
	// Overwrite existing files?
	$overwrite = false;
	
	// Download all files from $path, included those from subfolders
	$recursive = false;
	
	// Print the data off all files (and folders) when they are found
	$showFound = false;


	/*
	 * MAIN CODE
	 * */
	echo "\n";
	
	$fileArray = array();
	$folderArray = array();
	$ignore = 0;

	function readRemoteFolder($path) {
		GLOBAL $mac, $overwrite, $recursive, $showFound;
		GLOBAL $fileArray, $folderArray, $ignore;
		
		// Check that the path has a slash at end and add if it hasn't, add one
		if(substr($path, -1) !== '/')
			$path = $path.'/';
	
		// Get a list (in XML format) of all files in the directory
		echo 'Retrieving file list from "',$path,'"... ';
		$xml = shell_exec("obexftp -b $mac -l $path 2> /dev/null");
		echo "OK.\n";
	
		// Put it into an array, each line with a different key
		$xmlArray = split("\n", $xml);
		unset($xml);
	
		// Run throught all lines
		foreach($xmlArray AS $value) {
			// Check if the current line is about a file, a folder, or is just crap
			if(substr($value, 0, 6) !== '<file ' && (substr($value, 0, 8) !== '<folder ' || !$recursive)) continue;
			
			// Extract all the data from the current XML line
			// <file name="NAME" size="SIZE" type="TYPE" modified="MODIFIED" user-perm="PERM" />
			// <folder name="NAME" size="SIZE" type="TYPE" modified="MODIFIED" user-perm="PERM" />
			list(, $name, , $size, , $type, , $modified, , $perm) = split('"', $value);
			
			if(substr($value, 0, 6) === '<file ') {
			
				// Print a list of all files found and their data
				if($showFound)
					echo "FILE: Name: $name | Size: $size | Type = $type | Modified: $modified | Permissions: $perm\n";
			
				// If the file doesn't exist or file-overwrite is enabled...
				if(!is_file('./'.$name) || $overwrite) {
					// Add the path of the current file to an array
					$fileArray[] = $path.$name;
				} else 
					$ignore ++;
				
			} else if(substr($value, 0, 8) === '<folder ' && $recursive) {
				
				// Print a list of all folders found and their data
				if($showFound)
					echo "FOLDER: Name: $name | Size: $size | Type = $type | Modified: $modified | Permissions: $perm\n";
				
				// Get all files and folders that are in this folder
				readRemoteFolder($path.$name);
				
			} else 
				continue;
		}
		
		return true;
	
	}	// -END- readRemoteFolder()
	
	readRemoteFolder($path);
	
	// Check if there are files to retrieve
	if(!is_array($fileArray)) {
		if($ignore < 0) {
			die('All files were ignored.'."\n");
		} else
			die('No files were found.'."\n");
	}
	
	// Print the number of files found
	if($overwrite) {
		echo 'Files found:',count($fileArray),".\n";
	} else {
		$fileCount = count($fileArray);
		$filesFound = $fileCount + $ignore;
		echo "Found $filesFound files. $ignore of them were ignored, $fileCount remaining.\n";
	}
	
	// Download all files found (they will be saved into the directory where this script is run)
	foreach($fileArray AS $value) {
		echo 'Retrieving file "',$value,'"... ';
		$fileName = str_replace(' ', '\\ ', escapeshellcmd($value));
		shell_exec("obexftp -b $mac -g ". $fileName . ' 2> /dev/null');
		echo "OK.\n";
	}

	echo "\n\n";
?>