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