<?php 
/**
 * Cpmfetch :: cfimageget
 * 
 * This is a support library to help grab an image from a coppermine database and display them on normal 
 * HTML page by coming back as an image.  It also contains the object used by any extentions of cfimageget
 * 
 * Usage:  Call this file from an img tag and it will return graphic data to you
 *
 * cfimageget.php - will return random from anywhere in the gallery
 * cfimageget.php?category=1 - will return random image from category 1
 * cfimageget.php?album=1 - will return a random image from album 1
 * cfimageget.php?album=1 - will return a random image from album 1
 *
 * Other options to add on
 * size=[thumb|int or normal|large]
 * cmd=[last|random]
 * The presedence is album over category, so if you add in both - you get whatever album is looking for
 *
 * @author vuud
 * @package cpmfetch
 * @since 1.6.4
 * @version $Revision: 1.13 $
 */

/**
* This code directly accesses the cpmfetch_dao object
*/
require_once "cpmfetch_dao.php";

// CHANGE THIS TO TRUE IF PEOPLE ARE ALLOWED QUERIES THROUGH HERE
$ALLOW_DIRECT_ACCESS = true;

if ($ALLOW_DIRECT_ACCESS && substr($_SERVER['SCRIPT_FILENAME'],-14) == "cfimageget.php") {
	

// This is the relative path to the cpmfetch_config.php file
// If this script is in the same directory as the cpmfetch.php file, you should be able
// to leave it blank

$pathToConfigFile = "";

// If no cmd= is submitted, it will do this:	
$default_command = "random";

// This would allow for non-public photos to be shown if set to FALSE	
//	$do_not_show_private = true;

	
$cpmfetch = new cfimageget($pathToConfigFile);

// $cpmfetch->setAllowedTypes(array(".jpg",".gif",".png",".bmp")); 
// $cpmfetch->setPrivacyFlagOn($do_not_show_private);
			
// THIS PART IS CODE... IF YOU DID NOT KNOW THAT, DO NOT TOUCH ANYTHING BELOW (VOIDS WARRENTY)		
	if (array_key_exists("cmd",$_GET)) {
		switch ($_GET['cmd']) {
			case ('random'):
			case ('last'):
				$cmd_parameter = $_GET['cmd'];
			break;
			default:
				$cmd_parameter = $default_command;
				break;
		}		
	} else {
		$cmd_parameter = $default_command;
	}
	
	if (array_key_exists("album", $_GET)) {
		$cpmfetch->setAlbumSelection($_GET['album']);
	}												
							
	if (array_key_exists("category", $_GET)) {
		$cpmfetch->setCategorySelection($_GET['category']);
	}												

	if (array_key_exists("cat", $_GET)) {
		$cpmfetch->setCategorySelection($_GET['cat']);
	}												

		
	if (array_key_exists("size", $_GET)) {

		switch ($_GET['size']) {
			case ('thumb'):
				$cpmfetch->setDefaultSize("thumb");
				break;
			case ('int'):
			case ('normal');
				$cpmfetch->setDefaultSize("int");
				break;
			case ('large'):
				$cpmfetch->setDefaultSize("large");
				break;		
			default:
				$ERROR = true;
				$ERROR_NO = 2;
		}
	} // end size if	

	switch ($cmd_parameter) {
		case('random'):
				$resultset = $cpmfetch->getRandom();				
				break;
		case('last'):
				$resultset = $cpmfetch->getLastAdded();
				break;
		default:
				//$ERROR = true;
				//$ERROR_NO = 4;
				break;	
	}				
} // END DIRECT RUN
else {
	print "cfimageget.php is not enabled.  Edit the file to change the ALLOW_DIRECT_ACCESS line to be true.";
}



class cfimageget {

	var $cpm;
	var $urltocpm;
	var $filepathtocpm;
	
	var $sourceString = "";
	var $albumSelection = "";
	var $categorySelection = "";
	
	var $debugMode = false;
	var $defaultsize = "thumb";
	
	var $version = "2.1.1";

	var $allowedtypes = array('.jpg','.gif','.png');
	
	function cfimageget ($pathToConfigFile = "") {
				$this->cpm = new cpm_dao($config_file);
				$this->cpm->cpm_debugMode("false");

	} // end cfimageget
	

	function setDefaultSize($size) {
		$this->defaultsize = $size;

	}

	
	function getLastAdded() {
		$resultset = $this->cpm->getLastAddedMediaFrom($this->getSourceString(),1);
		$this->_dumpPhoto($resultset);
	}
	
	function getRandom( ) {
		$resultset = $this->cpm->getRandomImageFrom($this->getSourceString(),1);				
		$this->_dumpPhoto($resultset);	
	}

	function OverridePathToCoppermine($newpath_) {
		$this->cpm->OverridePathToCoppermine($newpath_);
	}	
		
	function OverrideUrlToCoppermine($newpath_) {
		$this->cpm->OverrideUrlToCoppermine($newpath_);	
	}	
		
		
   function setPrivacyFlagOn($bool) { 
		$this->cpm->cpm_unlock_private($bool);
	}

	function setAlbumSelection($source) {
		$this->albumSelection = $source;
	}
	
	function setCategorySelection($source) {
		$this->categorySelection = $source;
	}
		
	function getSourceString( ) {
		$fullString = "";
		
		if ($this->albumSelection == "" && $this->categorySelection == "") {
			$fullString = $this->sourceString;
		} elseif ( $this->albumSelection != "" && $this->categorySelection == "") {
			$fullString = "album=" . $this->albumSelection;
		} elseif ( $this->categorySelection != "" && $this->albumSelection == "") {
			$fullString = "cat=" . $this->categorySelection;
		} else {
			$fullString = "cat=" . $this->categorySelection . ":album=" . $this->albumSelection;
		}
	
		return ($fullString);
	}

	function setSourceString($sources) {
		$this->sourceString = $sources;
	}
		
	function setAllowedTypes($filterarray) { 
	   $this->allowedTypes = $filterarray;
		$this->cpm->cpm_setFilter($filterarray);
	}
	
	function setDebugMode($bool) {
	   $this->debugMode = true;
		$this->cpm->cpm_debugMode($bool);
	}
	
	function _dumpPhoto($resultset ) {
			if (count($resultset)==0) {
				$ERROR = true;
				$ERROR_NO = 5;
				$filenametoshow = './problem.gif';	
			} else {
				$row = array_shift($resultset);
				//$filenametoshow = $_SERVER['DOCUMENT_ROOT'];
				//$filenametoshow = $this->cpm->getImageToUse($row['pFilepath'],$row['pFilename'],$this->defaultsize);
				$filenametoshow = $this->cpm->cpm_getConfigEntry('filesystem_path_to_cpg');

				switch($this->defaultsize) {
					case ('thumb'):
						$filenametoshow .= $row['fullPathToThumb'];
						break;

					case ('int'):
					case ('normal'):
						$filenametoshow .= $row['fullPathToNormal'];
						break;

					case ('large'):
						$filenametoshow .= $row['fullPathToFull'];
						break;
				}

			}
	
			switch (strtolower(substr($row[1],strrpos($row[1],'.')))) {
				case '.jpeg':
				case '.jpg':
				case '.jpe':
					$contenttype = 'image/jpeg';
					break;
				case '.gif':
					$contenttype = 'image/gif';
					break;
				case '.png':
					$contenttype = 'image/png';
					break;
				case '.tiff';
				case '.tif';
					$contenttype = 'image/tiff';
					break;
				case '.bmp'	;
					$contenttype - 'application/x-MS-bmp';
					break;
				default:
					$contenttype = 'image/jpeg';
		}
	
		header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   		header ("Content-Type: $contenttype");
				  		
		if ($file = fopen($filenametoshow, 'rb')) {
   		while(!feof($file) and (connection_status()==0)) {
    			print(fread($file, 1024*8));
    			flush();
   		}
   		
			$status = (connection_status()==0);
  	 		fclose($file);
		}
				
	}
	
}  //End of class

?>
