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 // Check to see whether there's already a Facebook link for this user
35 $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
37 // If the user has opted not to initially allow the app to have
38 // Facebook status update permission, store that preference. Only
39 // promt the user the first time she uses the app
40 if ($this->arg('skip')) {
41 $facebook->api_client->data_setUserPreference(
42 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
47 if ($_POST['submit'] == 'Send') {
48 $this->saveNewNotice($flink);
52 $user = $flink->getUser();
53 common_set_user($user);
55 // If this is the first time the user has started the app
56 // prompt for Facebook status update permission
57 if (!$facebook->api_client->users_hasAppPermission('status_update')) {
59 if ($facebook->api_client->data_getUserPreference(
60 FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
61 $this->getUpdatePermission();
66 // Use is authenticated and has already been prompted once for
67 // Facebook status update permission? Then show the main page
69 $this->showHome($flink, null);
73 // User hasn't authenticated yet, prompt for creds
79 function login($fbuid)
81 $nickname = common_canonical_nickname($this->trimmed('nickname'));
82 $password = $this->arg('password');
88 if (common_check_user($nickname, $password)) {
90 $user = User::staticGet('nickname', $nickname);
93 $this->showLoginForm(_("Server error - couldn't get user!"));
96 $flink = DB_DataObject::factory('foreign_link');
97 $flink->user_id = $user->id;
98 $flink->foreign_id = $fbuid;
99 $flink->service = FACEBOOK_SERVICE;
100 $flink->created = common_sql_now();
101 $flink->set_flags(true, false, false);
103 $flink_id = $flink->insert();
105 // XXX: Do some error handling here
107 $this->setDefaults();
108 //$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
110 $this->getUpdatePermission();
114 $msg = _('Incorrect username or password.');
118 $this->showLoginForm($msg);
122 function setDefaults()
124 $facebook = get_facebook();
126 // A default prefix string for notices
127 $facebook->api_client->data_setUserPreference(
128 FACEBOOK_NOTICE_PREFIX, 'dented: ');
129 $facebook->api_client->data_setUserPreference(
130 FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
133 function showHome($flink, $msg)
136 $facebook = get_facebook();
137 $fbuid = $facebook->require_login();
139 $user = $flink->getUser();
141 $notice = $user->getCurrentNotice();
142 update_profile_box($facebook, $fbuid, $user, $notice);
144 $this->showHeader($msg);
145 $this->showNoticeForm($user);
146 $this->showNav('Home');
148 echo $this->showNotices($user);
153 function showNotices($user)
156 $page = $this->trimmed('page');
161 $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
163 $cnt = $this->showNoticeList($notice);
165 facebookPagination($page > 1, $cnt > NOTICES_PER_PAGE,
166 $page, 'all', array('nickname' => $user->nickname));
169 function showNoticeList($notice)
171 $nl = new FacebookNoticeList($notice);
175 function getUpdatePermission() {
177 $facebook = get_facebook();
178 $fbuid = $facebook->require_login();
182 $this->showStylesheets();
183 $this->showScripts();
187 common_element_start('div', array('class' => 'content'));
189 // Figure what the URL of our app is.
190 $app_props = $facebook->api_client->Admin_getAppProperties(
191 array('canvas_name', 'application_name'));
192 $app_url = 'http://apps.facebook.com/' . $app_props['canvas_name'] . '/index.php';
193 $app_name = $app_props['application_name'];
195 $instructions = sprintf(_('If you would like the %s app to automatically update ' .
196 'your Facebook status with your latest notice, you need ' .
197 'to give it permission.'), $app_name);
199 common_element_start('p');
200 common_element('span', array('id' => 'permissions_notice'), $instructions);
201 common_element_end('p');
203 common_element_start('form', array('method' => 'post',
204 'action' => $app_url,
205 'id' => 'facebook-skip-permissions'));
207 common_element_start('ul', array('id' => 'fb-permissions-list'));
208 common_element_start('li', array('id' => 'fb-permissions-item'));
209 common_element_start('fb:prompt-permission', array('perms' => 'status_update',
210 'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
211 common_element('span', array('class' => 'facebook-button'),
212 _('Allow Identi.ca to update my Facebook status'));
213 common_element_end('fb:prompt-permission');
214 common_element_end('li');
216 common_element_start('li', array('id' => 'fb-permissions-item'));
217 common_submit('skip', _('Skip'));
218 common_element_end('li');
219 common_element_end('ul');
221 common_element_end('form');
222 common_element_end('div');
228 function saveNewNotice($flink)
231 $user = $flink->getUser();
233 $content = $_POST['status_textarea'];
236 $this->showHome($flink, _('No content!'));
239 $content_shortened = common_shorten_links($content);
241 if (mb_strlen($content_shortened) > 140) {
242 common_debug("Content = '$content_shortened'", __FILE__);
243 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
244 $this->showHome($flink, _('That\'s too long. Max notice size is 140 chars.'));
249 $inter = new CommandInterpreter();
251 $cmd = $inter->handle_command($user, $content_shortened);
254 $cmd->execute(new WebChannel());
258 $replyto = $this->trimmed('inreplyto');
260 $notice = Notice::saveNew($user->id, $content,
261 'Facebook', 1, ($replyto == 'false') ? null : $replyto);
263 if (is_string($notice)) {
264 $this->showHome($flink, 'Error!');
268 common_broadcast_notice($notice);
269 $this->showHome($flink, 'Success!');