From bd4e7249a347b012c34d23a739b56cb6aea63240 Mon Sep 17 00:00:00 2001 From: Friendika Date: Thu, 10 Feb 2011 18:01:38 -0800 Subject: more work on facebook addon --- addon/facebook/facebook.php | 95 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 7 deletions(-) (limited to 'addon/facebook/facebook.php') diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index acf37fcdb..7a7c3428a 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -10,6 +10,58 @@ * */ +define('FACEBOOK_MAXPOSTLEN', 420); + +/* declare the facebook_module function so that /facebook url requests will land here */ + +function facebook_module() {} + + + +/* Callback from Facebook oauth requests. */ + +function facebook_init(&$a) { + + if($a->argc != 2) + return; + $nick = $a->argv[1]; + if(strlen($nick)) + $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", + dbesc($nick) + ); + if(! count($r)) + return; + + $uid = $r[0]['uid']; + $auth_code = (($_GET['code']) ? $_GET['code'] : ''); + $error = (($_GET['error_description']) ? $_GET['error_description'] : ''); + + + if($auth_code && $uid) { + + $appid = get_config('facebook','appid'); + $appsecret = get_config('facebook', 'appsecret'); + + $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id=' + . $appid . '&client_secret=' . $appsecret . '&redirect_uri=' + . urlencode($a->get_baseurl() . '/facebook/' . $nick) + . '&code=' . $auth_code); + if(strpos($x,'access_token=') !== false) { + $token = str_replace('access_token=', '', $x); + if(strpos($token,'&') !== false) + $token = substr('$token,0,strpos($token,'&')); + set_pconfig($uid,'facebook','access_token',$token); + } + + // todo: is this a browser session or a server session? where do we go? + } + +} + +function facebook_content(&$a) { + $o = "facebook module loaded"; + return $o; +} function facebook_install() { register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); @@ -21,27 +73,56 @@ function facebook_uninstall() { } - - function facebook_post_hook(&$a,&$b) { /** * Post to Facebook stream */ - if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) { + if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) { - $appid = get_config('system', 'facebook_appid' ); - $secret = get_config('system', 'facebook_secret' ); + $appid = get_config('facebook', 'appid' ); + $secret = get_config('facebook', 'appsecret' ); if($appid && $secret) { - $fb_post = get_pconfig(local_user(),'facebook','post'); + $fb_post = get_pconfig(local_user(),'facebook','post'); + $fb_token = get_pconfig(local_user(),'facebook','access_token'); - if($fb_post) { + if($fb_post && $fb_token) { require_once('library/facebook.php'); require_once('include/bbcode.php'); + + // make links readable before we strip the code + + $msg = preg_replace('\[url\=(.?*)\](.?*)\[\/url\]/is','$2 ($1)',$msg); + + $msg = preg_replace('\[img\](.?*)\[\/img\]/is', t('Image: ') . '$1',$msg); + + $msg = trim(strip_tags(bbcode($b['body']))); + if (strlen($msg) > FACEBOOK_MAXPOSTLEN) { + $shortlink = ""; + require_once('addon/twitter/slinky.php'); + + $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id']; + $slinky = new Slinky( $posturl ); + // setup a cascade of shortening services + // try to get a short link from these services + // in the order ur1.ca, trim, id.gd, tinyurl + $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); + $shortlink = $slinky->short(); + // the new message will be shortened such that "... $shortlink" + // will fit into the character limit + $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4); + $msg .= '... ' . $shortlink; + } + if(! strlen($msg)) + return; + + + + $facebook = new Facebook(array( 'appId' => $appid, 'secret' => $secret, -- cgit v1.2.3 From fc5a3cac14d40286f0daf909b0243725a8ef76e3 Mon Sep 17 00:00:00 2001 From: Friendika Date: Thu, 10 Feb 2011 21:25:24 -0800 Subject: post to FB is working --- addon/facebook/facebook.php | 69 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 17 deletions(-) (limited to 'addon/facebook/facebook.php') diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index 7a7c3428a..c1f3c5a38 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -18,7 +18,7 @@ function facebook_module() {} -/* Callback from Facebook oauth requests. */ +/* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */ function facebook_init(&$a) { @@ -37,6 +37,9 @@ function facebook_init(&$a) { $error = (($_GET['error_description']) ? $_GET['error_description'] : ''); + if($error) + logger('facebook_init: Error: ' . $error); + if($auth_code && $uid) { $appid = get_config('facebook','appid'); @@ -46,11 +49,15 @@ function facebook_init(&$a) { . $appid . '&client_secret=' . $appsecret . '&redirect_uri=' . urlencode($a->get_baseurl() . '/facebook/' . $nick) . '&code=' . $auth_code); + + logger('facebook_init: returned access token: ' . $x, LOGGER_DATA); + if(strpos($x,'access_token=') !== false) { $token = str_replace('access_token=', '', $x); if(strpos($token,'&') !== false) - $token = substr('$token,0,strpos($token,'&')); + $token = substr($token,0,strpos($token,'&')); set_pconfig($uid,'facebook','access_token',$token); + set_pconfig($uid,'facebook','post','true'); } // todo: is this a browser session or a server session? where do we go? @@ -59,7 +66,34 @@ function facebook_init(&$a) { } function facebook_content(&$a) { - $o = "facebook module loaded"; + + if(! local_user()) { + notice( t('Permission denied.') . EOL); + return ''; + } + + if($a->argc > 1 && $a->argv[1] === 'remove') { + del_pconfig(local_user(),'facebook','post'); + notice( t('Facebook disabled') . EOL); + } + + $appid = get_config('facebook','appid'); + + if(! $appid) { + notify( t('Facebook API key is missing.') . EOL); + return ''; + } + + $o .= '

' . t('Facebook Connect') . '

'; + + $o .= '
'; + + $o .= '' . t('Install Facebook posting') . '
'; + + $o .= '' . t('Remove Facebook posting') . '
'; + + return $o; } @@ -79,8 +113,11 @@ function facebook_post_hook(&$a,&$b) { * Post to Facebook stream */ + logger('Facebook post'); + if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) { + $appid = get_config('facebook', 'appid' ); $secret = get_config('facebook', 'appsecret' ); @@ -93,14 +130,18 @@ function facebook_post_hook(&$a,&$b) { require_once('library/facebook.php'); require_once('include/bbcode.php'); + $msg = $b['body']; + + logger('Facebook post2: msg=' . $msg, LOGGER_DATA); // make links readable before we strip the code - $msg = preg_replace('\[url\=(.?*)\](.?*)\[\/url\]/is','$2 ($1)',$msg); + $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg); - $msg = preg_replace('\[img\](.?*)\[\/img\]/is', t('Image: ') . '$1',$msg); + $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg); + + $msg = trim(strip_tags(bbcode($msg))); - $msg = trim(strip_tags(bbcode($b['body']))); if (strlen($msg) > FACEBOOK_MAXPOSTLEN) { $shortlink = ""; require_once('addon/twitter/slinky.php'); @@ -120,20 +161,14 @@ function facebook_post_hook(&$a,&$b) { if(! strlen($msg)) return; + logger('Facebook post: msg=' . $msg, LOGGER_DATA); + $postvars = array('access_token' => $fb_token, 'message' => $msg); + $x = post_url('https://graph.facebook.com/me/feed', $postvars); + + logger('Facebook post returns: ' . $x, LOGGER_DEBUG); - $facebook = new Facebook(array( - 'appId' => $appid, - 'secret' => $secret, - 'cookie' => true - )); - try { - $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> bbcode($b['body']), 'cb' => '')); - } - catch (FacebookApiException $e) { - notice( t('Facebook status update failed.') . EOL); - } } } } -- cgit v1.2.3 From 40574589e27d9135a434dcc4f9dfa899fe5f8ec6 Mon Sep 17 00:00:00 2001 From: Friendika Date: Thu, 10 Feb 2011 23:10:50 -0800 Subject: fb connecter - document installation --- addon/facebook/facebook.php | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'addon/facebook/facebook.php') diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index c1f3c5a38..a41344a3d 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -1,12 +1,32 @@ config['facebook']['appid'] = 'xxxxxxxxxxx'; + * $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx'; + * Replace with the settings Facebook gives you. + * 2. Enable the facebook plugin by including it in .htconfig.php - e.g. + * $a->config['system']['addon'] = 'plugin1,plugin2,facebook'; + * 3. Visit your site url + '/facebook' (e.g. http://example.com/facebook) + * and click 'Install Facebook posting'. + * 4. This will ask you to login to Facebook and grant permission to the + * plugin to do its stuff. Allow it to do so. + * 5. You're done. To turn it off visit your site's /facebook page again and + * 'Remove Facebook posting'. + * + * Turn logging on (see the github Friendika wiki page 'Settings') and + * repeat these steps if you have trouble. + * Vidoes and embeds will not be posted if there is no other content. Links + * and images will be converted to text and long posts truncated - with a link + * to view the full post. Posts with permission settings and comments will + * not be posted to Facebook. * */ -- cgit v1.2.3 From c16e86add81d9f2867a0da024954b5cbc14a9f4b Mon Sep 17 00:00:00 2001 From: Friendika Date: Fri, 11 Feb 2011 02:35:19 -0800 Subject: toggles "post to facebook, post to twitter", etc. --- addon/facebook/facebook.php | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'addon/facebook/facebook.php') diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index a41344a3d..2a5715b2e 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -3,6 +3,9 @@ /** * This module still needs a lot of work, but is functional today. * Please review this section if you upgrade because things will change. + * If you have issues upgrading, remove facebook from the addon list, + * view a page on your site, then add it back to the list. This will reset + * all of the plugin 'hooks'. * * 1. register an API key from developer.facebook.com * a. We'd be very happy if you include "Friendika" in the application name @@ -77,7 +80,7 @@ function facebook_init(&$a) { if(strpos($token,'&') !== false) $token = substr($token,0,strpos($token,'&')); set_pconfig($uid,'facebook','access_token',$token); - set_pconfig($uid,'facebook','post','true'); + set_pconfig($uid,'facebook','post','1'); } // todo: is this a browser session or a server session? where do we go? @@ -118,12 +121,41 @@ function facebook_content(&$a) { } function facebook_install() { - register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); + register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); + register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets'); + register_hook('post_local_start','addon/facebook/facebook.php', 'facebook_post_local'); + } function facebook_uninstall() { - unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); + unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); + unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets'); + unregister_hook('post_local_start','addon/facebook/facebook.php', 'facebook_post_local'); +} + + +function facebook_jot_nets(&$a,&$b) { + if(! local_user()) + return; + + $fb_post = get_pconfig(local_user(),'facebook','post'); + if(intval($fb_post) == 1) { + $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default'); + $selected = ((intval($fb_defpost == 1)) ? ' selected="selected" ' : ''); + $b .= '
' + . t('Post to Facebook') . '
'; + } +} + +function facebook_post_local(&$a,&$b) { + if(! local_user()) + return; + + if((x($b,'facebook_enable')) && (intval($b['facebook_enable']))) + set_pconfig(local_user(),'facebook','enable','1'); + else + del_pconfig(local_user(),'facebook','enable'); } @@ -143,10 +175,11 @@ function facebook_post_hook(&$a,&$b) { if($appid && $secret) { - $fb_post = get_pconfig(local_user(),'facebook','post'); - $fb_token = get_pconfig(local_user(),'facebook','access_token'); + $fb_post = intval(get_pconfig(local_user(),'facebook','post')); + $fb_enable = intval(get_pconfig(local_user(),'facebook','enable')); + $fb_token = get_pconfig(local_user(),'facebook','access_token'); - if($fb_post && $fb_token) { + if($fb_post && $fb_token && $fb_enable) { require_once('library/facebook.php'); require_once('include/bbcode.php'); -- cgit v1.2.3 From 5e02519a1311cb62d7aaa2f8dc947fd0affd454c Mon Sep 17 00:00:00 2001 From: Friendika Date: Fri, 11 Feb 2011 04:32:38 -0800 Subject: disable network toggles when ACL's are entered --- addon/facebook/facebook.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addon/facebook/facebook.php') diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index 2a5715b2e..93e7962c3 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -143,7 +143,7 @@ function facebook_jot_nets(&$a,&$b) { if(intval($fb_post) == 1) { $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default'); $selected = ((intval($fb_defpost == 1)) ? ' selected="selected" ' : ''); - $b .= '
' + $b .= '
' . t('Post to Facebook') . '
'; } } -- cgit v1.2.3 From 89cbd17c721dae57b18686fd7f3e5f8c74a279d9 Mon Sep 17 00:00:00 2001 From: Friendika Date: Sat, 12 Feb 2011 05:07:24 -0800 Subject: more fb debug msgs --- addon/facebook/facebook.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'addon/facebook/facebook.php') diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index 93e7962c3..7bebb3b4f 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -149,6 +149,7 @@ function facebook_jot_nets(&$a,&$b) { } function facebook_post_local(&$a,&$b) { + if(! local_user()) return; @@ -175,17 +176,20 @@ function facebook_post_hook(&$a,&$b) { if($appid && $secret) { + logger('facebook: have appid+secret'); + $fb_post = intval(get_pconfig(local_user(),'facebook','post')); $fb_enable = intval(get_pconfig(local_user(),'facebook','enable')); $fb_token = get_pconfig(local_user(),'facebook','access_token'); if($fb_post && $fb_token && $fb_enable) { + logger('facebook: able to post'); require_once('library/facebook.php'); require_once('include/bbcode.php'); $msg = $b['body']; - logger('Facebook post2: msg=' . $msg, LOGGER_DATA); + logger('Facebook post: original msg=' . $msg, LOGGER_DATA); // make links readable before we strip the code -- cgit v1.2.3