2010-12-28 02:28:34 -05:00
|
|
|
<?php
|
|
|
|
|
2010-12-28 02:36:19 -05:00
|
|
|
/**
|
2011-02-11 02:10:50 -05:00
|
|
|
* This module still needs a lot of work, but is functional today.
|
|
|
|
* Please review this section if you upgrade because things will change.
|
2011-02-11 05:35:19 -05:00
|
|
|
* 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'.
|
2010-12-28 02:36:19 -05:00
|
|
|
*
|
2011-02-23 19:39:21 -05:00
|
|
|
* 1. register an API key for your site from developer.facebook.com
|
2011-02-11 02:10:50 -05:00
|
|
|
* a. We'd be very happy if you include "Friendika" in the application name
|
2011-02-23 19:39:21 -05:00
|
|
|
* to increase name recognition. The Friendika icons are also present
|
|
|
|
* in the images directory and may be uploaded as a Facebook app icon.
|
|
|
|
* Use images/ff-16.jpg for the Icon and images/ff-128.jpg for the Logo.
|
|
|
|
* b. The url should be your site URL with a trailing slash.
|
|
|
|
* You may use http://portal.friendika.com/privacy as the privacy policy
|
|
|
|
* URL unless your site has different requirements, and
|
|
|
|
* http://portal.friendika.com as the Terms of Service URL unless
|
|
|
|
* you have different requirements. (Friendika is a software application
|
|
|
|
* and does not require Terms of Service, though your installation of it might).
|
2011-02-11 02:10:50 -05:00
|
|
|
* c. Set the following values in your .htconfig.php file
|
|
|
|
* $a->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.
|
2010-12-28 02:36:19 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
define('FACEBOOK_MAXPOSTLEN', 420);
|
|
|
|
|
|
|
|
/* declare the facebook_module function so that /facebook url requests will land here */
|
|
|
|
|
|
|
|
function facebook_module() {}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
/* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
|
2011-02-10 21:01:38 -05:00
|
|
|
|
|
|
|
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'] : '');
|
|
|
|
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
if($error)
|
|
|
|
logger('facebook_init: Error: ' . $error);
|
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
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);
|
2011-02-11 00:25:24 -05:00
|
|
|
|
|
|
|
logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
|
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
if(strpos($x,'access_token=') !== false) {
|
|
|
|
$token = str_replace('access_token=', '', $x);
|
|
|
|
if(strpos($token,'&') !== false)
|
2011-02-11 00:25:24 -05:00
|
|
|
$token = substr($token,0,strpos($token,'&'));
|
2011-02-10 21:01:38 -05:00
|
|
|
set_pconfig($uid,'facebook','access_token',$token);
|
2011-02-11 05:35:19 -05:00
|
|
|
set_pconfig($uid,'facebook','post','1');
|
2011-02-10 21:01:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: is this a browser session or a server session? where do we go?
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-09 17:39:18 -05:00
|
|
|
function facebook_post(&$a) {
|
|
|
|
|
|
|
|
if(local_user()){
|
|
|
|
$value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
|
|
|
|
set_pconfig(local_user(),'facebook','post_by_default', $value);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
function facebook_content(&$a) {
|
2011-02-11 00:25:24 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-03-09 17:39:18 -05:00
|
|
|
$fb_installed = get_pconfig(local_user(),'facebook','post');
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
$appid = get_config('facebook','appid');
|
|
|
|
|
|
|
|
if(! $appid) {
|
2011-02-27 04:36:52 -05:00
|
|
|
notice( t('Facebook API key is missing.') . EOL);
|
2011-02-11 00:25:24 -05:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2011-02-23 21:24:29 -05:00
|
|
|
$a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'
|
|
|
|
. $a->get_baseurl() . '/addon/facebook/facebook.css' . '" media="all" />' . "\r\n";
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
$o .= '<h3>' . t('Facebook Connect') . '</h3>';
|
|
|
|
|
2011-03-09 17:39:18 -05:00
|
|
|
if(! $fb_installed) {
|
|
|
|
$o .= '<div id="facebook-enable-wrapper">';
|
2011-02-11 00:25:24 -05:00
|
|
|
|
2011-03-09 17:39:18 -05:00
|
|
|
$o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
|
|
|
|
. $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook post connector') . '</a>';
|
|
|
|
$o .= '</div>';
|
|
|
|
}
|
2011-02-11 00:25:24 -05:00
|
|
|
|
2011-03-09 17:39:18 -05:00
|
|
|
if($fb_installed) {
|
|
|
|
$o .= '<div id="facebook-disable-wrapper">';
|
|
|
|
|
|
|
|
$o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a></div>';
|
|
|
|
|
|
|
|
$o .= '<div id="facebook-post-default-form">';
|
|
|
|
$o .= '<form action="facebook" method="post" >';
|
|
|
|
$post_by_default = get_pconfig(local_user(),'facebook','post_by_default');
|
|
|
|
$checked = (($post_by_default) ? ' checked="checked" ' : '');
|
|
|
|
$o .= '<input type="checkbox" name="post_by_default" value="1"' . $checked . '/>' . ' ' . t('Post to Facebook by default') . '<br />';
|
|
|
|
$o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
|
|
|
|
}
|
2011-02-11 00:25:24 -05:00
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
return $o;
|
|
|
|
}
|
2010-12-28 02:28:34 -05:00
|
|
|
|
|
|
|
function facebook_install() {
|
2011-02-11 05:35:19 -05:00
|
|
|
register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
|
|
|
|
register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
|
2011-02-20 06:32:57 -05:00
|
|
|
register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
|
2010-12-28 02:28:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function facebook_uninstall() {
|
2011-02-11 05:35:19 -05:00
|
|
|
unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
|
|
|
|
unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
|
2011-02-20 06:32:57 -05:00
|
|
|
unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
|
2011-02-11 05:35:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-20 06:32:57 -05:00
|
|
|
function facebook_plugin_settings(&$a,&$b) {
|
|
|
|
|
2011-02-23 21:24:29 -05:00
|
|
|
$b .= '<div class="settings-block">';
|
2011-02-20 06:32:57 -05:00
|
|
|
$b .= '<h3>' . t('Facebook') . '</h3>';
|
|
|
|
$b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
|
2011-02-23 21:24:29 -05:00
|
|
|
$b .= '</div>';
|
2011-02-20 06:32:57 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-11 05:35:19 -05:00
|
|
|
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');
|
2011-03-09 17:39:18 -05:00
|
|
|
$selected = ((intval($fb_defpost) == 1) ? ' checked="checked" ' : '');
|
2011-02-11 07:32:38 -05:00
|
|
|
$b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> '
|
2011-02-11 05:35:19 -05:00
|
|
|
. t('Post to Facebook') . '</div>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 02:28:34 -05:00
|
|
|
|
|
|
|
function facebook_post_hook(&$a,&$b) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post to Facebook stream
|
|
|
|
*/
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
logger('Facebook post');
|
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
|
2010-12-28 02:28:34 -05:00
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
|
2011-02-10 21:01:38 -05:00
|
|
|
$appid = get_config('facebook', 'appid' );
|
|
|
|
$secret = get_config('facebook', 'appsecret' );
|
2010-12-28 02:28:34 -05:00
|
|
|
|
|
|
|
if($appid && $secret) {
|
|
|
|
|
2011-02-12 08:07:24 -05:00
|
|
|
logger('facebook: have appid+secret');
|
|
|
|
|
2011-02-11 05:35:19 -05:00
|
|
|
$fb_post = intval(get_pconfig(local_user(),'facebook','post'));
|
2011-02-12 22:24:19 -05:00
|
|
|
$fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
|
2011-02-11 05:35:19 -05:00
|
|
|
$fb_token = get_pconfig(local_user(),'facebook','access_token');
|
2010-12-28 02:28:34 -05:00
|
|
|
|
2011-02-12 22:24:19 -05:00
|
|
|
logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG);
|
2011-02-11 05:35:19 -05:00
|
|
|
if($fb_post && $fb_token && $fb_enable) {
|
2011-02-12 08:07:24 -05:00
|
|
|
logger('facebook: able to post');
|
2010-12-28 02:28:34 -05:00
|
|
|
require_once('library/facebook.php');
|
|
|
|
require_once('include/bbcode.php');
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
$msg = $b['body'];
|
|
|
|
|
2011-02-12 08:07:24 -05:00
|
|
|
logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
|
2011-02-10 21:01:38 -05:00
|
|
|
|
|
|
|
// make links readable before we strip the code
|
|
|
|
|
2011-03-15 20:47:49 -04:00
|
|
|
$msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 $1',$msg);
|
2011-02-10 21:01:38 -05:00
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
$msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
|
|
|
|
|
|
|
|
$msg = trim(strip_tags(bbcode($msg)));
|
2011-02-20 22:51:00 -05:00
|
|
|
$msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
|
2011-02-10 21:01:38 -05:00
|
|
|
|
|
|
|
if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
|
|
|
|
$shortlink = "";
|
2011-02-21 23:19:33 -05:00
|
|
|
require_once('library/slinky.php');
|
2011-02-10 21:01:38 -05:00
|
|
|
|
|
|
|
$display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
|
2011-03-17 18:31:46 -04:00
|
|
|
$slinky = new Slinky( $display_url );
|
2011-02-10 21:01:38 -05:00
|
|
|
// 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;
|
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
logger('Facebook post: msg=' . $msg, LOGGER_DATA);
|
2011-02-10 21:01:38 -05:00
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
$postvars = array('access_token' => $fb_token, 'message' => $msg);
|
2011-02-10 21:01:38 -05:00
|
|
|
|
2011-02-11 00:25:24 -05:00
|
|
|
$x = post_url('https://graph.facebook.com/me/feed', $postvars);
|
|
|
|
|
|
|
|
logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
|
2011-02-10 21:01:38 -05:00
|
|
|
|
2010-12-28 02:28:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-31 02:28:33 -05:00
|
|
|
}
|
2010-12-28 02:28:34 -05:00
|
|
|
|