]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/siteadminpanel.php
Merge branch '0.9.x' into mapstraction
[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                                                  'ssl', 'sslserver', 'site', 'path',
96                                                  'textlimit', 'dupelimit', 'locale_path'),
97                                  'snapshot' => array('run', 'reporturl', 'frequency'));
98
99         static $booleans = array('site' => array('private', 'inviteonly', 'closed', 'fancy'));
100
101         $values = array();
102
103         foreach ($settings as $section => $parts) {
104             foreach ($parts as $setting) {
105                 $values[$section][$setting] = $this->trimmed($setting);
106             }
107         }
108
109         foreach ($booleans as $section => $parts) {
110             foreach ($parts as $setting) {
111                 $values[$section][$setting] = ($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 site name
145
146         if (empty($values['site']['name'])) {
147             $this->clientError(_("Site name must have non-zero length."));
148         }
149
150         // Validate email
151
152         $values['site']['email'] = common_canonical_email($values['site']['email']);
153
154         if (empty($values['site']['email'])) {
155             $this->clientError(_('You must have a valid contact email address'));
156         }
157         if (!Validate::email($values['site']['email'], common_config('email', 'check_domain'))) {
158             $this->clientError(_('Not a valid email address'));
159         }
160
161         // Validate timezone
162
163         if (is_null($values['site']['timezone']) ||
164             !in_array($values['site']['timezone'], DateTimeZone::listIdentifiers())) {
165             $this->clientError(_('Timezone not selected.'));
166             return;
167         }
168
169         // Validate language
170
171         if (!is_null($values['site']['language']) &&
172             !in_array($values['site']['language'], array_keys(get_nice_language_list()))) {
173             $this->clientError(sprintf(_('Unknown language "%s"'), $values['site']['language']));
174         }
175
176         // Validate report URL
177
178         if (!is_null($values['snapshot']['reporturl']) &&
179             !Validate::uri($values['snapshot']['reporturl'], array('allowed_schemes' => array('http', 'https')))) {
180             $this->clientError(_("Invalid snapshot report URL."));
181         }
182
183         // Validate snapshot run value
184
185         if (!in_array($values['snapshot']['run'], array('web', 'cron', 'never'))) {
186             $this->clientError(_("Invalid snapshot run value."));
187         }
188
189         // Validate snapshot run value
190
191         if (!Validate::number($values['snapshot']['frequency'])) {
192             $this->clientError(_("Snapshot frequency must be a number."));
193         }
194
195         // Validate SSL setup
196
197         if (in_array($values['site']['ssl'], array('sometimes', 'always'))) {
198             if (empty($values['site']['sslserver'])) {
199                 $this->clientError(_("You must set an SSL sever when enabling SSL."));
200             }
201         }
202
203         if (mb_strlen($values['site']['sslserver']) > 255) {
204             $this->clientError(_("Invalid SSL server. Max length is 255 characters."));
205         }
206
207         // Validate text limit
208
209         if (!Validate::number($values['site']['textlimit'], array('min' => 140))) {
210             $this->clientError(_("Minimum text limit is 140c."));
211         }
212
213         // Validate dupe limit
214
215         if (!Validate::number($values['site']['dupelimit'], array('min' => 1))) {
216             $this->clientError(_("Dupe limit must 1 or more seconds."));
217         }
218
219         // Validate locales path
220
221         // XXX: What else do we need to validate for lacales path here? --Z
222
223         if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
224             $this->clientError(sprintf(_("Locales directory not readable: %s"), $values['site']['locale_path']));
225         }
226
227     }
228 }
229
230 class SiteAdminPanelForm extends AdminForm
231 {
232     /**
233      * ID of the form
234      *
235      * @return int ID of the form
236      */
237
238     function id()
239     {
240         return 'form_site_admin_panel';
241     }
242
243     /**
244      * class of the form
245      *
246      * @return string class of the form
247      */
248
249     function formClass()
250     {
251         return 'form_settings';
252     }
253
254     /**
255      * Action of the form
256      *
257      * @return string URL of the action
258      */
259
260     function action()
261     {
262         return common_local_url('siteadminpanel');
263     }
264
265     /**
266      * Data elements of the form
267      *
268      * @return void
269      */
270
271     function formData()
272     {
273         $this->out->elementStart('fieldset', array('id' => 'settings_admin_general'));
274         $this->out->element('legend', null, _('General'));
275         $this->out->elementStart('ul', 'form_data');
276         $this->li();
277         $this->input('name', _('Site name'),
278                      _('The name of your site, like "Yourcompany Microblog"'));
279         $this->unli();
280
281         $this->li();
282         $this->input('broughtby', _('Brought by'),
283                      _('Text used for credits link in footer of each page'));
284         $this->unli();
285
286         $this->li();
287         $this->input('broughtbyurl', _('Brought by URL'),
288                      _('URL used for credits link in footer of each page'));
289         $this->unli();
290         $this->li();
291         $this->input('email', _('Email'),
292                      _('contact email address for your site'));
293         $this->unli();
294         $this->out->elementEnd('ul');
295         $this->out->elementEnd('fieldset');
296
297         $this->out->elementStart('fieldset', array('id' => 'settings_admin_local'));
298         $this->out->element('legend', null, _('Local'));
299         $this->out->elementStart('ul', 'form_data');
300         $timezones = array();
301
302         foreach (DateTimeZone::listIdentifiers() as $k => $v) {
303             $timezones[$v] = $v;
304         }
305
306         asort($timezones);
307
308         $this->li();
309         $this->out->dropdown('timezone', _('Default timezone'),
310                              $timezones, _('Default timezone for the site; usually UTC.'),
311                              true, $this->value('timezone'));
312         $this->unli();
313
314         $this->li();
315         $this->out->dropdown('language', _('Language'),
316                              get_nice_language_list(), _('Default site language'),
317                              false, $this->value('language'));
318         $this->unli();
319
320         $this->li();
321         $this->input('locale_path', _('Path to locales'), _('Directory path to locales'));
322         $this->unli();
323         $this->out->elementEnd('ul');
324         $this->out->elementEnd('fieldset');
325
326         $this->out->elementStart('fieldset', array('id' => 'settings_admin_urls'));
327         $this->out->element('legend', null, _('URLs'));
328         $this->out->elementStart('ul', 'form_data');
329         $this->li();
330         $this->input('server', _('Server'), _('Site\'s server hostname.'));
331         $this->unli();
332
333         $this->li();
334         $this->input('path', _('Path'), _('Site path'));
335         $this->unli();
336
337         $this->li();
338         $this->out->checkbox('fancy', _('Fancy URLs'),
339                              (bool) $this->value('fancy'),
340                              _('Use fancy (more readable and memorable) URLs?'));
341         $this->unli();
342         $this->out->elementEnd('ul');
343         $this->out->elementEnd('fieldset');
344
345         $this->out->elementStart('fieldset', array('id' => 'settings_admin_access'));
346         $this->out->element('legend', null, _('Access'));
347         $this->out->elementStart('ul', 'form_data');
348         $this->li();
349         $this->out->checkbox('private', _('Private'),
350                              (bool) $this->value('private'),
351                              _('Prohibit anonymous users (not logged in) from viewing site?'));
352         $this->unli();
353
354         $this->li();
355         $this->out->checkbox('inviteonly', _('Invite only'),
356                              (bool) $this->value('inviteonly'),
357                              _('Make registration invitation only.'));
358         $this->unli();
359
360         $this->li();
361         $this->out->checkbox('closed', _('Closed'),
362                              (bool) $this->value('closed'),
363                              _('Disable new registrations.'));
364         $this->unli();
365         $this->out->elementEnd('ul');
366         $this->out->elementEnd('fieldset');
367
368         $this->out->elementStart('fieldset', array('id' => 'settings_admin_snapshots'));
369         $this->out->element('legend', null, _('Snapshots'));
370         $this->out->elementStart('ul', 'form_data');
371         $this->li();
372         $snapshot = array('web' => _('Randomly during Web hit'),
373                           'cron' => _('In a scheduled job'),
374                           'never' => _('Never'));
375         $this->out->dropdown('run', _('Data snapshots'),
376                              $snapshot, _('When to send statistical data to status.net servers'),
377                              false, $this->value('run', 'snapshot'));
378         $this->unli();
379
380         $this->li();
381         $this->input('frequency', _('Frequency'),
382                      _('Snapshots will be sent once every N Web hits'),
383                      'snapshot');
384         $this->unli();
385
386         $this->li();
387         $this->input('reporturl', _('Report URL'),
388                      _('Snapshots will be sent to this URL'),
389                      'snapshot');
390         $this->unli();
391         $this->out->elementEnd('ul');
392         $this->out->elementEnd('fieldset');
393
394         $this->out->elementStart('fieldset', array('id' => 'settings_admin_ssl'));
395         $this->out->element('legend', null, _('SSL'));
396         $this->out->elementStart('ul', 'form_data');
397         $this->li();
398         $ssl = array('never' => _('Never'),
399                      'sometimes' => _('Sometimes'),
400                      'always' => _('Always'));
401
402         $this->out->dropdown('ssl', _('Use SSL'),
403                              $ssl, _('When to use SSL'),
404                              false, $this->value('ssl', 'site'));
405         $this->unli();
406
407         $this->li();
408         $this->input('sslserver', _('SSL Server'),
409                      _('Server to direct SSL requests to'));
410         $this->unli();
411         $this->out->elementEnd('ul');
412         $this->out->elementEnd('fieldset');
413
414         $this->out->elementStart('fieldset', array('id' => 'settings_admin_limits'));
415         $this->out->element('legend', null, _('Limits'));
416         $this->out->elementStart('ul', 'form_data');
417         $this->li();
418         $this->input('textlimit', _('Text limit'), _('Maximum number of characters for notices.'));
419         $this->unli();
420
421         $this->li();
422         $this->input('dupelimit', _('Dupe limit'), _('How long users must wait (in seconds) to post the same thing again.'));
423         $this->unli();
424         $this->out->elementEnd('ul');
425         $this->out->elementEnd('fieldset');
426     }
427
428     /**
429      * Action elements
430      *
431      * @return void
432      */
433
434     function formActions()
435     {
436         $this->out->submit('submit', _('Save'), 'submit', null, _('Save site settings'));
437     }
438 }