<?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_IncrementIPLoginAttempts") ||
    !$mysqli->query("
	
	CREATE PROCEDURE Login_IncrementIPLoginAttempts(IN ip VARCHAR(15))
	BEGIN

	DECLARE ipattempts INT;
	
	SELECT `failedattempts`	FROM `signature` WHERE (`loggedip` = ip) LIMIT 1 INTO ipattempts;
	
	SET ipattempts = ipattempts + 1;

	IF ipattempts IS NOT NULL THEN

		UPDATE `signature` SET `failedattempts`=ipattempts WHERE (`loggedip` = ip) LIMIT 1;

	ELSE

		INSERT INTO `signature` (`loggedip`, `failedattempts`)VALUES (ip, '1');
		SET ipattempts = 1;
	
	END IF;

	SELECT ipattempts AS ipattempts;
	
	END;
	
	")) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}else{
	echo "Stored Procedure Login_IncrementIPLoginAttempts created successfully.";
}

?>