]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/facebookhome.php
d2ac7617d1c76d0848bb4df0ab9e17c66a835781
[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 ($flink) {
38             $this->showHome($flink, null);
39         } else {
40             $this->login($fbuid);
41         }
42
43     }
44
45     function login($fbuid)
46     {
47         $nickname = common_canonical_nickname($this->trimmed('nickname'));
48         $password = $this->arg('password');
49
50         $msg = null;
51
52         if ($nickname) {
53
54             if (common_check_user($nickname, $password)) {
55
56                 $user = User::staticGet('nickname', $nickname);
57
58                 if (!$user) {
59                     $this->showLoginForm(_("Server error - couldn't get user!"));
60                 }
61
62                 $flink = DB_DataObject::factory('foreign_link');
63                 $flink->user_id = $user->id;
64                 $flink->foreign_id = $fbuid;
65                 $flink->service = FACEBOOK_SERVICE;
66                 $flink->created = common_sql_now();
67                 $flink->set_flags(true, false, false);
68
69                 $flink_id = $flink->insert();
70
71                 // XXX: Do some error handling here
72
73                 $this->setDefaults();
74
75                 $this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
76
77             } else {
78                 $msg = _('Incorrect username or password.');
79             }
80         }
81
82         $this->showLoginForm($msg);
83     }
84
85     function setDefaults()
86     {
87         $facebook = get_facebook();
88
89         // A default prefix string for notices
90         $facebook->api_client->data_setUserPreference(1, 'dented: ');
91     }
92
93     function showHome($flink, $msg)
94     {
95
96         $facebook = get_facebook();
97         $fbuid = $facebook->require_login();
98
99         $user = $flink->getUser();
100
101         $notice = $user->getCurrentNotice();
102         update_profile_box($facebook, $fbuid, $user, $notice);
103
104
105         $this->show_header('Home');
106
107         if ($msg) {
108             $this->element('fb:success', array('message' => $msg));
109         }
110
111         echo $this->show_notices($user);
112
113         $this->show_footer();
114     }
115
116     function show_notices($user)
117     {
118
119         $page = $this->trimmed('page');
120         if (!$page) {
121             $page = 1;
122         }
123
124         $notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
125
126         $cnt = $this->show_notice_list($notice);
127
128         common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
129                           $page, 'all', array('nickname' => $user->nickname));
130     }
131
132     function show_notice_list($notice)
133     {
134         $nl = new NoticeList($notice);
135         return $nl->show();
136     }
137
138 }