示例#1
0
function gpluspost_feeditem($pid, $uid)
{
    global $a;
    require_once 'include/api.php';
    require_once 'include/bbcode.php';
    require_once "include/html2plain.php";
    require_once "include/network.php";
    $skipwithoutlink = get_pconfig($uid, 'gpluspost', 'skip_without_link');
    $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
    foreach ($items as $item) {
        $item['body'] = bb_CleanPictureLinks($item['body']);
        $item['body'] = bb_remove_share_information($item['body'], true);
        if ($item["title"] != "") {
            $item['body'] = "*" . $item["title"] . "*\n\n" . $item['body'];
        }
        // Looking for the first image
        $image = '';
        if (preg_match("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/is", $item['body'], $matches)) {
            $image = $matches[3];
        }
        if ($image == '') {
            if (preg_match("/\\[img\\](.*?)\\[\\/img\\]/is", $item['body'], $matches)) {
                $image = $matches[1];
            }
        }
        $multipleimages = strpos($item['body'], "[img") != strrpos($item['body'], "[img");
        // When saved into the database the content is sent through htmlspecialchars
        // That means that we have to decode all image-urls
        $image = htmlspecialchars_decode($image);
        $link = '';
        // look for bookmark-bbcode and handle it with priority
        if (preg_match("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/is", $item['body'], $matches)) {
            $link = $matches[1];
        }
        $multiplelinks = strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark");
        $body = $item['body'];
        $body = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '*$1*', $body);
        $body = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '_$1_', $body);
        $body = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '-$1-', $body);
        // At first convert the text to html
        $html = bbcode(api_clean_plain_items($body), false, false, 2);
        // Then convert it to plain text
        $msg = trim(html2plain($html, 0, true));
        $msg = html_entity_decode($msg, ENT_QUOTES, 'UTF-8');
        // If there is no bookmark element then take the first link
        if ($link == '') {
            $links = collecturls($html);
            if (sizeof($links) > 0) {
                reset($links);
                $link = current($links);
            }
            $multiplelinks = sizeof($links) > 1;
            if ($multiplelinks) {
                $html2 = bbcode($msg, false, false);
                $links2 = collecturls($html2);
                if (sizeof($links2) > 0) {
                    reset($links2);
                    $link = current($links2);
                    $multiplelinks = sizeof($links2) > 1;
                }
            }
        }
        $msglink = "";
        if ($multiplelinks) {
            $msglink = $item["plink"];
        } else {
            if ($link != "") {
                $msglink = $link;
            } else {
                if ($multipleimages) {
                    $msglink = $item["plink"];
                } else {
                    if ($image != "") {
                        $msglink = $image;
                    }
                }
            }
        }
        if ($msglink == "" and $skipwithoutlink) {
            continue;
        } else {
            if ($msglink == "") {
                $msglink = $item["plink"];
            }
        }
        // Fetching the title - or the first line
        if ($item["title"] != "") {
            $title = $item["title"];
        } else {
            $lines = explode("\n", $msg);
            $title = $lines[0];
        }
        //if ($image != $msglink)
        //	$html = trim(str_replace($msglink, "", $html));
        $title = trim(str_replace($msglink, "", $title));
        $msglink = original_url($msglink);
        if ($uid == 0) {
            $title = $item["author-name"] . ": " . $title;
        }
        $msglink = htmlspecialchars(html_entity_decode($msglink));
        if (strpos($msg, $msglink) == 0) {
            $msg .= "\n" . $msglink;
        }
        $msg = nl2br($msg);
        $title = str_replace("&", "&", $title);
        //$html = str_replace("&", "&", $html);
        echo "\t" . '<entry xmlns="http://www.w3.org/2005/Atom">' . "\n";
        echo "\t\t" . '<title type="html" xml:space="preserve"><![CDATA[' . $title . "]]></title>\n";
        echo "\t\t" . '<link rel="alternate" type="text/html" href="' . $msglink . '" />' . "\n";
        // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
        echo "\t\t<id>" . $item["uri"] . "</id>\n";
        echo "\t\t<updated>" . date("c", strtotime($item["edited"])) . "</updated>\n";
        echo "\t\t<published>" . date("c", strtotime($item["created"])) . "</published>\n";
        echo "\t\t<author>\n\t\t\t<name><![CDATA[" . $item["author-name"] . "]]></name>\n";
        echo "\t\t\t<uri>" . $item["author-link"] . "</uri>\n\t\t</author>\n";
        //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
        echo "\t\t" . '<content type="html" xml:space="preserve" xml:base="' . $item["plink"] . '"><![CDATA[' . $msg . "]]></content>\n";
        echo "\t</entry>\n";
    }
}
示例#2
0
function html2plain($html, $wraplength = 75, $compact = false)
{
    global $lang;
    $message = str_replace("\r", "", $html);
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
    @$doc->loadHTML($message);
    $xpath = new DomXPath($doc);
    $list = $xpath->query("//pre");
    foreach ($list as $node) {
        $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
    }
    $message = $doc->saveHTML();
    $message = str_replace(array("\n<", ">\n", "\r", "\n", " "), array("<", ">", "<br>", " ", ""), $message);
    $message = preg_replace('= [\\s]*=i', " ", $message);
    // Collecting all links
    $urls = collecturls($message);
    @$doc->loadHTML($message);
    node2bbcode($doc, 'html', array(), '', '');
    node2bbcode($doc, 'body', array(), '', '');
    // MyBB-Auszeichnungen
    /*
    node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
    node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
    node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
    
    node2bbcode($doc, 'strong', array(), '*', '*');
    node2bbcode($doc, 'b', array(), '*', '*');
    node2bbcode($doc, 'i', array(), '/', '/');
    node2bbcode($doc, 'u', array(), '_', '_');
    */
    if ($compact) {
        node2bbcode($doc, 'blockquote', array(), "»", "«");
    } else {
        node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
    }
    node2bbcode($doc, 'br', array(), "\n", '');
    node2bbcode($doc, 'span', array(), "", "");
    node2bbcode($doc, 'pre', array(), "", "");
    node2bbcode($doc, 'div', array(), "\r", "\r");
    node2bbcode($doc, 'p', array(), "\n", "\n");
    //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
    //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
    node2bbcode($doc, 'li', array(), "\n* ", "\n");
    node2bbcode($doc, 'hr', array(), "\n" . str_repeat("-", 70) . "\n", "");
    node2bbcode($doc, 'tr', array(), "\n", "");
    node2bbcode($doc, 'td', array(), "\t", "");
    node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
    node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
    node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
    node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
    node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
    node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
    // Problem: there is no reliable way to detect if it is a link to a tag or profile
    //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', '', true);
    node2bbcode($doc, 'a', array('href' => '/(.+)/', 'rel' => 'oembed'), ' $1 ', '', true);
    //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
    //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
    //node2bbcode($doc, 'img', array(), '', '');
    if (!$compact) {
        node2bbcode($doc, 'img', array('src' => '/(.+)/'), '[img]$1', '[/img]');
    } else {
        node2bbcode($doc, 'img', array('src' => '/(.+)/'), '', '');
    }
    node2bbcode($doc, 'iframe', array('src' => '/(.+)/'), ' $1 ', '', true);
    $message = $doc->saveHTML();
    if (!$compact) {
        $message = str_replace("[img]", "", $message);
        $message = str_replace("[/img]", "", $message);
    }
    // was ersetze ich da?
    // Irgendein stoerrisches UTF-Zeug
    $message = str_replace(chr(194) . chr(160), ' ', $message);
    $message = str_replace("&nbsp;", " ", $message);
    // Aufeinanderfolgende DIVs
    $message = preg_replace('=\\r *\\r=i', "\n", $message);
    $message = str_replace("\r", "\n", $message);
    $message = strip_tags($message);
    $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
    if (!$compact) {
        $counter = 1;
        foreach ($urls as $id => $url) {
            if (strpos($message, $url) == false) {
                $message .= "\n" . $url . " ";
            }
        }
        //$message .= "\n[".($counter++)."] ".$url;
    }
    do {
        $oldmessage = $message;
        $message = str_replace("\n\n\n", "\n\n", $message);
    } while ($oldmessage != $message);
    $message = quotelevel(trim($message), $wraplength);
    return trim($message);
}
示例#3
0
function statusnet_shortenmsg($b, $max_char)
{
    require_once "include/bbcode.php";
    require_once "include/html2plain.php";
    // Looking for the first image
    $image = '';
    if (preg_match("/\\[[zi]mg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/[zi]mg\\]/is", $b['body'], $matches)) {
        $image = $matches[3];
    }
    if ($image == '') {
        if (preg_match("/\\[[zi]mg\\](.*?)\\[\\/[zi]mg\\]/is", $b['body'], $matches)) {
            $image = $matches[1];
        }
    }
    // @fixme for zmg
    $multipleimages = strpos($b['body'], "[img") != strrpos($b['body'], "[img");
    // When saved into the database the content is sent through htmlspecialchars
    // That means that we have to decode all image-urls
    $image = htmlspecialchars_decode($image);
    $body = $b["body"];
    if ($b["title"] != "") {
        $body = $b["title"] . "\n\n" . $body;
    }
    if (strpos($body, "[bookmark") !== false) {
        // splitting the text in two parts:
        // before and after the bookmark
        $pos = strpos($body, "[bookmark");
        $body1 = substr($body, 0, $pos);
        $body2 = substr($body, $pos);
        // Removing all quotes after the bookmark
        // they are mostly only the content after the bookmark.
        $body2 = preg_replace("/\\[quote\\=([^\\]]*)\\](.*?)\\[\\/quote\\]/ism", '', $body2);
        $body2 = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", '', $body2);
        $body = $body1 . $body2;
    }
    // Add some newlines so that the message could be cut better
    $body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"), array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
    // remove the recycle signs and the names since they aren't helpful on twitter
    // recycle 1
    $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
    $body = preg_replace('/' . $recycle . '\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', "\n", $body);
    // recycle 2 (Test)
    $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
    $body = preg_replace('/' . $recycle . '\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', "\n", $body);
    // remove the share element
    $body = preg_replace("/\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", "\n\n\$2\n\n", $body);
    // At first convert the text to html
    $html = bbcode($body, false, false);
    // Then convert it to plain text
    //$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
    $msg = trim(html2plain($html, 0, true));
    $msg = html_entity_decode($msg, ENT_QUOTES, 'UTF-8');
    // Removing multiple newlines
    while (strpos($msg, "\n\n\n") !== false) {
        $msg = str_replace("\n\n\n", "\n\n", $msg);
    }
    // Removing multiple spaces
    while (strpos($msg, "  ") !== false) {
        $msg = str_replace("  ", " ", $msg);
    }
    $msg = str_replace('#^http', 'http', $msg);
    $origmsg = $msg;
    // Removing URLs
    $msg = preg_replace('/(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)/i', "", $msg);
    $msg = trim($msg);
    $link = '';
    // look for bookmark-bbcode and handle it with priority
    if (preg_match("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/is", $b['body'], $matches)) {
        $link = $matches[1];
    }
    $multiplelinks = strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark");
    // If there is no bookmark element then take the first link
    if ($link == '') {
        $links = collecturls($html);
        if (sizeof($links) > 0) {
            reset($links);
            $link = current($links);
        }
        $multiplelinks = sizeof($links) > 1;
    }
    $msglink = "";
    if ($multiplelinks) {
        $msglink = $b["plink"];
    } else {
        if ($link != "") {
            $msglink = $link;
        } else {
            if ($multipleimages) {
                $msglink = $b["plink"];
            } else {
                if ($image != "") {
                    $msglink = $image;
                }
            }
        }
    }
    if ($msglink == "" and strlen($msg) > $max_char) {
        $msglink = $b["plink"];
    }
    // If the message is short enough then don't modify it. (if the link exists in the original message)
    if (strlen(trim($origmsg)) <= $max_char and (strpos($origmsg, $msglink) or $msglink == "")) {
        return array("msg" => trim($origmsg), "image" => "");
    }
    if (strlen($msglink) > 20) {
        $msglink = short_link($msglink);
    }
    if (strlen(trim($msg . " " . $msglink)) > $max_char) {
        $msg = substr($msg, 0, $max_char - strlen($msglink));
        $lastchar = substr($msg, -1);
        $msg = substr($msg, 0, -1);
        $pos = strrpos($msg, "\n");
        if ($pos > 0) {
            $msg = substr($msg, 0, $pos);
        } else {
            if ($lastchar != "\n") {
                $msg = substr($msg, 0, -3) . "...";
            }
        }
    }
    $msg = str_replace("\n", " ", $msg);
    // Removing multiple spaces - again
    while (strpos($msg, "  ") !== false) {
        $msg = str_replace("  ", " ", $msg);
    }
    return array("msg" => trim($msg . " " . $msglink), "image" => $image);
}
示例#4
0
/**
 * @param App $a
 * @param object $b
 * @return mixed
 */
function facebook_post_hook(&$a, &$b)
{
    if ($b['deleted'] || $b['created'] !== $b['edited']) {
        return;
    }
    /**
     * Post to Facebook stream
     */
    require_once 'include/group.php';
    require_once 'include/html2plain.php';
    logger('Facebook post');
    $reply = false;
    $likes = false;
    $deny_arr = array();
    $allow_arr = array();
    $toplevel = $b['id'] == $b['parent'] ? true : false;
    $linking = get_pconfig($b['uid'], 'facebook', 'no_linking') ? 0 : 1;
    if (!$toplevel && $linking) {
        $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($b['parent']), intval($b['uid']));
        if (count($r) && substr($r[0]['uri'], 0, 4) === 'fb::') {
            $reply = substr($r[0]['uri'], 4);
        } elseif (count($r) && substr($r[0]['extid'], 0, 4) === 'fb::') {
            $reply = substr($r[0]['extid'], 4);
        } else {
            return;
        }
        $u = q("SELECT * FROM user where uid = %d limit 1", intval($b['uid']));
        if (!count($u)) {
            return;
        }
        // only accept comments from the item owner. Other contacts are unknown to FB.
        if (!link_compare($b['author-link'], $a->get_baseurl() . '/profile/' . $u[0]['nickname'])) {
            return;
        }
        logger('facebook reply id=' . $reply);
    }
    if (strstr($b['postopts'], 'facebook') || $b['private'] || $reply) {
        if ($b['private'] && $reply === false) {
            $allow_people = expand_acl($b['allow_cid']);
            $allow_groups = expand_groups(expand_acl($b['allow_gid']));
            $deny_people = expand_acl($b['deny_cid']);
            $deny_groups = expand_groups(expand_acl($b['deny_gid']));
            $recipients = array_unique(array_merge($allow_people, $allow_groups));
            $deny = array_unique(array_merge($deny_people, $deny_groups));
            $allow_str = dbesc(implode(', ', $recipients));
            if ($allow_str) {
                $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( {$allow_str} ) AND `network` = 'face'");
                if (count($r)) {
                    foreach ($r as $rr) {
                        $allow_arr[] = $rr['notify'];
                    }
                }
            }
            $deny_str = dbesc(implode(', ', $deny));
            if ($deny_str) {
                $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( {$deny_str} ) AND `network` = 'face'");
                if (count($r)) {
                    foreach ($r as $rr) {
                        $deny_arr[] = $rr['notify'];
                    }
                }
            }
            if (count($deny_arr) && !count($allow_arr)) {
                // One or more FB folks were denied access but nobody on FB was specifically allowed access.
                // This might cause the post to be open to public on Facebook, but only to selected members
                // on another network. Since this could potentially leak a post to somebody who was denied,
                // we will skip posting it to Facebook with a slightly vague but relevant message that will
                // hopefully lead somebody to this code comment for a better explanation of what went wrong.
                notice(t('Post to Facebook cancelled because of multi-network access permission conflict.') . EOL);
                return;
            }
            // if it's a private message but no Facebook members are allowed or denied, skip Facebook post
            if (!count($allow_arr) && !count($deny_arr)) {
                return;
            }
        }
        if ($b['verb'] == ACTIVITY_LIKE) {
            $likes = true;
        }
        $appid = get_config('facebook', 'appid');
        $secret = get_config('facebook', 'appsecret');
        if ($appid && $secret) {
            logger('facebook: have appid+secret');
            $fb_token = get_pconfig($b['uid'], 'facebook', 'access_token');
            // post to facebook if it's a public post and we've ticked the 'post to Facebook' box,
            // or it's a private message with facebook participants
            // or it's a reply or likes action to an existing facebook post
            if ($fb_token && ($toplevel || $b['private'] || $reply)) {
                logger('facebook: able to post');
                require_once 'library/facebook.php';
                require_once 'include/bbcode.php';
                $msg = $b['body'];
                logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
                // make links readable before we strip the code
                // unless it's a dislike - just send the text as a comment
                // if($b['verb'] == ACTIVITY_DISLIKE)
                //	$msg = trim(strip_tags(bbcode($msg)));
                // Old code
                /*$search_str = $a->get_baseurl() . '/search';
                
                				if(preg_match("/\[url=(.*?)\](.*?)\[\/url\]/is",$msg,$matches)) {
                
                					// don't use hashtags for message link
                
                					if(strpos($matches[2],$search_str) === false) {
                						$link = $matches[1];
                						if(substr($matches[2],0,5) != '[img]')
                							$linkname = $matches[2];
                					}
                				}
                
                				// strip tag links to avoid link clutter, this really should be 
                				// configurable because we're losing information
                
                				$msg = preg_replace("/\#\[url=(.*?)\](.*?)\[\/url\]/is",'#$2',$msg);
                
                				// provide the link separately for normal links
                				$msg = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/is",'$2 $1',$msg);
                
                				if(preg_match("/\[img\](.*?)\[\/img\]/is",$msg,$matches))
                					$image = $matches[1];
                
                				$msg = preg_replace("/\[img\](.*?)\[\/img\]/is", t('Image: ') . '$1', $msg);
                
                				if((strpos($link,z_root()) !== false) && (! $image))
                					$image = $a->get_baseurl() . '/images/friendica-64.jpg';
                
                				$msg = trim(strip_tags(bbcode($msg)));*/
                // New code
                // Looking for the first image
                $image = '';
                if (preg_match("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/is", $b['body'], $matches)) {
                    $image = $matches[3];
                }
                if ($image == '') {
                    if (preg_match("/\\[img\\](.*?)\\[\\/img\\]/is", $b['body'], $matches)) {
                        $image = $matches[1];
                    }
                }
                // Checking for a bookmark element
                $body = $b['body'];
                if (strpos($body, "[bookmark") !== false) {
                    // splitting the text in two parts:
                    // before and after the bookmark
                    $pos = strpos($body, "[bookmark");
                    $body1 = substr($body, 0, $pos);
                    $body2 = substr($body, $pos);
                    // Removing the bookmark and all quotes after the bookmark
                    // they are mostly only the content after the bookmark.
                    $body2 = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '', $body2);
                    $body2 = preg_replace("/\\[quote\\=([^\\]]*)\\](.*?)\\[\\/quote\\]/ism", '', $body2);
                    $body2 = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", '', $body2);
                    $body = $body1 . $body2;
                }
                // At first convert the text to html
                $html = bbcode($body);
                // Then convert it to plain text
                $msg = trim($b['title'] . " \n\n" . html2plain($html, 0, true));
                $msg = html_entity_decode($msg, ENT_QUOTES, 'UTF-8');
                // Removing multiple newlines
                while (strpos($msg, "\n\n\n") !== false) {
                    $msg = str_replace("\n\n\n", "\n\n", $msg);
                }
                // add any attachments as text urls
                $arr = explode(',', $b['attach']);
                if (count($arr)) {
                    $msg .= "\n";
                    foreach ($arr as $r) {
                        $matches = false;
                        $cnt = preg_match('|\\[attach\\]href=\\"(.*?)\\" size=\\"(.*?)\\" type=\\"(.*?)\\" title=\\"(.*?)\\"\\[\\/attach\\]|', $r, $matches);
                        if ($cnt) {
                            $msg .= "\n" . $matches[1];
                        }
                    }
                }
                $link = '';
                $linkname = '';
                // look for bookmark-bbcode and handle it with priority
                if (preg_match("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/is", $b['body'], $matches)) {
                    $link = $matches[1];
                    $linkname = $matches[2];
                }
                // If there is no bookmark element then take the first link
                if ($link == '') {
                    $links = collecturls($html);
                    if (sizeof($links) > 0) {
                        reset($links);
                        $link = current($links);
                    }
                }
                // Remove trailing and leading spaces
                $msg = trim($msg);
                // Since facebook increased the maxpostlen massively this never should happen again :)
                if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
                    require_once 'library/slinky.php';
                    $display_url = $b['plink'];
                    $slinky = new Slinky($display_url);
                    // 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;
                }
                // Fallback - if message is empty
                if (!strlen($msg)) {
                    $msg = $link;
                }
                if (!strlen($msg)) {
                    $msg = $image;
                }
                if (!strlen($msg)) {
                    $msg = $linkname;
                }
                // If there is nothing to post then exit
                if (!strlen($msg)) {
                    return;
                }
                logger('Facebook post: msg=' . $msg, LOGGER_DATA);
                if ($likes) {
                    $postvars = array('access_token' => $fb_token);
                } else {
                    $postvars = array('access_token' => $fb_token, 'message' => $msg);
                    if (isset($image)) {
                        $postvars['picture'] = $image;
                        //$postvars['type'] = "photo";
                    }
                    if (isset($link)) {
                        $postvars['link'] = $link;
                        //$postvars['type'] = "link";
                    }
                    if (isset($linkname)) {
                        $postvars['name'] = $linkname;
                    }
                }
                if ($b['private'] && $toplevel) {
                    $postvars['privacy'] = '{"value": "CUSTOM", "friends": "SOME_FRIENDS"';
                    if (count($allow_arr)) {
                        $postvars['privacy'] .= ',"allow": "' . implode(',', $allow_arr) . '"';
                    }
                    if (count($deny_arr)) {
                        $postvars['privacy'] .= ',"deny": "' . implode(',', $deny_arr) . '"';
                    }
                    $postvars['privacy'] .= '}';
                }
                if ($reply) {
                    $url = 'https://graph.facebook.com/' . $reply . '/' . ($likes ? 'likes' : 'comments');
                } else {
                    if ($link != "" or $image != "" or $b['title'] == '' or strlen($msg) < 500) {
                        $url = 'https://graph.facebook.com/me/feed';
                        if ($b['plink']) {
                            $postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' . $b['plink'] . '"}';
                        }
                    } else {
                        // if its only a message and a subject and the message is larger than 500 characters then post it as note
                        $postvars = array('access_token' => $fb_token, 'message' => bbcode($b['body']), 'subject' => $b['title']);
                        $url = 'https://graph.facebook.com/me/notes';
                    }
                }
                logger('facebook: post to ' . $url);
                logger('facebook: postvars: ' . print_r($postvars, true));
                // "test_mode" prevents anything from actually being posted.
                // Otherwise, let's do it.
                if (!get_config('facebook', 'test_mode')) {
                    $x = post_url($url, $postvars);
                    logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
                    $retj = json_decode($x);
                    if ($retj->id) {
                        q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1", dbesc('fb::' . $retj->id), intval($b['id']));
                    } else {
                        if (!$likes) {
                            $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $postvars));
                            require_once 'include/queue_fn.php';
                            add_to_queue($a->contact, NETWORK_FACEBOOK, $s);
                            notice(t('Facebook post failed. Queued for retry.') . EOL);
                        }
                        if (isset($retj->error) && $retj->error->type == "OAuthException" && $retj->error->code == 190) {
                            logger('Facebook session has expired due to changed password.', LOGGER_DEBUG);
                            $last_notification = get_pconfig($b['uid'], 'facebook', 'session_expired_mailsent');
                            if (!$last_notification || $last_notification < time() - FACEBOOK_SESSION_ERR_NOTIFICATION_INTERVAL) {
                                require_once 'include/enotify.php';
                                $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($b['uid']));
                                notification(array('uid' => $b['uid'], 'type' => NOTIFY_SYSTEM, 'system_type' => 'facebook_connection_invalid', 'language' => $r[0]['language'], 'to_name' => $r[0]['username'], 'to_email' => $r[0]['email'], 'source_name' => t('Administrator'), 'source_link' => $a->config["system"]["url"], 'source_photo' => $a->config["system"]["url"] . '/images/person-80.jpg'));
                                set_pconfig($b['uid'], 'facebook', 'session_expired_mailsent', time());
                            } else {
                                logger('Facebook: No notification, as the last one was sent on ' . $last_notification, LOGGER_DEBUG);
                            }
                        }
                    }
                }
            }
        }
    }
}