//
//  Copyright (c) 2013 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
//
//  Permission to use, copy, modify, and/or distribute this software for any purpose
//  with or without fee is hereby granted, provided that the above copyright notice
//  and this permission notice appear in all copies.
//
//  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
//  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
//  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
//  OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
//  DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
//  ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
if ($_MGM['path'][1]=="complete") {
	if ($_MGM['path'][2]=="process") {
		$file = (isset($_REQUEST['file']) ? $_REQUEST['file'] : "");
		$filename = pathinfo($file, PATHINFO_FILENAME);
		$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
		echo "Processing ".basename($file)."
\n";
		if (!file_exists($file)) {
			echo "Error: File does not exist.";
			exit();
		}
		$allowedExtensions = array("png", "jpg", "jpeg", "gif", "tif", "tiff", "bmp");
		if (!in_array($extension, $allowedExtensions)) {
			echo "Error: Extension is not allowed.";
			unlink($file);
			exit();
		}
		
		$fileSize = filesize($file);
		echo "Size: ".$fileSize."
\n";
		
		list($width, $height, $type, $attr) = getimagesize($file);
		if (!isset($width) || !isset($height)) {
			echo "Error: Cannot read image.";
			unlink($file);
			exit();
		}
		echo "Width: ".$width." Height: ".$height."
\n";
		
		$hash = md5_file($file);
		if ($hash==NULL) {
			echo "Error: Unable to hash image.";
			unlink($file);
			exit();
		}
		echo "Hash: ".$hash."
\n";
		
		$newFile = "./data/".$hash.".".$extension;
		if (file_exists($newFile)) {
			echo "Error: File already uploaded.";
			unlink($file);
			exit();
		}
		
		$ocr = "";
		if (file_exists("./ocr")) {
			$descriptorspec = array(
				0 => array("pipe", "r"),
				1 => array("pipe", "w"),
				2 => array("pipe", "w")
			);
			$process = proc_open("./ocr \"".$file."\"", $descriptorspec, $pipes, getcwd());
		
			fclose($pipes[0]);
			while (is_resource($process)) {
				$read = $pipes;
				$write = null;
				$except = null;
				$result = stream_select($read, $write, $except, 30);
			
				if ($result==0) {
					fclose($pipes[1]);
					fclose($pipes[2]);
					proc_terminate($process,9);
					break;
				} else if ($result>0) {
					$line = fread($pipes[1], 8192);
					if (strlen($line)==0) {
						fclose($pipes[1]);
						fclose($pipes[2]);
						proc_close($process);
						break;
					}
					$ocr .= $line;
				}
			}	
			echo "OCR: ".htmlspecialchars($ocr, ENT_COMPAT | ENT_HTML401, 'UTF-8', true)."
\n";
		}
		
		$tags = array();
		$external_data = "";
		
		$plugins = glob("./external_data_plugins/*.php");
		for ($pluginIndex=0; $pluginIndex
\n";
		
		rename($file, $newFile);
		echo "Moved Original.
\n";
		
		databaseQuery("INSERT INTO images (user_id,hash,extension,name,file_size,width,height,thumb_file_size,thumb_width,thumb_height,tags,external_data,ocr,time) VALUES (%s,%s,%s,%s,%d,%d,%d,%d,%d,%d,%s,%s,%s,%d)", $_MGM['user']['docid'], $hash, $extension, $filename, $fileSize, $width, $height, filesize($thumbFile), $newWidth, $newHeight, implode(" ", $tags), $external_data, $ocr, filemtime($newFile));
		echo "Complete.
\n";
		exit();
	}
	$files = glob("./load/*");
	require_once("header.php");
	?>
	Processing...