]> 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
46     private $facebook; // Facebook PHP-SDK client obj
47     private $flink;
48     private $user;
49
50     /**
51      * For initializing members of the class.
52      *
53      * @param array $argarray misc. arguments
54      *
55      * @return boolean true
56      */
57     function prepare($args) {
58         parent::prepare($args);
59
60         $this->facebook = new Facebook(
61             array(
62                 'appId'  => common_config('facebook', 'appid'),
63                 'secret' => common_config('facebook', 'secret'),
64                 'cookie' => true,
65             )
66         );
67
68         $this->user = common_current_user();
69
70         $this->flink = Foreign_link::getByUserID(
71             $this->user->id,
72             FACEBOOK_SERVICE
73         );
74
75         return true;
76     }
77
78     /*
79      * Check the sessions token and dispatch
80      */
81     function handlePost($args) {
82         // CSRF protection
83
84         $token = $this->trimmed('token');
85         if (!$token || $token != common_session_token()) {
86             $this->showForm(
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('Facebook settings');
107     }
108
109     /**
110      * Instructions for use
111      *
112      * @return instructions for use
113      */
114     function getInstructions() {
115         return _('Facebook settings');
116     }
117
118     /*
119      * Show the settings form if he/she has a link to Facebook
120      *
121      * @return void
122      */
123     function showContent() {
124
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             $this->element('p', 'form_note', _m('Connected Facebook user'));
140
141             $this->elementStart('p', array('class' => 'facebook-user-display'));
142
143             $this->element(
144                 'fb:profile-pic',
145                 array(
146                     'uid' => $this->flink->foreign_id,
147                     'size' => 'small',
148                     'linked' => 'true',
149                     'facebook-logo' => 'true'
150                 )
151             );
152
153             $this->element(
154                 'fb:name',
155                 array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
156             );
157
158             $this->elementEnd('p');
159
160             $this->elementStart('ul', 'form_data');
161
162             $this->elementStart('li');
163
164             $this->checkbox(
165                 'noticesync',
166                 _m('Publish my notices to Facebook.'),
167                 ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true
168             );
169
170             $this->elementEnd('li');
171
172             $this->elementStart('li');
173
174             $this->checkbox(
175                     'replysync',
176                     _m('Send "@" replies to Facebook.'),
177                     ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true
178             );
179
180             $this->elementEnd('li');
181
182             $this->elementStart('li');
183
184             // TRANS: Submit button to save synchronisation settings.
185             $this->submit('save', _m('BUTTON', 'Save'));
186
187             $this->elementEnd('li');
188
189             $this->elementEnd('ul');
190
191             $this->elementStart('fieldset');
192
193             // TRANS: Legend.
194             $this->element('legend', null, _m('Disconnect my account from Facebook'));
195
196             if (empty($this->user->password)) {
197
198                 $this->elementStart('p', array('class' => 'form_guide'));
199
200                 $msg = sprintf(
201                     _m(
202                         'Disconnecting your Faceboook would make it impossible to '
203                             . 'log in! Please [set a password](%s) first.'
204                     ),
205                     common_local_url('passwordsettings')
206                 );
207
208                 $this->raw(common_markup_to_html($msg));
209                 $this->elementEnd('p');
210
211             } else {
212
213                 $msg = sprintf(
214                     _m(
215                         'Keep your %1$s account but disconnect from Facebook. ' .
216                         'You\'ll use your 1%$s password to log in.'
217                     ),
218                     common_config('site', 'name')
219                 );
220
221                 // TRANS: Submit button.
222                 $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
223             }
224
225             $this->elementEnd('fieldset');
226
227             $this->elementEnd('form');
228          }
229     }
230
231     /*
232      * Save the user's Facebook settings
233      *
234      * @return void
235      */
236     function saveSettings() {
237
238         $noticesync = $this->boolean('noticesync');
239         $replysync  = $this->boolean('replysync');
240
241         $original = clone($this->flink);
242         $this->flink->set_flags($noticesync, false, $replysync, false);
243         $result = $this->flink->update($original);
244
245         if ($result === false) {
246             $this->showForm(_m('There was a problem saving your sync preferences.'));
247         } else {
248             // TRANS: Confirmation that synchronisation settings have been saved into the system.
249             $this->showForm(_m('Sync preferences saved.'), true);
250         }
251     }
252
253     /*
254      * Disconnect the user's Facebook account - deletes the Foreign_link
255      * and shows the user a success message if all goes well.
256      */
257     function disconnect() {
258
259         $result = $this->flink->delete();
260         $this->flink = null;
261
262         if ($result === false) {
263             common_log_db_error($user, 'DELETE', __FILE__);
264             $this->serverError(_m('Couldn\'t delete link to Facebook.'));
265             return;
266         }
267
268         $this->showForm(_m('You have disconnected from Facebook.'), true);
269     }
270
271 }