]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/BitlyUrl/bitlyadminpanelaction.php
56f78e164888941c62ac0029fa53a4d0b91ee169
[quix0rs-gnu-social.git] / plugins / BitlyUrl / bitlyadminpanelaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Admin panel for plugin to use bit.ly URL shortening services.
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    Brion Vibber <brion@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 bit.ly URL shortener settings
36  *
37  * @category Admin
38  * @package  StatusNet
39  * @author   Brion Vibber <brion@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 BitlyadminpanelAction extends AdminPanelAction
45 {
46     /**
47      * Returns the page title
48      *
49      * @return string page title
50      */
51
52     function title()
53     {
54         return _m('bit.ly URL shortening');
55     }
56
57     /**
58      * Instructions for using this form.
59      *
60      * @return string instructions
61      */
62
63     function getInstructions()
64     {
65         return _m('URL shortening with bit.ly requires ' .
66                   '[a bit.ly account and API key](http://bit.ly/a/your_api_key). ' .
67                   'This verifies that this is an authorized account, and ' .
68                   'allow you to use bit.ly\'s tracking features and custom domains.');
69     }
70
71     /**
72      * Show the bit.ly admin panel form
73      *
74      * @return void
75      */
76
77     function showForm()
78     {
79         $form = new BitlyAdminPanelForm($this);
80         $form->show();
81         return;
82     }
83
84     /**
85      * Save settings from the form
86      *
87      * @return void
88      */
89
90     function saveSettings()
91     {
92         static $settings = array(
93             'bitly' => array('default_login', 'default_apikey')
94         );
95
96         static $booleans = array(
97             'bitly' => array('allow_override')
98         );
99
100         $values = array();
101
102         foreach ($settings as $section => $parts) {
103             foreach ($parts as $setting) {
104                 $values[$section][$setting]
105                     = $this->trimmed($setting);
106             }
107         }
108
109         foreach ($booleans as $section => $parts) {
110             foreach ($parts as $setting) {
111                 $values[$section][$setting]
112                     = ($this->boolean($setting)) ? 1 : 0;
113             }
114         }
115
116         // This throws an exception on validation errors
117
118         $this->validate($values);
119
120         // assert(all values are valid);
121
122         $config = new Config();
123
124         $config->query('BEGIN');
125
126         foreach ($settings as $section => $parts) {
127             foreach ($parts as $setting) {
128                 Config::save($section, $setting, $values[$section][$setting]);
129             }
130         }
131
132         foreach ($booleans as $section => $parts) {
133             foreach ($parts as $setting) {
134                 Config::save($section, $setting, $values[$section][$setting]);
135             }
136         }
137
138         $config->query('COMMIT');
139
140         return;
141     }
142
143     function validate(&$values)
144     {
145         // Validate consumer key and secret (can't be too long)
146
147         if (mb_strlen($values['bitly']['default_apikey']) > 255) {
148             $this->clientError(
149                 _m("Invalid login. Max length is 255 characters.")
150             );
151         }
152
153         if (mb_strlen($values['bitly']['default_apikey']) > 255) {
154             $this->clientError(
155                 _m("Invalid API key. Max length is 255 characters.")
156             );
157         }
158     }
159 }
160
161 class BitlyAdminPanelForm extends AdminForm
162 {
163     /**
164      * ID of the form
165      *
166      * @return int ID of the form
167      */
168
169     function id()
170     {
171         return 'bitlyadminpanel';
172     }
173
174     /**
175      * class of the form
176      *
177      * @return string class of the form
178      */
179
180     function formClass()
181     {
182         return 'form_settings';
183     }
184
185     /**
186      * Action of the form
187      *
188      * @return string URL of the action
189      */
190
191     function action()
192     {
193         return common_local_url('bitlyadminpanel');
194     }
195
196     /**
197      * Data elements of the form
198      *
199      * @return void
200      */
201
202     function formData()
203     {
204         $this->out->elementStart(
205             'fieldset',
206             array('id' => 'settings_bitly')
207         );
208         $this->out->element('legend', null, _m('Default credentials'));
209         $this->out->element('p', 'form_guide',
210             _m('When users select the bit.ly shortening service, by default ' .
211                'these credentials will be used. If you leave these empty, ' .
212                'users will need to set up their own credentials in order to ' .
213                'use bit.ly.'));
214         $this->out->elementStart('ul', 'form_data');
215
216         $this->li();
217         $this->input(
218             'default_login',
219             _m('Login name'),
220             null,
221             'bitly'
222         );
223         $this->unli();
224
225         $this->li();
226         $this->input(
227             'default_apikey',
228              _m('API key'),
229             null,
230             'bitly'
231         );
232         $this->unli();
233
234         $this->out->elementEnd('ul');
235         $this->out->elementEnd('fieldset');
236
237         $this->out->elementStart(
238             'fieldset',
239             array('id' => 'settings_bitly-options')
240         );
241         $this->out->element('legend', null, _m('Options'));
242
243         $this->out->elementStart('ul', 'form_data');
244
245         $this->li();
246
247         $this->out->checkbox(
248             'allow_override', _m('Allow users to specify their own API key.'),
249             (bool) $this->value('bitly', 'allow_override'),
250             _m('If set, users will be able to specify their own bit.ly login and API key ' .
251                'to be used for shortening their links.'),
252             'true'
253         );
254         $this->unli();
255
256         $this->out->elementEnd('ul');
257
258         $this->out->elementEnd('fieldset');
259     }
260
261     /**
262      * Action elements
263      *
264      * @return void
265      */
266
267     function formActions()
268     {
269         $this->out->submit('submit', _('Save'), 'submit', null, _m('Save bit.ly settings'));
270     }
271 }