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'.
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'.
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.
43 define('FACEBOOK_MAXPOSTLEN', 420);
45 /* declare the facebook_module function so that /facebook url requests will land here */
47 function facebook_module() {}
51 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
53 function facebook_init(&$a) {
59 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
66 $auth_code = (($_GET['code']) ? $_GET['code'] : '');
67 $error = (($_GET['error_description']) ? $_GET['error_description'] : '');
71 logger('facebook_init: Error: ' . $error);
73 if($auth_code && $uid) {
75 $appid = get_config('facebook','appid');
76 $appsecret = get_config('facebook', 'appsecret');
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);
83 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
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');
93 // todo: is this a browser session or a server session? where do we go?
98 function facebook_content(&$a) {
101 notice( t('Permission denied.') . EOL);
105 if($a->argc > 1 && $a->argv[1] === 'remove') {
106 del_pconfig(local_user(),'facebook','post');
107 notice( t('Facebook disabled') . EOL);
110 $appid = get_config('facebook','appid');
113 notify( t('Facebook API key is missing.') . EOL);
117 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
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 />';
124 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a><br />';
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');
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');
144 function facebook_plugin_settings(&$a,&$b) {
146 $b .= '<h3>' . t('Facebook') . '</h3>';
147 $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
151 function facebook_jot_nets(&$a,&$b) {
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>';
165 function facebook_post_hook(&$a,&$b) {
168 * Post to Facebook stream
171 logger('Facebook post');
173 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
176 $appid = get_config('facebook', 'appid' );
177 $secret = get_config('facebook', 'appsecret' );
179 if($appid && $secret) {
181 logger('facebook: have appid+secret');
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');
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');
195 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
197 // make links readable before we strip the code
199 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
201 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
203 $msg = trim(strip_tags(bbcode($msg)));
204 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
206 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
208 require_once('library/slinky.php');
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;
225 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
227 $postvars = array('access_token' => $fb_token, 'message' => $msg);
229 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
231 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);