]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/facebookhome.php
Merge branch 'master' of root@laconi.ca:/var/www/laconi.ca/software/laconica
[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);
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
136     }
137
138     function setDefaults()
139     {
140         // A default prefix string for notices
141         $this->facebook->api_client->data_setUserPreference(
142             FACEBOOK_NOTICE_PREFIX, 'dented: ');
143         $this->facebook->api_client->data_setUserPreference(
144             FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
145     }
146     
147
148     function showNoticeForm()
149     {
150         
151         $post_action = "$this->app_uri/index.php";
152         
153         $notice_form = new FacebookNoticeForm($this, $post_action, null, 
154             $post_action, $this->user);
155         $notice_form->show();
156     
157     
158     }
159
160     function title()
161     {
162         if ($this->page > 1) {
163             return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page);
164         } else {
165             return sprintf(_("%s and friends"), $this->user->nickname);
166         }
167     }
168
169     function showContent()
170     {
171
172         $notice = $this->user->noticesWithFriends(($this->page-1) *
173             NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
174         
175         $nl = new NoticeList($notice, $this);
176
177         $cnt = $nl->show();
178
179         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
180                           $this->page, 'index.php', array('nickname' => $this->user->nickname));
181
182     }
183
184     function showNoticeList($notice)
185     {
186                 
187         $nl = new NoticeList($notice, $this);
188         return $nl->show();
189     }
190
191     function getUpdatePermission() {
192
193         $this->showStylesheets();
194
195         $this->elementStart('div', array('class' => 'facebook_guide'));
196
197         $instructions = sprintf(_('If you would like the %s app to automatically update ' .
198             'your Facebook status with your latest notice, you need ' .
199             'to give it permission.'), $this->app_name);
200
201         $this->elementStart('p');
202         $this->element('span', array('id' => 'permissions_notice'), $instructions);
203         $this->elementEnd('p');
204
205         $this->elementStart('form', array('method' => 'post',
206                                            'action' => "index.php",
207                                            'id' => 'facebook-skip-permissions'));
208
209         $this->elementStart('ul', array('id' => 'fb-permissions-list'));
210         $this->elementStart('li', array('id' => 'fb-permissions-item'));
211         
212         $next = urlencode("$this->app_uri/index.php");
213         $api_key = common_config('facebook', 'apikey');
214         
215         $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
216             $api_key . '&v=1.0&ext_perm=status_update&next=' . $next . 
217             '&next_cancel=' . $next . '&submit=skip';
218         
219         $this->elementStart('span', array('class' => 'facebook-button'));
220         $this->element('a', array('href' => $auth_url), 
221             sprintf(_('Okay, do it!'), $this->app_name));
222         $this->elementEnd('span');
223
224         $this->elementEnd('li');
225
226         $this->elementStart('li', array('id' => 'fb-permissions-item'));
227         $this->submit('skip', _('Skip'));
228         $this->elementEnd('li');
229         $this->elementEnd('ul');
230
231         $this->elementEnd('form');
232         $this->elementEnd('div');
233
234     }
235
236     function saveNewNotice()
237     {
238
239         $user = $this->flink->getUser();
240
241         $content = $this->trimmed('status_textarea');
242         
243         if (!$content) {
244             $this->showPage(_('No notice content!'));
245             return;
246         } else {
247             $content_shortened = common_shorten_links($content);
248
249             if (mb_strlen($content_shortened) > 140) {
250                 common_debug("Content = '$content_shortened'", __FILE__);
251                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
252                 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
253                 return;
254             }
255         }
256
257         $inter = new CommandInterpreter();
258
259         $cmd = $inter->handle_command($user, $content_shortened);
260
261         if ($cmd) {
262             
263             // XXX fix this
264             
265             $cmd->execute(new WebChannel());
266             return;
267         }
268
269         $replyto = $this->trimmed('inreplyto');
270
271         $notice = Notice::saveNew($user->id, $content,
272             'Facebook', 1, ($replyto == 'false') ? null : $replyto);
273
274         if (is_string($notice)) {
275             $this->showPage($notice);
276             return;
277         }
278
279         common_broadcast_notice($notice);
280         
281     }
282     
283     /**
284      * Generate pagination links
285      *
286      * @param boolean $have_before is there something before?
287      * @param boolean $have_after  is there something after?
288      * @param integer $page        current page
289      * @param string  $action      current action
290      * @param array   $args        rest of query arguments
291      *
292      * @return nothing
293      */
294     function pagination($have_before, $have_after, $page, $action, $args=null)
295     {
296                 
297         // Does a little before-after block for next/prev page
298      
299         // XXX: Fix so this uses common_local_url() if possible.
300      
301         if ($have_before || $have_after) {
302             $this->elementStart('div', array('class' => 'pagination'));
303             $this->elementStart('dl', null);
304             $this->element('dt', null, _('Pagination'));
305             $this->elementStart('dd', null);
306             $this->elementStart('ul', array('class' => 'nav'));
307         }
308         if ($have_before) {
309             $pargs   = array('page' => $page-1);
310             $newargs = $args ? array_merge($args, $pargs) : $pargs;
311             $this->elementStart('li', array('class' => 'nav_prev'));            
312             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
313                            _('After'));
314             $this->elementEnd('li');
315         }
316         if ($have_after) {
317             $pargs   = array('page' => $page+1);
318             $newargs = $args ? array_merge($args, $pargs) : $pargs;
319             $this->elementStart('li', array('class' => 'nav_next'));
320             $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
321                            _('Before'));
322             $this->elementEnd('li');
323         }
324         if ($have_before || $have_after) {
325             $this->elementEnd('ul');
326             $this->elementEnd('dd');
327             $this->elementEnd('dl');
328             $this->elementEnd('div');
329         }
330     }
331     
332
333 }