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