-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathParseClient.php
More file actions
executable file
·813 lines (702 loc) · 22.4 KB
/
ParseClient.php
File metadata and controls
executable file
·813 lines (702 loc) · 22.4 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<?php
/**
* Class ParseClient | Parse/ParseClient.php
*/
namespace Parse;
use Exception;
use Parse\HttpClients\ParseCurlHttpClient;
use Parse\HttpClients\ParseHttpable;
use Parse\HttpClients\ParseStreamHttpClient;
use Parse\Internal\Encodable;
/**
* Class ParseClient - Main class for Parse initialization and communication.
*
* @author Fosco Marotto <[email protected]>
* @package Parse
*/
final class ParseClient
{
/**
* The remote Parse Server to communicate with
*
* @var string
*/
private static $serverURL = null;
/**
* The mount path for the current parse server
*
* @var string
*/
private static $mountPath = null;
/**
* The application id.
*
* @var string
*/
private static $applicationId;
/**
* The REST API Key.
*
* @var string|null
*/
private static $restKey;
/**
* The Master Key.
*
* @var string
*/
private static $masterKey;
/**
* Enable/Disable curl exceptions.
*
* @var bool
*/
private static $enableCurlExceptions;
/**
* The object for managing persistence.
*
* @var ParseStorageInterface
*/
private static $storage;
/**
* Are revocable sessions enabled?
*
* @var bool
*/
private static $forceRevocableSession = false;
/**
* Number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
*
* @var int
*/
private static $connectionTimeout;
/**
* Maximum number of seconds of request/response operation.
*
* @var int
*/
private static $timeout;
/**
* Http client for requests
*
* @var ParseHttpable
*/
private static $httpClient;
/**
* CA file holding one or more certificates to verify a peer
*
* @var string
*/
private static $caFile;
/**
* Options to pass to the http client.
*
* @var array
*/
private static $httpOptions;
/**
* Constant for version string to include with requests.
*
* @var string
*/
const VERSION_STRING = '2.4.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
* @param bool $enableCurlExceptions Enable or disable Parse curl exceptions
* @param string $account_key An account key from Parse.com can enable creating apps via API.
*
* @throws Exception
*/
public static function initialize(
$app_id,
$rest_key,
$master_key,
$enableCurlExceptions = true
) {
if (!ParseObject::hasRegisteredSubclass('_User')) {
ParseUser::registerSubclass();
}
if (!ParseObject::hasRegisteredSubclass('_Role')) {
ParseRole::registerSubclass();
}
if (!ParseObject::hasRegisteredSubclass('_Installation')) {
ParseInstallation::registerSubclass();
}
if (!ParseObject::hasRegisteredSubclass('_PushStatus')) {
ParsePushStatus::registerSubclass();
}
if (!ParseObject::hasRegisteredSubclass('_Audience')) {
ParseAudience::registerSubclass();
}
ParseSession::registerSubclass();
self::$applicationId = $app_id;
self::$restKey = $rest_key;
self::$masterKey = $master_key;
self::$enableCurlExceptions = $enableCurlExceptions;
if (!static::$storage) {
if (session_status() === PHP_SESSION_ACTIVE) {
self::setStorage(new ParseSessionStorage());
} else {
self::setStorage(new ParseMemoryStorage());
}
}
}
/**
* ParseClient::setServerURL, to change the Parse Server address & mount path for this app
* @param string $serverURL The remote server url
* @param string $mountPath The mount path for this server
*
* @throws \Exception
*
*/
public static function setServerURL($serverURL, $mountPath)
{
if (!$serverURL) {
throw new Exception('Invalid Server URL.');
}
if (!$mountPath) {
throw new Exception('Invalid Mount Path.');
}
self::$serverURL = rtrim($serverURL, '/');
self::$mountPath = trim($mountPath, '/') . '/';
// check if mount path is root
if (self::$mountPath == "/") {
// root path should have no mount path
self::$mountPath = "";
}
}
/**
* Clears the existing server url.
* Used primarily for testing purposes.
*/
public static function _clearServerURL()
{
self::$serverURL = null;
}
/**
* Clears the existing mount path.
* Used primarily for testing purposes.
*/
public static function _clearMountPath()
{
self::$mountPath = null;
}
/**
* Sets the Http client to use for requests
*
* @param ParseHttpable $httpClient Http client to use
*/
public static function setHttpClient(ParseHttpable $httpClient)
{
self::$httpClient = $httpClient;
}
/**
* Returns an array of information regarding the current server's health
*
* @return array
*/
public static function getServerHealth()
{
self::assertServerInitialized();
// get our prepared http client
$httpClient = self::getPreparedHttpClient();
// try to get a response from the server
$url = self::createRequestUrl('health');
$response = $httpClient->send($url);
$errorCode = $httpClient->getErrorCode();
if ($errorCode) {
return [
'status' => $httpClient->getResponseStatusCode(),
'error' => $errorCode,
'error_message' => $httpClient->getErrorMessage()
];
}
$status = [
'status' => $httpClient->getResponseStatusCode(),
];
// attempt to decode this response
$decoded = json_decode($response, true);
if (isset($decoded)) {
// add decoded response
$status['response'] = $decoded;
} else {
if ($response === 'OK') {
// implied status: ok!
$status['response'] = [
'status' => 'ok'
];
} else {
// add plain response
$status['response'] = $response;
}
}
return $status;
}
/**
* Gets the current Http client, or creates one to suite the need
*
* @return ParseHttpable
*/
public static function getHttpClient()
{
if (static::$httpClient) {
// use existing client
return static::$httpClient;
} else {
// default to cURL/stream
return function_exists('curl_init') ? new ParseCurlHttpClient() : new ParseStreamHttpClient();
}
}
/**
* Clears the currently set http client
*/
public static function clearHttpClient()
{
self::$httpClient = null;
}
/**
* Sets a CA file to validate peers of our requests with
*
* @param string $caFile CA file to set
*/
public static function setCAFile($caFile)
{
self::$caFile = $caFile;
}
/**
* Sets http options to pass to the http client
* For curl
* https://www.php.net/manual/en/function.curl-setopt.php
*
* For stream context
* https://www.php.net/manual/en/context.php
*
* @param array $httpOptions options to set
*/
public static function setHttpOptions($httpOptions)
{
self::$httpOptions = $httpOptions;
}
/**
* ParseClient::_encode, internal method for encoding object values.
*
* @param mixed $value Value to encode
* @param bool $allowParseObjects Allow nested objects
*
* @throws \Exception
*
* @return mixed Encoded results.
*/
public static function _encode($value, $allowParseObjects)
{
if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
return [
'__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
*/
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 (!isset($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 === 'Polygon') {
return new ParsePolygon($data['coordinates']);
}
if ($typeString === 'Object') {
$output = ParseObject::create($data['className']);
$output->_mergeAfterFetch($data);
return $output;
}
if ($typeString === 'Relation') {
return $data;
}
$newDict = [];
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.
*/
public static function _encodeArray($value, $allowParseObjects)
{
$output = [];
foreach ($value as $key => $item) {
$output[$key] = self::_encode($item, $allowParseObjects);
}
return $output;
}
/**
* Returns an httpClient prepared for use
*
* @return ParseHttpable
*/
private static function getPreparedHttpClient()
{
// get our http client
$httpClient = self::getHttpClient();
// setup
$httpClient->setup();
if (isset(self::$caFile)) {
// set CA file
$httpClient->setCAFile(self::$caFile);
}
if (isset(self::$httpOptions)) {
$httpClient->setHttpOptions(self::$httpOptions);
}
return $httpClient;
}
/**
* Creates an absolute request url from a relative one
*
* @param string $relativeUrl Relative url to create full request url from
* @return string
*/
private static function createRequestUrl($relativeUrl)
{
return self::$serverURL . '/' . self::$mountPath.ltrim($relativeUrl, '/');
}
/**
* 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.
* @param string $contentType The content type for this request, default is application/json
* @param bool $returnHeaders Allow to return response headers
*
* @throws \Exception
*
* @return mixed Result from Parse API Call.
*/
public static function _request(
$method,
$relativeUrl,
$sessionToken = null,
$data = null,
$useMasterKey = false,
$contentType = 'application/json',
$returnHeaders = false
) {
if ($data === '[]') {
$data = '{}';
}
// get our prepared http client
$httpClient = self::getPreparedHttpClient();
// verify the server url and mount path have been set
self::assertServerInitialized();
self::assertParseInitialized();
// add appId & client version
$httpClient->addRequestHeader('X-Parse-Application-Id', self::$applicationId);
$httpClient->addRequestHeader('X-Parse-Client-Version', 'php' . self::VERSION_STRING);
if ($sessionToken) {
// add our current session token
$httpClient->addRequestHeader('X-Parse-Session-Token', $sessionToken);
}
if ($useMasterKey) {
// pass master key
$httpClient->addRequestHeader('X-Parse-Master-Key', self::$masterKey);
} elseif (isset(self::$restKey)) {
// pass REST key
$httpClient->addRequestHeader('X-Parse-REST-API-Key', self::$restKey);
}
if (self::$forceRevocableSession) {
// indicate we are using revocable sessions
$httpClient->addRequestHeader('X-Parse-Revocable-Session', '1');
}
/*
* 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/
*/
$httpClient->addRequestHeader('Expect', '');
// create request url
$url = self::createRequestUrl($relativeUrl);
if ($method === 'POST' || $method === 'PUT') {
// add content type to the request
$httpClient->addRequestHeader('Content-type', $contentType);
}
if (!is_null(self::$connectionTimeout)) {
// set connection timeout
$httpClient->setConnectionTimeout(self::$connectionTimeout);
}
if (!is_null(self::$timeout)) {
// set request/response timeout
$httpClient->setTimeout(self::$timeout);
}
// send our request
$response = $httpClient->send($url, $method, $data);
// check content type of our response
$contentType = $httpClient->getResponseContentType() || '';
if (strpos($contentType, 'text/html') !== false) {
throw new ParseException('Bad Request', -1);
}
if ($httpClient->getErrorCode()) {
if (self::$enableCurlExceptions) {
throw new ParseException($httpClient->getErrorMessage(), $httpClient->getErrorCode());
} else {
return false;
}
}
$decoded = json_decode($response, true);
if (!isset($decoded) && $response !== '') {
throw new ParseException(
'Bad Request. Could not decode Response: '.
'('.json_last_error().') '.self::getLastJSONErrorMsg(),
-1
);
}
if (isset($decoded['error'])) {
// check to convert error to a string, if an array
// used to handle an Array 'error' from back4app.com
$errorMessage = is_array($decoded['error']) ? json_encode($decoded['error']) : $decoded['error'];
throw new ParseException(
$errorMessage,
isset($decoded['code']) ? $decoded['code'] : 0
);
}
if ($returnHeaders) {
$decoded['_headers'] = $httpClient->getResponseHeaders();
}
return $decoded;
}
/**
* Returns the last error message from a failed json_decode call
*
* @return string
*/
private static function getLastJSONErrorMsg()
{
// check if json_last_error_msg is defined (>= 5.5.0)
if (!function_exists('json_last_error_msg')) {
// return custom error messages for each code
$error_strings = array(
JSON_ERROR_NONE => 'No error',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
JSON_ERROR_CTRL_CHAR => 'Control character error, potentially incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, potentially incorrectly encoded'
);
$error = json_last_error();
return isset($error_strings[$error]) ? $error_strings[$error] : 'Unknown error';
}
// use existing function
return json_last_error_msg();
}
/**
* ParseClient::setStorage, will update the storage object used for
* persistence.
*
* @param ParseStorageInterface $storageObject
*/
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.
*/
public static function _unsetStorage()
{
self::$storage = null;
}
/**
* Asserts that the server and mount path have been initialized
*
* @throws Exception
*/
private static function assertServerInitialized()
{
if (self::$serverURL === null) {
throw new Exception(
'Missing a valid server url. '.
'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '.
' before making any requests.'
);
}
if (self::$mountPath === null) {
throw new Exception(
'Missing a valid mount path. '.
'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '.
' before making any requests.'
);
}
}
/**
* Asserts that the sdk has been initialized with a valid application id
*
* @throws Exception
*/
private static function assertParseInitialized()
{
if (self::$applicationId === null) {
throw new Exception(
'You must call ParseClient::initialize() before making any requests.',
109
);
}
}
/**
* Get remote Parse API url.
*
* @return string
*/
public static function getAPIUrl()
{
return self::$serverURL.'/'.self::$mountPath;
}
/**
* Get remote Parse API mount path
*
* @return string
*/
public static function getMountPath()
{
return self::$mountPath;
}
/**
* 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.
* @param bool $local Whether to return the local push time
*
* @return string
*/
public static function getPushDateFormat($value, $local = false)
{
$dateFormatString = 'Y-m-d\TH:i:s';
if (!$local) {
$dateFormatString .= '\Z';
}
$date = date_format($value, $dateFormatString);
return $date;
}
/**
* Allows an existing application to start using revocable sessions, without forcing
* all requests for the app to use them. After calling this method, login & signup requests
* will be returned a unique and revocable session token.
*/
public static function enableRevocableSessions()
{
self::$forceRevocableSession = true;
}
/**
* Sets number of seconds to wait while trying to connect. Use 0 to wait indefinitely, null to default behaviour.
*
* @param int|null $connectionTimeout
*/
public static function setConnectionTimeout($connectionTimeout)
{
self::$connectionTimeout = $connectionTimeout;
}
/**
* Sets maximum number of seconds of request/response operation.
* Use 0 to wait indefinitely, null to default behaviour.
*
* @param int|null $timeout
*/
public static function setTimeout($timeout)
{
self::$timeout = $timeout;
}
}