]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/designsettings.php
5ce9ddedadf5529c516e94f372d06024f242b67c
[quix0rs-gnu-social.git] / lib / designsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Change user password
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    Sarven Capadisli <csarven@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR . '/lib/accountsettingsaction.php';
36 require_once INSTALLDIR . '/lib/webcolor.php';
37
38 /**
39  * Base class for setting a user or group design
40  *
41  * Shows the design setting form and also handles some things like saving
42  * background images, and fetching a default design
43  *
44  * @category Settings
45  * @package  StatusNet
46  * @author   Zach Copley <zach@status.net>
47  * @author   Sarven Capadisli <csarven@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  */
51
52 class DesignSettingsAction extends AccountSettingsAction
53 {
54
55     var $submitaction = null;
56
57     /**
58      * Title of the page
59      *
60      * @return string Title of the page
61      */
62
63     function title()
64     {
65         return _('Profile design');
66     }
67
68     /**
69      * Instructions for use
70      *
71      * @return instructions for use
72      */
73
74     function getInstructions()
75     {
76         return _('Customize the way your profile looks ' .
77         'with a background image and a colour palette of your choice.');
78     }
79
80     /**
81      * Shows the design settings form
82      *
83      * @param Design $design a working design to show
84      *
85      * @return nothing
86      */
87
88     function showDesignForm($design)
89     {
90
91         $this->elementStart('form', array('method' => 'post',
92                                           'enctype' => 'multipart/form-data',
93                                           'id' => 'form_settings_design',
94                                           'class' => 'form_settings',
95                                           'action' => $this->submitaction));
96         $this->elementStart('fieldset');
97         $this->hidden('token', common_session_token());
98
99         $this->elementStart('fieldset', array('id' =>
100             'settings_design_background-image'));
101         $this->element('legend', null, _('Change background image'));
102         $this->elementStart('ul', 'form_data');
103         $this->elementStart('li');
104         $this->element('label', array('for' => 'design_background-image_file'),
105                                 _('Upload file'));
106         $this->element('input', array('name' => 'design_background-image_file',
107                                       'type' => 'file',
108                                       'id' => 'design_background-image_file'));
109         $this->element('p', 'form_guide', _('You can upload your personal ' .
110             'background image. The maximum file size is 2Mb.'));
111         $this->element('input', array('name' => 'MAX_FILE_SIZE',
112                                       'type' => 'hidden',
113                                       'id' => 'MAX_FILE_SIZE',
114                                       'value' => ImageFile::maxFileSizeInt()));
115         $this->elementEnd('li');
116
117         if (!empty($design->backgroundimage)) {
118
119             $this->elementStart('li', array('id' =>
120                 'design_background-image_onoff'));
121
122             $this->element('img', array('src' =>
123                 Design::url($design->backgroundimage)));
124
125             $attrs = array('name' => 'design_background-image_onoff',
126                            'type' => 'radio',
127                            'id' => 'design_background-image_on',
128                            'class' => 'radio',
129                            'value' => 'on');
130
131             if ($design->disposition & BACKGROUND_ON) {
132                 $attrs['checked'] = 'checked';
133             }
134
135             $this->element('input', $attrs);
136
137             $this->element('label', array('for' => 'design_background-image_on',
138                                           'class' => 'radio'),
139                                           _('On'));
140
141             $attrs = array('name' => 'design_background-image_onoff',
142                            'type' => 'radio',
143                            'id' => 'design_background-image_off',
144                            'class' => 'radio',
145                            'value' => 'off');
146
147             if ($design->disposition & BACKGROUND_OFF) {
148                 $attrs['checked'] = 'checked';
149             }
150
151             $this->element('input', $attrs);
152
153             $this->element('label', array('for' => 'design_background-image_off',
154                                           'class' => 'radio'),
155                                           _('Off'));
156             $this->element('p', 'form_guide', _('Turn background image on or off.'));
157             $this->elementEnd('li');
158
159             $this->elementStart('li');
160             $this->checkbox('design_background-image_repeat',
161                             _('Tile background image'),
162                             ($design->disposition & BACKGROUND_TILE) ? true : false);
163             $this->elementEnd('li');
164         }
165
166         $this->elementEnd('ul');
167         $this->elementEnd('fieldset');
168
169         $this->elementStart('fieldset', array('id' => 'settings_design_color'));
170         $this->element('legend', null, _('Change colours'));
171         $this->elementStart('ul', 'form_data');
172
173         try {
174
175             $bgcolor = new WebColor($design->backgroundcolor);
176
177             $this->elementStart('li');
178             $this->element('label', array('for' => 'swatch-1'), _('Background'));
179             $this->element('input', array('name' => 'design_background',
180                                           'type' => 'text',
181                                           'id' => 'swatch-1',
182                                           'class' => 'swatch',
183                                           'maxlength' => '7',
184                                           'size' => '7',
185                                           'value' => ''));
186             $this->elementEnd('li');
187
188             $ccolor = new WebColor($design->contentcolor);
189
190             $this->elementStart('li');
191             $this->element('label', array('for' => 'swatch-2'), _('Content'));
192             $this->element('input', array('name' => 'design_content',
193                                           'type' => 'text',
194                                           'id' => 'swatch-2',
195                                           'class' => 'swatch',
196                                           'maxlength' => '7',
197                                           'size' => '7',
198                                           'value' => ''));
199             $this->elementEnd('li');
200
201             $sbcolor = new WebColor($design->sidebarcolor);
202
203             $this->elementStart('li');
204             $this->element('label', array('for' => 'swatch-3'), _('Sidebar'));
205             $this->element('input', array('name' => 'design_sidebar',
206                                         'type' => 'text',
207                                         'id' => 'swatch-3',
208                                         'class' => 'swatch',
209                                         'maxlength' => '7',
210                                         'size' => '7',
211                                         'value' => ''));
212             $this->elementEnd('li');
213
214             $tcolor = new WebColor($design->textcolor);
215
216             $this->elementStart('li');
217             $this->element('label', array('for' => 'swatch-4'), _('Text'));
218             $this->element('input', array('name' => 'design_text',
219                                         'type' => 'text',
220                                         'id' => 'swatch-4',
221                                         'class' => 'swatch',
222                                         'maxlength' => '7',
223                                         'size' => '7',
224                                         'value' => ''));
225             $this->elementEnd('li');
226
227             $lcolor = new WebColor($design->linkcolor);
228
229             $this->elementStart('li');
230             $this->element('label', array('for' => 'swatch-5'), _('Links'));
231             $this->element('input', array('name' => 'design_links',
232                                          'type' => 'text',
233                                          'id' => 'swatch-5',
234                                          'class' => 'swatch',
235                                          'maxlength' => '7',
236                                          'size' => '7',
237                                          'value' => ''));
238             $this->elementEnd('li');
239
240         } catch (WebColorException $e) {
241             common_log(LOG_ERR, 'Bad color values in design ID: ' .$design->id);
242         }
243
244         $this->elementEnd('ul');
245         $this->elementEnd('fieldset');
246
247         $this->submit('defaults', _('Use defaults'), 'submit form_action-default',
248             'defaults', _('Restore default designs'));
249
250         $this->element('input', array('id' => 'settings_design_reset',
251                                      'type' => 'reset',
252                                      'value' => 'Reset',
253                                      'class' => 'submit form_action-primary',
254                                      'title' => _('Reset back to default')));
255
256         $this->submit('save', _('Save'), 'submit form_action-secondary',
257             'save', _('Save design'));
258
259         $this->elementEnd('fieldset');
260         $this->elementEnd('form');
261     }
262
263     /**
264      * Handle a post
265      *
266      * Validate input and save changes. Reload the form with a success
267      * or error message.
268      *
269      * @return void
270      */
271
272     function handlePost()
273     {
274         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
275
276             // Workaround for PHP returning empty $_POST and $_FILES when POST
277             // length > post_max_size in php.ini
278
279             if (empty($_FILES)
280                 && empty($_POST)
281                 && ($_SERVER['CONTENT_LENGTH'] > 0)
282             ) {
283                 $msg = _('The server was unable to handle that much POST ' .
284                     'data (%s bytes) due to its current configuration.');
285
286                 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
287                 return;
288             }
289         }
290
291         // CSRF protection
292         $token = $this->trimmed('token');
293         if (!$token || $token != common_session_token()) {
294             $this->showForm(_('There was a problem with your session token. '.
295                               'Try again, please.'));
296             return;
297         }
298
299         if ($this->arg('save')) {
300             $this->saveDesign();
301         } else if ($this->arg('defaults')) {
302             $this->restoreDefaults();
303         } else {
304             $this->showForm(_('Unexpected form submission.'));
305         }
306     }
307
308     /**
309      * Add the Farbtastic stylesheet
310      *
311      * @return void
312      */
313
314     function showStylesheets()
315     {
316         parent::showStylesheets();
317         $this->cssLink('css/farbtastic.css','base','screen, projection, tv');
318     }
319
320     /**
321      * Add the Farbtastic scripts
322      *
323      * @return void
324      */
325
326     function showScripts()
327     {
328         parent::showScripts();
329
330         $this->script('js/farbtastic/farbtastic.js');
331         $this->script('js/userdesign.go.js');
332
333         $this->autofocus('design_background-image_file');
334     }
335
336     /**
337      * Get a default design
338      *
339      * @return Design design
340      */
341
342     function defaultDesign()
343     {
344         $defaults = common_config('site', 'design');
345
346         $design = new Design();
347
348         try {
349
350             $color = new WebColor();
351
352             $color->parseColor($defaults['backgroundcolor']);
353             $design->backgroundcolor = $color->intValue();
354
355             $color->parseColor($defaults['contentcolor']);
356             $design->contentcolor = $color->intValue();
357
358             $color->parseColor($defaults['sidebarcolor']);
359             $design->sidebarcolor = $color->intValue();
360
361             $color->parseColor($defaults['textcolor']);
362             $design->textcolor = $color->intValue();
363
364             $color->parseColor($defaults['linkcolor']);
365             $design->linkcolor = $color->intValue();
366
367             $design->backgroundimage = $defaults['backgroundimage'];
368
369             $design->disposition = $defaults['disposition'];
370
371         } catch (WebColorException $e) {
372             common_log(LOG_ERR, _('Bad default color settings: ' .
373                 $e->getMessage()));
374         }
375
376         return $design;
377     }
378
379     /**
380      * Save the background image, if any, and set its disposition
381      *
382      * @param Design $design a working design to attach the img to
383      *
384      * @return nothing
385      */
386
387     function saveBackgroundImage($design)
388     {
389
390         // Now that we have a Design ID we can add a file to the design.
391         // XXX: This is an additional DB hit, but figured having the image
392         // associated with the Design rather than the User was worth
393         // it. -- Zach
394
395         if ($_FILES['design_background-image_file']['error'] ==
396             UPLOAD_ERR_OK) {
397
398             $filepath = null;
399
400             try {
401                 $imagefile =
402                     ImageFile::fromUpload('design_background-image_file');
403             } catch (Exception $e) {
404                 $this->showForm($e->getMessage());
405                 return;
406             }
407
408             $filename = Design::filename($design->id,
409                 image_type_to_extension($imagefile->type),
410                     common_timestamp());
411
412             $filepath = Design::path($filename);
413
414             move_uploaded_file($imagefile->filepath, $filepath);
415
416             // delete any old backround img laying around
417
418             if (isset($design->backgroundimage)) {
419                 @unlink(Design::path($design->backgroundimage));
420             }
421
422             $original = clone($design);
423
424             $design->backgroundimage = $filename;
425
426             // default to on, no tile
427
428             $design->setDisposition(true, false, false);
429
430             $result = $design->update($original);
431
432             if ($result === false) {
433                 common_log_db_error($design, 'UPDATE', __FILE__);
434                 $this->showForm(_('Couldn\'t update your design.'));
435                 return;
436             }
437         }
438     }
439
440     /**
441      * Restore the user or group design to system defaults
442      *
443      * @return nothing
444      */
445
446     function restoreDefaults()
447     {
448         $design   = $this->getWorkingDesign();
449         $default  = $this->defaultDesign();
450         $original = clone($design);
451
452         $design->backgroundcolor = $default->backgroundcolor;
453         $design->contentcolor    = $default->contentcolor;
454         $design->sidebarcolor    = $default->sidebarcolor;
455         $design->textcolor       = $default->textcolor;
456         $design->linkcolor       = $default->linkcolor;
457
458         $design->setDisposition(false, true, false);
459
460         $result = $design->update($original);
461
462         if ($result === false) {
463             common_log_db_error($design, 'UPDATE', __FILE__);
464             $this->showForm(_('Couldn\'t update your design.'));
465             return;
466         }
467
468         $this->showForm(_('Design defaults restored.'), true);
469     }
470
471 }