<?php
/*
 * $RCSfile: GalleryPendingUser.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.8 $ $Date: 2005/08/23 03:49:52 $
 * @package SelfRegistration
 * @author Sebastian Eichner <mailsp@sebastian-eichner.de>
 */

/**
 * Load the parent class
 */
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryEntity.class');

/**
 * Representation of a single pending self-registered user
 * (not yet a valid GalleryUser)
 *
 * This class is the container for information about Gallery
 * users.  Each instance of User contains a unique user id. It
 * must be implemented by a class that has a persistent store for
 * the relevant user data.
 *
 * @g2 <class-name>GalleryPendingUser</class-name>
 * @g2 <parent-class-name>GalleryEntity</parent-class-name>
 * @g2 <schema>
 * @g2   <schema-major>1</schema-major>
 * @g2   <schema-minor>0</schema-minor>
 * @g2 </schema>
 * @g2 <requires-id/>
 *
 * @package SelfRegistration
 * @subpackage Classes
 */
class GalleryPendingUser_core extends GalleryEntity {

    /*
     * ****************************************
     *                 Members
     * ****************************************
     */

    /**
     * The User's username
     *
     * @g2 <member>
     * @g2   <member-name>userName</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>SMALL</member-size>
     * @g2   <unique/>
     * @g2   <required/>
     * @g2 </member>
     *
     * @var string $_userName
     * @access private
     */
    var $_userName;

    /**
     * The User's full name
     *
     * @g2 <member>
     * @g2   <member-name>fullName</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2 </member>
     *
     * @var string $_fullName
     * @access private
     */
    var $_fullName;

    /**
     * The User's password in a hashed form.
     *
     * @g2 <member>
     * @g2   <member-name>hashedPassword</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2 </member>
     *
     * @var string $_hashedPassword
     * @access private
     */
    var $_hashedPassword;

    /**
     * The User's email address.
     *
     * @g2 <member>
     * @g2   <member-name>email</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2 </member>
     *
     * @var string $_email
     * @access private
     */
    var $_email;

    /**
     * The User's language preference
     *
     * @g2 <member>
     * @g2   <member-name>language</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>MEDIUM</member-size>
     * @g2 </member>
     *
     * @var string $_language
     * @access private
     */
    var $_language;

    /**
     * The registration key (used in confirmation url)
     *
     * @g2 <member>
     * @g2   <member-name>registrationKey</member-name>
     * @g2   <member-type>STRING</member-type>
     * @g2   <member-size>SMALL</member-size>
     * @g2 </member>
     *
     * @var string $_registrationKey
     * @access private
     */
    var $_registrationKey;

    /*
     * ****************************************
     *                 Methods
     * ****************************************
     */

    /**
     * Create a new instance of this user in the persistent store
     *
     * @return object GalleryStatus a status code
     */
    function create($userName) {
	global $gallery;

	// first check for a collision in the pending users table
	$query = '
        SELECT
          [GalleryPendingUser::id]
        FROM
          [GalleryPendingUser]
        WHERE
          [GalleryPendingUser::userName] = ?
        ';

	/* Check to see if we have a collision */
	list($ret, $results) =
	    $gallery->search($query, array($userName),
			     array('limit' => array('count' => 1)));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$result = $results->nextResult();
	if ($result[0] > 0) {
	    return GalleryStatus::error(ERROR_COLLISION, __FILE__, __LINE__);
	}

	// then check for a collision in the real user table
	$query = '
        SELECT
          [GalleryUser::id]
        FROM
          [GalleryUser]
        WHERE
          [GalleryUser::userName] = ?
        ';

	/* Check to see if we have a collision */
	list($ret, $results) =
	    $gallery->search($query, array($userName),
			     array('limit' => array('count' => 1)));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$result = $results->nextResult();
	if ($result[0] > 0) {
	    return GalleryStatus::error(ERROR_COLLISION, __FILE__, __LINE__);
	}

	// okay, we don't have any collisions, so go on
	$ret = parent::create();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$this->setUserName($userName);
	$this->setRegistrationKey('G' . rand(100000,999999) . '2');
	return GalleryStatus::success();
    }

    /**
     * Is the password provided correct?
     *
     * @param string a plaintext password
     * @return boolean true if the password is correct
     */
    function isCorrectPassword($password) {
	return !strcmp(md5($password), $this->getHashedPassword());
    }

    /**
     * Change the user's password to the new value provided.
     *
     * @param string a plaintext password
     */
    function changePassword($newPassword) {
	$this->setHashedPassword(md5($newPassword));
    }
}

include(dirname(__FILE__) . '/interfaces/GalleryPendingUser.inc');
?>
