|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/twitter/ |
|
4
|
|
|
* @copyright Copyright (c) Dukt |
|
5
|
|
|
* @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\twitter\controllers; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use craft\web\Controller; |
|
12
|
|
|
use dukt\twitter\Plugin; |
|
13
|
|
|
use League\OAuth1\Client\Credentials\CredentialsException; |
|
14
|
|
|
use yii\web\Response; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* OAuth controller |
|
18
|
|
|
* |
|
19
|
|
|
* @author Dukt <[email protected]> |
|
20
|
|
|
* @since 3.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class OauthController extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
// Public Methods |
|
25
|
|
|
// ========================================================================= |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Connect. |
|
29
|
|
|
* |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function actionConnect(): Response |
|
33
|
|
|
{ |
|
34
|
|
|
// Oauth provider |
|
35
|
|
|
$provider = Plugin::getInstance()->getOauth()->getOauthProvider(); |
|
36
|
|
|
|
|
37
|
|
|
// Obtain temporary credentials |
|
38
|
|
|
$temporaryCredentials = $provider->getTemporaryCredentials(); |
|
39
|
|
|
|
|
40
|
|
|
// Store credentials in the session |
|
41
|
|
|
Craft::$app->getSession()->set('oauth.temporaryCredentials', $temporaryCredentials); |
|
42
|
|
|
|
|
43
|
|
|
// Redirect to login screen |
|
44
|
|
|
$authorizationUrl = $provider->getAuthorizationUrl($temporaryCredentials); |
|
45
|
|
|
|
|
46
|
|
|
return $this->redirect($authorizationUrl); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Callback. |
|
51
|
|
|
* |
|
52
|
|
|
* @return Response |
|
53
|
|
|
*/ |
|
54
|
|
|
public function actionCallback(): Response |
|
55
|
|
|
{ |
|
56
|
|
|
$provider = Plugin::getInstance()->getOauth()->getOauthProvider(); |
|
57
|
|
|
$oauthToken = Craft::$app->getRequest()->getParam('oauth_token'); |
|
58
|
|
|
$oauthVerifier = Craft::$app->getRequest()->getParam('oauth_verifier'); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
// Retrieve the temporary credentials we saved before. |
|
62
|
|
|
$temporaryCredentials = Craft::$app->getSession()->get('oauth.temporaryCredentials'); |
|
63
|
|
|
|
|
64
|
|
|
// Obtain token credentials from the server. |
|
65
|
|
|
$tokenCredentials = $provider->getTokenCredentials($temporaryCredentials, $oauthToken, $oauthVerifier); |
|
66
|
|
|
|
|
67
|
|
|
// Save token |
|
68
|
|
|
Plugin::getInstance()->getOauth()->saveToken($tokenCredentials); |
|
69
|
|
|
|
|
70
|
|
|
// Redirect |
|
71
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('twitter', 'Connected to Twitter.')); |
|
72
|
|
|
} catch (CredentialsException $e) { |
|
73
|
|
|
// Failed to get the token credentials or user details. |
|
74
|
|
|
Craft::error('Couldn’t connect to Twitter: '.$e->getTraceAsString(), __METHOD__); |
|
75
|
|
|
Craft::$app->getSession()->setError($e->getMessage()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->redirect('twitter/settings'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Disconnect |
|
83
|
|
|
* |
|
84
|
|
|
* @return Response |
|
85
|
|
|
*/ |
|
86
|
|
|
public function actionDisconnect(): Response |
|
87
|
|
|
{ |
|
88
|
|
|
if (Plugin::getInstance()->getOauth()->deleteToken()) { |
|
89
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('twitter', 'Disconnected from Twitter.')); |
|
90
|
|
|
} else { |
|
91
|
|
|
Craft::$app->getSession()->setError(Craft::t('twitter', 'Couldn’t disconnect from Twitter')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$referer = Craft::$app->getRequest()->referrer; |
|
95
|
|
|
|
|
96
|
|
|
return $this->redirect($referer); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|