]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/urlsettings.php
Merge remote branch 'gitorious/1.0.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 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
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 AccountSettingsAction
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         return _('Manage various other options.');
72     }
73
74     function showScripts()
75     {
76         parent::showScripts();
77         $this->autofocus('urlshorteningservice');
78     }
79
80     /**
81      * Content area of the page
82      *
83      * Shows a form for uploading an avatar.
84      *
85      * @return void
86      */
87
88     function showContent()
89     {
90         $user = common_current_user();
91
92         $this->elementStart('form', array('method' => 'post',
93                                           'id' => 'form_settings_other',
94                                           'class' => 'form_settings',
95                                           'action' =>
96                                           common_local_url('urlsettings')));
97         $this->elementStart('fieldset');
98         $this->hidden('token', common_session_token());
99         $this->elementStart('ul', 'form_data');
100
101         $shorteners = array(_('[none]') => array('freeService' => false));
102
103         Event::handle('GetUrlShorteners', array(&$shorteners));
104
105         $services = array();
106         foreach($shorteners as $name=>$value)
107         {
108             $services[$name]=$name;
109             if($value['freeService']){
110                 $services[$name].=_(' (free service)');
111             }
112         }
113         if($services)
114         {
115             asort($services);
116
117             $this->elementStart('li');
118             $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
119                             $services, _('Automatic shortening service to use.'),
120                             false, $user->urlshorteningservice);
121             $this->elementEnd('li');
122         }
123         $this->elementStart('li');
124         $this->input('maxurllength',
125                      _('URL longer than'),
126                      (!is_null($this->arg('maxurllength'))) ?
127                      $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
128                      _('URLs longer than this will be shortened, 0 means always shorten.'));
129         $this->elementEnd('li');
130         $this->elementStart('li');
131         $this->input('maxnoticelength',
132                      _('Text longer than'),
133                      (!is_null($this->arg('maxnoticelength'))) ?
134                      $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
135                      _('URLs in notices longer than this will be shortened, 0 means always shorten.'));
136         $this->elementEnd('li');
137         $this->elementEnd('ul');
138         $this->submit('save', _('Save'));
139         $this->elementEnd('fieldset');
140         $this->elementEnd('form');
141     }
142
143     /**
144      * Handle a post
145      *
146      * Saves the changes to url-shortening prefs and shows a success or failure
147      * message.
148      *
149      * @return void
150      */
151
152     function handlePost()
153     {
154         // CSRF protection
155         $token = $this->trimmed('token');
156         if (!$token || $token != common_session_token()) {
157             $this->showForm(_('There was a problem with your session token. '.
158                               'Try again, please.'));
159             return;
160         }
161
162         $urlshorteningservice = $this->trimmed('urlshorteningservice');
163
164         if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
165             $this->showForm(_('URL shortening service is too long (max 50 chars).'));
166             return;
167         }
168
169         $maxurllength = $this->trimmed('maxurllength');
170
171         if (!Validate::number($maxurllength, array('min' => 0))) {
172             throw new ClientException(_('Invalid number for max url length.'));
173         }
174
175         $maxnoticelength = $this->trimmed('maxnoticelength');
176
177         if (!Validate::number($maxnoticelength, array('min' => 0))) {
178             throw new ClientException(_('Invalid number for max notice length.'));
179         }
180
181         $user = common_current_user();
182
183         assert(!is_null($user)); // should already be checked
184
185         $user->query('BEGIN');
186
187         $original = clone($user);
188
189         $user->urlshorteningservice = $urlshorteningservice;
190
191         $result = $user->update($original);
192
193         if ($result === false) {
194             common_log_db_error($user, 'UPDATE', __FILE__);
195             $this->serverError(_('Couldn\'t update user.'));
196             return;
197         }
198
199         $prefs = User_urlshortener_prefs::getPrefs($user);
200         $orig  = null;
201
202         if (empty($prefs)) {
203             $prefs = new User_urlshortener_prefs();
204
205             $prefs->user_id = $user->id;
206             $prefs->created = common_sql_now();
207         } else {
208             $orig = clone($prefs);
209         }
210
211         $prefs->urlshorteningservice = $urlshorteningservice;
212         $prefs->maxurllength         = $maxurllength;
213         $prefs->maxnoticelength      = $maxnoticelength;
214
215         if (!empty($orig)) {
216             $result = $prefs->update($orig);
217         } else {
218             $result = $prefs->insert();
219         }
220
221         if (!$result) {
222             throw new ServerException(_('Error saving user URL shortening preferences.'));
223         }
224
225         $user->query('COMMIT');
226
227         $this->showForm(_('Preferences saved.'), true);
228     }
229 }