/** @file */ function oembed_replacecb($matches) { $embedurl = $matches[1]; $j = oembed_fetch_url($embedurl); $s = oembed_format_object($j); return $s; }
function oembed_replacecb($matches) { // logger('oembedcb'); $embedurl = $matches[1]; $j = oembed_fetch_url($embedurl); $s = oembed_format_object($j); return $s; //oembed_iframe($s,$j->width,$j->height); }
function nakedoembed($match) { $url = count($match) == 2 ? $match[1] : $match[2]; $o = oembed_fetch_url($url); if ($o['type'] == 'error') { return $match[0]; } return '[embed]' . $url . '[/embed]'; }
function oembed_process($url) { $j = oembed_fetch_url($url); logger('oembed_process: ' . print_r($j, true)); if ($j && $j->type !== 'error') { return '[embed]' . $url . '[/embed]'; } return false; }
function tryoembed($match) { $url = count($match) == 2 ? $match[1] : $match[2]; $o = oembed_fetch_url($url); if ($o->type == 'error') { return $match[0]; } $html = oembed_format_object($o); return $html; }
function oembed_content(&$a) { if ($a->argc == 2) { echo "<html><body>"; $url = base64url_decode($a->argv[1]); $j = oembed_fetch_url($url); echo $j->html; echo "</body></html>"; } killme(); }
function tryoembed($match) { $url = count($match) == 2 ? $match[1] : $match[2]; $o = oembed_fetch_url($url); //echo "<pre>"; var_dump($match, $url, $o); killme(); if ($o->type == "error") { return $match[0]; } $html = oembed_format_object($o); return $html; //oembed_iframe($html,$o->width,$o->height); }
function oembed_init(&$a) { // logger('mod_oembed ' . $a->query_string, LOGGER_ALL); if (argc() > 1) { if (argv(1) == 'b2h') { $url = array("", trim(hex2bin($_REQUEST['url']))); echo oembed_replacecb($url); killme(); } elseif (argv(1) == 'h2b') { $text = trim(hex2bin($_REQUEST['text'])); echo oembed_html2bbcode($text); killme(); } else { echo "<html><body>"; $j = oembed_fetch_url(base64url_decode(argv(1))); echo $j->html; // logger('mod-oembed ' . $j->html, LOGGER_ALL); echo "</body></html>"; } } killme(); }
function init() { // logger('mod_oembed ' . \App::$query_string, LOGGER_ALL); if (argc() > 1) { if (argv(1) == 'b2h') { $url = array("", trim(hex2bin($_REQUEST['url']))); echo oembed_replacecb($url); killme(); } elseif (argv(1) == 'h2b') { $text = trim(hex2bin($_REQUEST['text'])); echo oembed_html2bbcode($text); killme(); } else { echo "<html><head><base target=\"_blank\" /></head><body>"; $src = base64url_decode(argv(1)); $j = oembed_fetch_url($src); echo $j['html']; // logger('mod-oembed ' . $h, LOGGER_ALL); echo "</body></html>"; } } killme(); }
function oembed_content(&$a) { // logger('mod_oembed ' . $a->query_string, LOGGER_ALL); if ($a->argv[1] == 'b2h') { $url = array("", trim(hex2bin($_REQUEST['url']))); echo oembed_replacecb($url); killme(); } if ($a->argv[1] == 'h2b') { $text = trim(hex2bin($_REQUEST['text'])); echo oembed_html2bbcode($text); killme(); } if ($a->argc == 2) { echo "<html><body>"; $url = base64url_decode($a->argv[1]); $j = oembed_fetch_url($url); echo $j->html; // logger('mod-oembed ' . $j->html, LOGGER_ALL); echo "</body></html>"; } killme(); }
function tryoembed($match) { //$url = ((count($match)==2)?$match[1]:$match[2]); $url = $match[1]; // Always embed the SSL version $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"), array("https://www.youtube.com/", "https://player.vimeo.com/"), $url); //logger("tryoembed: $url"); $o = oembed_fetch_url($url); if (isset($match[2])) { $o->title = $match[2]; } if ($o->type == "error") { return $match[0]; } $html = oembed_format_object($o); return $html; //oembed_iframe($html,$o->width,$o->height); }
function parseurl_getsiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) { require_once "include/network.php"; $a = get_app(); $siteinfo = array(); if ($count > 10) { logger("parseurl_getsiteinfo: Endless loop detected for " . $url, LOGGER_DEBUG); return $siteinfo; } $url = trim($url, "'"); $url = trim($url, '"'); $url = original_url($url); $siteinfo["url"] = $url; $siteinfo["type"] = "link"; $stamp1 = microtime(true); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent()); $header = curl_exec($ch); $curl_info = @curl_getinfo($ch); $http_code = $curl_info['http_code']; curl_close($ch); $a->save_timestamp($stamp1, "network"); if (($curl_info['http_code'] == "301" or $curl_info['http_code'] == "302" or $curl_info['http_code'] == "303" or $curl_info['http_code'] == "307") and ($curl_info['redirect_url'] != "" or $curl_info['location'] != "")) { if ($curl_info['redirect_url'] != "") { $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count); } else { $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count); } return $siteinfo; } // if the file is too large then exit if ($curl_info["download_content_length"] > 1000000) { return $siteinfo; } // if it isn't a HTML file then exit if ($curl_info["content_type"] != "" and !strstr(strtolower($curl_info["content_type"]), "html")) { return $siteinfo; } if ($do_oembed) { require_once "include/oembed.php"; $oembed_data = oembed_fetch_url($url); if ($oembed_data->type != "error") { $siteinfo["type"] = $oembed_data->type; } if ($oembed_data->type == "link" and $siteinfo["type"] != "photo") { if (isset($oembed_data->title)) { $siteinfo["title"] = $oembed_data->title; } if (isset($oembed_data->description)) { $siteinfo["text"] = trim($oembed_data->description); } if (isset($oembed_data->thumbnail_url)) { $siteinfo["image"] = $oembed_data->thumbnail_url; } } } $stamp1 = microtime(true); // Now fetch the body as well $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent()); $header = curl_exec($ch); $curl_info = @curl_getinfo($ch); $http_code = $curl_info['http_code']; curl_close($ch); $a->save_timestamp($stamp1, "network"); // Fetch the first mentioned charset. Can be in body or header $charset = ""; if (preg_match('/charset=(.*?)[' . "'" . '"\\s\\n]/', $header, $matches)) { $charset = trim(trim(trim(array_pop($matches)), ';,')); } if ($charset == "") { $charset = "utf-8"; } $pos = strpos($header, "\r\n\r\n"); if ($pos) { $body = trim(substr($header, $pos)); } else { $body = $header; } if ($charset != '' and strtoupper($charset) != "UTF-8") { logger("parseurl_getsiteinfo: detected charset " . $charset, LOGGER_DEBUG); //$body = mb_convert_encoding($body, "UTF-8", $charset); $body = iconv($charset, "UTF-8//TRANSLIT", $body); } $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8"); $doc = new DOMDocument(); @$doc->loadHTML($body); deletenode($doc, 'style'); deletenode($doc, 'script'); deletenode($doc, 'option'); deletenode($doc, 'h1'); deletenode($doc, 'h2'); deletenode($doc, 'h3'); deletenode($doc, 'h4'); deletenode($doc, 'h5'); deletenode($doc, 'h6'); deletenode($doc, 'ol'); deletenode($doc, 'ul'); $xpath = new DomXPath($doc); $list = $xpath->query("//meta[@content]"); foreach ($list as $node) { $attr = array(); if ($node->attributes->length) { foreach ($node->attributes as $attribute) { $attr[$attribute->name] = $attribute->value; } } if (@$attr["http-equiv"] == 'refresh') { $path = $attr["content"]; $pathinfo = explode(";", $path); $content = ""; foreach ($pathinfo as $value) { if (substr(strtolower($value), 0, 4) == "url=") { $content = substr($value, 4); } } if ($content != "") { $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count); return $siteinfo; } } } //$list = $xpath->query("head/title"); $list = $xpath->query("//title"); foreach ($list as $node) { $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8"); } //$list = $xpath->query("head/meta[@name]"); $list = $xpath->query("//meta[@name]"); foreach ($list as $node) { $attr = array(); if ($node->attributes->length) { foreach ($node->attributes as $attribute) { $attr[$attribute->name] = $attribute->value; } } $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8")); if ($attr["content"] != "") { switch (strtolower($attr["name"])) { case "fulltitle": $siteinfo["title"] = $attr["content"]; break; case "description": $siteinfo["text"] = $attr["content"]; break; case "thumbnail": $siteinfo["image"] = $attr["content"]; break; case "twitter:image": $siteinfo["image"] = $attr["content"]; break; case "twitter:image:src": $siteinfo["image"] = $attr["content"]; break; case "twitter:card": if ($siteinfo["type"] == "" or $attr["content"] == "photo") { $siteinfo["type"] = $attr["content"]; } break; case "twitter:description": $siteinfo["text"] = $attr["content"]; break; case "twitter:title": $siteinfo["title"] = $attr["content"]; break; case "dc.title": $siteinfo["title"] = $attr["content"]; break; case "dc.description": $siteinfo["text"] = $attr["content"]; break; case "keywords": $keywords = explode(",", $attr["content"]); break; case "news_keywords": $keywords = explode(",", $attr["content"]); break; } } if ($siteinfo["type"] == "summary") { $siteinfo["type"] = "link"; } } if (isset($keywords)) { $siteinfo["keywords"] = array(); foreach ($keywords as $keyword) { $siteinfo["keywords"][] = trim($keyword); } } //$list = $xpath->query("head/meta[@property]"); $list = $xpath->query("//meta[@property]"); foreach ($list as $node) { $attr = array(); if ($node->attributes->length) { foreach ($node->attributes as $attribute) { $attr[$attribute->name] = $attribute->value; } } $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8")); if ($attr["content"] != "") { switch (strtolower($attr["property"])) { case "og:image": $siteinfo["image"] = $attr["content"]; break; case "og:title": $siteinfo["title"] = $attr["content"]; break; case "og:description": $siteinfo["text"] = $attr["content"]; break; } } } if (@$siteinfo["image"] == "" and !$no_guessing) { $list = $xpath->query("//img[@src]"); foreach ($list as $node) { $attr = array(); if ($node->attributes->length) { foreach ($node->attributes as $attribute) { $attr[$attribute->name] = $attribute->value; } } $src = completeurl($attr["src"], $url); $photodata = @getimagesize($src); if ($photodata && $photodata[0] > 150 and $photodata[1] > 150) { if ($photodata[0] > 300) { $photodata[1] = round($photodata[1] * (300 / $photodata[0])); $photodata[0] = 300; } if ($photodata[1] > 300) { $photodata[0] = round($photodata[0] * (300 / $photodata[1])); $photodata[1] = 300; } $siteinfo["images"][] = array("src" => $src, "width" => $photodata[0], "height" => $photodata[1]); } } } else { $src = completeurl($siteinfo["image"], $url); unset($siteinfo["image"]); $photodata = @getimagesize($src); if ($photodata && $photodata[0] > 10 and $photodata[1] > 10) { $siteinfo["images"][] = array("src" => $src, "width" => $photodata[0], "height" => $photodata[1]); } } if (@$siteinfo["text"] == "" and @$siteinfo["title"] != "" and !$no_guessing) { $text = ""; $list = $xpath->query("//div[@class='article']"); foreach ($list as $node) { if (strlen($node->nodeValue) > 40) { $text .= " " . trim($node->nodeValue); } } if ($text == "") { $list = $xpath->query("//div[@class='content']"); foreach ($list as $node) { if (strlen($node->nodeValue) > 40) { $text .= " " . trim($node->nodeValue); } } } // If none text was found then take the paragraph content if ($text == "") { $list = $xpath->query("//p"); foreach ($list as $node) { if (strlen($node->nodeValue) > 40) { $text .= " " . trim($node->nodeValue); } } } if ($text != "") { $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text)); while (strpos($text, " ")) { $text = trim(str_replace(" ", " ", $text)); } $siteinfo["text"] = trim(html_entity_decode(substr($text, 0, 350), ENT_QUOTES, "UTF-8") . '...'); } } logger("parseurl_getsiteinfo: Siteinfo for " . $url . " " . print_r($siteinfo, true), LOGGER_DEBUG); call_hooks('getsiteinfo', $siteinfo); return $siteinfo; }
function twitter_expand_entities($a, $body, $item, $no_tags = false, $dontincludemedia) { require_once "include/oembed.php"; $tags = ""; if (isset($item->entities->urls)) { $type = ""; $footerurl = ""; $footerlink = ""; $footer = ""; foreach ($item->entities->urls as $url) { if ($url->url and $url->expanded_url and $url->display_url) { $expanded_url = twitter_original_url($url->expanded_url); $oembed_data = oembed_fetch_url($expanded_url); // Quickfix: Workaround for URL with "[" and "]" in it if (strpos($expanded_url, "[") or strpos($expanded_url, "]")) { $expanded_url = $url->url; } if ($type == "") { $type = $oembed_data->type; } if ($oembed_data->type == "video") { $body = str_replace($url->url, "[video]" . $expanded_url . "[/video]", $body); $dontincludemedia = true; } elseif ($oembed_data->type == "photo" and isset($oembed_data->url) and !$dontincludemedia) { $body = str_replace($url->url, "[url=" . $expanded_url . "][img]" . $oembed_data->url . "[/img][/url]", $body); $dontincludemedia = true; } elseif ($oembed_data->type != "link") { $body = str_replace($url->url, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body); } else { $img_str = fetch_url($expanded_url, true, $redirects, 4); $tempfile = tempnam(get_config("system", "temppath"), "cache"); file_put_contents($tempfile, $img_str); $mime = image_type_to_mime_type(exif_imagetype($tempfile)); unlink($tempfile); if (substr($mime, 0, 6) == "image/") { $type = "photo"; $body = str_replace($url->url, "[img]" . $expanded_url . "[/img]", $body); $dontincludemedia = true; } else { $type = $oembed_data->type; $footerurl = $expanded_url; $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]"; //$footerlink = "[url=".$expanded_url."]".$url->display_url."[/url]"; $body = str_replace($url->url, $footerlink, $body); } } } } if ($footerurl != "") { $footer = twitter_siteinfo($footerurl, $dontincludemedia); } if ($footerlink != "" and trim($footer) != "") { $removedlink = trim(str_replace($footerlink, "", $body)); if (strstr($body, $removedlink)) { $body = $removedlink; } $body .= "\n\n[class=type-" . $type . "]" . $footer . "[/class]"; } if ($no_tags) { return array("body" => $body, "tags" => ""); } $tags_arr = array(); foreach ($item->entities->hashtags as $hashtag) { $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]"; $tags_arr["#" . $hashtag->text] = $url; $body = str_replace("#" . $hashtag->text, $url, $body); } foreach ($item->entities->user_mentions as $mention) { $url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]"; $tags_arr["@" . $mention->screen_name] = $url; $body = str_replace("@" . $mention->screen_name, $url, $body); } // it seems as if the entities aren't always covering all mentions. So the rest will be checked here $tags = get_tags($body); if (count($tags)) { foreach ($tags as $tag) { if (strstr(trim($tag), " ")) { continue; } if (strpos($tag, '#') === 0) { if (strpos($tag, '[url=')) { continue; } // don't link tags that are already embedded in links if (preg_match('/\\[(.*?)' . preg_quote($tag, '/') . '(.*?)\\]/', $body)) { continue; } if (preg_match('/\\[(.*?)\\]\\((.*?)' . preg_quote($tag, '/') . '(.*?)\\)/', $body)) { continue; } $basetag = str_replace('_', ' ', substr($tag, 1)); $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; $body = str_replace($tag, $url, $body); $tags_arr["#" . $basetag] = $url; continue; } elseif (strpos($tag, '@') === 0) { if (strpos($tag, '[url=')) { continue; } $basetag = substr($tag, 1); $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; $body = str_replace($tag, $url, $body); $tags_arr["@" . $basetag] = $url; } } } $tags = implode($tags_arr, ","); } return array("body" => $body, "tags" => $tags); }
function statusnet_convertmsg($a, $body, $no_tags = false) { require_once "include/oembed.php"; require_once "include/items.php"; require_once "include/network.php"; $body = preg_replace("=\\[url\\=https?://([0-9]*).([0-9]*).([0-9]*).([0-9]*)/([0-9]*)\\](.*?)\\[\\/url\\]=ism", "\$1.\$2.\$3.\$4/\$5", $body); $URLSearchString = "^\\[\\]"; $links = preg_match_all("/[^!#@]\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", $body, $matches, PREG_SET_ORDER); $footer = ""; $footerurl = ""; $footerlink = ""; $type = ""; if ($links) { foreach ($matches as $match) { $search = "[url=" . $match[1] . "]" . $match[2] . "[/url]"; logger("statusnet_convertmsg: expanding url " . $match[1], LOGGER_DEBUG); $expanded_url = original_url($match[1]); logger("statusnet_convertmsg: fetching data for " . $expanded_url, LOGGER_DEBUG); $oembed_data = oembed_fetch_url($expanded_url, true); logger("statusnet_convertmsg: fetching data: done", LOGGER_DEBUG); if ($type == "") { $type = $oembed_data->type; } if ($oembed_data->type == "video") { //$body = str_replace($search, "[video]".$expanded_url."[/video]", $body); $type = $oembed_data->type; $footerurl = $expanded_url; $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]"; $body = str_replace($search, $footerlink, $body); } elseif ($oembed_data->type == "photo" and isset($oembed_data->url) and !$dontincludemedia) { $body = str_replace($search, "[url=" . $expanded_url . "][img]" . $oembed_data->url . "[/img][/url]", $body); } elseif ($oembed_data->type != "link") { $body = str_replace($search, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body); } else { $img_str = fetch_url($expanded_url, true, $redirects, 4); $tempfile = tempnam(get_temppath(), "cache"); file_put_contents($tempfile, $img_str); $mime = image_type_to_mime_type(exif_imagetype($tempfile)); unlink($tempfile); if (substr($mime, 0, 6) == "image/") { $type = "photo"; $body = str_replace($search, "[img]" . $expanded_url . "[/img]", $body); } else { $type = $oembed_data->type; $footerurl = $expanded_url; $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]"; $body = str_replace($search, $footerlink, $body); } } } if ($footerurl != "") { $footer = add_page_info($footerurl); } if ($footerlink != "" and trim($footer) != "") { $removedlink = trim(str_replace($footerlink, "", $body)); if ($removedlink == "" or strstr($body, $removedlink)) { $body = $removedlink; } $body .= $footer; } } if ($no_tags) { return array("body" => $body, "tags" => ""); } $str_tags = ''; $cnt = preg_match_all("/([!#@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", $body, $matches, PREG_SET_ORDER); if ($cnt) { foreach ($matches as $mtch) { if (strlen($str_tags)) { $str_tags .= ','; } if ($mtch[1] == "#") { // Replacing the hash tags that are directed to the GNU Social server with internal links $snhash = "#[url=" . $mtch[2] . "]" . $mtch[3] . "[/url]"; $frdchash = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($mtch[3]) . ']' . $mtch[3] . '[/url]'; $body = str_replace($snhash, $frdchash, $body); $str_tags .= $frdchash; } else { $str_tags .= "@[url=" . $mtch[2] . "]" . $mtch[3] . "[/url]"; } // To-Do: // There is a problem with links with to GNU Social groups, so these links are stored with "@" like friendica groups //$str_tags .= $mtch[1]."[url=".$mtch[2]."]".$mtch[3]."[/url]"; } } return array("body" => $body, "tags" => $str_tags); }
function fbpost_fetchwall($a, $uid) { require_once "include/oembed.php"; require_once "include/network.php"; require_once "include/items.php"; require_once "mod/item.php"; require_once "include/bbcode.php"; $access_token = get_pconfig($uid, 'facebook', 'access_token'); $post_to_page = get_pconfig($uid, 'facebook', 'post_to_page'); $mirror_page = get_pconfig($uid, 'facebook', 'mirror_page'); $lastcreated = get_pconfig($uid, 'facebook', 'last_created'); if ((int) $post_to_page == 0) { $post_to_page = "me"; } if ((int) $mirror_page != 0) { $post_to_page = $mirror_page; } $url = "https://graph.facebook.com/" . $post_to_page . "/feed?access_token=" . $access_token; $first_time = $lastcreated == ""; if ($lastcreated != "") { $url .= "&since=" . urlencode($lastcreated); } $feed = fetch_url($url); $data = json_decode($feed); if (!is_array($data->data)) { return; } $items = array_reverse($data->data); foreach ($items as $item) { if ($item->created_time > $lastcreated) { $lastcreated = $item->created_time; } if ($first_time) { continue; } if ($item->application->id == get_config('facebook', 'appid')) { continue; } if (isset($item->privacy) && $item->privacy->value !== 'EVERYONE' && (int) $mirror_page == 0) { continue; } elseif (isset($item->privacy) && $item->privacy->value !== 'EVERYONE' && $item->privacy->value !== '') { continue; } elseif (!isset($item->privacy)) { continue; } if ($post_to_page != $item->from->id and (int) $post_to_page != 0) { continue; } if (!strstr($item->id, $item->from->id . "_") and isset($item->to) and (int) $post_to_page == 0) { continue; } $_SESSION["authenticated"] = true; $_SESSION["uid"] = $uid; unset($_REQUEST); $_REQUEST["type"] = "wall"; $_REQUEST["api_source"] = true; $_REQUEST["profile_uid"] = $uid; //$_REQUEST["source"] = "Facebook"; $_REQUEST["source"] = $item->application->name; $_REQUEST["extid"] = NETWORK_FACEBOOK; $_REQUEST["title"] = ""; $_REQUEST["body"] = isset($item->message) ? escape_tags($item->message) : ''; $pagedata = array(); $content = ""; $pagedata["type"] = ""; if (isset($item->name) and isset($item->link)) { $item->link = original_url($item->link); $oembed_data = oembed_fetch_url($item->link); $pagedata["type"] = $oembed_data->type; $pagedata["url"] = $item->link; $pagedata["title"] = $item->name; $content = "[bookmark=" . $item->link . "]" . $item->name . "[/bookmark]"; // If a link is not only attached but also added in the body, look if it can be removed in the body. $removedlink = trim(str_replace($item->link, "", $_REQUEST["body"])); if ($removedlink == "" or strstr($_REQUEST["body"], $removedlink)) { $_REQUEST["body"] = $removedlink; } } elseif (isset($item->name)) { $content .= "[b]" . $item->name . "[/b]"; } $pagedata["text"] = ""; if (isset($item->description) and $item->type != "photo") { $pagedata["text"] = $item->description; } if (isset($item->caption) and $item->type == "photo") { $pagedata["text"] = $item->caption; } // Only import the picture when the message is no video // oembed display a picture of the video as well //if ($item->type != "video") { //if (($item->type != "video") and ($item->type != "photo")) { if ($pagedata["type"] == "" or $pagedata["type"] == "link") { $pagedata["type"] = $item->type; if (isset($item->picture)) { $pagedata["images"][0]["src"] = $item->picture; } if ($pagedata["type"] == "photo" and isset($item->object_id)) { logger('fbpost_fetchwall: fetching fbid ' . $item->object_id, LOGGER_DEBUG); $url = "https://graph.facebook.com/" . $item->object_id . "?access_token=" . $access_token; $feed = fetch_url($url); $data = json_decode($feed); if (isset($data->images)) { $pagedata["images"][0]["src"] = $data->images[0]->source; logger('got fbid image from images for ' . $item->object_id, LOGGER_DEBUG); } elseif (isset($data->source)) { $pagedata["images"][0]["src"] = $data->source; logger('got fbid image from source for ' . $item->object_id, LOGGER_DEBUG); } elseif (isset($data->picture)) { $pagedata["images"][0]["src"] = $data->picture; logger('got fbid image from picture for ' . $item->object_id, LOGGER_DEBUG); } } if (trim($_REQUEST["body"] . $content . $pagedata["text"]) == '') { logger('facebook: empty body 1 ' . $item->id . ' ' . print_r($item, true)); continue; } $pagedata["images"][0]["src"] = fbpost_cleanpicture($pagedata["images"][0]["src"]); if ($pagedata["images"][0]["src"] != "" && isset($item->link)) { $item->link = original_url($item->link); $pagedata["url"] = $item->link; $content .= "\n" . '[url=' . $item->link . '][img]' . $pagedata["images"][0]["src"] . '[/img][/url]'; } else { if ($pagedata["images"][0]["src"] != "") { $content .= "\n" . '[img]' . $pagedata["images"][0]["src"] . '[/img]'; } // if just a link, it may be a wall photo - check if (isset($item->link)) { $content .= fbpost_get_photo($uid, $item->link); } } } if (trim($_REQUEST["body"] . $content . $pagedata["text"]) == '') { logger('facebook: empty body 2 ' . $item->id . ' ' . print_r($item, true)); continue; } if ($pagedata["type"] != "") { $_REQUEST["body"] .= add_page_info_data($pagedata); } else { if ($content) { $_REQUEST["body"] .= "\n" . trim($content); } if ($pagedata["text"]) { $_REQUEST["body"] .= "\n[quote]" . $pagedata["text"] . "[/quote]"; } $_REQUEST["body"] = trim($_REQUEST["body"]); } if (isset($item->place)) { if ($item->place->name or $item->place->location->street or $item->place->location->city or $item->place->location->country) { $_REQUEST["location"] = ''; if ($item->place->name) { $_REQUEST["location"] .= $item->place->name; } if ($item->place->location->street) { $_REQUEST["location"] .= " " . $item->place->location->street; } if ($item->place->location->city) { $_REQUEST["location"] .= " " . $item->place->location->city; } if ($item->place->location->country) { $_REQUEST["location"] .= " " . $item->place->location->country; } $_REQUEST["location"] = trim($_REQUEST["location"]); } if ($item->place->location->latitude and $item->place->location->longitude) { $_REQUEST["coord"] = substr($item->place->location->latitude, 0, 8) . ' ' . substr($item->place->location->longitude, 0, 8); } } if (trim($_REQUEST["body"]) == '') { logger('facebook: empty body 3 ' . $item->id . ' ' . print_r($item, true)); continue; } if (trim(strip_tags(bbcode($_REQUEST["body"], false, false))) == '') { logger('facebook: empty body 4 ' . $item->id . ' ' . print_r($item, true)); continue; } //print_r($_REQUEST); logger('facebook: posting for user ' . $uid); item_post($a); } set_pconfig($uid, 'facebook', 'last_created', $lastcreated); }
function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $create_user) { $access_token = get_pconfig($uid, 'facebook', 'access_token'); require_once "include/oembed.php"; require_once "include/network.php"; require_once "include/items.php"; // check if it was already imported $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($uid), dbesc('fb::' . $post->post_id)); if (count($r)) { return; } $postarray = array(); $postarray['gravity'] = 0; $postarray['uid'] = $uid; $postarray['wall'] = 0; $postarray['verb'] = ACTIVITY_POST; $postarray['object-type'] = ACTIVITY_OBJ_NOTE; // default value - is maybe changed later when media is attached $postarray['network'] = dbesc(NETWORK_FACEBOOK); $postarray['uri'] = "fb::" . $post->post_id; $postarray['thr-parent'] = $postarray['uri']; $postarray['parent-uri'] = $postarray['uri']; $postarray['plink'] = $post->permalink; $postarray['author-name'] = $contacts[$post->actor_id]->name; $postarray['author-link'] = $contacts[$post->actor_id]->url; $postarray['author-avatar'] = $contacts[$post->actor_id]->pic_square; $postarray['owner-name'] = $contacts[$post->source_id]->name; $postarray['owner-link'] = $contacts[$post->source_id]->url; $postarray['owner-avatar'] = $contacts[$post->source_id]->pic_square; $contact_id = 0; if ($post->parent_post_id != "" and $post->actor_id == $post->source_id) { $pos = strpos($post->parent_post_id, "_"); if ($pos != 0) { $user_id = substr($post->parent_post_id, 0, $pos); $userdata = fbsync_fetchuser($a, $uid, $user_id); $contact_id = $userdata["contact-id"]; $postarray['contact-id'] = $contact_id; if (array_key_exists("name", $userdata) and $userdata["name"] != "" and !link_compare($userdata["link"], $postarray['author-link'])) { $postarray['owner-name'] = $userdata["name"]; $postarray['owner-link'] = $userdata["link"]; $postarray['owner-avatar'] = $userdata["avatar"]; if (!intval(get_config('system', 'wall-to-wall_share'))) { $prebody = "[share author='" . $postarray['author-name'] . "' profile='" . $postarray['author-link'] . "' avatar='" . $postarray['author-avatar'] . "']"; $postarray['author-name'] = $postarray['owner-name']; $postarray['author-link'] = $postarray['owner-link']; $postarray['author-avatar'] = $postarray['owner-avatar']; } } } } if ($contact_id <= 0) { if ($post->actor_id != $post->source_id) { // Testing if we know the source or the actor $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], false); if ($contact_id == 0 and array_key_exists($post->actor_id, $contacts)) { $contact_id = fbsync_fetch_contact($uid, $contacts[$post->actor_id], false); } // If we don't know anyone, we guess we should know the source. Could be the wrong decision if ($contact_id == 0) { $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); } } else { $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); } if ($contact_id == -1) { logger('fbsync_createpost: Contact is blocked. Post not imported ' . print_r($post, true), LOGGER_DEBUG); return; } elseif ($contact_id <= 0 and !$create_user) { logger('fbsync_createpost: No matching contact found. Post not imported ' . print_r($post, true), LOGGER_DEBUG); return; } elseif ($contact_id == 0) { // This case should never happen logger('fbsync_createpost: No matching contact found. Using own id. (Should never happen) ' . print_r($post, true), LOGGER_DEBUG); $contact_id = $self[0]["id"]; } $postarray['contact-id'] = $contact_id; } $postarray["body"] = isset($post->message) ? escape_tags($post->message) : ''; $msgdata = fbsync_convertmsg($a, $postarray["body"]); $postarray["body"] = $msgdata["body"]; $postarray["tag"] = $msgdata["tags"]; // Change the object type when an attachment is present if (isset($post->attachment->fb_object_type)) { logger('fb_object_type: ' . $post->attachment->fb_object_type . " " . print_r($post->attachment, true), LOGGER_DEBUG); } switch ($post->attachment->fb_object_type) { case 'photo': $postarray['object-type'] = ACTIVITY_OBJ_IMAGE; // photo is deprecated: http://activitystrea.ms/head/activity-schema.html#image break; case 'video': $postarray['object-type'] = ACTIVITY_OBJ_VIDEO; break; case '': //$postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK; break; default: logger('Unknown object type ' . $post->attachment->fb_object_type, LOGGER_DEBUG); break; } $pagedata = array(); $content = ""; $pagedata["type"] = ""; if (isset($post->attachment->name) and isset($post->attachment->href)) { $post->attachment->href = original_url($post->attachment->href); $oembed_data = oembed_fetch_url($post->attachment->href); $pagedata["type"] = $oembed_data->type; if ($pagedata["type"] == "rich") { $pagedata["type"] = "link"; } $pagedata["url"] = $post->attachment->href; $pagedata["title"] = $post->attachment->name; $content = "[bookmark=" . $post->attachment->href . "]" . $post->attachment->name . "[/bookmark]"; // If a link is not only attached but also added in the body, look if it can be removed in the body. $removedlink = trim(str_replace($post->attachment->href, "", $postarray["body"])); if ($removedlink == "" or strstr($postarray["body"], $removedlink)) { $postarray["body"] = $removedlink; } } elseif (isset($post->attachment->name) and $post->attachment->name != "") { $content = "[b]" . $post->attachment->name . "[/b]"; } $pagedata["text"] = ""; if (isset($post->attachment->description) and $post->attachment->fb_object_type != "photo") { $pagedata["text"] = $post->attachment->description; } if (isset($post->attachment->caption) and $post->attachment->fb_object_type == "photo") { $pagedata["text"] = $post->attachment->caption; } if ($pagedata["text"] . $post->attachment->href . $content . $postarray["body"] == "") { return; } if (isset($post->attachment->media) and ($pagedata["type"] == "" or $pagedata["type"] == "link")) { foreach ($post->attachment->media as $media) { if (isset($media->type)) { $pagedata["type"] = $media->type; } if (isset($media->src)) { $pagedata["images"][0]["src"] = $media->src; } if (isset($media->photo)) { if (isset($media->photo->images) and count($media->photo->images) > 1) { $pagedata["images"][0]["src"] = $media->photo->images[1]->src; } if (isset($media->photo->fbid)) { logger('fbsync_createpost: fetching fbid ' . $media->photo->fbid, LOGGER_DEBUG); $url = "https://graph.facebook.com/" . $media->photo->fbid . "?access_token=" . $access_token; $feed = fetch_url($url); $data = json_decode($feed); if (isset($data->images)) { $pagedata["images"][0]["src"] = $data->images[0]->source; logger('fbsync_createpost: got fbid ' . $media->photo->fbid . ' image ' . $pagedata["images"][0]["src"], LOGGER_DEBUG); } else { logger('fbsync_createpost: error fetching fbid ' . $media->photo->fbid . ' ' . print_r($data, true), LOGGER_DEBUG); } } } $pagedata["images"][0]["src"] = fbpost_cleanpicture($pagedata["images"][0]["src"]); if (isset($media->href) and $pagedata["images"][0]["src"] != "" and $media->href != "") { $media->href = original_url($media->href); $pagedata["url"] = $media->href; $content .= "\n" . '[url=' . $media->href . '][img]' . $pagedata["images"][0]["src"] . '[/img][/url]'; } else { if ($pagedata["images"][0]["src"] != "") { $content .= "\n" . '[img]' . $pagedata["images"][0]["src"] . '[/img]'; } // if just a link, it may be a wall photo - check if (isset($post->link)) { $content .= fbpost_get_photo($media->href); } } } } if ($pagedata["type"] != "") { if ($pagedata["type"] == "link") { $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK; } $postarray["body"] .= add_page_info_data($pagedata); } else { if ($content) { $postarray["body"] .= "\n" . trim($content); } if ($pagedata["text"]) { $postarray["body"] .= "\n[quote]" . trim($pagedata["text"]) . "[/quote]"; } $postarray["body"] = trim($postarray["body"]); } if (trim($postarray["body"]) == "") { return; } if ($prebody != "") { $postarray["body"] = $prebody . $postarray["body"] . "[/share]"; } $postarray['created'] = datetime_convert('UTC', 'UTC', date("c", $post->created_time)); $postarray['edited'] = datetime_convert('UTC', 'UTC', date("c", $post->updated_time)); $postarray['app'] = $applications[$post->app_id]->display_name; if ($postarray['app'] == "") { $postarray['app'] = "Facebook"; } if (isset($post->privacy) && $post->privacy->value !== '') { $postarray['private'] = 1; $postarray['allow_cid'] = '<' . $self[0]['id'] . '>'; } /* $postarray["location"] = $post->place->name; postarray["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1]; */ //$types = array(46, 80, 237, 247, 308); //if (!in_array($post->type, $types)) // $postarray["body"] = "Type: ".$post->type."\n".$postarray["body"]; //print_r($post); //print_r($postarray); $item = item_store($postarray); logger('fbsync_createpost: User ' . $self[0]["nick"] . ' posted feed item ' . $item, LOGGER_DEBUG); }