Enforce standards

This commit is contained in:
Hypolite Petovan 2017-04-06 23:43:55 -04:00
parent c9dafe3b4e
commit 8149e21add
2 changed files with 190 additions and 173 deletions

View file

@ -1,11 +1,12 @@
<?php
require_once("include/oembed.php");
require_once("include/event.php");
require_once("library/markdown.php");
require_once("include/html2bbcode.php");
require_once("include/bbcode.php");
require_once("library/html-to-markdown/HTML_To_Markdown.php");
use League\HTMLToMarkdown\HtmlConverter;
require_once "include/oembed.php";
require_once "include/event.php";
require_once "library/markdown.php";
require_once "include/html2bbcode.php";
require_once "include/bbcode.php";
/**
* @brief Callback function to replace a Diaspora style mention in a mention for Friendica
@ -111,16 +112,19 @@ function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
$OriginalText = $Text;
// Since Diaspora is creating a summary for links, this function removes them before posting
if ($fordiaspora)
if ($fordiaspora) {
$Text = bb_remove_share_information($Text);
}
/**
* Transform #tags, strip off the [url] and replace spaces with underscore
*/
$URLSearchString = "^\[\]";
$Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
), $Text);
$Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
function ($matches) {
return '#' . str_replace(' ', '_', $matches[2]);
}
, $Text);
// Converting images with size parameters to simple images. Markdown doesn't know it.
$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
@ -134,14 +138,15 @@ function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
$tagline = "";
foreach ($tags[2] as $tag) {
$tag = html_entity_decode($tag, ENT_QUOTES, 'UTF-8');
if (!strpos(html_entity_decode($Text, ENT_QUOTES, 'UTF-8'), "#".$tag))
$tagline .= "#".$tag." ";
if (!strpos(html_entity_decode($Text, ENT_QUOTES, 'UTF-8'), '#' . $tag)) {
$tagline .= '#' . $tag . ' ';
}
}
$Text = $Text." ".$tagline;
}
} else
} else {
$Text = bbcode($Text, $preserve_nl, false, 4);
}
// mask some special HTML chars from conversation to markdown
$Text = str_replace(array('&lt;', '&gt;', '&amp;'), array('&_lt_;', '&_gt_;', '&_amp_;'), $Text);

View file

@ -1,12 +1,12 @@
<?php
use \Friendica\Core\Config;
require_once("include/oembed.php");
require_once('include/event.php');
require_once('include/map.php');
require_once('mod/proxy.php');
require_once('include/Contact.php');
require_once('include/plaintext.php');
require_once 'include/oembed.php';
require_once 'include/event.php';
require_once 'include/map.php';
require_once 'mod/proxy.php';
require_once 'include/Contact.php';
require_once 'include/plaintext.php';
function bb_PictureCacheExt($matches) {
if (strpos($matches[3], "data:image/") === 0)
@ -186,7 +186,6 @@ function tryoembed($match){
$html = oembed_format_object($o);
return $html;
}
// [noparse][i]italic[/i][/noparse] turns into
@ -212,13 +211,13 @@ function bb_unspacefy_and_trim($st) {
return $unspacefied;
}
function bb_find_open_close($s, $open, $close, $occurance = 1) {
function bb_find_open_close($s, $open, $close, $occurence = 1) {
if($occurance < 1)
$occurance = 1;
if($occurence < 1)
$occurence = 1;
$start_pos = -1;
for($i = 1; $i <= $occurance; $i++) {
for($i = 1; $i <= $occurence; $i++) {
if( $start_pos !== false)
$start_pos = strpos($s, $open, $start_pos + 1);
}
@ -236,13 +235,13 @@ function bb_find_open_close($s, $open, $close, $occurance = 1) {
return $res;
}
function get_bb_tag_pos($s, $name, $occurance = 1) {
function get_bb_tag_pos($s, $name, $occurence = 1) {
if($occurance < 1)
$occurance = 1;
if($occurence < 1)
$occurence = 1;
$start_open = -1;
for($i = 1; $i <= $occurance; $i++) {
for($i = 1; $i <= $occurence; $i++) {
if( $start_open !== false)
$start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
}
@ -275,9 +274,9 @@ function bb_tag_preg_replace($pattern, $replace, $name, $s) {
$string = $s;
$occurance = 1;
$pos = get_bb_tag_pos($string, $name, $occurance);
while($pos !== false && $occurance < 1000) {
$occurence = 1;
$pos = get_bb_tag_pos($string, $name, $occurence);
while($pos !== false && $occurence < 1000) {
$start = substr($string, 0, $pos['start']['open']);
$subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
@ -288,8 +287,8 @@ function bb_tag_preg_replace($pattern, $replace, $name, $s) {
$subject = preg_replace($pattern, $replace, $subject);
$string = $start . $subject . $end;
$occurance++;
$pos = get_bb_tag_pos($string, $name, $occurance);
$occurence++;
$pos = get_bb_tag_pos($string, $name, $occurence);
}
return $string;
@ -779,8 +778,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
$Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
// when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
if (!$tryoembed)
if (!$tryoembed) {
$Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "\n[share$1$2]$3[/share]", $Text);
}
// Check for [code] text here, before the linefeeds are messed with.
// The highlighter will unescape and re-escape the content.
@ -814,8 +814,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
$Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
if($preserve_nl)
if ($preserve_nl) {
$Text = str_replace(array("\n", "\r"), array('', ''), $Text);
}
// Set up the parameters for a URL search string
$URLSearchString = "^\[\]";
@ -823,20 +824,21 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
$MAILSearchString = $URLSearchString;
// Remove all hashtag addresses
if ((!$tryoembed OR $simplehtml) AND !in_array($simplehtml, array(3, 7)))
if ((!$tryoembed OR $simplehtml) AND !in_array($simplehtml, array(3, 7))) {
$Text = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text);
elseif ($simplehtml == 3)
} elseif ($simplehtml == 3) {
$Text = preg_replace("/([@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
'$1<a href="$2">$3</a>',
$Text);
elseif ($simplehtml == 7)
} elseif ($simplehtml == 7) {
$Text = preg_replace("/([@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
'$1<span class="vcard"><a href="$2" class="url" title="$3"><span class="fn nickname mention">$3</span></a></span>',
$Text);
elseif (!$simplehtml)
} elseif (!$simplehtml) {
$Text = preg_replace("/([@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
'$1<a href="$2" class="userinfo mention" title="$3">$3</a>',
$Text);
}
// Bookmarks in red - will be converted to bookmarks in friendica
$Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
@ -850,33 +852,37 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
$Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]',$Text);
}
if ($simplehtml == 5)
if ($simplehtml == 5) {
$Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text);
}
// Perform URL Search
if ($tryoembed)
if ($tryoembed) {
$Text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", 'tryoembed', $Text);
}
if ($simplehtml == 5)
if ($simplehtml == 5) {
$Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url]$1[/url]', $Text);
else
} else {
$Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url=$1]$2[/url]', $Text);
}
// Handle Diaspora posts
$Text = preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi", 'bb_DiasporaLinks', $Text);
// if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
// if ($simplehtml != 7) {
if (!$forplaintext)
if (!$forplaintext) {
$Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
else {
} else {
$Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism", " $1 ", $Text);
$Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text);
}
// }
if ($tryoembed)
if ($tryoembed) {
$Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'tryoembed', $Text);
}
$Text = preg_replace("/([#])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
'$1<a href="$2" class="tag" title="$3">$3</a>', $Text);
@ -1007,8 +1013,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
// Check for [spoiler] text
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism", "$SpoilerLayout", $Text);
}
// Check for [spoiler=Author] text
@ -1016,10 +1023,11 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
"<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
$Text);
}
// Declare the format for [quote] layout
$QuoteLayout = '<blockquote>$1</blockquote>';
@ -1027,8 +1035,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
// Check for [quote] text
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism", "$QuoteLayout", $Text);
}
// Check for [quote=Author] text
@ -1036,10 +1045,11 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
"<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
$Text);
}
// [img=widthxheight]image source[/img]
@ -1083,10 +1093,11 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
// html5 video and audio
if ($tryoembed)
if ($tryoembed) {
$Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
else
} else {
$Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
}
// Youtube extensions
if ($tryoembed) {
@ -1099,11 +1110,12 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
$Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $Text);
$Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $Text);
if ($tryoembed)
if ($tryoembed) {
$Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
else
} else {
$Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
'<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
}
if ($tryoembed) {
$Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism", 'tryoembed', $Text);
@ -1113,11 +1125,12 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
$Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $Text);
$Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $Text);
if ($tryoembed)
if ($tryoembed) {
$Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
else
} else {
$Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
'<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
}
// $Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
@ -1253,4 +1266,3 @@ function fetch_abstract($text, $addon = "") {
return $abstract;
}
?>