forked from cihadoge/parse-php-sdk-for-codeigniter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseClient.php
More file actions
executable file
·380 lines (339 loc) · 9.83 KB
/
Copy pathParseClient.php
File metadata and controls
executable file
·380 lines (339 loc) · 9.83 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
require_once "ParseStorageInterface.php";
require_once "ParseSessionStorage.php";
require_once "ParseMemoryStorage.php";
require_once "ParseException.php";
require_once "ParseUser.php";
require_once "ParseRole.php";
require_once "ParseInstallation.php";
/**
* ParseClient - Main class for Parse initialization and communication
*
* @package Parse
* @author Fosco Marotto <fjm@fb.com>
*/
class ParseClient
{
/**
* Constant for the API Server Host Address.
* @ignore
*/
const HOST_NAME = 'https://api.parse.com';
/**
* @var - String for applicationId.
*/
private static $applicationId;
/**
* @var - String for REST API Key.
*/
private static $restKey;
/**
* @var - String for Master Key.
*/
private static $masterKey;
/**
* @var ParseStorageInterface Object for managing persistence
*/
private static $storage;
/**
* Constant for version string to include with requests.
* @ignore
*/
const VERSION_STRING = 'php1.0.0';
/**
* Parse\Client::initialize, must be called before using Parse features.
*
* @param string $app_id Parse Application ID
* @param string $rest_key Parse REST API Key
* @param string $master_key Parse Master Key
*
* @return null
*/
public static function initialize($app_id, $rest_key, $master_key)
{
ParseUser::registerSubclass();
ParseRole::registerSubclass();
ParseInstallation::registerSubclass();
self::$applicationId = $app_id;
self::$restKey = $rest_key;
self::$masterKey = $master_key;
if (!static::$storage) {
if (session_status() === PHP_SESSION_ACTIVE) {
self::setStorage(new ParseSessionStorage());
} else {
self::setStorage(new ParseMemoryStorage());
}
}
}
/**
* ParseClient::_encode, internal method for encoding object values.
*
* @param mixed $value Value to encode
* @param bool $allowParseObjects Allow nested objects
*
* @return mixed Encoded results.
*
* @throws \Exception
* @ignore
*/
public static function _encode($value, $allowParseObjects)
{
if ($value instanceof \DateTime) {
return array(
'__type' => 'Date', 'iso' => self::getProperDateFormat($value)
);
}
if ($value instanceof \stdClass) {
return $value;
}
if ($value instanceof ParseObject) {
if (!$allowParseObjects) {
throw new \Exception('ParseObjects not allowed here.');
}
return $value->_toPointer();
}
if ($value instanceof Encodable) {
return $value->_encode();
}
if (is_array($value)) {
return self::_encodeArray($value, $allowParseObjects);
}
if (!is_scalar($value) && $value !== null) {
throw new \Exception('Invalid type encountered.');
}
return $value;
}
/**
* ParseClient::_decode, internal method for decoding server responses.
*
* @param mixed $data The value to decode
*
* @return mixed
* @ignore
*/
public static function _decode($data)
{
// The json decoded response from Parse will make JSONObjects into stdClass
// objects. We'll change it to an associative array here.
if ($data instanceof \stdClass) {
$tmp = (array)$data;
if (!empty($tmp)) {
return self::_decode(get_object_vars($data));
}
}
if (!$data && !is_array($data)) {
return null;
}
if (is_array($data)) {
$typeString = (isset($data['__type']) ? $data['__type'] : null);
if ($typeString === 'Date') {
return new \DateTime($data['iso']);
}
if ($typeString === 'Bytes') {
return base64_decode($data['base64']);
}
if ($typeString === 'Pointer') {
return ParseObject::create($data['className'], $data['objectId']);
}
if ($typeString === 'File') {
return ParseFile::_createFromServer($data['name'], $data['url']);
}
if ($typeString === 'GeoPoint') {
return new ParseGeoPoint($data['latitude'], $data['longitude']);
}
if ($typeString === 'Object') {
$output = ParseObject::create($data['className']);
$output->_mergeAfterFetch($data);
return $output;
}
if ($typeString === 'Relation') {
return $data;
}
$newDict = array();
foreach ($data as $key => $value) {
$newDict[$key] = static::_decode($value);
}
return $newDict;
}
return $data;
}
/**
* ParseClient::_encodeArray, internal method for encoding arrays.
*
* @param array $value Array to encode.
* @param bool $allowParseObjects Allow nested objects.
*
* @return array Encoded results.
* @ignore
*/
public static function _encodeArray($value, $allowParseObjects)
{
$output = array();
foreach ($value as $key => $item) {
$output[$key] = self::_encode($item, $allowParseObjects);
}
return $output;
}
/**
* Parse\Client::_request, internal method for communicating with Parse.
*
* @param string $method HTTP Method for this request.
* @param string $relativeUrl REST API Path.
* @param null $sessionToken Session Token.
* @param null $data Data to provide with the request.
* @param bool $useMasterKey Whether to use the Master Key.
*
* @return mixed Result from Parse API Call.
* @throws \Exception
* @ignore
*/
public static function _request($method, $relativeUrl, $sessionToken = null,
$data = null, $useMasterKey = false)
{
if ($data === '[]') {
$data = '{}';
}
self::assertParseInitialized();
$headers = self::_getRequestHeaders($sessionToken, $useMasterKey);
$url = self::HOST_NAME . $relativeUrl;
if ($method === 'GET' && !empty($data)) {
$url .= '?' . http_build_query($data);
}
$rest = curl_init();
curl_setopt($rest, CURLOPT_URL, $url);
curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
if ($method === 'POST') {
$headers[] = 'Content-Type: application/json';
curl_setopt($rest, CURLOPT_POST, 1);
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
}
if ($method === 'PUT') {
$headers[] = 'Content-Type: application/json';
curl_setopt($rest, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
}
if ($method === 'DELETE') {
curl_setopt($rest, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($rest, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($rest);
$status = curl_getinfo($rest, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
if (curl_errno($rest)) {
throw new ParseException(curl_error($rest), curl_errno($rest));
}
curl_close($rest);
if (strpos($contentType, 'text/html') !== false) {
throw new ParseException('Bad Request', -1);
}
$decoded = json_decode($response, true);
if (isset($decoded['error'])) {
throw new ParseException($decoded['error'],
isset($decoded['code']) ? $decoded['code'] : 0
);
}
return $decoded;
}
/**
* ParseClient::setStorage, will update the storage object used for
* persistence.
*
* @param ParseStorageInterface $storageObject
*
* @return null
*/
public static function setStorage(ParseStorageInterface $storageObject)
{
self::$storage = $storageObject;
}
/**
* ParseClient::getStorage, will return the storage object used for
* persistence.
* @return ParseStorageInterface
*/
public static function getStorage()
{
return self::$storage;
}
/**
* ParseClient::_unsetStorage, will null the storage object.
*
* Without some ability to clear the storage objects, all test cases would
* use the first assigned storage object.
*
* @return null
* @ignore
*/
public static function _unsetStorage()
{
self::$storage = null;
}
private static function assertParseInitialized()
{
if (self::$applicationId === null) {
throw new \Exception(
'You must call Parse::initialize() before making any requests.'
);
}
}
/**
* @param $sessionToken
* @param $useMasterKey
*
* @return array
* @ignore
*/
public static function _getRequestHeaders($sessionToken, $useMasterKey)
{
$headers = array('X-Parse-Application-Id: ' . self::$applicationId,
'X-Parse-Client-Version: ' . self::VERSION_STRING);
if ($sessionToken) {
$headers[] = 'X-Parse-Session-Token: ' . $sessionToken;
}
if ($useMasterKey) {
$headers[] = 'X-Parse-Master-Key: ' . self::$masterKey;
} else {
$headers[] = 'X-Parse-REST-API-Key: ' . self::$restKey;
}
/**
* Set an empty Expect header to stop the 100-continue behavior for post
* data greater than 1024 bytes.
* http://pilif.github.io/2007/02/the-return-of-except-100-continue/
*/
$headers[] = 'Expect: ';
return $headers;
}
/**
* Get a date value in the format stored on Parse.
*
* All the SDKs do some slightly different date handling.
* PHP provides 6 digits for the microseconds (u) so we have to chop 3 off.
*
* @param \DateTime $value DateTime value to format.
*
* @return string
*/
public static function getProperDateFormat($value)
{
$dateFormatString = 'Y-m-d\TH:i:s.u';
$date = date_format($value, $dateFormatString);
$date = substr($date, 0, -3) . 'Z';
return $date;
}
/**
* Get a date value in the format to use in Local Push Scheduling on Parse.
*
* All the SDKs do some slightly different date handling.
* Format from Parse doc: an ISO 8601 date without a time zone, i.e. 2014-10-16T12:00:00 .
*
* @param \DateTime $value DateTime value to format.
*
* @return string
*/
public static function getLocalPushDateFormat($value)
{
$dateFormatString = 'Y-m-d\TH:i:s';
$date = date_format($value, $dateFormatString);
return $date;
}
}