]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/licenseadminpanel.php
Merge branch 'master' into 1.0.x
[quix0rs-gnu-social.git] / actions / licenseadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * License 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    Zach Copley <zach@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  * License settings
36  *
37  * @category Admin
38  * @package  StatusNet
39  * @author   Zach Copley <zach@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 class LicenseadminpanelAction extends AdminPanelAction
44 {
45     /**
46      * Returns the page title
47      *
48      * @return string page title
49      */
50
51     function title()
52     {
53         // TRANS: User admin panel title
54         return _m('TITLE', 'License');
55     }
56
57     /**
58      * Instructions for using this form.
59      *
60      * @return string instructions
61      */
62     function getInstructions()
63     {
64         // TRANS: Form instructions for the site license admin panel.
65         return _('License for this StatusNet site');
66     }
67
68     /**
69      * Show the site admin panel form
70      *
71      * @return void
72      */
73     function showForm()
74     {
75         $form = new LicenseAdminPanelForm($this);
76         $form->show();
77         return;
78     }
79
80     /**
81      * Save settings from the form
82      *
83      * @return void
84      */
85     function saveSettings()
86     {
87         static $settings = array(
88             'license' => array('type', 'owner', 'url', 'title', 'image')
89         );
90
91         $values = array();
92
93         foreach ($settings as $section => $parts) {
94             foreach ($parts as $setting) {
95                 $values[$section][$setting] = $this->trimmed($setting);
96             }
97         }
98
99         // This throws an exception on validation errors
100
101         $this->validate($values);
102
103         // assert(all values are valid);
104
105         $config = new Config();
106
107         $config->query('BEGIN');
108
109         foreach ($settings as $section => $parts) {
110             foreach ($parts as $setting) {
111                 Config::save($section, $setting, $values[$section][$setting]);
112             }
113         }
114
115         $config->query('COMMIT');
116
117         return;
118     }
119
120     /**
121      * Validate License admin form values
122      *
123      * @param array &$values from the form
124      *
125      * @return nothing
126      */
127     function validate(&$values)
128     {
129         // Validate license type (shouldn't have to do it, but just in case)
130
131         $types = array('private', 'allrightsreserved', 'cc');
132
133         if (!in_array($values['license']['type'], $types)) {
134             // TRANS: Client error displayed selecting an invalid license in the license admin panel.
135             $this->clientError(_('Invalid license selection.'));
136         }
137
138         // Make sure the user has set an owner if the site has a private
139         // license
140
141         if ($values['license']['type'] == 'allrightsreserved'
142             && empty($values['license']['owner'])
143         ) {
144             $this->clientError(
145                 // TRANS: Client error displayed when not specifying an owner for the all rights reserved license in the license admin panel.
146                 _('You must specify the owner of the content when using the All Rights Reserved license.')
147             );
148         }
149
150         // Make sure the license title is not too long
151         if (mb_strlen($values['license']['type']) > 255) {
152             $this->clientError(
153                 // TRANS: Client error displayed selecting a too long license title in the license admin panel.
154                 _('Invalid license title. Maximum length is 255 characters.')
155             );
156         }
157
158         // make sure the license URL and license image URL are valid URLs
159
160         $options = array('allowed_schemes' => array('http', 'https'));
161
162         // URLs should be set for cc license
163
164         if ($values['license']['type'] == 'cc') {
165             if (!Validate::uri($values['license']['url'], $options)) {
166                 // TRANS: Client error displayed specifying an invalid license URL in the license admin panel.
167                 $this->clientError(_('Invalid license URL.'));
168             }
169             if (!Validate::uri($values['license']['image'], $options)) {
170                 // TRANS: Client error displayed specifying an invalid license image URL in the license admin panel.
171                 $this->clientError(_('Invalid license image URL.'));
172             }
173         }
174
175         // can be either blank or a valid URL for private & allrightsreserved
176
177         if (!empty($values['license']['url'])) {
178             if (!Validate::uri($values['license']['url'], $options)) {
179                 // TRANS: Client error displayed specifying an invalid license URL in the license admin panel.
180                 $this->clientError(_('License URL must be blank or a valid URL.'));
181             }
182         }
183
184         // can be either blank or a valid URL for private & allrightsreserved
185
186         if (!empty($values['license']['image'])) {
187             if (!Validate::uri($values['license']['image'], $options)) {
188                 // TRANS: Client error displayed specifying an invalid license image URL in the license admin panel.
189                 $this->clientError(_('License image must be blank or valid URL.'));
190             }
191         }
192     }
193 }
194
195 class LicenseAdminPanelForm extends AdminForm
196 {
197     /**
198      * ID of the form
199      *
200      * @return int ID of the form
201      */
202     function id()
203     {
204         return 'licenseadminpanel';
205     }
206
207     /**
208      * class of the form
209      *
210      * @return string class of the form
211      */
212     function formClass()
213     {
214         return 'form_settings';
215     }
216
217     /**
218      * Action of the form
219      *
220      * @return string URL of the action
221      */
222
223     function action()
224     {
225         return common_local_url('licenseadminpanel');
226     }
227
228     /**
229      * Data elements of the form
230      *
231      * @return void
232      */
233
234     function formData()
235     {
236         $this->out->elementStart(
237             'fieldset', array('id' => 'settings_license-selection')
238         );
239         // TRANS: Form legend in the license admin panel.
240         $this->out->element('legend', null, _('License selection'));
241         $this->out->elementStart('ul', 'form_data');
242
243         $this->li();
244
245         $types = array(
246             // TRANS: License option in the license admin panel.
247             'private' => _('Private'),
248             // TRANS: License option in the license admin panel.
249             'allrightsreserved' => _('All Rights Reserved'),
250             // TRANS: License option in the license admin panel.
251             'cc' => _('Creative Commons')
252         );
253
254         $this->out->dropdown(
255             'type',
256             // TRANS: Dropdown field label in the license admin panel.
257             _('Type'),
258             $types,
259             // TRANS: Dropdown field instructions in the license admin panel.
260             _('Select a license.'),
261             false,
262             $this->value('type', 'license')
263         );
264
265         $this->unli();
266
267         $this->out->elementEnd('ul');
268         $this->out->elementEnd('fieldset');
269
270         $this->out->elementStart(
271             'fieldset',
272             array('id' => 'settings_license-details')
273         );
274         // TRANS: Form legend in the license admin panel.
275         $this->out->element('legend', null, _('License details'));
276         $this->out->elementStart('ul', 'form_data');
277
278         $this->li();
279         $this->input(
280             'owner',
281             // TRANS: Field label in the license admin panel.
282             _('Owner'),
283             // TRANS: Field title in the license admin panel.
284             _('Name of the owner of the site\'s content (if applicable).'),
285             'license'
286         );
287         $this->unli();
288
289         $this->li();
290         $this->input(
291             'title',
292             // TRANS: Field label in the license admin panel.
293             _('License Title'),
294             // TRANS: Field title in the license admin panel.
295             _('The title of the license.'),
296             'license'
297         );
298         $this->unli();
299
300         $this->li();
301         $this->input(
302             'url',
303             // TRANS: Field label in the license admin panel.
304             _('License URL'),
305             // TRANS: Field title in the license admin panel.
306             _('URL for more information about the license.'),
307             'license'
308         );
309         $this->unli();
310
311         $this->li();
312         $this->input(
313             // TRANS: Field label in the license admin panel.
314             'image', _('License Image URL'),
315             // TRANS: Field title in the license admin panel.
316             _('URL for an image to display with the license.'),
317             'license'
318         );
319         $this->unli();
320
321         $this->out->elementEnd('ul');
322         $this->out->elementEnd('fieldset');
323     }
324
325     /**
326      * Action elements
327      *
328      * @return void
329      */
330     function formActions()
331     {
332         $this->out->submit(
333             'submit',
334             // TRANS: Button text in the license admin panel.
335             _m('BUTTON','Save'),
336             'submit',
337             null,
338             // TRANS: Button title in the license admin panel.
339             _('Save license settings.')
340         );
341     }
342 }