This commit is contained in:
friendica 2015-04-07 18:13:31 -07:00
commit b212482330

View File

@ -1,6 +1,10 @@
<?php /** @file */
<?php
/**
* @file include/bbcode.php
* @brief BBCode related functions for parsing, etc.
*/
require_once("include/oembed.php");
require_once('include/oembed.php');
require_once('include/event.php');
require_once('include/zot.php');
require_once('include/hubloc.php');
@ -10,18 +14,19 @@ function tryoembed($match) {
$o = oembed_fetch_url($url);
if ($o->type=="error") return $match[0];
if ($o->type == 'error')
return $match[0];
$html = oembed_format_object($o);
return $html;
}
function tryzrlaudio($match) {
$link = $match[1];
$zrl = is_matrix_url($link);
if($zrl)
$link = zid($link);
return '<audio src="' . str_replace(' ','%20',$link) . '" controls="controls"><a href="' . str_replace(' ','%20',$link) . '">' . $link . '</a></audio>';
}
@ -30,8 +35,8 @@ function tryzrlvideo($match) {
$zrl = is_matrix_url($link);
if($zrl)
$link = zid($link);
return '<video controls="controls" src="' . str_replace(' ','%20',$link) . '" style="width:100%; max-width:' . get_app()->videowidth . 'px"><a href="' . str_replace(' ','%20',$link) . '">' . $link . '</a></video>';
return '<video controls="controls" src="' . str_replace(' ','%20',$link) . '" style="width:100%; max-width:' . get_app()->videowidth . 'px"><a href="' . str_replace(' ','%20',$link) . '">' . $link . '</a></video>';
}
// [noparse][i]italic[/i][/noparse] turns into
@ -43,6 +48,7 @@ function bb_spacefy($st) {
$captured = $st[1];
$spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
$new_str = str_replace($captured, $spacefied, $whole_match);
return $new_str;
}
@ -51,9 +57,10 @@ function bb_spacefy($st) {
// returning [i]italic[/i]
function bb_unspacefy_and_trim($st) {
$whole_match = $st[0];
//$whole_match = $st[0];
$captured = $st[1];
$unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
return $unspacefied;
}
@ -103,8 +110,8 @@ function bb_extract_images($body) {
function bb_replace_images($body, $images) {
$newbody = $body;
$cnt = 0;
if(! $images)
return $newbody;
@ -119,10 +126,15 @@ function bb_replace_images($body, $images) {
return $newbody;
}
/**
* @brief Parses crypt BBCode.
*
* @param array $match
* @return string HTML code
*/
function bb_parse_crypt($match) {
$matches = array();
$attributes = $match[1];
$algorithm = "";
@ -137,7 +149,6 @@ function bb_parse_crypt($match) {
$hint = "";
preg_match("/hint='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$hint = $matches[1];
@ -150,7 +161,6 @@ function bb_parse_crypt($match) {
$Text = '<br /><div id="' . $x . '"><img src="' . z_root() . '/images/lock_icon.gif" onclick="red_decrypt(\'' . $algorithm . '\',\'' . $hint . '\',\'' . $match[2] . '\',\'#' . $x . '\');" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /></div><br />';
return $Text;
}
function bb_parse_app($match) {
@ -159,7 +169,6 @@ function bb_parse_app($match) {
$app = app_decode($match[1]);
if ($app)
return app_render($app);
}
function bb_parse_element($match) {
@ -167,9 +176,16 @@ function bb_parse_element($match) {
if ($j) {
$o = EOL . '<a href="#" onclick="importElement(\'' . $match[1] . '\'); return false;" >' . t('Install design element: ') . $j['pagetitle'] . '</a>' . EOL;
}
return $o;
}
/**
* @brief Returns an QR-code image from a value given in $match[1].
*
* @param array $match
* @return string HTML img with QR-code of $match[1]
*/
function bb_qr($match) {
return '<img class="zrl" src="' . z_root() . '/photo/qr?f=&qr=' . urlencode($match[1]) . '" alt="' . t('QR code') . '" title="' . htmlspecialchars($match[1],ENT_QUOTES,'UTF-8') . '" />';
}
@ -177,6 +193,7 @@ function bb_qr($match) {
function bb_ShareAttributes($match) {
$matches = array();
$attributes = $match[1];
$author = "";
@ -204,13 +221,14 @@ function bb_ShareAttributes($match) {
if ($matches[1] != "")
$posted = $matches[1];
// message_id is never used, do we still need it?
$message_id = "";
preg_match("/message_id='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$message_id = $matches[1];
// FIXME - this should really be a wall-item-ago so it will get updated on the client
/** @FIXME - this should really be a wall-item-ago so it will get updated on the client */
$reldate = (($posted) ? relative_date($posted) : '');
$headline = '<div class="shared_container"> <div class="shared_header">';
@ -230,24 +248,30 @@ function bb_ShareAttributes($match) {
$text = $headline . '<div class="reshared-content">' . trim($match[2]) . '</div></div>';
return($text);
return $text;
}
function bb_location($match) {
// not yet implemented
}
function bbiframe($match) {
/**
* @brief Returns an iframe from $match[1].
*
* @param array $match
* @return string HTML iframe with content of $match[1]
*/
function bb_iframe($match) {
$a = get_app();
$sandbox = ((strpos($match[1],get_app()->get_hostname())) ? ' sandbox="allow-scripts" ' : '');
$sandbox = ((strpos($match[1], $a->get_hostname())) ? ' sandbox="allow-scripts" ' : '');
return '<iframe ' . $sandbox . ' src="' . $match[1] . '" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="' . $match[1] . '">' . $match[1] . '</a></iframe>';
}
function bb_ShareAttributesSimple($match) {
$matches = array();
$attributes = $match[1];
$author = "";
@ -268,7 +292,7 @@ function bb_ShareAttributesSimple($match) {
if ($matches[1] != "")
$profile = $matches[1];
$text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$author."</a>: div class=\"reshared-content\">" .$match[2]."</div>";
$text = '<br />' . html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8') . ' <a href="' . $profile . '">' . $author . '</a>: div class="reshared-content">' . $match[2] . '</div>';
return($text);
}
@ -291,16 +315,21 @@ function bb_map_location($match) {
return str_replace($match[0],'<div class="map" >' . generate_named_map($match[1]) . '</div>', $match[0]);
}
function bbopentag($match) {
function bb_opentag($match) {
$rnd = mt_rand();
return "<br /><div onclick=\"openClose('opendiv-" . $rnd . "');return false;\" class=\"fakelink\">" . $match[1] . "</div><div id=\"opendiv-" . $rnd . "\" style=\"display: none;\">" . $match[2] . "</div>";
}
/**
* @brief Sanitize style properties from BBCode to HTML.
*
* @param array $input
* @return string A HTML span tag with the styles.
*/
function bb_sanitize_style($input) {
//whitelist property limits (0 = no limitation)
$w = array( // color properties
// whitelist array: property => limits (0 = no limitation)
$w = array(
// color properties
"color" => 0,
"background-color" => 0,
// box properties
@ -311,11 +340,12 @@ function bb_sanitize_style($input) {
"clear" => 0,
// text properties
"text-decoration" => 0,
);
$css = array();
$css_string = $input[1];
$a = explode(';', $css_string);
foreach($a as $parts){
list($k, $v) = explode(':', $parts);
$css[ trim($k) ] = trim($v);
@ -324,6 +354,7 @@ function bb_sanitize_style($input) {
// sanitize properties
$b = array_merge(array_diff_key($css, $w), array_diff_key($w, $css));
$css = array_diff_key($css, $b);
$css_string_san = '';
foreach ($css as $key => $value) {
if ($w[$key] != null) {
@ -346,7 +377,8 @@ function bb_sanitize_style($input) {
}
$css_string_san .= $key . ":" . $value ."; ";
}
return "<span style=\"" . $css_string_san . "\">" . $input[2] . "</span>";
return '<span style="' . $css_string_san . '">' . $input[2] . '</span>';
}
// BBcode 2 HTML was written by WAY2WEB.net
@ -471,8 +503,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// Perform URL Search
$urlchars = '[a-zA-Z0-9\:\/\-\?\&\;\.\=\@\_\~\#\%\$\!\+\,\@]';
@ -482,12 +512,11 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
}
if (strpos($Text,'[/qr]') !== false) {
$Text = preg_replace_callback("/\[qr\](.*?)\[\/qr\]/ism","bb_qr",$Text);
$Text = preg_replace_callback("/\[qr\](.*?)\[\/qr\]/ism", 'bb_qr', $Text);
}
if (strpos($Text,'[/share]') !== false) {
$Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributes",$Text);
$Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism", 'bb_ShareAttributes', $Text);
}
if($tryoembed) {
if (strpos($Text,'[/url]') !== false) {
@ -518,11 +547,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
if (strpos($Text,'[/map]') !== false) {
$Text = preg_replace_callback("/\[map\](.*?)\[\/map\]/ism", 'bb_map_location', $Text);
}
if (strpos($Text,'[map=') !== false) {
$Text = preg_replace_callback("/\[map=(.*?)\]/ism", 'bb_map_coords', $Text);
}
if (strpos($Text,'[map]') !== false) {
$Text = preg_replace("/\[map\]/", '<div class="map"></div>', $Text);
}
@ -595,7 +622,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// Check for table of content with params
if (strpos($Text,'[toc') !== false) {
$Text = preg_replace("/\[toc([^\]]+?)\]/ism",'<ul$1></ul>',$Text);
}
// Check for centered text
if (strpos($Text,'[/center]') !== false) {
@ -647,13 +673,15 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
if (strpos($Text,'[/font]') !== false) {
$Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm", "<span style=\"font-family: $1;\">$2</span>", $Text);
}
// Declare the format for [code] layout
// Declare the format for [code] layout
$CodeLayout = '<code>$1</code>';
// Check for [code] text
if (strpos($Text,'[code]') !== false) {
$Text = preg_replace("/\[code\](.*?)\[\/code\]/ism", "$CodeLayout", $Text);
}
// Declare the format for [spoiler] layout
$SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
@ -677,12 +705,10 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$endlessloop = 0;
while ((strpos($Text, "[/open]")!== false) and (strpos($Text, "[open=") !== false) and (++$endlessloop < 20)) {
$rnd = mt_rand();
$Text = preg_replace_callback("/\[open=(.*?)\](.*?)\[\/open\]/ism",'bbopentag',$Text);
$Text = preg_replace_callback("/\[open=(.*?)\](.*?)\[\/open\]/ism", 'bb_opentag', $Text);
}
// Declare the format for [quote] layout
$QuoteLayout = '<blockquote>$1</blockquote>';
@ -768,7 +794,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$Text = preg_replace_callback("/\[element\](.*?)\[\/element\]/ism",'bb_parse_element', $Text);
}
// html5 video and audio
if (strpos($Text,'[/video]') !== false) {
$Text = preg_replace_callback("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mpeg|mpg))\[\/video\]/ism", 'tryzrlvideo', $Text);
@ -785,7 +810,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// Try to Oembed
if ($tryoembed) {
if (strpos($Text,'[/video]') !== false) {
$Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
}
@ -816,19 +840,16 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$Text = preg_replace("/\[zaudio\](.*?)\[\/zaudio\]/", '<a class="zid" href="$1">$1</a>', $Text);
}
if ($tryoembed){
if (strpos($Text,'[/iframe]') !== false) {
$Text = preg_replace_callback("/\[iframe\](.*?)\[\/iframe\]/ism", 'bbiframe', $Text);
$Text = preg_replace_callback("/\[iframe\](.*?)\[\/iframe\]/ism", 'bb_iframe', $Text);
}
}
else {
} else {
if (strpos($Text,'[/iframe]') !== false) {
$Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
}
}
// Youtube extensions
if (strpos($Text,'[youtube]') !== false) {
if ($tryoembed) {
@ -907,4 +928,3 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
return $Text;
}