]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/designsettings.php
9650679ac5b620c51f356c1d57d07e3235abfbac
[quix0rs-gnu-social.git] / lib / 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     var $submitaction = null;
42
43     /**
44      * Title of the page
45      *
46      * @return string Title of the page
47      */
48
49     function title()
50     {
51         return _('Profile design');
52     }
53
54     /**
55      * Instructions for use
56      *
57      * @return instructions for use
58      */
59
60     function getInstructions()
61     {
62         return _('Customize the way your profile looks ' .
63         'with a background image and a colour palette of your choice.');
64     }
65
66     function showDesignForm($design)
67     {
68
69         $this->elementStart('form', array('method' => 'post',
70                                           'enctype' => 'multipart/form-data',
71                                           'id' => 'form_settings_design',
72                                           'class' => 'form_settings',
73                                           'action' => $this->submitaction));
74         $this->elementStart('fieldset');
75         $this->hidden('token', common_session_token());
76
77         $this->elementStart('fieldset', array('id' =>
78             'settings_design_background-image'));
79         $this->element('legend', null, _('Change background image'));
80         $this->elementStart('ul', 'form_data');
81         $this->elementStart('li');
82         $this->element('label', array('for' => 'design_background-image_file'),
83                                 _('Upload file'));
84         $this->element('input', array('name' => 'design_background-image_file',
85                                       'type' => 'file',
86                                       'id' => 'design_background-image_file'));
87         $this->element('p', 'form_guide', _('You can upload your personal ' .
88             'background image. The maximum file size is 2Mb.'));
89         $this->element('input', array('name' => 'MAX_FILE_SIZE',
90                                       'type' => 'hidden',
91                                       'id' => 'MAX_FILE_SIZE',
92                                       'value' => ImageFile::maxFileSizeInt()));
93         $this->elementEnd('li');
94
95         if (!empty($design->backgroundimage)) {
96
97             $this->elementStart('li', array('id' => 'design_background-image_onoff'));
98
99             $this->element('img', array('src' =>
100                 Design::url($design->backgroundimage)));
101
102             $attrs = array('name' => 'design_background-image_onoff',
103                            'type' => 'radio',
104                            'id' => 'design_background-image_on',
105                            'class' => 'radio',
106                            'value' => 'on');
107
108             if ($design->disposition & BACKGROUND_ON) {
109                 $attrs['checked'] = 'checked';
110             }
111
112             $this->element('input', $attrs);
113
114             $this->element('label', array('for' => 'design_background-image_on',
115                                           'class' => 'radio'),
116                                           _('On'));
117
118             $attrs = array('name' => 'design_background-image_onoff',
119                            'type' => 'radio',
120                            'id' => 'design_background-image_off',
121                            'class' => 'radio',
122                            'value' => 'off');
123
124             if ($design->disposition & BACKGROUND_OFF) {
125                 $attrs['checked'] = 'checked';
126             }
127
128             $this->element('input', $attrs);
129
130             $this->element('label', array('for' => 'design_background-image_off',
131                                           'class' => 'radio'),
132                                           _('Off'));
133             $this->element('p', 'form_guide', _('Turn background image on or off.'));
134             $this->elementEnd('li');
135
136             $this->elementStart('li');
137             $this->checkbox('design_background-image_repeat',
138                             _('Tile background image'),
139                             ($design->disposition & BACKGROUND_TILE) ? true : false );
140             $this->elementEnd('li');
141         }
142
143         $this->elementEnd('ul');
144         $this->elementEnd('fieldset');
145
146         $this->elementStart('fieldset', array('id' => 'settings_design_color'));
147         $this->element('legend', null, _('Change colours'));
148         $this->elementStart('ul', 'form_data');
149
150         try {
151
152             $bgcolor = new WebColor($design->backgroundcolor);
153
154             $this->elementStart('li');
155             $this->element('label', array('for' => 'swatch-1'), _('Background'));
156             $this->element('input', array('name' => 'design_background',
157                                           'type' => 'text',
158                                           'id' => 'swatch-1',
159                                           'class' => 'swatch',
160                                           'maxlength' => '7',
161                                           'size' => '7',
162                                           'value' => '#' . $bgcolor->hexValue()));
163             $this->elementEnd('li');
164
165             $ccolor = new WebColor($design->contentcolor);
166
167             $this->elementStart('li');
168             $this->element('label', array('for' => 'swatch-2'), _('Content'));
169             $this->element('input', array('name' => 'design_content',
170                                           'type' => 'text',
171                                           'id' => 'swatch-2',
172                                           'class' => 'swatch',
173                                           'maxlength' => '7',
174                                           'size' => '7',
175                                           'value' => '#' . $ccolor->hexValue()));
176             $this->elementEnd('li');
177
178             $sbcolor = new WebColor($design->sidebarcolor);
179
180             $this->elementStart('li');
181             $this->element('label', array('for' => 'swatch-3'), _('Sidebar'));
182             $this->element('input', array('name' => 'design_sidebar',
183                                         'type' => 'text',
184                                         'id' => 'swatch-3',
185                                         'class' => 'swatch',
186                                         'maxlength' => '7',
187                                         'size' => '7',
188                                         'value' => '#' . $sbcolor->hexValue()));
189             $this->elementEnd('li');
190
191             $tcolor = new WebColor($design->textcolor);
192
193             $this->elementStart('li');
194             $this->element('label', array('for' => 'swatch-4'), _('Text'));
195             $this->element('input', array('name' => 'design_text',
196                                         'type' => 'text',
197                                         'id' => 'swatch-4',
198                                         'class' => 'swatch',
199                                         'maxlength' => '7',
200                                         'size' => '7',
201                                         'value' => '#' . $tcolor->hexValue()));
202             $this->elementEnd('li');
203
204             $lcolor = new WebColor($design->linkcolor);
205
206             $this->elementStart('li');
207             $this->element('label', array('for' => 'swatch-5'), _('Links'));
208             $this->element('input', array('name' => 'design_links',
209                                          'type' => 'text',
210                                          'id' => 'swatch-5',
211                                          'class' => 'swatch',
212                                          'maxlength' => '7',
213                                          'size' => '7',
214                                          'value' => '#' . $lcolor->hexValue()));
215
216            $this->elementEnd('li');
217
218        } catch (WebColorException $e) {
219            common_log(LOG_ERR, 'Bad color values in design ID: ' .
220                $design->id);
221        }
222
223        $this->elementEnd('ul');
224        $this->elementEnd('fieldset');
225
226        $this->element('input', array('id' => 'settings_design_reset',
227                                      'type' => 'reset',
228                                      'value' => 'Reset',
229                                      'class' => 'submit form_action-primary',
230                                      'title' => _('Reset back to default')));
231
232         $this->submit('save', _('Save'), 'submit form_action-secondary',
233             'save', _('Save design'));
234
235         $this->elementEnd('fieldset');
236         $this->elementEnd('form');
237     }
238
239     /**
240      * Handle a post
241      *
242      * Validate input and save changes. Reload the form with a success
243      * or error message.
244      *
245      * @return void
246      */
247
248     function handlePost()
249     {
250         // XXX: Robin's workaround for a bug in PHP where $_POST
251         // and $_FILE are empty in the case that the uploaded
252         // file is bigger than PHP is configured to handle.
253
254         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
255             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
256
257                 $msg = _('The server was unable to handle that much POST ' .
258                     'data (%s bytes) due to its current configuration.');
259
260                 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
261             }
262         }
263
264         // CSRF protection
265         $token = $this->trimmed('token');
266         if (!$token || $token != common_session_token()) {
267             $this->showForm(_('There was a problem with your session token. '.
268                               'Try again, please.'));
269             return;
270         }
271
272         if ($this->arg('save')) {
273             $this->saveDesign();
274         } else if ($this->arg('reset')) {
275             $this->resetDesign();
276         } else {
277             $this->showForm(_('Unexpected form submission.'));
278         }
279     }
280
281     /**
282      * Add the Farbtastic stylesheet
283      *
284      * @return void
285      */
286
287     function showStylesheets()
288     {
289         parent::showStylesheets();
290         $farbtasticStyle =
291           common_path('theme/base/css/farbtastic.css?version='.LACONICA_VERSION);
292
293         $this->element('link', array('rel' => 'stylesheet',
294                                      'type' => 'text/css',
295                                      'href' => $farbtasticStyle,
296                                      'media' => 'screen, projection, tv'));
297     }
298
299     /**
300      * Add the Farbtastic scripts
301      *
302      * @return void
303      */
304
305     function showScripts()
306     {
307         parent::showScripts();
308
309         $farbtasticPack = common_path('js/farbtastic/farbtastic.js');
310         $userDesignGo   = common_path('js/userdesign.go.js');
311
312         $this->element('script', array('type' => 'text/javascript',
313                                        'src' => $farbtasticPack));
314         $this->element('script', array('type' => 'text/javascript',
315                                        'src' => $userDesignGo));
316     }
317
318     /**
319      * Get a default user design
320      *
321      * @return Design design
322      */
323
324     function defaultDesign()
325     {
326         $defaults = common_config('site', 'design');
327
328         $design = new Design();
329
330         try {
331
332             $color = new WebColor();
333
334             $color->parseColor($defaults['backgroundcolor']);
335             $design->backgroundcolor = $color->intValue();
336
337             $color->parseColor($defaults['contentcolor']);
338             $design->contentcolor = $color->intValue();
339
340             $color->parseColor($defaults['sidebarcolor']);
341             $design->sidebarcolor = $color->intValue();
342
343             $color->parseColor($defaults['textcolor']);
344             $design->textcolor = $color->intValue();
345
346             $color->parseColor($defaults['linkcolor']);
347             $design->linkcolor = $color->intValue();
348
349             $design->backgroundimage = $defaults['backgroundimage'];
350
351             $design->disposition = $defaults['disposition'];
352
353         } catch (WebColorException $e) {
354             common_log(LOG_ERR, _('Bad default color settings: ' .
355                 $e->getMessage()));
356         }
357
358         return $design;
359     }
360
361     function saveBackgroundImage($design) {
362
363         // Now that we have a Design ID we can add a file to the design.
364         // XXX: This is an additional DB hit, but figured having the image
365         // associated with the Design rather than the User was worth
366         // it. -- Zach
367
368         if ($_FILES['design_background-image_file']['error'] ==
369             UPLOAD_ERR_OK) {
370
371             $filepath = null;
372
373             try {
374                     $imagefile =
375                         ImageFile::fromUpload('design_background-image_file');
376                 } catch (Exception $e) {
377                     $this->showForm($e->getMessage());
378                     return;
379                 }
380
381             $filename = Design::filename($design->id,
382                 image_type_to_extension($imagefile->type),
383                     common_timestamp());
384
385             $filepath = Design::path($filename);
386
387             move_uploaded_file($imagefile->filepath, $filepath);
388
389             $original = clone($design);
390             $design->backgroundimage = $filename;
391
392             // default to on, no tile
393
394             $design->setDisposition(true, false, false);
395
396             $result = $design->update($original);
397
398             if ($result === false) {
399                 common_log_db_error($design, 'UPDATE', __FILE__);
400                 $this->showForm(_('Couldn\'t update your design.'));
401                 return;
402             }
403         }
404     }
405
406 }