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