fix prefinery

This commit is contained in:
Alex Grintsvayg 2016-07-21 11:16:18 -04:00
parent c808a9857c
commit 25845bfae4
2 changed files with 116 additions and 117 deletions

View file

@ -1,11 +1,5 @@
<?php
/**
* Description of DownloadActions
*
* @author jeremy
*/
class DownloadActions extends Actions
{
const OS_ANDROID = 'android',
@ -27,29 +21,33 @@ class DownloadActions extends Actions
public static function executeGet()
{
if (static::param('e'))
$email = static::param('e');
if ($email)
{
if (!filter_var(static::param('e'), FILTER_VALIDATE_EMAIL) || !static::findInPrefinery(static::param('e')))
$emailIsValid = filter_var($email, FILTER_VALIDATE_EMAIL);
$user = [];
if ($emailIsValid)
{
$user = Prefinery::findUser($email);
if ($user)
{
static::setSessionVarsForPrefineryUser($user);
}
}
if (!$emailIsValid || !$user)
{
Session::unsetKey(Session::KEY_PREFINERY_USER_ID);
Session::unsetKey(Session::KEY_DOWNLOAD_ALLOWED);
}
}
if (Session::get(Session::KEY_DOWNLOAD_ALLOWED))
if (!Session::get(Session::KEY_DOWNLOAD_ALLOWED))
{
return static::executeGetAccepted();
return ['download/get', ['os' => static::guessOs()]];
}
$os = static::guessOs();
return ['download/get', [
'os' => $os
]];
}
public static function executeGetAccepted()
{
$osChoices = static::getOses();
$os = static::guessOs();
@ -60,7 +58,7 @@ class DownloadActions extends Actions
'os' => $os,
'osTitle' => $osTitle,
'osIcon' => $osIcon,
'prefineryUser' => Session::get(Session::KEY_PREFINERY_USER_ID) ? Prefinery::findTesterById(Session::get(Session::KEY_PREFINERY_USER_ID)) : [],
'prefineryUser' => $user ?: [],
'downloadHtml' => View::exists('download/' . $partial) ?
View::render('download/' . $partial, ['downloadUrl' => static::getDownloadUrl($os)]) :
false
@ -70,6 +68,7 @@ class DownloadActions extends Actions
return ['download/get-no-os'];
}
public static function executeSignup()
{
$email = static::param('email');
@ -81,7 +80,7 @@ class DownloadActions extends Actions
}
else
{
$referrerId = isset($_GET['referrer_id']) ? $_GET['referrer_id'] : (isset($_POST['referrer_id']) ? $_POST['referrer_id'] : null);
$referrerId = static::param('referrer_id');
$failure = false;
try
{
@ -90,9 +89,11 @@ class DownloadActions extends Actions
catch (MailchimpSubscribeException $e)
{
}
try
{
static::subscribeToPrefinery($email, $code, $referrerId);
$user = Prefinery::findOrCreateUser($email, $code, $referrerId);
static::setSessionVarsForPrefineryUser($user);
}
catch (PrefineryException $e)
{
@ -103,7 +104,8 @@ class DownloadActions extends Actions
{
Session::set(Session::KEY_DOWNLOAD_ALLOWED, false);
Session::set(Session::KEY_DOWNLOAD_ACCESS_ERROR,
'We were unable to add you to the wait list. Received error "' . $e->getMessage() . '". Please contact ' . Config::HELP_CONTACT_EMAIL . ' if you think this is a mistake.' );
'We were unable to add you to the wait list. Received error "' . $e->getMessage() . '". Please contact ' .
Config::HELP_CONTACT_EMAIL . ' if you think this is a mistake.');
}
}
@ -127,54 +129,21 @@ class DownloadActions extends Actions
];
}
protected static function registerPrefineryUser($userData, $checkin = true)
protected static function setSessionVarsForPrefineryUser($userData)
{
Session::set(Session::KEY_DOWNLOAD_ALLOWED, in_array($userData['status'], ['active', 'invited']));
Session::set(Session::KEY_PREFINERY_USER_ID, $userData['id']);
if ($checkin)
{
//check-in changes status and should not be used
// Prefinery::checkIn($userData['id']);
}
}
public static function findInPrefinery($emailOrId)
{
$userData = is_numeric($emailOrId) ? Prefinery::findTesterById($emailOrId) : Prefinery::findTesterByEmail($emailOrId);
if ($userData)
{
static::registerPrefineryUser($userData);
}
return (boolean)$userData;
}
public static function subscribeToPrefinery($email, $inviteCode = null, $referrerId = null)
{
if (!static::findInPrefinery($email))
{
$userData = Prefinery::createTester(array_filter([
'email' => $email,
'status' => $inviteCode ? 'invited' : 'applied',
'invitation_code' => $inviteCode,
'referrer_id' => $referrerId,
'profile' => ['ip' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT']]
]));
static::registerPrefineryUser($userData, false);
}
Session::set(Session::KEY_DOWNLOAD_ALLOWED, in_array($userData['status'], [Prefinery::STATE_ACTIVE, Prefinery::STATE_INVITED]));
Session::set(Session::KEY_PREFINERY_USER_ID, (int)$userData['id']);
}
public static function prepareReferPartial(array $vars)
{
if (!Session::get(Session::KEY_PREFINERY_USER_ID))
$userId = (int)Session::get(Session::KEY_PREFINERY_USER_ID);
if (!$userId)
{
return null;
}
$prefineryUser = Prefinery::findTesterById(Session::get(Session::KEY_PREFINERY_USER_ID));
$prefineryUser = Prefinery::findUser($userId);
preg_match('/\?r\=(\w+)/', $prefineryUser['share_link'], $matches);
@ -238,7 +207,8 @@ class DownloadActions extends Actions
{
try
{
$releaseData = json_decode(Curl::get('https://api.github.com/repos/lbryio/lbry/releases/latest', [], ['user_agent' => 'LBRY']), true);
$releaseData =
json_decode(Curl::get('https://api.github.com/repos/lbryio/lbry/releases/latest', [], ['user_agent' => 'LBRY']), true);
if ($apc)
{
apc_store($key, $releaseData, 600); // cache for 10 min
@ -257,7 +227,8 @@ class DownloadActions extends Actions
foreach ($releaseData['assets'] as $asset)
{
if ($os == static::OS_LINUX && in_array($asset['content_type'], ['application/x-debian-package', 'application/x-deb']) ||
$os == static::OS_OSX && $asset['content_type'] == 'application/x-diskcopy')
$os == static::OS_OSX && $asset['content_type'] == 'application/x-diskcopy'
)
{
return $asset['browser_download_url'];
}

View file

@ -2,6 +2,16 @@
class Prefinery
{
const STATE_APPLIED = 'applied';
const STATE_INVITED = 'invited';
const STATE_IMPORTED = 'imported';
const STATE_REJECTED = 'rejected';
const STATE_ACTIVE = 'active';
const STATE_SUSPENDED = 'suspended';
const DOMAIN = 'https://lbry.prefinery.com';
const PREFIX = '/api/v2/betas/8679';
protected static $curlOptions = [
'headers' => [
'Accept: application/json',
@ -10,30 +20,23 @@ class Prefinery
'json_post' => true
];
protected static function get($endpoint, array $data = [])
public static function findUser($emailOrId)
{
$apiKey = Config::get('prefinery_key');
$baseUrl = 'https://lbry.prefinery.com/api/v2/betas/8679';
return static::decodePrefineryResponse(
Curl::get($baseUrl . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions)
);
$user = is_numeric($emailOrId) ? Prefinery::findTesterById($emailOrId) : Prefinery::findTesterByEmail($emailOrId);
if ($user)
{
unset($user['invitation_code']); // so we dont leak it
}
return $user;
}
protected static function post($endpoint, array $data = [], $allowEmptyResponse = true)
protected static function findTesterById($id)
{
$apiKey = Config::get('prefinery_key');
$baseUrl = 'https://lbry.prefinery.com/api/v2/betas/8679';
return static::decodePrefineryResponse(
Curl::post($baseUrl . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions),
$allowEmptyResponse
);
return static::get('/testers/' . (int)$id);
}
public static function findTesterByEmail($email)
protected static function findTesterByEmail($email)
{
$data = static::get('/testers', ['email' => $email]);
@ -41,7 +44,7 @@ class Prefinery
{
foreach ($data as $userData) //can partial match on email, very unlikely though
{
if ($userData['email'] == $email)
if (strtolower($userData['email']) == strtolower($email))
{
return $userData;
}
@ -52,22 +55,44 @@ class Prefinery
return null;
}
public static function findTesterById($id)
public static function findOrCreateUser($email, $inviteCode = null, $referrerId = null)
{
return static::get('/testers/' . $id);
$user = static::findUser($email);
if (!$user)
{
$user = Prefinery::createTester(array_filter([
'email' => $email,
'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' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT']]
]));
}
public static function createTester(array $testerData)
{
$params = ['tester' => array_filter($testerData)];
return static::post('/testers', $params, false);
unset($user['invitation_code']); // so we dont leak it
return $user;
}
public static function checkIn($prefineryId)
protected static function createTester(array $testerData)
{
throw new Exception('this sets a user to active, you probably do not want this');
static::post('/testers/' . $prefineryId . '/checkin');
return static::post('/testers', ['tester' => array_filter($testerData)], false);
}
protected static function get($endpoint, array $data = [])
{
$apiKey = Config::get('prefinery_key');
return static::decodePrefineryResponse(
Curl::get(static::DOMAIN . static::PREFIX . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions)
);
}
protected static function post($endpoint, array $data = [], $allowEmptyResponse = true)
{
$apiKey = Config::get('prefinery_key');
return static::decodePrefineryResponse(
Curl::post(static::DOMAIN . static::PREFIX . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions),
$allowEmptyResponse
);
}
protected static function decodePrefineryResponse($rawBody, $allowEmptyResponse = true)
@ -86,7 +111,8 @@ class Prefinery
if (isset($data['errors']))
{
throw new PrefineryException(implode("\n", array_map(function($error) {
throw new PrefineryException(implode("\n", array_map(function ($error)
{
return $error['message'];
}, (array)$data['errors'])));
}
@ -95,4 +121,6 @@ class Prefinery
}
}
class PrefineryException extends Exception {}
class PrefineryException extends Exception
{
}