]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitteradminpanel.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitteradminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Twitter bridge administration panel
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Administer global Twitter bridge settings
36  *
37  * @category Admin
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class TwitteradminpanelAction extends AdminPanelAction
45 {
46     /**
47      * Returns the page title
48      *
49      * @return string page title
50      */
51
52     function title()
53     {
54         return _m('Twitter');
55     }
56
57     /**
58      * Instructions for using this form.
59      *
60      * @return string instructions
61      */
62
63     function getInstructions()
64     {
65         return _m('Twitter bridge settings');
66     }
67
68     /**
69      * Show the Twitter admin panel form
70      *
71      * @return void
72      */
73
74     function showForm()
75     {
76         $form = new TwitterAdminPanelForm($this);
77         $form->show();
78         return;
79     }
80
81     /**
82      * Save settings from the form
83      *
84      * @return void
85      */
86
87     function saveSettings()
88     {
89         static $settings = array(
90             'twitter'     => array('consumer_key', 'consumer_secret'),
91             'integration' => array('source')
92         );
93
94         static $booleans = array(
95             'twitter'       => array('signin')
96         );
97         if (Event::handle('TwitterBridgeAdminImportControl')) {
98             $booleans['twitterimport'] = array('enabled');
99         }
100
101         $values = array();
102
103         foreach ($settings as $section => $parts) {
104             foreach ($parts as $setting) {
105                 $values[$section][$setting]
106                     = $this->trimmed($setting);
107             }
108         }
109
110         foreach ($booleans as $section => $parts) {
111             foreach ($parts as $setting) {
112                 $values[$section][$setting]
113                     = ($this->boolean($setting)) ? 1 : 0;
114             }
115         }
116
117         // This throws an exception on validation errors
118
119         $this->validate($values);
120
121         // assert(all values are valid);
122
123         $config = new Config();
124
125         $config->query('BEGIN');
126
127         foreach ($settings as $section => $parts) {
128             foreach ($parts as $setting) {
129                 Config::save($section, $setting, $values[$section][$setting]);
130             }
131         }
132
133         foreach ($booleans as $section => $parts) {
134             foreach ($parts as $setting) {
135                 Config::save($section, $setting, $values[$section][$setting]);
136             }
137         }
138
139         $config->query('COMMIT');
140
141         return;
142     }
143
144     function validate(&$values)
145     {
146         // Validate consumer key and secret (can't be too long)
147
148         if (mb_strlen($values['twitter']['consumer_key']) > 255) {
149             $this->clientError(
150                 _m("Invalid consumer key. Max length is 255 characters.")
151             );
152         }
153
154         if (mb_strlen($values['twitter']['consumer_secret']) > 255) {
155             $this->clientError(
156                 _m("Invalid consumer secret. Max length is 255 characters.")
157             );
158         }
159     }
160
161     function isImportEnabled()
162     {
163         // Since daemon setup isn't automated yet...
164         // @todo: if merged into main queues, detect presence of daemon config
165         return true;
166     }
167 }
168
169 class TwitterAdminPanelForm extends AdminForm
170 {
171     /**
172      * ID of the form
173      *
174      * @return int ID of the form
175      */
176
177     function id()
178     {
179         return 'twitteradminpanel';
180     }
181
182     /**
183      * class of the form
184      *
185      * @return string class of the form
186      */
187
188     function formClass()
189     {
190         return 'form_settings';
191     }
192
193     /**
194      * Action of the form
195      *
196      * @return string URL of the action
197      */
198
199     function action()
200     {
201         return common_local_url('twitteradminpanel');
202     }
203
204     /**
205      * Data elements of the form
206      *
207      * @return void
208      */
209
210     function formData()
211     {
212         $this->out->elementStart(
213             'fieldset',
214             array('id' => 'settings_twitter-application')
215         );
216         $this->out->element('legend', null, _m('Twitter application settings'));
217         $this->out->elementStart('ul', 'form_data');
218
219         $this->li();
220         $this->input(
221             'consumer_key',
222             _m('Consumer key'),
223             _m('Consumer key assigned by Twitter'),
224             'twitter'
225         );
226         $this->unli();
227
228         $this->li();
229         $this->input(
230             'consumer_secret',
231              _m('Consumer secret'),
232             _m('Consumer secret assigned by Twitter'),
233             'twitter'
234         );
235         $this->unli();
236
237         $globalConsumerKey = common_config('twitter', 'global_consumer_key');
238         $globalConsumerSec = common_config('twitter', 'global_consumer_secret');
239
240         if (!empty($globalConsumerKey) && !empty($globalConsumerSec)) {
241             $this->li();
242             $this->out->element('p', 'form_guide', _('Note: a global consumer key and secret are set.'));
243             $this->unli();
244         }
245
246         $this->li();
247         $this->input(
248             'source',
249              _m('Integration source'),
250             _m('Name of your Twitter application'),
251             'integration'
252         );
253         $this->unli();
254
255         $this->out->elementEnd('ul');
256         $this->out->elementEnd('fieldset');
257
258         $this->out->elementStart(
259             'fieldset',
260             array('id' => 'settings_twitter-options')
261         );
262         $this->out->element('legend', null, _m('Options'));
263
264         $this->out->elementStart('ul', 'form_data');
265
266         $this->li();
267
268         $this->out->checkbox(
269             'signin', _m('Enable "Sign-in with Twitter"'),
270             (bool) $this->value('signin', 'twitter'),
271             _m('Allow users to login with their Twitter credentials')
272         );
273         $this->unli();
274
275         if (Event::handle('TwitterBridgeAdminImportControl')) {
276             $this->li();
277             $this->out->checkbox(
278                 'enabled', _m('Enable Twitter import'),
279                 (bool) $this->value('enabled', 'twitterimport'),
280                 _m('Allow users to import their Twitter friends\' timelines. Requires daemons to be manually configured.')
281             );
282             $this->unli();
283         }
284
285         $this->out->elementEnd('ul');
286
287         $this->out->elementEnd('fieldset');
288     }
289
290     /**
291      * Action elements
292      *
293      * @return void
294      */
295
296     function formActions()
297     {
298         $this->out->submit('submit', _('Save'), 'submit', null, _('Save Twitter settings'));
299     }
300 }