X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=actions%2Ffacebookhome.php;h=34989c9786aa9814f9396d49bcba0db3934e9af3;hb=9f079764aa945e9e126ffa41cece17bc8951fb78;hp=8ee2d4cd36812392931078833652f4b86d9ebfc6;hpb=1a8869424b9640a1d6a689e7882f04c757f78b38;p=quix0rs-gnu-social.git diff --git a/actions/facebookhome.php b/actions/facebookhome.php index 8ee2d4cd36..34989c9786 100644 --- a/actions/facebookhome.php +++ b/actions/facebookhome.php @@ -1,7 +1,7 @@ . + * along with this program. If not, see . */ if (!defined('LACONICA')) { exit(1); } -require_once(INSTALLDIR.'/lib/facebookaction.php'); +require_once INSTALLDIR.'/lib/facebookaction.php'; -class FacebookhomeAction extends FacebookAction { +class FacebookhomeAction extends FacebookAction +{ - function handle($args) { - parent::handle($args); + var $page = null; - $this->login(); - } + function prepare($argarray) + { + parent::prepare($argarray); - function login() { + $this->page = $this->trimmed('page'); - $user = null; + if (!$this->page) { + $this->page = 1; + } - $facebook = $this->get_facebook(); - $fbuid = $facebook->require_login(); + return true; + } - # check to see whether there's already a Facebook link for this user - $flink = Foreign_link::getByForeignID($fbuid, 2); // 2 == Facebook + function handle($args) + { + parent::handle($args); - if ($flink) { + // If the user has opted not to initially allow the app to have + // Facebook status update permission, store that preference. Only + // promt the user the first time she uses the app + if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') { + $this->facebook->api_client->data_setUserPreference( + FACEBOOK_PROMPTED_UPDATE_PREF, 'true'); + } - $user = $flink->getUser(); - $this->show_home($facebook, $fbuid, $user); + if ($this->flink) { - } else { + $this->user = $this->flink->getUser(); - # Make the user put in her Laconica creds - $nickname = common_canonical_nickname($this->trimmed('nickname')); - $password = $this->arg('password'); + // If this is the first time the user has started the app + // prompt for Facebook status update permission + if (!$this->facebook->api_client->users_hasAppPermission('status_update')) { - if ($nickname) { + if ($this->facebook->api_client->data_getUserPreference( + FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') { + $this->getUpdatePermission(); + return; + } + } - if (common_check_user($nickname, $password)) { + // Make sure the user's profile box has the lastest notice + $notice = $this->user->getCurrentNotice(); + if ($notice) { + $this->updateProfileBox($notice); + } + if ($this->arg('status_submit') == 'Send') { + $this->saveNewNotice(); + } - $user = User::staticGet('nickname', $nickname); + // User is authenticated and has already been prompted once for + // Facebook status update permission? Then show the main page + // of the app + $this->showPage(); - if (!$user) { - echo ''; - $this->show_login_form(); - } + } else { - $flink = DB_DataObject::factory('foreign_link'); - $flink->user_id = $user->id; - $flink->foreign_id = $fbuid; - $flink->service = 2; # Facebook - $flink->created = common_sql_now(); + // User hasn't authenticated yet, prompt for creds + $this->login(); + } - # $this->set_flags($flink, $noticesync, $replysync, $friendsync); + } - $flink_id = $flink->insert(); + function login() + { - if ($flink_id) { - echo ''; - } + $this->showStylesheets(); - $this->show_home($facebook, $fbuid, $user); + $nickname = common_canonical_nickname($this->trimmed('nickname')); + $password = $this->arg('password'); - return; - } else { - echo ''; - } - } + $msg = null; - $this->show_login_form(); - } + if ($nickname) { - } + if (common_check_user($nickname, $password)) { - function show_home($facebook, $fbuid, $user) { + $user = User::staticGet('nickname', $nickname); - $this->show_header('Home'); + if (!$user) { + $this->showLoginForm(_("Server error - couldn't get user!")); + } - echo $this->show_notices($user); - $this->update_profile_box($facebook, $fbuid, $user); + $flink = DB_DataObject::factory('foreign_link'); + $flink->user_id = $user->id; + $flink->foreign_id = $this->fbuid; + $flink->service = FACEBOOK_SERVICE; + $flink->created = common_sql_now(); + $flink->set_flags(true, false, false, false); - $this->show_footer(); - } + $flink_id = $flink->insert(); - function show_notices($user) { + // XXX: Do some error handling here - $page = $this->trimmed('page'); - if (!$page) { - $page = 1; - } + $this->setDefaults(); - $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); + $this->getUpdatePermission(); + return; - echo '
    '; + } else { + $msg = _('Incorrect username or password.'); + } + } - $cnt = 0; + $this->showLoginForm($msg); + $this->showFooter(); - while ($notice->fetch() && $cnt <= NOTICES_PER_PAGE) { - $cnt++; + } - if ($cnt > NOTICES_PER_PAGE) { - break; - } + function setDefaults() + { + $this->facebook->api_client->data_setUserPreference( + FACEBOOK_PROMPTED_UPDATE_PREF, 'false'); + } - echo $this->render_notice($notice); - } + function showNoticeForm() + { + $post_action = "$this->app_uri/index.php"; - echo '
      '; + $notice_form = new FacebookNoticeForm($this, $post_action, null, + $post_action, $this->user); + $notice_form->show(); + } - $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE, - $page, 'index.php', array('nickname' => $user->nickname)); + function title() + { + if ($this->page > 1) { + return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page); + } else { + return sprintf(_("%s and friends"), $this->user->nickname); + } + } - } + function showContent() + { + $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); + + $nl = new NoticeList($notice, $this); + + $cnt = $nl->show(); + + $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE, + $this->page, 'index.php', array('nickname' => $this->user->nickname)); + } + + function showNoticeList($notice) + { + + $nl = new NoticeList($notice, $this); + return $nl->show(); + } + + function getUpdatePermission() { + + $this->showStylesheets(); + + $this->elementStart('div', array('class' => 'facebook_guide')); + + $instructions = sprintf(_('If you would like the %s app to automatically update ' . + 'your Facebook status with your latest notice, you need ' . + 'to give it permission.'), $this->app_name); + + $this->elementStart('p'); + $this->element('span', array('id' => 'permissions_notice'), $instructions); + $this->elementEnd('p'); + + $this->elementStart('form', array('method' => 'post', + 'action' => "index.php", + 'id' => 'facebook-skip-permissions')); + + $this->elementStart('ul', array('id' => 'fb-permissions-list')); + $this->elementStart('li', array('id' => 'fb-permissions-item')); + + $next = urlencode("$this->app_uri/index.php"); + $api_key = common_config('facebook', 'apikey'); + + $auth_url = 'http://www.facebook.com/authorize.php?api_key=' . + $api_key . '&v=1.0&ext_perm=status_update&next=' . $next . + '&next_cancel=' . $next . '&submit=skip'; + + $this->elementStart('span', array('class' => 'facebook-button')); + $this->element('a', array('href' => $auth_url), + sprintf(_('Okay, do it!'), $this->app_name)); + $this->elementEnd('span'); + + $this->elementEnd('li'); + + $this->elementStart('li', array('id' => 'fb-permissions-item')); + $this->submit('skip', _('Skip')); + $this->elementEnd('li'); + $this->elementEnd('ul'); + + $this->elementEnd('form'); + $this->elementEnd('div'); + + } + + /** + * Generate pagination links + * + * @param boolean $have_before is there something before? + * @param boolean $have_after is there something after? + * @param integer $page current page + * @param string $action current action + * @param array $args rest of query arguments + * + * @return nothing + */ + function pagination($have_before, $have_after, $page, $action, $args=null) + { + + // Does a little before-after block for next/prev page + + // XXX: Fix so this uses common_local_url() if possible. + + if ($have_before || $have_after) { + $this->elementStart('div', array('class' => 'pagination')); + $this->elementStart('dl', null); + $this->element('dt', null, _('Pagination')); + $this->elementStart('dd', null); + $this->elementStart('ul', array('class' => 'nav')); + } + if ($have_before) { + $pargs = array('page' => $page-1); + $newargs = $args ? array_merge($args, $pargs) : $pargs; + $this->elementStart('li', array('class' => 'nav_prev')); + $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'), + _('After')); + $this->elementEnd('li'); + } + if ($have_after) { + $pargs = array('page' => $page+1); + $newargs = $args ? array_merge($args, $pargs) : $pargs; + $this->elementStart('li', array('class' => 'nav_next')); + $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'), + _('Before')); + $this->elementEnd('li'); + } + if ($have_before || $have_after) { + $this->elementEnd('ul'); + $this->elementEnd('dd'); + $this->elementEnd('dl'); + $this->elementEnd('div'); + } + } }