]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitteradminpanel.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[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 class TwitteradminpanelAction extends AdminPanelAction
44 {
45     /**
46      * Returns the page title
47      *
48      * @return string page title
49      */
50     function title()
51     {
52         return _m('Twitter');
53     }
54
55     /**
56      * Instructions for using this form.
57      *
58      * @return string instructions
59      */
60     function getInstructions()
61     {
62         return _m('Twitter bridge settings');
63     }
64
65     /**
66      * Show the Twitter admin panel form
67      *
68      * @return void
69      */
70     function showForm()
71     {
72         $form = new TwitterAdminPanelForm($this);
73         $form->show();
74         return;
75     }
76
77     /**
78      * Save settings from the form
79      *
80      * @return void
81      */
82     function saveSettings()
83     {
84         static $settings = array(
85             'twitter'     => array('consumer_key', 'consumer_secret'),
86             'integration' => array('source')
87         );
88
89         static $booleans = array(
90             'twitter'       => array('signin')
91         );
92         if (Event::handle('TwitterBridgeAdminImportControl')) {
93             $booleans['twitterimport'] = array('enabled');
94         }
95
96         $values = array();
97
98         foreach ($settings as $section => $parts) {
99             foreach ($parts as $setting) {
100                 $values[$section][$setting]
101                     = $this->trimmed($setting);
102             }
103         }
104
105         foreach ($booleans as $section => $parts) {
106             foreach ($parts as $setting) {
107                 $values[$section][$setting]
108                     = ($this->boolean($setting)) ? 1 : 0;
109             }
110         }
111
112         // This throws an exception on validation errors
113
114         $this->validate($values);
115
116         // assert(all values are valid);
117
118         $config = new Config();
119
120         $config->query('BEGIN');
121
122         foreach ($settings as $section => $parts) {
123             foreach ($parts as $setting) {
124                 Config::save($section, $setting, $values[$section][$setting]);
125             }
126         }
127
128         foreach ($booleans as $section => $parts) {
129             foreach ($parts as $setting) {
130                 Config::save($section, $setting, $values[$section][$setting]);
131             }
132         }
133
134         $config->query('COMMIT');
135
136         return;
137     }
138
139     function validate(&$values)
140     {
141         // Validate consumer key and secret (can't be too long)
142
143         if (mb_strlen($values['twitter']['consumer_key']) > 255) {
144             $this->clientError(
145                 _m("Invalid consumer key. Max length is 255 characters.")
146             );
147         }
148
149         if (mb_strlen($values['twitter']['consumer_secret']) > 255) {
150             $this->clientError(
151                 _m("Invalid consumer secret. Max length is 255 characters.")
152             );
153         }
154     }
155
156     function isImportEnabled()
157     {
158         // Since daemon setup isn't automated yet...
159         // @todo: if merged into main queues, detect presence of daemon config
160         return true;
161     }
162 }
163
164 class TwitterAdminPanelForm extends AdminForm
165 {
166     /**
167      * ID of the form
168      *
169      * @return int ID of the form
170      */
171     function id()
172     {
173         return 'twitteradminpanel';
174     }
175
176     /**
177      * class of the form
178      *
179      * @return string class of the form
180      */
181     function formClass()
182     {
183         return 'form_settings';
184     }
185
186     /**
187      * Action of the form
188      *
189      * @return string URL of the action
190      */
191     function action()
192     {
193         return common_local_url('twitteradminpanel');
194     }
195
196     /**
197      * Data elements of the form
198      *
199      * @return void
200      */
201     function formData()
202     {
203         $this->out->elementStart(
204             'fieldset',
205             array('id' => 'settings_twitter-application')
206         );
207         $this->out->element('legend', null, _m('Twitter application settings'));
208         $this->out->elementStart('ul', 'form_data');
209
210         $this->li();
211         $this->input(
212             'consumer_key',
213             _m('Consumer key'),
214             _m('Consumer key assigned by Twitter'),
215             'twitter'
216         );
217         $this->unli();
218
219         $this->li();
220         $this->input(
221             'consumer_secret',
222              _m('Consumer secret'),
223             _m('Consumer secret assigned by Twitter'),
224             'twitter'
225         );
226         $this->unli();
227
228         $globalConsumerKey = common_config('twitter', 'global_consumer_key');
229         $globalConsumerSec = common_config('twitter', 'global_consumer_secret');
230
231         if (!empty($globalConsumerKey) && !empty($globalConsumerSec)) {
232             $this->li();
233             $this->out->element('p', 'form_guide', _m('Note: a global consumer key and secret are set.'));
234             $this->unli();
235         }
236
237         $this->li();
238         $this->input(
239             'source',
240              _m('Integration source'),
241             _m('Name of your Twitter application'),
242             'integration'
243         );
244         $this->unli();
245
246         $this->out->elementEnd('ul');
247         $this->out->elementEnd('fieldset');
248
249         $this->out->elementStart(
250             'fieldset',
251             array('id' => 'settings_twitter-options')
252         );
253         $this->out->element('legend', null, _m('Options'));
254
255         $this->out->elementStart('ul', 'form_data');
256
257         $this->li();
258
259         $this->out->checkbox(
260             'signin', _m('Enable "Sign-in with Twitter"'),
261             (bool) $this->value('signin', 'twitter'),
262             _m('Allow users to login with their Twitter credentials')
263         );
264         $this->unli();
265
266         if (Event::handle('TwitterBridgeAdminImportControl')) {
267             $this->li();
268             $this->out->checkbox(
269                 'enabled', _m('Enable Twitter import'),
270                 (bool) $this->value('enabled', 'twitterimport'),
271                 _m('Allow users to import their Twitter friends\' timelines. Requires daemons to be manually configured.')
272             );
273             $this->unli();
274         }
275
276         $this->out->elementEnd('ul');
277
278         $this->out->elementEnd('fieldset');
279     }
280
281     /**
282      * Action elements
283      *
284      * @return void
285      */
286     function formActions()
287     {
288         $this->out->submit('submit', _m('Save'), 'submit', null, _m('Save Twitter settings'));
289     }
290 }