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/public/support.php

161 lines
6.1 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-05-03 16:25:57 +00:00
2015-07-01 00:16:22 +00:00
// Switch between modes (we only allow this to be used by logged in user)
2015-09-14 21:41:43 +00:00
if (isset($_REQUEST['mode'])
&& Users::checkLogin()
&& Permissions::check('SITE', 'OBTAIN_PREMIUM', $currentUser->data['user_id'], 1)) {
2015-07-01 00:16:22 +00:00
// Initialise Payments class
if (!Payments::init()) {
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
2015-07-01 00:16:22 +00:00
} else {
switch ($_REQUEST['mode']) {
2015-08-21 22:07:45 +00:00
// Create the purchase
2015-07-01 00:16:22 +00:00
case 'purchase':
// Compare time and session so we know the link isn't forged
2015-09-14 21:41:43 +00:00
if (!isset($_REQUEST['time'])
|| $_REQUEST['time'] < time() - 1000) {
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
2015-07-01 00:16:22 +00:00
break;
}
// Match session ids for the same reason
2015-09-14 21:41:43 +00:00
if (!isset($_REQUEST['session'])
|| $_REQUEST['session'] != session_id()) {
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
2015-07-01 00:16:22 +00:00
break;
}
// Half if shit isn't gucci
2015-09-14 21:41:43 +00:00
if (!isset($_POST['months'])
|| !is_numeric($_POST['months'])
|| (int) $_POST['months'] < 1
|| (int) $_POST['months'] > Configuration::getConfig('premium_amount_max')) {
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
2015-07-01 00:16:22 +00:00
} else {
// Calculate the total
$total = (float) Configuration::getConfig('premium_price_per_month') * (int) $_POST['months'];
2015-07-30 17:07:23 +00:00
$total = number_format($total, 2, '.', '');
2015-07-01 00:16:22 +00:00
// Generate item name
2015-09-14 21:41:43 +00:00
$itemName = Configuration::getConfig('sitename')
. ' Premium - '
. (string) $_POST['months']
. ' month'
. ((int) $_POST['months'] == 1 ? '' : 's');
2015-07-01 00:16:22 +00:00
// Attempt to create a transaction
2015-09-14 21:41:43 +00:00
if ($transaction = Payments::createTransaction(
$total,
$itemName,
Configuration::getConfig('sitename') . ' Premium Purchase',
'http://' . Configuration::getConfig('url_main') . $urls->format('SITE_PREMIUM')
)) {
2015-07-01 00:16:22 +00:00
// Store the amount of months in the global session array
$_SESSION['premiumMonths'] = (int) $_POST['months'];
2015-07-01 00:16:22 +00:00
header('Location: ' . $transaction);
2015-07-01 00:16:22 +00:00
exit;
} else {
header('Location: ' . $urls->format('SITE_PREMIUM') . '?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
2015-09-14 21:41:43 +00:00
if (isset($_GET['success'])
&& isset($_GET['paymentId'])
&& isset($_GET['PayerID'])
&& isset($_SESSION['premiumMonths'])) {
2015-07-01 00:16:22 +00:00
// Attempt to complete the transaction
try {
2015-07-01 14:29:12 +00:00
$finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']);
} catch (Exception $e) {
trigger_error('Something went horribly wrong.', E_USER_ERROR);
}
2015-07-01 14:29:12 +00:00
// Attempt to complete the transaction
if ($finalise) {
2015-07-01 14:29:12 +00:00
// Make the user premium
$expiration = Users::addUserPremium($currentUser->data['user_id'], (2628000 * $_SESSION['premiumMonths']));
Users::updatePremiumMeta($currentUser->data['user_id']);
2015-09-14 21:41:43 +00:00
Main::updatePremiumTracker(
$currentUser->data['user_id'],
2015-09-14 21:41:43 +00:00
((float) Configuration::getConfig('premium_price_per_month') * $_SESSION['premiumMonths']),
$currentUser->data['username']
. ' bought premium for '
. $_SESSION['premiumMonths']
. ' month'
. ($_SESSION['premiumMonths'] == 1 ? '' : 's')
. '.'
);
2015-07-01 00:16:22 +00:00
// Redirect to the complete
header('Location: ' . $urls->format('SITE_PREMIUM') . '?mode=complete');
2015-07-01 00:16:22 +00:00
exit;
}
}
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
2015-07-01 00:16:22 +00:00
break;
case 'complete':
print Templates::render('main/premiumcomplete.tpl', array_merge([
2015-07-01 17:20:20 +00:00
'page' => [
'expiration' => ($prem = Users::checkUserPremium($currentUser->data['user_id'])[2]) !== null ? $prem : 0,
],
], $renderData));
2015-07-01 00:16:22 +00:00
break;
default:
header('Location: ' . $urls->format('SITE_PREMIUM'));
2015-07-01 00:16:22 +00:00
break;
}
}
exit;
}
// Premium tracker
if (isset($_GET['tracker'])) {
$renderData['page'] = [
'currentPage' => isset($_GET['page']) && ($_GET['page'] - 1) >= 0 ? $_GET['page'] - 1 : 0,
'premiumData' => ($_PREMIUM = Main::getPremiumTrackerData()),
'premiumTable' => array_chunk($_PREMIUM['table'], 20, true),
];
print Templates::render('main/supporttracker.tpl', $renderData);
exit;
}
2015-05-03 16:25:57 +00:00
// Set default variables
$renderData['page'] = [
2015-08-21 22:07:45 +00:00
'fail' => isset($_GET['fail']),
'price' => Configuration::getConfig('premium_price_per_month'),
'current' => $currentUser->checkPremium(),
'amount_max' => Configuration::getConfig('premium_amount_max'),
2015-08-21 22:07:45 +00:00
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);