lbry.com/controller/action/DeveloperActions.class.php
Alex Grintsvayg 39f19d19a8 typo??
2017-02-28 13:40:56 -05:00

128 lines
4.2 KiB
PHP

<?php
class DeveloperActions extends Actions
{
const DEVELOPER_REWARD = 250,
API_DOC_URL = 'https://lbryio.github.io/lbry/';
public static function executeQuickstart(string $step = null)
{
$stepLabels = [
'' => 'Home',
'install' => 'Installation',
'api' => 'The API',
'credits' => 'Credits'
];
$allSteps = array_keys($stepLabels);
$currentStep = $step ?: $allSteps[0];
$viewParams = [
'currentStep' => $currentStep,
'stepLabels' => $stepLabels
];
if ($currentStep !== 'all')
{
if (!isset($stepLabels[$currentStep]))
{
Controller::redirect('/quickstart');
}
$stepNum = array_flip($allSteps)[$currentStep];
$viewParams += [
'stepNum' => $stepNum,
'prevStep' => $stepNum === 0 ? null : $allSteps[$stepNum - 1],
'nextStep' => $stepNum + 1 >= count($allSteps) ? null : $allSteps[$stepNum + 1],
];
}
return ['developer/quickstart', $viewParams];
}
public static function prepareQuickstartHomePartial(array $vars)
{
return $vars + [
'usdValue' => static::DEVELOPER_REWARD * LBRY::getLBCtoUSDRate()
];
}
public static function prepareFormNewDeveloperRewardPartial(array $vars)
{
return $vars + [
'defaultWalletAddress' => Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS),
'error' => Session::get(Session::KEY_DEVELOPER_LAST_FORM) == "new_developer" ? Session::getFlash(Session::KEY_DEVELOPER_CREDITS_ERROR) : '',
'apiUrl' => LBRY::getApiUrl('/user/new_github')
];
}
public static function prepareFormCreditsPublishPartial(array $vars)
{
return $vars + [
'defaultWalletAddress' => Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS),
'error' => Session::get(Session::KEY_DEVELOPER_LAST_FORM) == "new_publish" ? Session::getFlash(Session::KEY_DEVELOPER_CREDITS_ERROR) : '',
'apiUrl' => LBRY::getApiUrl('/reward/new')
];
}
public static function executeQuickstartAuth()
{
Session::set(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, trim(Request::getPostParam('wallet_address')));
Session::set(Session::KEY_DEVELOPER_LAST_FORM, Request::getPostParam('formName'));
if (Request::getPostParam('returnUrl'))
{
Session::set(Session::KEY_DEVELOPER_RETURN_URL_SUCCESS, Request::getPostParam('returnUrl'));
}
if (!Config::get('github_developer_credits_client_id'))
{
throw new Exception('no github client id');
}
$gitHubParams = [
'client_id' => Config::get('github_developer_credits_client_id'),
'redirect_uri' => Request::getHostAndProto() . '/quickstart/github/callback',
'scope' => 'user:email,public_repo',
'allow_signup' => false
];
return Controller::redirect('https://github.com/login/oauth/authorize?' . http_build_query($gitHubParams));
}
public static function executeQuickstartGithubCallback()
{
$code = Request::getParam('code');
if (!$code)
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid response from GitHub.');
}
else
{
$authResponseData = Curl::post('https://github.com/login/oauth/access_token', [
'code' => $code,
'client_id' => Config::get('github_developer_credits_client_id'),
'client_secret' => Config::get('github_developer_credits_client_secret')
], [
'headers' => ['Accept: application/json'],
'json_response' => true
]);
if (!$authResponseData || !isset($authResponseData['access_token']))
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.');
}
elseif (isset($authResponseData['error_description']))
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']);
}
else
{
Session::set(Session::KEY_GITHUB_ACCESS_TOKEN, $authResponseData['access_token']);
}
}
return Controller::redirect(Session::get(Session::KEY_DEVELOPER_RETURN_URL_SUCCESS, '/quickstart/credits'));
}
}