<?php
/*
 * $RCSfile: AuthenticateStep.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.
 */

class AuthenticateStep extends InstallStep {
    var $_uniqueKey;
    var $_firstTime;

    function AuthenticateStep() {
	for ($len=64, $rand='';
	     strlen($rand) < $len;
	     $rand .= chr(!mt_rand(0,2) ? mt_rand(48,57) :
			  (!mt_rand(0,1) ? mt_rand(65,90) :
			   mt_rand(97,122))));
	$this->_uniqueKey = md5($rand);
	$this->_firstTime = true;
    }

    function stepName() {
	return _('Authenticate');
    }

    function processRequest() {
	if (!empty($_GET['downloadLogin'])) {
	    header("Content-Type: text/plain");
	    header("Content-Length: " . strlen($this->_uniqueKey));
	    header("Content-Description: Download login.txt to your computer.");
	    header("Content-Disposition: attachment; filename=login.txt");
	    print $this->_uniqueKey;
	    return false;
	}

	return true;
    }

    function loadTemplateData(&$templateData) {
	$authenticationDir = dirname(dirname(dirname(__FILE__)));
	if (isset($_SERVER['DOCUMENT_ROOT'])
		&& preg_match('#^' . $_SERVER['DOCUMENT_ROOT'] . '/(.*)#',
			      $authenticationDir, $matches)) {
	    $authenticationDir = $matches[1];
	}

	if (!$this->isComplete()) {
	    /* Authenticate */
	    $authenticated = false;
	    $authFile = dirname(__FILE__) . '/../../login.txt';
	    if (!file_exists($authFile)) {
		if (!$this->_firstTime) {
		    $templateData['errors'][] =
			sprintf(_('<b>Error:</b> could not locate <b>login.txt</b>. ' .
				  'Please place it in your <tt>%s/</tt> directory.'),
				$authenticationDir);
		}
	    } else if (!is_readable($authFile)) {
		$templateData['errors'][] =
		    _('<b>Error:</b> your <b>login.txt</b> file is not readable. ' .
		      'Please give Gallery read permissions on the file.');
	    } else {

		$fileAuth = trim(join("", file($authFile)));
		if ($fileAuth == $this->_uniqueKey) {
		    $this->setComplete(true);
		} else {
		    $templateData['errors'][] =
			_('<b>Error:</b> your <b>login.txt</b> key does not match correctly. ' .
			  'Please download a new authentication string from below and try again.');
		}
	    }
	}

	$this->_firstTime = false;

	$templateData['authenticationDir'] = $authenticationDir;

	if ($this->isComplete()) {
	    $templateData['bodyFile'] = 'AuthenticateSuccessful.html';
	} else {
	    $templateData['bodyFile'] = 'AuthenticateRequest.html';
	}
    }

    function getUniqueKey() {
	return $this->_uniqueKey;
    }
}
?>
