]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/facebookhome.php
7430cc41fa7c2428262da36cd09613351942fb14
[quix0rs-gnu-social.git] / actions / facebookhome.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/lib/facebookaction.php';
23
24 class FacebookhomeAction extends FacebookAction
25 {
26
27     function handle($args)
28     {
29         parent::handle($args);
30
31         $facebook = get_facebook();
32         $fbuid = $facebook->require_login();
33
34         // If the user has opted not to initially allow the app to have
35         // Facebook status update permission, store that preference. Only
36         // promt the user the first time she uses the app
37         if ($this->arg('skip')) {
38             $facebook->api_client->data_setUserPreference(
39                 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
40         }
41
42         // Check to see whether there's already a Facebook link for this user
43         $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
44
45         if ($flink) {
46
47             $user = $flink->getUser();
48             common_set_user($user);
49
50             // If this is the first time the user has started the app
51             // prompt for Facebook status update permission
52             if (!$facebook->api_client->users_hasAppPermission('status_update')) {
53
54                 if ($facebook->api_client->data_getUserPreference(
55                         FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
56                     $this->getUpdatePermission();
57                     return;
58                 }
59             }
60
61             // Use is authenticated and has already been prompted once for
62             // Facebook status update permission? Then show the main page
63             // of the app
64             $this->showHome($flink, null);
65
66         } else {
67
68             // User hasn't authenticated yet, prompt for creds
69             $this->login($fbuid);
70         }
71
72     }
73
74     function login($fbuid)
75     {
76         $nickname = common_canonical_nickname($this->trimmed('nickname'));
77         $password = $this->arg('password');
78
79         $msg = null;
80
81         if ($nickname) {
82
83             if (common_check_user($nickname, $password)) {
84
85                 $user = User::staticGet('nickname', $nickname);
86
87                 if (!$user) {
88                     $this->showLoginForm(_("Server error - couldn't get user!"));
89                 }
90
91                 $flink = DB_DataObject::factory('foreign_link');
92                 $flink->user_id = $user->id;
93                 $flink->foreign_id = $fbuid;
94                 $flink->service = FACEBOOK_SERVICE;
95                 $flink->created = common_sql_now();
96                 $flink->set_flags(true, false, false);
97
98                 $flink_id = $flink->insert();
99
100                 // XXX: Do some error handling here
101
102                 $this->setDefaults();
103                 //$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
104
105                 $this->getUpdatePermission();
106                 return;
107
108             } else {
109                 $msg = _('Incorrect username or password.');
110             }
111         }
112
113         $this->showLoginForm($msg);
114
115     }
116
117     function setDefaults()
118     {
119         $facebook = get_facebook();
120
121         // A default prefix string for notices
122         $facebook->api_client->data_setUserPreference(
123             FACEBOOK_NOTICE_PREFIX, 'dented: ');
124         $facebook->api_client->data_setUserPreference(
125             FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
126     }
127
128     function showHome($flink, $msg)
129     {
130
131         $facebook = get_facebook();
132         $fbuid = $facebook->require_login();
133
134         $user = $flink->getUser();
135
136         $notice = $user->getCurrentNotice();
137         update_profile_box($facebook, $fbuid, $user, $notice);
138
139
140         $this->showHeader('Home');
141
142         if ($msg) {
143             common_element('fb:success', array('message' => $msg));
144         }
145
146         echo $this->show_notices($user);
147
148         $this->showFooter();
149     }
150
151     function show_notices($user)
152     {
153
154         $page = $this->trimmed('page');
155         if (!$page) {
156             $page = 1;
157         }
158
159         $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
160
161         $cnt = $this->show_notice_list($notice);
162
163         common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
164                           $page, 'all', array('nickname' => $user->nickname));
165     }
166
167     function show_notice_list($notice)
168     {
169         $nl = new FacebookNoticeList($notice);
170         return $nl->show();
171     }
172
173     function getUpdatePermission() {
174
175         $facebook = get_facebook();
176         $fbuid = $facebook->require_login();
177
178         start_fbml();
179
180         common_element('link', array('rel' => 'stylesheet',
181                                      'type' => 'text/css',
182                                      'href' => getFacebookBaseCSS()));
183
184         common_element('link', array('rel' => 'stylesheet',
185                                      'type' => 'text/css',
186                                      'href' => getFacebookThemeCSS()));
187
188         common_element('script', array('type' => 'text/javascript',
189                                        'src' => getFacebookJS()),
190                                        ' ');
191
192         $this->showLogo();
193
194         common_element_start('div', array('class' => 'content'));
195
196         // Figure what the URL of our app is.
197         $app_props = $facebook->api_client->Admin_getAppProperties(
198                 array('canvas_name', 'application_name'));
199         $app_url = 'http://apps.facebook.com/' . $app_props['canvas_name'] . '/index.php';
200         $app_name = $app_props['application_name'];
201
202         $instructions = sprintf(_('If you would like the %s app to automatically update ' .
203             'your Facebook status with your latest notice, you need ' .
204             'to give it permission.'), $app_name);
205
206         common_element_start('p');
207         common_element('span', array('id' => 'permissions_notice'), $instructions);
208         common_element_end('p');
209
210         common_element_start('form', array('method' => 'post',
211                                            'action' => $app_url,
212                                            'id' => 'facebook-skip-permissions'));
213
214         common_element_start('ul', array('id' => 'fb-permissions-list'));
215         common_element_start('li', array('id' => 'fb-permissions-item'));
216         common_element_start('fb:prompt-permission', array('perms' => 'status_update',
217             'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
218         common_element('span', array('class' => 'facebook-button'),
219             _('Allow Identi.ca to update my Facebook status'));
220         common_element_end('fb:prompt-permission');
221         common_element_end('li');
222
223         common_element_start('li', array('id' => 'fb-permissions-item'));
224         common_submit('skip', _('Skip'));
225         common_element_end('li');
226         common_element_end('ul');
227
228         common_element_end('form');
229         common_element_end('div');
230
231         common_end_xml();
232
233     }
234
235 }