1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Craft; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* YouTube Controller. |
7
|
|
|
* |
8
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2015, Itmundi |
10
|
|
|
* @license MIT |
11
|
|
|
* |
12
|
|
|
* @link http://github.com/boboldehampsink |
13
|
|
|
*/ |
14
|
|
|
class YouTubeController extends BaseController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Connect. |
18
|
|
|
*/ |
19
|
|
|
public function actionConnect() |
20
|
|
|
{ |
21
|
|
|
// Get referer |
22
|
|
|
$referer = craft()->httpSession->get('youtube.referer'); |
23
|
|
|
|
24
|
|
|
// If not set, set it |
25
|
|
|
if (!$referer) { |
26
|
|
|
$referer = craft()->request->getUrlReferrer(); |
27
|
|
|
craft()->httpSession->add('youtube.referer', $referer); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Set YouTube OAuth options |
31
|
|
|
$options = array( |
32
|
|
|
'plugin' => 'youtube', |
33
|
|
|
'provider' => 'google', |
34
|
|
|
'scope' => array( |
35
|
|
|
'https://www.googleapis.com/auth/userinfo.profile', |
36
|
|
|
'https://www.googleapis.com/auth/userinfo.email', |
37
|
|
|
'https://www.googleapis.com/auth/youtube', |
38
|
|
|
'https://www.googleapis.com/auth/youtube.upload', |
39
|
|
|
), |
40
|
|
|
'authorizationOptions' => array( |
41
|
|
|
'access_type' => 'offline', |
42
|
|
|
'approval_prompt' => 'force', |
43
|
|
|
), |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
// Connect |
47
|
|
|
if ($response = craft()->oauth->connect($options)) { |
48
|
|
|
if ($response['success']) { |
49
|
|
|
|
50
|
|
|
// Get token |
51
|
|
|
$token = $response['token']; |
52
|
|
|
|
53
|
|
|
// Save token |
54
|
|
|
craft()->youTube_oauth->saveToken($token); |
55
|
|
|
|
56
|
|
|
// Session notice |
57
|
|
|
craft()->userSession->setNotice(Craft::t('Connected.')); |
58
|
|
|
} else { |
59
|
|
|
// Session notice |
60
|
|
|
craft()->userSession->setError(Craft::t($response['errorMsg'])); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
// session error |
64
|
|
|
craft()->userSession->setError(Craft::t('Couldn’t connect')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Redirect |
68
|
|
|
craft()->httpSession->remove('youtube.referer'); |
69
|
|
|
$this->redirect($referer); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Disconnect. |
74
|
|
|
*/ |
75
|
|
|
public function actionDisconnect() |
76
|
|
|
{ |
77
|
|
|
// Delete token |
78
|
|
|
craft()->youTube_oauth->deleteToken(); |
79
|
|
|
|
80
|
|
|
// Set notice |
81
|
|
|
craft()->userSession->setNotice(Craft::t('Disconnected.')); |
82
|
|
|
|
83
|
|
|
// Redirect |
84
|
|
|
$redirect = craft()->request->getUrlReferrer(); |
85
|
|
|
$this->redirect($redirect); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|