]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/facebookhome.php
change LACONICA to STATUSNET
[quix0rs-gnu-social.git] / actions / 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')) { exit(1); }
21
22 require_once INSTALLDIR.'/lib/facebookaction.php';
23
24 class FacebookhomeAction extends FacebookAction
25 {
26
27     var $page = null;
28
29     function prepare($argarray)
30     {
31         parent::prepare($argarray);
32
33         $this->page = $this->trimmed('page');
34
35         if (!$this->page) {
36             $this->page = 1;
37         }
38
39         return true;
40     }
41
42     function handle($args)
43     {
44         parent::handle($args);
45
46         // If the user has opted not to initially allow the app to have
47         // Facebook status update permission, store that preference. Only
48         // 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                 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
52         }
53
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                     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
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         $nickname = common_canonical_nickname($this->trimmed('nickname'));
98         $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                 if (!$user) {
109                     $this->showLoginForm(_("Server error - couldn't get user!"));
110                 }
111
112                 $flink = DB_DataObject::factory('foreign_link');
113                 $flink->user_id = $user->id;
114                 $flink->foreign_id = $this->fbuid;
115                 $flink->service = FACEBOOK_SERVICE;
116                 $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                 $this->setDefaults();
124
125                 $this->getUpdatePermission();
126                 return;
127
128             } else {
129                 $msg = _('Incorrect username or password.');
130             }
131         }
132
133         $this->showLoginForm($msg);
134         $this->showFooter();
135
136     }
137
138     function setDefaults()
139     {
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             $post_action, $this->user);
150         $notice_form->show();
151     }
152
153     function title()
154     {
155         if ($this->page > 1) {
156             return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page);
157         } else {
158             return sprintf(_("%s and friends"), $this->user->nickname);
159         }
160     }
161
162     function showContent()
163     {
164         $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
165
166         $nl = new NoticeList($notice, $this);
167
168         $cnt = $nl->show();
169
170         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
171                           $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     function getUpdatePermission() {
182
183         $this->showStylesheets();
184
185         $this->elementStart('div', array('class' => 'facebook_guide'));
186
187         $instructions = sprintf(_('If you would like the %s app to automatically update ' .
188             'your Facebook status with your latest notice, you need ' .
189             'to give it permission.'), $this->app_name);
190
191         $this->elementStart('p');
192         $this->element('span', array('id' => 'permissions_notice'), $instructions);
193         $this->elementEnd('p');
194
195         $this->elementStart('form', array('method' => 'post',
196                                            'action' => "index.php",
197                                            'id' => 'facebook-skip-permissions'));
198
199         $this->elementStart('ul', array('id' => 'fb-permissions-list'));
200         $this->elementStart('li', array('id' => 'fb-permissions-item'));
201
202         $next = urlencode("$this->app_uri/index.php");
203         $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             sprintf(_('Okay, do it!'), $this->app_name));
212         $this->elementEnd('span');
213
214         $this->elementEnd('li');
215
216         $this->elementStart('li', array('id' => 'fb-permissions-item'));
217         $this->submit('skip', _('Skip'));
218         $this->elementEnd('li');
219         $this->elementEnd('ul');
220
221         $this->elementEnd('form');
222         $this->elementEnd('div');
223
224     }
225
226     /**
227      * Generate pagination links
228      *
229      * @param boolean $have_before is there something before?
230      * @param boolean $have_after  is there something after?
231      * @param integer $page        current page
232      * @param string  $action      current action
233      * @param array   $args        rest of query arguments
234      *
235      * @return nothing
236      */
237     function pagination($have_before, $have_after, $page, $action, $args=null)
238     {
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         if ($have_before || $have_after) {
245             $this->elementStart('div', array('class' => 'pagination'));
246             $this->elementStart('dl', null);
247             $this->element('dt', null, _('Pagination'));
248             $this->elementStart('dd', null);
249             $this->elementStart('ul', array('class' => 'nav'));
250         }
251         if ($have_before) {
252             $pargs   = array('page' => $page-1);
253             $newargs = $args ? array_merge($args, $pargs) : $pargs;
254             $this->elementStart('li', array('class' => 'nav_prev'));
255             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
256                            _('After'));
257             $this->elementEnd('li');
258         }
259         if ($have_after) {
260             $pargs   = array('page' => $page+1);
261             $newargs = $args ? array_merge($args, $pargs) : $pargs;
262             $this->elementStart('li', array('class' => 'nav_next'));
263             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
264                            _('Before'));
265             $this->elementEnd('li');
266         }
267         if ($have_before || $have_after) {
268             $this->elementEnd('ul');
269             $this->elementEnd('dd');
270             $this->elementEnd('dl');
271             $this->elementEnd('div');
272         }
273     }
274
275 }