]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/othersettings.php
0c141838752597926e09cb239c95b0a06c84ee63
[quix0rs-gnu-social.git] / actions / othersettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 class OthersettingsAction extends SettingsAction {
25
26     function get_instructions()
27     {
28         return _('Manage various other options.');
29     }
30
31     function show_form($msg=null, $success=false)
32     {
33         $user = common_current_user();
34
35         $this->form_header(_('Other Settings'), $msg, $success);
36
37         common_element('h2', null, _('URL Auto-shortening'));
38         common_element_start('form', array('method' => 'post',
39                                            'id' => 'othersettings',
40                                            'action' =>
41                                            common_local_url('othersettings')));
42         common_hidden('token', common_session_token());
43
44         $services = array(
45             '' => 'None',
46             'ur1.ca' => 'ur1.ca (free service)',
47             '2tu.us' => '2tu.us (free service)',
48             'ptiturl.com' => 'ptiturl.com',
49             'bit.ly' => 'bit.ly',
50             'tinyurl.com' => 'tinyurl.com',
51             'is.gd' => 'is.gd',
52             'snipr.com' => 'snipr.com',
53             'metamark.net' => 'metamark.net'
54         );
55
56         common_dropdown('urlshorteningservice', _('Service'), $services, _('Automatic shortening service to use.'), FALSE, $user->urlshorteningservice);
57
58         common_submit('save', _('Save'));
59
60         common_element_end('form');
61
62 //        common_element('h2', null, _('Delete my account'));
63 //        $this->show_delete_form();
64
65         common_show_footer();
66     }
67
68     function show_feeds_list($feeds)
69     {
70         common_element_start('div', array('class' => 'feedsdel'));
71         common_element('p', null, 'Feeds:');
72         common_element_start('ul', array('class' => 'xoxo'));
73
74         foreach ($feeds as $key => $value) {
75             $this->common_feed_item($feeds[$key]);
76         }
77         common_element_end('ul');
78         common_element_end('div');
79     }
80
81     //TODO move to common.php (and retrace its origin)
82     function common_feed_item($feed)
83     {
84         $user = common_current_user();
85         $nickname = $user->nickname;
86
87         switch($feed['item']) {
88             case 'notices': default:
89                 $feed_classname = $feed['type'];
90                 $feed_mimetype = "application/".$feed['type']."+xml";
91                 $feed_title = "$nickname's ".$feed['version']." notice feed";
92                 $feed['textContent'] = "RSS";
93                 break;
94
95             case 'foaf':
96                 $feed_classname = "foaf";
97                 $feed_mimetype = "application/".$feed['type']."+xml";
98                 $feed_title = "$nickname's FOAF file";
99                 $feed['textContent'] = "FOAF";
100                 break;
101         }
102         common_element_start('li');
103         common_element('a', array('href' => $feed['href'],
104                                   'class' => $feed_classname,
105                                   'type' => $feed_mimetype,
106                                   'title' => $feed_title),
107                             $feed['textContent']);
108         common_element_end('li');
109     }
110
111 //    function show_delete_form() {
112 //        $user = common_current_user();
113 //      $notices = DB_DataObject::factory('notice');
114 //      $notices->profile_id = $user->id;
115 //      $notice_count = (int) $notices->count();
116 //
117 //        common_element_start('form', array('method' => 'POST',
118 //                                           'id' => 'delete',
119 //                                           'action' =>
120 //                                           common_local_url('deleteprofile')));
121 //
122 //        common_hidden('token', common_session_token());
123 //      common_element('p', null, "You can copy your notices and contacts by saving the two links below before deleting your account. Be careful, this operation cannot be undone.");
124 //
125 //        $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('limit' => $notice_count, 'nickname' => $user->nickname)),
126 //                                              'type' => 'rss',
127 //                                              'version' => 'RSS 1.0',
128 //                                              'item' => 'notices'),
129 //                                     1=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
130 //                                              'type' => 'rdf',
131 //                                              'version' => 'FOAF',
132 //                                              'item' => 'foaf')));
133 //
134 //        common_submit('deleteaccount', _('Delete my account'));
135 //        common_element_end('form');
136 //    }
137
138     function handle_post()
139     {
140
141         # CSRF protection
142         $token = $this->trimmed('token');
143         if (!$token || $token != common_session_token()) {
144             $this->show_form(_('There was a problem with your session token. Try again, please.'));
145             return;
146         }
147
148         if ($this->arg('save')) {
149             $this->save_preferences();
150         }else {
151             $this->show_form(_('Unexpected form submission.'));
152         }
153     }
154
155     function save_preferences()
156     {
157
158         $urlshorteningservice = $this->trimmed('urlshorteningservice');
159
160         if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
161             $this->show_form(_('URL shortening service is too long (max 50 chars).'));
162             return;
163         }
164
165         $user = common_current_user();
166
167         assert(!is_null($user)); # should already be checked
168
169         $user->query('BEGIN');
170
171         $original = clone($user);
172
173         $user->urlshorteningservice = $urlshorteningservice;
174
175         $result = $user->update($original);
176
177         if ($result === FALSE) {
178             common_log_db_error($user, 'UPDATE', __FILE__);
179             common_server_error(_('Couldn\'t update user.'));
180             return;
181         }
182
183         $user->query('COMMIT');
184
185         $this->show_form(_('Preferences saved.'), true);
186     }
187 }