forked from neilcrookes/CakePHP-Twitter-API-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_app_controller.php
174 lines (156 loc) · 5.62 KB
/
twitter_app_controller.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
<?php
/**
* Plugin base controller.
*
*
*
* @author Neil Crookes <neil@neilcrookes.com>
* @link http://www.neilcrookes.com
* @copyright (c) 2010 Neil Crookes
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*/
class TwitterAppController extends AppController {
/**
* Components the controllers use
*
* @var array
*/
public $components = array('TwitterAuth');
/**
* Overrides CakePHP's default paging settings
*
* @var array
*/
public $paginate = array(
'page' => 1,
'limit' => 10,
);
/**
* Called automatically before any action
*
* Sets any action to be allowed in case you've got Auth enabled in your app
*/
public function beforeFilter() {
parent::beforeFilter();
if (isset($this->Auth)) {
$this->Auth->allow('*');
}
}
/**
* Overrides Controller::paginate() to set paging options in the params
* property, which is then available in the view, and is used by the Paginator
* helper.
*
* @param mixed $object
* @param mixed $scope
* @param mixed $whitelist
* @return array The result set
*/
public function paginate($object = null, $scope = array(), $whitelist = array()) {
if (isset($this->passedArgs['page'])) {
$this->paginate['page'] = $this->passedArgs['page'];
}
if (isset($this->passedArgs['show'])) {
$this->paginate['limit'] = $this->passedArgs['show'];
}
$options = $this->paginate;
if (isset($options[$object])) {
$options = array_merge($options, $options[$object]);
unset($options[$object]);
}
$type = 'all';
if (isset($options[0])) {
$type = $options[0];
unset($options[0]);
}
$results = $this->{$this->modelClass}->find($type, $options);
$this->params['paging'][$object] = array(
'page' => $options['page'],
'current' => count($results),
'prevPage' => ($options['page'] > 1),
'nextPage' => true,
'defaults' => array_merge(array('limit' => 20, 'step' => 1), $options),
'options' => $options
);
if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
$this->helpers[] = 'Paginator';
}
return $results;
}
/**
* This action gets an OAuth Request Token and OAuth Request Token Secret from
* Twitter then redirects the user to Twitter to authorize the OAuth Request
* Token.
*
* If for some reason we couldn't get a request token, an error message is set
* in the session flash and the user is redirected to the return to param. If
* that is not set, the error message is dumped out.
*
* @param string $returnTo The url to return to on success or failure. N.B.
* should be urlencode()'d 3 times in HtmlHelper::link().
*/
public function connect($returnTo = null) {
if ($returnTo) {
$this->Session->write('Twitter.Auth.return_to', urldecode($returnTo));
}
$ds = $this->TwitterAuth->getDataSource();
$oAuthConsumerKey = $ds->config['oauth_consumer_key'];
$oAuthConsumerSecret = $ds->config['oauth_consumer_secret'];
$oAuthCallback = Router::url(array('action' => 'callback'), true);
$requestToken = $this->TwitterAuth->getOAuthRequestToken($oAuthConsumerKey, $oAuthConsumerSecret, $oAuthCallback);
if ($requestToken) {
$this->Session->write('Twitter.Auth.oauth_request_token', $requestToken['oauth_token']);
$this->Session->write('Twitter.Auth.oauth_request_token_secret', $requestToken['oauth_token_secret']);
$this->TwitterAuth->authorize($requestToken['oauth_token']);
} else {
$error = __('Could not get OAuth Request Token from Twitter', true);
if ($returnTo) {
$this->Session->setFlash($error);
$this->redirect($returnTo);
}
die($error);
}
}
/**
* This action is the callback url specified in the connect action above that
* Twitter redirects the user back to after they have authorised the request
* token.
*
* This actually exchanges the authorised request token for the OAuth Access
* Token and OAuth Access Token Secret and stores them in the session before
* redirecting the user back to the URL passed in in the returnTo parameter to
* the connect action above, or if that is not set, the details are dumped
* out.
*/
public function callback() {
$ds = $this->TwitterAuth->getDataSource();
$oAuthConsumerKey = $ds->config['oauth_consumer_key'];
$oAuthConsumerSecret = $ds->config['oauth_consumer_secret'];
$oAuthRequestToken = $this->Session->read('Twitter.Auth.oauth_request_token');
$oAuthRequestTokenSecret = $this->Session->read('Twitter.Auth.oauth_request_token_secret');
$oAuthVerifier = $this->params['url']['oauth_verifier'];
$accessToken = $this->TwitterAuth->getOAuthAccessToken($oAuthConsumerKey, $oAuthConsumerSecret, $oAuthRequestToken, $oAuthRequestTokenSecret, $oAuthVerifier);
if ($accessToken) {
$sessionData = $this->Session->read('Twitter.Auth');
$sessionData = array_merge($sessionData, $accessToken);
$this->Session->write('Twitter.Auth', $sessionData);
if ($this->Session->check('Twitter.Auth.return_to')) {
$this->redirect($this->Session->read('Twitter.Auth.return_to'));
} else {
die(pr($this->Session->read('Twitter.Auth')));
}
} else {
$error = __('Could not get OAuth Access Token from Twitter', true);
if ($this->Session->check('Twitter.Auth.return_to')) {
$this->redirect($this->Session->read('Twitter.Auth.return_to'));
} else {
die($error);
}
}
}
public function logout() {
$this->Session->delete('Twitter.Auth');
$this->redirect($this->referer('/', true));
}
}
?>