-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgooddata.class.php
More file actions
197 lines (176 loc) · 6.68 KB
/
gooddata.class.php
File metadata and controls
197 lines (176 loc) · 6.68 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Class simplifying use of GoodData API
*/
class GoodData {
private static $HOST = 'secure.gooddata.com'; // configure if using a testing server
private $headers = array('accept' => 'Accept: application/json','content-type' => 'Content-Type: application/json; charset=utf-8');
private $authSst;
private $authTt;
/**
* Constructor
*
* @param string $login Username to use for login
* @param string $password Password to use for login
* @param string $project Optional Initial project id, can be re-set later
* @author Jakub Nesetril
*/
public function __construct() {
self::$HOST = 'https://secure.gooddata.com';
$this->authSst = $sst;
}
public function login($login, $password) {
// POST /gdc/account/login {postUserLogin: {login: '...', password: '...', remember: 0/1}}
$data = json_encode(array('postUserLogin'=>array('login'=>$login,'password'=>$password,'remember'=>0)));
// https://secure.gooddata.com/gdc/account/login
$stream = $this->getHttpStream(self::$HOST.'/gdc/account/login', 'POST', array_values($this->headers), $data);
$meta = stream_get_meta_data($stream);
fclose($stream);
// Find GDCAuthSST cookie value and save
$this->authSst = $this->findCookie($meta['wrapper_data'], 'GDCAuthSST');
if (is_null($this->authSst)) {
throw new GDCException("Cookie GDCAuthSST not found (".__METHOD__.")");
}
$this->getToken();
// Add both cookies to headers
$this->headers['x-gdc-auth'] = "X-GDC-AUTH: ".$this->authSst;
$this->headers['cookie'] = "Cookie: GDCAuthSST=".$this->authSst."; GDCAuthTT=".$this->authTt."\r\n";
}
/**
* Retrieves and returns security token
*
* @return string
*/
public function getToken() {
$stream = $this->getHttpStream(self::$HOST.'/gdc/account/token', 'GET');
$meta = stream_get_meta_data($stream);
fclose($stream);
// Find GDCAuthTT cookie value and save
$token = $this->findCookie($meta['wrapper_data'], 'GDCAuthTT');
if (is_null($token)) {
var_dump($meta['wrapper_data']);
throw new Exception('status=FAILED action=getToken Did not find GDCAuthTT in response cookies');
exit;
}
$this->authTt = $token;
return $token;
}
/**
* Get Permissions for User in a Project
*
* @return String JSON-string
* @author Jakub Nešetřil
**/
public function getPermissions($project, $user) {
$url = self::$HOST.$project.'/users/'.$user.'/permissions';
$stream = $this->getHttpStream($url,'GET');
$response = stream_get_contents($stream);
fclose($stream);
return json_decode($response);
}
/**
* Get User object from URL
*
* @return String JSON-string
* @param String $url Link to the user (starting with /gdc)
* @author Jakub Nešetřil
**/
public function getUser($url) {
$url = self::$HOST.$url;
$stream = $this->getHttpStream($url,'GET');
$response = stream_get_contents($stream);
fclose($stream);
return json_decode($response);
}
/**
* undocumented function
*
* @return void
* @author Jakub Nešetřil
**/
public function getProjects() {
$url = self::$HOST.'/gdc/md';
$stream = $this->getHttpStream($url,'GET');
$response = stream_get_contents($stream);
fclose($stream);
return json_decode($response);
}
/**
* Get Project Information
*
* @return String JSON-string
* @author Jakub Nešetřil
**/
public function getProject($project) {
$url = self::$HOST.$project;
$stream = $this->getHttpStream($url,'GET');
$response = stream_get_contents($stream);
fclose($stream);
return json_decode($response);
}
/**
* Utility function: Gets an HTTP stream using options passed
*
* @param string $api Which part of api to use (eg 'login')
* @param string $method POST / GET
* @param array $headers Array of headers to use, as well as standard headers
* @param string $content Data to be included in request. Should usually be JSON-encoded
* @return HTTP Stream context
* @author Jakub Nesetril
*/
private function getHttpStream($url, $method, $headers=array(), $content='') {
global $cafile;
// Add dynamic headers (cookie, x-gdc-auth) and combine custom headers with standard headers
if (!array_key_exists('cookie', $headers)) {
$headers['cookie'] = 'Cookie: GDCAuthSST='.$this->authSst.'; GDCAuthTT='.$this->authTt;
}
if (isset($this->authSst)) {
$headers['x-gdc-auth'] = "X-GDC-AUTH: ".$this->authSst;
}
$headers = array_filter(array_merge($this->headers, $headers));
// Set up context options including content
$options = array(
'ssl' => array(),
'http' => array(
'method' => $method,
'header' => array_values($headers),
)
);
if (!empty($content)) {
$options['http']['content'] = $content;
}
// Try to set up encryption
if (file_exists($cafile)) {
$options['ssl']['cafile'] = $cafile;
$options['ssl']['verify_peer'] = true;
} else {
echo('status=WARNING action=http Proceeding without peer cerfificate verification');
}
// Get context and stream, return stream
$context = stream_context_create($options);
$stream = fopen($url, 'r', false, $context); // Suppress warning on failure as fopen error is unusable
if (!$stream) {
throw new Exception('status=FAILED action=http Could not open stream to '.$url,'ERROR');
throw new Exception('Could not open stream to '.$url);
exit;
}
return $stream;
}
/**
* Utility function: Retrieves specified cookie from supplied response headers
* NB: Very basic parsing - ignores path, domain, expiry
*
* @return string or null if specified cookie not found
* @author Jakub Nesetril
*/
private function findCookie(array $headers, $name) {
$cookie = array_shift(array_filter($headers, create_function('$v', 'return (strpos($v, "Set-Cookie: '.$name.'=") === 0);')));
if (!$cookie) {
return null;
}
$cookie = array_shift(explode('; ', str_replace('Set-Cookie: ', '', $cookie)));
$cookie = substr($cookie, strpos($cookie, '=')+1);
return $cookie;
}
}
?>