<?

class Quota {
	var $currentTraffic;
	var $currentSpace;
	var $spaceQuota;
	var $trafficQuota;


	function Quota() {
		global $permissions, $auth;
		$this->username = $auth->currentUserInfo['username'];
		$this->currentTraffic = $auth->currentUserInfo['traffic'];
		$this->trafficQuota = $permissions['trafficQuota'];
		if (!$permissions['unlimitedSpace']) {
			$this->currentSpace = $this->getUsedSpace();
		}
		$this->spaceQuota = $permissions['spaceQuota'];
	}

	function recalcUsedSpace() {
		$this->currentSpace = $this->getUsedSpace();
	}

	function getUsedSpace() {
		global $sys, $fm, $permissions, $settings;
		$homefolder = $permissions['path'];
		$trash = gluePath((($settings->config['trashpath'])?$settings->config['trashpath']:gluePath($sys['root'], "/system/data/trash/")), $this->username);
		return $fm->getDirSize($homefolder) + $fm->getDirSize($trash);
	}


	function canAddToSpace() {
		if (func_num_args() > 0) {
			$arg = func_get_arg(0);
		} else {
			$arg = 0;
		}
		if ($this->currentSpace+$arg <= $this->spaceQuota) {
			return true;
		} else {
			return false;
		}
	}

	function addTraffic($size) {
		global $auth;
		$auth->updateUser($auth->currentUserInfo['id'], array("traffic"=>$this->currentTraffic+$size));
		$this->currentTraffic = $this->currentTraffic+$size;
	}

	function canAddTraffic() {
		if ($this->currentTraffic < $this->trafficQuota) {
			return true;
		} else {
			return false;
		}
	}
	
	function resetTrafficIfMonth() {
		global $db, $auth, $settings;
		if (time() > $auth->currentUserInfo['trafficdate']+2592000) {
			$sql = "SELECT * FROM ".$settings->config['users_tablename']." WHERE id = '".$auth->currentUserInfo['id']."'"; 
			$rs = $db->query($sql);
			$data['traffic'] = 0;
			$data['trafficdate'] = time();
			$updateSQL = $db->GetUpdateSQL($rs, $data);
			$db->query($updateSQL);
			$this->currentTraffic = 0;
		}
	}


}

?>