mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-09-03 02:35:11 +00:00
send credits
This commit is contained in:
parent
4849c5b370
commit
02bd91272d
3 changed files with 132 additions and 30 deletions
|
@ -6,6 +6,9 @@ class Session
|
||||||
KEY_DOWNLOAD_ALLOWED = 'beta_download_allowed2',
|
KEY_DOWNLOAD_ALLOWED = 'beta_download_allowed2',
|
||||||
KEY_PREFINERY_USER_ID = 'prefinery_user_id',
|
KEY_PREFINERY_USER_ID = 'prefinery_user_id',
|
||||||
KEY_PREFINER_USED_CUSTOM_CODE = 'prefinery_used_custom_code',
|
KEY_PREFINER_USED_CUSTOM_CODE = 'prefinery_used_custom_code',
|
||||||
|
KEY_DEVELOPER_CREDITS_ERROR = 'developer_credits_error',
|
||||||
|
KEY_DEVELOPER_CREDITS_SUCCESS = 'developer_credits_success',
|
||||||
|
KEY_DEVELOPER_CREDITS_WALLET_ADDRESS = 'developer_credits_wallet_address',
|
||||||
KEY_LIST_SUB_ERROR = 'list_error',
|
KEY_LIST_SUB_ERROR = 'list_error',
|
||||||
KEY_USER_CULTURE = 'user_culture';
|
KEY_USER_CULTURE = 'user_culture';
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,7 @@
|
||||||
|
|
||||||
class AcquisitionActions extends Actions
|
class AcquisitionActions extends Actions
|
||||||
{
|
{
|
||||||
const DEVELOPER_REWARD = 250,
|
const DEVELOPER_REWARD = 250;
|
||||||
SESSION_KEY_DEVELOPER_CREDITS_ERROR = 'acquisition.developer-credits-error',
|
|
||||||
SESSION_KEY_DEVELOPER_CREDITS_SUCCESS = 'acquisition.developer-credits-success',
|
|
||||||
SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS = 'acquisition.developer-credits-wallet-address';
|
|
||||||
|
|
||||||
public static function executeThanks()
|
public static function executeThanks()
|
||||||
{
|
{
|
||||||
|
@ -47,27 +44,25 @@ class AcquisitionActions extends Actions
|
||||||
|
|
||||||
public static function executeDeveloperProgram()
|
public static function executeDeveloperProgram()
|
||||||
{
|
{
|
||||||
$vars = [
|
return ['acquisition/developer-program', [
|
||||||
'defaultWalletAddress' => Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS),
|
'defaultWalletAddress' => Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS),
|
||||||
'error' => Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR),
|
'error' => Session::getFlash(Session::KEY_DEVELOPER_CREDITS_ERROR),
|
||||||
'success' => Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_SUCCESS)
|
'success' => Session::getFlash(Session::KEY_DEVELOPER_CREDITS_SUCCESS)
|
||||||
];
|
]];
|
||||||
Session::unsetKey(static::SESSION_KEY_DEVELOPER_CREDITS_SUCCESS);
|
|
||||||
Session::unsetKey(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR);
|
|
||||||
return ['acquisition/developer-program', $vars];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function executeDeveloperProgramPost()
|
public static function executeDeveloperProgramPost()
|
||||||
{
|
{
|
||||||
$walletAddress = trim(Request::getPostParam('wallet'));
|
$walletAddress = trim(Request::getPostParam('wallet'));
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, $walletAddress);
|
Session::set(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, $walletAddress);
|
||||||
|
|
||||||
if (!$walletAddress)
|
if (!$walletAddress)
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'Please provide a wallet address.');
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Please provide a wallet address.');
|
||||||
}
|
}
|
||||||
elseif (!preg_match('/^b[1-9A-HJ-NP-Za-km-z]{33}$/', $walletAddress))
|
elseif (!preg_match('/^b[1-9A-HJ-NP-Za-km-z]{33}$/', $walletAddress))
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid wallet address.');
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid wallet address.');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -90,15 +85,15 @@ class AcquisitionActions extends Actions
|
||||||
public static function executeDeveloperProgramGithubCallback()
|
public static function executeDeveloperProgramGithubCallback()
|
||||||
{
|
{
|
||||||
$code = Request::getParam('code');
|
$code = Request::getParam('code');
|
||||||
$walletAddress = Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS);
|
$walletAddress = Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS);
|
||||||
|
|
||||||
if (!$walletAddress)
|
if (!$walletAddress)
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'Your wallet address disappeared while authenticated with GitHub.');
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Your wallet address disappeared while authenticated with GitHub.');
|
||||||
}
|
}
|
||||||
elseif (!$code)
|
elseif (!$code)
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid response from GitHub.');
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid response from GitHub.');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -112,11 +107,11 @@ class AcquisitionActions extends Actions
|
||||||
]);
|
]);
|
||||||
if (!$authResponseData || !isset($authResponseData['access_token']))
|
if (!$authResponseData || !isset($authResponseData['access_token']))
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.');
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.');
|
||||||
}
|
}
|
||||||
elseif (isset($authResponseData['error_description']))
|
elseif (isset($authResponseData['error_description']))
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']);
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -126,21 +121,55 @@ class AcquisitionActions extends Actions
|
||||||
'json_response' => true
|
'json_response' => true
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (date('Y-m', strtotime($userResponseData['created_at'])) > '2017-01')
|
if (date('Y-m-d', strtotime($userResponseData['created_at'])) > '2017-01-30')
|
||||||
{
|
{
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'This GitHub account is too recent.');
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This GitHub account is too recent.');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// print_r($userResponseData);
|
$lockName = 'github_dev_credits_write';
|
||||||
/*
|
$dataFile = ROOT_DIR . '/data/writeable/github_developer_credits';
|
||||||
* TODO: send credits here, see success message below for relevant values
|
|
||||||
* (keep wallet address in success message)
|
|
||||||
*/
|
|
||||||
|
|
||||||
Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_SUCCESS,
|
$lock = Lock::getLock($lockName, true); // EXCLUSIVE LOCK. IF SENDING CREDITS IS SLOW, THIS COULD BLOCK USERS
|
||||||
'Send credits to GitHub user ' . $userResponseData['login'] . ' (' . $userResponseData['email'] . ') at wallet address ' .
|
|
||||||
$walletAddress);
|
$existing = is_file($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
|
||||||
|
|
||||||
|
if (isset($existing[$userResponseData['login']]))
|
||||||
|
{
|
||||||
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'You account already received credits.');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list($code, $out, $err) =
|
||||||
|
Shell::exec('/usr/bin/lbrynet-cli send_amount_to_address amount=250 ' . escapeshellarg('address=' . $walletAddress));
|
||||||
|
|
||||||
|
if ($code != 0)
|
||||||
|
{
|
||||||
|
if (stripos($out, 'InsufficientFundsError') !== false)
|
||||||
|
{
|
||||||
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR,
|
||||||
|
'Our wallet is running low on funds. Please ping jeremy@lbry.io so he can refill it, then try again.');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR,
|
||||||
|
'Failed to send credits. This is an error on our side. Please email jeremy@lbry.io if it persists.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$existing[$userResponseData['login']] = [$userResponseData['email'], $walletAddress, date('Y-m-d H:i:s')];
|
||||||
|
file_put_contents($dataFile, json_encode($existing));
|
||||||
|
|
||||||
|
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_SUCCESS,
|
||||||
|
'Send credits to GitHub user ' . $userResponseData['login'] . ' (' . $userResponseData['email'] . ') at wallet address ' .
|
||||||
|
$walletAddress);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Lock::freeLock($lock);
|
||||||
|
$lock = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
70
lib/tools/Lock.class.php
Normal file
70
lib/tools/Lock.class.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
/* HOW TO USE
|
||||||
|
|
||||||
|
$lockFile = Lock::getLock('lock-name');
|
||||||
|
if (is_resource($lockFile)) {
|
||||||
|
|
||||||
|
// you have a lock here, do whatever you want
|
||||||
|
|
||||||
|
Lock::freeLock($lockFile);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// you could not get the lock. show a message or throw an exception or whatever
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
class Lock
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Creates a lockfile and acquires an exclusive lock on it.
|
||||||
|
*
|
||||||
|
* @param string $name The name of the lockfile.
|
||||||
|
* @param boolean $blocking Block until lock becomes available (default: don't block, just fail)
|
||||||
|
* @return mixed Returns the lockfile, or FALSE if a lock could not be acquired.
|
||||||
|
*/
|
||||||
|
public static function getLock($name, $blocking = false)
|
||||||
|
{
|
||||||
|
if (!preg_match('/^[A-Za-z0-9\.\-_]+$/', $name))
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Invalid lock name: "' . $name . '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = static::getLockDir() . '/' . $name;
|
||||||
|
if (!preg_match('/\.lo?ck$/', $filename))
|
||||||
|
{
|
||||||
|
$filename .= '.lck';
|
||||||
|
}
|
||||||
|
if (!file_exists($filename))
|
||||||
|
{
|
||||||
|
file_put_contents($filename, '');
|
||||||
|
chmod($filename, 0666); // if the file cant be opened for writing later, getting the lock will fail
|
||||||
|
}
|
||||||
|
$lockFile = fopen($filename, 'w+');
|
||||||
|
if (!flock($lockFile, $blocking ? LOCK_EX : LOCK_EX|LOCK_NB))
|
||||||
|
{
|
||||||
|
fclose($lockFile);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $lockFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free a lock.
|
||||||
|
*
|
||||||
|
* @param resource $lockFile
|
||||||
|
*/
|
||||||
|
public static function freeLock($lockFile)
|
||||||
|
{
|
||||||
|
if ($lockFile)
|
||||||
|
{
|
||||||
|
flock($lockFile, LOCK_UN);
|
||||||
|
fclose($lockFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getLockDir()
|
||||||
|
{
|
||||||
|
return sys_get_temp_dir();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue