1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: jaredchu |
5
|
|
|
* Date: 11/29/16 |
6
|
|
|
* Time: 3:47 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace JCFirebase; |
10
|
|
|
|
11
|
|
|
use Requests; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class JCFirebase |
15
|
|
|
* @package JCFirebase |
16
|
|
|
* reference https://www.firebase.com/docs/rest/api/ |
17
|
|
|
*/ |
18
|
|
|
class JCFirebase |
19
|
|
|
{ |
20
|
|
|
public $firebaseURI; |
21
|
|
|
public $firebaseDefaultPath; |
22
|
|
|
public $requestHeader = array( |
23
|
|
|
'accept' => 'application/json', |
24
|
|
|
'contentType' => 'application/json; charset=utf-8', |
25
|
|
|
'dataType' => 'json' |
26
|
|
|
); |
27
|
|
|
public $requestOptions = array(); |
28
|
|
|
/** |
29
|
|
|
* @var OAuth |
30
|
|
|
*/ |
31
|
|
|
protected $auth; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* JCFirebase constructor. |
36
|
|
|
* |
37
|
|
|
* @param $firebaseURI |
38
|
|
|
* @param array $firebaseAuth |
39
|
|
|
* @param string $firebaseDefaultPath |
40
|
|
|
*/ |
41
|
|
|
public function __construct($firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/') |
42
|
|
|
{ |
43
|
|
|
$this->firebaseURI = $firebaseURI; |
44
|
|
|
$this->firebaseDefaultPath = $firebaseDefaultPath; |
45
|
|
|
$this->setAuth($firebaseAuth); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $firebaseURI |
51
|
|
|
* @param $jsonString |
52
|
|
|
* @param string $firebaseDefaultPath |
53
|
|
|
* @return JCFirebase |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public static function fromJson($firebaseURI, $jsonString, $firebaseDefaultPath = '/') |
57
|
|
|
{ |
58
|
|
|
if ($jsonString) { |
59
|
|
|
$serviceAccount = $jsonString->client_email; |
60
|
|
|
$privateKey = $jsonString->private_key; |
61
|
|
|
|
62
|
|
|
return new self($firebaseURI, array( |
63
|
|
|
'key' => $privateKey, |
64
|
|
|
'iss' => $serviceAccount |
65
|
|
|
), $firebaseDefaultPath); |
66
|
|
|
} else { |
67
|
|
|
throw new \Exception("can't get data from key file"); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $firebaseURI |
73
|
|
|
* @param $keyFile |
74
|
|
|
* @param string $firebaseDefaultPath |
75
|
|
|
* |
76
|
|
|
* @return JCFirebase |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public static function fromKeyFile($firebaseURI, $keyFile, $firebaseDefaultPath = '/') |
80
|
|
|
{ |
81
|
|
|
$jsonString = null; |
82
|
|
|
try { |
83
|
|
|
$jsonString = json_decode(file_get_contents($keyFile)); |
84
|
|
|
} catch (\Exception $exception) { |
85
|
|
|
$jsonString = json_decode(Requests::get($keyFile)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return self::fromJson($firebaseURI, $jsonString, $firebaseDefaultPath); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setAuth($firebaseServiceAccount) |
92
|
|
|
{ |
93
|
|
|
if (isset($firebaseServiceAccount['key']) && isset($firebaseServiceAccount['iss'])) { |
94
|
|
|
$this->auth = new OAuth($firebaseServiceAccount['key'], $firebaseServiceAccount['iss']); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getPathURI($path = '', $print = '') |
99
|
|
|
{ |
100
|
|
|
//remove last slash from firebaseURI |
101
|
|
|
$template = '/'; |
102
|
|
|
$this->firebaseURI = rtrim($this->firebaseURI, $template); |
103
|
|
|
$path = rtrim($path, $template); |
104
|
|
|
$path = ltrim($path, $template); |
105
|
|
|
|
106
|
|
|
//check https |
107
|
|
|
if (strpos($this->firebaseURI, 'http://') !== false) { |
108
|
|
|
throw new \Exception("https is required."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
//check firebaseURI |
112
|
|
|
if (empty($this->firebaseURI)) { |
113
|
|
|
throw new \Exception("firebase URI is required"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (strpos($this->firebaseDefaultPath, "/") !== 0) { |
117
|
|
|
throw new \Exception("firebase default path must contain /"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
121
|
|
|
|
122
|
|
|
//set query data |
123
|
|
|
$queryData = array(); |
124
|
|
|
if (!empty($print)) { |
125
|
|
|
$queryData[JCFirebaseOption::OPTION_PRINT] = $print; |
126
|
|
|
} |
127
|
|
|
if (!empty($queryData)) { |
128
|
|
|
$pathURI = $pathURI . '?' . http_build_query($queryData); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->refreshToken(); |
132
|
|
|
|
133
|
|
|
return $pathURI; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getShallow($path = '', $options = array()) |
137
|
|
|
{ |
138
|
|
|
return Requests::get( |
139
|
|
|
$this->getPathURI($path) . '?' . http_build_query(array( |
140
|
|
|
JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
141
|
|
|
)), |
142
|
|
|
$this->requestHeader, |
143
|
|
|
$this->addDataToRequest($options) |
|
|
|
|
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $path |
149
|
|
|
* @param array $options |
150
|
|
|
* |
151
|
|
|
* @return \Requests_Response |
152
|
|
|
*/ |
153
|
|
|
public function get($path = '', $options = array()) |
154
|
|
|
{ |
155
|
|
|
return Requests::get( |
156
|
|
|
$this->addDataToPathURI($path, $options), $this->requestHeader, |
157
|
|
|
$this->addDataToRequest($options) |
|
|
|
|
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $path |
163
|
|
|
* @param array $options |
164
|
|
|
* |
165
|
|
|
* @return \Requests_Response |
166
|
|
|
*/ |
167
|
|
|
public function put($path = '', $options = array()) |
168
|
|
|
{ |
169
|
|
|
return Requests::put($this->getPathURI($path), $this->requestHeader, |
170
|
|
|
$this->addDataToRequest($options, true)); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $path |
175
|
|
|
* @param array $options |
176
|
|
|
* |
177
|
|
|
* @return \Requests_Response |
178
|
|
|
*/ |
179
|
|
|
public function post($path = '', $options = array()) |
180
|
|
|
{ |
181
|
|
|
return Requests::post($this->getPathURI($path), $this->requestHeader, |
182
|
|
|
$this->addDataToRequest($options, true)); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $path |
187
|
|
|
* @param array $options |
188
|
|
|
* |
189
|
|
|
* @return \Requests_Response |
190
|
|
|
*/ |
191
|
|
|
public function patch($path = '', $options = array()) |
192
|
|
|
{ |
193
|
|
|
return Requests::patch($this->getPathURI($path), $this->requestHeader, |
194
|
|
|
$this->addDataToRequest($options, true)); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $path |
199
|
|
|
* @param array $options |
200
|
|
|
* |
201
|
|
|
* @return \Requests_Response |
202
|
|
|
*/ |
203
|
|
|
public function delete($path = '', $options = array()) |
204
|
|
|
{ |
205
|
|
|
return Requests::delete($this->getPathURI($path), $this->requestHeader, |
206
|
|
|
$this->addDataToRequest($options)); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function refreshToken() |
210
|
|
|
{ |
211
|
|
|
$this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
protected function addDataToPathURI($path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET) |
215
|
|
|
{ |
216
|
|
|
$print = ''; |
217
|
|
|
if (isset($options['print'])) { |
218
|
|
|
if (JCFirebaseOption::isAllowPrint($reqType, $options['print'])) { |
219
|
|
|
$print = $options['print']; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this->getPathURI($path, $print); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
protected function addDataToRequest($options = array(), $jsonEncode = false) |
227
|
|
|
{ |
228
|
|
|
$requestOptions = array(); |
229
|
|
|
|
230
|
|
|
if (isset($options['data'])) { |
231
|
|
|
$requestOptions = array_merge($options['data'], $requestOptions); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($jsonEncode) { |
235
|
|
|
$requestOptions = json_encode($requestOptions); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $requestOptions; |
239
|
|
|
} |
240
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.