-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDailymotion.php
1286 lines (1204 loc) · 48.2 KB
/
Dailymotion.php
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Provides access to the Dailymotion Graph API.
*
* @author Olivier Poitrey <rs@dailymotion.com>
* @author Nicolas Grevet <nicolas.grevet@dailymotion.com>
* @author Samir Amzani <samir.amzani@dailymotion.com>
*/
class Dailymotion
{
/**
* Current version number of this SDK.
* @var string Version number
*/
const VERSION = '1.7.1';
/**
* An authorization is requested to the end-user by redirecting it to an authorization page hosted
* on Dailymotion. Once authorized, a refresh token is requested by the API client to the token
* server and stored in the end-user's cookie (or other storage technique implemented by subclasses).
* The refresh token is then used to request time limited access token to the token server.
* @var int
*/
const GRANT_TYPE_AUTHORIZATION = 1;
/**
* @deprecated since 2010-11-03
* @see Dailymotion::GRANT_TYPE_AUTHORIZATION
* @var int
*/
const GRANT_TYPE_TOKEN = 1;
/**
* This grant type is a 2 legs authentication: it doesn't allow to act on behalf of another user.
* With this grant type, all API requests will be performed with the user identity of the API key owner.
* @var int
*/
const GRANT_TYPE_CLIENT_CREDENTIALS = 2;
/**
* @deprecated since 2010-12-14
* @see Dailymotion::GRANT_TYPE_CLIENT_CREDENTIALS
* @var int
*/
const GRANT_TYPE_NONE = 2;
/**
* This grant type allows to authenticate an end-user by directly providing its credentials.
* This profile is highly discouraged for web-server workflows. If used, the username and password
* *MUST NOT* be stored by the client.
* @var int
*/
const GRANT_TYPE_PASSWORD = 3;
/**
* Type of display for the OAuth authorization.
* @var string
*/
const DISPLAY_PAGE = 'page';
const DISPLAY_POPUP = 'popup';
const DISPLAY_MOBILE = 'mobile';
/**
* Maximum number of redirections to follow when not in `safe_mode` or `open_basedir`.
* @var int
*/
const CURLOPT_MAXREDIRS = 3;
/**
* Name of the cookie containing the session.
* @var string
*/
const SESSION_COOKIE = 'dms_%s';
/**
* Activate debug output.
* @var boolean
*/
public $debug = false;
/**
* Maximum number of seconds allowed for each HTTP request to complete.
* @var int
*/
public $timeout = 5;
/**
* Maximum number of seconds to wait for connection establishment of HTTP requests.
* @var int
*/
public $connectionTimeout = 2;
/**
* An HTTP proxy to tunnel HTTP requests through (format: `hostname[:port]`).
* @var string
*/
public $proxy = null;
/**
* Username and password for the HTTP proxy (format: `user:password`).
* @var string
*/
public $proxyCredentials = null;
/**
* The Dailymotion API endpoint URL.
* @var string
*/
public $apiEndpointUrl = 'https://api.dailymotion.com';
/**
* The Dailymotion OAuth authorization server endpoint URL.
* @var string
*/
public $oauthAuthorizeEndpointUrl = 'https://www.dailymotion.com/oauth/authorize';
/**
* The Dailymotion OAuth token server endpoind URL.
* @var string
*/
public $oauthTokenEndpointUrl = 'https://api.dailymotion.com/oauth/token';
/**
* Domain name of the cookie used to store the session.
* @var string
*/
public $cookieDomain = null;
/**
* Life time (in seconds) of the cookie used to store the session.
* @var int
*/
public $cookieLifeTime = 31536000; // 1 year
/**
* Type of the current OAuth2 authorization attempt.
* @var int
*/
protected $grantType = null;
/**
* Information about the current OAuth2 authorization attempt.
* @var array
*/
protected $grantInfo = array();
/**
* Session information for the current OAuth2 authorization attempt.
* @var array
*/
protected $session = array();
/**
* Whether to store the session information for the current OAuth2 authorization attempt or not.
* @var boolean
*/
protected $storeSession = true;
/**
* If the current server supports it, we follow redirects.
* @var boolean
*/
protected $followRedirects;
/**
* List of query parameters that get automatically dropped when rebuilding the current URL.
* @var array
*/
protected static $DROP_QUERY_PARAMS = array(
'code',
'scope',
'error',
'error_description',
'error_uri',
'state',
'uid',
'sig',
);
/**
* Change the default grant type.
* To create an API key/secret pair, go to: http://www.dailymotion.com/profile/developer
*
* @param int $type Can be one of `Dailymotion::GRANT_TYPE_AUTHORIZATION`,
* `Dailymotion::GRANT_TYPE_CLIENT_CREDENTIALS` or `Dailymotion::GRANT_TYPE_PASSWORD`.
* @param string $apiKey Client API key.
* @param string $apiSecret Client API secret.
* @param array $scope Permission scope requested.
* See: http://developer.dailymotion.com/documentation#extended-oauth-permissions
* for a list of available permissions. To requested several scope keys, provide several
* scopes in the array.
* @param array $info Information associated to the chosen grant type.
*
* $info keys:
* - redirect_uri: If `$type` is `Dailymotion::GRANT_TYPE_AUTHORIZATION`, this key can be provided.
* If omitted, the current URL will be used. Make sure this value stays the same
* before the user is redirect to the authorization page and after the authorization
* page redirects to this URI (the token server will change this).
* - username: If `$type` is `Dailymotion::GRANT_TYPE_PASSWORD`, end-user credentials are required.
* - password: If `$type` is `Dailymotion::GRANT_TYPE_PASSWORD`, end-user credentials are required.
*
* @throws DailymotionAuthRequiredException If no `$info` values are provided and no valid session is available.
* @throws InvalidArgumentException If grant type is not supported or required grant info is missing.
* @return Dailymotion `$this`
*/
public function setGrantType($type, $apiKey, $apiSecret, array $scope = array(), array $info = array())
{
if ($type === null)
{
$this->grantType = null;
$this->grantInfo = array();
}
else
{
switch ($type)
{
case self::GRANT_TYPE_AUTHORIZATION:
if (!isset($info['redirect_uri']))
{
$info['redirect_uri'] = $this->getCurrentUrl();
}
break;
case self::GRANT_TYPE_CLIENT_CREDENTIALS:
case self::GRANT_TYPE_PASSWORD:
break;
default:
throw new InvalidArgumentException('Invalid grant type: ' . $type);
}
if (empty($apiKey))
{
throw new InvalidArgumentException('Missing API key');
}
elseif (empty($apiSecret))
{
throw new InvalidArgumentException('Missing API secret');
}
$info['scope'] = $scope;
$info['key'] = (string) $apiKey;
$info['secret'] = (string) $apiSecret;
$this->grantType = (int) $type;
$this->grantInfo = $info;
}
return $this;
}
/**
* Get an authorization URL for use with redirects. By default, full page redirect is assumed.
* If you are using a generated URL with a `window.open()` call in Javascript, you can pass in
* `Dailymotion::DISPLAY_POPUP` for `$display`.
*
* @param string $display Can either be `Dailymotion::DISPLAY_PAGE` (default, full page),
* `Dailymotion::DISPLAY_POPUP` or `Dailymotion::DISPLAY_MOBILE`.
* @return string Authorization URL for use with redirects.
*/
public function getAuthorizationUrl($display = self::DISPLAY_PAGE)
{
if ($this->grantType !== self::GRANT_TYPE_AUTHORIZATION)
{
throw new RuntimeException('This method can only be used with `AUTHORIZATION` grant type.');
}
return $this->oauthAuthorizeEndpointUrl . '?' . http_build_query(
array(
'response_type' => 'code',
'client_id' => $this->grantInfo['key'],
'redirect_uri' => $this->grantInfo['redirect_uri'],
'scope' => implode(chr(32), $this->grantInfo['scope']),
'display' => $display,
),
null,
'&'
);
}
/**
* Get the file path with the cURL format.
* PHP 5.5 introduced a CurlFile object that deprecates the old `@filename` syntax.
* See: https://wiki.php.net/rfc/curl-file-upload
*
* @param $filePath Path to the file to upload on the local filesystem.
* @return mixed cURL file path.
*/
protected function getCurlFile($filePath)
{
if (function_exists('curl_file_create'))
{
return curl_file_create($filePath);
}
else
{
return sprintf("@%s", $filePath);
}
}
/**
* Upload a file on Dailymotion's servers and generate an URL to be used with API methods.
* Caution: This does not create a video on Dailymotion, it only uploads a file to Dailymotion's servers for you to
* use as the `url` field of a video object. If your video is already online, you can skip this step and move on to:
* ```
* Dailymotion::post('/me/videos', array(
* 'title' => 'My video title',
* 'channel' => 'My video channel',
* 'tags' => 'My video tags',
* 'url' => 'URL to my video file',
* 'published' => true,
* );
* ```
*
* @param string $filePath path to the file to upload on the local filesystem
* @param string $forceHostname force a specific Dailymotion server (not recommended)
* @param string &$progressUrl If this variable is given, it will include the progress URL in it
* @param string $callbackUrl It will ping a given url once the upload is finished
* @param int $workers Number of concurrent connections used to upload file (can be up to 8)
* Setting to 0 will use multipart/form-data upload method
* @param callable $progressCallback Method called to track the upload progress. See progressCallback method as example
*
* @return string URL of the file on Dailymotion's servers
*
* @throws DailymotionApiException if the API itself returned an error
*/
public function uploadFile($filePath, $forceHostname = null, &$progressUrl = null, $callbackUrl = null, $workers = 0, $progressCallback = null)
{
$result = $this->get('/file/upload');
$progressUrl = $result['progress_url'];
if (!empty($forceHostname))
{
$result['upload_url'] = preg_replace('#://[^/]+/#', "://{$forceHostname}/", $result['upload_url']);
}
// Temporarily remove the timeout for uploads
$timeout = $this->timeout;
$this->timeout = null;
// Upload the file to Dailymotion's servers
if($workers === 0)
{
$result = json_decode(
$this->httpRequest($result['upload_url'], array('file' => $this->getCurlFile($filePath))),
true
);
}
else
{
$upload = new XUpload(
$result['upload_url'],
$filePath,
intval(min(max($workers,1),8)),
$progressCallback,
array('socket' => $this->proxy, 'credentials' => $this->proxyCredentials)
);
$result = $upload->start();
}
$this->timeout = $timeout;
if (isset($result['error']))
{
$message = isset($result['error']['message']) ? $result['error']['message'] : $result['error'];
$code = isset($result['error']['code']) ? $result['error']['code'] : 0;
throw new DailymotionApiException($message, $code);
}
if (!empty($callbackUrl))
{
$this->httpRequest($callbackUrl.'?'.http_build_query($result), null);
}
return $result['url'];
}
/**
* Example of progress callback which print the upload percentage using STDERR flow.
*
* @param int $sent bytes sent
* @param int $size bytes total
*
* @return true
*/
public function progressCallback($sent, $size)
{
$percent = min(($sent * 100) / $size, 100);
fprintf(STDERR, "\r[%s%s] %.2f%% ", str_repeat('*', intval($percent)), str_repeat(' ', 100 - intval($percent)), $percent);
return true;
}
/**
* Alias for `Dailymotion::call("GET {$path}", $args)`.
* @see Dailymotion::call()
*/
public function get($path, $args = array())
{
return $this->call("GET {$path}", $args, false);
}
/**
* Alias for `Dailymotion::call("POST {$path}", $args)`.
* @see Dailymotion::call()
*/
public function post($path, $args = array())
{
return $this->call("POST {$path}", $args);
}
/**
* Alias for `Dailymotion::call("DELETE {$path}", $args)`.
* @see Dailymotion::call()
*/
public function delete($path, $args = array())
{
return $this->call("DELETE {$path}", $args);
}
/**
* Call a remote endpoint on the API.
*
* @param string $resource API endpoint to call.
* @param array $args Associative array of arguments.
* @param bool $forcePost Force use of HTTP POST to perform the request.
*
* @throws DailymotionApiException If the API itself returned an error.
* @throws DailymotionAuthException If we can't authenticate the request.
* @throws DailymotionAuthRequiredException If no authentication info is available.
* @throws DailymotionTransportException If a network error occurs during the request.
*
* @return mixed Endpoint call response
*/
public function call($resource, $args = [], $forcePost = true)
{
$headers = array('Content-Type: application/json');
$url = $this->apiEndpointUrl;
$statusCode = null;
$isGetRequest = !$forcePost && strpos($resource, "GET ") === 0;
if ($isGetRequest)
{
$payload = null;
$url .= substr($resource, 4);
if (!empty($args))
{
foreach ($args as $key => $value) {
if (is_array($value)) {
$args[$key] = implode(",", $value);
}
}
$url .= "?". http_build_query($args);
}
}
else
{
$payload = json_encode([
'call' => $resource,
'args' => $args,
]);
}
try
{
$result = json_decode(
$this->oauthRequest(
$url,
$payload,
$this->getAccessToken(),
$headers,
$statusCode
),
true
);
}
catch (DailymotionAuthException $e)
{
if ($e->error === 'invalid_token')
{
// Retry by forcing the refresh of the access token
$result = json_decode(
$this->oauthRequest(
$url,
$payload,
$this->getAccessToken(true),
$headers,
$statusCode
),
true
);
}
else
{
throw $e;
}
}
if (empty($result))
{
throw new DailymotionApiException('Invalid API server response');
}
if (!$isGetRequest && $statusCode !== 200)
{
throw new DailymotionApiException("Unknown error: {$statusCode}", $statusCode);
}
if (is_array($result) && isset($result['error']))
{
$message = isset($result['error']['message']) ? $result['error']['message'] : null;
$code = isset($result['error']['code']) ? $result['error']['code'] : null;
if ($code === 403)
{
throw new DailymotionAuthRequiredException($message, $code);
}
else
{
throw new DailymotionApiException($message, $code);
}
}
if (!isset($result['result']))
{
if ($isGetRequest)
{
return $result;
}
throw new DailymotionApiException("Invalid API server response: no `result` key found.");
}
return $result['result'];
}
/**
* Remove the right for the current API key to access the current user account.
* @return Dailymotion `$this`
*/
public function logout()
{
$this->call('/logout');
$this->clearSession();
return $this;
}
/**
* Get the current access token. If no access token is available, try to obtain one using the refresh token
* or code (depending on the state of the OAuth transaction). If no access token is available and no refresh
* token or code can be found, an exception is thrown.
*
* @param boolean $forceRefresh Force the refresh of the access token, event if not expired.
*
* @throws DailymotionAuthRequiredException If we can't get an access token, client need to request end-user authorization.
* @throws DailymotionAuthRefusedException If the user refused the authorization.
* @throws DailymotionAuthException If an OAuth error occurred.
*
* @return string Access token or `null` if no grant type defined (un-authen API calls).
*/
public function getAccessToken($forceRefresh = false)
{
if ($this->grantType === null)
{
// No grant type defined, the request won't be authenticated
return null;
}
$session = $this->getSession();
// Check if session is present and if it was created for the same grant type
// i.e: if the grant type to create the session was `AUTHORIZATION` and the current grant type is
// `CLIENT_CREDENTIALS`, we don't want to call the API on behalf of another user.
if (!empty($session) && isset($session['grant_type']) && ((int) $session['grant_type'] === $this->grantType))
{
if (!$forceRefresh && isset($session['access_token']))
{
if (!isset($session['expires']) || (time() < $session['expires']))
{
return $session['access_token'];
}
// else: Token expired
}
// No valid access token found, try to refresh it
if (isset($session['refresh_token']))
{
$grantType = $session['grant_type'];
$session = $this->oauthTokenRequest(array(
'grant_type' => 'refresh_token',
'client_id' => $this->grantInfo['key'],
'client_secret' => $this->grantInfo['secret'],
'scope' => implode(chr(32), $this->grantInfo['scope']),
'refresh_token' => $session['refresh_token'],
));
$session['grant_type'] = $grantType;
$this->setSession($session);
return $session['access_token'];
}
}
try
{
if ($this->grantType === self::GRANT_TYPE_AUTHORIZATION)
{
$code = filter_input(INPUT_GET, 'code');
$error = filter_input(INPUT_GET, 'error');
if (!empty($code))
{
// We've been called back by authorization server
$session = $this->oauthTokenRequest(array(
'grant_type' => 'authorization_code',
'client_id' => $this->grantInfo['key'],
'client_secret' => $this->grantInfo['secret'],
'scope' => implode(chr(32), $this->grantInfo['scope']),
'redirect_uri' => $this->grantInfo['redirect_uri'],
'code' => $code,
));
$session['grant_type'] = $this->grantType;
$this->setSession($session);
return $session['access_token'];
}
elseif (!empty($error))
{
$message = filter_input(INPUT_GET, 'error_description');
if ($error === 'access_denied')
{
$e = new DailymotionAuthRefusedException($message);
}
else
{
$e = new DailymotionAuthException($message);
}
$e->error = $error;
throw $e;
}
else
{
// Ask the client to request end-user authorization
throw new DailymotionAuthRequiredException();
}
}
elseif ($this->grantType === self::GRANT_TYPE_CLIENT_CREDENTIALS)
{
$session = $this->oauthTokenRequest(array(
'grant_type' => 'client_credentials',
'client_id' => $this->grantInfo['key'],
'client_secret' => $this->grantInfo['secret'],
'scope' => implode(chr(32), $this->grantInfo['scope']),
));
$session['grant_type'] = $this->grantType;
$this->setSession($session);
return $session['access_token'];
}
elseif ($this->grantType === self::GRANT_TYPE_PASSWORD)
{
if (!isset($this->grantInfo['username']) || !isset($this->grantInfo['password']))
{
// Ask the client to request end-user credentials
throw new DailymotionAuthRequiredException();
}
$session = $this->oauthTokenRequest(array(
'grant_type' => 'password',
'client_id' => $this->grantInfo['key'],
'client_secret' => $this->grantInfo['secret'],
'scope' => implode(chr(32), $this->grantInfo['scope']),
'username' => $this->grantInfo['username'],
'password' => $this->grantInfo['password'],
));
$session['grant_type'] = $this->grantType;
$this->setSession($session);
return $session['access_token'];
}
}
catch (DailymotionAuthException $e)
{
// clear session on error
$this->clearSession();
throw $e;
}
}
/**
* Set the session and store it if `$this->storeSession` is true.
*
* @param $session array the session to set
* @return Dailymotion `$this`
*/
public function setSession(array $session = array())
{
$this->session = $session;
if ($this->storeSession)
{
$this->storeSession($session);
}
return $this;
}
/**
* Get the session if any.
* @return array Current session or an empty array if none found.
*/
public function getSession()
{
if (empty($this->session))
{
$this->session = $this->readSession();
}
return $this->session;
}
/**
* Clear the currently stored session.
* @return Dailymotion `$this`
*/
public function clearSession()
{
$this->setSession();
return $this;
}
/**
* Read the session from the session store.
* Default storage is cookie, subclass can implement another storage type if needed.
* Information stored in the session are useless without the API secret. Storing these information on the client
* should thus be safe as long as the API secret is kept... secret.
*
* @return array Stored session or an empty array if none found.
*/
protected function readSession()
{
$session = array();
$cookieName = sprintf(self::SESSION_COOKIE, $this->grantInfo['key']);
$cookieValue = filter_input(INPUT_COOKIE, $cookieName);
if (!empty($cookieValue))
{
parse_str(trim($cookieValue, '"'), $session);
}
return $session;
}
/**
* Store the given session to the session store.
* Default storage is cookie, subclass can implement another storage type if needed.
* Information stored in the session are useless without the API secret. Storing these information on the client
* should thus be safe as long as the API secret is kept... secret.
*
* @param array $session Session to store, if nothing is passed, the current session is removed from the session store.
* @return Dailymotion `$this`
*/
protected function storeSession(array $session = array())
{
if (headers_sent())
{
if (php_sapi_name() !== 'cli')
{
error_log('Could not set session in cookie: headers already sent.');
}
return $this;
}
$cookieName = sprintf(self::SESSION_COOKIE, $this->grantInfo['key']);
if (!empty($session))
{
$value = '"' . http_build_query($session, null, '&') . '"';
$expires = time() + $this->cookieLifeTime;
}
else
{
$cookieValue = filter_input(INPUT_COOKIE, $cookieName);
if (empty($cookieValue))
{
// No need to remove an unexisting cookie
return $this;
}
$value = 'deleted';
$expires = time() - 3600;
}
setcookie($cookieName, $value, $expires, '/', $this->cookieDomain);
return $this;
}
/**
* Perform a request to a token server complient with the OAuth 2.0 specification.
*
* @param array $args Arguments to send to the token server.
* @throws DailymotionAuthException If the token server sends an error or invalid response.
* @return array Cconfigured session.
*/
protected function oauthTokenRequest(array $args)
{
$statusCode = null;
$responseHeaders = array();
$result = json_decode(
$response = $this->httpRequest(
$this->oauthTokenEndpointUrl,
$args,
null,
$statusCode,
$responseHeaders,
true
),
true
);
if (empty($result))
{
throw new DailymotionAuthException("Invalid token server response: {$response}");
}
elseif (isset($result['error']))
{
$message = isset($result['error_description']) ? $result['error_description'] : null;
$e = new DailymotionAuthException($message);
$e->error = $result['error'];
throw $e;
}
elseif (isset($result['access_token']))
{
return array(
'access_token' => $result['access_token'],
'expires' => time() + $result['expires_in'],
'refresh_token' => isset($result['refresh_token']) ? $result['refresh_token'] : null,
'scope' => isset($result['scope']) ? explode(chr(32), $result['scope']) : array(),
);
}
else
{
throw new DailymotionAuthException('No access token found in the token server response');
}
}
/**
* Perform an OAuth 2.0 authenticated request.
*
* @param string $url the URL to perform the HTTP request to.
* @param string $payload Encoded method request to POST.
* @param string $accessToken OAuth access token to authenticate the request with.
* @param array $headers List of headers to send with the request (format `array('Header-Name: header value')`).
* @param int &$statusCode Reference variable to store the response status code.
* @param array &$responseHeaders Reference variable to store the response headers.
*
* @throws DailymotionAuthException If an OAuth error occurs.
* @throws DailymotionTransportException If a network error occurs during the request.
*
* @return string Response body.
*/
protected function oauthRequest($url, $payload, $accessToken = null, $headers = array(), &$statusCode = null, &$responseHeaders = array())
{
if ($accessToken !== null)
{
$headers[] = "Authorization: Bearer {$accessToken}";
}
$result = $this->httpRequest(
$url,
$payload,
$headers,
$statusCode,
$responseHeaders
);
switch ($statusCode)
{
case 401: // Invalid or expired token
case 400: // Invalid request
case 403: // Insufficient scope
$error = null;
$message = null;
$match = array();
if (array_key_exists('www-authenticate', $responseHeaders))
{
if (preg_match('/error="(.*?)"(?:, error_description="(.*?)")?/', $responseHeaders['www-authenticate'], $match))
{
$error = $match[1];
$message = $match[2];
}
$e = new DailymotionAuthException($message);
$e->error = $error;
throw $e;
}
}
return $result;
}
/**
* Perform an HTTP request by posting the given payload and returning the result.
* Override this method if you don't want to use cURL.
*
* @param string $url URL to perform the HTTP request to.
* @param mixed $payload Data to POST. If it's an associative array and `$encodePayload` is set to true,
* it will be url-encoded and the `Content-Type` header will automatically be set
* to `multipart/form-data`. If it's a string make sure you set the correct
* `Content-Type` header yourself.
* @param array $headers List of headers to send with the request (format `array('Header-Name: header value')`).
* @param int &$statusCode Reference variable to store the response status code.
* @param array &$responseHeaders Reference variable to store the response headers.
* @param boolean $encodePayload Whether or not to encode the payload if it's an associative array.
*
* @throws DailymotionTransportException If a network error occurs during the request.
* @return string Response body
*/
protected function httpRequest($url, $payload = null, $headers = null, &$statusCode = null, &$responseHeaders = array(), $encodePayload = false)
{
$payload = (is_array($payload) && (true === $encodePayload))
? http_build_query($payload, null, '&')
: $payload;
// Force removal of the Expect: 100-continue header automatically added by cURL
$headers[] = 'Expect:';
// cURL options
$options = array(
CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_PROXY => $this->proxy,
CURLOPT_PROXYUSERPWD => $this->proxyCredentials,
CURLOPT_USERAGENT => sprintf('Dailymotion-PHP/%s (PHP %s; %s)', self::VERSION, PHP_VERSION, php_sapi_name()),
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_NOPROGRESS => !($this->debug && is_array($payload) && array_key_exists('file', $payload)),
);
if(!is_null($payload))
{
$options[CURLOPT_POSTFIELDS] = $payload;
}
// There is no constructor to this class and I don't intend to add one just for this (PHP 4 legacy and all).
if (is_null($this->followRedirects))
{
// We use filter_var() here because depending on PHP's version, these ini_get() may or may not return:
// true/false or even the strings 'on', 'off', 'true' or 'false' or folder paths... better safe than sorry.
$this->followRedirects =
(false === filter_var(ini_get('open_basedir'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))
&& (false === filter_var(ini_get('safe_mode'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
}
// If the current server supports it, we follow redirects
if ($this->followRedirects === true)
{
$options[CURLOPT_FOLLOWLOCATION] = true;
$options[CURLOPT_MAXREDIRS] = self::CURLOPT_MAXREDIRS;
}
$this->debugRequest($url, $payload, $headers);
// Execute the cURL request
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
// CURLE_SSL_CACERT error
if (curl_errno($ch) == 60)
{
error_log('Invalid or no certificate authority found, using bundled information');
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/dm_ca_chain_bundle.crt');
$response = curl_exec($ch);
}
// Invalid empty response
if ($response === false)
{
$e = new DailymotionTransportException(curl_error($ch), curl_errno($ch));
curl_close($ch);
throw $e;
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
list($responseHeadersString, $payload) = explode("\r\n\r\n", $response, 2);
strtok($responseHeadersString, "\r\n"); // skip status code
while(($name = trim(strtok(":"))) && ($value = trim(strtok("\r\n"))))
{
$responseHeaders[strtolower($name)] = (isset($responseHeaders[$name])
? $responseHeaders[$name] . '; '
: null) . $value;
}
$this->debugResponse($url, $payload, $responseHeaders);
return $payload;
}
/**
* Returns the current URL, stripping if of known OAuth parameters that should not persist.
* @return string Current URL.
*/
protected function getCurrentUrl()
{
$filters = array(
'HTTPS' => FILTER_VALIDATE_BOOLEAN,
'HTTP_SSL_HTTPS' => FILTER_VALIDATE_BOOLEAN,
'HTTP_X_FORWARDED_PROTO' => FILTER_SANITIZE_STRING,
'HTTP_HOST' => FILTER_SANITIZE_STRING,
'REQUEST_URI' => FILTER_SANITIZE_STRING,
);
// FastCGI seems to cause strange side-effects with unexpected NULL values when using INPUT_SERVER and
// INPUT_ENV with the filter_input() and filter_input_array() functions, so we're using a workaround there.
// See: http://fr2.php.net/manual/en/function.filter-input.php#77307
if (PHP_SAPI === 'cgi-fcgi')
{
$values = $filters;
array_walk($values, function (&$value, $key) { $value = isset($_SERVER[$key]) ? $_SERVER[$key] : null; });
$values = filter_var_array($values, $filters);
}
else
{
$values = filter_input_array(INPUT_SERVER, $filters);
}
$secure = ($values['HTTPS'] || $values['HTTP_SSL_HTTPS'] || (strtolower($values['HTTP_X_FORWARDED_PROTO']) === 'https'));
$scheme = $secure ? 'https://' : 'http://';
$currentUrl = $scheme . $values['HTTP_HOST'] . $values['REQUEST_URI'];
$parts = parse_url($currentUrl);
// Remove OAuth callback params
$query = null;
if (!empty($parts['query']))