]> git.mxchange.org Git - friendica.git/commitdiff
post to FB is working
authorFriendika <info@friendika.com>
Fri, 11 Feb 2011 05:25:24 +0000 (21:25 -0800)
committerFriendika <info@friendika.com>
Fri, 11 Feb 2011 05:25:24 +0000 (21:25 -0800)
addon/facebook/facebook.php
index.php

index 7a7c3428a9884fd0d865598692b2848bfcfad303..c1f3c5a383db1e0fd747f92623d7f1e825cf03f5 100644 (file)
@@ -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 .= '<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 />';
+
+
        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);
-                               }
                        }
                }
        }
index d0de1b9dc69ce83da782704f4e3a34f4bb37abf6..661676bfd83d766b9ffb0ab1357cd2934392fa60 100644 (file)
--- a/index.php
+++ b/index.php
@@ -143,7 +143,7 @@ if(strlen($a->module)) {
                include("mod/{$a->module}.php");
                $a->module_loaded = true;
        }
-       else {
+       if(! $a->module_loaded) {
                if((x($_SERVER,'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && isset($dreamhost_error_hack)) {
                        logger('index.php: dreamhost_error_hack invoked. Original URI =' . $_SERVER['REQUEST_URI']);
                        goaway($a->get_baseurl() . $_SERVER['REQUEST_URI']);