<?php
global $wpdb;
/**
 * Template Name: PassReset
 * Description: PassReset
 **/

$key=strip_tags(trim($_GET['key']));
if (!$key) $key=strip_tags(trim($_POST['key']));
$login=strip_tags(trim($_GET['login']));
if (!$login) $login=strip_tags(trim($_POST['login']));
$pass1=strip_tags(trim($_POST['pass1']));
$pass2=strip_tags(trim($_POST['pass2']));

if ($key && $login) {
    
    $query = 'SELECT ID, user_login, user_activation_key FROM '.$wpdb->users.' WHERE user_activation_key="'.$key.'" AND user_login="'.$login.'"';
    
    $user = $wpdb->get_row($query);
    
    if ($login == $user->user_login && $key == $user->user_activation_key) {
        
        if ($pass1 == $pass2 && ($pass1!='' && $pass2!='')) {
            if (strlen ($pass1)>7) {
                // ACTUALIZAR CONTRASENA
                // user_activation_key se elimina automaticamente
                wp_set_password($pass1, $user->ID);
                                
                // Mensaje y enlace al inicio
                $msg = __ ('Contrase&ntilde;a actualizada.','buddypress');
                $msg .= ' ' . __ ('Haz clic en el siguiente enlace para iniciar sesi&oacute;n con tu nueva contrase&ntilde;a.','buddypress');
                $msg .= '<div style="font-family: Arial; text-align: right; font-weight: bold; padding: 10px; margin-top: 20px; margin-bottom: 10px;"><a style="padding: 6px; background: #fff;" href="'. get_site_url() .'">'. __('ENTRAR AL SITIO','buddypress') .'</a></div>';
                $updated = 1;
            }
            else {
                $msg = __ ('La contrase&ntilde;a debe tener al menos 8 caracteres.','buddypress');
            }
        }
        elseif ($pass1=='' && $pass2=='') {
            $msg= '';
        }
        else {
            $msg = __ ('No coincide la confirmaci&oacute;n de la contrase&ntilde;a.','buddypress'); 
        }
    }
    else {
        $msg = __ ('Datos incorrectos.','buddypress');
        wp_redirect(get_site_url());
    }            
}
else {
    $msg = __ ('Datos incorrectos.','buddypress');
    wp_redirect(get_site_url());
}

get_header("home"); ?>
<style>
.intro-middle-box p{
    font-size: 15px;
    margin-bottom: 12px;
}

#pass-restore {
    margin-top: 50px;
}
form.standard-form input[type="password"] {
    padding: 0px 3px;
    border: 1px solid #D9D9D9;
    border-radius: 5px;
    height: 23px;
    width: 149px;
    background: none;
    margin-bottom: 10px;
}

</style>
<div id="intro-container">

    <div id="header">
        <?php echo header_home_links_registration( $_COOKIE['LANG'] ); ?>
    </div>

    <div class="iron-lang">
        <a href="<?php echo site_url(); ?>?lang=es">Castellano</a>
        &nbsp;|&nbsp;
        <a href="<?php echo site_url(); ?>?lang=eu">Euskera</a>
        &nbsp;|&nbsp;
        <a href="<?php echo site_url(); ?>?lang=en">English</a>
    </div>
    
    <div id="content" style="margin-top: 25px;">
    
        <div class="intro-middle-box">
            <p>
                <?php _e ('Has llegado aqu&iacute; porque has recibido un email solicitando tu contrase&ntilde;a de acceso al sitio <b>Kronikoen sarea</b>. Si se trata de un error, por favor ponte en contacto con nosotros.','buddypress'); ?>
            </p>
            <p>
                <?php _e ('Para completar la solicitud, introduce la nueva contrase&ntilde;a en el siguiente formulario.', 'buddypress'); ?>
            </p>
        </div>
        
        <div class="intro-middle-box nomargin" id="pass-restore">
            <?php if ($updated != 1) : ?>
            
                <h1><?php _e ('Password Reset', 'buddypress'); ?></h1>        
                <form id="passreset_form" name="passreset_form" method="post" action="<?php echo get_site_url(); ?>/recuperar-contrasena" class="standard-form">
                
                    <input type="password" class="settings-input small" value="" placeholder="<?php _e ('Nueva Clave','buddypress'); ?>" size="16" id="pass1" name="pass1">
                    &nbsp;<?php _e ('Nueva Clave','buddypress'); ?><br />
                    <input type="password" class="settings-input small" value="" placeholder="<?php _e ('Repetir Clave','buddypress'); ?>" size="16" id="pass2" name="pass2">
                    &nbsp;<?php _e ('Repetir Clave','buddypress'); ?><br />
                    
                    <div class="submit">
                        <input type="submit" class="auto" id="submit" value="<?php _e ('Actualizar contrase&ntilde;a','buddypress'); ?>" name="submit" />
                        <input type="hidden" name="key" value="<?php echo $user->user_activation_key; ?>">
                        <input type="hidden" name="login" value="<?php echo $user->user_login; ?>">
                    </div>
                    
                </form>
                
                <?php if ($msg) : ?>
                    <div style="margin: 10px; padding: 10px; background: #E2E2E2 font-size: 13px;">
                    <?php echo $msg; ?>
                    </div>
                <?php endif; ?>
            
            <?php else : ?>
                <h1><?php _e ('Password Reset', 'buddypress'); ?></h1>
                <div style="margin: 10px; padding: 10px; background: #f4f2c0">
                <?php echo $msg; ?>
                </div>
            <?php endif; ?>
        </div>
        
    </div>

</div>

<?php get_footer("home") ?>