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_post(&$a) {
101 $value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
102 set_pconfig(local_user(),'facebook','post_by_default', $value);
107 function facebook_content(&$a) {
110 notice( t('Permission denied.') . EOL);
114 if($a->argc > 1 && $a->argv[1] === 'remove') {
115 del_pconfig(local_user(),'facebook','post');
116 notice( t('Facebook disabled') . EOL);
119 $fb_installed = get_pconfig(local_user(),'facebook','post');
121 $appid = get_config('facebook','appid');
124 notice( t('Facebook API key is missing.') . EOL);
128 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'
129 . $a->get_baseurl() . '/addon/facebook/facebook.css' . '" media="all" />' . "\r\n";
131 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
133 if(! $fb_installed) {
134 $o .= '<div id="facebook-enable-wrapper">';
136 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
137 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook post connector') . '</a>';
142 $o .= '<div id="facebook-disable-wrapper">';
144 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a></div>';
146 $o .= '<div id="facebook-post-default-form">';
147 $o .= '<form action="facebook" method="post" >';
148 $post_by_default = get_pconfig(local_user(),'facebook','post_by_default');
149 $checked = (($post_by_default) ? ' checked="checked" ' : '');
150 $o .= '<input type="checkbox" name="post_by_default" value="1"' . $checked . '/>' . ' ' . t('Post to Facebook by default') . '<br />';
151 $o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
157 function facebook_install() {
158 register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
159 register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
160 register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
164 function facebook_uninstall() {
165 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
166 unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
167 unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
171 function facebook_plugin_settings(&$a,&$b) {
173 $b .= '<div class="settings-block">';
174 $b .= '<h3>' . t('Facebook') . '</h3>';
175 $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
180 function facebook_jot_nets(&$a,&$b) {
184 $fb_post = get_pconfig(local_user(),'facebook','post');
185 if(intval($fb_post) == 1) {
186 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
187 $selected = ((intval($fb_defpost) == 1) ? ' checked="checked" ' : '');
188 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> '
189 . t('Post to Facebook') . '</div>';
194 function facebook_post_hook(&$a,&$b) {
197 * Post to Facebook stream
200 logger('Facebook post');
202 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
205 $appid = get_config('facebook', 'appid' );
206 $secret = get_config('facebook', 'appsecret' );
208 if($appid && $secret) {
210 logger('facebook: have appid+secret');
212 $fb_post = intval(get_pconfig(local_user(),'facebook','post'));
213 $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
214 $fb_token = get_pconfig(local_user(),'facebook','access_token');
216 logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG);
217 if($fb_post && $fb_token && $fb_enable) {
218 logger('facebook: able to post');
219 require_once('library/facebook.php');
220 require_once('include/bbcode.php');
224 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
226 // make links readable before we strip the code
228 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 $1',$msg);
230 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
232 $msg = trim(strip_tags(bbcode($msg)));
233 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
235 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
237 require_once('library/slinky.php');
239 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
240 $slinky = new Slinky( $posturl );
241 // setup a cascade of shortening services
242 // try to get a short link from these services
243 // in the order ur1.ca, trim, id.gd, tinyurl
244 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
245 $shortlink = $slinky->short();
246 // the new message will be shortened such that "... $shortlink"
247 // will fit into the character limit
248 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
249 $msg .= '... ' . $shortlink;
254 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
256 $postvars = array('access_token' => $fb_token, 'message' => $msg);
258 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
260 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);