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