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