git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

facebookhome.php 8.68 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14 15 16
 * GNU Affero General Public License for more details.
 *
 * 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('STATUSNET')) { exit(1); }
21

22
require_once INSTALLDIR.'/lib/facebookaction.php';
23

24 25
class FacebookhomeAction extends FacebookAction
{
26

27
    var $page = null;
28

29
    function prepare($argarray)
30
    {
31
        parent::prepare($argarray);
32

33
        $this->page = $this->trimmed('page');
34

35 36 37
        if (!$this->page) {
            $this->page = 1;
        }
38

39 40 41
        return true;
    }

42 43
    function handle($args)
    {
44 45
        parent::handle($args);

46 47 48
        // 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
49
        if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
50
            $this->facebook->api_client->data_setUserPreference(
51 52 53
                FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
        }

54
        if ($this->flink) {
55

56
            $this->user = $this->flink->getUser();
57 58

            // If this is the first time the user has started the app
59
            // prompt for Facebook status update permission
60
            if (!$this->facebook->api_client->users_hasAppPermission('publish_stream')) {
61 62

                 if ($this->facebook->api_client->data_getUserPreference(
63 64 65
                    FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
                        $this->getUpdatePermission();
                        return;
66 67 68 69 70
                 }
             }

             // Make sure the user's profile box has the lastest notice
             $notice = $this->user->getCurrentNotice();
71 72 73
             if ($notice) {
                 $this->updateProfileBox($notice);
             }
74

75
             if ($this->arg('status_submit') == 'Send') {
76
                $this->saveNewNotice();
77
             }
78

79
            // User is authenticated and has already been prompted once for
80 81
            // Facebook status update permission? Then show the main page
            // of the app
82
            $this->showPage();
83

84
        } else {
85 86

            // User hasn't authenticated yet, prompt for creds
87
            $this->login();
88
        }
89

90
    }
91

92
    function login()
93
    {
94

95
        $this->showStylesheets();
96

97 98
        $nickname = common_canonical_nickname($this->trimmed('nickname'));
        $password = $this->arg('password');
99

100
        $msg = null;
101

102
        if ($nickname) {
103

104
            if (common_check_user($nickname, $password)) {
105

106
                $user = User::staticGet('nickname', $nickname);
107

108 109 110
                if (!$user) {
                    $this->showLoginForm(_("Server error - couldn't get user!"));
                }
111

112 113
                $flink = DB_DataObject::factory('foreign_link');
                $flink->user_id = $user->id;
114
                $flink->foreign_id = $this->fbuid;
115 116
                $flink->service = FACEBOOK_SERVICE;
                $flink->created = common_sql_now();
117
                $flink->set_flags(true, false, false, false);
118

119
                $flink_id = $flink->insert();
120

121
                // XXX: Do some error handling here
122

123 124
                $this->setDefaults();

125
                $this->getUpdatePermission();
126
                return;
127

128 129 130
            } else {
                $msg = _('Incorrect username or password.');
            }
131
        }
132

133
        $this->showLoginForm($msg);
134
        $this->showFooter();
135

136
    }
137

138 139
    function setDefaults()
    {
140
        $this->facebook->api_client->data_setUserPreference(
141
            FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
142 143
    }

144
    function showNoticeForm()
145
    {
146
        $post_action = "$this->app_uri/index.php";
147 148

        $notice_form = new FacebookNoticeForm($this, $post_action, null,
149 150 151
            $post_action, $this->user);
        $notice_form->show();
    }
152

153 154 155 156 157 158 159
    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);
        }
160
    }
161

162
    function showContent()
163
    {
164 165
        $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);

166
        $nl = new NoticeList($notice, $this);
167

168
        $cnt = $nl->show();
169

170 171
        $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
                          $this->page, 'index.php', array('nickname' => $this->user->nickname));
172
    }
173

174
    function showNoticeList($notice)
175
    {
176

177
        $nl = new NoticeList($notice, $this);
178
        return $nl->show();
179
    }
180

181 182
    function getUpdatePermission() {

183 184
        $this->showStylesheets();

185
        $this->elementStart('div', array('class' => 'facebook_guide'));
186 187 188

        $instructions = sprintf(_('If you would like the %s app to automatically update ' .
            'your Facebook status with your latest notice, you need ' .
189
            'to give it permission.'), $this->app_name);
190

191 192 193
        $this->elementStart('p');
        $this->element('span', array('id' => 'permissions_notice'), $instructions);
        $this->elementEnd('p');
194

195
        $this->elementStart('form', array('method' => 'post',
196
                                           'action' => "index.php",
197 198
                                           'id' => 'facebook-skip-permissions'));

199 200
        $this->elementStart('ul', array('id' => 'fb-permissions-list'));
        $this->elementStart('li', array('id' => 'fb-permissions-item'));
201

202 203
        $next = urlencode("$this->app_uri/index.php");
        $api_key = common_config('facebook', 'apikey');
204

205
        $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
206
            $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
207
            '&next_cancel=' . $next . '&submit=skip';
208

209
        $this->elementStart('span', array('class' => 'facebook-button'));
210
        $this->element('a', array('href' => $auth_url),
211 212 213
            sprintf(_('Okay, do it!'), $this->app_name));
        $this->elementEnd('span');

214
        $this->elementEnd('li');
215

216
        $this->elementStart('li', array('id' => 'fb-permissions-item'));
217
        $this->submit('skip', _('Skip'));
218 219
        $this->elementEnd('li');
        $this->elementEnd('ul');
220

221 222
        $this->elementEnd('form');
        $this->elementEnd('div');
223 224

    }
225

226 227 228 229 230 231 232 233 234 235 236 237 238
    /**
     * 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)
    {
239

240
        // Does a little before-after block for next/prev page
241

242
        // XXX: Fix so this uses common_local_url() if possible.
243

244 245 246 247 248 249 250 251 252 253
        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;
254
            $this->elementStart('li', array('class' => 'nav_prev'));
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
            $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');
        }
273
    }
274

275
}