From f17d0ae5c45977f8ab489957dfb795d4d2729dcd Mon Sep 17 00:00:00 2001
From: Jeremy Kauffman
Date: Tue, 9 Aug 2016 08:41:06 -0400
Subject: [PATCH] fix for custom codes (blah) + put support for curl
---
controller/Session.class.php | 1 +
controller/action/DownloadActions.class.php | 4 ++
lib/tools/Curl.class.php | 29 ++++++++++++---
lib/tools/Prefinery.class.php | 41 ++++++++++++++++++---
view/template/download/_betaNotice.php | 4 +-
view/template/download/getAllowed.php | 7 +++-
6 files changed, 72 insertions(+), 14 deletions(-)
diff --git a/controller/Session.class.php b/controller/Session.class.php
index 229072b8..f6e9bd0b 100644
--- a/controller/Session.class.php
+++ b/controller/Session.class.php
@@ -11,6 +11,7 @@ class Session
KEY_DOWNLOAD_ACCESS_ERROR = 'download_error2',
KEY_DOWNLOAD_ALLOWED = 'beta_download_allowed2',
KEY_PREFINERY_USER_ID = 'prefinery_user_id',
+ KEY_PREFINER_USED_CUSTOM_CODE = 'prefinery_used_custom_code',
KEY_LIST_SUB_ERROR = 'list_error',
KEY_LIST_SUB_SIGNATURE = 'list_sub_sig',
KEY_LIST_SUB_SUCCESS = 'list_success',
diff --git a/controller/action/DownloadActions.class.php b/controller/action/DownloadActions.class.php
index 4364a0f5..33be7180 100644
--- a/controller/action/DownloadActions.class.php
+++ b/controller/action/DownloadActions.class.php
@@ -98,6 +98,10 @@ class DownloadActions extends Actions
{
$user = Prefinery::findOrCreateUser($email, $code, $referrerId);
static::setSessionVarsForPrefineryUser($user);
+ if ($code && strlen($code) > 2 && in_array(substr($code, 0, 2), ['my', 'pf', 'sl']))
+ {
+ Session::set(Session::KEY_PREFINER_USED_CUSTOM_CODE, true);
+ }
}
catch (PrefineryException $e)
{
diff --git a/lib/tools/Curl.class.php b/lib/tools/Curl.class.php
index 4da33b17..9fa9b258 100644
--- a/lib/tools/Curl.class.php
+++ b/lib/tools/Curl.class.php
@@ -2,8 +2,9 @@
class Curl
{
- const GET = 'GET';
- const POST = 'POST';
+ const GET = 'GET',
+ POST = 'POST',
+ PUT = 'PUT';
public static function get($url, $params = [], $options = [])
{
@@ -17,6 +18,12 @@ class Curl
return $body;
}
+ public static function put($url, $params = [], $options = [])
+ {
+ list ($status, $headers, $body) = static::doCurl(static::PUT, $url, $params, $options);
+ return $body;
+ }
+
public static function doCurl($method, $url, $params = [], $options = [])
{
$defaults = [
@@ -28,7 +35,7 @@ class Curl
'proxy' => null,
'password' => null,
'cookie' => null,
- 'json_post' => false,
+ 'json_data' => false,
];
$invalid = array_diff_key($options, $defaults);
@@ -39,7 +46,7 @@ class Curl
$options = array_merge($defaults, $options);
- if (!in_array($method, [static::GET, static::POST]))
+ if (!in_array($method, [static::GET, static::POST, static::PUT]))
{
throw new DomainException('Invalid method: ' . $method);
}
@@ -77,7 +84,19 @@ class Curl
if ($method == static::POST)
{
curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $options['json_post'] ? json_encode($params) : http_build_query($params));
+ }
+
+ if ($method == static::PUT)
+ {
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_PUT, 1);
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
+ }
+
+ if (in_array($method, [static::PUT, static::POST]))
+ {
+ print_r($options['json_data'] ? json_encode($params) : http_build_query($params));
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $options['json_data'] ? json_encode($params) : http_build_query($params));
}
if ($options['proxy'])
diff --git a/lib/tools/Prefinery.class.php b/lib/tools/Prefinery.class.php
index 3d7eb6b9..f2c52713 100644
--- a/lib/tools/Prefinery.class.php
+++ b/lib/tools/Prefinery.class.php
@@ -17,7 +17,7 @@ class Prefinery
'Accept: application/json',
'Content-type: application/json'
],
- 'json_post' => true
+ 'json_data' => true
];
@@ -65,14 +65,25 @@ class Prefinery
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
$user = Prefinery::createTester(array_filter([
'email' => $email,
- 'status' => $inviteCode ? static::STATE_ACTIVE : static::STATE_APPLIED, # yes, has to be ACTIVE to validate invite code
+ 'status' => $inviteCode ? static::STATE_ACTIVE: static::STATE_APPLIED, # yes, has to be ACTIVE to validate invite code
'invitation_code' => $inviteCode,
'referrer_id' => $referrerId,
'profile' => ['ip' => $ip, 'user_agent' => $ua]
]));
+
+// if ($inviteCode)
+// {
+// $user = static::updateTester(array_intersect_key($user, ['id' => null]) + ['status' => static::STATE_ACTIVE]);
+// if ($user['invitation_code'] != $inviteCode)
+// {
+// $user['is_custom_code'] = true;
+// }
+// }
+
+ $user['is_custom_code'] = false;
}
- unset($user['invitation_code']); // so we dont leak it
+// unset($user['invitation_code']); // so we dont leak it
return $user;
}
@@ -81,6 +92,25 @@ class Prefinery
return static::post('/testers', ['tester' => array_filter($testerData)], false);
}
+ public static function updateTester(array $testerData)
+ {
+ if (!$testerData['id'])
+ {
+ throw new PrefineryException('Update tester must be called with a tester id');
+ }
+ return static::put('/testers/' . $testerData['id'], ['tester' => array_diff_key(array_filter($testerData), ['id' => null])], false);
+ }
+
+ protected static function put($endpoint, array $data = [])
+ {
+ $apiKey = Config::get('prefinery_key');
+ $options = static::$curlOptions;
+ $options['headers'][] = 'X-HTTP-Method-Override: PUT';
+ return static::decodePrefineryResponse(
+ Curl::put(static::DOMAIN . static::PREFIX . $endpoint . '.json?api_key=' . $apiKey, $data, $options)
+ );
+ }
+
protected static function get($endpoint, array $data = [])
{
$apiKey = Config::get('prefinery_key');
@@ -119,10 +149,9 @@ class Prefinery
if (isset($data['errors']))
{
- throw new PrefineryException(implode("\n", array_map(function ($error)
- {
+ throw new PrefineryException($data['errors'] ? implode("\n", array_map(function ($error) {
return $error['message'];
- }, (array)$data['errors'])));
+ }, (array)$data['errors'])) : 'Received empty error array.');
}
return $data;
diff --git a/view/template/download/_betaNotice.php b/view/template/download/_betaNotice.php
index 9d711058..6cbdc9c1 100644
--- a/view/template/download/_betaNotice.php
+++ b/view/template/download/_betaNotice.php
@@ -1,4 +1,4 @@
-
+
{{download.beta}}
{{download.curse}}
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/view/template/download/getAllowed.php b/view/template/download/getAllowed.php
index 1b3e4859..a91aa1b7 100644
--- a/view/template/download/getAllowed.php
+++ b/view/template/download/getAllowed.php
@@ -8,7 +8,12 @@
$osTitle]) ?>
- {{download.verb}}
+
+
+ It looks like you may have been invited via a custom code.
+ If so, please check your email for a single-use code to claim credits after installing.
+
+