qa-db-limits.php 2.77 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-db-limits.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Database-level access to tables which monitor rate limits


	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	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.

	More about this license: http://www.question2answer.org/license.php
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


	function qa_db_limits_get($userid, $ip, $action)
/*
	Get rate limit information for $action from the database for user $userid and/or IP address $ip, if they're set.
	Return as an array with the limit type in the key, and a labelled array of the period and count.
*/
	{
		$selects=array();
		$arguments=array();
Scott Vivian committed
41

Gideon Greenspan committed
42 43 44 45 46
		if (isset($userid)) {
			$selects[]="(SELECT 'user' AS limitkey, period, count FROM ^userlimits WHERE userid=$ AND action=$)";
			$arguments[]=$userid;
			$arguments[]=$action;
		}
Scott Vivian committed
47

Gideon Greenspan committed
48 49 50 51 52
		if (isset($ip)) {
			$selects[]="(SELECT 'ip' AS limitkey, period, count FROM ^iplimits WHERE ip=COALESCE(INET_ATON($), 0) AND action=$)";
			$arguments[]=$ip;
			$arguments[]=$action;
		}
Scott Vivian committed
53

Gideon Greenspan committed
54 55 56
		if (count($selects)) {
			$query=qa_db_apply_sub(implode(' UNION ALL ', $selects), $arguments);
			return qa_db_read_all_assoc(qa_db_query_raw($query), 'limitkey');
Scott Vivian committed
57

Gideon Greenspan committed
58 59 60 61
		} else
			return array();
	}

Scott Vivian committed
62

Gideon Greenspan committed
63 64 65 66 67 68 69 70 71 72 73 74
	function qa_db_limits_user_add($userid, $action, $period, $count)
/*
	Increment the database rate limit count for user $userid and $action by $count within $period
*/
	{
		qa_db_query_sub(
			'INSERT INTO ^userlimits (userid, action, period, count) VALUES ($, $, #, #) '.
			'ON DUPLICATE KEY UPDATE count=IF(period=#, count+#, #), period=#',
			$userid, $action, $period, $count, $period, $count, $count, $period
		);
	}

Scott Vivian committed
75

Gideon Greenspan committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	function qa_db_limits_ip_add($ip, $action, $period, $count)
/*
	Increment the database rate limit count for IP address $ip and $action by $count within $period
*/
	{
		qa_db_query_sub(
			'INSERT INTO ^iplimits (ip, action, period, count) VALUES (COALESCE(INET_ATON($), 0), $, #, #) '.
			'ON DUPLICATE KEY UPDATE count=IF(period=#, count+#, #), period=#',
			$ip, $action, $period, $count, $period, $count, $count, $period
		);
	}


/*
	Omit PHP closing tag to help avoid accidental output
*/