]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/licenseadminpanel.php
Merge branch 'master' into 0.9.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     /**
47      * Returns the page title
48      *
49      * @return string page title
50      */
51
52     function title()
53     {
54         // TRANS: User admin panel title
55         return _m('TITLE', 'License');
56     }
57
58     /**
59      * Instructions for using this form.
60      *
61      * @return string instructions
62      */
63     function getInstructions()
64     {
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             $this->clientError(_("Invalid license selection."));
135         }
136
137         // Make sure the user has set an owner if the site has a private
138         // license
139
140         if ($values['license']['type'] == 'allrightsreserved'
141             && empty($values['license']['owner'])
142         ) {
143             $this->clientError(
144                 _("You must specify the owner of the content when using the All Rights Reserved license.")
145             );
146         }
147
148         // Make sure the license title is not too long
149         if (mb_strlen($values['license']['type']) > 255) {
150             $this->clientError(
151                 _('Invalid license title. Maximum length is 255 characters.')
152             );
153         }
154
155         // make sure the license URL and license image URL are valid URLs
156
157         $options = array('allowed_schemes' => array('http', 'https'));
158
159         // URLs should be set for cc license
160
161         if ($values['license']['type'] == 'cc') {
162             if (!Validate::uri($values['license']['url'], $options)) {
163                 $this->clientError(_("Invalid license URL."));
164             }
165             if (!Validate::uri($values['license']['image'], $options)) {
166                 $this->clientError(_("Invalid license image URL."));
167             }
168         }
169
170         // can be either blank or a valid URL for private & allrightsreserved
171
172         if (!empty($values['license']['url'])) {
173             if (!Validate::uri($values['license']['url'], $options)) {
174                 $this->clientError(_("License URL must be blank or a valid URL."));
175             }
176         }
177
178         // can be either blank or a valid URL for private & allrightsreserved
179
180         if (!empty($values['license']['image'])) {
181             if (!Validate::uri($values['license']['image'], $options)) {
182                 $this->clientError(_("License image must be blank or valid URL."));
183             }
184         }
185     }
186 }
187
188 class LicenseAdminPanelForm extends AdminForm
189 {
190     /**
191      * ID of the form
192      *
193      * @return int ID of the form
194      */
195     function id()
196     {
197         return 'licenseadminpanel';
198     }
199
200     /**
201      * class of the form
202      *
203      * @return string class of the form
204      */
205     function formClass()
206     {
207         return 'form_settings';
208     }
209
210     /**
211      * Action of the form
212      *
213      * @return string URL of the action
214      */
215
216     function action()
217     {
218         return common_local_url('licenseadminpanel');
219     }
220
221     /**
222      * Data elements of the form
223      *
224      * @return void
225      */
226
227     function formData()
228     {
229         $this->out->elementStart(
230             'fieldset', array('id' => 'settings_license-selection')
231         );
232         $this->out->element('legend', null, _('License selection'));
233         $this->out->elementStart('ul', 'form_data');
234
235         $this->li();
236
237         $types = array(
238             'private' => _('Private'),
239             'allrightsreserved' => _('All Rights Reserved'),
240             'cc' => _('Creative Commons')
241         );
242
243         $this->out->dropdown(
244             'type',
245             _('Type'),
246             $types,
247             _('Select license'),
248             false,
249             $this->value('type', 'license')
250         );
251
252         $this->unli();
253
254         $this->out->elementEnd('ul');
255         $this->out->elementEnd('fieldset');
256
257         $this->out->elementStart(
258             'fieldset',
259             array('id' => 'settings_license-details')
260         );
261         $this->out->element('legend', null, _('License details'));
262         $this->out->elementStart('ul', 'form_data');
263
264         $this->li();
265         $this->input(
266             'owner',
267             _('Owner'),
268             _('Name of the owner of the site\'s content (if applicable).'),
269             'license'
270         );
271         $this->unli();
272
273         $this->li();
274         $this->input(
275             'title',
276             _('License Title'),
277             _('The title of the license.'),
278             'license'
279         );
280         $this->unli();
281
282         $this->li();
283         $this->input(
284             'url',
285             _('License URL'),
286             _('URL for more information about the license.'),
287             'license'
288         );
289         $this->unli();
290
291         $this->li();
292         $this->input(
293             'image', _('License Image URL'),
294             _('URL for an image to display with the license.'),
295             'license'
296         );
297         $this->unli();
298
299         $this->out->elementEnd('ul');
300         $this->out->elementEnd('fieldset');
301     }
302
303     /**
304      * Action elements
305      *
306      * @return void
307      */
308     function formActions()
309     {
310         $this->out->submit(
311             'submit', _('Save'), 'submit', null, _('Save license settings')
312         );
313     }
314 }