<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS 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.

AthenaRMS 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php

require_once 'db.php';

class TicketHash {

    function TicketHash() {
      global $dbi;

      $this->db = &$dbi;
      $this->sth = $this->db->prepare(
	  'SELECT payload FROM email_ticket_map WHERE payload_hash = ?');
    }

    ##
    ## Creates a hash entry in the email_ticket_map table and returns
    ## the hash.
    ##
    function makeHash($payload) {

	## Check to see if there's an entry in the database with that
	## hash.  If there is, try a different random number
	## Repeat until we get ourselves a unique hash.
	do {
	    $hash = md5($payload . rand());
	} while ($this->fetch($hash));

	## And insert the data into the database.
	$sql = "
	    insert into email_ticket_map (
		payload_hash,
		payload
	    ) values (
		?,?
	    )
";

	 $h = $this->db->prepare($sql); 
	 $res=$this->db->execute($h, array($hash,$payload));

	 if (DB::isError($res)) {
	    $this->error = 'TicketHash::makeHash(): '
		. $res->getMessage() . $res->getUserInfo();
	    return;
	 }

	 return $hash;
    }

    function fetch($hash) {
	$res = $this->db->execute($this->sth, array($hash));

	if (DB::isError($res)) {
	    $this->error = 'TicketHash::fetch(): '
		. $res->getMessage() . $res->getUserInfo();
	    return;
	}

	$row = $res->fetchrow();
	return $row[0];
    }
}

?>
