]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/pathsadminpanel.php
Merge remote branch 'gitorious/0.9.x' into 0.9.x
[quix0rs-gnu-social.git] / actions / pathsadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Paths 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  * Paths 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 PathsadminpanelAction extends AdminPanelAction
49 {
50
51     /**
52      * Returns the page title
53      *
54      * @return string page title
55      */
56
57     function title()
58     {
59         return _('Paths');
60     }
61
62     /**
63      * Instructions for using this form.
64      *
65      * @return string instructions
66      */
67
68     function getInstructions()
69     {
70         return _('Path and server settings for this StatusNet site');
71     }
72
73     /**
74      * Show the paths admin panel form
75      *
76      * @return void
77      */
78
79     function showForm()
80     {
81         $form = new PathsAdminPanelForm($this);
82         $form->show();
83         return;
84     }
85
86     /**
87      * Save settings from the form
88      *
89      * @return void
90      */
91
92     function saveSettings()
93     {
94         static $settings = array(
95                                  'site' => array('path', 'locale_path', 'ssl', 'sslserver'),
96                                  'theme' => array('server', 'dir', 'path', 'sslserver', 'sslpath'),
97                                  'avatar' => array('server', 'dir', 'path'),
98                                  'background' => array('server', 'dir', 'path', 'sslserver', 'sslpath'),
99                                  'attachments' => array('server', 'dir', 'path', 'sslserver', 'sslpath')
100                                  );
101
102         // XXX: If we're only going to have one boolean on thi page we
103         // can remove some of the boolean processing code --Z
104
105         static $booleans = array('site' => array('fancy'));
106
107         $values = array();
108
109         foreach ($settings as $section => $parts) {
110             foreach ($parts as $setting) {
111                 $values[$section][$setting] = $this->trimmed("$section-$setting");
112             }
113         }
114
115         foreach ($booleans as $section => $parts) {
116             foreach ($parts as $setting) {
117                 $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
118             }
119         }
120
121         $this->validate($values);
122
123         // assert(all values are valid);
124
125         $config = new Config();
126
127         $config->query('BEGIN');
128
129         foreach ($settings as $section => $parts) {
130             foreach ($parts as $setting) {
131                 Config::save($section, $setting, $values[$section][$setting]);
132             }
133         }
134
135         foreach ($booleans as $section => $parts) {
136             foreach ($parts as $setting) {
137                 Config::save($section, $setting, $values[$section][$setting]);
138             }
139         }
140
141         $config->query('COMMIT');
142
143         return;
144     }
145
146     /**
147      * Attempt to validate setting values
148      *
149      * @return void
150      */
151
152     function validate(&$values)
153     {
154
155         // Validate theme dir
156
157         if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
158             $this->clientError(sprintf(_("Theme directory not readable: %s."), $values['theme']['dir']));
159         }
160
161         // Validate avatar dir
162
163         if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
164             $this->clientError(sprintf(_("Avatar directory not writable: %s."), $values['avatar']['dir']));
165         }
166
167         // Validate background dir
168
169         if (empty($values['background']['dir']) || !is_writable($values['background']['dir'])) {
170             $this->clientError(sprintf(_("Background directory not writable: %s."), $values['background']['dir']));
171         }
172
173         // Validate locales dir
174
175         // XXX: What else do we need to validate for lacales path here? --Z
176
177         if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
178             $this->clientError(sprintf(_("Locales directory not readable: %s."), $values['site']['locale_path']));
179         }
180
181         // Validate SSL setup
182
183         if (mb_strlen($values['site']['sslserver']) > 255) {
184             $this->clientError(_('Invalid SSL server. The maximum length is 255 characters.'));
185         }
186     }
187
188 }
189
190 class PathsAdminPanelForm extends AdminForm
191 {
192
193     /**
194      * ID of the form
195      *
196      * @return int ID of the form
197      */
198
199     function id()
200     {
201         return 'form_paths_admin_panel';
202     }
203
204     /**
205      * class of the form
206      *
207      * @return string class of the form
208      */
209
210     function formClass()
211     {
212         return 'form_settings';
213     }
214
215     /**
216      * Action of the form
217      *
218      * @return string URL of the action
219      */
220
221     function action()
222     {
223         return common_local_url('pathsadminpanel');
224     }
225
226     /**
227      * Data elements of the form
228      *
229      * @return void
230      */
231
232     function formData()
233     {
234         $this->out->elementStart('fieldset', array('id' => 'settings_paths_locale'));
235         $this->out->element('legend', null, _('Site'), 'site');
236         $this->out->elementStart('ul', 'form_data');
237
238         $this->li();
239         $this->input('server', _('Server'), _('Site\'s server hostname.'));
240         $this->unli();
241
242         $this->li();
243         $this->input('path', _('Path'), _('Site path'));
244         $this->unli();
245
246         $this->li();
247         $this->input('locale_path', _('Locale Directory'), _('Directory path to locales'), 'site');
248         $this->unli();
249
250         $this->li();
251         $this->out->checkbox('fancy', _('Fancy URLs'),
252                              (bool) $this->value('fancy'),
253                              _('Use fancy (more readable and memorable) URLs?'));
254         $this->unli();
255
256         $this->out->elementEnd('ul');
257         $this->out->elementEnd('fieldset');
258
259         $this->out->elementStart('fieldset', array('id' => 'settings_paths_theme'));
260         $this->out->element('legend', null, _('Theme'));
261
262         $this->out->elementStart('ul', 'form_data');
263
264         $this->li();
265         $this->input('server', _('Server'), _('Server for themes'), 'theme');
266         $this->unli();
267
268         $this->li();
269         $this->input('path', _('Path'), _('Web path to themes'), 'theme');
270         $this->unli();
271
272         $this->li();
273         $this->input('sslserver', _('SSL server'), _('SSL server for themes (default: SSL server)'), 'theme');
274         $this->unli();
275
276         $this->li();
277         $this->input('sslpath', _('SSL path'), _('SSL path to themes (default: /theme/)'), 'theme');
278         $this->unli();
279
280         $this->li();
281         $this->input('dir', _('Directory'), _('Directory where themes are located'), 'theme');
282         $this->unli();
283
284         $this->out->elementEnd('ul');
285
286         $this->out->elementEnd('fieldset');
287         $this->out->elementStart('fieldset', array('id' => 'settings_avatar-paths'));
288         $this->out->element('legend', null, _('Avatars'));
289
290         $this->out->elementStart('ul', 'form_data');
291
292         $this->li();
293         $this->input('server', _('Avatar server'), 'Server for avatars', 'avatar');
294         $this->unli();
295
296         $this->li();
297         $this->input('path', _('Avatar path'), 'Web path to avatars', 'avatar');
298         $this->unli();
299
300         $this->li();
301         $this->input('dir', _('Avatar directory'), 'Directory where avatars are located', 'avatar');
302         $this->unli();
303
304         $this->out->elementEnd('ul');
305
306         $this->out->elementEnd('fieldset');
307
308         $this->out->elementStart('fieldset', array('id' =>
309                                                    'settings_design_background-paths'));
310         $this->out->element('legend', null, _('Backgrounds'));
311         $this->out->elementStart('ul', 'form_data');
312
313         $this->li();
314         $this->input('server', _('Server'), 'Server for backgrounds', 'background');
315         $this->unli();
316
317         $this->li();
318         $this->input('path', _('Path'), 'Web path to backgrounds', 'background');
319         $this->unli();
320
321         $this->li();
322         $this->input('sslserver', _('SSL server'), 'Server for backgrounds on SSL pages', 'background');
323         $this->unli();
324
325         $this->li();
326         $this->input('sslpath', _('SSL path'), 'Web path to backgrounds on SSL pages', 'background');
327         $this->unli();
328
329         $this->li();
330         $this->input('dir', _('Directory'), 'Directory where backgrounds are located', 'background');
331         $this->unli();
332
333         $this->out->elementEnd('ul');
334         $this->out->elementEnd('fieldset');
335
336         $this->out->elementStart('fieldset', array('id' =>
337                                                    'settings_design_attachments-paths'));
338
339         $this->out->element('legend', null, _('Attachments'));
340         $this->out->elementStart('ul', 'form_data');
341
342         $this->li();
343         $this->input('server', _('Server'), 'Server for attachments', 'attachments');
344         $this->unli();
345
346         $this->li();
347         $this->input('path', _('Path'), 'Web path to attachments', 'attachments');
348         $this->unli();
349
350         $this->li();
351         $this->input('sslserver', _('SSL server'), 'Server for attachments on SSL pages', 'attachments');
352         $this->unli();
353
354         $this->li();
355         $this->input('sslpath', _('SSL path'), 'Web path to attachments on SSL pages', 'attachments');
356         $this->unli();
357
358         $this->li();
359         $this->input('dir', _('Directory'), 'Directory where attachments are located', 'attachments');
360         $this->unli();
361
362         $this->out->elementEnd('ul');
363         $this->out->elementEnd('fieldset');
364
365         $this->out->elementStart('fieldset', array('id' => 'settings_admin_ssl'));
366         $this->out->element('legend', null, _('SSL'));
367         $this->out->elementStart('ul', 'form_data');
368         $this->li();
369
370         $ssl = array('never' => _('Never'),
371                      'sometimes' => _('Sometimes'),
372                      'always' => _('Always'));
373
374         $this->out->dropdown('site-ssl', _('Use SSL'),
375                              $ssl, _('When to use SSL'),
376                              false, $this->value('ssl', 'site'));
377         $this->unli();
378
379         $this->li();
380         $this->input('sslserver', _('SSL server'),
381                      _('Server to direct SSL requests to'), 'site');
382         $this->unli();
383         $this->out->elementEnd('ul');
384         $this->out->elementEnd('fieldset');
385
386     }
387
388     /**
389      * Action elements
390      *
391      * @return void
392      */
393
394     function formActions()
395     {
396         $this->out->submit('save', _('Save'), 'submit',
397                            'save', _('Save paths'));
398     }
399
400     /**
401      * Utility to simplify some of the duplicated code around
402      * params and settings. Overriding the input() in the base class
403      * to handle a whole bunch of cases of settings with the same
404      * name under different sections.
405      *
406      * @param string $setting      Name of the setting
407      * @param string $title        Title to use for the input
408      * @param string $instructions Instructions for this field
409      * @param string $section      config section, default = 'site'
410      *
411      * @return void
412      */
413
414     function input($setting, $title, $instructions, $section='site')
415     {
416         $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
417     }
418 }