<?php
	class DonateComponent extends Component {
		function GetUUIDs() {
			$uuids[] = 'donate$';
			$uuids[] = 'donate\/([^\/]*)\/([0-9]+)$';
			return $uuids;
		}

		function Run($page, $matches) {
			$action = isset($matches[1])?$matches[1]:'index';
			$id = isset($matches[2])?intval($matches[2]):0;
			
			if ($id > 0)
				$res = get_db_tuple(get_db_rows("SELECT * FROM donations WHERE ".db_insert_id_field()."=".$id));

			_page_header(SITE_NAME." :: Donations");

			if ($action == "index")
			{
?>
<h1>Make a donation</h1>
<?php
				if ($_POST)
				{
					$amount = sprintf("%01.2f",$_POST['amount']);
					$timestamp = time();
	                                $ins = get_db_rows("INSERT INTO donations (offered, timestamp) VALUES ('" . $amount . "', " . $timestamp . ")");
					$oid = db_insert_id($ins);
?>
<p>
	You have decided you want to donate &euro; <?=$amount?>.<br/>
	Please press below button in order to get redirected to PayPal's website in order to continue.
</p>
<form action="https://www.paypal.com/nl/cgi-bin/webscr" method="post">
	<input type="hidden" name="cmd" value="_xclick" />
	<input type="hidden" name="business" value="outsider@scarynet.org" />
	<input type="hidden" name="cbt" value="Return to <?=SITE_NAME?>" />
	<input type="hidden" name="item_name" value="<?=SITE_NAME?> Donation" />
	<input type="hidden" name="invoice" value="<?=$timestamp?>" />
	<input type="hidden" name="currency_code" value="EUR" />
	<input type="hidden" name="no_shipping" value="1" />
	<input type="hidden" name="no_note" value="1" />
	<input type="hidden" name="lc" value="US" />
	<input type="hidden" name="amount" value="<?=$amount?>" />
	<input type="hidden" name="return" value="http://dronebl.org/donate/true/<?=$oid?>" />
	<input type="hidden" name="cancel_return" value="http://dronebl.org/donate/false/<?=$oid?>" />
	<input type="hidden" name="notify_url" value="http://dronebl.org/donate/notify/<?=$oid?>" />
	<input type="hidden" name="image_url" value="http://dronebl.org/images/dronebl-logo.png" />
	<input type="submit" style="width:120px" name="submit" value="Continue donation" />
</form>
<?php
				}
				else
				{
					$total = get_db_tuple(get_db_rows("SELECT SUM(amount) FROM donations"));
?>
<script type="text/javascript">
	function checkDonate()
	{
		amount = document.getElementById('amount');
		amountSel = document.getElementById('amountSel');
		amountMan = document.getElementById('amountMan');
		if (amountSel.options[amountSel.selectedIndex].value > 0)
			amount.value = amountSel.options[amountSel.selectedIndex].value;
		else if (parseFloat(amountMan.value) >= 5 || parseFloat(amountMan.value) < 1000000)
			amount.value = amountMan.value;
		else
			return false;
		return true;
	}
	function checkSel()
	{
		amountSel = document.getElementById('amountSel');
		amountMan = document.getElementById('amountManDiv');
		if (amountSel.options[amountSel.selectedIndex].value == 0)
			amountMan.style.display = 'inline';
		else
			amountMan.style.display = 'none';
	}
</script>
<h2>Current sum of all donations is &euro; <?=$total[0]?></h2>
<p>
	Maintaining <?=SITE_NAME?> costs money. At the moment, all expenses are paid out of our own pockets. Hosting, hardware, time.<br/>
	Any money donated will be used to keep up with the costs. It will be used to cover the costs of hosting, and if there is a surplus,<br/>
	we will donate money to our hostings in order to enable them to secure their network so they can deal more efficiently<br/>
	with the regular DDoS attacks they receive because we reside there.
</p>
<p>
	None of the donated money will be spent for personal pleasures like hookers, the sportscars we always wanted etc. etc.<br/>
	Donations are handled through PayPal. If you want to donate otherwise, feel free to contact us.
</p>
<p>
	Due to the existance of this donation page, the question may have been risen that <?=SITE_NAME?> cannot run without donations. This is NOT the case.<br/>
	As stated above, <?=SITE_NAME?> is getting paid out of our own pockets. We will at this moment NEVER threaten to suspend the service should there be<br/>
	no donations made. Again, this is a DONATION page, not a hidden contribution cost page! To make it even more clear:
</p>
<ul>
	<li>Donations end up in the same paypal account used to pay the hosting using a subscription.</li>
	<li>The paypal account is backed up by my personal bank account. So any remains in costs get paid from it.</li>
	<li>Funds are heading one direction. From paypal to hosting(s). Not towards bank (too expensive anyways considering paypal's charges).</li>
</ul>
<form method="post" action="" onsubmit="return checkDonate()">
	<input type="hidden" name="amount" id="amount" value="0" />
	<label for="amountSel">Pick an amount</label>
	<select id="amountSel" onchange="checkSel()">
		<option value="5">&euro; 5,00</option>
		<option value="10">&euro; 10,00</option>
		<option value="20">&euro; 20,00</option>
		<option value="50">&euro; 50,00</option>
		<option value="0">Custom (min. &euro; 5,00)</option>
	</select>
	<div id="amountManDiv" style="display:none">
		&euro; <input type="text" id="amountMan" style="width:80px"/>
	</div>
	<input type="submit" value="Ok" />
</form>
<h2>We now also accept certain cryptocurrency!</h2>
<p>These are the addresses we accept them on:</p>
<ul>
	<li><a href="bitcoin:1FewgAURyKh3pY7tgaPopv71AP6iZJCuVh">bitcoin:1FewgAURyKh3pY7tgaPopv71AP6iZJCuVh (minimum 0.0001 BTC)</a></li>
	<li><a href="bitcoincash:1Fw1LxJHZYJZCndQ9yHQAB3AQJQnZEqTqp">bitcoincash:1Fw1LxJHZYJZCndQ9yHQAB3AQJQnZEqTqp (minimum 0.0001 BCH)</a></li>
	<li><a href="blackcoin:BBmMDoGhw1Z8cuUuJFPBDFyTJ3wFG8GCwL">blackcoin:BBmMDoGhw1Z8cuUuJFPBDFyTJ3wFG8GCwL (minimum 0.1 BLK)</a></li>
	<li><a href="bytecoin:23ZzRntsVK7g5fSHyvpMtb1cVgADpJLHGHE3zBEei5KELRDTWAAtyG4Fr6MNqj3PGR4PGXzCGYQw7UemxRoRxCC97q6djQ6">bytecoin:23ZzRntsVK7g5fSHyvpMtb1cVgADpJLHGHE3zBEei5KELRDTWAAtyG4Fr6MNqj3PGR4PGXzCGYQw7UemxRoRxCC97q6djQ6 (minimum 10 BCN)</a></li>
	<li><a href="dash:XwwLJEjWRPUvSPTrfNVzTUvhF4Fp7dJxcX">dash:XwwLJEjWRPUvSPTrfNVzTUvhF4Fp7dJxcX (minimum 0.001 DASH)</a></li>
	<li><a href="dogecoin:DJDF7iSWwnjSay9325tEYe7QNk73zEXTfa">dogecoin:DJDF7iSWwnjSay9325tEYe7QNk73zEXTfa (minimum 10 DOGE)</a></li>
	<li><a href="emercoin:EaxZ3f3hziq2iQGg2UiYZuQHAXSkCRzxEH">emercoin:EaxZ3f3hziq2iQGg2UiYZuQHAXSkCRzxEH (minimum 0.01 EMC)</a></li>
	<li><a href="ethereum:0x4de9e4894705a70f2a9d20a7b07f9ab96fb6c23b">ethereum:0x4de9e4894705a70f2a9d20a7b07f9ab96fb6c23b (minimum 0.001 ETH)</a></li>
	<li><a href="litecoin:Lbvfu8Y6c8mf7aKiwRZnEA9LmPGvRL899D">litecoin:Lbvfu8Y6c8mf7aKiwRZnEA9LmPGvRL899D (minimum 0.005 LTC)</a></li>
	<li><a href="monero:4BrL51JCc9NGQ71kWhnYoDRffsDZy7m1HUU7MRU4nUMXAHNFBEJhkTZV9HdaL4gfuNBxLPc3BeMkLGaPbF5vWtANQsSf46oWCNwHidAJ5T">monero:4BrL51JCc9NGQ71kWhnYoDRffsDZy7m1HUU7MRU4nUMXAHNFBEJhkTZV9HdaL4gfuNBxLPc3BeMkLGaPbF5vWtANQsSf46oWCNwHidAJ5T (minimum 0.001 XMR)</a></li>
	<li><a href="peercoin:PWfyhrefPm8fMrvzMBNctwXjpBxdFRjdM5">peercoin:PWfyhrefPm8fMrvzMBNctwXjpBxdFRjdM5 (minimum 0.01 PPC)</a></li>
	<li><a href="primecoin:AQH1AwTWoaMGCKvAC8mG7UaBkcYUfjSQ72">primecoin:AQH1AwTWoaMGCKvAC8mG7UaBkcYUfjSQ72 (minimum 0.1 XPM)</a></li>
	<li><a href="reddcoin:Rp9QZdsHz92UbnC1SoEogvZpiZf1cKCxd7">reddcoin:Rp9QZdsHz92UbnC1SoEogvZpiZf1cKCxd7 (minimum 100 RDD)</a></li>
	<li><a href="zcash:t1N1ucK37ewPnzdtqJVRxao7rHCqXLdfTXh">zcash:t1N1ucK37ewPnzdtqJVRxao7rHCqXLdfTXh (minimum 0.0001 ZEC)</a></li>
</ul>
<?php
				}
			}
			else if ($action == "true" && $res)
			{
?>
<p>
	Thank you very much for your donation.
</p>
<?php
			}
			else if ($action == "false" && $res)
			{
?>
<p>
	You aborted the donation. If this is in error, please feel free to <a href="/donate">try again</a>.
</p>
<?php
			}
			else if ($action == "notify" && $res && $_POST && $_POST['test_ipn'] != "1" && $_POST['receiver_email'] == 'outsider@scarynet.org')
			{
				$pp_txn = strip_tags($_POST['txn_id']);
				$status = strip_tags($_POST['payment_status']);
				$amount = $_POST['mc_gross'];
				$res['status'] = $status;
				switch ($status)
				{
					case 'Canceled-Reversal':
					case 'Completed':
						$res['amount'] = $res['amount']+$amount;
						break;
					case 'Refunded':
					case 'Reversed':
						$res['amount'] = $res['amount']-$amount;
						if ($status == 'Reversed')
							$res['reason'] = strip_tags($_POST['reason_code']);
						else
							$res['reason'] = '';
						break;
					case 'Pending':
						$res['reason'] = strip_tags($_POST['pending_reason']);
						break;
				}
				$updq = "UPDATE donations SET ";
				foreach (array_keys($res) AS $field)
				{
					if ($field!='id')
						$updq .= $field."='".$res[$field]."',";
				}
				$updq = substr($updq,0,-1)." WHERE ".db_insert_id_field()."=".$id;
				$upd = get_db_rows($updq);
			}
			else
			{
				header('Location: /donate');
			}

			_page_footer();
		}

	};

	new DonateComponent;
?>
