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