<?php

function cloudfw_login_form(){
if (!(current_user_can('read'))){ 

global $user_login;
global $current_user;
get_currentuserinfo();

?>

<?php cloudfw_get_ahook("cloudfw_user_login_page"); ?> 

<div id="cloudfw-login-container">
<h2><?php _e('Login','cloudfw'); ?></h2>

<div id="user-ops-left">

<form action="<?php echo home_url(); ?>/wp-login.php" method="post" class="login_form" autocomplete="off">
<div class="form-container">

    <p class="form-sub-container">
    	<label for="log"><?php _e('Username','cloudfw');?></label>
    	<input type="text" name="log" id="log" value="<?php echo esc_html(stripslashes($user_login), 1) ?>" size="20" class="login-input" />
    </p>
    <div class="cf"></div>
    <p>
    	<span class="form-sub-container">
        <label for="pwd"><?php _e('Password','cloudfw');?></label> 
    	<input type="password" name="pwd" id="pwd" size="20" class="login-input" />
        </span>
        
    <p id="rememberme-container" class="form-sub-container">
        <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /><?php _e('Remember me','cloudfw');?></label>
    </p>

	</p>
	<div class="cf"></div>
</div>
<div class="cf"></div>
<p><input type="submit" name="submit" value="<?php _e('Login','cloudfw');?>" class="button" autocomplete="off" /></p>
<p><input type="hidden" name="redirect_to" value="<?php echo home_url(); ?>" /> </p>

</form>

</div>

<div id="user-ops-right">
	<?php cloudfw_get_ahook("cloudfw_user_page_sidebar", 'login'); ?>   
</div>

<div class="cf"></div>

</div>
<?php } else { ?>
<div id="cloudfw-login-container">
	<?php cloudfw_user_update_form();?> 
</div>
<?php }
}

function cloudfw_register_form(){	
		
?>

<?php 	
if (!(current_user_can('read'))){
if (get_option('users_can_register')) : ?>

<h2><?php _e('Register New User','cloudfw'); ?></h2>

<div id="user-ops-left">

<form action="" name="cloudfw-user-registerform" id="cloudfw-user-registerform" class="login_form" method="post">
    
    <p class="form-username form-sub-container">
        <label for="user_name"><?php _e('Username (required)', 'cloudfw'); ?></label>
        <input class="login-input" name="user_name" type="text" id="user_name" value="<?php echo esc_html( $_REQUEST['user_name'], 1 ); ?>" />
    </p>
    
    <div class="cf"></div>
    
    <p class="form-email form-sub-container">
    <label for="user_email"><?php _e('E-mail (required)', 'cloudfw'); ?></label>
        <input class="login-input" name="user_email" type="text" id="user_email" value="<?php echo esc_html( $_POST['user_email'], 1 ); ?>" />
    </p>
    
    <div class="cf"></div>
    
    <p class="first_name form-sub-container">
        <label for="first_name"><?php _e('First Name', 'cloudfw'); ?></label>
        <input class="login-input" name="first_name" type="text" id="first_name" value="<?php echo esc_html( $_POST['first_name'], 1 ); ?>" />
    </p>

	<div class="cf"></div>

    <p class="last_name form-sub-container">
        <label for="last_name"><?php _e('Last Name', 'cloudfw'); ?></label>
        <input class="login-input" name="last_name" type="text" id="last_name" value="<?php echo esc_html( $_POST['last_name'], 1 ); ?>" />
    </p>
    
    <div class="cf"></div>

    <p class="nickname form-sub-container">
        
        <input class="login-input" name="nickname" type="hidden" id="nickname" value="<?php echo esc_html( $_POST['nickname'], 1 ); ?>" />
    </p>
    
    <div class="cf"></div>

	<?php cloudfw_get_ahook('cloudfw_signup_extra', 'register'); ?> 
                
    <p id="reg_passmail"><?php _e('A password will be e-mailed to you.','cloudfw'); ?></p>

    <p class="form-submit">

    <?php echo $referer; ?>
        <input type="submit" name="wp-submit" id="wp-submit" class="button green" value="<?php if ( current_user_can( 'create_users' ) ) _e('Add User', 'cloudfw'); else _e('Register', 'cloudfw'); ?>" />
        <?php wp_nonce_field( 'cloudfw-add-user' ) ?>
        </p>
 
</form>

</div>

<div id="user-ops-right">
	<?php cloudfw_get_ahook('cloudfw_user_page_sidebar', 'register'); ?>   
</div>

<div class="cf"></div>

<?php else: ?>

    <div class="cl cl_4_3">
        <?php echo init_boxes(array('title' => __('Registration Disabled','cloudfw')), __('User registration has been disabled','cloudfw'), 'caution'); ?>
    </div>
    
<div class="cf"></div>
<?php endif; 

}else{ ?>
     
<div class="cl cl_4_3">
        <?php echo init_boxes(array('title' => __('You have already logged in.','cloudfw')),NULL, 'caution'); ?>
    </div>
    
<div class="cf"></div>

<?php }//endif - is_login

}
	
	
	
	
function cloudfw_user_update_form(){
			
	global $current_user;
	get_currentuserinfo();
	
			?>
        
<h2><?php _e('Account Informations','cloudfw'); ?></h2>

<div id="user-ops-left">
<form action="" name="cloudfw-user-updateform" id="cloudfw-user-updateform" class="login_form" method="post">
    
    <p class="first_name form-sub-container">
        <label for="first_name"><?php _e('First Name', 'cloudfw'); ?></label>
        <input class="login-input" name="first_name" type="text" id="first_name" value="<?php the_author_meta( 'first_name', $current_user->id ); ?>" />
    </p>

	<div class="cf"></div>

    <p class="last_name form-sub-container">
        <label for="last_name"><?php _e('Last Name', 'cloudfw'); ?></label>
        <input class="login-input" name="last_name" type="text" id="last_name" value="<?php the_author_meta( 'last_name', $current_user->id ); ?>" />
    </p>
    
    <div class="cf"></div>
    
   
	<?php cloudfw_get_ahook('cloudfw_signup_extra', 'update', $current_user->id); ?> 


    <p class="form-submit">

    <?php echo $referer; ?>
        <input type="submit" name="wp-submit" id="wp-submit" class="button sky" value="<?php _e('Update', 'cloudfw'); ?>" />
        <?php wp_nonce_field( 'cloudfw-add-user' ) ?>
    </p>
 
</form>


</div>

<div id="user-ops-right">
	<?php cloudfw_get_ahook("cloudfw_user_page_sidebar", 'update'); ?>   
</div>

<div class="cf"></div>

	<?php
	}	
function cloudfw_register_form_send(){

require_once( ABSPATH . WPINC . '/registration.php' );
	global $error;
 
	$user_pass = wp_generate_password();
	$userdata = array(
		'user_pass' => $user_pass,
		'user_login' => esc_attr( $_POST['user_name'] ),
		'first_name' => esc_attr( $_POST['first_name'] ),
		'last_name' => esc_attr( $_POST['last_name'] ),
		'nickname' => esc_attr( $_POST['nickname'] ),
		'user_email' => esc_attr( $_POST['user_email'] ),
		'role' => get_option( 'default_role' ),
	);
 	
	if (!wp_verify_nonce( $_POST['_wpnonce'], "cloudfw-add-user"))
		$error = __('Session Expired. Try Again.', 'cloudfw');
	elseif ( !$userdata['user_login'] )
		$error = __('A username is required for registration.', 'cloudfw');
	elseif ( username_exists($userdata['user_login']) )
		$error = __('Sorry, that username already exists.', 'cloudfw');
	elseif ( !is_email($userdata['user_email'], true) )
		$error = __('You must enter a valid email address.', 'cloudfw');
	elseif ( email_exists($userdata['user_email']) )
		$error = __('Sorry, that email address is already used!', 'cloudfw');
	
	if (!$error){
		$new_user = wp_insert_user( $userdata );
		wp_new_user_notification($new_user, $user_pass);
	} 
	
	cloudfw_get_ahook("cloudfw_signup_save", 'create', $new_user);	

			
	
	
	if (!$error){
		if ( current_user_can( 'create_users' ) ){
			;
			echo '<div class="cl cl_4_3">'.init_boxes(array('title' => __('Registration Completed Successfully','cloudfw')), sprintf( __('A user account for %1$s has been created.', 'cloudfw'), $_POST['user_name'] ), 'success').'</div>';
		} else { 
			echo '<div class="cl cl_4_3">'.init_boxes(array('title' => sprintf(__('Thank you for registering, %1$s.', 'cloudfw'), $_POST['user_name'])),
			__('Registration completed successfully. Please check your email address. That\'s where you\'ll recieve your login password.', 'cloudfw'), 'success').'</div>';
		}
		
	}else{
		if ($new_user) wp_delete_user($new_user);
		
		echo '<div id="cloudfw-modal-message-box" class="cl cl_4_3">'.init_boxes(array('title' => __('Please Check Your Informations','cloudfw')), $error, 'caution').'</div>';
		cloudfw_register_form();
	}
	
	?>
	<div class="cf"></div>
	<?php
	
}


function cloudfw_register_form_update(){

require_once( ABSPATH . WPINC . '/registration.php' );
require_once( ABSPATH . 'wp-admin/includes' . '/template.php' );
	
	global $current_user, $wp_roles, $error;
	get_currentuserinfo();
 
	$userdata = array(
		'ID' => $current_user->id,
		'first_name' => esc_attr( $_POST['first_name'] ),
		'last_name' => esc_attr( $_POST['last_name'] )
	);
 	
	if (!wp_verify_nonce( $_POST['_wpnonce'], "cloudfw-add-user"))
		$error = __('Session Expired. Try Again.', 'cloudfw');
	elseif ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
		if ( $_POST['pass1'] == $_POST['pass2'] )
			$userdata["user_pass"] = esc_attr( $_POST['pass1'] );
		else
			$error = __('The passwords you entered do not match.  Your password was not updated.', 'cloudfw');
	}
	
	cloudfw_get_ahook("cloudfw_signup_save", 'update', $current_user->id);
	
	if (!$error){
		
		wp_update_user($userdata);
			
		echo '<div id="cloudfw-modal-message-box" class="cl cl_4_3">'.init_boxes(array('title' => __('Informations Updated','cloudfw')),NULL, 'success').'</div>';
		
	}else{
		
		echo '<div id="cloudfw-modal-message-box" class="cl cl_4_3">'.init_boxes(array('title' => __('Please Check Your Informations','cloudfw')), $error, 'caution').'</div>';
	} 
	
		cloudfw_user_update_form();

	
}
?>