]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/siteadminpanel.php
Merge branch '0.9.x' into 1.0.x
[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-2010 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         $values = array();
99
100         foreach ($settings as $section => $parts) {
101             foreach ($parts as $setting) {
102                 $values[$section][$setting] = $this->trimmed($setting);
103             }
104         }
105
106         // This throws an exception on validation errors
107
108         $this->validate($values);
109
110         // assert(all values are valid);
111
112         $config = new Config();
113
114         $config->query('BEGIN');
115
116         foreach ($settings as $section => $parts) {
117             foreach ($parts as $setting) {
118                 Config::save($section, $setting, $values[$section][$setting]);
119             }
120         }
121
122         $config->query('COMMIT');
123
124         return;
125     }
126
127     function validate(&$values)
128     {
129         // Validate site name
130
131         if (empty($values['site']['name'])) {
132             $this->clientError(_("Site name must have non-zero length."));
133         }
134
135         // Validate email
136
137         $values['site']['email'] = common_canonical_email($values['site']['email']);
138
139         if (empty($values['site']['email'])) {
140             $this->clientError(_('You must have a valid contact email address.'));
141         }
142         if (!Validate::email($values['site']['email'], common_config('email', 'check_domain'))) {
143             $this->clientError(_('Not a valid email address.'));
144         }
145
146         // Validate timezone
147
148         if (is_null($values['site']['timezone']) ||
149             !in_array($values['site']['timezone'], DateTimeZone::listIdentifiers())) {
150             $this->clientError(_('Timezone not selected.'));
151             return;
152         }
153
154         // Validate language
155
156         if (!is_null($values['site']['language']) &&
157             !in_array($values['site']['language'], array_keys(get_nice_language_list()))) {
158             $this->clientError(sprintf(_('Unknown language "%s".'), $values['site']['language']));
159         }
160
161         // Validate report URL
162
163         if (!is_null($values['snapshot']['reporturl']) &&
164             !Validate::uri($values['snapshot']['reporturl'], array('allowed_schemes' => array('http', 'https')))) {
165             $this->clientError(_("Invalid snapshot report URL."));
166         }
167
168         // Validate snapshot run value
169
170         if (!in_array($values['snapshot']['run'], array('web', 'cron', 'never'))) {
171             $this->clientError(_("Invalid snapshot run value."));
172         }
173
174         // Validate snapshot run value
175
176         if (!Validate::number($values['snapshot']['frequency'])) {
177             $this->clientError(_("Snapshot frequency must be a number."));
178         }
179
180         // Validate text limit
181
182         if (!Validate::number($values['site']['textlimit'], array('min' => 140))) {
183             $this->clientError(_("Minimum text limit is 140 characters."));
184         }
185
186         // Validate dupe limit
187
188         if (!Validate::number($values['site']['dupelimit'], array('min' => 1))) {
189             $this->clientError(_("Dupe limit must 1 or more seconds."));
190         }
191
192     }
193 }
194
195 class SiteAdminPanelForm extends AdminForm
196 {
197     /**
198      * ID of the form
199      *
200      * @return int ID of the form
201      */
202
203     function id()
204     {
205         return 'form_site_admin_panel';
206     }
207
208     /**
209      * class of the form
210      *
211      * @return string class of the form
212      */
213
214     function formClass()
215     {
216         return 'form_settings';
217     }
218
219     /**
220      * Action of the form
221      *
222      * @return string URL of the action
223      */
224
225     function action()
226     {
227         return common_local_url('siteadminpanel');
228     }
229
230     /**
231      * Data elements of the form
232      *
233      * @return void
234      */
235
236     function formData()
237     {
238         $this->out->elementStart('fieldset', array('id' => 'settings_admin_general'));
239         $this->out->element('legend', null, _('General'));
240         $this->out->elementStart('ul', 'form_data');
241         $this->li();
242         $this->input('name', _('Site name'),
243                      _('The name of your site, like "Yourcompany Microblog"'));
244         $this->unli();
245
246         $this->li();
247         $this->input('broughtby', _('Brought by'),
248                      _('Text used for credits link in footer of each page'));
249         $this->unli();
250
251         $this->li();
252         $this->input('broughtbyurl', _('Brought by URL'),
253                      _('URL used for credits link in footer of each page'));
254         $this->unli();
255         $this->li();
256         $this->input('email', _('Email'),
257                      _('Contact email address for your site'));
258         $this->unli();
259         $this->out->elementEnd('ul');
260         $this->out->elementEnd('fieldset');
261
262         $this->out->elementStart('fieldset', array('id' => 'settings_admin_local'));
263         $this->out->element('legend', null, _('Local'));
264         $this->out->elementStart('ul', 'form_data');
265         $timezones = array();
266
267         foreach (DateTimeZone::listIdentifiers() as $k => $v) {
268             $timezones[$v] = $v;
269         }
270
271         asort($timezones);
272
273         $this->li();
274         $this->out->dropdown('timezone', _('Default timezone'),
275                              $timezones, _('Default timezone for the site; usually UTC.'),
276                              true, $this->value('timezone'));
277         $this->unli();
278
279         $this->li();
280         $this->out->dropdown('language', _('Language'),
281                              get_nice_language_list(), _('Default site language'),
282                              false, $this->value('language'));
283         $this->unli();
284
285         $this->out->elementEnd('ul');
286         $this->out->elementEnd('fieldset');
287
288         $this->out->elementStart('fieldset', array('id' => 'settings_admin_snapshots'));
289         $this->out->element('legend', null, _('Snapshots'));
290         $this->out->elementStart('ul', 'form_data');
291         $this->li();
292         $snapshot = array('web' => _('Randomly during Web hit'),
293                           'cron' => _('In a scheduled job'),
294                           'never' => _('Never'));
295         $this->out->dropdown('run', _('Data snapshots'),
296                              $snapshot, _('When to send statistical data to status.net servers'),
297                              false, $this->value('run', 'snapshot'));
298         $this->unli();
299
300         $this->li();
301         $this->input('frequency', _('Frequency'),
302                      _('Snapshots will be sent once every N web hits'),
303                      'snapshot');
304         $this->unli();
305
306         $this->li();
307         $this->input('reporturl', _('Report URL'),
308                      _('Snapshots will be sent to this URL'),
309                      'snapshot');
310         $this->unli();
311         $this->out->elementEnd('ul');
312         $this->out->elementEnd('fieldset');
313
314         $this->out->elementStart('fieldset', array('id' => 'settings_admin_limits'));
315         $this->out->element('legend', null, _('Limits'));
316         $this->out->elementStart('ul', 'form_data');
317         $this->li();
318         $this->input('textlimit', _('Text limit'), _('Maximum number of characters for notices.'));
319         $this->unli();
320
321         $this->li();
322         $this->input('dupelimit', _('Dupe limit'), _('How long users must wait (in seconds) to post the same thing again.'));
323         $this->unli();
324         $this->out->elementEnd('ul');
325         $this->out->elementEnd('fieldset');
326     }
327
328     /**
329      * Action elements
330      *
331      * @return void
332      */
333
334     function formActions()
335     {
336         $this->out->submit('submit', _('Save'), 'submit', null, _('Save site settings'));
337     }
338 }