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