]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/siteadminpanel.php
Lots of tiny message changes.
[quix0rs-gnu-social.git] / actions / siteadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Site 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    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * Administer site settings
38  *
39  * @category Admin
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Zach Copley <zach@status.net>
43  * @author   Sarven Capadisli <csarven@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class SiteadminpanelAction extends AdminPanelAction
49 {
50     /**
51      * Returns the page title
52      *
53      * @return string page title
54      */
55
56     function title()
57     {
58         return _('Site');
59     }
60
61     /**
62      * Instructions for using this form.
63      *
64      * @return string instructions
65      */
66
67     function getInstructions()
68     {
69         return _('Basic settings for this StatusNet site.');
70     }
71
72     /**
73      * Show the site admin panel form
74      *
75      * @return void
76      */
77
78     function showForm()
79     {
80         $form = new SiteAdminPanelForm($this);
81         $form->show();
82         return;
83     }
84
85     /**
86      * Save settings from the form
87      *
88      * @return void
89      */
90
91     function saveSettings()
92     {
93         static $settings = array('site' => array('name', 'broughtby', 'broughtbyurl',
94                                                  'email', 'timezone', 'language',
95                                                  'site', 'textlimit', 'dupelimit'),
96                                  'snapshot' => array('run', 'reporturl', 'frequency'));
97
98         static $booleans = array('site' => array('private', 'inviteonly', 'closed', 'fancy'));
99
100         $values = array();
101
102         foreach ($settings as $section => $parts) {
103             foreach ($parts as $setting) {
104                 $values[$section][$setting] = $this->trimmed($setting);
105             }
106         }
107
108         foreach ($booleans as $section => $parts) {
109             foreach ($parts as $setting) {
110                 $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
111             }
112         }
113
114         // This throws an exception on validation errors
115
116         $this->validate($values);
117
118         // assert(all values are valid);
119
120         $config = new Config();
121
122         $config->query('BEGIN');
123
124         foreach ($settings as $section => $parts) {
125             foreach ($parts as $setting) {
126                 Config::save($section, $setting, $values[$section][$setting]);
127             }
128         }
129
130         foreach ($booleans as $section => $parts) {
131             foreach ($parts as $setting) {
132                 Config::save($section, $setting, $values[$section][$setting]);
133             }
134         }
135
136         $config->query('COMMIT');
137
138         return;
139     }
140
141     function validate(&$values)
142     {
143         // Validate site name
144
145         if (empty($values['site']['name'])) {
146             $this->clientError(_("Site name must have non-zero length."));
147         }
148
149         // Validate email
150
151         $values['site']['email'] = common_canonical_email($values['site']['email']);
152
153         if (empty($values['site']['email'])) {
154             $this->clientError(_('You must have a valid contact email address.'));
155         }
156         if (!Validate::email($values['site']['email'], common_config('email', 'check_domain'))) {
157             $this->clientError(_('Not a valid email address.'));
158         }
159
160         // Validate timezone
161
162         if (is_null($values['site']['timezone']) ||
163             !in_array($values['site']['timezone'], DateTimeZone::listIdentifiers())) {
164             $this->clientError(_('Timezone not selected.'));
165             return;
166         }
167
168         // Validate language
169
170         if (!is_null($values['site']['language']) &&
171             !in_array($values['site']['language'], array_keys(get_nice_language_list()))) {
172             $this->clientError(sprintf(_('Unknown language "%s".'), $values['site']['language']));
173         }
174
175         // Validate report URL
176
177         if (!is_null($values['snapshot']['reporturl']) &&
178             !Validate::uri($values['snapshot']['reporturl'], array('allowed_schemes' => array('http', 'https')))) {
179             $this->clientError(_("Invalid snapshot report URL."));
180         }
181
182         // Validate snapshot run value
183
184         if (!in_array($values['snapshot']['run'], array('web', 'cron', 'never'))) {
185             $this->clientError(_("Invalid snapshot run value."));
186         }
187
188         // Validate snapshot run value
189
190         if (!Validate::number($values['snapshot']['frequency'])) {
191             $this->clientError(_("Snapshot frequency must be a number."));
192         }
193
194         // Validate text limit
195
196         if (!Validate::number($values['site']['textlimit'], array('min' => 140))) {
197             $this->clientError(_("Minimum text limit is 140 characters."));
198         }
199
200         // Validate dupe limit
201
202         if (!Validate::number($values['site']['dupelimit'], array('min' => 1))) {
203             $this->clientError(_("Dupe limit must 1 or more seconds."));
204         }
205
206     }
207 }
208
209 class SiteAdminPanelForm extends AdminForm
210 {
211     /**
212      * ID of the form
213      *
214      * @return int ID of the form
215      */
216
217     function id()
218     {
219         return 'form_site_admin_panel';
220     }
221
222     /**
223      * class of the form
224      *
225      * @return string class of the form
226      */
227
228     function formClass()
229     {
230         return 'form_settings';
231     }
232
233     /**
234      * Action of the form
235      *
236      * @return string URL of the action
237      */
238
239     function action()
240     {
241         return common_local_url('siteadminpanel');
242     }
243
244     /**
245      * Data elements of the form
246      *
247      * @return void
248      */
249
250     function formData()
251     {
252         $this->out->elementStart('fieldset', array('id' => 'settings_admin_general'));
253         $this->out->element('legend', null, _('General'));
254         $this->out->elementStart('ul', 'form_data');
255         $this->li();
256         $this->input('name', _('Site name'),
257                      _('The name of your site, like "Yourcompany Microblog"'));
258         $this->unli();
259
260         $this->li();
261         $this->input('broughtby', _('Brought by'),
262                      _('Text used for credits link in footer of each page'));
263         $this->unli();
264
265         $this->li();
266         $this->input('broughtbyurl', _('Brought by URL'),
267                      _('URL used for credits link in footer of each page'));
268         $this->unli();
269         $this->li();
270         $this->input('email', _('Email'),
271                      _('Contact email address for your site'));
272         $this->unli();
273         $this->out->elementEnd('ul');
274         $this->out->elementEnd('fieldset');
275
276         $this->out->elementStart('fieldset', array('id' => 'settings_admin_local'));
277         $this->out->element('legend', null, _('Local'));
278         $this->out->elementStart('ul', 'form_data');
279         $timezones = array();
280
281         foreach (DateTimeZone::listIdentifiers() as $k => $v) {
282             $timezones[$v] = $v;
283         }
284
285         asort($timezones);
286
287         $this->li();
288         $this->out->dropdown('timezone', _('Default timezone'),
289                              $timezones, _('Default timezone for the site; usually UTC.'),
290                              true, $this->value('timezone'));
291         $this->unli();
292
293         $this->li();
294         $this->out->dropdown('language', _('Language'),
295                              get_nice_language_list(), _('Default site language'),
296                              false, $this->value('language'));
297         $this->unli();
298
299         $this->out->elementEnd('ul');
300         $this->out->elementEnd('fieldset');
301
302         $this->out->elementStart('fieldset', array('id' => 'settings_admin_urls'));
303         $this->out->element('legend', null, _('URLs'));
304         $this->out->elementStart('ul', 'form_data');
305         $this->li();
306         $this->input('server', _('Server'), _('Site\'s server hostname.'));
307         $this->unli();
308
309         $this->li();
310         $this->out->checkbox('fancy', _('Fancy URLs'),
311                              (bool) $this->value('fancy'),
312                              _('Use fancy (more readable and memorable) URLs?'));
313         $this->unli();
314         $this->out->elementEnd('ul');
315         $this->out->elementEnd('fieldset');
316
317         $this->out->elementStart('fieldset', array('id' => 'settings_admin_access'));
318         $this->out->element('legend', null, _('Access'));
319         $this->out->elementStart('ul', 'form_data');
320         $this->li();
321         $this->out->checkbox('private', _('Private'),
322                              (bool) $this->value('private'),
323                              _('Prohibit anonymous users (not logged in) from viewing site?'));
324         $this->unli();
325
326         $this->li();
327         $this->out->checkbox('inviteonly', _('Invite only'),
328                              (bool) $this->value('inviteonly'),
329                              _('Make registration invitation only.'));
330         $this->unli();
331
332         $this->li();
333         $this->out->checkbox('closed', _('Closed'),
334                              (bool) $this->value('closed'),
335                              _('Disable new registrations.'));
336         $this->unli();
337         $this->out->elementEnd('ul');
338         $this->out->elementEnd('fieldset');
339
340         $this->out->elementStart('fieldset', array('id' => 'settings_admin_snapshots'));
341         $this->out->element('legend', null, _('Snapshots'));
342         $this->out->elementStart('ul', 'form_data');
343         $this->li();
344         $snapshot = array('web' => _('Randomly during Web hit'),
345                           'cron' => _('In a scheduled job'),
346                           'never' => _('Never'));
347         $this->out->dropdown('run', _('Data snapshots'),
348                              $snapshot, _('When to send statistical data to status.net servers'),
349                              false, $this->value('run', 'snapshot'));
350         $this->unli();
351
352         $this->li();
353         $this->input('frequency', _('Frequency'),
354                      _('Snapshots will be sent once every N web hits'),
355                      'snapshot');
356         $this->unli();
357
358         $this->li();
359         $this->input('reporturl', _('Report URL'),
360                      _('Snapshots will be sent to this URL'),
361                      'snapshot');
362         $this->unli();
363         $this->out->elementEnd('ul');
364         $this->out->elementEnd('fieldset');
365
366         $this->out->elementStart('fieldset', array('id' => 'settings_admin_limits'));
367         $this->out->element('legend', null, _('Limits'));
368         $this->out->elementStart('ul', 'form_data');
369         $this->li();
370         $this->input('textlimit', _('Text limit'), _('Maximum number of characters for notices.'));
371         $this->unli();
372
373         $this->li();
374         $this->input('dupelimit', _('Dupe limit'), _('How long users must wait (in seconds) to post the same thing again.'));
375         $this->unli();
376         $this->out->elementEnd('ul');
377         $this->out->elementEnd('fieldset');
378     }
379
380     /**
381      * Action elements
382      *
383      * @return void
384      */
385
386     function formActions()
387     {
388         $this->out->submit('submit', _('Save'), 'submit', null, _('Save site settings'));
389     }
390 }