3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/lib/facebookaction.php';
24 class FacebookhomeAction extends FacebookAction
27 function handle($args)
29 parent::handle($args);
31 $facebook = get_facebook();
32 $fbuid = $facebook->require_login();
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');
42 // Check to see whether there's already a Facebook link for this user
43 $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
47 $user = $flink->getUser();
48 common_set_user($user);
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')) {
54 if ($facebook->api_client->data_getUserPreference(
55 FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
56 $this->getUpdatePermission();
61 // Use is authenticated and has already been prompted once for
62 // Facebook status update permission? Then show the main page
64 $this->showHome($flink, null);
68 // User hasn't authenticated yet, prompt for creds
74 function login($fbuid)
76 $nickname = common_canonical_nickname($this->trimmed('nickname'));
77 $password = $this->arg('password');
83 if (common_check_user($nickname, $password)) {
85 $user = User::staticGet('nickname', $nickname);
88 $this->showLoginForm(_("Server error - couldn't get user!"));
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);
98 $flink_id = $flink->insert();
100 // XXX: Do some error handling here
102 $this->setDefaults();
103 //$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
105 $this->getUpdatePermission();
109 $msg = _('Incorrect username or password.');
113 $this->showLoginForm($msg);
117 function setDefaults()
119 $facebook = get_facebook();
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');
128 function showHome($flink, $msg)
131 $facebook = get_facebook();
132 $fbuid = $facebook->require_login();
134 $user = $flink->getUser();
136 $notice = $user->getCurrentNotice();
137 update_profile_box($facebook, $fbuid, $user, $notice);
140 $this->showHeader('Home');
143 common_element('fb:success', array('message' => $msg));
146 echo $this->show_notices($user);
151 function show_notices($user)
154 $page = $this->trimmed('page');
159 $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
161 $cnt = $this->show_notice_list($notice);
163 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
164 $page, 'all', array('nickname' => $user->nickname));
167 function show_notice_list($notice)
169 $nl = new FacebookNoticeList($notice);
173 function getUpdatePermission() {
175 $facebook = get_facebook();
176 $fbuid = $facebook->require_login();
180 common_element('link', array('rel' => 'stylesheet',
181 'type' => 'text/css',
182 'href' => getFacebookCSS()));
186 common_element_start('div', array('class' => 'content'));
188 // Figure what the URL of our app is.
189 $app_props = $facebook->api_client->Admin_getAppProperties(
190 array('canvas_name', 'application_name'));
191 $app_url = 'http://apps.facebook.com/' . $app_props['canvas_name'] . '/index.php';
192 $app_name = $app_props['application_name'];
194 $instructions = sprintf(_('If you would like the %s app to automatically update ' .
195 'your Facebook status with your latest notice, you need ' .
196 'to give it permission.'), $app_name);
198 common_element_start('p');
199 common_element('span', array('id' => 'permissions_notice'), $instructions);
200 common_element_end('p');
202 common_element_start('form', array('method' => 'post',
203 'action' => $app_url,
204 'id' => 'facebook-skip-permissions'));
206 common_element_start('ul', array('id' => 'fb-permissions-list'));
207 common_element_start('li', array('id' => 'fb-permissions-item'));
208 common_element_start('fb:prompt-permission', array('perms' => 'status_update',
209 'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
210 common_element('span', array('class' => 'facebook-button'),
211 _('Allow Identi.ca to update my Facebook status'));
212 common_element_end('fb:prompt-permission');
213 common_element_end('li');
215 common_element_start('li', array('id' => 'fb-permissions-item'));
216 common_submit('skip', _('Skip'));
217 common_element_end('li');
218 common_element_end('ul');
220 common_element_end('form');
221 common_element_end('div');