Location: PHPKode > projects > FNServer Image Collaboration Software > fn-0.5.4.2/fnuploader.php
#!/usr/bin/php
<?php

/*
	FNUploader.php
	v0.75 / 12.01.2004
	Marc Rohlfing
*/

// set environment variables
//	$HOST = "http://127.0.0.1/~marc/work/fn/?page=fnuploader";
	$HOST = "http://rowlff.dotgeek.org/fn/index.php?page=fnuploader";
//	$CD = "CocoaDialog.app/Contents/MacOS/CocoaDialog";
	$CD = "$argv[1]/Contents/Resources/CocoaDialog.app/Contents/MacOS/CocoaDialog";
	
// check for preferences file, create new one on first run
	if (file_exists($_SERVER['HOME'] . "/Library/Preferences/fnuploader.prefs")) {
		$handle = fopen($_SERVER['HOME'] . "/Library/Preferences/fnuploader.prefs", "r");
		$USERNAME = trim(fgets($handle));
		$PASSWORD = trim(fgets($handle));
	}
	else {
		$execstr= $CD . " msgbox --title \"FNUploader\" --text \"Welcome to the FNUploader\" --informative-text \"You are using this application for the first time.\nPlease enter your username and password on the following screens.\nYou will only have to do this once...\" --button1 \"OK\" --button2 \"Cancel\" --string-output";
    	$button = trim(exec($execstr));
    	if ($button == 'Cancel') {
    		die();
    	}
		$execstr= $CD . " standard-inputbox --title \"FNUploader - Setting preferences...\" --informative-text \"Enter your username\" --no-cancel";
    	$USERNAME = exec($execstr);
		$execstr= $CD . " standard-inputbox --title \"FNUploader - Setting preferences...\" --informative-text \"Enter your password\" --no-cancel";
    	$PASSWORD = trim(exec($execstr));
   		$handle = fopen($_SERVER['HOME'] . "/Library/Preferences/fnuploader.prefs", "w");
		fwrite($handle, "$USERNAME\n");
		fwrite($handle, "$PASSWORD\n");
	}
	fclose($handle);

// determine number of dropped files
	$files = count($argv);
	$photos = $files - 2;
	
// nothing dropped?
	if ($photos < 1) {
		$execstr= $CD . " msgbox --title \"FNUploader - Warning!\" --text \"Warning!\" --informative-text \"You did not drop any picture files on the FNUploader!\" --button1 \"OK\" --style warning";
    	exec($execstr);
    	die();
	}

// get tags for the photos
	$execstr= $CD . " inputbox --title \"FNUploader - Entering tags...\" --informative-text \"Enter at least one tag for the photos you are uploading\" --button1 \"OK\"";
	$TAGS = exec($execstr);
	if ($TAGS == '') {
		die();
	}
		
	if ($photos == 1) {
		$uploadphotos = "1 photo";
	}
	else {
		$uploadphotos = "$photos photos";
	}

	$execstr= $CD . " progressbar --title \"FNUploader - Uploading photos...\" --text \"Uploading $uploadphotos\" --indeterminate";
    $bar = popen($execstr, "w");

// upload photos
	For ($i = 2; $i < $files; $i++) {
		$message = upload($argv[$i]);
		if ($message <> '') {
			fclose($bar);
			$execstr= $CD . " msgbox --title \"FNUploader - Warning!\" --text \"Upload interrupted!\" --informative-text \"$message\" --button1 \"Close\" --style warning";
		    exec($execstr);		
			die();
		}
	}

	fclose($bar);

// and pronounce success	
	$execstr= $CD . " msgbox --title \"FNUploader - Upload finished\" --text \"Upload finished\" --informative-text \"Your photos have been successfully uploaded\" --button1 \"Close\"";
    exec($execstr);	



// helper function

	function upload($filename) {
		global $HOST, $USERNAME, $PASSWORD, $TAGS;

		$parts = parse_url($HOST);
		$image = @file_get_contents($filename);
		
	   	$request .= "--AaB03x\r\ncontent-disposition: form-data; name=\"user\"\r\n\r\n$USERNAME\r\n";
	   	$request .= "--AaB03x\r\ncontent-disposition: form-data; name=\"password\"\r\n\r\n$PASSWORD\r\n";
	   	$request .= "--AaB03x\r\ncontent-disposition: form-data; name=\"tags\"\r\n\r\n$TAGS\r\n";
		$request .= "--AaB03x\r\ncontent-disposition: form-data; name=\"image\"; filename=\"$filename\"\r\n";
		$request .= "Content-Type: image/jpeg\r\n";
		$request .= "Content-Transfer-Encoding: binary\r\n\r\n";
		$request .= "$image\r\n";
		$request .= "--AaB03x--\r\n\r\n";

		$contentlength = strlen($request);
		
	   	$request = "POST $parts[path]?$parts[query] HTTP/1.0\r\nHost: $parts[host]\r\nContent-Type: multipart/form-data,boundary=AaB03x\r\nContent-Length: $contentlength\r\nConnection: close\r\n\r\n" . $request;

    	$fp = fsockopen($parts['host'], 80, $errno, $errstr, 30);

	    fwrite($fp, $request);
	    while (! feof($fp))
	    $result .= fgets($fp, 128);
	    fclose($fp);
	    $statuscode = substr($result, 9, 3);
	    $result = preg_replace("#^HTTP.*\r\n\r\n#Ums", "", $result);   	    	 	
	    if ($statuscode == "404")
	    	return 'Could not connect to the FNServer!';
		return $result;
    }

?>
Return current item: FNServer Image Collaboration Software