]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookSSO/actions/facebookdeauthorize.php
Merge branch '0.9.x' into facebook-upgrade
[quix0rs-gnu-social.git] / plugins / FacebookSSO / actions / facebookdeauthorize.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * An action that handles deauthorize callbacks from Facebook
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Plugin
24  * @package   StatusNet
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /*
36  * Action class for handling deauthorize callbacks from Facebook. If the user
37  * doesn't have a password let her know she'll need to contact the site
38  * admin to get back into her account (if possible).
39  */
40 class FacebookdeauthorizeAction extends Action
41 {
42     private $facebook;
43
44     /**
45      * For initializing members of the class.
46      *
47      * @param array $args misc. arguments
48      *
49      * @return boolean true
50      */
51     function prepare($args)
52     {
53         $this->facebook = Facebookclient::getFacebook();
54
55         return true;
56     }
57
58     /**
59      * Handler method
60      *
61      * @param array $args is ignored since it's now passed in in prepare()
62      */
63     function handle($args)
64     {
65         parent::handle($args);
66
67         $data = $this->facebook->getSignedRequest();
68
69         if (isset($data['user_id'])) {
70
71             $fbuid = $data['user_id'];
72
73             $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
74             $user = $flink->getUser();
75
76             // Remove the link to Facebook
77             $result = $flink->delete();
78
79             if (!$result) {
80                 common_log_db_error($flink, 'DELETE', __FILE__);
81                 common_log(
82                     LOG_WARNING,
83                     sprintf(
84                         'Unable to delete Facebook foreign link '
85                             . 'for %s (%d), fbuid %s',
86                         $user->nickname,
87                         $user->id,
88                         $fbuid
89                     ),
90                     __FILE__
91                 );
92                 return;
93             }
94
95             common_log(
96                 LOG_INFO,
97                 sprintf(
98                     'Facebook callback: %s (%d), fbuid %s has deauthorized '
99                         . 'the Facebook application.',
100                     $user->nickname,
101                     $user->id,
102                     $fbuid
103                 ),
104                 __FILE__
105             );
106
107             // Warn the user about being locked out of their account
108             // if we can.
109             if (empty($user->password) && !empty($user->email)) {
110                 $this->emailWarn($user);
111             } else {
112                 common_log(
113                     LOG_WARNING,
114                     sprintf(
115                         '%s (%d), fbuid %d has deauthorized his/her Facebook '
116                         . 'connection but hasn\'t set a password so s/he '
117                         . 'is locked out.',
118                         $user->nickname,
119                         $user->id,
120                         $fbuid
121                     ),
122                     __FILE__
123                 );
124             }
125
126         } else {
127             if (!empty($data)) {
128                 common_log(
129                     LOG_WARNING,
130                     sprintf(
131                         'Facebook called the deauthorize callback '
132                         . ' but didn\'t provide a user ID.'
133                     ),
134                     __FILE__
135                 );
136             } else {
137                 // It probably wasn't Facebook that hit this action,
138                 // so redirect to the public timeline
139                 common_redirect(common_local_url('public'), 303);
140             }
141         }
142     }
143
144     /*
145      * Send the user an email warning that their account has been
146      * disconnected and he/she has no way to login and must contact
147      * the site administrator for help.
148      *
149      * @param User $user the deauthorizing user
150      *
151      */
152     function emailWarn($user)
153     {
154         $profile = $user->getProfile();
155
156         $siteName  = common_config('site', 'name');
157         $siteEmail = common_config('site', 'email');
158
159         if (empty($siteEmail)) {
160             common_log(
161                 LOG_WARNING,
162                     "No site email address configured. Please set one."
163             );
164         }
165
166         common_switch_locale($user->language);
167
168         $subject = _m('Contact the %s administrator to retrieve your account');
169
170         $msg = <<<BODY
171 Hi %1$s,
172
173 We've noticed you have deauthorized the Facebook connection for your
174 %2$s account.  You have not set a password for your %2$s account yet, so
175 you will not be able to login. If you wish to continue using your %2$s
176 account, please contact the site administrator (%3$s) to set a password.
177
178 Sincerely,
179
180 %2$s
181 BODY;
182         $body = sprintf(
183             _m($msg),
184             $user->nickname,
185             $siteName,
186             $siteEmail
187         );
188
189         common_switch_locale();
190
191         if (mail_to_user($user, $subject, $body)) {
192             common_log(
193                 LOG_INFO,
194                 sprintf(
195                     'Sent account lockout warning to %s (%d)',
196                     $user->nickname,
197                     $user->id
198                 ),
199                 __FILE__
200             );
201         } else {
202             common_log(
203                 LOG_WARNING,
204                 sprintf(
205                     'Unable to send account lockout warning to %s (%d)',
206                     $user->nickname,
207                     $user->id
208                 ),
209                 __FILE__
210             );
211         }
212     }
213
214 }