<?php

						/*************************************\
						
						    Handshakes Professional (3.0)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////
						
						admin_validate_section("manage",11);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':
									manage_unverified();
									break;
																		
								case 'resend_verification':
									resend_verification();
									break;
									
								case 'verify':
									verify_members();
									break;
								
							}//switch
							
						///////////////////////////////////////
						
function manage_unverified(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 11;
	$data["title_id"] = "34";	
	$p = $GLOBALS["Get"]->val("p");
	
	$data["title"] = "Handshakes::Admin Console - Manage Unverified";	
	
	$sql_query_total = "SELECT COUNT(members.mem_id) ";
	$sql_query_result = "SELECT members.mem_id ";
	
	$sql_query="FROM members,profiles
				WHERE members.mem_id = profiles.mem_id
				AND members.verified='0'
				AND members.mem_id > 100000
				ORDER BY profiles.joined DESC";
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
	
	$sql_query.= " LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query_result.$sql_query);	
	
	$data["cards"] = admin_build_cards($result,"index.php?section=manage_unverified&p=".$p);
	$data["cards_total"] = count($result);	
	
	$data["pages"] = handle_multipages($p,$total,"index.php?section=manage_unverified",30);	
	
	$data["link"] = "index.php?section=manage_unverified&p=".$p;
	
	$data["onload"] = "";
	
	$layout[0] = "unverified_list";
	
	admin_output($layout,$data);
	
}//manage_unverified

function resend_verification(){
	
	admin_validate();
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	$mem_id = $GLOBALS["Get"]->val("mem_id");
	
	if(is_array($mem_id) && count($mem_id)){
		foreach($mem_id as $member){
			do_resend_verification($member);
		}//foreach
	}//if
	elseif($mem_id){
		do_resend_verification($mem_id);
	}//else
	
	load_page($ret_link);
	
}//resend_verification

function do_resend_verification($mem_id){
	
	$sql_query="SELECT email,username
				FROM members
				WHERE mem_id=".$mem_id;
	
	$member = $GLOBALS["DB"]->row($sql_query);
	
	$sql_query="SELECT val,password
				FROM verification
				WHERE email='".$member["email"]."'";
	
	$verify = $GLOBALS["DB"]->row($sql_query);
	
	//building email message
	$mail_details = array (		
		"mes_id"    => 0,
		"recipient" => $mem_id,
		"link"      => URL."/index.php?handler=join&action=verify&val=".$verify["val"],
		"code"      => $verify["val"],
		"password"  => $verify["password"]
	);
	
	if($GLOBALS["settings"][username_use] < 2)
		$mail_details["login"] = $member["email"];
	else
		$mail_details["login"] = $member["username"];
		
	include_once(DOC_ROOT."/classes/Mail.class.php");
		
	$GLOBALS["Mail"]->details = $mail_details;
	$GLOBALS["Mail"]->send_external();
	//email sent
	
}//do_resend_verification

function verify_members(){
	
	admin_validate();
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	$mem_id = $GLOBALS["Get"]->val("mem_id");
	
	if(is_array($mem_id) && count($mem_id)){
		foreach($mem_id as $member){
			do_verify_member($member);
		}//foreach
	}//if
	elseif($mem_id){
		do_verify_member($mem_id);
	}//else
	
	load_page($ret_link);
	
}//verify_members

function do_verify_member($mem_id){
	
	$sql_query="SELECT email
				FROM members
				WHERE mem_id=".$mem_id;
	
	$email = $GLOBALS["DB"]->single($sql_query);	
	
	$GLOBALS["DB"]->values = array($email);
	
	$sql_query="SELECT email,password,inv_id
				FROM verification
				WHERE email=':0:'";
	
	$get = $GLOBALS["DB"]->fetch($sql_query);
	
	$sql_query="UPDATE members
				SET verified='1'
				WHERE mem_id='".$mem_id."'";
	
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query="DELETE FROM verification
				WHERE email='".mysql_escape_string($get->email)."'";
	
	$GLOBALS["DB"]->execute($sql_query);
	
	if(!empty($get->inv_id)){
		$sql_query="SELECT mem_id
					FROM invitations
					WHERE inv_id=':0:'";
		
		$GLOBALS["DB"]->values = array ($get->inv_id);
		$invitee = $GLOBALS["DB"]->single($sql_query);	
		
		$GLOBALS["Network"]->add_friend($mem_id,$invitee);
		
		$sql_query="UPDATE invitations
					SET stat='f'
					WHERE inv_id=':0:'";		
		
		$GLOBALS["DB"]->values = array ($get->inv_id);
		$GLOBALS["DB"]->execute($sql_query);
		
		$sql_query="UPDATE invitations
					SET stat='j'
					WHERE email=':0:'
					AND inv_id!=':1:'";
		
		$GLOBALS["DB"]->values = array ($get->email,$get->inv_id);
		$GLOBALS["DB"]->execute($sql_query);
			
	}//if	
	
	if($GLOBALS["settings"]["default_friend"])
		$GLOBALS["Network"]->add_friend($mem_id,$GLOBALS["settings"]["default_friend"]);
	
}//do_verify_member

?>