]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/facebookhome.php
ae29ee1f841feb508807ae4eda4babd854aae0f2
[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 class FacebookhomeAction extends FacebookAction
25 {
26
27     function handle($args)
28     {
29         parent::handle($args);
30
31         $facebook = get_facebook();
32         $fbuid = $facebook->require_login();
33
34         // Check to see whether there's already a Facebook link for this user
35         $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
36
37         // If the user has opted not to initially allow the app to have
38         // Facebook status update permission, store that preference. Only
39         // promt the user the first time she uses the app
40         if ($this->arg('skip')) {
41             $facebook->api_client->data_setUserPreference(
42                 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
43         }
44
45         if ($flink) {
46
47             if ($_POST['submit'] == 'Send') {
48                 $this->saveNewNotice($flink);
49                 return;
50             }
51
52             $user = $flink->getUser();
53             common_set_user($user);
54
55             // If this is the first time the user has started the app
56             // prompt for Facebook status update permission
57             if (!$facebook->api_client->users_hasAppPermission('status_update')) {
58
59                 if ($facebook->api_client->data_getUserPreference(
60                         FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
61                     $this->getUpdatePermission();
62                     return;
63                 }
64             }
65
66             // Use is authenticated and has already been prompted once for
67             // Facebook status update permission? Then show the main page
68             // of the app
69             $this->showHome($flink, null);
70
71         } else {
72
73             // User hasn't authenticated yet, prompt for creds
74             $this->login($fbuid);
75         }
76
77     }
78
79     function login($fbuid)
80     {
81         $nickname = common_canonical_nickname($this->trimmed('nickname'));
82         $password = $this->arg('password');
83
84         $msg = null;
85
86         if ($nickname) {
87
88             if (common_check_user($nickname, $password)) {
89
90                 $user = User::staticGet('nickname', $nickname);
91
92                 if (!$user) {
93                     $this->showLoginForm(_("Server error - couldn't get user!"));
94                 }
95
96                 $flink = DB_DataObject::factory('foreign_link');
97                 $flink->user_id = $user->id;
98                 $flink->foreign_id = $fbuid;
99                 $flink->service = FACEBOOK_SERVICE;
100                 $flink->created = common_sql_now();
101                 $flink->set_flags(true, false, false);
102
103                 $flink_id = $flink->insert();
104
105                 // XXX: Do some error handling here
106
107                 $this->setDefaults();
108                 //$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
109
110                 $this->getUpdatePermission();
111                 return;
112
113             } else {
114                 $msg = _('Incorrect username or password.');
115             }
116         }
117
118         $this->showLoginForm($msg);
119
120     }
121
122     function setDefaults()
123     {
124         $facebook = get_facebook();
125
126         // A default prefix string for notices
127         $facebook->api_client->data_setUserPreference(
128             FACEBOOK_NOTICE_PREFIX, 'dented: ');
129         $facebook->api_client->data_setUserPreference(
130             FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
131     }
132
133     function showHome($flink, $msg)
134     {
135
136         $facebook = get_facebook();
137         $fbuid = $facebook->require_login();
138
139         $user = $flink->getUser();
140
141         $notice = $user->getCurrentNotice();
142         update_profile_box($facebook, $fbuid, $user, $notice);
143
144         $this->showHeader($msg);
145         $this->showNoticeForm($user);
146         $this->showNav('Home');
147
148         echo $this->showNotices($user);
149
150         $this->showFooter();
151     }
152
153     function showNotices($user)
154     {
155
156         $page = $this->trimmed('page');
157         if (!$page) {
158             $page = 1;
159         }
160
161         $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
162
163         $cnt = $this->showNoticeList($notice);
164
165         facebookPagination($page > 1, $cnt > NOTICES_PER_PAGE,
166             $page, 'all', array('nickname' => $user->nickname));
167     }
168
169     function showNoticeList($notice)
170     {
171         $nl = new FacebookNoticeList($notice);
172         return $nl->show();
173     }
174
175     function getUpdatePermission() {
176
177         $facebook = get_facebook();
178         $fbuid = $facebook->require_login();
179
180         startFBML();
181
182         $this->showStylesheets();
183         $this->showScripts();
184
185         $this->showLogo();
186
187         common_element_start('div', array('class' => 'content'));
188
189         // Figure what the URL of our app is.
190         $app_props = $facebook->api_client->Admin_getAppProperties(
191                 array('canvas_name', 'application_name'));
192         $app_url = 'http://apps.facebook.com/' . $app_props['canvas_name'] . '/index.php';
193         $app_name = $app_props['application_name'];
194
195         $instructions = sprintf(_('If you would like the %s app to automatically update ' .
196             'your Facebook status with your latest notice, you need ' .
197             'to give it permission.'), $app_name);
198
199         common_element_start('p');
200         common_element('span', array('id' => 'permissions_notice'), $instructions);
201         common_element_end('p');
202
203         common_element_start('form', array('method' => 'post',
204                                            'action' => $app_url,
205                                            'id' => 'facebook-skip-permissions'));
206
207         common_element_start('ul', array('id' => 'fb-permissions-list'));
208         common_element_start('li', array('id' => 'fb-permissions-item'));
209         common_element_start('fb:prompt-permission', array('perms' => 'status_update',
210             'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
211         common_element('span', array('class' => 'facebook-button'),
212             _('Allow Identi.ca to update my Facebook status'));
213         common_element_end('fb:prompt-permission');
214         common_element_end('li');
215
216         common_element_start('li', array('id' => 'fb-permissions-item'));
217         common_submit('skip', _('Skip'));
218         common_element_end('li');
219         common_element_end('ul');
220
221         common_element_end('form');
222         common_element_end('div');
223
224         common_end_xml();
225
226     }
227
228     function saveNewNotice($flink)
229     {
230
231         $user = $flink->getUser();
232
233         $content = $_POST['status_textarea'];
234
235         if (!$content) {
236             $this->showHome($flink, _('No content!'));
237             return;
238         } else {
239             $content_shortened = common_shorten_links($content);
240
241             if (mb_strlen($content_shortened) > 140) {
242                 common_debug("Content = '$content_shortened'", __FILE__);
243                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
244                 $this->showHome($flink, _('That\'s too long. Max notice size is 140 chars.'));
245                 return;
246             }
247         }
248
249         $inter = new CommandInterpreter();
250
251         $cmd = $inter->handle_command($user, $content_shortened);
252
253         if ($cmd) {
254             $cmd->execute(new WebChannel());
255             return;
256         }
257
258         $replyto = $this->trimmed('inreplyto');
259
260         $notice = Notice::saveNew($user->id, $content,
261             'Facebook', 1, ($replyto == 'false') ? null : $replyto);
262
263         if (is_string($notice)) {
264             $this->showHome($flink, 'Error!');
265             return;
266         }
267
268         common_broadcast_notice($notice);
269         $this->showHome($flink, 'Success!');
270     }
271
272 }