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