@@ -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 ();
0 commit comments