]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookutil.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / lib / facebookutil.php
1 <?php
2 /*
3  * StatusNet - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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 require_once INSTALLDIR.'/extlib/facebook/facebook.php';
21 require_once INSTALLDIR.'/lib/facebookaction.php';
22 require_once INSTALLDIR.'/lib/noticelist.php';
23
24 define("FACEBOOK_SERVICE", 2); // Facebook is foreign_service ID 2
25 define("FACEBOOK_NOTICE_PREFIX", 1);
26 define("FACEBOOK_PROMPTED_UPDATE_PREF", 2);
27
28 function getFacebook()
29 {
30     static $facebook = null;
31
32     $apikey = common_config('facebook', 'apikey');
33     $secret = common_config('facebook', 'secret');
34
35     if ($facebook === null) {
36         $facebook = new Facebook($apikey, $secret);
37     }
38
39     if (empty($facebook)) {
40         common_log(LOG_ERR, 'Could not make new Facebook client obj!',
41             __FILE__);
42     }
43
44     return $facebook;
45 }
46
47 function isFacebookBound($notice, $flink) {
48
49     if (empty($flink)) {
50         return false;
51     }
52
53     // Avoid a loop
54
55     if ($notice->source == 'Facebook') {
56         common_log(LOG_INFO, "Skipping notice $notice->id because its " .
57                    'source is Facebook.');
58         return false;
59     }
60
61     // If the user does not want to broadcast to Facebook, move along
62
63     if (!($flink->noticesync & FOREIGN_NOTICE_SEND == FOREIGN_NOTICE_SEND)) {
64         common_log(LOG_INFO, "Skipping notice $notice->id " .
65             'because user has FOREIGN_NOTICE_SEND bit off.');
66         return false;
67     }
68
69     // If it's not a reply, or if the user WANTS to send @-replies,
70     // then, yeah, it can go to Facebook.
71
72     if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
73         ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
74         return true;
75     }
76
77     return false;
78
79 }
80
81 function facebookBroadcastNotice($notice)
82 {
83     $facebook = getFacebook();
84     $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
85
86     if (isFacebookBound($notice, $flink)) {
87
88         // Okay, we're good to go, update the FB status
89
90         $status = null;
91         $fbuid = $flink->foreign_id;
92         $user = $flink->getUser();
93         $attachments  = $notice->attachments();
94
95         try {
96
97             // Get the status 'verb' (prefix) the user has set
98
99             // XXX: Does this call count against our per user FB request limit?
100             // If so we should consider storing verb elsewhere or not storing
101
102             $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX,
103                                                                     $fbuid);
104
105             $status = "$prefix $notice->content";
106
107             $can_publish = $facebook->api_client->users_hasAppPermission('publish_stream',
108                                                                          $fbuid);
109
110             $can_update  = $facebook->api_client->users_hasAppPermission('status_update',
111                                                                          $fbuid);
112
113             if (!empty($attachments) && $can_publish == 1) {
114                 $fbattachment = format_attachments($attachments);
115                 $facebook->api_client->stream_publish($status, $fbattachment,
116                                                       null, null, $fbuid);
117                 common_log(LOG_INFO,
118                            "Posted notice $notice->id w/attachment " .
119                            "to Facebook user's stream (fbuid = $fbuid).");
120             } elseif ($can_update == 1 || $can_publish == 1) {
121                 $facebook->api_client->users_setStatus($status, $fbuid, false, true);
122                 common_log(LOG_INFO,
123                            "Posted notice $notice->id to Facebook " .
124                            "as a status update (fbuid = $fbuid).");
125             } else {
126                 $msg = "Not sending notice $notice->id to Facebook " .
127                   "because user $user->nickname hasn't given the " .
128                   'Facebook app \'status_update\' or \'publish_stream\' permission.';
129                 common_log(LOG_WARNING, $msg);
130             }
131
132             // Finally, attempt to update the user's profile box
133
134             if ($can_publish == 1 || $can_update == 1) {
135                 updateProfileBox($facebook, $flink, $notice);
136             }
137
138         } catch (FacebookRestClientException $e) {
139
140             $code = $e->getCode();
141
142             common_log(LOG_WARNING, 'Facebook returned error code ' .
143                        $code . ': ' . $e->getMessage());
144             common_log(LOG_WARNING,
145                        'Unable to update Facebook status for ' .
146                        "$user->nickname (user id: $user->id)!");
147
148             if ($code == 200 || $code == 250) {
149
150                 // 200 The application does not have permission to operate on the passed in uid parameter.
151                 // 250 Updating status requires the extended permission status_update or publish_stream.
152                 // see: http://wiki.developers.facebook.com/index.php/Users.setStatus#Example_Return_XML
153
154                 remove_facebook_app($flink);
155
156             } else {
157
158                 // Try sending again later.
159
160                 return false;
161             }
162
163         }
164     }
165
166     return true;
167
168 }
169
170 function updateProfileBox($facebook, $flink, $notice) {
171     $fbaction = new FacebookAction($output = 'php://output',
172                                    $indent = true, $facebook, $flink);
173     $fbaction->updateProfileBox($notice);
174 }
175
176 function format_attachments($attachments)
177 {
178     $fbattachment          = array();
179     $fbattachment['media'] = array();
180
181     // Facebook only supports one attachment per item
182
183     $attachment = $attachments[0];
184     $fbmedia    = array();
185
186     if (strncmp($attachment->mimetype, 'image/', strlen('image/')) == 0) {
187         $fbmedia['type']         = 'image';
188         $fbmedia['src']          = $attachment->url;
189         $fbmedia['href']         = $attachment->url;
190         $fbattachment['media'][] = $fbmedia;
191     } else if ($attachment->mimetype == 'audio/mpeg') {
192         $fbmedia['type']         = 'mp3';
193         $fbmedia['src']          = $attachment->url;
194         $fbattachment['media'][] = $fbmedia;
195     }else if ($attachment->mimetype == 'application/x-shockwave-flash') {
196         $fbmedia['type']         = 'flash';
197
198         // http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29
199         // says that imgsrc is required... but we have no value to put in it
200         // $fbmedia['imgsrc']='';
201
202         $fbmedia['swfsrc']       = $attachment->url;
203         $fbattachment['media'][] = $fbmedia;
204     }else{
205         $fbattachment['name'] = ($attachment->title ?
206                                  $attachment->title : $attachment->url);
207         $fbattachment['href'] = $attachment->url;
208     }
209
210     return $fbattachment;
211 }
212
213 function remove_facebook_app($flink)
214 {
215
216     $user = $flink->getUser();
217
218     common_log(LOG_INFO, 'Removing Facebook App Foreign link for ' .
219         "user $user->nickname (user id: $user->id).");
220
221     $result = $flink->delete();
222
223     if (empty($result)) {
224         common_log(LOG_ERR, 'Could not remove Facebook App ' .
225             "Foreign_link for $user->nickname (user id: $user->id)!");
226         common_log_db_error($flink, 'DELETE', __FILE__);
227     }
228
229     // Notify the user that we are removing their FB app access
230
231     $result = mail_facebook_app_removed($user);
232
233     if (!$result) {
234
235         $msg = 'Unable to send email to notify ' .
236             "$user->nickname (user id: $user->id) " .
237             'that their Facebook app link was ' .
238             'removed!';
239
240         common_log(LOG_WARNING, $msg);
241     }
242
243 }