]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/facebookhome.php
Merge branch '0.7.x' into 0.8.x
[quix0rs-gnu-social.git] / actions / facebookhome.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/lib/facebookaction.php';
23
24
25 class FacebookhomeAction extends FacebookAction
26 {
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
57             $this->user = $this->flink->getUser();
58
59             // If this is the first time the user has started the app
60             // prompt for Facebook status update permission
61             if (!$this->facebook->api_client->users_hasAppPermission('status_update')) {
62
63                  if ($this->facebook->api_client->data_getUserPreference(
64                     FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
65                         $this->getUpdatePermission();
66                         return;
67                  }
68              }
69
70              // Make sure the user's profile box has the lastest notice
71              $notice = $this->user->getCurrentNotice();
72              if ($notice) {
73                  $this->updateProfileBox($notice);
74              }
75
76              if ($this->arg('status_submit') == 'Send') {            
77                 $this->saveNewNotice();
78              }
79
80             // User is authenticated and has already been prompted once for
81             // Facebook status update permission? Then show the main page
82             // of the app
83             $this->showPage();
84             
85         } else {
86
87             // User hasn't authenticated yet, prompt for creds
88             $this->login();
89         }
90
91     }
92     
93     function login()
94     {
95         
96         $this->showStylesheets();
97         
98         $nickname = common_canonical_nickname($this->trimmed('nickname'));
99         $password = $this->arg('password');
100
101         $msg = null;
102
103         if ($nickname) {
104
105             if (common_check_user($nickname, $password)) {
106
107                 $user = User::staticGet('nickname', $nickname);
108
109                 if (!$user) {
110                     $this->showLoginForm(_("Server error - couldn't get user!"));
111                 }
112
113                 $flink = DB_DataObject::factory('foreign_link');
114                 $flink->user_id = $user->id;
115                 $flink->foreign_id = $this->fbuid;
116                 $flink->service = FACEBOOK_SERVICE;
117                 $flink->created = common_sql_now();
118                 $flink->set_flags(true, false, false, false);
119
120                 $flink_id = $flink->insert();
121
122                 // XXX: Do some error handling here
123
124                 $this->setDefaults();
125
126                 $this->getUpdatePermission();
127                 return;
128
129             } else {
130                 $msg = _('Incorrect username or password.');
131             }
132         }
133
134         $this->showLoginForm($msg);
135         $this->showFooter();
136
137     }
138
139     function setDefaults()
140     {
141         $this->facebook->api_client->data_setUserPreference(
142             FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
143     }
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(_("%s and friends, page %d"), $this->user->nickname, $this->page);
159         } else {
160             return sprintf(_("%s and friends"), $this->user->nickname);
161         }
162     }
163
164     function showContent()
165     {
166         $notice = $this->user->noticesWithFriends(($this->page-1) *
167             NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
168         
169         $nl = new NoticeList($notice, $this);
170
171         $cnt = $nl->show();
172
173         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
174                           $this->page, 'index.php', array('nickname' => $this->user->nickname));
175     }
176
177     function showNoticeList($notice)
178     {
179                 
180         $nl = new NoticeList($notice, $this);
181         return $nl->show();
182     }
183
184     function getUpdatePermission() {
185
186         $this->showStylesheets();
187
188         $this->elementStart('div', array('class' => 'facebook_guide'));
189
190         $instructions = sprintf(_('If you would like the %s app to automatically update ' .
191             'your Facebook status with your latest notice, you need ' .
192             'to give it permission.'), $this->app_name);
193
194         $this->elementStart('p');
195         $this->element('span', array('id' => 'permissions_notice'), $instructions);
196         $this->elementEnd('p');
197
198         $this->elementStart('form', array('method' => 'post',
199                                            'action' => "index.php",
200                                            'id' => 'facebook-skip-permissions'));
201
202         $this->elementStart('ul', array('id' => 'fb-permissions-list'));
203         $this->elementStart('li', array('id' => 'fb-permissions-item'));
204         
205         $next = urlencode("$this->app_uri/index.php");
206         $api_key = common_config('facebook', 'apikey');
207         
208         $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
209             $api_key . '&v=1.0&ext_perm=status_update&next=' . $next . 
210             '&next_cancel=' . $next . '&submit=skip';
211         
212         $this->elementStart('span', array('class' => 'facebook-button'));
213         $this->element('a', array('href' => $auth_url), 
214             sprintf(_('Okay, do it!'), $this->app_name));
215         $this->elementEnd('span');
216
217         $this->elementEnd('li');
218
219         $this->elementStart('li', array('id' => 'fb-permissions-item'));
220         $this->submit('skip', _('Skip'));
221         $this->elementEnd('li');
222         $this->elementEnd('ul');
223
224         $this->elementEnd('form');
225         $this->elementEnd('div');
226
227     }
228     
229     /**
230      * Generate pagination links
231      *
232      * @param boolean $have_before is there something before?
233      * @param boolean $have_after  is there something after?
234      * @param integer $page        current page
235      * @param string  $action      current action
236      * @param array   $args        rest of query arguments
237      *
238      * @return nothing
239      */
240     function pagination($have_before, $have_after, $page, $action, $args=null)
241     {
242                 
243         // Does a little before-after block for next/prev page
244      
245         // XXX: Fix so this uses common_local_url() if possible.
246      
247         if ($have_before || $have_after) {
248             $this->elementStart('div', array('class' => 'pagination'));
249             $this->elementStart('dl', null);
250             $this->element('dt', null, _('Pagination'));
251             $this->elementStart('dd', null);
252             $this->elementStart('ul', array('class' => 'nav'));
253         }
254         if ($have_before) {
255             $pargs   = array('page' => $page-1);
256             $newargs = $args ? array_merge($args, $pargs) : $pargs;
257             $this->elementStart('li', array('class' => 'nav_prev'));            
258             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
259                            _('After'));
260             $this->elementEnd('li');
261         }
262         if ($have_after) {
263             $pargs   = array('page' => $page+1);
264             $newargs = $args ? array_merge($args, $pargs) : $pargs;
265             $this->elementStart('li', array('class' => 'nav_next'));
266             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
267                            _('Before'));
268             $this->elementEnd('li');
269         }
270         if ($have_before || $have_after) {
271             $this->elementEnd('ul');
272             $this->elementEnd('dd');
273             $this->elementEnd('dl');
274             $this->elementEnd('div');
275         }
276     }
277     
278
279 }