<?php
/*
 * $RCSfile: UserRecoverPasswordHelper_simple.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.3 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Jay Rossiter <cryptographite@users.sf.net>
 */

/**
 * Utilities used in the UserRequestPassword* controllers
 *
 * @package GalleryCore
 * @subpackage Helpers
 * @static
 */
class UserRecoverPasswordHelper_simple {
    /**
     * Get the request expiration for the UserPasswordRequest from the database.
     * If authString is empty, only search by username
     *
     * @param string username
     * @param string authorization string (null acceptable)
     * @return array object GalleryStatus a status code
     *               int epoch-based request expiration
     */
    function getRequestExpires($username, $authString) {
	global $gallery;

	$query = '
	    SELECT 
		[GalleryRecoverPasswordMap::requestExpires] 
	    FROM 
		[GalleryRecoverPasswordMap] 
	    WHERE
		[GalleryRecoverPasswordMap::username] = ?
        ';

	if (!empty($authString)) {
	    $query .= ' AND [GalleryRecoverPasswordMap::authString] = ?';
	    $data = array($username, $authString);
	} else {
	    $data = array($username);
	}

	list($ret, $searchResults) = $gallery->search($query, $data);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if ($request = $searchResults->nextResult()) {
	    $requestExpires = (int)$request[0];
	} else {
	    $requestExpires = null;
	}

	return array(GalleryStatus::success(), $requestExpires);
    }

    /**
     * Get the request authString from the database.
     *
     * @param string username
     * @return array object GalleryStatus a status code
     *               string authString
     */
    function getAuthString($username) {
	global $gallery;

	$query = '
	    SELECT 
		[GalleryRecoverPasswordMap::authString] 
	    FROM 
		[GalleryRecoverPasswordMap] 
	    WHERE
		[GalleryRecoverPasswordMap::username] = ?
        ';

	$data = array($username);

	list($ret, $searchResults) = $gallery->search($query, $data);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	if ($request = $searchResults->nextResult()) {
	    $authString = $request[0];
	} else {
	    $authString = null;
	}

	return array(GalleryStatus::success(), $authString);
    }
}
?>
