]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookBridge/actions/facebooksettings.php
31e020a3ce7a42351856245ae2d13b0a82007946
[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 SettingsAction {
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                 // TRANS: Client error displayed when the session token does not match or is not given.
87                 _m('There was a problem with your session token. Try again, please.')
88             );
89             return;
90         }
91
92         if ($this->arg('save')) {
93             $this->saveSettings();
94         } else if ($this->arg('disconnect')) {
95             $this->disconnect();
96         }
97     }
98
99     /**
100      * Returns the page title
101      *
102      * @return string page title
103      */
104     function title() {
105         // TRANS: Page title for Facebook settings.
106         return _m('TITLE','Facebook settings');
107     }
108
109     /**
110      * Instructions for use
111      *
112      * @return instructions for use
113      */
114     function getInstructions() {
115         // TRANS: Instructions for Facebook settings.
116         return _m('Facebook settings');
117     }
118
119     /*
120      * Show the settings form if he/she has a link to Facebook
121      *
122      * @return void
123      */
124     function showContent() {
125         if (!empty($this->flink)) {
126
127             $this->elementStart(
128                 'form',
129                 array(
130                     'method' => 'post',
131                     'id' => 'form_settings_facebook',
132                     'class' => 'form_settings',
133                     'action' => common_local_url('facebooksettings')
134                 )
135             );
136
137             $this->hidden('token', common_session_token());
138
139             // TRANS: Form note. User is connected to facebook.
140             $this->element('p', 'form_note', _m('Connected Facebook user'));
141
142             $this->elementStart('p', array('class' => 'facebook-user-display'));
143
144             $this->element(
145                 'fb:profile-pic',
146                 array(
147                     'uid' => $this->flink->foreign_id,
148                     'size' => 'small',
149                     'linked' => 'true',
150                     'facebook-logo' => 'true'
151                 )
152             );
153
154             $this->element(
155                 'fb:name',
156                 array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
157             );
158
159             $this->elementEnd('p');
160
161             $this->elementStart('ul', 'form_data');
162
163             $this->elementStart('li');
164
165             $this->checkbox(
166                 'noticesync',
167                 // TRANS: Checkbox label in Facebook settings.
168                 _m('Publish my notices to Facebook.'),
169                 ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true
170             );
171
172             $this->elementEnd('li');
173
174             $this->elementStart('li');
175
176             $this->checkbox(
177                     'replysync',
178                     // TRANS: Checkbox label in Facebook settings.
179                     _m('Send "@" replies to Facebook.'),
180                     ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true
181             );
182
183             $this->elementEnd('li');
184
185             $this->elementStart('li');
186
187             // TRANS: Submit button to save synchronisation settings.
188             $this->submit('save', _m('BUTTON', 'Save'));
189
190             $this->elementEnd('li');
191
192             $this->elementEnd('ul');
193
194             $this->elementStart('fieldset');
195
196             // TRANS: Fieldset legend for form to disconnect from Facebook.
197             $this->element('legend', null, _m('Disconnect my account from Facebook'));
198
199             if (empty($this->user->password)) {
200                 $this->elementStart('p', array('class' => 'form_guide'));
201
202                 $msg = sprintf(
203                     // TRANS: Notice in disconnect from Facebook form if user has no local StatusNet password.
204                     _m('Disconnecting your Faceboook would make it impossible to '.
205                        'log in! Please [set a password](%s) first.'),
206                     common_local_url('passwordsettings')
207                 );
208
209                 $this->raw(common_markup_to_html($msg));
210                 $this->elementEnd('p');
211             } else {
212                 // @todo FIXME: i18n: This message is not being used.
213                 // TRANS: Message displayed when initiating disconnect of a StatusNet user
214                 // TRANS: from a Facebook account. %1$s is the StatusNet site name.
215                 $msg = sprintf(_m('Keep your %1$s account but disconnect from Facebook. ' .
216                                   'You\'ll use your %1$s password to log in.'),
217                                common_config('site', 'name')
218                 );
219
220                 // TRANS: Submit button.
221                 $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
222             }
223
224             $this->elementEnd('fieldset');
225
226             $this->elementEnd('form');
227          }
228     }
229
230     /*
231      * Save the user's Facebook settings
232      *
233      * @return void
234      */
235     function saveSettings() {
236         $noticesync = $this->boolean('noticesync');
237         $replysync  = $this->boolean('replysync');
238
239         $original = clone($this->flink);
240         $this->flink->set_flags($noticesync, false, $replysync, false);
241         $result = $this->flink->update($original);
242
243         if ($result === false) {
244             // TRANS: Notice in case saving of synchronisation preferences fail.
245             $this->showForm(_m('There was a problem saving your sync preferences.'));
246         } else {
247             // TRANS: Confirmation that synchronisation settings have been saved into the system.
248             $this->showForm(_m('Sync preferences saved.'), true);
249         }
250     }
251
252     /*
253      * Disconnect the user's Facebook account - deletes the Foreign_link
254      * and shows the user a success message if all goes well.
255      */
256     function disconnect() {
257         $result = $this->flink->delete();
258         $this->flink = null;
259
260         if ($result === false) {
261             common_log_db_error($user, 'DELETE', __FILE__);
262             // TRANS: Server error displayed when deleting the link to a Facebook account fails.
263             $this->serverError(_m('Could not delete link to Facebook.'));
264         }
265
266         // TRANS: Confirmation message. StatusNet account was unlinked from Facebook.
267         $this->showForm(_m('You have disconnected from Facebook.'), true);
268     }
269 }