<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: REGISTRATION.INC.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$reefless -> loadClass('Account');
$step1 = $check_len = $rlValid -> xSql($_POST['f']);
$data = $errors = array();

// get account types list
$account_types = $rlAccount -> getAccountTypes('visitor');

// check username for exist
$username = $step1['username'];
$user_exist = $rlDb -> getOne('Username', "`Username` = '{$username}'", 'accounts');

if ( !empty($user_exist) )
{
	array_push($errors, $iPhone -> pValid(str_replace(array('<b>{username}</b>', '{username}'), "'{$username}'", $lang['notice_account_exist'])));
}

// check e-mail
$e_mail = $step1['email'];
if ( !empty($e_mail) && !$rlValid -> isEmail($e_mail) )
{
	array_push($errors, $iPhone -> pValid($lang['notice_bad_email']));
}

// check dublicate e-mail
if ( $config['account_email_checking'] )
{
	$email_exist = $rlDb -> getOne('Mail', "`Mail` = '{$e_mail}'", 'accounts');
	if ( !empty($email_exist) )
	{
		array_push($errors, $iPhone -> pValid(str_replace(array('<b>{email}</b>', '{email}'), "'{$e_mail}'", $lang['notice_account_email_exist'])));
	}
}

// check personal address
if ( $account_types[$step1['type']]['Own_location'] && !empty($step1['location']) )
{
	$location = trim($step1['location']);
	$wildcard_deny = explode(',', $config['account_wildcatd_deny']);
	$rlDb -> setTable('pages');
	$deny_pages_tmp = $rlDb -> fetch(array('Path'), null, "WHERE `Path` <> ''");
	foreach ($deny_pages_tmp as $deny_page)
	{
		array_push($wildcard_deny, $deny_page['Path']);
	}
	unset($deny_pages_tmp);

	$rlValid -> sql($location);
	preg_match('/[\W]+/', str_replace(array('-', '_'), '', $location), $matches);

	if ( empty($location) || !empty($matches) )
	{
		array_push($errors, $iPhone -> pValid($lang['personal_address_error']));
	}

	// check for uniqueness
	else if ( in_array($location, $wildcard_deny) || $rlDb -> getOne('ID', "`Own_address` = '{$location}'", 'accounts') )
	{
		array_push($errors, $iPhone -> pValid($lang['personal_address_in_use']));
	}
}
elseif ( $account_types[$step1['type']]['Own_location'] && empty($step1['location']) )
{
	array_push($errors, $iPhone -> pValid($lang['personal_address_error']));
}

if ( !empty($errors) )
{
	$data['registration'] = false;
	$data['message'] = implode("\n", $errors);
}
else
{
	$reefless -> loadClass('Actions');
	$reefless -> loadClass('Mail');

	$data = array(
		'Type' => $account_types[$step1['type']]['Key'],
		'Username' => $step1['username'],
		'Own_address' => $step1['location'],
		'Password' => md5($step1['password']),
		'Password_tmp' => $config['account_email_confirmation'] ? $step1['password'] : '',
		'Display_email' => 0,
		'Mail' => $step1['email'],
		'Date' => 'NOW()'
	);

	// check administrator confirmation
	$data['Status'] = $config['account_admin_confirmation'] ? 'pending' : 'active';

	if ( $config['account_email_confirmation'] )
	{
		$confirm_code = md5(mt_rand());
		$data['Confirm_code'] = $confirm_code;
		$data['Status'] = 'incomplete';
	}

	$rlHook -> load('phpRegistrationBeforeInsert');

	// insert data
	if ( $rlActions -> insertOne($data, 'accounts', array('Username', 'Password', 'Password_tmp')) )
	{
		$account_id = mysql_insert_id();
		$r_pages = $rlDb -> getAll("SELECT `Key`, `Path` FROM `". RL_DBPREFIX ."pages` WHERE `Key` = 'confirm' OR `Key` = 'login'");
		$confirm_page = $r_pages[0]['Key'] == 'confirm' ? $r_pages[0]['Path'] : $r_pages[1]['Path'];
		$login_page = $r_pages[1]['Key'] == 'login' ? $r_pages[1]['Path'] : $r_pages[0]['Path'];

		// prepare email confirmation
		if ( $config['account_email_confirmation'] )
		{
			// create activation link
			$activation_link = RL_URL_HOME;
			$activation_link .= $config['mod_rewrite'] ? "{$confirm_page}.html?key=" : "?page={$confirm_page}&key=" ;
			$activation_link .= $confirm_code;
			$activation_link = '<a href="'. $activation_link .'">'. $activation_link .'</a>';

			$mail_tpl = $rlMail -> getEmailTemplate('account_created_incomplete');
			$find = array('{activation_link}', '{username}');
			$replace = array($activation_link, $username);
			$mail_tpl['body'] = str_replace($find, $replace, $mail_tpl['body']);
		}
		else
		{
			$account_area_link = RL_URL_HOME;
			$account_area_link .= $config['mod_rewrite'] ? $login_page .'.html' : '?page='. $login_page;
			$account_area_link = '<a href="'. $account_area_link .'">'. $lang['blocks+name+account_area'] .'</a>';

			$mail_tpl = $rlMail -> getEmailTemplate($config['account_admin_confirmation'] ? 'account_created_pending' : 'account_created_active');
			$find = array('{username}', '{password}', '{name}', '{account_area}' );
			$replace = array($profile['username'], $profile['password'], $username, $account_area_link);
			$mail_tpl['body'] = str_replace($find, $replace, $mail_tpl['body']);
		}

		// send e-mail to new user
		$rlMail -> send($mail_tpl, $step1['email']);

		// prepare admin notification e-mail
		$mail_tpl = $rlMail -> getEmailTemplate('account_created_admin');

		$details_link = RL_URL_HOME . ADMIN .'/index.php?controller=accounts&action=view&userid='. $account_id;
		$details_link = '<a href="'. $details_link .'">'. $details_link .'</a>';

		$find = array('{first_name}', '{last_name}', '{username}', '{join_date}', '{status}', '{details_link}');
		$replace = array('Not specified', 'Not specified', $step1['username'],
			date(str_replace(array('b', '%'), array('M', ''), RL_DATE_FORMAT)),
			$lang[$data['Status']], $details_link );
		$mail_tpl['body'] = str_replace($find, $replace, $mail_tpl['body']);

		if ( $config['account_admin_confirmation'] )
		{
			$activation_link = RL_URL_HOME . ADMIN .'/index.php?controller=accounts&action=remote_activation&id='. $account_id . '&hash='. md5($rlDb -> getOne('Date', "`ID` = '{$account_id}'", 'accounts'));
			$activation_link = '<a href="'. $activation_link .'">'. $activation_link .'</a>';
			$mail_tpl['body'] = preg_replace('/(\{if activation is enabled\})(.*)(\{activation_link\})(.*)(\{\/if\})/', '$2 '. $activation_link .' $4', $mail_tpl['body']);
		}
		else
		{
			$mail_tpl['body'] = preg_replace('/\{if activation is enabled\}(.*)\{\/if\}/', '', $mail_tpl['body']);
		}

		// send e-mail to admin
		$rlMail -> send($mail_tpl, $config['site_main_email']);

		if ( $config['account_auto_login'] && !$config['account_email_confirmation'] && !$config['account_admin_confirmation'] )
		{
			$rlAccount -> login($step1['username'], $step1['password']);
			$data['autoLogin'] = true;
		}

		$data['registration'] = true;
		$data['message'] = $lang['notice_reg_complete'];
	}
}

$iPhone -> printAsXml($data);