<?php
	require_once('rpc2.component');

	/* inherit RPCVersion2Component for LLSDAPI tools */
	class RiskManagementComponent extends RPCVersion2Component {
		var $weightActiveListing = 3;
		var $weightInactiveListing = 1.5;
		var $maximumWeight = 10;

		function RiskManagementComponent() {
			$this->Component();
			$this->BindLLSDAPIAction("CHECK", 'CheckAction');
		}

		function GetUUIDs() {
			$uuids[] = '^ws\/risk_management$';
			return $uuids;
		}

		function CheckAction($tag) {
			global $expiry_length;

			$length = time() - (86400 * EXPIRY_LENGTH);
			$tuple = get_db_tuple(get_db_rows("SELECT COUNT(*) AS count FROM blacklist WHERE ip='".db_escape($tag['attributes']['IP'])."' AND listed=1 AND timestamp >= $length"));
			
			$curScore = $tuple['count'] * $this->weightActiveListing;
			$tuple = get_db_tuple(get_db_rows("SELECT COUNT(*) AS count FROM blacklist WHERE ip='".db_escape($tag['attributes']['IP'])."' AND listed=0 AND timestamp >= $length"));
			$curScore += ($tuple['count'] * $this->weightInactiveListing);

			if ($curScore > $this->maximumWeight)
				$curScore = $this->maximumWeight;

			$result['ip'] = $tag['attributes']['IP'];
			$result['score'] = $curScore;
			$result['weightActiveListing'] = $this->weightActiveListing;
			$result['weightInactiveListing'] = $this->weightInactiveListing;
			$result['maxScore'] = $this->maximumWeight;
			return $this->FormatLLSDObject("result", $result);
		}
	};

	new RiskManagementComponent;

?>
