Skip to content

Commit 00f4dc1

Browse files
committed
Merge branch 'hotfix_twitter_oauth' into develop
2 parents 9409394 + d4d7955 commit 00f4dc1

File tree

8 files changed

+1253
-75
lines changed

8 files changed

+1253
-75
lines changed

images/twitter_login.png

2.43 KB
Loading

protected/application/admin/controllers/PostController.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,17 @@ private function createArray($from, $to) {
938938
}
939939

940940
private function notifyTwitter($item) {
941-
// Get twitter credentials
942-
$username = $this->_properties->getProperty('twitter_username');
943-
$password = $this->_properties->getProperty('twitter_password');
941+
// Get twitter consumer tokens and user secrets
942+
$consumer_key = $this->_config->twitter->consumer_key;
943+
$consumer_secret = $this->_config->twitter->consumer_secret;
944+
$oauth_token = $this->_properties->getProperty('twitter_oauth_token');
945+
$oauth_token_secret = $this->_properties->getProperty('twitter_oauth_token_secret');
946+
947+
if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) {
948+
$this->addErrorMessage("Missing twitter OAuth credentials to continue");
949+
}
950+
951+
// Should we append a text before the tweet ?
944952
$has_preamble = $this->_properties->getProperty('preamble', true);
945953

946954
// Get item
@@ -962,10 +970,13 @@ private function notifyTwitter($item) {
962970
}
963971

964972
try {
965-
$twitter = new Stuffpress_Services_Twitter($username, $password);
966-
$twitter->sendTweet($tweet);
973+
$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
974+
$response = $connection->post('statuses/update', array('status' => $tweet));
975+
if (isset($response->error)) {
976+
$this->addErrorMessage("Failed posting to Twitter with error " . $response->error);
977+
}
967978
} catch (Exception $e) {
968-
//
979+
$this->addErrorMessage("Failed posting to Twitter with unknwon error");
969980
}
970981
}
971982

protected/application/admin/controllers/SnsController.php

+99-52
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,27 @@ class Admin_SnsController extends Admin_BaseController
2121
protected $_section = 'config';
2222

2323
public function indexAction() {
24-
// Get the user properties
25-
$values = $this->_properties->getProperties(array("twitter_auth", "twitter_username", "twitter_services"));
26-
24+
2725
// If not logged in, get the login form
28-
if (!$values['twitter_auth']) {
26+
if (!$this->_properties->getProperty('twitter_auth',false)) {
2927
if (!$this->view->twitter_login_form) {
30-
$this->view->twitter_login_form = $this->getTwitterLoginForm();
28+
$this->view->twitter_login = true;
3129
}
3230
}
3331
// Else get the config form
3432
else {
3533
if (!$this->view->twitter_config_form) {
3634
$form = $this->getTwitterConfigForm();
37-
$form->twitter_services->setValue(unserialize($values['twitter_services']));
35+
$form->twitter_services->setValue($this->_properties->getProperty('twitter_services'));
3836
$this->view->twitter_config_form = $form;
3937
}
38+
39+
$this->view->twitter_username = $this->_properties->getProperty('twitter_username');
4040
}
4141

4242
// Prepare view
4343
$this->common();
44-
$this->view->twitter_auth = $values['twitter_auth'];
45-
$this->view->twitter_user = $values['twitter_username'];
44+
$this->view->twitter_auth = $this->_properties->getProperty('twitter_auth', false);
4645
$this->view->status_messages = $this->getStatusMessages();
4746
$this->view->error_messages = $this->getErrorMessages();
4847
$this->view->headScript()->appendFile('js/controllers/sns.js');
@@ -71,6 +70,98 @@ public function submitAction()
7170
return $this->_helper->json->sendJson(false);
7271
}
7372

73+
public function connectAction() {
74+
75+
if (! isset($this->_config->twitter->consumer_key) && !isset($this->_config->twitter->consumer_secret)) {
76+
$this->addErrorMessage("Missing OAuth consumer key and secret");
77+
$this->_forward('index');
78+
return;
79+
}
80+
81+
$consumer_key = $this->_config->twitter->consumer_key;
82+
$consumer_secret = $this->_config->twitter->consumer_secret;
83+
$oauth_callback = $this->getStaticUrl() . "/admin/sns/callback";
84+
85+
/* Create a new twitter client */
86+
$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret);
87+
88+
/* Get temporary credentials. */
89+
$request_token = $connection->getRequestToken($oauth_callback);
90+
91+
/* Save temporary credentials to session. */
92+
$oauth_token = $request_token['oauth_token'];
93+
$oauth_token_secret = $request_token['oauth_token_secret'];
94+
$this->_properties->setProperty("twitter_oauth_token", $oauth_token);
95+
$this->_properties->setProperty("twitter_oauth_token_secret", $oauth_token_secret);
96+
97+
/* If last connection failed don't display authorization link. */
98+
switch ($connection->http_code) {
99+
case 200:
100+
/* Build authorize URL and redirect user to Twitter. */
101+
$this->_redirect($connection->getAuthorizeURL($oauth_token));
102+
break;
103+
default:
104+
/* Show notification if something went wrong. */
105+
$this->addErrorMessage('Could not connect to Twitter. Refresh the page or try again later.');
106+
}
107+
108+
$this->_forward('index');
109+
}
110+
111+
public function callbackAction() {
112+
/* Get the saved tokens */
113+
$oauth_token = $this->_properties->getProperty('twitter_oauth_token');
114+
$oauth_token_secret = $this->_properties->getProperty('twitter_oauth_token_secret');
115+
116+
if (!isset($oauth_token) && !isset($oauth_token_secret)) {
117+
$this->addErrorMessage("Missing temporary OAuth tokens");
118+
$this->_forward('index');
119+
return;
120+
}
121+
122+
/* Get the consumer key and secret from the config */
123+
if (! isset($this->_config->twitter->consumer_key) && !isset($this->_config->twitter->consumer_secret)) {
124+
$this->addErrorMessage("Missing OAuth consumer key and secret");
125+
$this->_forward('index');
126+
return;
127+
}
128+
129+
$consumer_key = $this->_config->twitter->consumer_key;
130+
$consumer_secret = $this->_config->twitter->consumer_secret;
131+
$oauth_callback = $this->getStaticUrl() . "/admin/sns/callback";
132+
133+
/* If the oauth_token is old redirect to the connect page. */
134+
if (isset($_REQUEST['oauth_token'])) {
135+
if ($oauth_token != $_REQUEST['oauth_token']) {
136+
$this->_properties->deleteProperty("twitter_auth");
137+
die("Session should be cleared");
138+
}
139+
}
140+
141+
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
142+
$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
143+
144+
/* Request access tokens from twitter */
145+
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
146+
147+
/* Save the access tokens. Normally these would be saved in a database for future use. */
148+
$this->_properties->setProperty('twitter_oauth_token', $access_token['oauth_token']);
149+
$this->_properties->setProperty('twitter_oauth_token_secret', $access_token['oauth_token_secret']);
150+
$this->_properties->setProperty('twitter_user_id', $access_token['user_id']);
151+
$this->_properties->setProperty('twitter_username', $access_token['screen_name']);
152+
153+
/* If HTTP response is 200 continue otherwise send to connect page to retry */
154+
if (200 == $connection->http_code) {
155+
/* The user has been verified and the access tokens can be saved for future use */
156+
$this->_properties->setProperty('twitter_auth', true);
157+
} else {
158+
/* Save HTTP status for error dialog on connnect page.*/
159+
die("Error, We should clear the session.");
160+
}
161+
162+
$this->_forward('index');
163+
}
164+
74165
public function loginAction()
75166
{
76167
// Is the form correctly posted ?
@@ -147,51 +238,7 @@ private function getTwitterConfigForm() {
147238

148239
return $form;
149240
}
150-
151-
private function getTwitterLoginForm() {
152-
$form = new Stuffpress_Form();
153-
154-
// Add the form element details
155-
$form->setMethod('post');
156-
$form->setName('formTwitterLogin');
157-
$form->setAction('admin/sns/login');
158241

159-
// Twitter account
160-
$e = $form->createElement('text', 'username', array('size' => 12, 'label' => 'Username', 'decorators' => array('ViewHelper', 'Errors')));
161-
$e->setRequired(true);
162-
$form->addElement($e);
163-
164-
// Twitter account
165-
$e = $form->createElement('password', 'password', array('size' => 12, 'label' => 'Password', 'decorators' => array('ViewHelper', 'Errors')));
166-
$e->setRequired(true);
167-
$form->addElement($e);
168-
169-
// Save button
170-
$form->addElement('submit', 'login', array('label' => 'Sign in', 'decorators' => $form->buttonDecorators));
171-
172-
return $form;
173-
}
174-
175-
private function validateTwitterAccount($username, $password) {
176-
$url = "http://twitter.com/account/verify_credentials.json";
177-
$curl = curl_init();
178-
curl_setopt($curl, CURLOPT_URL,$url);
179-
curl_setopt($curl, CURLOPT_HEADER, false);
180-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
181-
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
182-
curl_setopt($curl, CURLOPT_USERAGENT,'Storytlr/1.0');
183-
184-
$response = curl_exec($curl);
185-
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
186-
curl_close ($curl);
187-
188-
if ($http_code != 200) {
189-
return false;
190-
} else {
191-
return true;
192-
}
193-
}
194-
195242
private function getAvailableSources() {
196243
$sourcesTable = new Sources();
197244
$sources = $sourcesTable->getSources();

protected/application/admin/models/SourceModel.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,22 @@ protected function updateTwitter($items) {
302302
$shortUrl = new ShortUrl();
303303
$user = $users->getUser($this->getUserID());
304304

305+
// Get twitter consumer tokens and user secrets
306+
$config = Zend_Registry::get("configuration");
307+
$consumer_key = $config->twitter->consumer_key;
308+
$consumer_secret = $config->twitter->consumer_secret;
309+
305310
// Get twitter credentials
306311
$properties = new Properties(array(Properties::KEY => $user->id));
307312
$auth = $properties->getProperty('twitter_auth');
308313
$services = $properties->getProperty('twitter_services');
309-
$username = $properties->getProperty('twitter_username');
310-
$password = $properties->getProperty('twitter_password');
314+
$oauth_token = $properties->getProperty('twitter_oauth_token');
315+
$oauth_token_secret = $properties->getProperty('twitter_oauth_token_secret');
316+
317+
if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) {
318+
return;
319+
}
320+
311321
$has_preamble = $properties->getProperty('preamble', true);
312322

313323
// Return if not all conditions are met
@@ -341,8 +351,8 @@ protected function updateTwitter($items) {
341351
}
342352

343353
try {
344-
$twitter = new Stuffpress_Services_Twitter($username, $password);
345-
$twitter->sendTweet($tweet);
354+
$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
355+
$response = $connection->post('statuses/update', array('status' => $tweet));
346356
} catch (Exception $e) {}
347357
}
348358
} else {

protected/application/admin/views/scripts/sns/index.phtml

+2-11
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,10 @@ something directly to Storytlr.
2121
</td>
2222
<td class='element'>
2323
<?php if (!$this->twitter_auth) :?>
24-
You must first login into Twitter.
25-
<div id='twitter_login_form'>
26-
<form id="<?= $this->escape($this->twitter_login_form->getName()) ?>"" action="<?= $this->escape($this->twitter_login_form->getAction()) ?>" method="<?= $this->escape($this->twitter_login_form->getMethod()) ?>">
27-
<table>
28-
<tr><td>Username:</td><td><?= $this->twitter_login_form->username ?></td></tr>
29-
<tr><td>Password:</td><td><?= $this->twitter_login_form->password ?></td></tr>
30-
<tr><td></td><td><?= $this->twitter_login_form->login ?></td></tr>
31-
</table>
32-
</form>
33-
</div>
24+
<a href="admin/sns/connect"><img src="images/twitter_login.png"/></a>
3425
<?php else : ?>
3526
<div id='twitter_account'>
36-
You are logged in as <a href="http://twitter.com/<?= $this->twitter_user ?>"><?= $this->twitter_user ?></a> on Twitter (<a href="admin/sns/logout/service/twitter">logout</a>)
27+
You are logged in as <a href="http://twitter.com/<?= $this->twitter_username ?>"><?= $this->twitter_username ?></a> on Twitter (<a href="admin/sns/logout/service/twitter">logout</a>)
3728
<?php endif ?>
3829
</td>
3930
</tr>

protected/config/config.ini.sample

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ rss.default.hide_content=1
4444
;;flickr.api_key=
4545

4646
;; Twitter settings
47-
;;twitter.username=
48-
;;twitter.password=
47+
;;twitter.consumer_key=
48+
;;twitter.consumer_secret=
4949
;;twitter.default.hide_replies=1
5050

5151
;; Seesmic settings

0 commit comments

Comments
 (0)