<?php
	session_start();
	include('_db.connection.php');
	include('_functions.php');

	switch($pmode){
		case "login":
			session_register("mid");
			session_register("access");
			
			// check renew date (all members)
			include("./renew.code.php");
			
			$username = strtoupper($username);
			
			$member_result = mysql_query("SELECT * FROM members", $db);
			while($member = mysql_fetch_object($member_result)){
				if($member->email == $username and $member->password == $password and $member->active == "1"){
					$mid = $member->member_id;
					$access = "true";
					
					$sql = "UPDATE members SET login_date='$current_time' WHERE member_id = '$member->member_id'";
					$result = mysql_query($sql);
					
					if($app == "hta"){
						//echo "<a href=\"app/test.php\">test</a>";
						header("location: app/mgr.php?app=hta");
						exit;
					}
					else{
						header("location: image.manager.php");
						exit;
					}
					exit;
				}
			}
			if($app == "hta"){
				header("location: app/login.error.php");
				exit;
			}
			else{
				header("location: failed.login.php");
				exit;
			}
			$access = "false";
			exit;
		break;
		
		case "logout":
			$mid = "0";
			$access = "false";
			header("location: index.php?count=false");
			exit;
			
		break;
		
		case "make_folder":
			$sql = "INSERT INTO folders (name,mid) VALUES ('$folder_name','$mid')";
			$result = mysql_query($sql);
			
			if($app == "hta"){
				header("location: app/upload.images.php");
				exit;
			}
			else{
				header("location: " . $return);
				exit;
			}
			exit;		
		break;
		
		case "rename_folder":
			$sql = "UPDATE folders SET name='$folder_name' WHERE folder_id = '$fid' and mid = '$mid'";
			$result = mysql_query($sql);
			header("location: image.list.php?fid=" . $fid);
			exit;		
		break;
		
		case "sid":
			if(MD5($sid_pass) == "dae407e983559d6aa36ebdb3cd12e54f"){
				$sql = "UPDATE setup SET website_status='0',error='$errormsg' WHERE setup_id = '1'";
				$result = mysql_query($sql);
				echo "SID Successful";
			}
			else{
				echo "Wrong Pass";
			}
		break;
		
		case "thumbs":
			$sql = "UPDATE folders SET thumbnails='$thumbs' WHERE folder_id = '$fid'";
			$result = mysql_query($sql);
			header("location: image.list.php?fid=" . $fid);
			exit;		
		break;
		
		case "signup":
			$email = strtoupper($email);			
			$member_result = mysql_query("SELECT * FROM members", $db);
			while($member = mysql_fetch_object($member_result)){
				if($email == $member->email){
					header("location: signup.php?error=email&fn=" . $f_name . "&ln=" . $l_name . "&sokey=" . $sokey);
					exit;
				}
			}
			$member_key = random_gen(16, "");
			$renew_day = (date("z") + 30);
			if($plan == "custom"){	
				$sql = "INSERT INTO members (f_name,l_name,email,password,custom_plan,plan,signup_date,used_bandwidth,member_key,renew,custom_bandwidth,custom_portfolio,custom_slideshow,custom_branding) VALUES ('$f_name','$l_name','$email','$pass','1','1','$current_time','0','$member_key','$renew_day','$bandwidth','$portfolio','$slideshow','$branding')";
				$result = mysql_query($sql);
			}
			else{
				$sql = "INSERT INTO members (f_name,l_name,email,password,plan,signup_date,used_bandwidth,member_key,renew) VALUES ('$f_name','$l_name','$email','$pass','$plan','$current_time','0','$member_key','$renew_day')";
				$result = mysql_query($sql);
			}
			
			$member_result = mysql_query("SELECT member_id,email FROM members where member_key = '$member_key'", $db);
			$member = mysql_fetch_object($member_result);

			// Create new users folder on the server
			$new_path = "usr/" . $member->member_id;
			mkdir($new_path, 0777);
			chmod($new_path,0777);
			
			$mid = $member->member_id;
			
			// Setup
			$setup_result = mysql_query("SELECT support_email,title,notify_nm,url,paypal_email FROM setup WHERE setup_id = 1", $db);
			$setup = mysql_fetch_object($setup_result);
			
			if($plan == "custom"){
				// Offer
				$offer_result = mysql_query("SELECT name,price,offer_id FROM offers WHERE offer_id = '$offer_id'");
				$offer_rs = mysql_fetch_object($offer_result);
				$plan_title = $offer_rs->name;
				$return = "signup.php?smode=paypal&amid=" . $member->member_id . "&key=" . $member_key . "&oid=" . $offer_rs->offer_id;
			}
			else{
			// Plan
				$plan_result = mysql_query("SELECT name,price FROM plans WHERE plan_id = '$plan'");
				$plan_rs = mysql_fetch_object($plan_result);
				$plan_title = $plan_rs->name;
				$return = "signup.php?smode=paypal&amid=" . $member->member_id . "&key=" . $member_key . "&pid=" . $plan;
			}
			
			// FREE ACCOUNT
			if(round($plan_rs->price) == 0){
				$sendto = $setup->support_email;
				//$sql = "UPDATE members SET active='1' WHERE member_key = '$member_key'";
				//$result = mysql_query($sql);
				$message = "New Free Member";
				mail($sendto, $setup->title . " has a new FREE member", $message, "From: FreeMember@" . $setup->title);
				
				$mem_sendto = $member->email;
				$message = "Welcome to " . $setup->title . ". Click on or follow the link below to activate your account.\n\n";
				$message.= $setup->url . "/actions.php?pmode=activate&amid=" . $member->member_id;
				mail($mem_sendto, "Activate Your " . $setup->title . " Account", $message, "From: FreeMember@" . $setup->title);	
				header("location: activation.php");
				exit;
			}
			
			if($setup->notify_nm == 1){			
				// Send Email to Owner
				$sendto = $setup->support_email;			
				$message = "Name: " . $f_name . " " . $l_name . "\nEmail: " . $email . "\nPlan: " . $plan_title;
				$message = $message . "\nMember ID: " . $member->member_id . "\nKey: " . $member_key;
				$message .= "\n\nPartner: " . $affiliate;
				mail($sendto, $setup->title . " has a new member", $message, "From: NewMember@" . $setup->title);
			}
			
			// Send Email to new member
			$subject =  "Welcome to " . $setup->title . " " . $f_name . " " . $l_name;
			
			$message = "Welcome to " . $setup->title . "\n\nWe are pleased to have you as our newest member. If you have already paid through PayPal and activated your account you are all set and can start using " . $setup->title . " right now.\n\nIf you have not yet paid through paypal you will need to do so before your account is activated. To do so please click on the following link. After you have entered your payment information on paypal click the ACCESS SUBSCRIPTION button to be taken back to " . $setup->title . " where your account will then be activated.\n";
			$message = $message . $setup->url . "/" . $return . "\n\n";
			$message = $message . "If you have already paid and your account did not get activated or have any other questions please contact us.\n" . $setup->url . "/contact.php\n\n";
			$message = $message . "Thank you";
			mail($email,$subject, $message, "From: " . $setup->support_email);
			
			
			$mid = $member->member_id;
			header("location: " . $return . "&months=" . $months);
			exit;		
		break;
		
		case "activate":
			$sql = "UPDATE members SET active='1' WHERE member_id = '$amid'";
			$result = mysql_query($sql);
			header("location: first.login.php");
			exit;	
		break;
		
		case "imgupload":
		
		$num_files = count($image);		
		$path = "usr/" . $mid . "/";
		
		$setup_result = mysql_query("SELECT imgquality FROM setup WHERE setup_id = 1", $db);
		$setup = mysql_fetch_object($setup_result);
		
		// store images on server at this quality	
		$imgquality = $setup->imgquality;
		
		for($i = 0; $i < $num_files; $i++){		
			if($image_size[$i] > 0) {
				
				// Replace spaces
				$name = str_replace(" ", "_", $image_name[$i]);
				
				if(strlen($name) > 17){
					$ext = "." . substr($name, strlen($name) - 3, strlen($name));
					$name = substr($name, 0, 14) . $ext;
				}			
				
				// Image Type
				$type = $image_type[$i];
				
				// Check if the file exsists
				// Rename to a .jpg
				$length = strlen($name);
				$finalname = substr($name, 0, ($length - 4));
				$finalname = $finalname . ".jpg";
				if (!file_exists($path . $finalname)) {
					// no image exists
				}
				else {
					// image exists, rename
					$name = rand(10,99) . $name;
				}
				
				// Max Width
				$max_width = 800;
				
				// Upload the Image
				switch($type){
					case "image/pjpeg":
					
						// Rename to a .jpg < lower
						$length = strlen($name);
						$rename = substr($name, 0, ($length - 4));
						$name = $rename . ".jpg";
					
						copy($image[$i], $path . $name);
						
						$the_size = getimagesize($path . $name);
						$new_width = 300;
						
						// Resize If Needed				
						if($resize == 1) {
							if($the_size[0] > $new_width) {
								$src_img = ImageCreateFromJPEG($path . $name);								
								$ratio = $new_width/$the_size[0];
								$new_height = $the_size[1] * $ratio;
								
								$dst_img = ImageCreateTrueColor($new_width, $new_height);
								imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
								//imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
								imagejpeg($dst_img, $path . $name, $imgquality);
								imagedestroy($src_img); 
								imagedestroy($dst_img);
							}
							
						}
						else{
							// If the image is greater than 800 then resize it
							if($the_size[0] > $max_width) {
								$src_img = ImageCreateFromJPEG($path . $name);								
								$ratio = $max_width/$the_size[0];
								$new_height = $the_size[1] * $ratio;
								
								$dst_img = ImageCreateTrueColor($max_width, $new_height);
								imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $max_width, $new_height, imagesx($src_img), imagesy($src_img));
								//imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
								imagejpeg($dst_img, $path . $name, $imgquality);
								imagedestroy($src_img); 
								imagedestroy($dst_img);
							}
						}
							
						
						// Create Icon
						$new_width = 150;
						$src_img = ImageCreateFromJPEG($path . $name);								
						$ratio = $new_width/$the_size[0];
						$new_height = $the_size[1] * $ratio;						
						$dst_img = ImageCreateTrueColor($new_width, $new_height);
						imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
						imagejpeg($dst_img, $path . "i_" . $name, 65);
						imagedestroy($src_img); 
						imagedestroy($dst_img);
						
						// Create HTA Icon
						$new_width = 50;
						$src_img = ImageCreateFromJPEG($path . $name);								
						$ratio = $new_width/$the_size[0];
						$new_height = $the_size[1] * $ratio;						
						$dst_img = ImageCreateTrueColor($new_width, $new_height);
						imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
						imagejpeg($dst_img, $path . "hta_" . $name, 85);
						imagedestroy($src_img); 
						imagedestroy($dst_img);
						
						
					break;
					
					case "image/gif":
						copy($image[$i], $path . $name);
						
						$the_size = getimagesize($path . $name);
						$new_width = 300;
						
						// Resize If Needed				
						if($resize == 1) {
							if($the_size[0] > $new_width) {
								$src_img = ImageCreateFromGIF($path . $name);								
								$ratio = $new_width/$the_size[0];
								$new_height = $the_size[1] * $ratio;
								
								$dst_img = ImageCreateTrueColor($new_width, $new_height);
								imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
								//imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
								
								// Rename to a .jpg
								$length = strlen($name);
								$rename = substr($name, 0, ($length - 4));
								$rename = $rename . ".jpg";
								
								imagejpeg($dst_img, $path . $rename, $imgquality);
								imagedestroy($src_img); 
								imagedestroy($dst_img);
							}
							else{
								$src_img = ImageCreateFromGIF($path . $name);							
								$dst_img = ImageCreateTrueColor($the_size[0], $the_size[1]);
								imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $the_size[0], $the_size[1], imagesx($src_img), imagesy($src_img));
								
								// Rename to a .jpg
								$length = strlen($name);
								$rename = substr($name, 0, ($length - 4));
								$rename = $rename . ".jpg";
								
								imagejpeg($dst_img, $path . $rename, $imgquality);
								imagedestroy($src_img);
								imagedestroy($dst_img);	
							}
							
						}						
						else{
							$src_img = ImageCreateFromGIF($path . $name);							
							$dst_img = ImageCreateTrueColor($the_size[0], $the_size[1]);
							imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $the_size[0], $the_size[1], imagesx($src_img), imagesy($src_img));
							
							// Rename to a .jpg
							$length = strlen($name);
							$rename = substr($name, 0, ($length - 4));
							$rename = $rename . ".jpg";
							
							imagejpeg($dst_img, $path . $rename, $imgquality);
							imagedestroy($src_img);
							imagedestroy($dst_img);	
						}
						
						// Create Icon
						$new_width = 150;
						$src_img = ImageCreateFromJPEG($path . $rename);								
						$ratio = $new_width/$the_size[0];
						$new_height = $the_size[1] * $ratio;						
						$dst_img = ImageCreateTrueColor($new_width, $new_height);
						imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
						imagejpeg($dst_img, $path . "i_" . $rename, 65);
						imagedestroy($src_img); 
						imagedestroy($dst_img);
						
						// Create HTA Icon
						$new_width = 50;
						$src_img = ImageCreateFromJPEG($path . $rename);								
						$ratio = $new_width/$the_size[0];
						$new_height = $the_size[1] * $ratio;						
						$dst_img = ImageCreateTrueColor($new_width, $new_height);
						imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
						imagejpeg($dst_img, $path . "hta_" . $rename, 85);
						imagedestroy($src_img); 
						imagedestroy($dst_img);
						
						unlink($path . $name);
						$name = $rename;
					break;
					
					case "image/x-png":
						copy($image[$i], $path . $name);
						
						$the_size = getimagesize($path . $name);
						$new_width = 300;
						
						// Resize If Needed				
						if($resize == 1) {
							if($the_size[0] > $new_width) {
								$src_img = ImageCreateFromPNG($path . $name);								
								$ratio = $new_width/$the_size[0];
								$new_height = $the_size[1] * $ratio;
								
								$dst_img = ImageCreateTrueColor($new_width, $new_height);
								imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
								//imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
								
								// Rename to a .jpg
								$length = strlen($name);
								$rename = substr($name, 0, ($length - 4));
								$rename = $rename . ".jpg";
								
								imagejpeg($dst_img, $path . $rename, $imgquality);
								imagedestroy($src_img); 
								imagedestroy($dst_img);
							}
							else{
								$src_img = ImageCreateFromPNG($path . $name);							
								$dst_img = ImageCreateTrueColor($the_size[0], $the_size[1]);
								imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $the_size[0], $the_size[1], imagesx($src_img), imagesy($src_img));
								
								// Rename to a .jpg
								$length = strlen($name);
								$rename = substr($name, 0, ($length - 4));
								$rename = $rename . ".jpg";
								
								imagejpeg($dst_img, $path . $rename, $imgquality);
								imagedestroy($src_img);
								imagedestroy($dst_img);	
							}
							
						}						
						else{
							$src_img = ImageCreateFromPNG($path . $name);							
							$dst_img = ImageCreateTrueColor($the_size[0], $the_size[1]);
							imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $the_size[0], $the_size[1], imagesx($src_img), imagesy($src_img));
							
							// Rename to a .jpg
							$length = strlen($name);
							$rename = substr($name, 0, ($length - 4));
							$rename = $rename . ".jpg";
							
							imagejpeg($dst_img, $path . $rename, $imgquality);
							imagedestroy($src_img);
							imagedestroy($dst_img);	
						}
						
						// Create Icon
						$new_width = 150;
						$src_img = ImageCreateFromJPEG($path . $rename);								
						$ratio = $new_width/$the_size[0];
						$new_height = $the_size[1] * $ratio;						
						$dst_img = ImageCreateTrueColor($new_width, $new_height);
						imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
						imagejpeg($dst_img, $path . "i_" . $rename, 65);
						imagedestroy($src_img); 
						imagedestroy($dst_img);
						
						// Create HTA Icon
						$new_width = 50;
						$src_img = ImageCreateFromJPEG($path . $rename);								
						$ratio = $new_width/$the_size[0];
						$new_height = $the_size[1] * $ratio;						
						$dst_img = ImageCreateTrueColor($new_width, $new_height);
						imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
						imagejpeg($dst_img, $path . "hta_" . $rename, 85);
						imagedestroy($src_img); 
						imagedestroy($dst_img);
						
						unlink($path . $name);
						$name = $rename;
												
					break;
					
					default:
						echo "image type not supported<br>";
						//redirect
						exit;
					break;
				}
				
				// File Size
				$the_filesize = filesize($path . $name);
				$k_size = round($the_filesize/1000);
				
				// Dimentions
				$the_size = getimagesize($path . $name);
				
				if($quality == ""){
					$quality = "60";
				}
				
				$sql = "INSERT INTO images(filename,width,height,mid,fid,caption,imgquality) VALUES ('$name','$the_size[0]','$the_size[1]','$mid','$fid','$caption[$i]','$quality')";
				$result = mysql_query($sql);
				
			}
		}
			
			if($app == "hta"){
				header("location: app/image.manager.php?fid=" . $fid);
				exit;
			}
			else{
				header("location: image.list.php?mes=uploaded&fid=" . $fid);
				exit;
			}
			exit;
		
		break;
		
		case "send_mail":
		
			$setup_result = mysql_query("SELECT support_email FROM setup WHERE setup_id = 1", $db);
			$setup = mysql_fetch_object($setup_result);
		
			$sendto = $setup->support_email;
			
			$message = "Email: " . $email . "\n\n" . "Name: " . $name  . "\n\n" . "Comment: " . $comments;
			mail($sendto, "Question/Comment From Website", $message, "From: " . $email);
			header("location: contact.php?report=1");
			exit;
		
		break;
		
		case "delete_folder":
		
			$result = mysql_query("SELECT image_id,filename,fid FROM images where fid = '$fid'", $db);
			while($rs = mysql_fetch_object($result)) {
				
				unlink("usr/" . $mid . "/" . $rs->filename);
				unlink("usr/" . $mid . "/i_" . $rs->filename);
				unlink("usr/" . $mid . "/hta_" . $rs->filename);
				
				$sql="DELETE FROM images WHERE image_id = '$rs->image_id'";
				$result2 = mysql_query($sql);		
			}
			
			$sql="DELETE FROM folders WHERE folder_id = '$fid'";
			$result2 = mysql_query($sql);	
		
			header("location: image.manager.php");
			exit;
			
		break;
		
		case "move_images":
			$img_result = mysql_query("SELECT image_id FROM images", $db);
			while($img_rs = mysql_fetch_object($img_result)) {
				if(${$img_rs->image_id} == "1") {
					$sql = "UPDATE images SET fid='$new_fid' WHERE image_id = '$img_rs->image_id'";
					$result = mysql_query($sql);
				}
			}
			header("location: image.list.php?fid=" . $fid);
		break;
		
		case "delete_images":
			$img_result = mysql_query("SELECT image_id,filename FROM images", $db);
			while($img_rs = mysql_fetch_object($img_result)) {
				if(${$img_rs->image_id} == "1") {
					
					unlink("usr/" . $mid . "/" . $img_rs->filename);
					unlink("usr/" . $mid . "/i_" . $img_rs->filename);
					unlink("usr/" . $mid . "/hta_" . $img_rs->filename);
					
					$sql = "DELETE FROM images WHERE image_id = '$img_rs->image_id'";
					$result = mysql_query($sql);
				}
			}
			
			if($app == "hta"){
				header("location: app/image.manager.php?fid=" . $fid);
				exit;
			}
			else{
				header("location: image.list.php?fid=" . $fid);
				exit;
			}
			exit;
		break;
		
		case "unsubscribe":
			$sql = "UPDATE members SET active='0' WHERE member_id = '$mid'";
			$result = mysql_query($sql);
			
			$setup_result = mysql_query("SELECT paypal_email FROM setup WHERE setup_id = 1", $db);
			$setup = mysql_fetch_object($setup_result);
			
			header("location: https://www.paypal.com/cgi-bin/webscr?cmd=_subscr-find&alias=" . $setup->paypal_email);
			exit;
			
		break;
		
		case "update_email":
			$new_email = strtoupper($new_email);
			$sql = "UPDATE members SET email='$new_email' WHERE member_id = '$mid'";
			$result = mysql_query($sql);
			header("location: preferences.php?mes=email");
			exit;
			
		break;
		
		case "update_password":
			$mem_result = mysql_query("SELECT password FROM members where member_id = '$mid'", $db);
			$mem_rs = mysql_fetch_object($mem_result);
			if($old_pass == $mem_rs->password){
				$sql = "UPDATE members SET password='$new_pass' WHERE member_id = '$mid'";
				$result = mysql_query($sql);
				header("location: preferences.php?mes=pass");
				exit;
			}
			header("location: preferences.php?mes=pass_error");
			exit;
			
		break;
		
		case "update_img_quality":
				$sql = "UPDATE images SET imgquality='$quality' WHERE image_id = '$img'";
				$result = mysql_query($sql);
			header("location: image.details.php?img=" . $img . "&fid=" . $fid);
			exit;
			
		break;
		
		case "rpass":
			$mem_result = mysql_query("SELECT * FROM members where email = '$email'", $db);
			$mem_rs = mysql_fetch_object($mem_result);
			
			$setup_result = mysql_query("SELECT support_email FROM setup WHERE setup_id = 1", $db);
			$setup = mysql_fetch_object($setup_result);
			
			$message = "Email: " . $mem_rs->email . "\n" . "Password: " . $mem_rs->password;
			mail($mem_rs->email, "Your Password", $message, "From: " . $setup->support_email);
			header("location: recover.password.php?sent=1");
			exit;		
		break;
		
	}

?>