]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/designsettings.php
Merge branch '1.0.x' into feedsub-wizard
[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 /**
36  * Base class for setting a user or group design
37  *
38  * Shows the design setting form and also handles some things like saving
39  * background images, and fetching a default design
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @author   Sarven Capadisli <csarven@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class DesignSettingsAction extends SettingsAction
50 {
51     var $submitaction = null;
52
53     /**
54      * Title of the page
55      *
56      * @return string Title of the page
57      */
58     function title()
59     {
60         // TRANS: Page title for profile design page.
61         return _('Profile design');
62     }
63
64     /**
65      * Instructions for use
66      *
67      * @return instructions for use
68      */
69     function getInstructions()
70     {
71         // TRANS: Instructions for profile design page.
72         return _('Customize the way your profile looks ' .
73         'with a background image and a colour palette of your choice.');
74     }
75
76     /**
77      * Shows the design settings form
78      *
79      * @param Design $design a working design to show
80      *
81      * @return nothing
82      */
83     function showDesignForm($design)
84     {
85         $form = new DesignForm($this, $design, $this->selfUrl());
86         $form->show();
87
88     }
89
90     /**
91      * Handle a post
92      *
93      * Validate input and save changes. Reload the form with a success
94      * or error message.
95      *
96      * @return void
97      */
98     function handlePost()
99     {
100         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
101
102             // Workaround for PHP returning empty $_POST and $_FILES when POST
103             // length > post_max_size in php.ini
104
105             if (empty($_FILES)
106                 && empty($_POST)
107                 && ($_SERVER['CONTENT_LENGTH'] > 0)
108             ) {
109                 // TRANS: Form validation error in design settings form. POST should remain untranslated.
110                 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
111                           'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
112                           intval($_SERVER['CONTENT_LENGTH']));
113
114                 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
115                 return;
116             }
117         }
118
119         // CSRF protection
120         $token = $this->trimmed('token');
121         if (!$token || $token != common_session_token()) {
122             $this->showForm(_('There was a problem with your session token. '.
123                               'Try again, please.'));
124             return;
125         }
126
127         if ($this->arg('save')) {
128             $this->saveDesign();
129         } else if ($this->arg('defaults')) {
130             $this->restoreDefaults();
131         } else {
132             // TRANS: Unknown form validation error in design settings form.
133             $this->showForm(_('Unexpected form submission.'));
134         }
135     }
136
137     /**
138      * Add the Farbtastic stylesheet
139      *
140      * @return void
141      */
142     function showStylesheets()
143     {
144         parent::showStylesheets();
145         $this->cssLink('js/farbtastic/farbtastic.css',null,'screen, projection, tv');
146     }
147
148     /**
149      * Add the Farbtastic scripts
150      *
151      * @return void
152      */
153     function showScripts()
154     {
155         parent::showScripts();
156
157         $this->script('farbtastic/farbtastic.js');
158         $this->script('userdesign.go.js');
159
160         $this->autofocus('design_background-image_file');
161     }
162
163     /**
164      * Save the background image, if any, and set its disposition
165      *
166      * @param Design $design a working design to attach the img to
167      *
168      * @return nothing
169      */
170     function saveBackgroundImage($design)
171     {
172         // Now that we have a Design ID we can add a file to the design.
173         // XXX: This is an additional DB hit, but figured having the image
174         // associated with the Design rather than the User was worth
175         // it. -- Zach
176
177         if (array_key_exists('design_background-image_file', $_FILES) &&
178           $_FILES['design_background-image_file']['error'] == UPLOAD_ERR_OK) {
179
180             $filepath = null;
181
182             try {
183                 $imagefile = ImageFile::fromUpload('design_background-image_file');
184             } catch (Exception $e) {
185                 $this->showForm($e->getMessage());
186                 return;
187             }
188
189             $filename = Design::filename($design->id,
190                                          image_type_to_extension($imagefile->type),
191                                          common_timestamp());
192
193             $filepath = Design::path($filename);
194
195             move_uploaded_file($imagefile->filepath, $filepath);
196
197             // delete any old backround img laying around
198
199             if (isset($design->backgroundimage)) {
200                 @unlink(Design::path($design->backgroundimage));
201             }
202
203             $original = clone($design);
204
205             $design->backgroundimage = $filename;
206
207             // default to on, no tile
208
209             $design->setDisposition(true, false, false);
210
211             $result = $design->update($original);
212
213             if ($result === false) {
214                 common_log_db_error($design, 'UPDATE', __FILE__);
215                 // TRANS: Error message displayed if design settings could not be saved.
216                 $this->showForm(_('Couldn\'t update your design.'));
217                 return;
218             }
219         }
220     }
221
222     /**
223      * Restore the user or group design to system defaults
224      *
225      * @return nothing
226      */
227     function restoreDefaults()
228     {
229         $design = $this->getWorkingDesign();
230
231         if (!empty($design)) {
232
233             $result = $design->delete();
234
235             if ($result === false) {
236                 common_log_db_error($design, 'DELETE', __FILE__);
237                 // TRANS: Error message displayed if design settings could not be saved after clicking "Use defaults".
238                 $this->showForm(_('Couldn\'t update your design.'));
239                 return;
240             }
241         }
242
243         // TRANS: Success message displayed if design settings were saved after clicking "Use defaults".
244         $this->showForm(_('Design defaults restored.'), true);
245     }
246 }