function diaspora2bb($s) { $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8'); // Remove CR to avoid problems with following code $s = str_replace("\r", "", $s); $s = str_replace("\n", " \n", $s); // The parser cannot handle paragraphs correctly $s = str_replace(array("</p>", "<p>", '<p dir="ltr">'), array("<br>", "<br>", "<br>"), $s); // Escaping the hash tags $s = preg_replace('/\\#([^\\s\\#])/', '#$1', $s); $s = Markdown($s); $s = preg_replace('/\\@\\{(.+?)\\; (.+?)\\@(.+?)\\}/', '@[url=https://$3/u/$2]$1[/url]', $s); $s = str_replace('#', '#', $s); $s = html2bbcode($s); // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands $s = str_replace('♲', html_entity_decode('♲', ENT_QUOTES, 'UTF-8'), $s); // Convert everything that looks like a link to a link $s = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/)([a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1[url=$2$3]$2$3[/url]', $s); //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s); $s = bb_tag_preg_replace("/\\[url\\=?(.*?)\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/url\\]/ism", '[youtube]$2[/youtube]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\].*?\\[\\/url\\]/ism", '[youtube]$1[/youtube]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=?(.*?)\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/url\\]/ism", '[vimeo]$2[/vimeo]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=https?:\\/\\/vimeo.com\\/([0-9]+)\\](.*?)\\[\\/url\\]/ism", '[vimeo]$1[/vimeo]', 'url', $s); // remove duplicate adjacent code tags $s = preg_replace("/(\\[code\\])+(.*?)(\\[\\/code\\])+/ism", "[code]\$2[/code]", $s); // Don't show link to full picture (until it is fixed) $s = scale_external_images($s, false); return $s; }
function diaspora2bb($s) { // for testing purposes: Collect raw markdown articles // $file = tempnam("/tmp/friendica/", "markdown"); // file_put_contents($file, $s); $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8'); // Too many new lines. So deactivated the following line // $s = str_replace("\r","\n",$s); // Simply remove cr. $s = str_replace("\r", "", $s); // <br/> is invalid. Replace it with the valid expression $s = str_replace("<br/>", "<br />", $s); $s = preg_replace('/\\@\\{(.+?)\\; (.+?)\\@(.+?)\\}/', '@[url=https://$3/u/$2]$1[/url]', $s); // Escaping the hash tags - doesn't always seem to work // $s = preg_replace('/\#([^\s\#])/','\\#$1',$s); // This seems to work $s = preg_replace('/\\#([^\\s\\#])/', '#$1', $s); $s = Markdown($s); $s = str_replace('#', '#', $s); // we seem to have double linebreaks // $s = str_replace("\n",'<br />',$s); $s = html2bbcode($s); // $s = str_replace('*','*',$s); // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands $s = str_replace('♲', html_entity_decode('♲', ENT_QUOTES, 'UTF-8'), $s); // Convert everything that looks like a link to a link $s = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/)([a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1[url=$2$3]$2$3[/url]', $s); //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s); $s = bb_tag_preg_replace("/\\[url\\=?(.*?)\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/url\\]/ism", '[youtube]$2[/youtube]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\].*?\\[\\/url\\]/ism", '[youtube]$1[/youtube]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=?(.*?)\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/url\\]/ism", '[vimeo]$2[/vimeo]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=https?:\\/\\/vimeo.com\\/([0-9]+)\\](.*?)\\[\\/url\\]/ism", '[vimeo]$1[/vimeo]', 'url', $s); // remove duplicate adjacent code tags $s = preg_replace("/(\\[code\\])+(.*?)(\\[\\/code\\])+/ism", "[code]\$2[/code]", $s); // Don't show link to full picture (until it is fixed) $s = scale_external_images($s, false); return $s; }
/** * Render actions localized */ function localize_item(&$item) { $extracted = item_extract_images($item['body']); if ($extracted['images']) { $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']); } $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; if (activity_match($item['verb'], ACTIVITY_LIKE) || activity_match($item['verb'], ACTIVITY_DISLIKE)) { $r = q("SELECT * from `item`,`contact` WHERE\n\t\t\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri'])); if (count($r) == 0) { return; } $obj = $r[0]; $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]'; $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]'; switch ($obj['verb']) { case ACTIVITY_POST: switch ($obj['object-type']) { case ACTIVITY_OBJ_EVENT: $post_type = t('event'); break; default: $post_type = t('status'); } break; default: if ($obj['resource-id']) { $post_type = t('photo'); $m = array(); preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m); $rr['plink'] = $m[1]; } else { $post_type = t('status'); } } $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]'; if (activity_match($item['verb'], ACTIVITY_LIKE)) { $bodyverb = t('%1$s likes %2$s\'s %3$s'); } elseif (activity_match($item['verb'], ACTIVITY_DISLIKE)) { $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s'); } $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink); } if (activity_match($item['verb'], ACTIVITY_FRIEND)) { if ($item['object-type'] == "" || $item['object-type'] !== ACTIVITY_OBJ_PERSON) { return; } $Aname = $item['author-name']; $Alink = $item['author-link']; $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; $obj = parse_xml_string($xmlhead . $item['object']); $links = parse_xml_string($xmlhead . "<links>" . unxmlify($obj->link) . "</links>"); $Bname = $obj->title; $Blink = ""; $Bphoto = ""; foreach ($links->link as $l) { $atts = $l->attributes(); switch ($atts['rel']) { case "alternate": $Blink = $atts['href']; case "photo": $Bphoto = $atts['href']; } } $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]'; $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]'; if ($Bphoto != "") { $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]'; } $item['body'] = sprintf(t('%1$s is now friends with %2$s'), $A, $B) . "\n\n\n" . $Bphoto; } if (stristr($item['verb'], ACTIVITY_POKE)) { $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1)); if (!$verb) { return; } if ($item['object-type'] == "" || $item['object-type'] !== ACTIVITY_OBJ_PERSON) { return; } $Aname = $item['author-name']; $Alink = $item['author-link']; $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; $obj = parse_xml_string($xmlhead . $item['object']); $links = parse_xml_string($xmlhead . "<links>" . unxmlify($obj->link) . "</links>"); $Bname = $obj->title; $Blink = ""; $Bphoto = ""; foreach ($links->link as $l) { $atts = $l->attributes(); switch ($atts['rel']) { case "alternate": $Blink = $atts['href']; case "photo": $Bphoto = $atts['href']; } } $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]'; $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]'; if ($Bphoto != "") { $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]'; } // we can't have a translation string with three positions but no distinguishable text // So here is the translate string. $txt = t('%1$s poked %2$s'); // now translate the verb $txt = str_replace(t('poked'), t($verb), $txt); // then do the sprintf on the translation string $item['body'] = sprintf($txt, $A, $B) . "\n\n\n" . $Bphoto; } if (stristr($item['verb'], ACTIVITY_MOOD)) { $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1)); if (!$verb) { return; } $Aname = $item['author-name']; $Alink = $item['author-link']; $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]'; $txt = t('%1$s is currently %2$s'); $item['body'] = sprintf($txt, $A, t($verb)); } if (activity_match($item['verb'], ACTIVITY_TAG)) { $r = q("SELECT * from `item`,`contact` WHERE\n\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri'])); if (count($r) == 0) { return; } $obj = $r[0]; $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]'; $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]'; switch ($obj['verb']) { case ACTIVITY_POST: switch ($obj['object-type']) { case ACTIVITY_OBJ_EVENT: $post_type = t('event'); break; default: $post_type = t('status'); } break; default: if ($obj['resource-id']) { $post_type = t('photo'); $m = array(); preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m); $rr['plink'] = $m[1]; } else { $post_type = t('status'); } } $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]'; $parsedobj = parse_xml_string($xmlhead . $item['object']); $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content); $item['body'] = sprintf(t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag); } if (activity_match($item['verb'], ACTIVITY_FAVORITE)) { if ($item['object-type'] == "") { return; } $Aname = $item['author-name']; $Alink = $item['author-link']; $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; $obj = parse_xml_string($xmlhead . $item['object']); if (strlen($obj->id)) { $r = q("select * from item where uri = '%s' and uid = %d limit 1", dbesc($obj->id), intval($item['uid'])); if (count($r) && $r[0]['plink']) { $target = $r[0]; $Bname = $target['author-name']; $Blink = $target['author-link']; $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]'; $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]'; $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]'; $item['body'] = sprintf(t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P) . "\n"; } } } $matches = null; if (preg_match_all('/@\\[url=(.*?)\\]/is', $item['body'], $matches, PREG_SET_ORDER)) { foreach ($matches as $mtch) { if (!strpos($mtch[1], 'zrl=')) { $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']); } } } // add zrl's to public images $photo_pattern = "/\\[url=(.*?)\\/photos\\/(.*?)\\/image\\/(.*?)\\]\\[img(.*?)\\]h(.*?)\\[\\/img\\]\\[\\/url\\]/is"; if (preg_match($photo_pattern, $item['body'])) { $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3', true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]'; $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']); } // add sparkle links to appropriate permalinks $x = stristr($item['plink'], '/display/'); if ($x) { $sparkle = false; $y = best_link_url($item, $sparkle, true); if (strstr($y, '/redir/')) { $item['plink'] = $y . '?f=&url=' . $item['plink']; } } }
/** * @brief * * We don't want to support a bbcode specific markdown interpreter * and the markdown library we have is pretty good, but provides HTML output. * So we'll use that to convert to HTML, then convert the HTML back to bbcode, * and then clean up a few Diaspora specific constructs. * * @param string $s * @param boolean $use_zrl default false * @return string */ function diaspora2bb($s, $use_zrl = false) { $s = str_replace("
", "\r", $s); $s = str_replace("
\n>", "", $s); $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8'); // first try plustags $s = preg_replace_callback('/\\@\\{(.+?)\\; (.+?)\\@(.+?)\\}\\+/', 'diaspora_mention_callback', $s); $s = preg_replace_callback('/\\@\\{(.+?)\\; (.+?)\\@(.+?)\\}/', 'diaspora_mention_callback', $s); // Escaping the hash tags - doesn't always seem to work // $s = preg_replace('/\#([^\s\#])/','\\#$1',$s); // This seems to work $s = preg_replace('/\\#([^\\s\\#])/', '#$1', $s); $s = Markdown($s); $s = str_replace("\r", "", $s); $s = str_replace('#', '#', $s); $s = html2bbcode($s); // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands $s = str_replace('♲', html_entity_decode('♲', ENT_QUOTES, 'UTF-8'), $s); // Convert everything that looks like a link to a link if ($use_zrl) { $s = str_replace(array('[img', '/img]'), array('[zmg', '/zmg]'), $s); $s = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/)([a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1[zrl=$2$3]$2$3[/zrl]', $s); } else { $s = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/)([a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1[url=$2$3]$2$3[/url]', $s); } //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s); $s = bb_tag_preg_replace("/\\[url\\=?(.*?)\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/url\\]/ism", '[embed]https://www.youtube.com/watch?v=$2[/embed]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\].*?\\[\\/url\\]/ism", '[embed]https://www.youtube.com/watch?v=$1[/embed]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=?(.*?)\\]https?:\\/\t\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/url\\]/ism", '[embed]https://vimeo.com/$2[/embed]', 'url', $s); $s = bb_tag_preg_replace("/\\[url\\=https?:\\/\\/vimeo.com\\/([0-9]+)\\](.*?)\\[\\/url\\]/ism", '[embed]https://vimeo.com/$1[/embed]', 'url', $s); // remove duplicate adjacent code tags $s = preg_replace("/(\\[code\\])+(.*?)(\\[\\/code\\])+/ism", "[code]\$2[/code]", $s); // Don't show link to full picture (until it is fixed) $s = scale_external_images($s, false); return $s; }