]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitteradminpanel.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.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
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             'twitterimport' => array('enabled')
97         );
98
99         $values = array();
100
101         foreach ($settings as $section => $parts) {
102             foreach ($parts as $setting) {
103                 $values[$section][$setting]
104                     = $this->trimmed($setting);
105             }
106         }
107
108         foreach ($booleans as $section => $parts) {
109             foreach ($parts as $setting) {
110                 $values[$section][$setting]
111                     = ($this->boolean($setting)) ? 1 : 0;
112             }
113         }
114
115         // This throws an exception on validation errors
116
117         $this->validate($values);
118
119         // assert(all values are valid);
120
121         $config = new Config();
122
123         $config->query('BEGIN');
124
125         foreach ($settings as $section => $parts) {
126             foreach ($parts as $setting) {
127                 Config::save($section, $setting, $values[$section][$setting]);
128             }
129         }
130
131         foreach ($booleans as $section => $parts) {
132             foreach ($parts as $setting) {
133                 Config::save($section, $setting, $values[$section][$setting]);
134             }
135         }
136
137         $config->query('COMMIT');
138
139         return;
140     }
141
142     function validate(&$values)
143     {
144         // Validate consumer key and secret (can't be too long)
145
146         if (mb_strlen($values['twitter']['consumer_key']) > 255) {
147             $this->clientError(
148                 _m("Invalid consumer key. Max length is 255 characters.")
149             );
150         }
151
152         if (mb_strlen($values['twitter']['consumer_secret']) > 255) {
153             $this->clientError(
154                 _m("Invalid consumer secret. Max length is 255 characters.")
155             );
156         }
157     }
158 }
159
160 class TwitterAdminPanelForm extends AdminForm
161 {
162     /**
163      * ID of the form
164      *
165      * @return int ID of the form
166      */
167
168     function id()
169     {
170         return 'twitteradminpanel';
171     }
172
173     /**
174      * class of the form
175      *
176      * @return string class of the form
177      */
178
179     function formClass()
180     {
181         return 'form_settings';
182     }
183
184     /**
185      * Action of the form
186      *
187      * @return string URL of the action
188      */
189
190     function action()
191     {
192         return common_local_url('twitteradminpanel');
193     }
194
195     /**
196      * Data elements of the form
197      *
198      * @return void
199      */
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', _('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         $this->li();
267         $this->out->checkbox(
268             'enabled', _m('Enable Twitter import'),
269             (bool) $this->value('enabled', 'twitterimport'),
270             _m('Allow users to import their Twitter friends\' timelines')
271         );
272         $this->unli();
273
274         $this->out->elementEnd('ul');
275
276         $this->out->elementEnd('fieldset');
277     }
278
279     /**
280      * Action elements
281      *
282      * @return void
283      */
284
285     function formActions()
286     {
287         $this->out->submit('submit', _('Save'), 'submit', null, _('Save Twitter settings'));
288     }
289 }