summaryrefslogtreecommitdiff
path: root/actions/facebookhome.php
blob: 4c2b26355ca65a2c469155d270438327dd25fbf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
 *
 * 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.     If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('LACONICA')) { exit(1); }

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


class FacebookhomeAction extends FacebookAction
{

    var $page = null;
    
    function prepare($argarray)
    {        
        parent::prepare($argarray);
        
        $this->page = $this->trimmed('page');
       
        if (!$this->page) {
            $this->page = 1;
        }
        
        return true;
    }

    function handle($args)
    {
        parent::handle($args);        
                
        // 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');
        }

        if ($this->flink) {

            $this->user = $this->flink->getUser();

            // 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 ($this->facebook->api_client->data_getUserPreference(
                    FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
                        $this->getUpdatePermission();
                        return;
                 }
             }

             // 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 is authenticated and has already been prompted once for
            // Facebook status update permission? Then show the main page
            // of the app
            $this->showPage();
            
        } else {

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

    }
    
    function login()
    {
        
        $this->showStylesheets();
        
        $nickname = common_canonical_nickname($this->trimmed('nickname'));
        $password = $this->arg('password');

        $msg = null;

        if ($nickname) {

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

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

                if (!$user) {
                    $this->showLoginForm(_("Server error - couldn't get 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);

                $flink_id = $flink->insert();

                // XXX: Do some error handling here

                $this->setDefaults();

                $this->getUpdatePermission();
                return;

            } else {
                $msg = _('Incorrect username or password.');
            }
        }

        $this->showLoginForm($msg);
        $this->showFooter();

    }

    function setDefaults()
    {
        $this->facebook->api_client->data_setUserPreference(
            FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
    }
    

    function showNoticeForm()
    {
        $post_action = "$this->app_uri/index.php";
        
        $notice_form = new FacebookNoticeForm($this, $post_action, null, 
            $post_action, $this->user);
        $notice_form->show();
    }

    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->noticesWithFriends(($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');
        }
    }
    

}