<?php
require_once("seefun_config.php");
/*Get url parameters */
function sfd_url_param($param, $default="")
{
	$value = $default;
	if (isset($_GET[$param])) 
	{
	$value = (get_magic_quotes_gpc()) ? $_GET[$param] : addslashes($_GET[$param]);
	}
	return $value;
}

/*file listing*/
function sfd_list_dir($dir, $dir_name)
{
	if (is_dir($dir) && $handle = opendir($dir))
	{
		echo('<div class="sfd" id="sfd_file_list_body">
				<h3>Listing: '.$dir_name.'<span id="sfd_preview" style="position:absolute;"></span></h3>
				
				<a onclick="javascript:sfd_assign_file_field(\'\');" style="cursor:pointer;" >Reset</a> | <a onclick="javascript:sfd_file_list_close();" style="cursor:pointer;" >Close</a>');	
		$i=1;
		$k=0;
		
		while (false !== ($file = readdir($handle)))
		{
			$ext=sfd_findext($file);

			if (($ext=="gif" || $ext=="png" || $ext=="jpg" || $ext=="jpeg" || $ext=="bmp"))
			{
				if($i==1)
				{
					$i=2;
					$color="#DCDCDC";	
				}else{
					$i=1;
					$color="#FFFFFF";
				}
				echo('<div class="file_list" id="files_'.$k.'" style="background-color:'.$color.'; cursor:pointer;" onmouseover="sfd_hover(\'files_'.$k.'\', \''.$file.'\')" onmouseout="sfd_hover_off(\'files_'.$k.'\', \''.$color.'\')" > <a href="javascript:sfd_assign_file_field(\''.$file.'\');" onmousedown="return false;">'.$file.'</a> - <a href="javascript:sfd_delete(\''.$file.'\')" onmousedown="return false;">Delete</a> </div>');
				$k++;
			}
		}	
		
		echo('</div>');
				
		closedir($handle);
	}
}

/*find the extension of a filename*/
function sfd_findext($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

/*find the filel name without the extension*/
function sfd_find_name($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[0]; 
return $exts; 
}

/*upload valid files*/
function sfd_upload($dir_rel)
{
	$file_name2 = "";
	if($_POST)
	{
		$file_name=$_FILES['file']['name'];
		
		$ext=sfd_findext($file_name);
		
		if($ext=="gif" || $ext=="png" || $ext=="jpg" || $ext=="jpeg" || $ext=="bmp")
		{	
			$name =  str_replace(array('"',"'",'\\','/', ' ', '@', '*', '#'), '_', sfd_find_name($file_name));
			
			$file_name2=$name.".".$ext;
			
			if(is_uploaded_file($_FILES['file']['tmp_name']))
			{
				move_uploaded_file($_FILES['file']['tmp_name'], $dir_rel.$file_name2) or die("ERROR! The file was not uploaded!");
			
				if($_POST['x']!="")
				{	
					sfd_resize($dir_rel, $file_name2, $file_name2, $_POST['x']);
				}else if($_POST['y']){
					sfd_resize($dir_rel, $file_name2, $file_name2, "", $_POST['y']);
				}
			
			}else{
				die("File Not Uploaded");
			}
		}else{
			$file_name2 = "Invalid file type";
		}
	}
	return $file_name2;
}

/*this function is used to resize/save images. it can either save as a new file, or overwrite the origional*/
function sfd_resize($path, $file, $new_name="", $max_width="", $max_height="")
{
	$ext=sfd_findext($file);
	
	/*creat thumbnails*/
	list($width, $height) = getimagesize($path . $file);
	
	if($max_width!="" && ($width>=$height || $width>=$max_width))
	{
		$thumb_width=$max_width;
		$proportion=$max_width/$width;
		$thumb_height=$height*$proportion;
	}else if($max_height!="" && ($height>$width || $height>$max_height))
	{
		$thumb_height=$max_height;
		$proportion=$max_height/$height;
		$thumb_width=$width*$proportion;
	}else{
		$thumb_width=$width;
		$thumb_height=$height;
	}
	
	$thumb_canvas = imagecreatetruecolor($thumb_width, $thumb_height);
	if($ext=="gif" || $ext=="GIF")
	{
		$full_image = imagecreatefromgif($path . $file);
		imagecopyresampled($thumb_canvas, $full_image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
		/*save*/
		imagegif($thumb_canvas, $path.$new_name, 100);
	}else if($ext=="jpg" || $ext=="JPG")
	{
		$full_image = imagecreatefromjpeg($path . $file);
		imagecopyresampled($thumb_canvas, $full_image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
		/*save*/
		imagejpeg($thumb_canvas, $path.$new_name, 100);
	}else if($ext=="png" || $ext=="PNG")
	{
	  	die("Sorry, seefundo currently doesn't support re-sizing of up PNG's.");
	}
}

?>