-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgithubapi.php
More file actions
142 lines (129 loc) · 4.05 KB
/
Copy pathgithubapi.php
File metadata and controls
142 lines (129 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace diversen;
use diversen\mycurl;
/**
* contains simple class for using oauth with github
* @package githubapi
*/
/**
* Simple class for using the github oauth api:
*
*
*
*
* @package githubapi
*/
class githubapi {
/**
*
* @var array $errors holding errors
*/
public $errors = array ();
/**
* @var holding return code
* @var string
*/
public $returnCode = null;
/**
* We need a github OAuth login url from configuration
* @param array $config e.g.
*
* <code>
* $config = array (
* 'redirect_uri' => 'http://localhost:8080/callback.php',
* 'client_id' => 'app id',
* 'state' => md5(uniqid()),
* 'scope' => 'user'
* );
* </code>
*
* If you don't set scope you can only get users basic profile info,
* but you can still use it as e.g. a login method.
*
* @return string $url a github url where you can obtain users accept of
* using his account according to scope
*/
public function getAccessUrl ($config) {
$_SESSION['state'] = $config['state'];
$url = 'https://github.com/login/oauth/authorize';
$query = http_build_query($config);
$url.= '?' . $query;
return $url;
}
/**
* Sets the access token in a session variable, which
* then can be used when calling the api
* @param array $post e.g.
*
* <code>
* $post = array (
* 'redirect_uri' => 'http://localhost:8080/callback.php',
* 'client_id' => 'app_id',
* 'client_secret' => 'app_secret',
* );
* </code>
*
* @return boolean $res true on success and false on failure
* any errors will be stored in githubapi::$errors
*/
public function setAccessToken ($post) {
if (isset($_GET['error'])) {
$this->errors[] = $_GET['error'];
return false;
}
if (isset($_GET['code'])) {
$c = new mycurl('https://github.com/login/oauth/access_token');
$post['code'] = $_GET['code'];
$post['state'] = $_SESSION['state'];
$c->setPost($post);
$c->createCurl();
$resp = $c->getWebPage();
parse_str($resp, $ary);
if (isset($ary['access_token']) && isset($ary['token_type']) && $ary['token_type'] == 'bearer') {
$_SESSION['access_token'] = $ary['access_token'];
return true;
} else {
$this->errors[] = "No access token returned";
return false;
}
}
return false;
}
/**
* Make an API call. For all
*
* @see http://developer.github.com/v3/
*
* @param string $command e.g "/users"
* @param string $request e.g "POST" or PATCH, DELETE - if empty it is a GET
* @param array $post vaiables $_POST variables to send
* @param boolean $json should we return output as json. Default is false
* @return boolean|array false if failure. Else: $ary response from github server
*/
public function apiCall ($command, $request = null, $post = null, $json = false) {
if (!isset($_SESSION['access_token']) || empty($_SESSION['access_token'])) {
$this->errors[] = 'No valid token';
return false;
}
$end_point = 'https://api.github.com';
$command = $end_point . "$command";
$command.= "?access_token=$_SESSION[access_token]";
$c = new mycurl($command);
if (isset($request)) {
$c->setRequest($request);
}
if (isset($post)) {
$json = json_encode($post);
$c->setPost($json);
}
$c->createCurl();
$resp = $c->getWebPage();
$this->returnCode = $c->getHttpStatus();
if ($json) {
return $resp;
} else {
$ary = json_decode($resp, true);
return $ary;
}
}
}