]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookSSO/actions/facebooksettings.php
- Some reorganizing
[quix0rs-gnu-social.git] / plugins / FacebookSSO / actions / facebooksettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * 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
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Settings for Facebook
36  *
37  * @category Settings
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  *
43  * @see      SettingsAction
44  */
45
46 class FacebooksettingsAction extends ConnectSettingsAction
47 {
48     private $facebook;
49     private $flink;
50     private $user;
51     
52     function prepare($args)
53     {
54         parent::prepare($args);
55
56         $this->facebook = new Facebook(
57             array(
58                 'appId'  => common_config('facebook', 'appid'),
59                 'secret' => common_config('facebook', 'secret'),
60                 'cookie' => true,
61             )
62         );
63
64         $this->user = common_current_user();
65         $this->flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE);
66
67         return true;
68     }
69
70     function handlePost($args)
71     {
72         // CSRF protection
73
74         $token = $this->trimmed('token');
75         if (!$token || $token != common_session_token()) {
76             $this->showForm(
77                 _m('There was a problem with your session token. Try again, please.')
78             );
79             return;
80         }
81
82         if ($this->arg('save')) {
83             $this->saveSettings();
84         } else if ($this->arg('disconnect')) {
85             $this->disconnect();
86         }
87     }
88
89     function title()
90     {
91         // TRANS: Page title for Facebook settings.
92         return _m('Facebook settings');
93     }
94     
95     /**
96      * Instructions for use
97      *
98      * @return instructions for use
99      */
100
101     function getInstructions()
102     {
103         return _('Facebook settings');
104     }
105
106     function showContent()
107     {
108
109         if (empty($this->flink)) {
110
111             $this->element(
112                 'p',
113                 'instructions',
114                 _m('There is no Facebook user connected to this account.')
115             );
116
117             $attrs = array(
118                 'show-faces' => 'true',
119                 'perms' => 'user_location,user_website,offline_access,publish_stream'
120             );
121
122             $this->element('fb:login-button', $attrs);
123             
124
125         } else {
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->elementStart(
144                 'fb:profile-pic',
145                 array('uid' => $this->flink->foreign_id,
146                       'size' => 'small',
147                       'linked' => 'true',
148                       'facebook-logo' => 'true')
149             );
150             $this->elementEnd('fb:profile-pic');
151
152             $this->elementStart(
153                 'fb:name',
154                 array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
155             );
156
157             $this->elementEnd('fb:name');
158
159             $this->elementEnd('p');
160
161             $this->elementStart('ul', 'form_data');
162
163             $this->elementStart('li');
164
165             $this->checkbox(
166                 'noticesync',
167                 _m('Publish my notices to Facebook.'),
168                 ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true
169             );
170
171             $this->elementEnd('li');
172
173             $this->elementStart('li');
174
175             $this->checkbox(
176                 'replysync',
177                 _m('Send "@" replies to Facebook.'),
178                 ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true
179             );
180
181             $this->elementEnd('li');
182
183             $this->elementStart('li');
184
185             // TRANS: Submit button to save synchronisation settings.
186             $this->submit('save', _m('BUTTON','Save'));
187
188             $this->elementEnd('li');
189         
190             $this->elementEnd('ul');
191
192             $this->elementStart('fieldset');
193
194             // TRANS: Legend.
195             $this->element('legend', null, _m('Disconnect my account from Facebook'));
196
197             if (empty($this->user->password)) {
198
199                 $this->elementStart('p', array('class' => 'form_guide'));
200                 // @todo FIXME: Bad i18n. Patchwork message in three parts.
201                 // TRANS: Followed by a link containing text "set a password".
202                 $this->text(_m('Disconnecting your Faceboook ' .
203                                'would make it impossible to log in! Please '));
204                 $this->element('a',
205                     array('href' => common_local_url('passwordsettings')),
206                         // TRANS: Preceded by "Please " and followed by " first."
207                         _m('set a password'));
208                 // TRANS: Preceded by "Please set a password".
209                 $this->text(_m(' first.'));
210                 $this->elementEnd('p');
211             } else {
212
213                 $note = 'Keep your %s account but disconnect from Facebook. ' .
214                     'You\'ll use your %s password to log in.';
215
216                 $site = common_config('site', 'name');
217
218                 $this->element('p', 'instructions',
219                     sprintf($note, $site, $site));
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     function saveSettings()
232     {
233
234         $noticesync = $this->boolean('noticesync');
235         $replysync  = $this->boolean('replysync');
236
237         $original = clone($this->flink);
238         $this->flink->set_flags($noticesync, false, $replysync, false);
239         $result = $this->flink->update($original);
240
241         if ($result === false) {
242             $this->showForm(_m('There was a problem saving your sync preferences.'));
243         } else {
244             // TRANS: Confirmation that synchronisation settings have been saved into the system.
245             $this->showForm(_m('Sync preferences saved.'), true);
246         }
247     }
248
249     function disconnect()
250     {
251         $flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE);
252         $result = $flink->delete();
253
254         if ($result === false) {
255             common_log_db_error($user, 'DELETE', __FILE__);
256             $this->serverError(_m('Couldn\'t delete link to Facebook.'));
257             return;
258         }
259
260         $this->showForm(_m('You have disconnected from Facebook.'), true);
261
262     }
263 }
264