]> git.mxchange.org Git - friendica.git/blob - addon/facebook/facebook.php
use prepare_text instead of prepare_body
[friendica.git] / addon / facebook / facebook.php
1 <?php
2
3 /**
4  * This module still needs a lot of work, but is functional today.
5  * Please review this section if you upgrade because things will change.
6  * If you have issues upgrading, remove facebook from the addon list, 
7  * view a page on your site, then add it back to the list. This will reset
8  * all of the plugin 'hooks'. 
9  *
10  * 1. register an API key for your site from developer.facebook.com
11  *   a. We'd be very happy if you include "Friendika" in the application name
12  *      to increase name recognition. The Friendika icons are also present
13  *      in the images directory and may be uploaded as a Facebook app icon.
14  *      Use images/ff-16.jpg for the Icon and images/ff-128.jpg for the Logo.
15  *   b. The url should be your site URL with a trailing slash.
16  *      You may use http://portal.friendika.com/privacy as the privacy policy
17  *      URL unless your site has different requirements, and 
18  *      http://portal.friendika.com as the Terms of Service URL unless
19  *      you have different requirements. (Friendika is a software application
20  *      and does not require Terms of Service, though your installation of it might).
21  *   c. Set the following values in your .htconfig.php file
22  *         $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
23  *         $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
24  *      Replace with the settings Facebook gives you.
25  * 2. Enable the facebook plugin by including it in .htconfig.php - e.g. 
26  *     $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
27  * 3. Visit your site url + '/facebook' (e.g. http://example.com/facebook)
28  *    and click 'Install Facebook posting'.
29  * 4. This will ask you to login to Facebook and grant permission to the 
30  *    plugin to do its stuff. Allow it to do so. 
31  * 5. You're done. To turn it off visit your site's /facebook page again and
32  *    'Remove Facebook posting'.
33  *
34  * Turn logging on (see the github Friendika wiki page 'Settings') and 
35  * repeat these steps if you have trouble.
36  * Vidoes and embeds will not be posted if there is no other content. Links 
37  * and images will be converted to text and long posts truncated - with a link
38  * to view the full post. Posts with permission settings and comments will
39  * not be posted to Facebook. 
40  *
41  */
42
43 define('FACEBOOK_MAXPOSTLEN', 420);
44
45 /* declare the facebook_module function so that /facebook url requests will land here */
46
47 function facebook_module() {}
48
49
50
51 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
52
53 function facebook_init(&$a) {
54
55         if($a->argc != 2)
56                 return;
57         $nick = $a->argv[1];
58         if(strlen($nick))
59                 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
60                                 dbesc($nick)
61                 );
62         if(! count($r))
63                 return;
64
65         $uid           = $r[0]['uid'];
66         $auth_code     = (($_GET['code']) ? $_GET['code'] : '');
67         $error         = (($_GET['error_description']) ? $_GET['error_description'] : '');
68
69
70         if($error)
71                 logger('facebook_init: Error: ' . $error);
72
73         if($auth_code && $uid) {
74
75                 $appid = get_config('facebook','appid');
76                 $appsecret = get_config('facebook', 'appsecret');
77
78                 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
79                         . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
80                         . urlencode($a->get_baseurl() . '/facebook/' . $nick) 
81                         . '&code=' . $auth_code);
82
83                 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
84
85                 if(strpos($x,'access_token=') !== false) {
86                         $token = str_replace('access_token=', '', $x);
87                         if(strpos($token,'&') !== false)
88                                 $token = substr($token,0,strpos($token,'&'));
89                         set_pconfig($uid,'facebook','access_token',$token);
90                         set_pconfig($uid,'facebook','post','1');
91                 }
92
93                 // todo: is this a browser session or a server session? where do we go? 
94         }
95
96 }
97
98 function facebook_content(&$a) {
99
100         if(! local_user()) {
101                 notice( t('Permission denied.') . EOL);
102                 return '';
103         }
104
105         if($a->argc > 1 && $a->argv[1] === 'remove') {
106                 del_pconfig(local_user(),'facebook','post');
107                 notice( t('Facebook disabled') . EOL);
108         }
109
110         $appid = get_config('facebook','appid');
111
112         if(! $appid) {
113                 notice( t('Facebook API key is missing.') . EOL);
114                 return '';
115         }
116
117         $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' 
118                 . $a->get_baseurl() . '/addon/facebook/facebook.css' . '" media="all" />' . "\r\n";
119
120         $o .= '<h3>' . t('Facebook Connect') . '</h3>';
121
122         $o .= '<div id="facebook-enable-wrapper">';
123
124         $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
125                 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook post connector') . '</a>';
126         $o .= '</div><div id="facebook-disable-wrapper">';
127
128         $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a></div>';
129
130
131         return $o;
132 }
133
134 function facebook_install() {
135         register_hook('post_local_end',  'addon/facebook/facebook.php', 'facebook_post_hook');
136         register_hook('jot_networks',    'addon/facebook/facebook.php', 'facebook_jot_nets');
137         register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
138 }
139
140
141 function facebook_uninstall() {
142         unregister_hook('post_local_end',  'addon/facebook/facebook.php', 'facebook_post_hook');
143         unregister_hook('jot_networks',    'addon/facebook/facebook.php', 'facebook_jot_nets');
144         unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
145 }
146
147
148 function facebook_plugin_settings(&$a,&$b) {
149
150         $b .= '<div class="settings-block">';
151         $b .= '<h3>' . t('Facebook') . '</h3>';
152         $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
153         $b .= '</div>';
154
155 }
156
157 function facebook_jot_nets(&$a,&$b) {
158         if(! local_user())
159                 return;
160
161         $fb_post = get_pconfig(local_user(),'facebook','post');
162         if(intval($fb_post) == 1) {
163                 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
164                 $selected = ((intval($fb_defpost == 1)) ? ' selected="selected" ' : '');
165                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> ' 
166                         . t('Post to Facebook') . '</div>';     
167         }
168 }
169
170
171 function facebook_post_hook(&$a,&$b) {
172
173         /**
174          * Post to Facebook stream
175          */
176
177         logger('Facebook post');
178
179         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
180
181
182                 $appid  = get_config('facebook', 'appid'  );
183                 $secret = get_config('facebook', 'appsecret' );
184
185                 if($appid && $secret) {
186
187                         logger('facebook: have appid+secret');
188
189                         $fb_post   = intval(get_pconfig(local_user(),'facebook','post'));
190                         $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
191                         $fb_token  = get_pconfig(local_user(),'facebook','access_token');
192
193                         logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG); 
194                         if($fb_post && $fb_token && $fb_enable) {
195                                 logger('facebook: able to post');
196                                 require_once('library/facebook.php');
197                                 require_once('include/bbcode.php');     
198
199                                 $msg = $b['body'];
200
201                                 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
202
203                                 // make links readable before we strip the code
204
205                                 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
206
207                                 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
208
209                                 $msg = trim(strip_tags(bbcode($msg)));
210                                 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
211
212                                 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
213                                         $shortlink = "";
214                                         require_once('library/slinky.php');
215
216                                         $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
217                                         $slinky = new Slinky( $posturl );
218                                         // setup a cascade of shortening services
219                                         // try to get a short link from these services
220                                         // in the order ur1.ca, trim, id.gd, tinyurl
221                                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
222                                         $shortlink = $slinky->short();
223                                         // the new message will be shortened such that "... $shortlink"
224                                         // will fit into the character limit
225                                         $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
226                                         $msg .= '... ' . $shortlink;
227                                 }
228                                 if(! strlen($msg))
229                                         return;
230
231                                 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
232
233                                 $postvars = array('access_token' => $fb_token, 'message' => $msg);
234
235                                 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
236                                 
237                                 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
238
239                         }
240                 }
241         }
242 }
243