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