2015-05-03 16:25:57 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Sakura Support/Donate page
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Declare Namespace
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-12-29 21:52:19 +00:00
|
|
|
use Sakura\Perms\Site;
|
|
|
|
|
2015-05-03 16:25:57 +00:00
|
|
|
// Include components
|
2015-12-03 19:40:01 +00:00
|
|
|
require_once str_replace(basename(__DIR__), '', dirname(__FILE__)) . '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()
|
2015-12-29 21:52:19 +00:00
|
|
|
&& $currentUser->permission(Site::OBTAIN_PREMIUM)) {
|
2015-07-01 00:16:22 +00:00
|
|
|
// Initialise Payments class
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!Payments::init()) {
|
|
|
|
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
|
2015-07-01 00:16:22 +00:00
|
|
|
} else {
|
2015-09-14 20:51:23 +00:00
|
|
|
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) {
|
2015-09-14 20:51:23 +00:00
|
|
|
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()) {
|
2015-09-14 20:51:23 +00:00
|
|
|
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
|
2015-12-04 14:19:10 +00:00
|
|
|
|| (int) $_POST['months'] > Config::get('premium_amount_max')) {
|
2015-09-14 20:51:23 +00:00
|
|
|
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
|
2015-07-01 00:16:22 +00:00
|
|
|
} else {
|
|
|
|
// Calculate the total
|
2015-12-04 14:19:10 +00:00
|
|
|
$total = (float) Config::get('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-12-04 14:19:10 +00:00
|
|
|
$itemName = Config::get('sitename')
|
2015-09-14 21:41:43 +00:00
|
|
|
. ' Premium - '
|
|
|
|
. (string) $_POST['months']
|
2015-11-06 22:30:37 +00:00
|
|
|
. ' 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,
|
2015-12-04 14:19:10 +00:00
|
|
|
Config::get('sitename') . ' Premium Purchase',
|
2016-01-14 20:43:33 +00:00
|
|
|
'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . Config::get('url_main') . $urls->format('SITE_PREMIUM')
|
2015-09-14 21:41:43 +00:00
|
|
|
)) {
|
2015-07-01 00:16:22 +00:00
|
|
|
// Store the amount of months in the global session array
|
2015-09-14 20:51:23 +00:00
|
|
|
$_SESSION['premiumMonths'] = (int) $_POST['months'];
|
2015-07-01 00:16:22 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
header('Location: ' . $transaction);
|
2015-07-01 00:16:22 +00:00
|
|
|
exit;
|
|
|
|
} else {
|
2015-09-14 20:51:23 +00:00
|
|
|
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
|
2015-09-14 20:51:23 +00:00
|
|
|
try {
|
2015-07-01 14:29:12 +00:00
|
|
|
$finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']);
|
2015-09-14 20:51:23 +00:00
|
|
|
} 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
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($finalise) {
|
2015-07-01 14:29:12 +00:00
|
|
|
// Make the user premium
|
2016-01-17 01:58:31 +00:00
|
|
|
$expiration = Users::addUserPremium($currentUser->id, (2628000 * $_SESSION['premiumMonths']));
|
|
|
|
Users::updatePremiumMeta($currentUser->id);
|
|
|
|
Utils::updatePremiumTracker(
|
|
|
|
$currentUser->id,
|
2015-12-04 14:19:10 +00:00
|
|
|
((float) Config::get('premium_price_per_month') * $_SESSION['premiumMonths']),
|
2016-01-17 01:58:31 +00:00
|
|
|
$currentUser->username
|
2015-09-14 21:41:43 +00:00
|
|
|
. ' bought premium for '
|
|
|
|
. $_SESSION['premiumMonths']
|
|
|
|
. ' month'
|
|
|
|
. ($_SESSION['premiumMonths'] == 1 ? '' : 's')
|
|
|
|
. '.'
|
|
|
|
);
|
2015-07-01 00:16:22 +00:00
|
|
|
|
|
|
|
// Redirect to the complete
|
2015-09-14 20:51:23 +00:00
|
|
|
header('Location: ' . $urls->format('SITE_PREMIUM') . '?mode=complete');
|
2015-07-01 00:16:22 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
header('Location: ' . $urls->format('SITE_PREMIUM') . '?fail=true');
|
2015-07-01 00:16:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'complete':
|
2015-11-06 22:30:37 +00:00
|
|
|
$renderData = array_merge([
|
2015-07-01 17:20:20 +00:00
|
|
|
'page' => [
|
2015-11-11 21:54:56 +00:00
|
|
|
'expiration' => ($prem = $currentUser->isPremium()[2]) !== null ? $prem : 0,
|
2015-09-14 20:51:23 +00:00
|
|
|
],
|
2015-11-06 22:30:37 +00:00
|
|
|
], $renderData);
|
|
|
|
|
|
|
|
// Set parse variables
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
2015-09-07 20:53:47 +00:00
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Print page contents
|
2016-02-04 20:56:40 +00:00
|
|
|
echo Template::render('main/premiumcomplete');
|
2015-07-01 00:16:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-09-14 20:51:23 +00:00
|
|
|
header('Location: ' . $urls->format('SITE_PREMIUM'));
|
2015-07-01 00:16:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:57:36 +00:00
|
|
|
// Premium tracker
|
2015-09-14 20:51:23 +00:00
|
|
|
if (isset($_GET['tracker'])) {
|
2016-01-17 01:58:31 +00:00
|
|
|
$renderData['tracker'] = Utils::getPremiumTrackerData();
|
2015-08-31 21:57:36 +00:00
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
2015-11-06 22:30:37 +00:00
|
|
|
|
|
|
|
// Print page contents
|
2016-02-04 20:56:40 +00:00
|
|
|
echo Template::render('main/supporttracker');
|
2015-08-31 21:57:36 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:25:57 +00:00
|
|
|
// Set default variables
|
|
|
|
$renderData['page'] = [
|
2015-08-21 22:07:45 +00:00
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
'fail' => isset($_GET['fail']),
|
2015-12-04 14:19:10 +00:00
|
|
|
'price' => Config::get('premium_price_per_month'),
|
2015-11-11 21:54:56 +00:00
|
|
|
'current' => $currentUser->isPremium(),
|
2015-12-04 14:19:10 +00:00
|
|
|
'amount_max' => Config::get('premium_amount_max'),
|
2015-08-21 22:07:45 +00:00
|
|
|
|
2015-05-03 16:25:57 +00:00
|
|
|
];
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
2016-02-04 20:56:40 +00:00
|
|
|
Template::vars($renderData);
|
2015-11-06 22:30:37 +00:00
|
|
|
|
2015-05-03 16:25:57 +00:00
|
|
|
// Print page contents
|
2016-02-04 20:56:40 +00:00
|
|
|
echo Template::render('main/support');
|