friendica_2021-01/addon/facebook/facebook.php

177 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2010-12-28 08:36:19 +01:00
/**
* This module needs a lot of work.
*
* - setting/storing preferences
* - documentation on how to obtain FB API keys for your site
* - ensuring a valid FB login session
* - requesting permissions within the FB login session to post on your behalf until permission revoked.
*
*/
2011-02-11 03:01:38 +01:00
define('FACEBOOK_MAXPOSTLEN', 420);
/* declare the facebook_module function so that /facebook url requests will land here */
function facebook_module() {}
2011-02-11 06:25:24 +01:00
/* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
2011-02-11 03:01:38 +01: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 06:25:24 +01:00
if($error)
logger('facebook_init: Error: ' . $error);
2011-02-11 03:01:38 +01: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 06:25:24 +01:00
logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
2011-02-11 03:01:38 +01:00
if(strpos($x,'access_token=') !== false) {
$token = str_replace('access_token=', '', $x);
if(strpos($token,'&') !== false)
2011-02-11 06:25:24 +01:00
$token = substr($token,0,strpos($token,'&'));
2011-02-11 03:01:38 +01:00
set_pconfig($uid,'facebook','access_token',$token);
2011-02-11 06:25:24 +01:00
set_pconfig($uid,'facebook','post','true');
2011-02-11 03:01:38 +01:00
}
// todo: is this a browser session or a server session? where do we go?
}
}
function facebook_content(&$a) {
2011-02-11 06:25:24 +01: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);
}
$appid = get_config('facebook','appid');
if(! $appid) {
notify( t('Facebook API key is missing.') . EOL);
return '';
}
$o .= '<h3>' . t('Facebook Connect') . '</h3>';
$o .= '<br />';
$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 posting') . '</a><br />';
$o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook posting') . '</a><br />';
2011-02-11 03:01:38 +01:00
return $o;
}
function facebook_install() {
register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
}
function facebook_uninstall() {
unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
}
function facebook_post_hook(&$a,&$b) {
/**
* Post to Facebook stream
*/
2011-02-11 06:25:24 +01:00
logger('Facebook post');
2011-02-11 03:01:38 +01:00
if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
2011-02-11 06:25:24 +01:00
2011-02-11 03:01:38 +01:00
$appid = get_config('facebook', 'appid' );
$secret = get_config('facebook', 'appsecret' );
if($appid && $secret) {
2011-02-11 03:01:38 +01:00
$fb_post = get_pconfig(local_user(),'facebook','post');
$fb_token = get_pconfig(local_user(),'facebook','access_token');
2011-02-11 03:01:38 +01:00
if($fb_post && $fb_token) {
require_once('library/facebook.php');
require_once('include/bbcode.php');
2011-02-11 06:25:24 +01:00
$msg = $b['body'];
logger('Facebook post2: msg=' . $msg, LOGGER_DATA);
2011-02-11 03:01:38 +01:00
// make links readable before we strip the code
2011-02-11 06:25:24 +01:00
$msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
2011-02-11 03:01:38 +01:00
2011-02-11 06:25:24 +01:00
$msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
$msg = trim(strip_tags(bbcode($msg)));
2011-02-11 03:01:38 +01:00
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;
2011-02-11 06:25:24 +01:00
logger('Facebook post: msg=' . $msg, LOGGER_DATA);
2011-02-11 03:01:38 +01:00
2011-02-11 06:25:24 +01:00
$postvars = array('access_token' => $fb_token, 'message' => $msg);
2011-02-11 03:01:38 +01:00
2011-02-11 06:25:24 +01:00
$x = post_url('https://graph.facebook.com/me/feed', $postvars);
logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
2011-02-11 03:01:38 +01:00
}
}
}
2010-12-31 08:28:33 +01:00
}