From b8c7b25a0d65a2a42fe6e6f19c2d751c6e16eca9 Mon Sep 17 00:00:00 2001 From: rankscript Date: Fri, 5 Feb 2016 22:43:49 +0100 Subject: [PATCH 1/2] Configurable USER AGENT --- config.php | 1 + reddit.php | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/config.php b/config.php index 20108aa..1797616 100644 --- a/config.php +++ b/config.php @@ -10,6 +10,7 @@ class redditConfig{ //access token configuration from https://ssl.reddit.com/prefs/apps static $CLIENT_ID = 'YOUR CLIENT ID'; static $CLIENT_SECRET = 'YOUR SECRET'; + static $CLIENT_USERAGENT = 'v0 (by /u/YOUR_USERNAME)'; //access token request scopes //full list at http://www.reddit.com/dev/api/oauth diff --git a/reddit.php b/reddit.php index 3474a06..1e5b15a 100644 --- a/reddit.php +++ b/reddit.php @@ -672,9 +672,7 @@ private function runCurl($url, $postVals = null, $headers = null, $auth = false) CURLOPT_TIMEOUT => 10 ); - if (!empty($_SERVER['HTTP_USER_AGENT'])){ - $options[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT']; - } + $options[CURLOPT_USERAGENT] = php_uname('s').':'.redditConfig::$CLIENT_ID.':'.redditConfig::$CLIENT_USERAGENT; if ($postVals != null){ $options[CURLOPT_POSTFIELDS] = $postVals; From 02b422d1dcbef4a356ba51f151477c79eb2baae3 Mon Sep 17 00:00:00 2001 From: rankscript Date: Fri, 5 Feb 2016 22:48:58 +0100 Subject: [PATCH 2/2] Refresh Token and permanent auth support --- reddit.php | 92 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 19 deletions(-) diff --git a/reddit.php b/reddit.php index 1e5b15a..b10d215 100644 --- a/reddit.php +++ b/reddit.php @@ -11,52 +11,98 @@ */ class reddit{ private $access_token; - private $token_type; + private $token_type = 'bearer'; private $auth_mode = 'basic'; + static public $data; /** * Class Constructor * * Construct the class and simultaneously log a user in. * @link https://github.com/reddit/reddit/wiki/API%3A-login + * @param string $mode The auth mode to use, either oauth (default) or basic */ - public function __construct(){ - if(isset($_COOKIE['reddit_token'])){ - $token_info = explode(":", $_COOKIE['reddit_token']); - $this->token_type = $token_info[0]; - $this->access_token = $token_info[1]; - } else { + public function __construct($mode = 'oauth'){ + self::loadData(); + + if ($mode == 'oauth'){ + self::init_oauth(); + } else { + self::init_basic(); + } + } + + public function init_basic(){ + $this->apiHost = redditConfig::$ENDPOINT_STANDARD; + } + + public function init_oauth(){ + if (isset(self::$data['access_token']) && time() < self::$data['access_token_expires']) { + $this->access_token = self::$data['access_token']; + } + elseif (isset(self::$data['refresh_token'])) { + $refreshToken = self::$data['refresh_token']; + + //construct POST object for access token fetch request + $postvals = sprintf("grant_type=refresh_token&refresh_token=%s", + $refreshToken, + redditConfig::$ENDPOINT_OAUTH_REDIRECT, + redditConfig::$CLIENT_ID); + + //get JSON access token object (with refresh_token parameter) + $token = self::runCurl(redditConfig::$ENDPOINT_OAUTH_TOKEN, $postvals, null, true); + + //store token and type + if (isset($token['access_token'])){ + $this->access_token = $token['access_token']; + $this->token_type = $token['token_type']; + + //set token cookie for later use + self::$data['access_token'] = $this->access_token; + self::$data['access_token_expires'] = time() + 3600; + self::saveData(); + } + } + else { if (isset($_GET['code'])){ //capture code from auth $code = $_GET["code"]; //construct POST object for access token fetch request - $postvals = sprintf("code=%s&redirect_uri=%s&grant_type=authorization_code", + $postvals = sprintf("code=%s&redirect_uri=%s&grant_type=authorization_code&client_id=%s", $code, - redditConfig::$ENDPOINT_OAUTH_REDIRECT); + redditConfig::$ENDPOINT_OAUTH_REDIRECT, + redditConfig::$CLIENT_ID); //get JSON access token object (with refresh_token parameter) $token = self::runCurl(redditConfig::$ENDPOINT_OAUTH_TOKEN, $postvals, null, true); //store token and type - if (isset($token->access_token)){ - $this->access_token = $token->access_token; - $this->token_type = $token->token_type; + + if (isset($token['access_token'])){ + $this->access_token = $token['access_token']; + $this->token_type = $token['token_type']; //set token cookie for later use - $cookie_time = 60 * 59 + time(); //seconds * minutes = 59 minutes (token expires in 1hr) - setcookie('reddit_token', "{$this->token_type}:{$this->access_token}", $cookie_time); + self::$data['access_token'] = $this->access_token; + self::$data['access_token_expires'] = time() + 3600; + } + if (isset($token['refresh_token'])){ + //set refresh token cookie for later use + self::$data['refresh_token'] = $token['refresh_token']; } + self::saveData(); + } else { - $state = rand(); - $urlAuth = sprintf("%s?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&state=%s", + $state = mt_rand(); + $urlAuth = sprintf("%s?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&state=%s&duration=permanent", redditConfig::$ENDPOINT_OAUTH_AUTHORIZE, redditConfig::$CLIENT_ID, redditConfig::$ENDPOINT_OAUTH_REDIRECT, redditConfig::$SCOPES, $state); - //forward user to PayPal auth page + //forward user to Reddit auth page header("Location: $urlAuth"); } } @@ -693,7 +739,7 @@ private function runCurl($url, $postVals = null, $headers = null, $auth = false) $options[CURLOPT_SSL_VERIFYPEER] = false; $options[CURLOPT_SSL_VERIFYHOST] = 2; } - + curl_setopt_array($ch, $options); $apiResponse = curl_exec($ch); $response = json_decode($apiResponse); @@ -703,8 +749,16 @@ private function runCurl($url, $postVals = null, $headers = null, $auth = false) $response = $apiResponse; } curl_close($ch); - + return $response; } + + private function loadData() { + self::$data = file_exists('tokens.json') ? + json_decode(file_get_contents('tokens.json'), true) : array(); + } + private function saveData() { + file_put_contents('tokens.json', json_encode(self::$data)); + } } ?>