<?php
if ( !defined( 'SMARTY_DIR' ) ) {

	include_once( 'init.php' );
}
include( 'sessioninc.php' );

$userid = $_SESSION['UserId'];

$modified['username'] = $username = addslashes(stripEmails(strip_tags($_POST['txtusername'] )));

$modified['firstname'] = $firstname = addslashes(stripEmails(strip_tags(trim($_POST[ 'txtfirstname' ]))));

$modified['lastname'] = $lastname = addslashes(stripEmails(strip_tags(trim($_POST[ 'txtlastname' ]))));

$modified['about_me'] = $about_me = addslashes(stripEmails(strip_tags(trim($_POST[ 'about_me' ]))));

$modified['email'] = $email = addslashes(strip_tags(trim($_POST[ 'txtemail' ])));

$modified['gender'] = $gender = $_POST[ 'txtgender' ];

$modified['couple_usernames'] = $couple_usernames = addslashes(strip_tags(trim($_POST[ 'couple_usernames' ])));

$modified['birthmonth'] = $birthmonth = $_POST[ 'txtbirthMonth' ];

$modified['birthday'] = $birthday = $_POST[ 'txtbirthDay' ];

$modified['birthyear'] = $birthyear = $_POST[ 'txtbirthYear' ];

$modified['birth_date'] = strtotime($birthyear.'-'.$birthmonth.'-'.$birthday);

$modified['country'] = $from = strip_tags($_POST[ 'txtfrom' ]);

$modified['county'] = $county = strip_tags($_POST[ 'txtcounty' ]);

$modified['zip'] = $zip = strip_tags(trim($_POST[ 'txtzip' ]));

$modified['timezone'] = $timezone = $_POST['txttimezone'];

$modified['lookgender'] = $lookgender = $_POST[ 'txtlookgender' ];

$modified['lookagestart'] = $lookagestart = $_POST[ 'txtlookagestart' ];

$modified['lookageend'] = $lookageend = $_POST[ 'txtlookageend' ];

$modified['city'] = $city = trim($_POST[ 'txtcity' ]);

$modified['state_province'] = $stateprovince = addslashes(strip_tags(trim($_POST[ 'txtstateprovince' ])));

$modified['address1'] = $address1 = addslashes(stripEmails(strip_tags(trim($_POST['txtaddress1' ]))));

$modified['address2'] = $address2 = addslashes(stripEmails(strip_tags(trim($_POST['txtaddress2' ]))));

$modified['lookcountry'] = $lookfrom = $_POST[ 'txtlookfrom' ];

$modified['lookcounty'] = $lookcounty = addslashes(strip_tags($_POST[ 'txtlookcounty' ]));

$modified['lookcity'] = $lookcity = addslashes(strip_tags(trim($_POST[ 'txtlookcity' ])));

$modified['lookstate_province'] = $lookstateprovince = addslashes(strip_tags(trim($_POST[ 'txtlookstateprovince' ])));

$modified['lookzip'] = $lookzip = addslashes(strip_tags(trim($_POST[ 'txtlookzip' ])));

$modified['allow_viewonline'] = $viewonline = $_POST[ 'txtviewonline' ];

$modified['lookradius'] = $lookradius = addslashes(strip_tags(trim($_POST[ 'lookradius' ])));

$modified['radiustype'] = $radiustype = trim($_POST[ 'radiustype' ]);

$err =0;

if ( $firstname == '' ) {

	$err = FIRSTNAME_REQUIRED;

} elseif ( $lastname == '' ) {

	$err = LASTNAME_REQUIRED;

} elseif ( $email == '' ) {

	$err = EMAIL_REQUIRED;

} elseif ( strlen( $firstname ) > 50 ) {

	$err = FIRSTNAME_LENGTH;

} elseif ( strlen( $lastname ) > 25 ) {

	$err = LASTNAME_LENGTH;

} elseif ( strlen( $email ) > 255 ) {

	$err = EMAIL_LENGTH;

} elseif ( strpos( $firstname, '@' ) > 0 ) {

	$err = FIRSTNAME_REQUIRED;

} elseif ( strpos( $lastname, '@' ) > 0 ) {

	$err = LASTNAME_REQUIRED;

} elseif ( preg_replace( "/[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}/i", "", $email ) != "" ) {

	$err = EMAIL_REQUIRED;

} elseif ( ! checkdate( $birthmonth, $birthday, $birthyear ) ) {

	$err = INVALID_BIRTHDATE;

} elseif (($config['accept_about_me'] == 'Y' or $config['accept_about_me'] =='1') &&  $config['about_me_mandatory'] == 'Y' && $about_me == '') {

	$err = ABOUT_ME_MANDATORY;

} elseif ($config['accept_country'] == 'Y' or $config['accept_country'] == '1') {

	if ($config['accept_state'] == 'Y' or $config['accept_state'] == "1") {

		if ( $stateprovince == '' && $config['state_mandatory'] == 'Y' ) {

			$err = STATEPROVINCE_NEEDED;

		} elseif ( $county == ''  && $config['county_mandatory'] == 'Y' && ($config['accept_county'] == 'Y' or $config['accept_county'] == "1")) {

			$err = COUNTY_REQUIRED;

		} elseif ($config['accept_city'] == 'Y' or $config['accept_city'] == "1") {

			if ($city == ''  && $config['city_mandatory'] == 'Y') {

				$err = CITY_REQUIRED;

			} elseif ( strlen( $city ) > 255 ) {

				$err = CITY_LENGTH;
			}

		} elseif ( $zip == ''  && $config['zipcode_mandatory'] == 'Y' && ($config['accept_zipcode'] == 'Y' or $config['accept_zipcode'] == "1")) {

			$err = ZIP_REQUIRED;
		}
	}

} elseif ( $lookageend < $lookagestart && ($config['accept_lookage'] == 'Y' or $config['accept_lookage'] == "1") ) {

	$err = BIGGER_STARTAGE;

} elseif ($timezone == '-25' && $config['timezone_mandatory'] == 'Y' && ($config['accept_timezone'] == 'Y' or $config['accept_timezone'] == "1" ) ) {

	$err = INVALID_TIMEZONE;

}

if ($gender == 'C' ) {
	if (trim($couple_usernames) == '' or substr_count($couple_usernames,',') <= 0 or !isset($couple_usernames) ) {
		$err = COUPLE_USERNAMES_MISSING;
	} else {
		$userok = 0;
		$usrs=0;
		foreach(explode(',',$couple_usernames) as $k => $uname) {
			if (trim($uname) != '') {
				$user = $osDB->getOne('select username from ! where username = ?', array(USER_TABLE, trim($uname)) );
				$usrs++;
				if ($user != trim($uname)) {$userok++;}
			}
		}
		if ($userok > 0 ) {$err = 129; }
		if ($usrs < 2) {$err = COUPLE_USERNAMES_MISSING;}
	}
}


$_SESSION['modifiedrow'] = $modified;

unset($modified);

if (  $err != 0 ) {

	header ( "location: edituser.php?errid=$err" );

	exit();

}

$currec = $osDB->getRow('select * from ! where id = ?',array(USER_TABLE, $userid) );
$modified = 0;
$active = $currec['active'];
$status=$currec['status'];
$actkey = $currec['actkey'];
if ($config['update_profile_inactive'] == 'Y') {
	/* if profile is modified, make it for approval  - get current user data */
	/* Now check if any field is modified  */
	$modify_fields=array('firstname','lastname','email','about_me','gender','birth_date','country','zip','city','state_province','county','address1','address2','lookcountry','lookcounty','lookstate_province','lookcity','lookzip','couple_usernames');

	foreach ($modify_fields as $k) {
		if ( $k == 'address1' && $modified[$k] != $currec['address_line1'] ) {
			$modified++;
		} elseif ($k == 'address2' && $modified[$k] != $currec['address_line2'] ) {
			$modified++;
		} elseif ($modified[$k] != $currec[$k]) {
			$modified++;
		}
	}

	if ($email != $currec['email'] && $config['bypass_regconfirm'] != 'Y') {
		/* Email is changed. Must reconfirm the profile */
		$actkey = md5( $email . time() );
		$active = '0';
	}

	if ($modified > 0) {
		$status = 'approval';
	}
}

/* now get the latitude and longitude for zip and lookzip */
if ($from == 'GB') {
   $ukzip = explode(' ',$zip);
   $zipcd = $ukzip[0];
} else {
	$zipcd = $zip;
}

$ziprec = $osDB->getRow('select latitude, longitude from ! where countrycode = ? and code = ?', array(ZIPCODES_TABLE, $from,$zipcd) );

//	$birthdate = strtotime ( $birthday . ' ' . $birthmonth . ' ' . $birthyear );
$birthdate = $birthyear . '-' . $birthmonth . '-' . $birthday;

$osDB->query( "UPDATE ! SET
					allow_viewonline 	= ?,
					email 				= ?,
					country 			= ?,
					firstname 			= ?,
					lastname 			= ?,
					gender 				= ?,
					lookgender 			= ?,
					lookagestart 		= ?,
					lookageend 			= ?,
					lookcountry 		= ?,
					address_line1 		= ?,
					address_line2 		= ?,
					state_province 		= ?,
					county 				= ?,
					city 				= ?,
					zip 				= ?,
					timezone 			= ?,
					lookzip				= ?,
					lookcity 			= ?,
					lookcounty 			= ?,
					lookstate_province 	= ?,
					lookradius			= ?,
					about_me			= ?,
					radiustype			= ?,
					couple_usernames	= ?,
					zip_latitude		= ?,
					zip_longitude		= ?,
					active				= ?,
					status				= ?,
					actkey				= ?,
					birth_date = ?
					WHERE id=?", array( USER_TABLE, $viewonline, $email, $from, $firstname, $lastname,  $gender, $lookgender, $lookagestart, $lookageend, $lookfrom, $address1, $address2, $stateprovince, $county, $city, $zip, $timezone, $lookzip, $lookcity, $lookcounty, $lookstateprovince, $lookradius, $about_me, $radiustype, $couple_usernames, $ziprec['latitude'], $ziprec['longitude'], $active, $status, $actkey, $birthdate, $userid ) );

unset($_SESSION['modifiedrow'] );

$_SESSION['FullName'] = $firstname . ' ' . $lastname;

if ($email != $currec['email'] && $config['bypass_regconfirm'] != 'Y') {
	/* Email is changed. Must reconfirm the profile. Send email to user */

	$Subject = get_lang('profile_modify_email_sub');

	$From = $config['admin_email'];

	$To = $firstname.' '.$lastname.'<'.$email.'>';

	$body = get_lang('profile_modify_email', MAIL_FORMAT);

	$body = str_replace( '#FirstName#',  $firstname , $body );

	$body = str_replace( '#ConfCode#',  $actkey , $body );

	$body = str_replace( '#ConfirmationLink#',  HTTP_METHOD . $_SERVER['SERVER_NAME'] . DOC_ROOT . 'completemod.php?confcode' , $body );

	$body = str_replace( '#Email#',  $email , $body );

	$sendok = mailSender($From, $To, $email, $Subject, $body);
}

if ($config['update_profile_inactive'] == 'Y' && $modified > 0 ) {
	/* Inform the admin about user profile changes */
    $opt_lang = $_SESSION['opt_lang'];

	$_SESSION['opt_lang'] = $config['admin_lang'];

	$body = get_lang('modifieduser', MAIL_FORMAT);

	$body = str_replace( '#UserName#',  $username , $body );

	$Subject = get_lang('modifieduser_sub'). ' - ' . $config['site_name'];

	$From = $config['admin_email'];

	$To = $config['admin_email'];

	$email = $config['admin_email'];

	$sendok=mailSender($From, $To, $email, $Subject, $body);

	$_SESSION['opt_lang'] = $opt_lang;

	unset($opt_lang);
}

unset($firstname, $lastname);

$nextsectionid = $osDB->getOne('select id from ! where enabled = ? order by displayorder asc',array(SECTIONS_TABLE, 'Y') );

header( 'location: editquestions.php?sectionid='.$nextsectionid );

?>