]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/urlsettings.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / actions / urlsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Miscellaneous settings
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    Robin Millette <millette@status.net>
25  * @author    Evan Prodromou <evan@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
37 /**
38  * Miscellaneous settings actions
39  *
40  * Currently this just manages URL shortening.
41  *
42  * @category Settings
43  * @package  StatusNet
44  * @author   Robin Millette <millette@status.net>
45  * @author   Zach Copley <zach@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49
50 class UrlsettingsAction extends SettingsAction
51 {
52     /**
53      * Title of the page
54      *
55      * @return string Title of the page
56      */
57
58     function title()
59     {
60         return _('URL settings');
61     }
62
63     /**
64      * Instructions for use
65      *
66      * @return instructions for use
67      */
68
69     function getInstructions()
70     {
71         // TRANS: Instructions for tab "Other" in user profile settings.
72         return _('Manage various other options.');
73     }
74
75     function showScripts()
76     {
77         parent::showScripts();
78         $this->autofocus('urlshorteningservice');
79     }
80
81     /**
82      * Content area of the page
83      *
84      * Shows a form for uploading an avatar.
85      *
86      * @return void
87      */
88
89     function showContent()
90     {
91         $user = common_current_user();
92
93         $this->elementStart('form', array('method' => 'post',
94                                           'id' => 'form_settings_other',
95                                           'class' => 'form_settings',
96                                           'action' =>
97                                           common_local_url('urlsettings')));
98         $this->elementStart('fieldset');
99         $this->hidden('token', common_session_token());
100         $this->elementStart('ul', 'form_data');
101
102         Event::handle('GetUrlShorteners', array(&$shorteners));
103
104         $services = array();
105
106         foreach ($shorteners as $name => $value)
107         {
108             $services[$name] = $name;
109             if ($value['freeService']) {
110                 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
111                 // TRANS: user's profile settings. This message has one space at the beginning. Use your
112                 // TRANS: language's word separator here if it has one (most likely a single space).
113                 $services[$name] .= _(' (free service)');
114             }
115         }
116
117         // Include default values
118
119         $services['none']     = _('[none]');
120         $services['internal'] = _('[internal]');
121
122         if ($services) {
123
124             asort($services);
125
126             $this->elementStart('li');
127             // TRANS: Label for dropdown with URL shortener services.
128             $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
129                             // TRANS: Tooltip for for dropdown with URL shortener services.
130                             $services, _('Automatic shortening service to use.'),
131                             false, $user->urlshorteningservice);
132             $this->elementEnd('li');
133         }
134         $this->elementStart('li');
135         $this->input('maxurllength',
136                      _('URL longer than'),
137                      (!is_null($this->arg('maxurllength'))) ?
138                      $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
139                      _('URLs longer than this will be shortened, 0 means always shorten.'));
140         $this->elementEnd('li');
141         $this->elementStart('li');
142         $this->input('maxnoticelength',
143                      _('Text longer than'),
144                      (!is_null($this->arg('maxnoticelength'))) ?
145                      $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
146                      _('URLs in notices longer than this will be shortened, 0 means always shorten.'));
147         $this->elementEnd('li');
148         $this->elementEnd('ul');
149         // TRANS: Button text for saving "Other settings" in profile.
150         $this->submit('save', _m('BUTTON','Save'));
151         $this->elementEnd('fieldset');
152         $this->elementEnd('form');
153     }
154
155     /**
156      * Handle a post
157      *
158      * Saves the changes to url-shortening prefs and shows a success or failure
159      * message.
160      *
161      * @return void
162      */
163
164     function handlePost()
165     {
166         // CSRF protection
167         $token = $this->trimmed('token');
168         if (!$token || $token != common_session_token()) {
169             $this->showForm(_('There was a problem with your session token. '.
170                               'Try again, please.'));
171             return;
172         }
173
174         $urlshorteningservice = $this->trimmed('urlshorteningservice');
175
176         if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
177             // TRANS: Form validation error for form "Other settings" in user profile.
178             $this->showForm(_('URL shortening service is too long (maximum 50 characters).'));
179             return;
180         }
181
182         $maxurllength = $this->trimmed('maxurllength');
183
184         if (!Validate::number($maxurllength, array('min' => 0))) {
185             throw new ClientException(_('Invalid number for max url length.'));
186         }
187
188         $maxnoticelength = $this->trimmed('maxnoticelength');
189
190         if (!Validate::number($maxnoticelength, array('min' => 0))) {
191             throw new ClientException(_('Invalid number for max notice length.'));
192         }
193
194         $user = common_current_user();
195
196         assert(!is_null($user)); // should already be checked
197
198         $user->query('BEGIN');
199
200         $original = clone($user);
201
202         $user->urlshorteningservice = $urlshorteningservice;
203
204         $result = $user->update($original);
205
206         if ($result === false) {
207             common_log_db_error($user, 'UPDATE', __FILE__);
208             // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
209             $this->serverError(_('Could not update user.'));
210             return;
211         }
212
213         $prefs = User_urlshortener_prefs::getPrefs($user);
214         $orig  = null;
215
216         if (empty($prefs)) {
217             $prefs = new User_urlshortener_prefs();
218
219             $prefs->user_id = $user->id;
220             $prefs->created = common_sql_now();
221         } else {
222             $orig = clone($prefs);
223         }
224
225         $prefs->urlshorteningservice = $urlshorteningservice;
226         $prefs->maxurllength         = $maxurllength;
227         $prefs->maxnoticelength      = $maxnoticelength;
228
229         if (!empty($orig)) {
230             $result = $prefs->update($orig);
231         } else {
232             $result = $prefs->insert();
233         }
234
235         if (!$result) {
236             throw new ServerException(_('Error saving user URL shortening preferences.'));
237         }
238
239         $user->query('COMMIT');
240
241         // TRANS: Confirmation message after saving preferences.
242         $this->showForm(_('Preferences saved.'), true);
243     }
244 }