<?php

require_once('../../php/definitions.php');


$mysqli = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("DROP PROCEDURE IF EXISTS Login_AgentStoreAuthorizedSignature") ||
    !$mysqli->query("
	
	CREATE PROCEDURE Login_AgentStoreAuthorizedSignature(IN loggedinip VARCHAR(17), IN loggedtoken VARCHAR(128), IN fr_key INT(11), IN st_key INT(11), IN ag_key INT(11))   
	BEGIN

	DECLARE lastupdate TIMESTAMP;
    SET lastupdate = CURRENT_TIMESTAMP;

	UPDATE `signature` 
	SET 
		`failedattempts` = 0, 
		`token` = loggedtoken, 
		`franchise_key` = fr_key, 
		`store_key` = st_key, 
		`agent_key` = ag_key,
		`timestamp` = lastupdate
	WHERE (`loggedip` = loggedinip) LIMIT 1;

	END;

	")) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}else{
	echo "Stored Procedure Login_AgentStoreAuthorizedSignature created successfully.";
}

?>