mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-08-30 08:51:34 +00:00
fix prefinery
This commit is contained in:
parent
c808a9857c
commit
25845bfae4
2 changed files with 116 additions and 117 deletions
|
@ -1,67 +1,65 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
|
||||||
* Description of DownloadActions
|
|
||||||
*
|
|
||||||
* @author jeremy
|
|
||||||
*/
|
|
||||||
|
|
||||||
class DownloadActions extends Actions
|
class DownloadActions extends Actions
|
||||||
{
|
{
|
||||||
const OS_ANDROID = 'android',
|
const OS_ANDROID = 'android',
|
||||||
OS_IOS = 'ios',
|
OS_IOS = 'ios',
|
||||||
OS_LINUX = 'linux',
|
OS_LINUX = 'linux',
|
||||||
OS_OSX = 'osx',
|
OS_OSX = 'osx',
|
||||||
OS_WINDOWS = 'windows';
|
OS_WINDOWS = 'windows';
|
||||||
|
|
||||||
public static function getOses()
|
public static function getOses()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
static::OS_WINDOWS => ['/windows', 'Windows', 'icon-windows', '_windows'],
|
static::OS_WINDOWS => ['/windows', 'Windows', 'icon-windows', '_windows'],
|
||||||
static::OS_OSX => ['/osx', 'OS X', 'icon-apple', '_osx'],
|
static::OS_OSX => ['/osx', 'OS X', 'icon-apple', '_osx'],
|
||||||
static::OS_LINUX => ['/linux', 'Linux', 'icon-linux', '_linux'],
|
static::OS_LINUX => ['/linux', 'Linux', 'icon-linux', '_linux'],
|
||||||
static::OS_ANDROID => ['/android', 'Android', 'icon-android', '_android'],
|
static::OS_ANDROID => ['/android', 'Android', 'icon-android', '_android'],
|
||||||
static::OS_IOS => ['/ios', 'iOS', 'icon-mobile', '_ios']
|
static::OS_IOS => ['/ios', 'iOS', 'icon-mobile', '_ios']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function executeGet()
|
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_PREFINERY_USER_ID);
|
||||||
Session::unsetKey(Session::KEY_DOWNLOAD_ALLOWED);
|
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();
|
$osChoices = static::getOses();
|
||||||
$os = static::guessOs();
|
$os = static::guessOs();
|
||||||
|
|
||||||
if ($os && isset($osChoices[$os]))
|
if ($os && isset($osChoices[$os]))
|
||||||
{
|
{
|
||||||
list($uri, $osTitle, $osIcon, $partial) = $osChoices[$os];
|
list($uri, $osTitle, $osIcon, $partial) = $osChoices[$os];
|
||||||
return ['download/getAllowed', [
|
return ['download/getAllowed', [
|
||||||
'os' => $os,
|
'os' => $os,
|
||||||
'osTitle' => $osTitle,
|
'osTitle' => $osTitle,
|
||||||
'osIcon' => $osIcon,
|
'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) ?
|
'downloadHtml' => View::exists('download/' . $partial) ?
|
||||||
View::render('download/' . $partial, ['downloadUrl' => static::getDownloadUrl($os)]) :
|
View::render('download/' . $partial, ['downloadUrl' => static::getDownloadUrl($os)]) :
|
||||||
false
|
false
|
||||||
]];
|
]];
|
||||||
|
@ -70,10 +68,11 @@ class DownloadActions extends Actions
|
||||||
return ['download/get-no-os'];
|
return ['download/get-no-os'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function executeSignup()
|
public static function executeSignup()
|
||||||
{
|
{
|
||||||
$email = static::param('email');
|
$email = static::param('email');
|
||||||
$code = static::param('code');
|
$code = static::param('code');
|
||||||
|
|
||||||
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL))
|
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL))
|
||||||
{
|
{
|
||||||
|
@ -81,8 +80,8 @@ class DownloadActions extends Actions
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$referrerId = isset($_GET['referrer_id']) ? $_GET['referrer_id'] : (isset($_POST['referrer_id']) ? $_POST['referrer_id'] : null);
|
$referrerId = static::param('referrer_id');
|
||||||
$failure = false;
|
$failure = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
MailActions::subscribeToMailchimp($email, Mailchimp::LIST_GENERAL_ID);
|
MailActions::subscribeToMailchimp($email, Mailchimp::LIST_GENERAL_ID);
|
||||||
|
@ -90,9 +89,11 @@ class DownloadActions extends Actions
|
||||||
catch (MailchimpSubscribeException $e)
|
catch (MailchimpSubscribeException $e)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
static::subscribeToPrefinery($email, $code, $referrerId);
|
$user = Prefinery::findOrCreateUser($email, $code, $referrerId);
|
||||||
|
static::setSessionVarsForPrefineryUser($user);
|
||||||
}
|
}
|
||||||
catch (PrefineryException $e)
|
catch (PrefineryException $e)
|
||||||
{
|
{
|
||||||
|
@ -103,7 +104,8 @@ class DownloadActions extends Actions
|
||||||
{
|
{
|
||||||
Session::set(Session::KEY_DOWNLOAD_ALLOWED, false);
|
Session::set(Session::KEY_DOWNLOAD_ALLOWED, false);
|
||||||
Session::set(Session::KEY_DOWNLOAD_ACCESS_ERROR,
|
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.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,66 +123,33 @@ class DownloadActions extends Actions
|
||||||
public static function prepareSignupPartial(array $vars)
|
public static function prepareSignupPartial(array $vars)
|
||||||
{
|
{
|
||||||
return $vars + [
|
return $vars + [
|
||||||
'defaultEmail' => static::param('e'),
|
'defaultEmail' => static::param('e'),
|
||||||
'allowInviteCode' => true,
|
'allowInviteCode' => true,
|
||||||
'referralCode' => static::param('r', '')
|
'referralCode' => static::param('r', '')
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
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_DOWNLOAD_ALLOWED, in_array($userData['status'], [Prefinery::STATE_ACTIVE, Prefinery::STATE_INVITED]));
|
||||||
Session::set(Session::KEY_PREFINERY_USER_ID, $userData['id']);
|
Session::set(Session::KEY_PREFINERY_USER_ID, (int)$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function prepareReferPartial(array $vars)
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$prefineryUser = Prefinery::findTesterById(Session::get(Session::KEY_PREFINERY_USER_ID));
|
$prefineryUser = Prefinery::findUser($userId);
|
||||||
|
|
||||||
preg_match('/\?r\=(\w+)/', $prefineryUser['share_link'], $matches);
|
preg_match('/\?r\=(\w+)/', $prefineryUser['share_link'], $matches);
|
||||||
|
|
||||||
return $vars + [
|
return $vars + [
|
||||||
'prefineryUser' => $prefineryUser,
|
'prefineryUser' => $prefineryUser,
|
||||||
'referralCode' => $matches[1] ?: 'unknown'
|
'referralCode' => $matches[1] ?: 'unknown'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +157,7 @@ class DownloadActions extends Actions
|
||||||
{
|
{
|
||||||
//if exact OS is requested, use that
|
//if exact OS is requested, use that
|
||||||
$uri = strtok($_SERVER['REQUEST_URI'], '?');
|
$uri = strtok($_SERVER['REQUEST_URI'], '?');
|
||||||
foreach(static::getOses() as $os => $osChoice)
|
foreach (static::getOses() as $os => $osChoice)
|
||||||
{
|
{
|
||||||
if ($osChoice[0] == $uri)
|
if ($osChoice[0] == $uri)
|
||||||
{
|
{
|
||||||
|
@ -225,8 +194,8 @@ class DownloadActions extends Actions
|
||||||
throw new DomainException('Unknown OS');
|
throw new DomainException('Unknown OS');
|
||||||
}
|
}
|
||||||
|
|
||||||
$apc = $useCache && extension_loaded('apc') && ini_get('apc.enabled');
|
$apc = $useCache && extension_loaded('apc') && ini_get('apc.enabled');
|
||||||
$key = 'lbry_release_data';
|
$key = 'lbry_release_data';
|
||||||
$releaseData = null;
|
$releaseData = null;
|
||||||
|
|
||||||
if ($apc)
|
if ($apc)
|
||||||
|
@ -238,7 +207,8 @@ class DownloadActions extends Actions
|
||||||
{
|
{
|
||||||
try
|
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)
|
if ($apc)
|
||||||
{
|
{
|
||||||
apc_store($key, $releaseData, 600); // cache for 10 min
|
apc_store($key, $releaseData, 600); // cache for 10 min
|
||||||
|
@ -254,10 +224,11 @@ class DownloadActions extends Actions
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($releaseData['assets'] as $asset)
|
foreach ($releaseData['assets'] as $asset)
|
||||||
{
|
{
|
||||||
if ($os == static::OS_LINUX && in_array($asset['content_type'], ['application/x-debian-package', 'application/x-deb']) ||
|
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'];
|
return $asset['browser_download_url'];
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,46 +2,49 @@
|
||||||
|
|
||||||
class Prefinery
|
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 = [
|
protected static $curlOptions = [
|
||||||
'headers' => [
|
'headers' => [
|
||||||
'Accept: application/json',
|
'Accept: application/json',
|
||||||
'Content-type: application/json'
|
'Content-type: application/json'
|
||||||
],
|
],
|
||||||
'json_post' => true
|
'json_post' => true
|
||||||
];
|
];
|
||||||
|
|
||||||
protected static function get($endpoint, array $data = [])
|
|
||||||
|
public static function findUser($emailOrId)
|
||||||
{
|
{
|
||||||
$apiKey = Config::get('prefinery_key');
|
$user = is_numeric($emailOrId) ? Prefinery::findTesterById($emailOrId) : Prefinery::findTesterByEmail($emailOrId);
|
||||||
|
if ($user)
|
||||||
$baseUrl = 'https://lbry.prefinery.com/api/v2/betas/8679';
|
{
|
||||||
|
unset($user['invitation_code']); // so we dont leak it
|
||||||
return static::decodePrefineryResponse(
|
}
|
||||||
Curl::get($baseUrl . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions)
|
return $user;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function post($endpoint, array $data = [], $allowEmptyResponse = true)
|
protected static function findTesterById($id)
|
||||||
{
|
{
|
||||||
$apiKey = Config::get('prefinery_key');
|
return static::get('/testers/' . (int)$id);
|
||||||
|
|
||||||
$baseUrl = 'https://lbry.prefinery.com/api/v2/betas/8679';
|
|
||||||
|
|
||||||
return static::decodePrefineryResponse(
|
|
||||||
Curl::post($baseUrl . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions),
|
|
||||||
$allowEmptyResponse
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function findTesterByEmail($email)
|
protected static function findTesterByEmail($email)
|
||||||
{
|
{
|
||||||
$data = static::get('/testers', ['email' => $email]);
|
$data = static::get('/testers', ['email' => $email]);
|
||||||
|
|
||||||
if ($data && is_array($data) && count($data))
|
if ($data && is_array($data) && count($data))
|
||||||
{
|
{
|
||||||
foreach($data as $userData) //can partial match on email, very unlikely though
|
foreach ($data as $userData) //can partial match on email, very unlikely though
|
||||||
{
|
{
|
||||||
if ($userData['email'] == $email)
|
if (strtolower($userData['email']) == strtolower($email))
|
||||||
{
|
{
|
||||||
return $userData;
|
return $userData;
|
||||||
}
|
}
|
||||||
|
@ -52,22 +55,44 @@ class Prefinery
|
||||||
return null;
|
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']]
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($user['invitation_code']); // so we dont leak it
|
||||||
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function createTester(array $testerData)
|
protected static function createTester(array $testerData)
|
||||||
{
|
{
|
||||||
$params = ['tester' => array_filter($testerData)];
|
return static::post('/testers', ['tester' => array_filter($testerData)], false);
|
||||||
|
|
||||||
return static::post('/testers', $params, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function checkIn($prefineryId)
|
protected static function get($endpoint, array $data = [])
|
||||||
{
|
{
|
||||||
throw new Exception('this sets a user to active, you probably do not want this');
|
$apiKey = Config::get('prefinery_key');
|
||||||
static::post('/testers/' . $prefineryId . '/checkin');
|
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)
|
protected static function decodePrefineryResponse($rawBody, $allowEmptyResponse = true)
|
||||||
|
@ -86,7 +111,8 @@ class Prefinery
|
||||||
|
|
||||||
if (isset($data['errors']))
|
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'];
|
return $error['message'];
|
||||||
}, (array)$data['errors'])));
|
}, (array)$data['errors'])));
|
||||||
}
|
}
|
||||||
|
@ -95,4 +121,6 @@ class Prefinery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PrefineryException extends Exception {}
|
class PrefineryException extends Exception
|
||||||
|
{
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue