<?php
require_once(".htloggywoggy.php");
#loggywoggy("auth.php");

/**
 * Authentication backend implementations.
 */

require_once "backends.php";
class AuthBackend_VOICE extends Backend_VOICE
{
	function _init()
	{
loggywoggy("In _init of AuthBackend_VOICE.");
	}	#end of function _init

#Unlike other backends, this authenticate function returns
#		1 for good username/password combination
#		2
#		3 for complete validation through Authentify
	function authenticate($username,$password)
	{
		global $skipcall,$testcall,$phone_number;
loggywoggy("In authenticate of AuthBackend_VOICE with skipcall of $skipcall.");
		$query="select * from accounts where username = '$username'";
		$result= $this->db->getRow($query);
		if (PEAR::isError($result) || (!$result))
		{
			return 0;
		}
		else if ($result["password"] ==$this->_encodePassword($password))
		{
			return 1;
		}
		else
			return 0;
		if ($skipcall == 1 ) return 3;
		require_once(".htauthentify.inc.php");
		require_once("AuthentComms.php");
		$msg=new Message();
		$msg->tsoid = TSOID;
		$msg->application = APPLICATION;
		$msg->password = PASSWORD;
		$msg->action = ACTION;
		$msg->type = "request";
		if ($testcall == 1)
		{
			$msg->data["phoneNumber"] = new DataNode("+1-617-413-0706");
			$msg->data["confirmNumber"] = new DataNode("1234");
		}
		else
		{
			$msg->data["phoneNumber"] =new DataNode($phone["number"]);
			$msg->data["confirmNumber"] = new DataNode($transaction["confirmnumber"]);
		}
		$msg->data["language"] = new DataNode("en-us");
		$msg->asid=ASID;
		$comms = new AuthentComms(AUTHENTIFY_URL);
		$newmsg = $comms->sendMessage($msg);
		if( !isset($newmsg) )
		{
			loggywoggy($comms->lastError);
			loggywoggy("I guess this means that you won't get your certificate just yet.");
		}
		else
		{
			$msgs.="<BR>We have asked to have you authenticated at ".$phone["number"]." with ".$transaction["confirmnumber"].".";
		}
		
	}	#end of function authenticate

	function newAccount($username, $password, $query)
	{
loggywoggy("In newAccount of AuthBackend_VOICE.");
		$result = $this->db->query("INSERT INTO accounts (username, password) " .
                                   "VALUES (?, ?)",
                                   array($username,
                                         $this->_encodePassword($password)));

        // $query is ignored for this implementation, but you might
        // choose to change the login process to incorporate other
        // user details like an email address.  $query is the HTTP
        // query in which the account registration form was submitted.
        // You'll only need to bother with $query if you've modified
        // the account registration form template and need to access
        // your new fields.

		if (PEAR::isError($result))
		{
			print_r($result);
			return false;
		}
		else
		{
			return true;
		}
	}

    function removeAccount($username)
    {
loggywoggy("In removeAccount of AuthBackend_VOICE.");
        $this->db->query("DELETE FROM accounts WHERE username = ?",
                         array($username));
    }

    function setPassword($username, $password)
    {
loggywoggy("In setPassword of AuthBackend_VOICE.");
        $result = $this->db->query("UPDATE accounts SET password = ? WHERE username = ?",
                                   array($this->_encodePassword($password),
                                         $username));
        if (PEAR::isError($result)) {
            return false;
        } else {
            return true;
        }
    }

    function _encodePassword($p)
    {
loggywoggy("In _encodePassword of AuthBackend_VOICE.");
        return md5($p);
    }

    function search($str = null)
    {
loggywoggy("In search of AuthBackend_VOICE.");
        if ($str != null) {
            $str = "%$str%";

            // Return should be a list of account names; nothing more.
            $result = $this->db->getCol("SELECT username FROM accounts WHERE username ".
                                        "LIKE ? ORDER BY username",
                                        0, array($str));
        } else {
            $result = $this->db->getCol("SELECT username FROM accounts ORDER BY username", 0);
        }

        if (PEAR::isError($result)) {
            return array();
        } else {
            return $result;
        }
    }

}	#end of class AuthBackend_VOICE
##################################################################################################

class AuthBackend_MYSQL extends Backend_MYSQL {
    function _init()
    {
        // Create tables for OpenID storage backend.
        $tables = array(
                        "CREATE TABLE accounts (" .
                        "id INTEGER AUTO_INCREMENT PRIMARY KEY, " .
                        "username VARCHAR(255) UNIQUE, " .
                        "password VARCHAR(32))",
                        );

        foreach ($tables as $t) {
            $this->db->query($t);
        }
    }

    function newAccount($username, $password, $query)
    {
        $result = $this->db->query("INSERT INTO accounts (username, password) " .
                                   "VALUES (?, ?)",
                                   array($username,
                                         $this->_encodePassword($password)));

        // $query is ignored for this implementation, but you might
        // choose to change the login process to incorporate other
        // user details like an email address.  $query is the HTTP
        // query in which the account registration form was submitted.
        // You'll only need to bother with $query if you've modified
        // the account registration form template and need to access
        // your new fields.

        if (PEAR::isError($result)) {
            print_r($result);
            return false;
        } else {
            return true;
        }
    }

    function removeAccount($username)
    {
        $this->db->query("DELETE FROM accounts WHERE username = ?",
                         array($username));
    }

    function authenticate($username, $password)
    {
        $result = $this->db->getOne("SELECT id FROM accounts WHERE " .
                                    "username = ? AND password = ?",
                                    array($username,
                                          $this->_encodePassword($password)));
        if (PEAR::isError($result) || (!$result)) {
            return false;
        } else {
            return true;
        }
    }

    function setPassword($username, $password)
    {
        $result = $this->db->query("UPDATE accounts SET password = ? WHERE username = ?",
                                   array($this->_encodePassword($password),
                                         $username));
        if (PEAR::isError($result)) {
            return false;
        } else {
            return true;
        }
    }

    function _encodePassword($p)
    {
        return md5($p);
    }

    function search($str = null)
    {
        if ($str != null) {
            $str = "%$str%";

            // Return should be a list of account names; nothing more.
            $result = $this->db->getCol("SELECT username FROM accounts WHERE username ".
                                        "LIKE ? ORDER BY username",
                                        0, array($str));
        } else {
            $result = $this->db->getCol("SELECT username FROM accounts ORDER BY username", 0);
        }

        if (PEAR::isError($result)) {
            return array();
        } else {
            return $result;
        }
    }
}
#loggywoggy("Leaving auth.php");
?>