]> git.mxchange.org Git - friendica.git/blob - addon/facebook/facebook.php
provide privacy policy url as mandated for Facebook apps (how bloody ironic)
[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                 notify( t('Facebook API key is missing.') . EOL);
114                 return '';
115         }
116
117         $o .= '<h3>' . t('Facebook Connect') . '</h3>';
118
119         $o .= '<br />';
120
121         $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
122                 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook post connector') . '</a><br /><br />';
123
124         $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a><br />';
125
126
127         return $o;
128 }
129
130 function facebook_install() {
131         register_hook('post_local_end',  'addon/facebook/facebook.php', 'facebook_post_hook');
132         register_hook('jot_networks',    'addon/facebook/facebook.php', 'facebook_jot_nets');
133         register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
134 }
135
136
137 function facebook_uninstall() {
138         unregister_hook('post_local_end',  'addon/facebook/facebook.php', 'facebook_post_hook');
139         unregister_hook('jot_networks',    'addon/facebook/facebook.php', 'facebook_jot_nets');
140         unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
141 }
142
143
144 function facebook_plugin_settings(&$a,&$b) {
145
146         $b .= '<h3>' . t('Facebook') . '</h3>';
147         $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
148
149 }
150
151 function facebook_jot_nets(&$a,&$b) {
152         if(! local_user())
153                 return;
154
155         $fb_post = get_pconfig(local_user(),'facebook','post');
156         if(intval($fb_post) == 1) {
157                 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
158                 $selected = ((intval($fb_defpost == 1)) ? ' selected="selected" ' : '');
159                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> ' 
160                         . t('Post to Facebook') . '</div>';     
161         }
162 }
163
164
165 function facebook_post_hook(&$a,&$b) {
166
167         /**
168          * Post to Facebook stream
169          */
170
171         logger('Facebook post');
172
173         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
174
175
176                 $appid  = get_config('facebook', 'appid'  );
177                 $secret = get_config('facebook', 'appsecret' );
178
179                 if($appid && $secret) {
180
181                         logger('facebook: have appid+secret');
182
183                         $fb_post   = intval(get_pconfig(local_user(),'facebook','post'));
184                         $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
185                         $fb_token  = get_pconfig(local_user(),'facebook','access_token');
186
187                         logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG); 
188                         if($fb_post && $fb_token && $fb_enable) {
189                                 logger('facebook: able to post');
190                                 require_once('library/facebook.php');
191                                 require_once('include/bbcode.php');     
192
193                                 $msg = $b['body'];
194
195                                 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
196
197                                 // make links readable before we strip the code
198
199                                 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
200
201                                 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
202
203                                 $msg = trim(strip_tags(bbcode($msg)));
204                                 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
205
206                                 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
207                                         $shortlink = "";
208                                         require_once('library/slinky.php');
209
210                                         $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
211                                         $slinky = new Slinky( $posturl );
212                                         // setup a cascade of shortening services
213                                         // try to get a short link from these services
214                                         // in the order ur1.ca, trim, id.gd, tinyurl
215                                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
216                                         $shortlink = $slinky->short();
217                                         // the new message will be shortened such that "... $shortlink"
218                                         // will fit into the character limit
219                                         $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
220                                         $msg .= '... ' . $shortlink;
221                                 }
222                                 if(! strlen($msg))
223                                         return;
224
225                                 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
226
227                                 $postvars = array('access_token' => $fb_token, 'message' => $msg);
228
229                                 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
230                                 
231                                 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
232
233                         }
234                 }
235         }
236 }
237