| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * StatusNet, the distributed open-source microblogging tool | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * List a user's OAuth connected applications | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * PHP version 5 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * LICENCE: This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category  Settings | 
					
						
							|  |  |  |  * @package   StatusNet | 
					
						
							|  |  |  |  * @author    Zach Copley <zach@status.net> | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |  * @copyright 2008-2010 StatusNet, Inc. | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 | 
					
						
							|  |  |  |  * @link      http://status.net/ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-16 18:52:43 +02:00
										 |  |  | if (!defined('GNUSOCIAL')) { exit(1); } | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Show connected OAuth applications | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category Settings | 
					
						
							|  |  |  |  * @package  StatusNet | 
					
						
							|  |  |  |  * @author   Zach Copley <zach@status.net> | 
					
						
							|  |  |  |  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 | 
					
						
							|  |  |  |  * @link     http://status.net/ | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @see      SettingsAction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-01-23 12:35:35 -05:00
										 |  |  | class OauthconnectionssettingsAction extends SettingsAction | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |     var $page = null; | 
					
						
							| 
									
										
										
										
											2010-01-11 14:11:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |     protected $oauth_token = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function doPreparation() | 
					
						
							| 
									
										
										
										
											2010-01-11 14:11:43 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         $this->oauth_token = $this->arg('oauth_token'); | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |         $this->page = $this->int('page') ?: 1; | 
					
						
							| 
									
										
										
										
											2010-01-11 14:11:43 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Title of the page | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string Title of the page | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function title() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |         // TRANS: Title for OAuth connection settings.
 | 
					
						
							| 
									
										
										
										
											2010-01-14 23:36:13 +01:00
										 |  |  |         return _('Connected applications'); | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Instructions for use | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return instructions for use | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function getInstructions() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |         // TRANS: Instructions for OAuth connection settings.
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         return _('The following connections exist for your account.'); | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Content area of the page | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function showContent() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $offset = ($this->page - 1) * APPS_PER_PAGE; | 
					
						
							|  |  |  |         $limit  =  APPS_PER_PAGE + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |         $connection = $this->scoped->getConnectedApps($offset, $limit); | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-03 12:10:21 -08:00
										 |  |  |         $cnt = 0; | 
					
						
							| 
									
										
										
										
											2010-01-11 14:11:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         if (!empty($connection)) { | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |             $cal = new ConnectedAppsList($connection, $this->scoped, $this); | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |             $cnt = $cal->show(); | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-01-11 14:11:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |         if ($cnt == 0) { | 
					
						
							|  |  |  |             $this->showEmptyListMessage(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         $this->pagination( | 
					
						
							|  |  |  |             $this->page > 1, | 
					
						
							|  |  |  |             $cnt > APPS_PER_PAGE, | 
					
						
							|  |  |  |             $this->page, | 
					
						
							|  |  |  |             'connectionssettings', | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |             array('nickname' => $this->scoped->getNickname()) | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handle posts to this form | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Based on the button that was pressed, muxes out to other functions | 
					
						
							|  |  |  |      * to do the actual task requested. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * All sub-functions reload the form with a message -- success or failure. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |     protected function doPost() | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |         if ($this->arg('revoke')) { | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  |             return $this->revokeAccess($this->oauth_token); | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-17 00:20:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // TRANS: Client error when submitting a form with unexpected information.
 | 
					
						
							|  |  |  |         throw new ClientException(_('Unexpected form submission.'), 401); | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-02 07:35:54 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |      * Revoke an access token | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * XXX: Confirm revoke before doing it | 
					
						
							| 
									
										
										
										
											2010-02-02 07:35:54 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param int $appId the ID of the application | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |     function revokeAccess($token) | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $cur = common_current_user(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         $appUser = Oauth_application_user::getByUserAndToken($cur, $token); | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (empty($appUser)) { | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |             // TRANS: Client error when trying to revoke access for an application while not being a user of it.
 | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |             $this->clientError(_('You are not a user of that application.'), 401); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $app = Oauth_application::getKV('id', $appUser->application_id); | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 00:20:36 +02:00
										 |  |  |         $datastore = new ApiGNUsocialOAuthDataStore(); | 
					
						
							| 
									
										
										
										
											2010-02-02 07:35:54 +00:00
										 |  |  |         $datastore->revoke_token($appUser->token, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $appUser->delete(); | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$result) { | 
					
						
							| 
									
										
										
										
											2010-02-02 07:35:54 +00:00
										 |  |  |             common_log_db_error($orig, 'DELETE', __FILE__); | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |             // TRANS: Client error when revoking access has failed for some reason.
 | 
					
						
							|  |  |  |             // TRANS: %s is the application ID revoking access failed for.
 | 
					
						
							|  |  |  |             $this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id)); | 
					
						
							| 
									
										
										
										
											2010-01-13 21:11:08 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d'; | 
					
						
							|  |  |  |         common_log( | 
					
						
							|  |  |  |             LOG_INFO, | 
					
						
							|  |  |  |             sprintf( | 
					
						
							|  |  |  |                 $msg, | 
					
						
							|  |  |  |                 $cur->nickname, | 
					
						
							|  |  |  |                 $cur->id, | 
					
						
							|  |  |  |                 $appUser->token, | 
					
						
							|  |  |  |                 $appUser->application_id | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $msg = sprintf( | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |             // TRANS: Success message after revoking access for an application.
 | 
					
						
							|  |  |  |             // TRANS: %1$s is the application name, %2$s is the first part of the user token.
 | 
					
						
							|  |  |  |             _('You have successfully revoked access for %1$s and the access token starting with %2$s.'), | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |              $app->name, | 
					
						
							|  |  |  |              substr($appUser->token, 0, 7) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->showForm($msg, true); | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function showEmptyListMessage() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |         // TRANS: Empty list message when no applications have been authorised yet.
 | 
					
						
							| 
									
										
										
										
											2010-04-10 00:58:57 +02:00
										 |  |  |         $message = _('You have not authorized any applications to use your account.'); | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->elementStart('div', 'guide'); | 
					
						
							|  |  |  |         $this->raw(common_markup_to_html($message)); | 
					
						
							|  |  |  |         $this->elementEnd('div'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 16:58:49 -08:00
										 |  |  |     function showSections() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         $cur = common_current_user(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 21:24:19 -07:00
										 |  |  |         $this->elementStart('div', array('id' => 'developer-help', 'class' => 'section')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |         $this->element('h2', null, 'Developers'); | 
					
						
							|  |  |  |         $this->elementStart('p'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $devMsg = sprintf( | 
					
						
							| 
									
										
										
										
											2010-10-20 19:34:27 +02:00
										 |  |  |             // TRANS: Note for developers in the OAuth connection settings form.
 | 
					
						
							|  |  |  |             // TRANS: This message contains a Markdown link. Do not separate "](".
 | 
					
						
							|  |  |  |             // TRANS: %s is the URL to the OAuth settings.
 | 
					
						
							| 
									
										
										
										
											2010-10-19 20:54:53 -07:00
										 |  |  |             _('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'), | 
					
						
							|  |  |  |             common_local_url('oauthappssettings') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $output = common_markup_to_html($devMsg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->raw($output); | 
					
						
							|  |  |  |         $this->elementEnd('p'); | 
					
						
							| 
									
										
										
										
											2010-10-19 21:24:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->elementEnd('section'); | 
					
						
							| 
									
										
										
										
											2009-11-16 16:58:49 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-11-12 19:42:18 -08:00
										 |  |  | } |