<?php

require_once "../../phplib/util.php";
util_assertNotMirror();
util_assertNotLoggedIn();
$token = util_getRequestParameter('token');
$identity = util_getRequestParameter('identity');
$pt = PasswordToken::get_by_token($token);
$data = FileCache::get($identity);
if (!$pt) {
    FlashMessage::add('Ați introdus un cod de recuperare incorect.');
} else {
    if ($pt->createDate < time() - 24 * 3600) {
        FlashMessage::add('Codul de recuperare introdus a expirat.');
    } else {
        if (!$data) {
            FlashMessage::add('Ați introdus o identitate incorectă.');
        } else {
            $user = User::get_by_id($pt->userId);
            if (!$user) {
                FlashMessage::add('Ați introdus un cod de recuperare incorect.');
            } else {
                if ($user->identity) {
                    FlashMessage::add('Acest cont a fost deja revendicat de o identitate OpenID.');
                } else {
                    FlashMessage::add('Contul dumneavoastră a fost recuperat și unificat cu identitatea OpenID.', 'info');
                    session_login($user, $data);
                }
            }
        }
    }
 public static function handleRecoverPasswordRequest()
 {
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         $userClass = User::getStaticDefaultClass();
         if (empty($_REQUEST['username'])) {
             $error = 'Please provide either your username or email address to reset your password.';
         } elseif (!($User = $userClass::getByUsername($_REQUEST['username'])) && !($User = $userClass::getByEmail($_REQUEST['username']))) {
             $error = 'No account is currently registered for that username or email address.';
         } elseif (!$User->Email) {
             $error = 'Unforunately, there is no email address on file for this account. Please contact an administrator.';
         } else {
             $Token = PasswordToken::create(array('CreatorID' => $User->ID), true);
             $Token->sendEmail($User->Email);
             return static::respond('recoverPasswordComplete', array('success' => true));
         }
     }
     return static::respond('recoverPassword', array('success' => empty($error), 'error' => isset($error) ? $error : false));
 }