]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Facebook/facebookhome.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[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
29     var $page = null;
30
31     function prepare($argarray)
32     {
33         parent::prepare($argarray);
34
35         $this->page = $this->trimmed('page');
36
37         if (!$this->page) {
38             $this->page = 1;
39         }
40
41         return true;
42     }
43
44     function handle($args)
45     {
46         parent::handle($args);
47
48         // If the user has opted not to initially allow the app to have
49         // Facebook status update permission, store that preference. Only
50         // promt the user the first time she uses the app
51         if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
52             $this->facebook->api_client->data_setUserPreference(
53                 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
54         }
55
56         if ($this->flink) {
57
58             $this->user = $this->flink->getUser();
59
60             // If this is the first time the user has started the app
61             // prompt for Facebook status update permission
62             if (!$this->facebook->api_client->users_hasAppPermission('publish_stream')) {
63
64                  if ($this->facebook->api_client->data_getUserPreference(
65                     FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
66                         $this->getUpdatePermission();
67                         return;
68                  }
69              }
70
71              // Make sure the user's profile box has the lastest notice
72              $notice = $this->user->getCurrentNotice();
73              if ($notice) {
74                  $this->updateProfileBox($notice);
75              }
76
77              if ($this->arg('status_submit') == 'Send') {
78                 $this->saveNewNotice();
79              }
80
81             // User is authenticated and has already been prompted once for
82             // Facebook status update permission? Then show the main page
83             // of the app
84             $this->showPage();
85
86         } else {
87
88             // User hasn't authenticated yet, prompt for creds
89             $this->login();
90         }
91
92     }
93
94     function login()
95     {
96
97         $this->showStylesheets();
98
99         $nickname = common_canonical_nickname($this->trimmed('nickname'));
100         $password = $this->arg('password');
101
102         $msg = null;
103
104         if ($nickname) {
105
106             if (common_check_user($nickname, $password)) {
107
108                 $user = User::staticGet('nickname', $nickname);
109
110                 if (!$user) {
111                     $this->showLoginForm(_m("Server error - couldn't get user!"));
112                 }
113
114                 $flink = DB_DataObject::factory('foreign_link');
115                 $flink->user_id = $user->id;
116                 $flink->foreign_id = $this->fbuid;
117                 $flink->service = FACEBOOK_SERVICE;
118                 $flink->created = common_sql_now();
119                 $flink->set_flags(true, false, false, false);
120
121                 $flink_id = $flink->insert();
122
123                 // XXX: Do some error handling here
124
125                 $this->setDefaults();
126
127                 $this->getUpdatePermission();
128                 return;
129
130             } else {
131                 $msg = _m('Incorrect username or password.');
132             }
133         }
134
135         $this->showLoginForm($msg);
136         $this->showFooter();
137
138     }
139
140     function setDefaults()
141     {
142         $this->facebook->api_client->data_setUserPreference(
143             FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
144     }
145
146     function showNoticeForm()
147     {
148         $post_action = "$this->app_uri/index.php";
149
150         $notice_form = new FacebookNoticeForm($this, $post_action, null,
151             $post_action, $this->user);
152         $notice_form->show();
153     }
154
155     function title()
156     {
157         if ($this->page > 1) {
158             return sprintf(_m("%s and friends, page %d"), $this->user->nickname, $this->page);
159         } else {
160             return sprintf(_m("%s and friends"), $this->user->nickname);
161         }
162     }
163
164     function showContent()
165     {
166         $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
167
168         $nl = new NoticeList($notice, $this);
169
170         $cnt = $nl->show();
171
172         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
173                           $this->page, 'index.php', array('nickname' => $this->user->nickname));
174     }
175
176     function showNoticeList($notice)
177     {
178
179         $nl = new NoticeList($notice, $this);
180         return $nl->show();
181     }
182
183     function getUpdatePermission() {
184
185         $this->showStylesheets();
186
187         $this->elementStart('div', array('class' => 'facebook_guide'));
188
189         $instructions = sprintf(_m('If you would like the %s app to automatically update ' .
190             'your Facebook status with your latest notice, you need ' .
191             'to give it permission.'), $this->app_name);
192
193         $this->elementStart('p');
194         $this->element('span', array('id' => 'permissions_notice'), $instructions);
195         $this->elementEnd('p');
196
197         $this->elementStart('form', array('method' => 'post',
198                                            'action' => "index.php",
199                                            'id' => 'facebook-skip-permissions'));
200
201         $this->elementStart('ul', array('id' => 'fb-permissions-list'));
202         $this->elementStart('li', array('id' => 'fb-permissions-item'));
203
204         $next = urlencode("$this->app_uri/index.php");
205         $api_key = common_config('facebook', 'apikey');
206
207         $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
208             $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
209             '&next_cancel=' . $next . '&submit=skip';
210
211         $this->elementStart('span', array('class' => 'facebook-button'));
212         $this->element('a', array('href' => $auth_url),
213             sprintf(_m('Okay, do it!'), $this->app_name));
214         $this->elementEnd('span');
215
216         $this->elementEnd('li');
217
218         $this->elementStart('li', array('id' => 'fb-permissions-item'));
219         $this->submit('skip', _m('Skip'));
220         $this->elementEnd('li');
221         $this->elementEnd('ul');
222
223         $this->elementEnd('form');
224         $this->elementEnd('div');
225
226     }
227
228     /**
229      * Generate pagination links
230      *
231      * @param boolean $have_before is there something before?
232      * @param boolean $have_after  is there something after?
233      * @param integer $page        current page
234      * @param string  $action      current action
235      * @param array   $args        rest of query arguments
236      *
237      * @return nothing
238      */
239     function pagination($have_before, $have_after, $page, $action, $args=null)
240     {
241
242         // Does a little before-after block for next/prev page
243
244         // XXX: Fix so this uses common_local_url() if possible.
245
246         if ($have_before || $have_after) {
247             $this->elementStart('dl', 'pagination');
248             $this->element('dt', null, _m('Pagination'));
249             $this->elementStart('dd', null);
250             $this->elementStart('ul', array('class' => 'nav'));
251         }
252         if ($have_before) {
253             $pargs   = array('page' => $page-1);
254             $newargs = $args ? array_merge($args, $pargs) : $pargs;
255             $this->elementStart('li', array('class' => 'nav_prev'));
256             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
257                            _m('After'));
258             $this->elementEnd('li');
259         }
260         if ($have_after) {
261             $pargs   = array('page' => $page+1);
262             $newargs = $args ? array_merge($args, $pargs) : $pargs;
263             $this->elementStart('li', array('class' => 'nav_next'));
264             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
265                            _m('Before'));
266             $this->elementEnd('li');
267         }
268         if ($have_before || $have_after) {
269             $this->elementEnd('ul');
270             $this->elementEnd('dd');
271             $this->elementEnd('dl');
272         }
273     }
274
275 }