From 8ec833f8087b0bba2f167471c430543c271602dc Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Mon, 22 Feb 2016 23:20:59 +0100 Subject: [PATCH 1/3] New BBCode element "abstract" for network depending messages. --- include/bbcode.php | 42 ++++++++++++++++++++++++++++++++++++++++++ include/dfrn.php | 4 ++++ include/plaintext.php | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/include/bbcode.php b/include/bbcode.php index 6a44e19ec4..eb009477cf 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -851,6 +851,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal $a = get_app(); + // Remove the abstract element. It is a non visible element. + $Text = remove_abstract($Text); + // Hide all [noparse] contained bbtags by spacefying them // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image? @@ -1300,4 +1303,43 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal return trim($Text); } + +/** + * @brief Removes the "abstract" element from the text + * + * @param string $text The text with BBCode + * @return string The same text - but without "abstract" element + */ +function remove_abstract($text) { + $text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", '', $text); + $text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", '', $text); + + return $text; +} + +/** + * @brief Returns the value of the "abstract" element + * + * @param string $text The text that maybe contains the element + * @param string $addon The addon for which the abstract is meant for + * @return string The abstract + */ +function fetch_abstract($text, $addon = "") { + $abstract = ""; + $abstracts = array(); + $addon = strtolower($addon); + + if (preg_match_all("/\[abstract=(.*?)\](.*?)\[\/abstract\]/ism",$text, $results, PREG_SET_ORDER)) + foreach ($results AS $result) + $abstracts[strtolower($result[1])] = $result[2]; + + if (isset($abstracts[$addon])) + $abstract = $abstracts[$addon]; + + if ($abstract == "") + if (preg_match("/\[abstract\](.*?)\[\/abstract\]/ism",$text, $result)) + $abstract = $result[1]; + + return $abstract; +} ?> diff --git a/include/dfrn.php b/include/dfrn.php index f7a05bdb63..ad04a91295 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -18,6 +18,7 @@ require_once("include/event.php"); require_once("include/text.php"); require_once("include/oembed.php"); require_once("include/html2bbcode.php"); +require_once("include/bbcode.php"); /** * @brief This class contain functions to create and send DFRN XML files @@ -720,6 +721,9 @@ class dfrn { else $body = $item['body']; + // Remove the abstract element. It is only locally important. + $body = remove_abstract($body); + if ($type == 'html') { $htmlbody = $body; diff --git a/include/plaintext.php b/include/plaintext.php index 05431bee2d..199abcbb31 100644 --- a/include/plaintext.php +++ b/include/plaintext.php @@ -132,7 +132,7 @@ function shortenmsg($msg, $limit, $twitter = false) { return($msg); } -function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2) { +function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") { require_once("include/bbcode.php"); require_once("include/html2plain.php"); require_once("include/network.php"); @@ -144,6 +144,9 @@ function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2) { // Add an URL element if the text contains a raw link $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body); + // Remove the abstract + $body = remove_abstract($body); + // At first look at data that is attached via "type-..." stuff // This will hopefully replaced with a dedicated bbcode later //$post = get_attached_data($b["body"]); @@ -154,6 +157,44 @@ function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2) { elseif ($b["title"] != "") $post["text"] = trim($b["title"]); + $abstract = ""; + + // Fetch the abstract from the given target network + if ($target_network != "") { + $default_abstract = fetch_abstract($b["body"]); + $abstract = fetch_abstract($b["body"], $target_network); + + // If we post to a network with no limit we only fetch + // an abstract exactly for this network + if (($limit == 0) AND ($abstract == $default_abstract)) + $abstract = ""; + + } else // Try to guess the correct target network + switch ($htmlmode) { + case 8: + $abstract = fetch_abstract($b["body"], NETWORK_TWITTER); + break; + case 7: + $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET); + break; + case 6: + $abstract = fetch_abstract($b["body"], NETWORK_APPNET); + break; + default: // We don't know the exact target. + // We fetch an abstract since there is a posting limit. + if ($limit > 0) + $abstract = fetch_abstract($b["body"]); + } + + if ($abstract != "") { + $post["text"] = $abstract; + + if ($post["type"] == "text") { + $post["type"] = "link"; + $post["url"] = $b["plink"]; + } + } + $html = bbcode($post["text"], false, false, $htmlmode); $msg = html2plain($html, 0, true); $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8')); From bc283a5316e7f58edd26a93d8811b30527d8100c Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 23 Feb 2016 07:21:40 +0100 Subject: [PATCH 2/3] The "abstract" has moved after the "nobb" part in bbcode. --- include/bbcode.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index eb009477cf..c1156e3afe 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -851,9 +851,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal $a = get_app(); - // Remove the abstract element. It is a non visible element. - $Text = remove_abstract($Text); - // Hide all [noparse] contained bbtags by spacefying them // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image? @@ -861,6 +858,8 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text); $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text); + // Remove the abstract element. It is a non visible element. + $Text = remove_abstract($Text); // Move all spaces out of the tags $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text); From 148c89a20d3fa75904f87d4b7fc0e70077ace85a Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 23 Feb 2016 07:56:49 +0100 Subject: [PATCH 3/3] Added documentation --- include/plaintext.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/plaintext.php b/include/plaintext.php index 199abcbb31..a2b2c56522 100644 --- a/include/plaintext.php +++ b/include/plaintext.php @@ -132,6 +132,18 @@ function shortenmsg($msg, $limit, $twitter = false) { return($msg); } +/** + * @brief Convert a message into plaintext for connectors to other networks + * + * @param App $a The application class + * @param array $b The message array that is about to be posted + * @param int $limit The maximum number of characters when posting to that network + * @param bool $includedlinks Has an attached link to be included into the message? + * @param int $htmlmode This triggers the behaviour of the bbcode conversion + * @param string $target_network Name of the network where the post should go to. + * + * @return string The converted message + */ function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") { require_once("include/bbcode.php"); require_once("include/html2plain.php");