This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/main/support.php

137 lines
4.5 KiB
PHP
Raw Normal View History

2015-05-03 16:25:57 +00:00
<?php
/*
* Sakura Support/Donate page
*/
// Declare Namespace
namespace Sakura;
// Include components
require_once str_replace(basename(__DIR__), '', dirname(__FILE__)) .'_sakura/sakura.php';
2015-07-01 00:16:22 +00:00
// Switch between modes (we only allow this to be used by logged in user)
if(isset($_REQUEST['mode']) && Users::checkLogin() && Permissions::check('SITE', 'OBTAIN_PREMIUM', Session::$userId, 1)) {
// Initialise Payments class
if(!Payments::init()) {
2015-07-01 14:29:12 +00:00
header('Location: /support?fail=true');
2015-07-01 00:16:22 +00:00
} else {
switch($_REQUEST['mode']) {
// Create the purchase
case 'purchase':
// Compare time and session so we know the link isn't forged
if(!isset($_REQUEST['time']) || $_REQUEST['time'] < time() - 1000) {
2015-07-01 14:29:12 +00:00
header('Location: /support?fail=true');
2015-07-01 00:16:22 +00:00
break;
}
// Match session ids for the same reason
if(!isset($_REQUEST['session']) || $_REQUEST['session'] != session_id()) {
2015-07-01 14:29:12 +00:00
header('Location: /support?fail=true');
2015-07-01 00:16:22 +00:00
break;
}
// Half if shit isn't gucci
2015-07-01 18:22:45 +00:00
if(!isset($_POST['months']) || !is_numeric($_POST['months']) || (int)$_POST['months'] < 1 || (int)$_POST['months'] > Configuration::getConfig('premium_amount_max')) {
2015-07-01 00:16:22 +00:00
header('Location: /support?fail=true');
} else {
// Calculate the total
$total = (float)Configuration::getConfig('premium_price_per_month') * (int)$_POST['months'];
$total = money_format('%!i', $total);
// Generate item name
$itemName = 'Flashii Tenshi - '. (string)$_POST['months'] .' month'. ((int)$_POST['months'] == 1 ? '' : 's');
// Attempt to create a transaction
if($transaction = Payments::createTransaction($total, $itemName, 'Flashii Tenshi Purchase', 'http://'. Configuration::getLocalConfig('urls', 'main') .'/support')) {
// Store the amount of months in the global session array
$_SESSION['premiumMonths'] = (int)$_POST['months'];
header('Location: '. $transaction);
exit;
} else {
2015-07-01 14:29:12 +00:00
header('Location: /support?fail=true');
2015-07-01 00:16:22 +00:00
}
}
break;
// Finalising the purchase
case 'finish':
// Check if the success GET request is set and is true
if(isset($_GET['success']) && isset($_GET['paymentId']) && isset($_GET['PayerID']) && isset($_SESSION['premiumMonths'])) {
// Attempt to complete the transaction
2015-07-01 14:29:12 +00:00
try{
$finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']);
} catch(Exception $e) {}
// Attempt to complete the transaction
if($finalise) {
2015-07-01 00:16:22 +00:00
2015-07-01 14:29:12 +00:00
// Make the user premium
$expiration = Users::addUserPremium(Session::$userId, (2628000 * $_SESSION['premiumMonths']));
Users::updatePremiumMeta(Session::$userId);
2015-07-01 00:16:22 +00:00
// Redirect to the complete
2015-07-01 14:29:12 +00:00
header('Location: ?mode=complete&expire='. $expiration);
2015-07-01 00:16:22 +00:00
exit;
}
}
header('Location: /support?fail=true');
break;
case 'complete':
2015-07-01 17:20:20 +00:00
print Templates::render('errors/premiumComplete.tpl', array_merge([
'page' => [
'title' => 'Premium purchase complete!',
'expiration' => ($prem = Users::checkUserPremium(Session::$userId)[2]) !== null ? $prem : 0
]
], $renderData));
2015-07-01 00:16:22 +00:00
break;
default:
header('Location: /support');
break;
}
}
exit;
}
2015-05-03 16:25:57 +00:00
// Set default variables
$renderData['page'] = [
2015-07-03 17:33:02 +00:00
'title' => 'Support '. Configuration::getConfig('sitename'),
2015-07-01 18:22:45 +00:00
'fail' => isset($_GET['fail']),
'price' => Configuration::getConfig('premium_price_per_month'),
'current' => Users::checkUserPremium(Session::$userId),
'amount_max' => Configuration::getConfig('premium_amount_max')
2015-05-03 16:25:57 +00:00
];
// Print page contents
2015-07-01 14:29:12 +00:00
print Templates::render('main/support.tpl', $renderData);