]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookBridge/actions/facebooksettings.php
Merge branch 'master' into 0.9.x
[quix0rs-gnu-social.git] / plugins / FacebookBridge / actions / facebooksettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  *  Edit user settings for Facebook
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    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 /**
34  * Edit user settings for Facebook
35  *
36  * @category Settings
37  * @package  StatusNet
38  * @author   Zach Copley <zach@status.net>
39  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40  * @link     http://status.net/
41  *
42  * @see      SettingsAction
43  */
44 class FacebooksettingsAction extends ConnectSettingsAction {
45     private $facebook; // Facebook PHP-SDK client obj
46     private $flink;
47     private $user;
48
49     /**
50      * For initializing members of the class.
51      *
52      * @param array $argarray misc. arguments
53      *
54      * @return boolean true
55      */
56     function prepare($args) {
57         parent::prepare($args);
58
59         $this->facebook = new Facebook(
60             array(
61                 'appId'  => common_config('facebook', 'appid'),
62                 'secret' => common_config('facebook', 'secret'),
63                 'cookie' => true,
64             )
65         );
66
67         $this->user = common_current_user();
68
69         $this->flink = Foreign_link::getByUserID(
70             $this->user->id,
71             FACEBOOK_SERVICE
72         );
73
74         return true;
75     }
76
77     /*
78      * Check the sessions token and dispatch
79      */
80     function handlePost($args) {
81         // CSRF protection
82
83         $token = $this->trimmed('token');
84         if (!$token || $token != common_session_token()) {
85             $this->showForm(
86                 _m('There was a problem with your session token. Try again, please.')
87             );
88             return;
89         }
90
91         if ($this->arg('save')) {
92             $this->saveSettings();
93         } else if ($this->arg('disconnect')) {
94             $this->disconnect();
95         }
96     }
97
98     /**
99      * Returns the page title
100      *
101      * @return string page title
102      */
103     function title() {
104         // TRANS: Page title for Facebook settings.
105         return _m('Facebook settings');
106     }
107
108     /**
109      * Instructions for use
110      *
111      * @return instructions for use
112      */
113     function getInstructions() {
114         return _('Facebook settings');
115     }
116
117     /*
118      * Show the settings form if he/she has a link to Facebook
119      *
120      * @return void
121      */
122     function showContent() {
123         if (!empty($this->flink)) {
124
125             $this->elementStart(
126                 'form',
127                 array(
128                     'method' => 'post',
129                     'id' => 'form_settings_facebook',
130                     'class' => 'form_settings',
131                     'action' => common_local_url('facebooksettings')
132                 )
133             );
134
135             $this->hidden('token', common_session_token());
136
137             $this->element('p', 'form_note', _m('Connected Facebook user'));
138
139             $this->elementStart('p', array('class' => 'facebook-user-display'));
140
141             $this->element(
142                 'fb:profile-pic',
143                 array(
144                     'uid' => $this->flink->foreign_id,
145                     'size' => 'small',
146                     'linked' => 'true',
147                     'facebook-logo' => 'true'
148                 )
149             );
150
151             $this->element(
152                 'fb:name',
153                 array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
154             );
155
156             $this->elementEnd('p');
157
158             $this->elementStart('ul', 'form_data');
159
160             $this->elementStart('li');
161
162             $this->checkbox(
163                 'noticesync',
164                 _m('Publish my notices to Facebook.'),
165                 ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true
166             );
167
168             $this->elementEnd('li');
169
170             $this->elementStart('li');
171
172             $this->checkbox(
173                     'replysync',
174                     _m('Send "@" replies to Facebook.'),
175                     ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true
176             );
177
178             $this->elementEnd('li');
179
180             $this->elementStart('li');
181
182             // TRANS: Submit button to save synchronisation settings.
183             $this->submit('save', _m('BUTTON', 'Save'));
184
185             $this->elementEnd('li');
186
187             $this->elementEnd('ul');
188
189             $this->elementStart('fieldset');
190
191             // TRANS: Legend.
192             $this->element('legend', null, _m('Disconnect my account from Facebook'));
193
194             if (empty($this->user->password)) {
195                 $this->elementStart('p', array('class' => 'form_guide'));
196
197                 $msg = sprintf(
198                     _m(
199                         'Disconnecting your Faceboook would make it impossible to '
200                             . 'log in! Please [set a password](%s) first.'
201                     ),
202                     common_local_url('passwordsettings')
203                 );
204
205                 $this->raw(common_markup_to_html($msg));
206                 $this->elementEnd('p');
207             } else {
208                 // @todo FIXME: i18n: This message is not being used.
209                 $msg = sprintf(
210                     // TRANS: Message displayed when initiating disconnect of a StatusNet user
211                     // TRANS: from a Facebook account. %1$s is the StatusNet site name.
212                     _m(
213                         'Keep your %1$s account but disconnect from Facebook. ' .
214                         'You\'ll use your %1$s password to log in.'
215                     ),
216                     common_config('site', 'name')
217                 );
218
219                 // TRANS: Submit button.
220                 $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
221             }
222
223             $this->elementEnd('fieldset');
224
225             $this->elementEnd('form');
226          }
227     }
228
229     /*
230      * Save the user's Facebook settings
231      *
232      * @return void
233      */
234     function saveSettings() {
235         $noticesync = $this->boolean('noticesync');
236         $replysync  = $this->boolean('replysync');
237
238         $original = clone($this->flink);
239         $this->flink->set_flags($noticesync, false, $replysync, false);
240         $result = $this->flink->update($original);
241
242         if ($result === false) {
243             $this->showForm(_m('There was a problem saving your sync preferences.'));
244         } else {
245             // TRANS: Confirmation that synchronisation settings have been saved into the system.
246             $this->showForm(_m('Sync preferences saved.'), true);
247         }
248     }
249
250     /*
251      * Disconnect the user's Facebook account - deletes the Foreign_link
252      * and shows the user a success message if all goes well.
253      */
254     function disconnect() {
255         $result = $this->flink->delete();
256         $this->flink = null;
257
258         if ($result === false) {
259             common_log_db_error($user, 'DELETE', __FILE__);
260             $this->serverError(_m('Couldn\'t delete link to Facebook.'));
261             return;
262         }
263
264         $this->showForm(_m('You have disconnected from Facebook.'), true);
265     }
266 }