fix for custom codes (blah) + put support for curl

This commit is contained in:
Jeremy Kauffman 2016-08-09 08:41:06 -04:00
parent 98d9c5bb3f
commit f17d0ae5c4
6 changed files with 72 additions and 14 deletions

View file

@ -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',

View file

@ -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)
{

View file

@ -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'])

View file

@ -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;

View file

@ -1,4 +1,4 @@
<div class="notice notice-info">
<p>
<strong>{{download.beta}}</strong>
{{download.curse}}
</div>
</p>

View file

@ -8,7 +8,12 @@
<h1><?php echo __('download.for-os', ['%os%' => $osTitle]) ?> <span class="<?php echo $osIcon ?>"></span></h1>
<?php if ($downloadHtml): ?>
<?php echo View::render('download/_betaNotice') ?>
<h4>{{download.verb}}</h4>
<?php if (Session::get(Session::KEY_PREFINER_USED_CUSTOM_CODE)): ?>
<div class="notice notice-info spacer1">
It looks like you may have been invited via a custom code.
<strong>If so, please check your email for a single-use code to claim credits after installing.</strong>
</div>
<?php endif ?>
<?php echo $downloadHtml ?>
<?php else: ?>
<?php echo View::render('download/_unavailable', [