<?php 
//+-----------------------------------------------+//
//+ Copyright Floopo.com
//+ Register.php
//+ Registration component
//+-----------------------------------------------+//

defined('BASEPATH') or die('You can not directly access this file');//check its within this framework



//____DEPENDENCIES_________________________________
include_once (BASEPATH . '/lib/lib_formprocessor/formprocessor.php');
include_once (BASEPATH . '/lib/lib_postage/postage.php');

//____DEFAULT TEMPLATE_____________________________
$inner_template = TEMPLATES . 'tpl_register.htm';


//____PRIVACY SETTINGS_____________________________
if($config['allow_registration'] == 0){
$_SESSION["notice"] = 4;
@header('Location: '.$env_base_url.'/notice'); //notice dodes can be added in com_notice
}


//____Mysql: Connect to dbase______________________
//$quickmysql_debug = 1;
$dbase = new QuickMysql($config['db_name'], $config['db_user'], $config['db_pass'], $config['db_host']);


//____Reserved Names______________________
$reserved_array = explode(',', $config['reserved_names']); //add more in siteadmin
$system_reserved_array = explode(',', $system_reserved); //built in system reserved names


//____Form: validate submitted form_________________
if (isset($_POST['submit']) && $env_user_name == '' ) {
 
    $process = new FormProcessor();

    //check form filled in
    ($process->RequiredFields('all')) ? null:$display_notification = $lang['fill_in_all'];

    /*
	//check 'checkbox' filled in
    if ($process->obj_status) {
        ($process->RequiredCheckBox('agree')) ? null:$display_notification = 'You must agree to our terms';
    }*/

    //check email format
    if ($process->obj_status) {
        ($process->CheckFormatEmail('email_address')) ? null:$display_notification = $lang['invalid_email'];
    }

    //check match
    if ($process->obj_status) {
        ($process->CheckFieldMatch('email_address, email_address_2')) ? null:$display_notification = $lang['email_match_error'];
    }
    
    
    //____Check if name is Valid_______________________
    if($process->obj_status){
    if (!ereg('^[a-zA-Z0-9]*$', $_POST['user_name']) || in_array($_POST['user_name'],$reserved_array) || in_array($_POST['user_name'],$system_reserved_array)) {
    $display_notification = $lang['invalid_username'];
    }else{
    $name_check = 1;;//passed
    }
    }
    

    //____Mysql: Create New User_______________________
    if ($process->obj_status && $name_check == 1) {

        //Clean post data
        foreach ($_POST as $key => $value) {
            $$key = Sanatize(@mysql_real_escape_string($value));
            ($key == 'captcha')? $$key = $value:NULL; //no need to escape captcha code
        }

        //check captca (always turn to lower)
        if (strtolower($captcha) == $_SESSION['com_captcha']) {
        
        
            //Generate Random Code for user
            $activation_code = RandomCode(15);
            $location_ip = $_SERVER['REMOTE_ADDR'];
            $password = md5($password);

            //add user to database
            $user_name = strtolower($user_name);
            if (!$dbase->RecordExists("SELECT user_id
			                           FROM member_profile
									   WHERE user_name = '$user_name'
									   OR email_address = '$email_address'")) {
            	
               $member_message = $config['members_welcome_message'];
               
			   $dbase->InsertRecord("INSERT INTO member_profile (
			                         user_name, email_address, password, welcome_message, date_created, location_ip, activation_code, date_added_short
									 )values(
									 '$user_name', '$email_address', '$password', '$member_message', NOW(), '$location_ip', '$activation_code', NOW())");

               $dbase->InsertRecord("INSERT INTO web_profiles (
			                         user_name, site_name, site_url, full_name, location
									 )values(
									 '$user_name', NULL, NULL, NULL, NULL)");

                //success notifications display
                $display_message = $lang['complete_registration'];
                $inner_template = TEMPLATES . 'tpl_notification.htm';
             
			 $registration_complete = true;   
                
           }
            else {

                //display form and error
                $display_notification = $lang['username_not_available'];

            }
        }
        else {
            $display_notification = $lang['incorrect_code'];
        }
    }
    
    //____Send welcome email_______________________
    if($registration_complete == true){ 
    //Get Welcome Message
    $result = $dbase->SelectRecord("SELECT message, subject FROM system_messages WHERE title = 'welcome'");
    
    //Send Email
    $notify = new Postage($result['message']);
    $notify->EmailFormat('html');
    $notify->ToFromSubject($email_address, $config['system_email'], $result['subject']);
    $notify->EmailVars(array('user_name'=>$user_name,
                             'activation_code'=>$activation_code,
                             'site_name'=>$config['site_name'],
                             'site_url'=>$config['site_url'] ));
    $notify->SendNow();
   }
}

//____ERROR CONTROL - Already Logged in___________
if($env_user_name != '' && $env_salt != ''){
$display_notification = $lang['already_logged_in'];
$inner_template = TEMPLATES . 'tpl_index.htm';
}



//____Display any output_________________________
$TBS = new clsTinyButStrong;
$TBS->NoErr = true;
$TBS->LoadTemplate(TPL_WRAPPER);
$TBS->MergeField('formdata', $_POST);
$TBS->MergeField('config', $config);
$TBS->MergeField('lang', $lang);
$TBS->Render = TBS_OUTPUT;
$TBS->Show();
@mysql_close();
?>