]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/othersettings.php
move opening brace of class declaration to next line
[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
27     function get_instructions()
28     {
29         return _('Manage various other options.');
30     }
31
32     function show_form($msg=null, $success=false)
33     {
34         $user = common_current_user();
35
36         $this->form_header(_('Other Settings'), $msg, $success);
37
38         common_element('h2', null, _('URL Auto-shortening'));
39         common_element_start('form', array('method' => 'post',
40                                            'id' => 'othersettings',
41                                            'action' =>
42                                            common_local_url('othersettings')));
43         common_hidden('token', common_session_token());
44
45         $services = array(
46             '' => 'None',
47             'ur1.ca' => 'ur1.ca (free service)',
48             '2tu.us' => '2tu.us (free service)',
49             'ptiturl.com' => 'ptiturl.com',
50             'bit.ly' => 'bit.ly',
51             'tinyurl.com' => 'tinyurl.com',
52             'is.gd' => 'is.gd',
53             'snipr.com' => 'snipr.com',
54             'metamark.net' => 'metamark.net'
55         );
56
57         common_dropdown('urlshorteningservice', _('Service'), $services, _('Automatic shortening service to use.'), false, $user->urlshorteningservice);
58
59         common_submit('save', _('Save'));
60
61         common_element_end('form');
62
63 //        common_element('h2', null, _('Delete my account'));
64 //        $this->show_delete_form();
65
66         common_show_footer();
67     }
68
69     function show_feeds_list($feeds)
70     {
71         common_element_start('div', array('class' => 'feedsdel'));
72         common_element('p', null, 'Feeds:');
73         common_element_start('ul', array('class' => 'xoxo'));
74
75         foreach ($feeds as $key => $value) {
76             $this->common_feed_item($feeds[$key]);
77         }
78         common_element_end('ul');
79         common_element_end('div');
80     }
81
82     //TODO move to common.php (and retrace its origin)
83     function common_feed_item($feed)
84     {
85         $user = common_current_user();
86         $nickname = $user->nickname;
87
88         switch($feed['item']) {
89             case 'notices': default:
90                 $feed_classname = $feed['type'];
91                 $feed_mimetype = "application/".$feed['type']."+xml";
92                 $feed_title = "$nickname's ".$feed['version']." notice feed";
93                 $feed['textContent'] = "RSS";
94                 break;
95
96             case 'foaf':
97                 $feed_classname = "foaf";
98                 $feed_mimetype = "application/".$feed['type']."+xml";
99                 $feed_title = "$nickname's FOAF file";
100                 $feed['textContent'] = "FOAF";
101                 break;
102         }
103         common_element_start('li');
104         common_element('a', array('href' => $feed['href'],
105                                   'class' => $feed_classname,
106                                   'type' => $feed_mimetype,
107                                   'title' => $feed_title),
108                             $feed['textContent']);
109         common_element_end('li');
110     }
111
112 //    function show_delete_form() {
113 //        $user = common_current_user();
114 //      $notices = DB_DataObject::factory('notice');
115 //      $notices->profile_id = $user->id;
116 //      $notice_count = (int) $notices->count();
117 //
118 //        common_element_start('form', array('method' => 'POST',
119 //                                           'id' => 'delete',
120 //                                           'action' =>
121 //                                           common_local_url('deleteprofile')));
122 //
123 //        common_hidden('token', common_session_token());
124 //      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.");
125 //
126 //        $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('limit' => $notice_count, 'nickname' => $user->nickname)),
127 //                                              'type' => 'rss',
128 //                                              'version' => 'RSS 1.0',
129 //                                              'item' => 'notices'),
130 //                                     1=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
131 //                                              'type' => 'rdf',
132 //                                              'version' => 'FOAF',
133 //                                              'item' => 'foaf')));
134 //
135 //        common_submit('deleteaccount', _('Delete my account'));
136 //        common_element_end('form');
137 //    }
138
139     function handle_post()
140     {
141
142         # CSRF protection
143         $token = $this->trimmed('token');
144         if (!$token || $token != common_session_token()) {
145             $this->show_form(_('There was a problem with your session token. Try again, please.'));
146             return;
147         }
148
149         if ($this->arg('save')) {
150             $this->save_preferences();
151         }else {
152             $this->show_form(_('Unexpected form submission.'));
153         }
154     }
155
156     function save_preferences()
157     {
158
159         $urlshorteningservice = $this->trimmed('urlshorteningservice');
160
161         if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
162             $this->show_form(_('URL shortening service is too long (max 50 chars).'));
163             return;
164         }
165
166         $user = common_current_user();
167
168         assert(!is_null($user)); # should already be checked
169
170         $user->query('BEGIN');
171
172         $original = clone($user);
173
174         $user->urlshorteningservice = $urlshorteningservice;
175
176         $result = $user->update($original);
177
178         if ($result === false) {
179             common_log_db_error($user, 'UPDATE', __FILE__);
180             common_server_error(_('Couldn\'t update user.'));
181             return;
182         }
183
184         $user->query('COMMIT');
185
186         $this->show_form(_('Preferences saved.'), true);
187     }
188 }