web/callback.php
changeset 1558 761ba7426984
parent 1557 7c67caaafdeb
child 1571 4a1e6952afe5
equal deleted inserted replaced
1557:7c67caaafdeb 1558:761ba7426984
     1 <?php
     1 <?php
     2 header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
     2 header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
     3 
     3 
     4 include_once 'common.php';
     4 include_once 'common.php';
     5 use Abraham\TwitterOAuth\TwitterOAuth;
     5 use Abraham\TwitterOAuth\TwitterOAuth;
       
     6 use GuzzleHttp\Client;
     6 
     7 
     7 if(!isset($_REQUEST['rep'])) {
     8 if(!isset($_REQUEST['rep'])) {
     8     $rep = $C_default_rep;
     9     $rep = $C_default_rep;
     9 }
    10 }
    10 else {
    11 else {
    34     /**
    35     /**
    35      * With Access Token in hand, let's try accessing the client again
    36      * With Access Token in hand, let's try accessing the client again
    36      */
    37      */
    37     header('Location: ' . ( isset($_SESSION['TWITTER_REDIRECT_URL']) ? $_SESSION['TWITTER_REDIRECT_URL'] : ( URL_ROOT . "$rep/client.php" ) ) );
    38     header('Location: ' . ( isset($_SESSION['TWITTER_REDIRECT_URL']) ? $_SESSION['TWITTER_REDIRECT_URL'] : ( URL_ROOT . "$rep/client.php" ) ) );
    38 
    39 
       
    40 } elseif (!empty($_GET) && isset($_SESSION['SOCIAL_AUTH_STATE']) && isset($_SESSION['SOCIAL_LOGIN_DOMAIN'])) {
       
    41 
       
    42     if($_GET['state'] != $_SESSION['SOCIAL_AUTH_STATE']) {
       
    43         exit("Invalid state, state mismatch. Aborting.");
       
    44     }
       
    45 
       
    46     $login_domain = $_SESSION['SOCIAL_LOGIN_DOMAIN'];
       
    47     $base_uri = "https://$login_domain";
       
    48     $client = new Client([
       
    49         'base_uri' => $base_uri,
       
    50         'timeout'  => 2.0,
       
    51     ]);
       
    52 
       
    53     //get token
       
    54     $client_ids = get_cached_app_ids($login_domain, $rep, $appCacheHandle);
       
    55 
       
    56     $client_id = $client_ids["client_id"];
       
    57     $client_secret = $client_ids["client_secret"];
       
    58 
       
    59     if(!$client_id || !$client_secret) {
       
    60         exit("Invalid app cache. Aborting.");
       
    61     }
       
    62 
       
    63     $token_resp = $client->post("/oauth/token", ['form_params' => [
       
    64         'client_id' => $client_id,
       
    65         'client_secret' => $client_secret,
       
    66         'redirect_uri' => URL_ROOT . "callback.php?rep=$rep",
       
    67         'grant_type' => 'authorization_code',
       
    68         'code' => $_GET['code'],
       
    69         'scope' => 'read write push',
       
    70     ]]);
       
    71     $token_ent = json_decode($token_resp->getBody(), true);
       
    72 
       
    73     $_SESSION['SOCIAL_ACCESS_TOKEN'] = serialize($token_ent['access_token']);
       
    74     $_SESSION['SOCIAL_AUTH_STATE'] = null;
       
    75 
       
    76     header('Location: ' . ( URL_ROOT . "$rep/client.php" ) );
       
    77 
    39 } else {
    78 } else {
    40     /**
    79     /**
    41      * Mistaken request? Some malfeasant trying something?
    80      * Mistaken request? Some malfeasant trying something?
    42      */
    81      */
    43     exit('Invalid callback request. Oops. Sorry.');
    82     exit('Invalid callback request. Oops. Sorry.');