]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Facebook/facebookhome.php
FacebookPlugin: Fix up FBML canvas app so it keeps working after
[quix0rs-gnu-social.git] / plugins / Facebook / facebookhome.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 require_once INSTALLDIR . '/plugins/Facebook/facebookaction.php';
25
26 class FacebookhomeAction extends FacebookAction
27 {
28     var $page = null;
29
30     function prepare($argarray)
31     {
32         parent::prepare($argarray);
33
34         $this->page = $this->trimmed('page');
35
36         if (!$this->page) {
37             $this->page = 1;
38         }
39
40         return true;
41     }
42
43     function handle($args)
44     {
45         parent::handle($args);
46
47         if (!empty($this->flink)) {
48             $this->user = $this->flink->getUser();
49
50             // If this is the first time the user has started the app
51             // prompt for Facebook status update permission
52             if (!$this->facebook->api_client->users_hasAppPermission('publish_stream')) {
53                 $this->getUpdatePermission();
54                 return;
55             }
56
57              if ($this->arg('status_submit') == 'Send') {
58                 $this->saveNewNotice();
59              }
60
61             // User is authenticated and has already been prompted once for
62             // Facebook status update permission? Then show the main page
63             // of the app
64             $this->showPage();
65         } else {
66             // User hasn't authenticated yet, prompt for creds
67             $this->login();
68         }
69     }
70
71     function login()
72     {
73         $this->showStylesheets();
74
75         $nickname = common_canonical_nickname($this->trimmed('nickname'));
76         $password = $this->arg('password');
77
78         $msg = null;
79
80         if ($nickname) {
81             if (common_check_user($nickname, $password)) {
82                 $user = User::staticGet('nickname', $nickname);
83
84                 if (!$user) {
85                     $this->showLoginForm(_m("Server error: Couldn't get user!"));
86                 }
87
88                 $flink = DB_DataObject::factory('foreign_link');
89                 $flink->user_id = $user->id;
90                 $flink->foreign_id = $this->fbuid;
91                 $flink->service = FACEBOOK_SERVICE;
92                 $flink->created = common_sql_now();
93                 $flink->set_flags(true, false, false, false);
94
95                 $flink_id = $flink->insert();
96
97                 // XXX: Do some error handling here
98
99                 $this->getUpdatePermission();
100                 return;
101             } else {
102                 $msg = _m('Incorrect username or password.');
103             }
104         }
105
106         $this->showLoginForm($msg);
107         $this->showFooter();
108     }
109
110     function showNoticeForm()
111     {
112         $post_action = "$this->app_uri/index.php";
113
114         $notice_form = new FacebookNoticeForm($this, $post_action, null,
115             $post_action, $this->user);
116         $notice_form->show();
117     }
118
119     function title()
120     {
121         if ($this->page > 1) {
122             // @todo FIXME: Core should have methods to get "Full name (nickname)" in a localised form
123             // so that this can be used consistenly throughout StatusNet without having to implement it
124             // over and over..
125             // TRANS: Page title.
126             // TRANS: %1$s is a user nickname, %2$s is a page number.
127             return sprintf(_m('%1$s and friends, page %2$d'), $this->user->nickname, $this->page);
128         } else {
129             // TRANS: Page title.
130             // TRANS: %s is a user nickname
131             return sprintf(_m("%s and friends"), $this->user->nickname);
132         }
133     }
134
135     function showContent()
136     {
137         $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
138
139         $nl = new NoticeList($notice, $this);
140
141         $cnt = $nl->show();
142
143         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
144                           $this->page, 'index.php', array('nickname' => $this->user->nickname));
145     }
146
147     function showNoticeList($notice)
148     {
149         $nl = new NoticeList($notice, $this);
150         return $nl->show();
151     }
152
153     function getUpdatePermission() {
154         $this->showStylesheets();
155
156         $this->elementStart('div', array('class' => 'facebook_guide'));
157
158         // TRANS: Instructions. %s is the application name.
159         $instructions = sprintf(_m('If you would like the %s app to automatically update ' .
160             'your Facebook status with your latest notice, you need ' .
161             'to give it permission.'), $this->app_name);
162
163         $this->elementStart('p');
164         $this->element('span', array('id' => 'permissions_notice'), $instructions);
165         $this->elementEnd('p');
166
167         $this->elementStart('form', array('method' => 'post',
168                                            'action' => "index.php",
169                                            'id' => 'facebook-skip-permissions'));
170
171         $this->elementStart('ul', array('id' => 'fb-permissions-list'));
172         $this->elementStart('li', array('id' => 'fb-permissions-item'));
173
174         $next = urlencode("$this->app_uri/index.php");
175         $api_key = common_config('facebook', 'apikey');
176
177         $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
178             $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
179             '&next_cancel=' . $next . '&submit=skip';
180
181         $this->elementStart('span', array('class' => 'facebook-button'));
182         // @todo FIXME: sprintf not needed here?
183         $this->element('a', array('href' => $auth_url),
184             sprintf(_m('Okay, do it!'), $this->app_name));
185         $this->elementEnd('span');
186
187         $this->elementEnd('li');
188
189         $this->elementStart('li', array('id' => 'fb-permissions-item'));
190         // TRANS: Button text. Clicking the button will skip updating Facebook permissions.
191         $this->submit('skip', _m('BUTTON','Skip'));
192         $this->elementEnd('li');
193         $this->elementEnd('ul');
194
195         $this->elementEnd('form');
196         $this->elementEnd('div');
197     }
198
199     /**
200      * Generate pagination links
201      *
202      * @param boolean $have_before is there something before?
203      * @param boolean $have_after  is there something after?
204      * @param integer $page        current page
205      * @param string  $action      current action
206      * @param array   $args        rest of query arguments
207      *
208      * @return nothing
209      */
210     function pagination($have_before, $have_after, $page, $action, $args=null)
211     {
212         // Does a little before-after block for next/prev page
213
214         // XXX: Fix so this uses common_local_url() if possible.
215
216         if ($have_before || $have_after) {
217             $this->elementStart('dl', 'pagination');
218             $this->element('dt', null, _m('Pagination'));
219             $this->elementStart('dd', null);
220             $this->elementStart('ul', array('class' => 'nav'));
221         }
222         if ($have_before) {
223             $pargs   = array('page' => $page-1);
224             $newargs = $args ? array_merge($args, $pargs) : $pargs;
225             $this->elementStart('li', array('class' => 'nav_prev'));
226             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
227                            // TRANS: Pagination link.
228                            _m('After'));
229             $this->elementEnd('li');
230         }
231         if ($have_after) {
232             $pargs   = array('page' => $page+1);
233             $newargs = $args ? array_merge($args, $pargs) : $pargs;
234             $this->elementStart('li', array('class' => 'nav_next'));
235             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
236                            // TRANS: Pagination link.
237                            _m('Before'));
238             $this->elementEnd('li');
239         }
240         if ($have_before || $have_after) {
241             $this->elementEnd('ul');
242             $this->elementEnd('dd');
243             $this->elementEnd('dl');
244         }
245     }
246 }