|
| 1 | +<?php |
| 2 | + |
| 3 | +class Gitauth { |
| 4 | + function __construct($config){ |
| 5 | + $this->config = (object) $config; |
| 6 | + } |
| 7 | + function git_response_callback(){ |
| 8 | + if(!isset($_SESSION['response'])){ |
| 9 | + if($this->config->client_id != '' || |
| 10 | + $this->config->client_secreet != '' || |
| 11 | + $this->config->code != '' || |
| 12 | + $this->config->redirect_to != ''){ |
| 13 | + |
| 14 | + //GET USER ACCESS_TOKEN |
| 15 | + $ch = curl_init(); |
| 16 | + |
| 17 | + curl_setopt($ch, CURLOPT_URL,"https://github.com/login/oauth/access_token"); |
| 18 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); |
| 19 | + curl_setopt($ch, CURLOPT_POST, 1); |
| 20 | + curl_setopt($ch, CURLOPT_POSTFIELDS,'client_id='.$this->config->client_id. |
| 21 | + '&client_secret='.$this->config->client_secreet. |
| 22 | + '&code='.$this->config->code. |
| 23 | + '&accept=application/json'); |
| 24 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 25 | + |
| 26 | + $server_output = curl_exec($ch); |
| 27 | + $_SESSION['access_token'] = json_decode($server_output); |
| 28 | + $user = $_SESSION['access_token']; |
| 29 | + |
| 30 | + curl_close ($ch); |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + //GET USER RESPONSE |
| 36 | + $ch = curl_init(); |
| 37 | + |
| 38 | + curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/user?access_token='.$user->access_token); |
| 39 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: '.$this->config->app_name,'Accept: application/json')); |
| 40 | + curl_setopt($ch, CURLOPT_HEADER, 0); |
| 41 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 42 | + |
| 43 | + $get_response = curl_exec($ch); |
| 44 | + $_SESSION['response'] = $get_response;//json_decode($get_response); |
| 45 | + |
| 46 | + curl_close($ch); |
| 47 | + |
| 48 | + header('location: '.$this->config->redirect_to); |
| 49 | + |
| 50 | + }else{ |
| 51 | + echo 'Some config is missing'; |
| 52 | + } |
| 53 | + }else{ |
| 54 | + header('location: '.$this->config->redirect_to); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function git_authorize_url(){ |
| 59 | + return 'https://github.com/login/oauth/authorize?scope='.$this->config->scope.'&client_id='.$this->config->client_id; |
| 60 | + } |
| 61 | + function git_user_token(){ |
| 62 | + return $_SESSION['access_token']->access_token; |
| 63 | + } |
| 64 | + function git_user_data(){ |
| 65 | + return $_SESSION['response']; |
| 66 | + } |
| 67 | + function git_user_logout($redirect){ |
| 68 | + session_destroy(); |
| 69 | + |
| 70 | + header('location: '.$redirect); |
| 71 | + } |
| 72 | +} |
| 73 | +?> |
0 commit comments