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"; } }
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, $picture) { require_once "include/oembed.php"; require_once "include/network.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 = 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; $type = $oembed_data->type; $footerurl = $expanded_url; $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]"; $body = str_replace($url->url, $footerlink, $body); //} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) { } elseif ($oembed_data->type == "photo" and isset($oembed_data->url)) { $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_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]"; $body = str_replace($url->url, $footerlink, $body); } } } } if ($footerurl != "") { $footer = add_page_info($footerurl, false, $picture); } if ($footerlink != "" and trim($footer) != "") { $removedlink = trim(str_replace($footerlink, "", $body)); if ($removedlink == "" or strstr($body, $removedlink)) { $body = $removedlink; } $body .= $footer; } if ($footer == "" and $picture != "") { $body .= "\n\n[img]" . $picture . "[/img]\n"; } elseif ($footer == "" and $picture == "") { $body = add_page_info_to_body($body); } 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 fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) { require_once "include/Photo.php"; require_once "include/items.php"; require_once "include/network.php"; $post = ""; $quote = ""; $pagedata = array(); $pagedata["type"] = ""; foreach ($item->object->attachments as $attachment) { switch ($attachment->objectType) { case "video": $pagedata["type"] = "video"; $pagedata["url"] = original_url($attachment->url); $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName); break; case "article": $pagedata["type"] = "link"; $pagedata["url"] = original_url($attachment->url); $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName); $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image); if ($images["full"] != "") { $pagedata["images"][0]["src"] = $images["full"]; } $quote = trim(fromgplus_html2bbcode($attachment->content)); if ($quote != "") { $pagedata["text"] = $quote; } break; case "photo": // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses) if ($shared) { $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image); } else { if ($attachment->fullImage->url != "") { $images = store_photo($a, $uid, "", $attachment->fullImage->url); } elseif ($attachment->image->url != "") { $images = store_photo($a, $uid, "", $attachment->image->url); } } if ($images["preview"] != "") { $post .= "\n[url=" . $images["page"] . "][img]" . $images["preview"] . "[/img][/url]\n"; $pagedata["images"][0]["src"] = $images["preview"]; $pagedata["url"] = $images["page"]; } elseif ($images["full"] != "") { $post .= "\n[img]" . $images["full"] . "[/img]\n"; $pagedata["images"][0]["src"] = $images["full"]; if ($images["preview"] != "") { $pagedata["images"][1]["src"] = $images["preview"]; } } if ($attachment->displayName != "" and fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext)) { $post .= fromgplus_html2bbcode($attachment->displayName) . "\n"; $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName); } break; case "photo-album": $pagedata["url"] = original_url($attachment->url); $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName); $post .= "\n\n[bookmark=" . $pagedata["url"] . "]" . $pagedata["title"] . "[/bookmark]\n"; $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image); if ($images["preview"] != "") { $post .= "\n[url=" . $images["full"] . "][img]" . $images["preview"] . "[/img][/url]\n"; $pagedata["images"][0]["src"] = $images["preview"]; $pagedata["url"] = $images["full"]; } elseif ($images["full"] != "") { $post .= "\n[img]" . $images["full"] . "[/img]\n"; $pagedata["images"][0]["src"] = $images["full"]; if ($images["preview"] != "") { $pagedata["images"][1]["src"] = $images["preview"]; } } break; case "album": $pagedata["type"] = "link"; $pagedata["url"] = original_url($attachment->url); $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName); $thumb = $attachment->thumbnails[0]; $pagedata["images"][0]["src"] = $thumb->image->url; $quote = trim(fromgplus_html2bbcode($thumb->description)); if ($quote != "") { $pagedata["text"] = $quote; } break; case "audio": $pagedata["url"] = original_url($attachment->url); $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName); $post .= "\n\n[bookmark=" . $pagedata["url"] . "]" . $pagedata["title"] . "[/bookmark]\n"; break; //default: // die($attachment->objectType); } } if ($pagedata["type"] != "") { return add_page_info_data($pagedata); } return $post . $quote; }
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 original_url($url, $depth = 1, $fetchbody = false) { $a = get_app(); // Remove Analytics Data from Google and other tracking platforms $urldata = parse_url($url); if (is_string($urldata["query"])) { $query = $urldata["query"]; parse_str($query, $querydata); if (is_array($querydata)) { foreach ($querydata as $param => $value) { if (in_array($param, array("utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign", "wt_mc", "pk_campaign", "pk_kwd", "mc_cid", "mc_eid", "fb_action_ids", "fb_action_types", "fb_ref", "awesm", "wtrid", "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"))) { $pair = $param . "=" . urlencode($value); $url = str_replace($pair, "", $url); // Second try: if the url isn't encoded completely $pair = $param . "=" . str_replace(" ", "+", $value); $url = str_replace($pair, "", $url); // Third try: Maybey the url isn't encoded at all $pair = $param . "=" . $value; $url = str_replace($pair, "", $url); $url = str_replace(array("?&", "&&"), array("?", ""), $url); } } } if (substr($url, -1, 1) == "?") { $url = substr($url, 0, -1); } } if ($depth > 10) { return $url; } $url = trim($url, "'"); $stamp1 = microtime(true); $siteinfo = array(); $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, 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"); if (($curl_info['http_code'] == "301" or $curl_info['http_code'] == "302") and ($curl_info['redirect_url'] != "" or $curl_info['location'] != "")) { if ($curl_info['redirect_url'] != "") { return original_url($curl_info['redirect_url'], ++$depth, $fetchbody); } else { return original_url($curl_info['location'], ++$depth, $fetchbody); } } // Check for redirects in the meta elements of the body if there are no redirects in the header. if (!$fetchbody) { return original_url($url, ++$depth, true); } // if the file is too large then exit if ($curl_info["download_content_length"] > 1000000) { return $url; } // if it isn't a HTML file then exit if ($curl_info["content_type"] != "" and !strstr(strtolower($curl_info["content_type"]), "html")) { return $url; } $stamp1 = microtime(true); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); 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()); $body = curl_exec($ch); curl_close($ch); $a->save_timestamp($stamp1, "network"); if (trim($body) == "") { return $url; } // Check for redirect in meta elements $doc = new DOMDocument(); @$doc->loadHTML($body); $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=") { return original_url(substr($value, 4), ++$depth); } } } } return $url; }
function feed_import($xml, $importer, &$contact, &$hub) { $a = get_app(); logger("Import Atom/RSS feed", LOGGER_DEBUG); if ($xml == "") { return; } $doc = new DOMDocument(); @$doc->loadXML($xml); $xpath = new DomXPath($doc); $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom"); $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/"); $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/"); $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/"); $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/"); $author = array(); // Is it RDF? if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) { //$author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue; $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue; if ($author["author-name"] == "") { $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue; } $entries = $xpath->query('/rdf:RDF/rss:item'); } // Is it Atom? if ($xpath->query('/atom:feed/atom:entry')->length > 0) { //$self = $xpath->query("/atom:feed/atom:link[@rel='self']")->item(0)->attributes; //if (is_object($self)) // foreach($self AS $attributes) // if ($attributes->name == "href") // $author["author-link"] = $attributes->textContent; //if ($author["author-link"] == "") { // $alternate = $xpath->query("/atom:feed/atom:link[@rel='alternate']")->item(0)->attributes; // if (is_object($alternate)) // foreach($alternate AS $attributes) // if ($attributes->name == "href") // $author["author-link"] = $attributes->textContent; //} $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue; if ($author["author-name"] == "") { $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue; } if ($author["author-name"] == "") { $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue; } //$author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue; $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue; $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue; $entries = $xpath->query('/atom:feed/atom:entry'); } // Is it RSS? if ($xpath->query('/rss/channel')->length > 0) { //$author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue; $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue; //$author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue; if ($author["author-name"] == "") { $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue; } if ($author["author-name"] == "") { $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue; } $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue; $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue; $entries = $xpath->query('/rss/channel/item'); } //if ($author["author-link"] == "") $author["author-link"] = $contact["url"]; if ($author["author-name"] == "") { $author["author-name"] = $contact["name"]; } //if ($author["author-avatar"] == "") $author["author-avatar"] = $contact["thumb"]; $author["owner-link"] = $contact["url"]; $author["owner-name"] = $contact["name"]; $author["owner-avatar"] = $contact["thumb"]; $header = array(); $header["uid"] = $importer["uid"]; $header["network"] = NETWORK_FEED; $header["type"] = "remote"; $header["wall"] = 0; $header["origin"] = 0; $header["gravity"] = GRAVITY_PARENT; $header["private"] = 2; $header["verb"] = ACTIVITY_POST; $header["object-type"] = ACTIVITY_OBJ_NOTE; $header["contact-id"] = $contact["id"]; if (!strlen($contact["notify"])) { // one way feed - no remote comment ability $header["last-child"] = 0; } if (!is_object($entries)) { return; } $entrylist = array(); foreach ($entries as $entry) { $entrylist[] = $entry; } foreach (array_reverse($entrylist) as $entry) { $item = array_merge($header, $author); $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue; if ($item["title"] == "") { $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue; } if ($item["title"] == "") { $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue; } $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes; if (!is_object($alternate)) { $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes; } if (is_object($alternate)) { foreach ($alternate as $attributes) { if ($attributes->name == "href") { $item["plink"] = $attributes->textContent; } } } if ($item["plink"] == "") { $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue; } if ($item["plink"] == "") { $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue; } $item["plink"] = original_url($item["plink"]); $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue; if ($item["uri"] == "") { $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue; } if ($item["uri"] == "") { $item["uri"] = $item["plink"]; } $item["parent-uri"] = $item["uri"]; $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue; if ($published == "") { $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue; } if ($published == "") { $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue; } $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue; if ($updated == "") { $updated = $published; } if ($published != "") { $item["created"] = $published; } if ($updated != "") { $item["edited"] = $updated; } $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue; if ($creator == "") { $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue; } if ($creator == "") { $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue; } if ($creator != "") { $item["author-name"] = $creator; } if ($pubDate != "") { $item["edited"] = $item["created"] = $pubDate; } $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue; if ($creator != "") { $item["author-name"] = $creator; } //$item["object"] = $xml; $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')", intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN)); if ($r) { logger("Item with uri " . $item["uri"] . " for user " . $importer["uid"] . " already existed under id " . $r[0]["id"], LOGGER_DEBUG); continue; } // To-Do? // <category>Ausland</category> // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/> $attachments = array(); $enclosures = $xpath->query("enclosure", $entry); foreach ($enclosures as $enclosure) { $href = ""; $length = ""; $type = ""; $title = ""; foreach ($enclosure->attributes as $attributes) { if ($attributes->name == "url") { $href = $attributes->textContent; } elseif ($attributes->name == "length") { $length = $attributes->textContent; } elseif ($attributes->name == "type") { $type = $attributes->textContent; } } if (strlen($item["attach"])) { $item["attach"] .= ','; } $attachments[] = array("link" => $href, "type" => $type, "length" => $length); $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '"[/attach]'; } if ($contact["fetch_further_information"]) { $preview = ""; // Handle enclosures and treat them as preview picture foreach ($attachments as $attachment) { if ($attachment["type"] == "image/jpeg") { $preview = $attachment["link"]; } } $item["body"] = $item["title"] . add_page_info($item["plink"], false, $preview, $contact["fetch_further_information"] == 2, $contact["ffi_keyword_blacklist"]); $item["tag"] = add_page_keywords($item["plink"], false, $preview, $contact["fetch_further_information"] == 2, $contact["ffi_keyword_blacklist"]); $item["title"] = ""; $item["object-type"] = ACTIVITY_OBJ_BOOKMARK; unset($item["attach"]); } else { $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue); if ($body == "") { $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue); } if ($body == "") { $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue); } if ($body == "") { $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue); } // remove the content of the title if it is identically to the body // This helps with auto generated titles e.g. from tumblr if (title_is_body($item["title"], $body)) { $item["title"] = ""; } $item["body"] = html2bbcode($body); } logger("Stored feed: " . print_r($item, true), LOGGER_DEBUG); $notify = item_is_remote_self($contact, $item); $id = item_store($item, false, $notify); //print_r($item); logger("Feed for contact " . $contact["url"] . " stored under id " . $id); } }
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); }