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