<?php
############################################################
# \-\-\-\-\-\-\     AzDG  - S C R I P T S    /-/-/-/-/-/-/ #
############################################################
# AzDGDatingMedium        Version 1.9.3                    #
# Written by              AzDG (support@azdg.com)          #
# Created 24/10/02        Last Modified 22/03/06           #
# Scripts Home:           http://www.azdg.com              #
############################################################
# File name               check.php                        #
# File purpose            Check for add user to DB         #
# File created by         AzDG <support@azdg.com>          #
############################################################
include_once 'include/config.inc.php';
include_once 'include/options.inc.php';
include_once 'include/security.inc.php';
include_once 'include/functions.inc.php';
include_once 'templates/'.C_TEMP.'/config.php';

if (!isset($id) || !isset($code) || !is_numeric($id) || !is_md5($code))
{
	// Code must content only chars and numbers :)
	include_once C_PATH.'/header.php';
	printm($w[1].'3');
}

/// Check for users with code expired and remove these users
$to_remove=array();
$temp = mysql_query("SELECT id FROM ".C_MYSQL_TEMP." WHERE date < DATE_SUB(NOW(), INTERVAL ".C_REG_DAYS." DAY)");
while ($i = mysql_fetch_array($temp)){$to_remove[]=$i['id'];}
if(count($to_remove)) RemoveUser($to_remove);
/////////////



$temp = mysql_query("SELECT count(*) as total FROM ".C_MYSQL_TEMP." WHERE id='".$id."' AND code='".$code."' limit 1");
$row = mysql_fetch_array($temp);
$count = $row['total'];
if ($count != '0')
{
	mysql_query("DELETE FROM ".C_MYSQL_TEMP." WHERE id='".$id."' AND code='".$code."'");
	$result = mysql_query("SELECT t1.*,t2.* FROM ".C_MYSQL_MEMBERS_MAIN." as t1,".C_MYSQL_MEMBERS_SEC." as t2 WHERE t1.id = '".$id."' AND t1.id = t2.id");
	while ($i = mysql_fetch_array($result))
	{
		if (C_CHECK_REGISTER == '3')
		{
			$status = (trim($i['files']) == '' && C_CHECK_WPHOTOS) ? '7' : '1';
			$str = $w[159];

			if (C_NOTIFY_ADMIN)
			{
				$subject = $w[405];
				$tm = array(C_SNAME);
				$message = template($w[407], $tm);
				sendmail(C_FROMM, C_ADMINM, $subject, $message, 'text');
			}
		}
		else
		{
			$status = (C_SHOW_REGISTERED) ? 10 : 7;
			$str = $w[46];
		}
		mysql_query("UPDATE ".C_MYSQL_MEMBERS_MAIN." SET status='".$status."' WHERE id='".$id."'");
		mysql_query("UPDATE ".C_MYSQL_MEMBERS_SEC." SET status='".$status."' WHERE id='".$id."'");
		mysql_query("UPDATE ".C_MYSQL_MEMBERS_EXT." SET status='".$status."' WHERE id='".$id."'");


		$tm = array(C_SNAME);
		$subject = template($w[195], $tm);
		switch (C_ID)
		{
			case '0':
				$sendid = $i['username'];
				break;
			case '2':
				$sendid = $i['email'];
				break;
			default:
				$sendid = $id;
				break;
		}
		$tm = array($sendid, $i['password'], C_SNAME);
		$message = template($w[291], $tm);

		// Sending email with password to user, if user status is active and admin allow to send congratulation emails
		if (($status >= '7') && C_REG_EMAIL) sendmail(C_FROMM, $i['email'], $subject, $message, 'text');

		if (C_NOTIFY_ADMIN)
		{
			$subject = $w[405];
			$tm = array(C_SNAME, C_URL.'/view.php?l='.C_ADMINLANG.'&id='.$id);
			$message = template($w[406], $tm);
			sendmail(C_FROMM, C_ADMINM, $subject, $message, 'text');
		}

		if (($status >= '7') && (C_AUTO_LOGIN))
		{
			if(C_VERIFY_IMAGE_LOGIN) 
			{
				@$_SESSION['vc'] = "REGISTER";
				$tmps="&verifyimage=REGISTER";
			} else $tmps="";
  		?>
        <script language="JavaScript">
        <!--
        window.location.replace( '<?=C_URL."/login.php?l=".$l."&".s()."&p=s&id=".$sendid."&password=".$i['password'].$tmps?>');
        // -->
        </script>
        <?php
        die();
		}
	}
	include_once C_PATH.'/header.php';
	printm($str,2);
}

else
{
	unset($id);
	include_once C_PATH.'/header.php';
	printm($w[161]);
}
include_once C_PATH.'/footer.php';
?>