]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/designsettings.php
5def5053fdc7fa17a834f67cfb33ccb4d2f63f2a
[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             $this->elementEnd('li');
216
217         } catch (WebColorException $e) {
218             common_log(LOG_ERR, 'Bad color values in design ID: ' .$design->id);
219         }
220
221         $this->elementEnd('ul');
222         $this->elementEnd('fieldset');
223
224         $this->submit('defaults', _('Use defaults'), 'submit form_action-default',
225             'defaults', _('Restore default designs'));
226
227         $this->element('input', array('id' => 'settings_design_reset',
228                                      'type' => 'reset',
229                                      'value' => 'Reset',
230                                      'class' => 'submit form_action-primary',
231                                      'title' => _('Reset back to default')));
232
233         $this->submit('save', _('Save'), 'submit form_action-secondary',
234             'save', _('Save design'));
235
236         $this->elementEnd('fieldset');
237         $this->elementEnd('form');
238     }
239
240     /**
241      * Handle a post
242      *
243      * Validate input and save changes. Reload the form with a success
244      * or error message.
245      *
246      * @return void
247      */
248
249     function handlePost()
250     {
251         // XXX: Robin's workaround for a bug in PHP where $_POST
252         // and $_FILE are empty in the case that the uploaded
253         // file is bigger than PHP is configured to handle.
254
255         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
256             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
257
258                 $msg = _('The server was unable to handle that much POST ' .
259                     'data (%s bytes) due to its current configuration.');
260
261                 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
262             }
263         }
264
265         // CSRF protection
266         $token = $this->trimmed('token');
267         if (!$token || $token != common_session_token()) {
268             $this->showForm(_('There was a problem with your session token. '.
269                               'Try again, please.'));
270             return;
271         }
272
273         if ($this->arg('save')) {
274             $this->saveDesign();
275         } else if ($this->arg('reset')) {
276             $this->resetDesign();
277         } else {
278             $this->showForm(_('Unexpected form submission.'));
279         }
280     }
281
282     /**
283      * Add the Farbtastic stylesheet
284      *
285      * @return void
286      */
287
288     function showStylesheets()
289     {
290         parent::showStylesheets();
291         $farbtasticStyle =
292           common_path('theme/base/css/farbtastic.css?version='.LACONICA_VERSION);
293
294         $this->element('link', array('rel' => 'stylesheet',
295                                      'type' => 'text/css',
296                                      'href' => $farbtasticStyle,
297                                      'media' => 'screen, projection, tv'));
298     }
299
300     /**
301      * Add the Farbtastic scripts
302      *
303      * @return void
304      */
305
306     function showScripts()
307     {
308         parent::showScripts();
309
310         $farbtasticPack = common_path('js/farbtastic/farbtastic.js');
311         $userDesignGo   = common_path('js/userdesign.go.js');
312
313         $this->element('script', array('type' => 'text/javascript',
314                                        'src' => $farbtasticPack));
315         $this->element('script', array('type' => 'text/javascript',
316                                        'src' => $userDesignGo));
317     }
318
319     /**
320      * Get a default user design
321      *
322      * @return Design design
323      */
324
325     function defaultDesign()
326     {
327         $defaults = common_config('site', 'design');
328
329         $design = new Design();
330
331         try {
332
333             $color = new WebColor();
334
335             $color->parseColor($defaults['backgroundcolor']);
336             $design->backgroundcolor = $color->intValue();
337
338             $color->parseColor($defaults['contentcolor']);
339             $design->contentcolor = $color->intValue();
340
341             $color->parseColor($defaults['sidebarcolor']);
342             $design->sidebarcolor = $color->intValue();
343
344             $color->parseColor($defaults['textcolor']);
345             $design->textcolor = $color->intValue();
346
347             $color->parseColor($defaults['linkcolor']);
348             $design->linkcolor = $color->intValue();
349
350             $design->backgroundimage = $defaults['backgroundimage'];
351
352             $design->disposition = $defaults['disposition'];
353
354         } catch (WebColorException $e) {
355             common_log(LOG_ERR, _('Bad default color settings: ' .
356                 $e->getMessage()));
357         }
358
359         return $design;
360     }
361
362     function saveBackgroundImage($design) {
363
364         // Now that we have a Design ID we can add a file to the design.
365         // XXX: This is an additional DB hit, but figured having the image
366         // associated with the Design rather than the User was worth
367         // it. -- Zach
368
369         if ($_FILES['design_background-image_file']['error'] ==
370             UPLOAD_ERR_OK) {
371
372             $filepath = null;
373
374             try {
375                     $imagefile =
376                         ImageFile::fromUpload('design_background-image_file');
377                 } catch (Exception $e) {
378                     $this->showForm($e->getMessage());
379                     return;
380                 }
381
382             $filename = Design::filename($design->id,
383                 image_type_to_extension($imagefile->type),
384                     common_timestamp());
385
386             $filepath = Design::path($filename);
387
388             move_uploaded_file($imagefile->filepath, $filepath);
389
390             $original = clone($design);
391             $design->backgroundimage = $filename;
392
393             // default to on, no tile
394
395             $design->setDisposition(true, false, false);
396
397             $result = $design->update($original);
398
399             if ($result === false) {
400                 common_log_db_error($design, 'UPDATE', __FILE__);
401                 $this->showForm(_('Couldn\'t update your design.'));
402                 return;
403             }
404         }
405     }
406
407 }