function parse_url_content(&$a) { $text = null; $str_tags = ''; $textmode = false; if (local_user() && !feature_enabled(local_user(), 'richtext')) { $textmode = true; } //if($textmode) $br = $textmode ? "\n" : '<br />'; if (x($_GET, 'binurl')) { $url = trim(hex2bin($_GET['binurl'])); } else { $url = trim($_GET['url']); } if ($_GET['title']) { $title = strip_tags(trim($_GET['title'])); } if ($_GET['description']) { $text = strip_tags(trim($_GET['description'])); } if ($_GET['tags']) { $arr_tags = str_getcsv($_GET['tags']); if (count($arr_tags)) { array_walk($arr_tags, 'arr_add_hashes'); $str_tags = $br . implode(' ', $arr_tags) . $br; } } // add url scheme if missing $arrurl = parse_url($url); if (!x($arrurl, 'scheme')) { if (x($arrurl, 'host')) { $url = "http:" . $url; } else { $url = "http://" . $url; } } logger('parse_url: ' . $url); if ($textmode) { $template = '[bookmark=%s]%s[/bookmark]%s'; } else { $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s"; } $arr = array('url' => $url, 'text' => ''); call_hooks('parse_link', $arr); if (strlen($arr['text'])) { echo $arr['text']; killme(); } if ($url && $title && $text) { $title = str_replace(array("\r", "\n"), array('', ''), $title); if ($textmode) { $text = '[quote]' . trim($text) . '[/quote]' . $br; } else { $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />'; $title = htmlspecialchars($title); } $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('parse_url (unparsed): returns: ' . $result); echo $result; killme(); } $siteinfo = parseurl_getsiteinfo($url); // if ($textmode) { // require_once("include/items.php"); // // echo add_page_info_data($siteinfo); // killme(); // } $url = $siteinfo["url"]; // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems if (strpos($url, '[') or strpos($url, ']')) { require_once "include/network.php"; $url = short_link($url); } $sitedata = ""; if ($siteinfo["title"] != "") { $text = $siteinfo["text"]; $title = $siteinfo["title"]; } $image = ""; if ($siteinfo["type"] != "video" and sizeof($siteinfo["images"]) > 0) { /* Execute below code only if image is present in siteinfo */ $total_images = 0; $max_images = get_config('system', 'max_bookmark_images'); if ($max_images === false) { $max_images = 2; } else { $max_images = intval($max_images); } foreach ($siteinfo["images"] as $imagedata) { if ($textmode) { $image .= '[img=' . $imagedata["width"] . 'x' . $imagedata["height"] . ']' . $imagedata["src"] . '[/img]' . "\n"; } else { $image .= '<img height="' . $imagedata["height"] . '" width="' . $imagedata["width"] . '" src="' . $imagedata["src"] . '" alt="photo" /><br />'; } $total_images++; if ($max_images && $max_images >= $total_images) { break; } } } if (strlen($text)) { if ($textmode) { $text = '[quote]' . trim($text) . '[/quote]'; } else { $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote>'; } } if ($image) { $text = $br . $br . $image . $text; } else { $text = $br . $text; } $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('parse_url: returns: ' . $result); $sitedata .= trim($result); if ($siteinfo["type"] == "video" and $url != "") { echo "[class=type-video]" . $sitedata . "[/class]"; } elseif ($siteinfo["type"] != "photo") { echo "[class=type-link]" . $sitedata . "[/class]"; } else { echo "[class=type-photo]" . $title . $br . $image . "[/class]"; } killme(); }
function query_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") { require_once "mod/parse_url.php"; $data = Cache::get("parse_url:" . $url); if (is_null($data)) { $data = parseurl_getsiteinfo($url, true); Cache::set("parse_url:" . $url, serialize($data), CACHE_DAY); } else { $data = unserialize($data); } if ($photo != "") { $data["images"][0]["src"] = $photo; } logger('fetch page info for ' . $url . ' ' . print_r($data, true), LOGGER_DEBUG); if (!$keywords and isset($data["keywords"])) { unset($data["keywords"]); } if ($keyword_blacklist != "" and isset($data["keywords"])) { $list = explode(",", $keyword_blacklist); foreach ($list as $keyword) { $keyword = trim($keyword); $index = array_search($keyword, $data["keywords"]); if ($index !== false) { unset($data["keywords"][$index]); } } } return $data; }
function oembed_fetch_url($embedurl, $no_rich_type = false) { $embedurl = trim($embedurl, "'"); $embedurl = trim($embedurl, '"'); $a = get_app(); $txt = Cache::get($a->videowidth . $embedurl); // These media files should now be caught in bbcode.php // left here as a fallback in case this is called from another source $noexts = array("mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm"); $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION); if (is_null($txt)) { $txt = ""; if (!in_array($ext, $noexts)) { // try oembed autodiscovery $redirects = 0; $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*"); /**/ if ($html_text) { $dom = @DOMDocument::loadHTML($html_text); if ($dom) { $xpath = new DOMXPath($dom); $attr = "oembed"; $xattr = oe_build_xpath("class", "oembed"); $entries = $xpath->query("//link[@type='application/json+oembed']"); foreach ($entries as $e) { $href = $e->getAttributeNode("href")->nodeValue; $txt = fetch_url($href . '&maxwidth=' . $a->videowidth); break; } $entries = $xpath->query("//link[@type='text/json+oembed']"); foreach ($entries as $e) { $href = $e->getAttributeNode("href")->nodeValue; $txt = fetch_url($href . '&maxwidth=' . $a->videowidth); break; } } } } if ($txt == false || $txt == "") { $embedly = get_config("system", "embedly"); if ($embedly != "") { // try embedly service $ourl = "https://api.embed.ly/1/oembed?key=" . $embedly . "&url=" . urlencode($embedurl); $txt = fetch_url($ourl); logger("oembed_fetch_url: " . $txt, LOGGER_DEBUG); } } $txt = trim($txt); if ($txt[0] != "{") { $txt = '{"type":"error"}'; } else { //save in cache Cache::set($a->videowidth . $embedurl, $txt, CACHE_DAY); } } $j = json_decode($txt); if (!is_object($j)) { return false; } // Always embed the SSL version if (isset($j->html)) { $j->html = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"), array("https://www.youtube.com/", "https://player.vimeo.com/"), $j->html); } $j->embedurl = $embedurl; // If fetching information doesn't work, then improve via internal functions if ($j->type == "error" or $no_rich_type and $j->type == "rich") { require_once "mod/parse_url.php"; $data = parseurl_getsiteinfo($embedurl, true, false); $j->type = $data["type"]; if ($j->type == "photo") { $j->url = $data["url"]; //$j->width = $data["images"][0]["width"]; //$j->height = $data["images"][0]["height"]; } if (isset($data["title"])) { $j->title = $data["title"]; } if (isset($data["text"])) { $j->description = $data["text"]; } if (is_array($data["images"])) { $j->thumbnail_url = $data["images"][0]["src"]; $j->thumbnail_width = $data["images"][0]["width"]; $j->thumbnail_height = $data["images"][0]["height"]; } } call_hooks('oembed_fetch_url', $embedurl, $j); return $j; }
function linkinfo_content(&$a) { logger('linkinfo: ' . print_r($_REQUEST, true)); $text = null; $str_tags = ''; $br = "\n"; if (x($_GET, 'binurl')) { $url = trim(hex2bin($_GET['binurl'])); } else { $url = trim($_GET['url']); } $url = strip_zids($url); if (substr($url, 0, 1) != '/' && substr($url, 0, 4) != 'http') { $url = 'http://' . $url; } if ($_GET['title']) { $title = strip_tags(trim($_GET['title'])); } if ($_GET['description']) { $text = strip_tags(trim($_GET['description'])); } if ($_GET['tags']) { $arr_tags = str_getcsv($_GET['tags']); if (count($arr_tags)) { array_walk($arr_tags, 'arr_add_hashes'); $str_tags = $br . implode(' ', $arr_tags) . $br; } } logger('linkinfo: ' . $url); $result = z_fetch_url($url, false, 0, array('novalidate' => true, 'nobody' => true)); if ($result['success']) { $hdrs = array(); $h = explode("\n", $result['header']); foreach ($h as $l) { list($k, $v) = array_map("trim", explode(":", trim($l), 2)); $hdrs[$k] = $v; } if (array_key_exists('Content-Type', $hdrs)) { $type = $hdrs['Content-Type']; } if ($type) { $zrl = is_matrix_url($url); if (stripos($type, 'image/') !== false) { if ($zrl) { echo $br . '[zmg]' . $url . '[/zmg]' . $br; } else { echo $br . '[img]' . $url . '[/img]' . $br; } killme(); } if (stripos($type, 'video/') !== false) { if ($zrl) { echo $br . '[zvideo]' . $url . '[/zvideo]' . $br; } else { echo $br . '[video]' . $url . '[/video]' . $br; } killme(); } if (stripos($type, 'audio/') !== false) { if ($zrl) { echo $br . '[zaudio]' . $url . '[/zaudio]' . $br; } else { echo $br . '[audio]' . $url . '[/audio]' . $br; } killme(); } } } $template = $br . '#^[url=%s]%s[/url]%s' . $br; $arr = array('url' => $url, 'text' => ''); call_hooks('parse_link', $arr); if (strlen($arr['text'])) { echo $arr['text']; killme(); } $x = oembed_process($url); if ($x) { echo $x; killme(); } if ($url && $title && $text) { $text = $br . '[quote]' . trim($text) . '[/quote]' . $br; $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('linkinfo (unparsed): returns: ' . $result); echo $result; killme(); } $siteinfo = parseurl_getsiteinfo($url); // If this is a Red site, use zrl rather than url so they get zids sent to them by default if (x($siteinfo, 'generator') && strpos($siteinfo['generator'], Zotlabs\Project\System::get_platform_name() . ' ') === 0) { $template = str_replace('url', 'zrl', $template); } if ($siteinfo["title"] == "") { echo sprintf($template, $url, $url, '') . $str_tags; killme(); } else { $text = $siteinfo["text"]; $title = $siteinfo["title"]; } $image = ""; if (sizeof($siteinfo["images"]) > 0) { /* Execute below code only if image is present in siteinfo */ $total_images = 0; $max_images = get_config('system', 'max_bookmark_images'); if ($max_images === false) { $max_images = 2; } else { $max_images = intval($max_images); } foreach ($siteinfo["images"] as $imagedata) { if ($url) { $image .= sprintf('[url=%s]', $url); } $image .= '[img=' . $imagedata["width"] . 'x' . $imagedata["height"] . ']' . $imagedata["src"] . '[/img]'; if ($url) { $image .= '[/url]'; } $image .= "\n"; $total_images++; if ($max_images && $max_images >= $total_images) { break; } } } if (strlen($text)) { $text = $br . '[quote]' . trim($text) . '[/quote]' . $br; } if ($image) { $text = $br . $br . $image . $text; } $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('linkinfo: returns: ' . $result, LOGGER_DEBUG); echo trim($result); killme(); }
function parse_url_content(&$a) { $text = null; $str_tags = ''; $br = "\n"; if (x($_GET, 'binurl')) { $url = trim(hex2bin($_GET['binurl'])); } else { $url = trim($_GET['url']); } if (substr($url, 0, 1) != '/' && substr($url, 0, 4) != 'http') { $url = 'http://' . $url; } if ($_GET['title']) { $title = strip_tags(trim($_GET['title'])); } if ($_GET['description']) { $text = strip_tags(trim($_GET['description'])); } if ($_GET['tags']) { $arr_tags = str_getcsv($_GET['tags']); if (count($arr_tags)) { array_walk($arr_tags, 'arr_add_hashes'); $str_tags = $br . implode(' ', $arr_tags) . $br; } } logger('parse_url: ' . $url); $template = $br . '#^[url=%s]%s[/url]%s' . $br; $arr = array('url' => $url, 'text' => ''); call_hooks('parse_link', $arr); if (strlen($arr['text'])) { echo $arr['text']; killme(); } if ($url && $title && $text) { $text = $br . '[quote]' . trim($text) . '[/quote]' . $br; $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('parse_url (unparsed): returns: ' . $result); echo $result; killme(); } $siteinfo = parseurl_getsiteinfo($url); // If this is a Red site, use zrl rather than url so they get zids sent to them by default if (x($siteinfo, 'generator') && strpos($siteinfo['generator'], RED_PLATFORM . ' ') === 0) { $template = str_replace('url', 'zrl', $template); } if ($siteinfo["title"] == "") { echo sprintf($template, $url, $url, '') . $str_tags; killme(); } else { $text = $siteinfo["text"]; $title = $siteinfo["title"]; } $image = ""; if (sizeof($siteinfo["images"]) > 0) { /* Execute below code only if image is present in siteinfo */ $total_images = 0; $max_images = get_config('system', 'max_bookmark_images'); if ($max_images === false) { $max_images = 2; } else { $max_images = intval($max_images); } foreach ($siteinfo["images"] as $imagedata) { $image .= '[img=' . $imagedata["width"] . 'x' . $imagedata["height"] . ']' . $imagedata["src"] . '[/img]' . "\n"; $total_images++; if ($max_images && $max_images >= $total_images) { break; } } } if (strlen($text)) { $text = $br . '[quote]' . trim($text) . '[/quote]' . $br; } if ($image) { $text = $br . $br . $image . $text; } $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('parse_url: returns: ' . $result, LOGGER_DEBUG); echo trim($result); killme(); }
function get_attached_data($body) { /* - text: - type: link, video, photo - title: - url: - image: - description: - (thumbnail) */ // Simplify image codes $body = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '[img]$3[/img]', $body); $post = array(); if (preg_match_all("(\\[class=(.*?)\\](.*?)\\[\\/class\\])ism", $body, $attached, PREG_SET_ORDER)) { foreach ($attached as $data) { if (!in_array($data[1], array("type-link", "type-video", "type-photo"))) { continue; } $post["type"] = substr($data[1], 5); $post["text"] = trim(str_replace($data[0], "", $body)); $attacheddata = $data[2]; $URLSearchString = "^\\[\\]"; if (preg_match("/\\[img\\]([{$URLSearchString}]*)\\[\\/img\\]/ism", $attacheddata, $matches)) { $post["image"] = $matches[1]; } if (preg_match("/\\[bookmark\\=([{$URLSearchString}]*)\\](.*?)\\[\\/bookmark\\]/ism", $attacheddata, $matches)) { $post["url"] = $matches[1]; $post["title"] = $matches[2]; } // Search for description if (preg_match("/\\[quote\\](.*?)\\[\\/quote\\]/ism", $attacheddata, $matches)) { $post["description"] = $matches[1]; } } } // if nothing is found, it maybe having an image. if (!isset($post["type"])) { require_once "mod/parse_url.php"; require_once "include/Photo.php"; $URLSearchString = "^\\[\\]"; if (preg_match_all("(\\[url=([{$URLSearchString}]*)\\]\\s*\\[img\\]([{$URLSearchString}]*)\\[\\/img\\]\\s*\\[\\/url\\])ism", $body, $pictures, PREG_SET_ORDER)) { if (count($pictures) == 1) { // Checking, if the link goes to a picture $data = parseurl_getsiteinfo($pictures[0][1], true); if ($data["type"] == "photo") { $post["type"] = "photo"; if (isset($data["images"][0])) { $post["image"] = $data["images"][0]["src"]; } else { $post["image"] = $data["url"]; } $post["preview"] = $pictures[0][2]; $post["text"] = str_replace($pictures[0][0], "", $body); } else { $imgdata = get_photo_info($pictures[0][1]); if (substr($imgdata["mime"], 0, 6) == "image/") { $post["type"] = "photo"; $post["image"] = $pictures[0][1]; $post["preview"] = $pictures[0][2]; $post["text"] = str_replace($pictures[0][0], "", $body); } } } elseif (count($pictures) > 1) { $post["type"] = "link"; $post["url"] = $b["plink"]; $post["image"] = $pictures[0][2]; $post["text"] = $body; } } elseif (preg_match_all("(\\[img\\]([{$URLSearchString}]*)\\[\\/img\\])ism", $body, $pictures, PREG_SET_ORDER)) { if (count($pictures) == 1) { $post["type"] = "photo"; $post["image"] = $pictures[0][1]; $post["text"] = str_replace($pictures[0][0], "", $body); } elseif (count($pictures) > 1) { $post["type"] = "link"; $post["url"] = $b["plink"]; $post["image"] = $pictures[0][1]; $post["text"] = $body; } } if (!isset($post["type"])) { $post["type"] = "text"; $post["text"] = trim($body); } } elseif (isset($post["url"]) and $post["type"] == "video") { require_once "mod/parse_url.php"; $data = parseurl_getsiteinfo($post["url"], true); if (isset($data["images"][0])) { $post["image"] = $data["images"][0]["src"]; } } return $post; }
function add_page_info($url, $no_photos = false, $photo = "") { require_once "mod/parse_url.php"; $data = parseurl_getsiteinfo($url, true); logger('add_page_info: fetch page info for ' . $url . ' ' . print_r($data, true), LOGGER_DEBUG); // It maybe is a rich content, but if it does have everything that a link has, // then treat it that way if ($data["type"] == "rich" and is_string($data["title"]) and is_string($data["text"]) and sizeof($data["images"]) > 0) { $data["type"] = "link"; } if ($data["type"] != "link" and $data["type"] != "video" and $data["type"] != "photo" or $data["title"] == $url) { return ""; } if ($no_photos and $data["type"] == "photo") { return ""; } if ($data["type"] != "photo" and is_string($data["title"])) { $text .= "[bookmark=" . $url . "]" . trim($data["title"]) . "[/bookmark]"; } if ($data["type"] != "video" and $photo != "") { $text .= '[img]' . $photo . '[/img]'; } elseif ($data["type"] != "video" and sizeof($data["images"]) > 0) { $imagedata = $data["images"][0]; $text .= '[img]' . $imagedata["src"] . '[/img]'; } if ($data["type"] != "photo" and is_string($data["text"])) { $text .= "[quote]" . $data["text"] . "[/quote]"; } return "\n[class=type-" . $data["type"] . "]" . $text . "[/class]"; }
function parse_url_content(&$a) { $text = null; $str_tags = ''; $textmode = false; if (local_user() && !feature_enabled(local_user(), 'richtext')) { $textmode = true; } //if($textmode) $br = $textmode ? "\n" : '<br />'; if (x($_GET, 'binurl')) { $url = trim(hex2bin($_GET['binurl'])); } else { $url = trim($_GET['url']); } if ($_GET['title']) { $title = strip_tags(trim($_GET['title'])); } if ($_GET['description']) { $text = strip_tags(trim($_GET['description'])); } if ($_GET['tags']) { $arr_tags = str_getcsv($_GET['tags']); if (count($arr_tags)) { array_walk($arr_tags, 'arr_add_hashes'); $str_tags = $br . implode(' ', $arr_tags) . $br; } } logger('parse_url: ' . $url); if ($textmode) { $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br; } else { $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />"; } $arr = array('url' => $url, 'text' => ''); call_hooks('parse_link', $arr); if (strlen($arr['text'])) { echo $arr['text']; killme(); } if ($url && $title && $text) { if ($textmode) { $text = $br . '[quote]' . trim($text) . '[/quote]' . $br; } else { $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />'; } $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('parse_url (unparsed): returns: ' . $result); echo $result; killme(); } $siteinfo = parseurl_getsiteinfo($url); if ($siteinfo["title"] == "") { echo sprintf($template, $url, $url, '') . $str_tags; killme(); } else { $text = $siteinfo["text"]; $title = $siteinfo["title"]; } $image = ""; if (sizeof($siteinfo["images"]) > 0) { /* Execute below code only if image is present in siteinfo */ $total_images = 0; $max_images = get_config('system', 'max_bookmark_images'); if ($max_images === false) { $max_images = 2; } else { $max_images = intval($max_images); } foreach ($siteinfo["images"] as $imagedata) { if ($textmode) { $image .= '[img=' . $imagedata["width"] . 'x' . $imagedata["height"] . ']' . $imagedata["src"] . '[/img]' . "\n"; } else { $image .= '<img height="' . $imagedata["height"] . '" width="' . $imagedata["width"] . '" src="' . $imagedata["src"] . '" alt="photo" /><br />'; } $total_images++; if ($max_images && $max_images >= $total_images) { break; } } } if (strlen($text)) { if ($textmode) { $text = $br . '[quote]' . trim($text) . '[/quote]' . $br; } else { $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />'; } } if ($image) { $text = $br . $br . $image . $text; } $title = str_replace(array("\r", "\n"), array('', ''), $title); $result = sprintf($template, $url, $title ? $title : $url, $text) . $str_tags; logger('parse_url: returns: ' . $result); echo trim($result); killme(); }