Guest User

Untitled

a guest
Feb 23rd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.24 KB | None | 0 0
  1. <?php
  2. require_once('auth_functions.php');
  3. require_once('secret.php');
  4. session_start();
  5.  
  6. $useragent="drasy";
  7.  
  8. // Make sure that the secret matches the one set before the redirect.
  9. if (isset($_SESSION['auth_state']) and isset($_GET['state']) and $_SESSION['auth_state']==$_GET['state']) {
  10.     $code=$_GET['code'];
  11.     $state=$_GET['state'];
  12.  
  13.  
  14.     //Do the initial check.
  15.     $url='https://login.eveonline.com/oauth/token';
  16.     $verify_url='https://login.eveonline.com/oauth/verify';
  17.     $header='Authorization: Basic '.base64_encode($clientid.':'.$secret);
  18.     $fields_string='';
  19.     $fields=array(
  20.                 'grant_type' => 'authorization_code',
  21.                 'code' => $code
  22.             );
  23.     foreach ($fields as $key => $value) {
  24.         $fields_string .= $key.'='.$value.'&';
  25.     }
  26.     rtrim($fields_string, '&');
  27.     $ch = curl_init();
  28.     curl_setopt($ch, CURLOPT_URL, $url);
  29.     curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  30.     curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
  31.     curl_setopt($ch, CURLOPT_POST, count($fields));
  32.     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  33.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  35.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  36.     curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
  37.     $result = curl_exec($ch);
  38.  
  39.      if ($result===false) {
  40.         auth_error(curl_error($ch));
  41.     }
  42.     curl_close($ch);
  43.     $response=json_decode($result);
  44.     $auth_token=$response->access_token;
  45.     $ch = curl_init();
  46.  
  47. // Get the Character details from SSO
  48.  
  49.     $header='Authorization: Bearer '.$auth_token;
  50.     curl_setopt($ch, CURLOPT_URL, $verify_url);
  51.     curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  52.     curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
  53.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  54.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  55.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  56.     curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
  57.     $result = curl_exec($ch);
  58.     if ($result===false) {
  59.         auth_error(curl_error($ch));
  60.     }
  61.     curl_close($ch);
  62.     $response=json_decode($result);
  63.  
  64.  
  65.     if (!isset($response->CharacterID)) {
  66.         auth_error('No character ID returned');
  67.     }
  68.  
  69. // Lookup the character details in the DB.
  70.     require_once('db.inc.php');
  71.     $sql="select corporationname,corporationticker,user.corporationid,
  72.    alliancename,allianceticker,corporation.allianceid,characterid,characterownerhash,
  73.    user.id
  74.    from user
  75.    join corporation on user.corporationid=corporation.corporationid
  76.    join alliance on corporation.allianceid=alliance.allianceid
  77.    where
  78.    user.characterid=:characterid
  79.    and characterownerhash=:characterhash
  80.    ";
  81.  
  82.     $stmt = $dbh->prepare($sql);
  83.     $stmt->execute(array(':characterid'=>$response->CharacterID,':characterhash'=>$response->CharacterOwnerHash));
  84.  
  85.     while ($row = $stmt->fetchObject()) {
  86.         $userdetails=$row;
  87.         $userid=$row->id;
  88.     }
  89.  
  90. // Fill in character details, if they're not in the DB
  91.  
  92.     if (!isset($userdetails)) {
  93.         // No database entry for the user. lookup time.
  94.         error_log('Creating user details');
  95.         $ch = curl_init();
  96.         $lookup_url="https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=".$response->CharacterID;
  97.         curl_setopt($ch, CURLOPT_URL, $lookup_url);
  98.         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  99.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  100.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  101.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  102.         $result = curl_exec($ch);
  103.         curl_close($ch);
  104.         if ($result===false) {
  105.             auth_error('No such character on the API');
  106.         }
  107.         $xml=simplexml_load_string($result);
  108.         if (isset($xml->result->rowset->row->attributes()["characterID"])) {
  109.             $corporationID=(string)$xml->result->rowset->row->attributes()["corporationID"];
  110.             $corporationName=(string)$xml->result->rowset->row->attributes()["corporationName"];
  111.             $allianceID=(string)$xml->result->rowset->row->attributes()["allianceID"];
  112.             $allianceName=(string)$xml->result->rowset->row->attributes()["allianceName"];
  113.         } else {
  114.             auth_error("No character details returned from API");
  115.         }
  116.         //Alliance
  117.         if ($allianceID!=0) {
  118.             $alliancesql='select allianceid,allianceticker from alliance where allianceid=:allianceid';
  119.             $stmt = $dbh->prepare($alliancesql);
  120.             $stmt->execute(array(':allianceid'=>$allianceID));
  121.             while ($row = $stmt->fetchObject()) {
  122.                 $allianceticker=$row->allianceticker;
  123.             }
  124.             if (!isset($allianceticker)) {
  125.                 error_log('Getting alliance details');
  126.                 $alliance_url='http://public-crest.eveonline.com/alliances/'.$allianceID.'/';
  127.                 $ch = curl_init();
  128.                 curl_setopt($ch, CURLOPT_URL, $alliance_url);
  129.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  130.                 curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  131.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  132.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  133.                 $result = curl_exec($ch);
  134.                 curl_close($ch);
  135.                 $alliance_data=json_decode($result);
  136.                 $allianceticker=$alliance_data->shortName;
  137.                 $alliance_insert_sql="insert into alliance (allianceid,alliancename,allianceticker)
  138.                    values (:allianceid,:alliancename,:allianceticker)";
  139.                 $stmt = $dbh->prepare($alliance_insert_sql);
  140.                 $stmt->execute(
  141.                     array(
  142.                     ':allianceid'=>$allianceID,
  143.                     ':alliancename'=>$allianceName,
  144.                     ':allianceticker'=>$allianceticker)
  145.                 );
  146.             }
  147.  
  148.         } else {
  149.             $allianceName="No Alliance";
  150.             $allianceTicker="";
  151.         }
  152.         $userdetails['allianceid']=$allianceID;
  153.         $userdetails['alliancename']=$allianceName;
  154.         $userdetails['allianceticker']=$allianceticker;
  155.  
  156.         // Corporation
  157.         $corporationsql='select corporationid,corporationticker from corporation where corporationid=:corporationid';
  158.         $stmt = $dbh->prepare($corporationsql);
  159.         $stmt->execute(array(':corporationid'=>$corporationID));
  160.         while ($row = $stmt->fetchObject()) {
  161.             $corporationticker=$row->corporationid;
  162.         }
  163.         if (!isset($corporationticker)) {
  164.             error_log('Getting corporation details');
  165.             $corporation_url="https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationid=".$corporationID;
  166.             $ch = curl_init();
  167.             curl_setopt($ch, CURLOPT_URL, $corporation_url);
  168.             curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  169.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  170.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  171.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  172.             $result = curl_exec($ch);
  173.             curl_close($ch);
  174.             $corpxml=simplexml_load_string($result);
  175.             $corporationticker=$corpxml->result->ticker;
  176.             $corporation_insert_sql="insert into corporation
  177.                (corporationid,corporationname,corporationticker,allianceid)
  178.                values (:corporationid,:corporationname,:corporationticker,:allianceid)";
  179.             $stmt = $dbh->prepare($corporation_insert_sql);
  180.             $stmt->execute(
  181.                 array(
  182.                 ':corporationid'=>$corporationID,
  183.                 ':corporationname'=>$corporationName,
  184.                 ':corporationticker'=>$corporationticker,
  185.                 ':allianceid'=>$allianceID
  186.                 )
  187.             );
  188.         }
  189.         $userdetails['corporationid']=$corporationID;
  190.         $userdetails['corporationname']=$corporationName;
  191.         $userdetails['corporationticker']=$corporationticker;
  192.         $user_creation_sql='insert into user (characterid,characterownerhash,character_name,corporationid)
  193.            values (:characterid,:characterownerhash,:character_name,:corporationid)';
  194.         $stmt = $dbh->prepare($user_creation_sql);
  195.         $stmt->execute(
  196.             array(
  197.             ':characterid'=>$response->CharacterID,
  198.             ':characterownerhash'=>$response->CharacterOwnerHash,
  199.             ':character_name'=>$response->CharacterName,
  200.             ':corporationid'=>$corporationID
  201.             )
  202.         );
  203.         $userid=$dbh->lastInsertId();
  204.         $userdetails['id']=$userid;
  205.  
  206.         error_log("user added to db");
  207.     }
  208.  
  209.     $_SESSION['auth_characterid']=$response->CharacterID;
  210.     $_SESSION['auth_id']=$userid;
  211.     $_SESSION['auth_charactername']=$response->CharacterName;
  212.     $_SESSION['auth_userdetails']=json_encode($userdetails);
  213.     $_SESSION['auth_characterhash']=$response->CharacterOwnerHash;
  214.     session_write_close();
  215.     header('Location:'. $_SESSION['auth_redirect']);
  216.  
  217.     exit;
  218.  
  219. } else {
  220.     echo "State is wrong. Did you make sure to actually hit the login url first?";
  221.     error_log($_SESSION['auth_state']);
  222.     error_log($_GET['state']);
  223. }
Advertisement
Add Comment
Please, Sign In to add comment