]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookSSO/facebooklogin.php
fce481fc037a32913d49523401233f41261053d5
[quix0rs-gnu-social.git] / plugins / FacebookSSO / facebooklogin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * An action for logging in with 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  Pugin
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 class FacebookloginAction extends Action
36 {
37     function handle($args)
38     {
39         parent::handle($args);
40
41         if (common_is_real_login()) {
42             
43             $this->clientError(_m('Already logged in.'));
44
45         } else {
46
47             $facebook = new Facebook(
48                 array(
49                     'appId'  => common_config('facebook', 'appid'),
50                     'secret' => common_config('facebook', 'secret'),
51                     'cookie' => true,
52                 )
53             );
54
55             $session = $facebook->getSession();
56             $me      = null;
57
58             if ($session) {
59                 try {
60                     $fbuid = $facebook->getUser();
61                     $fbuser  = $facebook->api('/me');
62                 } catch (FacebookApiException $e) {
63                     common_log(LOG_ERROR, $e);
64                 }
65             }
66
67             if (!empty($fbuser)) {
68                 common_debug("Found a valid Facebook user", __FILE__);
69
70                 // Check to see if we have a foreign link already
71                 $flink = Foreign_link::getByForeignId($fbuid, FACEBOOK_SERVICE);
72
73                 if (empty($flink)) {
74
75                     // See if the user would like to register a new local
76                     // account
77                     common_redirect(
78                         common_local_url('facebookregister'),
79                         303
80                     );
81
82                 } else {
83
84                     // Log our user in!
85                     $user = $flink->getUser();
86
87                     if (!empty($user)) {
88
89                         common_debug(
90                             sprintf(
91                                 'Logged in Facebook user $s as user %d (%s)',
92                                 $this->fbuid,
93                                 $user->id,
94                                 $user->nickname
95                             ),
96                             __FILE__
97                         );
98
99                         common_set_user($user);
100                         common_real_login(true);
101                         $this->goHome($user->nickname);
102                     }
103                 }
104
105             }
106         }
107
108         $this->showPage();
109     }
110
111     function goHome($nickname)
112     {
113         $url = common_get_returnto();
114         if ($url) {
115             // We don't have to return to it again
116             common_set_returnto(null);
117         } else {
118             $url = common_local_url(
119                 'all',
120                 array('nickname' => $nickname)
121             );
122         }
123
124         common_redirect($url, 303);
125     }
126
127     function getInstructions()
128     {
129         // TRANS: Instructions.
130         return _m('Login with your Facebook Account');
131     }
132
133     function showPageNotice()
134     {
135         $instr = $this->getInstructions();
136         $output = common_markup_to_html($instr);
137         $this->elementStart('div', 'instructions');
138         $this->raw($output);
139         $this->elementEnd('div');
140     }
141
142     function title()
143     {
144         // TRANS: Page title.
145         return _m('Login with Facebook');
146     }
147
148     function showContent() {
149
150         $this->elementStart('fieldset');
151
152         $attrs = array(
153             'show-faces' => 'true',
154             'width'      => '100',
155             'max-rows'   => '2',
156             'perms'      => 'user_location,user_website,offline_access,publish_stream'
157         );
158
159         $this->element('fb:login-button', $attrs);
160         $this->elementEnd('fieldset');
161     }
162
163     function showLocalNav()
164     {
165         $nav = new LoginGroupNav($this);
166         $nav->show();
167     }
168 }
169