Created
February 25, 2019 08:31
-
-
Save eto4detak/1ca038246c9631c04d3c832a1887f4a3 to your computer and use it in GitHub Desktop.
wp OAuth 2.0 Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
// error_reporting(0); | |
require get_template_directory() . '/inc/vendor/autoload.php'; | |
require get_template_directory() . '/inc/Provider/response.php'; | |
require get_template_directory() . '/inc/Provider/EnvatoUser.php'; | |
require get_template_directory() . '/inc/Provider/Envato.php'; | |
$provider = new \Smachi\OAuth2\Client\Provider\Envato([ | |
'clientId' => 'id111111111111111', | |
'clientSecret' => 'pass111111111111111111', | |
'redirectUri' => 'https://example.com/', | |
]); | |
if ( ! isset( $_GET['code'] ) ) { | |
// If we don't have an authorization code then get one | |
$authUrl = $provider->getAuthorizationUrl(); | |
// Get the state generated for you and store it to the session. | |
$_SESSION['oauth2state'] = $provider->getState(); | |
// Redirect the user to the authorization URL. | |
echo new \Symfony\Component\HttpFoundation\RedirectResponse( $authUrl ); | |
// header('Location: ' . $authUrl); | |
exit; | |
} | |
// Check given state against previously stored one to mitigate CSRF attack | |
elseif ( empty( $_GET['state'] ) || ( $_GET['state'] !== $_SESSION['oauth2state'] ) ) { | |
unset( $_SESSION['oauth2state'] ); | |
exit('Invalid state'); | |
} | |
else{ | |
// return; | |
$token = $provider->getAccessToken( 'authorization_code', [ | |
'code' => $_GET['code'] | |
] ); | |
try { | |
// We got an access token, let's now get the user's details | |
$owner = $provider->getResourceOwner( $token, 'username' ); | |
$ownerEmail = $provider->getResourceOwner( $token, 'email' ); | |
// $ownerPurchases = $provider->getResourceOwner( $token, 'purchases', [ 'filter_by' => 'wordpress-themes' ] ); | |
$username = preg_replace( '/[^a-z0-9-_]/i', '', $owner->getUsername() ); | |
$email = $ownerEmail->getEmail(); | |
// $purchases = $ownerPurchases->getPurchases(); | |
$authorName = 'Templines'; | |
echo '<pre class="aaa" style="display:">'; | |
var_dump($username); | |
var_dump($email); | |
// var_dump($purchases); | |
echo '</pre>'; | |
$itemUrl = ''; | |
if ( empty( $purchases ) ) { | |
throw new \Exception( | |
"Only current buyers have access to <strong>$authorName</strong> support forums.", | |
401 | |
); | |
} | |
else{ | |
// Check for item support validity | |
$maybePurchaseFromAuthor = FALSE; | |
foreach($purchases as $item){ | |
if ( $authorName == $item['item']['author_username'] ){ | |
$maybePurchaseFromAuthor = TRUE; | |
if ( strtotime( $item['supported_until'] ) > time() ){ | |
// The support license is still valid | |
$itemUrl = $item['item']['url']; | |
break; | |
} | |
} | |
} | |
// Support expired | |
if ( $maybePurchaseFromAuthor ){ | |
if ( empty( $itemUrl ) ){ | |
throw new \Exception( | |
'Your support license has expired.<br>Please <a href="' . $itemUrl . '" target="_blank">renew it</a> and come back again to get access.', | |
901 | |
); | |
} | |
} | |
// Did not purchase any item from author | |
else{ | |
throw new \Exception( | |
"Only current buyers have access to <strong>$authorName</strong> support forums.", | |
401 | |
); | |
} | |
} | |
wp_redirect(get_home_url()); | |
} catch (\Exception $e){ | |
die( $e->getMessage() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment