<?php

include("../lib/db.inc");
include("../mailer/mailer.php");
include("../lib/util.php");

extract($_GET);
extract($_POST);

if(!isset($action))
{
	die();
}

if(strtoupper($action) == "SAVEWWOW")
{
	
	echo "AHA";
}

if(strtoupper($action) == "REGISTER")
{
	$sql = "select * from users where email = '$email'";
	$data->execSQL($sql);
	if($row = $data->getRow())
	{
		$result = "{\"result\":\"User already exist\",\"user_id\":\"$row->user_id\",\"user_type_id\":\"$row->user_type_id\"}";
		//echo json_encode("User already exist");
		echo $result;
	}
	else{
		$code = getPromoCode($email);
		
		$sq = "select user_type_id from user_type where user_type_description = '$user_type' limit 0,1";
		$data->execSQL($sql);
		$user_type_id = 1; //default to normal user
		if($row = $data->getRow())
		{
			$user_type_id = $row->user_type_id;
		}
		
		$sql = "insert into users (name, email, user_type_id,psw,user_code) 
		values('$fullname', '$email', '$user_type_id','$password','$code')";
		$data->execNonSql($sql);
		//echo "$sql<br/>";
		$new_user_id = $data->insertid;		
		//echo "$new_user_id";
		$result = "{\"result\":\"User added successfully\",\"user_id\":\"$new_user_id\",\"user_type_id\":\"$user_type_id\"}";
		//echo json_encode("User added successfully");
		echo $result;
	}
}

if(strtoupper($action) == "UPDATEPASSWORD")
{
	var_dump($_GET);
	
	$sql = "update users set psw = '$password' where email = '$email'";
	//echo $sql;
	$data->execNonSql($sql);
	echo "Password Updated";
}


if(strtoupper($action) == "LOGIN")
{
	$sql = "select * from users where email = '$email' and psw = '$password'";
	$data->execSQL($sql);
	//$result = array();
	if($row = $data->getRow())
	{
		//$result[] = $row;
		$result = "{\"result\":\"User already exist\",\"user_id\":\"$row->user_id\",\"user_type_id\":\"$row->user_type_id\"}";
		//echo json_encode("User already exist");
		echo $result;
		//echo json_encode($result);
	}
	else{
		
		echo json_encode("null");
	}
	
}



if(strtoupper($action) == "FORGOTPASSWORD")
{
	$sql = "select * from users where email = '$email'";
	$data->execSQL($sql);
	if($row = $data->getRow())
	{
		$par=encryptIt("user_id=$row->user_id",true);
		$par = "resetpassword.php?q=$par";
		$url = "http://fundi.sipnet.co.za/$par";
		$message = "Please click on the link below to reset your password : <br/><br/> <a href='$url'>$url</a><br/><br/>If clicking on the link does not work, please copy the link and paste it on the address bar of your browser";
		sendmail($row->email,"$row->name $row->surname","Reset Fundi Password","Reset Fundi Password ");
		//echo $par;
	}
	else{
		echo "User with the specified email does not exist";
	}
}

if(strtoupper($action) == "RESETPASSWORD")
{
	$sql = "update ysers set password = '$password' where user_id =$user_id";
	$data->execNonSql($sql);
	$result = array();
	
	$sql = "select * from users where user_id =$user_id";
	$data->execSQL($sql);
	if($row = $data->getRow())
	{
		$result[] = $row;
		//echo json_encode($row);
	}
	echo json_encode($result);
}

if(strtoupper($action) == "MOREINFO")
{
	$sql = "update users set email = '$email', user_type_id = '$user_type_id' where user_id = '$user_id'";
	$data->execNonSql($sql);
	
}


if(strtoupper($action) == "SPONSOR")
{
	$sql = "select * from users where user_id = $user_id";
	$data->execSQL($sql);
	$name = "";
	$code = "";
	if($row = $data->getRow())
	{
		$code = $row->uniquecode;
		$name = "$row->name $row->surname";
	}
	$subject = "$name would like you to Sponsor them";
	$par = encryptIt("email=$email&user_id=$user_id",true);
	$url = "http://fundi.sipnet.co.za/sponsor.php?$par";
	$message = "<p>$subject</p><p>For $name to access more resources and lessons on the fundi platform we request a nominal fee to be paid to sustain the platform. 
	Please click on the link below to sponsor $name<br/> <br><a href='$url'>$url</a></p><br/> ";
	
	$sql = "insert into sponsor (sponsor_name ,email,user_id ,sponsor_type_id) values ('$sponsor_name','$email','$user_id','$sponsor_type_id')";
	$data->execNonSql($sql);
	sendmail($email,$sponsor_name,$message,$subject);
	
	$sql = "select * from users where email = '$email'";
	//echo $sql;
	$data->execSQL($sql);
	if($row = $data->getRow())
	{
		;
	}
	else{	
	
		$sponsor_name = explode(" ",$sponsor_name);
		$firstname = "";
		$surname = "";
		
		if(count($sponsor_name)==1)
		{
			$firstname = $sponsor_name[0];
			$surname = "";
		}
		else{
			$num = 0;
			for($i = 0;$i< count($sponsor_name)-1;$i++)
			{
				$firstname .= $sponsor_name[$i]." ";
				$num++;
			}
			$surname = $sponsor_name[$num];			
		}
	
		$code = getPromoCode($email);
		$sql = "insert into users (name,surname,email,password,user_type_id,uniquecode) 
		values('$firstname','$surname','$email','$email','$sponsor_type_id','$code')";
		//echo $sql;
		$data->execNonSql($sql);
	}
	echo $url ;
}

if(strtoupper($action) == "SENDEMAIL")
{
	//$filename = "/home/sipnetco/sipnet.co.za/fundi/emailtemplate/invite.html";
	sendmail($to_email,$to_fullname,$message,$subject);
}



function sendmail($to_email,$to_fullname,$message,$subject)
{
	global $data;
	$mailer = new Mailer();
	
	$filename = "../emailtemplate/invite.html";
	$handler = fopen($filename,'r');
	$content = fread($handler,filesize($filename));
	
	$to_email = str_replace("~"," ",$to_email);
	$to_fullname = str_replace("~"," ",$to_fullname);
	$message =  str_replace("~"," ",$message);
	$subject =  str_replace("~"," ",$subject);
	//var_dump($mailer->ProcessMail($to_email,$to_fullname,$message,$subject));
	$content = str_replace(":NAME","$to_fullname",$content);
	$content = str_replace(":Message","$message",$content);
	$res = @$mailer->ProcessMail($to_email,$to_fullname,$content,$subject);
	$is_sent = $res?1:0;
	$sql = "insert into message(message,to_name,to_email ,is_sent,date_sent)
	values ('$message','$to_fullname','$to_email',$is_sent,now())";
	//echo $sql;
	$data->execNonSql($sql);
	
}





?>