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';
25 class FacebookhomeAction extends FacebookAction
30 function prepare($argarray)
32 parent::prepare($argarray);
34 $this->page = $this->trimmed('page');
43 function handle($args)
45 parent::handle($args);
47 // If the user has opted not to initially allow the app to have
48 // Facebook status update permission, store that preference. Only
49 // promt the user the first time she uses the app
50 if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
51 $this->facebook->api_client->data_setUserPreference(
52 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
57 $this->user = $this->flink->getUser();
59 // If this is the first time the user has started the app
60 // prompt for Facebook status update permission
61 if (!$this->facebook->api_client->users_hasAppPermission('status_update')) {
63 if ($this->facebook->api_client->data_getUserPreference(
64 FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
65 $this->getUpdatePermission();
70 // Make sure the user's profile box has the lastest notice
71 $notice = $this->user->getCurrentNotice();
73 $this->updateProfileBox($notice);
76 if ($this->arg('status_submit') == 'Send') {
77 $this->saveNewNotice();
80 // User is authenticated and has already been prompted once for
81 // Facebook status update permission? Then show the main page
87 // User hasn't authenticated yet, prompt for creds
96 $this->showStylesheets();
98 $nickname = common_canonical_nickname($this->trimmed('nickname'));
99 $password = $this->arg('password');
105 if (common_check_user($nickname, $password)) {
107 $user = User::staticGet('nickname', $nickname);
110 $this->showLoginForm(_("Server error - couldn't get user!"));
113 $flink = DB_DataObject::factory('foreign_link');
114 $flink->user_id = $user->id;
115 $flink->foreign_id = $this->fbuid;
116 $flink->service = FACEBOOK_SERVICE;
117 $flink->created = common_sql_now();
118 $flink->set_flags(true, false, false);
120 $flink_id = $flink->insert();
122 // XXX: Do some error handling here
124 $this->setDefaults();
125 //$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
127 $this->getUpdatePermission();
131 $msg = _('Incorrect username or password.');
135 $this->showLoginForm($msg);
139 function setDefaults()
141 // A default prefix string for notices
142 $this->facebook->api_client->data_setUserPreference(
143 FACEBOOK_NOTICE_PREFIX, 'dented: ');
144 $this->facebook->api_client->data_setUserPreference(
145 FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
149 function showNoticeForm()
152 $post_action = "$this->app_uri/index.php";
154 $notice_form = new FacebookNoticeForm($this, $post_action, null,
155 $post_action, $this->user);
156 $notice_form->show();
161 if ($this->page > 1) {
162 return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page);
164 return sprintf(_("%s and friends"), $this->user->nickname);
168 function showContent()
171 $notice = $this->user->noticesWithFriends(($this->page-1) *
172 NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
174 $nl = new NoticeList($notice, $this);
178 $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
179 $this->page, 'index.php', array('nickname' => $this->user->nickname));
183 function showNoticeList($notice)
186 $nl = new NoticeList($notice, $this);
190 function getUpdatePermission() {
192 $this->showStylesheets();
194 $this->elementStart('div', array('class' => 'facebook_guide'));
196 $instructions = sprintf(_('If you would like the %s app to automatically update ' .
197 'your Facebook status with your latest notice, you need ' .
198 'to give it permission.'), $this->app_name);
200 $this->elementStart('p');
201 $this->element('span', array('id' => 'permissions_notice'), $instructions);
202 $this->elementEnd('p');
204 $this->elementStart('form', array('method' => 'post',
205 'action' => "index.php",
206 'id' => 'facebook-skip-permissions'));
208 $this->elementStart('ul', array('id' => 'fb-permissions-list'));
209 $this->elementStart('li', array('id' => 'fb-permissions-item'));
211 $next = urlencode("$this->app_uri/index.php");
212 $api_key = common_config('facebook', 'apikey');
214 $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
215 $api_key . '&v=1.0&ext_perm=status_update&next=' . $next .
216 '&next_cancel=' . $next . '&submit=skip';
218 $this->elementStart('span', array('class' => 'facebook-button'));
219 $this->element('a', array('href' => $auth_url),
220 sprintf(_('Okay, do it!'), $this->app_name));
221 $this->elementEnd('span');
223 $this->elementEnd('li');
225 $this->elementStart('li', array('id' => 'fb-permissions-item'));
226 $this->submit('skip', _('Skip'));
227 $this->elementEnd('li');
228 $this->elementEnd('ul');
230 $this->elementEnd('form');
231 $this->elementEnd('div');
235 function saveNewNotice()
238 $user = $this->flink->getUser();
240 $content = $this->trimmed('status_textarea');
243 $this->showPage(_('No notice content!'));
246 $content_shortened = common_shorten_links($content);
248 if (mb_strlen($content_shortened) > 140) {
249 common_debug("Content = '$content_shortened'", __FILE__);
250 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
251 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
256 $inter = new CommandInterpreter();
258 $cmd = $inter->handle_command($user, $content_shortened);
264 $cmd->execute(new WebChannel());
268 $replyto = $this->trimmed('inreplyto');
270 $notice = Notice::saveNew($user->id, $content,
271 'Facebook', 1, ($replyto == 'false') ? null : $replyto);
273 if (is_string($notice)) {
274 $this->showPage($notice);
278 common_broadcast_notice($notice);
283 * Generate pagination links
285 * @param boolean $have_before is there something before?
286 * @param boolean $have_after is there something after?
287 * @param integer $page current page
288 * @param string $action current action
289 * @param array $args rest of query arguments
293 function pagination($have_before, $have_after, $page, $action, $args=null)
296 // Does a little before-after block for next/prev page
298 // XXX: Fix so this uses common_local_url() if possible.
300 if ($have_before || $have_after) {
301 $this->elementStart('div', array('class' => 'pagination'));
302 $this->elementStart('dl', null);
303 $this->element('dt', null, _('Pagination'));
304 $this->elementStart('dd', null);
305 $this->elementStart('ul', array('class' => 'nav'));
308 $pargs = array('page' => $page-1);
309 $newargs = $args ? array_merge($args, $pargs) : $pargs;
310 $this->elementStart('li', array('class' => 'nav_prev'));
311 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
313 $this->elementEnd('li');
316 $pargs = array('page' => $page+1);
317 $newargs = $args ? array_merge($args, $pargs) : $pargs;
318 $this->elementStart('li', array('class' => 'nav_next'));
319 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
321 $this->elementEnd('li');
323 if ($have_before || $have_after) {
324 $this->elementEnd('ul');
325 $this->elementEnd('dd');
326 $this->elementEnd('dl');
327 $this->elementEnd('div');