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

switch($a_mode){
	case "save_settings":
		$num_plans = count($plan_id);		
		for($i = 0; $i < $num_plans; $i++){
			
			$brandVar = ${"branding_" . $plan_id[$i]};
			$portVar = ${"portfolio_" . $plan_id[$i]};
			$ssVar = ${"slideshow_" . $plan_id[$i]};
			$spinVar = ${"spin_" . $plan_id[$i]};
				
			$sql = "UPDATE plans SET display_order='$display_order[$i]',spin='$spinVar',price='$price[$i]',price3='$price3[$i]',price6='$price6[$i]',price12='$price12[$i]',space='$disk_space[$i]',bandwidth='$bandwidth[$i]',branding='$brandVar',portfolio='$portVar',slideshow='$ssVar' WHERE plan_id = '$plan_id[$i]'";
			$result = mysql_query($sql);
		}
		
		/*
		// update plans
		$x = 1;
		while($x <= 3){
			$priceVar = ${"price_" . $x};
			$bandVar = ${"bandwidth_" . $x};
			$brandVar = ${"branding_" . $x};
			$portVar = ${"portfolio_" . $x};
			$ssVar = ${"slideshow_" . $x};
			$sql = "UPDATE plans SET price='$priceVar',bandwidth='$bandVar',branding='$brandVar',portfolio='$portVar',slideshow='$ssVar' WHERE plan_id = '$x'";
			$result = mysql_query($sql);
		$x++;
		}
		*/	
	
		$sql = "UPDATE setup SET ";
		
		if($admin_status == 1){
			$sql .= "access_id='$access_id',error='$errormsg',public_bgcolor='$public_bgcolor',demo_mode='$demo_mode',branding='$branding',quick_stats='$quick_stats',website_status='$website_status',";
		}
				
		$sql .= "mgr_style='$mgr_style',mgr_username='$mgr_username',mgr_password='$mgr_password',title='$title',url='$site_url',tagline='$tagline',support_email='$support_email',personal_email='$personal_email',meta_keywords='$keywords',meta_description='$description',cpanel='$cpanel',paypal_email='$paypal_email'";
		$sql .= "WHERE setup_id = '1'";
		$result = mysql_query($sql);		
	header("location:" . $return);
	exit;
	break;
	
	case "delete_selected":
	//echo "testing";
		$result = mysql_query("SELECT * FROM members", $db);
		while($rs = mysql_fetch_object($result)) {		
			if(${$rs->member_id} == "1") {
				//echo $rs->member_id;
				$img_files = "";
				$imgsql="DELETE FROM images WHERE mid = '$rs->member_id'";
				$imgresult2 = mysql_query($imgsql);
			
				$folsql="DELETE FROM folders WHERE mid = '$rs->member_id'";
				$folresult2 = mysql_query($folsql);

				$mem_path = realpath("../usr/" . $rs->member_id);

				if ($handle = opendir($mem_path)) {
	
				    /* This is the correct way to loop over the directory. */
				    while (false !== ($file = readdir($handle))) {
				        $img_files[]  = $file;
				    }
	
				    closedir($handle);
				}
				$x = count($img_files) - 1;
				$i = 2;
				while($i <= $x){
					//echo $img_files[$i] . "-" . $i . "<br>";
					unlink("../usr/" . $rs->member_id . "/" . $img_files[$i]);
					$i++;
				}
				rmdir("../usr/" . $rs->member_id);				
				
				$sql="DELETE FROM members WHERE member_id = '$rs->member_id'";
				$result2 = mysql_query($sql);
			}
		}
		header("location: mgr.index.php?n=2&p_mode=members");
		exit;
	break;
	
	case "delete_inactive":
	//echo "testing";
		$result = mysql_query("SELECT * FROM members", $db);
		while($rs = mysql_fetch_object($result)) {		
			if($rs->active != "1") {
				//echo $rs->member_id;
				$img_files = "";
				$imgsql="DELETE FROM images WHERE mid = '$rs->member_id'";
				$imgresult2 = mysql_query($imgsql);
			
				$folsql="DELETE FROM folders WHERE mid = '$rs->member_id'";
				$folresult2 = mysql_query($folsql);

				$mem_path = realpath("../usr/" . $rs->member_id);

				if ($handle = opendir($mem_path)) {
	
				    /* This is the correct way to loop over the directory. */
				    while (false !== ($file = readdir($handle))) {
				        $img_files[]  = $file;
				    }
	
				    closedir($handle);
				}
				
				$x = count($img_files) - 1;
				$i = 2;
				while($i <= $x){
					//echo $img_files[$i] . "-" . $i . "<br>";
					unlink("../usr/" . $rs->member_id . "/" . $img_files[$i]);
					$i++;
				}
				//echo "--------------<br>";
				
				rmdir("../usr/" . $rs->member_id);				
				
				$sql="DELETE FROM members WHERE member_id = '$rs->member_id'";
				$result2 = mysql_query($sql);
			}
		}
		header("location: mgr.index.php?n=2&p_mode=members");
		exit;
	break;
	
	case "delete_plan":
		
		$plan_result = mysql_query("SELECT * FROM plans where plan_id = '$pid'", $db);
		$plan_rs = mysql_fetch_object($plan_result);
	
		$mem_result = mysql_query("SELECT * FROM members where plan = '$pid'", $db);
		while($mem_rs = mysql_fetch_object($mem_result)) {
			
			$sql3 = "UPDATE members SET custom_plan='1',custom_bandwidth='$plan_rs->bandwidth',custom_space='$plan_rs->space',custom_branding='$plan_rs->branding',custom_portfolio='$plan_rs->portfolio',custom_slideshow='$plan_rs->slideshow' WHERE member_id = '$mem_rs->member_id'";
			$result3 = mysql_query($sql3);
		}
		
		//echo $plan_rs->bandwidth;
		
		$sql="DELETE FROM plans WHERE plan_id = '$pid'";
		$result2 = mysql_query($sql);
		header("location: mgr.index.php?n=1&p_mode=website_settings");
		exit;
		
	break;
	
	case "save_new_plan":
			$sql = "INSERT INTO plans (name,bandwidth,price,price3,price6,price12,branding,portfolio,slideshow,space,spin) VALUES ('$np_name','$np_bw','$np_price','$np_price_3','$np_price_6','$np_price_12','$np_branding','$np_portfolio','$np_slideshow','$np_disk_space','$np_spin')";
			$result = mysql_query($sql);
		header("location:" . $return);
		exit;
	break;
	
	case "save_new_news":
		if($title != ""){
			$new_date = $year . $month . $day;
			$sql = "INSERT INTO news (title,article,date) VALUES ('$title','$article','$new_date')";
			$result = mysql_query($sql);
		}
		header("location:" . $return);	
		exit;
	break;
	
	case "save_edit_news":
		if($title != ""){
			$new_date = $year . $month . $day;
			$sql = "UPDATE news SET title='$title',article='$article',date='$new_date' WHERE news_id = '$n_id'";
			$result = mysql_query($sql);
		}
		header("location:" . $return);
		exit;
	break;
	
	case "delete_news":
		$result = mysql_query("SELECT news_id FROM news", $db);
		while($rs = mysql_fetch_object($result)) {		
			if(${$rs->news_id} == "1") {
				$sql="DELETE FROM news WHERE news_id = '$rs->news_id'";
				$result2 = mysql_query($sql);
			}
		}
		header("location:" . $return);
		exit;	
	break;
	
	case "save_new_offer":
		if($name != ""){
			$key = random_gen(5,"");
			$sql = "INSERT INTO offers (name,branding,portfolio,slideshow,price,bandwidth,offer_key) VALUES ('$name','$branding','$portfolio','$slideshow','$price','$bandwidth','$key')";
			$result = mysql_query($sql);
		}
		header("location:" . $return);
		exit;
	break;
	
	case "change_mem_pass":
			$sql = "UPDATE members SET password='$password' WHERE member_id = '$memid'";
			$result = mysql_query($sql);
			//$return = "mgr.index.php?n=" . $n . "&p_mode=members";
			header("location:" . $return);
			exit;
			//echo $id;
	break;
	
	case "save_edit_offer":
		if($name != ""){
			$sql = "UPDATE offers SET name='$name',branding='$branding',portfolio='$portfolio',slideshow='$slideshow',price='$price',bandwidth='$bandwidth' WHERE offer_id = '$o_id'";
			$result = mysql_query($sql);
		}
		header("location:" . $return);
		exit;
	break;
	
	case "delete_offer":
		$result = mysql_query("SELECT offer_id FROM offers", $db);
		while($rs = mysql_fetch_object($result)) {		
			if(${$rs->offer_id} == "1") {
				$sql="DELETE FROM offers WHERE offer_id = '$rs->offer_id'";
				$result2 = mysql_query($sql);
			}
		}
		header("location:" . $return);
		exit;
	break;
	
	case "save_new_faq":
		if($question != ""){
			$sql = "INSERT INTO faqs (question,answer) VALUES ('$question','$answer')";
			$result = mysql_query($sql);
		}
		header("location:" . $return);
		exit;
	break;
	
	case "save_edit_faq":
		if($question != ""){
			$sql = "UPDATE faqs SET question='$question',answer='$answer' WHERE faq_id = '$f_id'";
			$result = mysql_query($sql);
		}
		header("location:" . $return);
		exit;
	break;
	
	case "delete_faqs":
		$result = mysql_query("SELECT faq_id FROM faqs", $db);
		while($rs = mysql_fetch_object($result)) {		
			if(${$rs->faq_id} == "1") {
				$sql="DELETE FROM faqs WHERE faq_id = '$rs->faq_id'";
				$result2 = mysql_query($sql);
			}
		}
		header("location:" . $return);
		exit;
	break;
	
	case "save_edit_copy":
		$sql = "UPDATE copy SET name='$name',copy='$copy' WHERE copy_id = '$c_id'";
		$result = mysql_query($sql);
		header("location:" . $return);
		exit;
	break;
	
	case "delete_account":
		if($id != ""){
			$imgsql="DELETE FROM images WHERE mid = '$id'";
			$imgresult2 = mysql_query($imgsql);
			
			$folsql="DELETE FROM folders WHERE mid = '$id'";
			$folresult2 = mysql_query($folsql);
			
			$sql="DELETE FROM members WHERE member_id = '$id'";
			$result2 = mysql_query($sql);

			$mem_path = realpath("../usr/" . $id);

			if ($handle = opendir($mem_path)) {

			    /* This is the correct way to loop over the directory. */
			    while (false !== ($file = readdir($handle))) {
			        $img_files[]  = $file;
			    }

			    closedir($handle);
			}
			$x = count($img_files) - 1;
			$i = 2;
			while($i <= $x){
				//echo $img_files[$i] . "-" . $i . "<br>";
				unlink("../usr/" . $id . "/" . $img_files[$i]);
				$i++;
			}
			rmdir("../usr/" . $id);
			$return = "mgr.index.php?n=" . $n . "&p_mode=members";
			header("location:" . $return);
			exit;
			//echo $id;
		}
	break;
	
	case "save_member_data":
		
		$sql = "UPDATE members SET active='$active',notes='$notes',";
		
			if($plan == "C"){
				 $sql .= "custom_plan='1',custom_bandwidth='$custom_bandwidth',custom_space='$custom_space',custom_branding='$c_branding',custom_portfolio='$c_portfolio',custom_slideshow='$c_slideshow',custom_spin='$c_spin'";
			}
			else {
				$sql .= "custom_plan='0',plan='$plan'";
			}
			
			if($renew == "1"){
				$renew_date = date("z") + 30;
				$sql .= ",renew='$renew_date',used_bandwidth='0',cycle_views='0'";
			}
			else{
				if($reset_bw == "1"){
					$sql .= ",used_bandwidth='0',cycle_views='0'";
				}
			}
				
		
		$sql .= " WHERE member_id = '$memid'";
		$result = mysql_query($sql);
		header("location:" . $return);
		exit;
		
	break;
}

?>

		