diff --git a/controller/action/DownloadActions.class.php b/controller/action/DownloadActions.class.php index 1a743116..e0a518de 100644 --- a/controller/action/DownloadActions.class.php +++ b/controller/action/DownloadActions.class.php @@ -1,67 +1,65 @@ ['/windows', 'Windows', 'icon-windows', '_windows'], - static::OS_OSX => ['/osx', 'OS X', 'icon-apple', '_osx'], - static::OS_LINUX => ['/linux', 'Linux', 'icon-linux', '_linux'], + static::OS_OSX => ['/osx', 'OS X', 'icon-apple', '_osx'], + static::OS_LINUX => ['/linux', 'Linux', 'icon-linux', '_linux'], 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() { - 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(); + $os = static::guessOs(); if ($os && isset($osChoices[$os])) { list($uri, $osTitle, $osIcon, $partial) = $osChoices[$os]; return ['download/getAllowed', [ - 'os' => $os, - 'osTitle' => $osTitle, - 'osIcon' => $osIcon, - 'prefineryUser' => Session::get(Session::KEY_PREFINERY_USER_ID) ? Prefinery::findTesterById(Session::get(Session::KEY_PREFINERY_USER_ID)) : [], - 'downloadHtml' => View::exists('download/' . $partial) ? + 'os' => $os, + 'osTitle' => $osTitle, + 'osIcon' => $osIcon, + 'prefineryUser' => $user ?: [], + 'downloadHtml' => View::exists('download/' . $partial) ? View::render('download/' . $partial, ['downloadUrl' => static::getDownloadUrl($os)]) : false ]]; @@ -70,10 +68,11 @@ class DownloadActions extends Actions return ['download/get-no-os']; } + public static function executeSignup() { $email = static::param('email'); - $code = static::param('code'); + $code = static::param('code'); if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) { @@ -81,8 +80,8 @@ class DownloadActions extends Actions } else { - $referrerId = isset($_GET['referrer_id']) ? $_GET['referrer_id'] : (isset($_POST['referrer_id']) ? $_POST['referrer_id'] : null); - $failure = false; + $referrerId = static::param('referrer_id'); + $failure = false; try { MailActions::subscribeToMailchimp($email, Mailchimp::LIST_GENERAL_ID); @@ -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.'); } } @@ -121,66 +123,33 @@ class DownloadActions extends Actions public static function prepareSignupPartial(array $vars) { return $vars + [ - 'defaultEmail' => static::param('e'), + 'defaultEmail' => static::param('e'), '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_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); return $vars + [ '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 $uri = strtok($_SERVER['REQUEST_URI'], '?'); - foreach(static::getOses() as $os => $osChoice) + foreach (static::getOses() as $os => $osChoice) { if ($osChoice[0] == $uri) { @@ -225,8 +194,8 @@ class DownloadActions extends Actions throw new DomainException('Unknown OS'); } - $apc = $useCache && extension_loaded('apc') && ini_get('apc.enabled'); - $key = 'lbry_release_data'; + $apc = $useCache && extension_loaded('apc') && ini_get('apc.enabled'); + $key = 'lbry_release_data'; $releaseData = null; if ($apc) @@ -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 @@ -254,10 +224,11 @@ class DownloadActions extends Actions 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']) || - $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']; } diff --git a/lib/tools/Prefinery.class.php b/lib/tools/Prefinery.class.php index ee5f5f63..9c1b4b0c 100644 --- a/lib/tools/Prefinery.class.php +++ b/lib/tools/Prefinery.class.php @@ -2,46 +2,49 @@ 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' => [ + 'headers' => [ 'Accept: application/json', 'Content-type: application/json' ], '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]); 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; } @@ -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']] + ])); + } + + 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', $params, false); + return static::post('/testers', ['tester' => array_filter($testerData)], 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'); - static::post('/testers/' . $prefineryId . '/checkin'); + $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 {} \ No newline at end of file +class PrefineryException extends Exception +{ +} \ No newline at end of file