From 010cf3b1e8b5119ab5b8a742a90b3b9af5a27f05 Mon Sep 17 00:00:00 2001
From: Hypolite Petovan <mrpetovan@gmail.com>
Date: Sun, 4 Feb 2018 19:23:49 -0500
Subject: [PATCH] Update references to BBCode methods

- Resolve name conflicts with Util\Network
---
 include/acl_selectors.php   |  8 ++++---
 include/api.php             |  4 ++--
 include/bb2diaspora.php     | 10 ++++----
 include/conversation.php    |  8 ++++---
 mod/display.php             |  4 +++-
 src/Content/Text/BBCode.php | 48 ++++++++++++++++++-------------------
 src/Protocol/DFRN.php       |  5 ++--
 view/theme/frio/theme.php   | 13 +++++-----
 view/theme/frost/theme.php  | 10 ++++----
 9 files changed, 56 insertions(+), 54 deletions(-)

diff --git a/include/acl_selectors.php b/include/acl_selectors.php
index 72f49a40ba..166cf97197 100644
--- a/include/acl_selectors.php
+++ b/include/acl_selectors.php
@@ -2,16 +2,18 @@
 /**
  * @file include/acl_selectors.php
  */
+
 use Friendica\App;
 use Friendica\Content\Feature;
 use Friendica\Content\Widget;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Network;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
 use Friendica\Model\GContact;
-use Friendica\Util\Network;
+use Friendica\Util\Network as NetworkUtil;
 
 require_once "mod/proxy.php";
 
@@ -248,7 +250,7 @@ function contact_select($selname, $selclass, $preselected = false, $size = 4, $p
 			}
 
 			if ($privmail) {
-				$trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
+				$trimmed = Network::formatMention($rr['url'], $rr['name']);
 			} else {
 				$trimmed = mb_substr($rr['name'],0,20);
 			}
@@ -734,7 +736,7 @@ function navbar_complete(App $a) {
 	if (! $localsearch) {
 		$p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
 
-		$x = Network::curl(get_server() . '/lsearch?f=' . $p .  '&search=' . urlencode($search));
+		$x = NetworkUtil::curl(get_server() . '/lsearch?f=' . $p .  '&search=' . urlencode($search));
 		if ($x['success']) {
 			$j = json_decode($x['body'],true);
 			if ($j && isset($j['results'])) {
diff --git a/include/api.php b/include/api.php
index e50bc92a8e..a477060135 100644
--- a/include/api.php
+++ b/include/api.php
@@ -2753,7 +2753,7 @@ function api_get_entitities(&$text, $bbcode)
 		return [];
 	}
 
-	$bbcode = bb_CleanPictureLinks($bbcode);
+	$bbcode = BBCode::cleanPictureLinks($bbcode);
 
 	// Change pure links in text to bbcode uris
 	$bbcode = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2]$2[/url]', $bbcode);
@@ -5173,7 +5173,7 @@ function api_clean_plain_items($Text)
 {
 	$include_entities = strtolower(x($_REQUEST, 'include_entities') ? $_REQUEST['include_entities'] : "false");
 
-	$Text = bb_CleanPictureLinks($Text);
+	$Text = BBCode::cleanPictureLinks($Text);
 	$URLSearchString = "^\[\]";
 
 	$Text = preg_replace("/([!#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text);
diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php
index 0cb50e95ee..c1c9fa4c3d 100644
--- a/include/bb2diaspora.php
+++ b/include/bb2diaspora.php
@@ -76,10 +76,10 @@ function diaspora2bb($s) {
 	$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);
+	$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
+	$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism'   , '[youtube]$1[/youtube]', 'url', $s);
+	$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism'        , '[vimeo]$2[/vimeo]'    , 'url', $s);
+	$s = BBCode::pregReplaceInTag('/\[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);
@@ -130,7 +130,7 @@ function bb2diaspora($Text, $preserve_nl = false, $fordiaspora = true) {
 
 	// Since Diaspora is creating a summary for links, this function removes them before posting
 	if ($fordiaspora) {
-		$Text = bb_remove_share_information($Text);
+		$Text = BBCode::removeShareInformation($Text);
 	}
 
 	/**
diff --git a/include/conversation.php b/include/conversation.php
index f0d6bbfaba..90634f4905 100644
--- a/include/conversation.php
+++ b/include/conversation.php
@@ -6,6 +6,7 @@
 use Friendica\App;
 use Friendica\Content\ContactSelector;
 use Friendica\Content\Feature;
+use Friendica\Content\Text\BBCode;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
@@ -72,7 +73,7 @@ function item_redir_and_replace_images($body, $images, $cid) {
 	$newbody = '';
 
 	$cnt = 1;
-	$pos = get_bb_tag_pos($origbody, 'url', 1);
+	$pos = BBCode::getTagPosition($origbody, 'url', 0);
 	while ($pos !== false && $cnt < 1000) {
 
 		$search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
@@ -90,7 +91,8 @@ function item_redir_and_replace_images($body, $images, $cid) {
 		$newbody .= $subject;
 
 		$cnt++;
-		$pos = get_bb_tag_pos($origbody, 'url', 1);
+		// Isn't this supposed to use $cnt value for $occurrences? - @MrPetovan
+		$pos = BBCode::getTagPosition($origbody, 'url', 0);
 	}
 	$newbody .= $origbody;
 
@@ -347,7 +349,7 @@ function localize_item(&$item) {
 	$photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
 	if (preg_match($photo_pattern, $item['body'])) {
 		$photo_replace = '[url=' . Profile::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']);
+		$item['body'] = BBCode::pregReplaceInTag($photo_pattern, $photo_replace, 'url', $item['body']);
 	}
 
 	// add sparkle links to appropriate permalinks
diff --git a/mod/display.php b/mod/display.php
index 8d991d4768..dd6b8b709c 100644
--- a/mod/display.php
+++ b/mod/display.php
@@ -2,9 +2,11 @@
 /**
  * @file mod/display.php
  */
+
 use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Network;
 use Friendica\Core\System;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
@@ -171,7 +173,7 @@ function display_fetchauthor($a, $item) {
 			$profiledata["photo"] = $matches[1];
 		}
 		$profiledata["nickname"] = $profiledata["name"];
-		$profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
+		$profiledata["network"] = Network::matchByProfileUrl($profiledata["url"]);
 
 		$profiledata["address"] = "";
 		$profiledata["about"] = "";
diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php
index 974ef14646..864c0d42d6 100644
--- a/src/Content/Text/BBCode.php
+++ b/src/Content/Text/BBCode.php
@@ -13,12 +13,13 @@ use Friendica\Core\Addon;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Network;
 use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Model\Contact;
 use Friendica\Object\Image;
 use Friendica\Util\Map;
-use Friendica\Util\Network;
+use Friendica\Util\Network as NetworkUtil;
 use Friendica\Util\ParseUrl;
 
 require_once "include/bbcode.php";
@@ -352,7 +353,7 @@ class BBCode
 		$body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
 
 		// Remove the abstract
-		$body = remove_abstract($body);
+		$body = self::removeAbstract($body);
 
 		// At first look at data that is attached via "type-..." stuff
 		// This will hopefully replaced with a dedicated bbcode later
@@ -369,8 +370,8 @@ class BBCode
 
 		// Fetch the abstract from the given target network
 		if ($target_network != "") {
-			$default_abstract = fetch_abstract($b["body"]);
-			$abstract = fetch_abstract($b["body"], $target_network);
+			$default_abstract = self::getAbstract($b["body"]);
+			$abstract = self::getAbstract($b["body"], $target_network);
 
 			// If we post to a network with no limit we only fetch
 			// an abstract exactly for this network
@@ -380,18 +381,18 @@ class BBCode
 		} else {// Try to guess the correct target network
 			switch ($htmlmode) {
 				case 8:
-					$abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
+					$abstract = self::getAbstract($b["body"], NETWORK_TWITTER);
 					break;
 				case 7:
-					$abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
+					$abstract = self::getAbstract($b["body"], NETWORK_STATUSNET);
 					break;
 				case 6:
-					$abstract = fetch_abstract($b["body"], NETWORK_APPNET);
+					$abstract = self::getAbstract($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"]);
+						$abstract = self::getAbstract($b["body"]);
 					}
 			}
 		}
@@ -518,7 +519,7 @@ class BBCode
 				} else {
 					$scaled = $mtch[1];
 				}
-				$i = Network::fetchUrl($scaled);
+				$i = NetworkUtil::fetchUrl($scaled);
 				if (!$i) {
 					return $srctext;
 				}
@@ -924,9 +925,9 @@ class BBCode
 	 */
 	public static function pregReplaceInTag($pattern, $replace, $name, $text)
 	{
-		$occurence = 1;
-		$pos = get_bb_tag_pos($text, $name, $occurence);
-		while ($pos !== false && $occurence < 1000) {
+		$occurrences = 0;
+		$pos = self::getTagPosition($text, $name, $occurrences);
+		while ($pos !== false && $occurrences++ < 1000) {
 			$start = substr($text, 0, $pos['start']['open']);
 			$subject = substr($text, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
 			$end = substr($text, $pos['end']['close']);
@@ -937,14 +938,13 @@ class BBCode
 			$subject = preg_replace($pattern, $replace, $subject);
 			$text = $start . $subject . $end;
 
-			$occurence++;
-			$pos = get_bb_tag_pos($text, $name, $occurence);
+			$pos = self::getTagPosition($text, $name, $occurrences);
 		}
 
 		return $text;
 	}
 
-	public static function extractImagesFromItemBody($body)
+	private static function extractImagesFromItemBody($body)
 	{
 		$saved_image = [];
 		$orig_body = $body;
@@ -985,7 +985,7 @@ class BBCode
 		return ['body' => $new_body, 'images' => $saved_image];
 	}
 
-	public static function interpolateSavedImagesIntoItemBody($body, array $images)
+	private static function interpolateSavedImagesIntoItemBody($body, array $images)
 	{
 		$newbody = $body;
 
@@ -1012,7 +1012,7 @@ class BBCode
 	 * @param bool|int $simplehtml
 	 * @return string
 	 */
-	public static function convertShare($share, $simplehtml)
+	private static function convertShare($share, $simplehtml)
 	{
 		$attributes = $share[2];
 
@@ -1082,13 +1082,13 @@ class BBCode
 		if (x($data, "name") && x($data, "addr")) {
 			$userid_compact = $data["name"] . " (" . $data["addr"] . ")";
 		} else {
-			$userid_compact = GetProfileUsername($profile, $author, true);
+			$userid_compact = Network::getAddrFromProfileUrl($profile, $author);
 		}
 
 		if (x($data, "addr")) {
 			$userid = $data["addr"];
 		} else {
-			$userid = GetProfileUsername($profile, $author, false);
+			$userid = Network::formatMention($profile, $author);
 		}
 
 		if (x($data, "name")) {
@@ -1378,7 +1378,7 @@ class BBCode
 		$text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'self::escapeNoparseCallback', $text);
 
 		// Remove the abstract element. It is a non visible element.
-		$text = remove_abstract($text);
+		$text = self::removeAbstract($text);
 
 		// Move all spaces out of the tags
 		$text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $text);
@@ -1388,7 +1388,7 @@ class BBCode
 		// large data sizes. Stash them away while we do bbcode conversion, and then put them back
 		// in after we've done all the regex matching. We cannot use any preg functions to do this.
 
-		$extracted = bb_extract_images($text);
+		$extracted = self::extractImagesFromItemBody($text);
 		$text = $extracted['body'];
 		$saved_image = $extracted['images'];
 
@@ -1767,7 +1767,7 @@ class BBCode
 		// Shared content
 		$text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
 			function ($match) use ($simple_html) {
-				return bb_ShareAttributes($match, $simple_html);
+				return self::convertShare($match, $simple_html);
 			}, $text);
 
 		$text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br/><img src="' .System::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . L10n::t('Encrypted content') . '" /><br />', $text);
@@ -1902,7 +1902,7 @@ class BBCode
 		$text = preg_replace($regex, '<$1$2="javascript:void(0)"$4 data-original-href="$3" class="invalid-href" title="' . L10n::t('Invalid link protocol') . '">', $text);
 
 		if ($saved_image) {
-			$text = bb_replace_images($text, $saved_image);
+			$text = self::interpolateSavedImagesIntoItemBody($text, $saved_image);
 		}
 
 		// Clean up the HTML by loading and saving the HTML with the DOM.
@@ -1959,7 +1959,7 @@ class BBCode
 	 * @param string $addon The addon for which the abstract is meant for
 	 * @return string The abstract
 	 */
-	public static function getAbstract($text, $addon = "")
+	private static function getAbstract($text, $addon = "")
 	{
 		$abstract = "";
 		$abstracts = [];
diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php
index 2269bffcfd..5d26e663c0 100644
--- a/src/Protocol/DFRN.php
+++ b/src/Protocol/DFRN.php
@@ -10,6 +10,7 @@ namespace Friendica\Protocol;
 
 use Friendica\App;
 use Friendica\Content\OEmbed;
+use Friendica\Content\Text\BBCode;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
@@ -29,8 +30,6 @@ use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
-use Friendica\Content\Text\BBCode;
-
 use dba;
 use DOMDocument;
 use DOMXPath;
@@ -931,7 +930,7 @@ class DFRN
 		}
 
 		// Remove the abstract element. It is only locally important.
-		$body = remove_abstract($body);
+		$body = BBCode::removeAbstract($body);
 
 		if ($type == 'html') {
 			$htmlbody = $body;
diff --git a/view/theme/frio/theme.php b/view/theme/frio/theme.php
index 3c4d5120cf..3f401612e2 100644
--- a/view/theme/frio/theme.php
+++ b/view/theme/frio/theme.php
@@ -6,7 +6,9 @@
  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
  *
  */
+
 use Friendica\App;
+use Friendica\Content\Text\Plaintext;
 use Friendica\Content\Widget;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
@@ -15,7 +17,6 @@ use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Database\DBM;
 use Friendica\Model\Profile;
-use Friendica\Object\Image;
 
 $frio = "view/theme/frio";
 
@@ -90,10 +91,8 @@ function frio_uninstall()
  */
 function frio_item_photo_links(App $a, &$body_info)
 {
-	$phototypes = Image::supportedTypes();
-	$occurence = 1;
-	$p = bb_find_open_close($body_info['html'], "<a", ">");
-
+	$occurence = 0;
+	$p = Plaintext::getBoundariesPosition($body_info['html'], "<a", ">");
 	while ($p !== false && ($occurence++ < 500)) {
 		$link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
 		$matches = [];
@@ -112,7 +111,7 @@ function frio_item_photo_links(App $a, &$body_info)
 			$body_info['html'] = str_replace($link, $newlink, $body_info['html']);
 		}
 
-		$p = bb_find_open_close($body_info['html'], "<a", ">", $occurence);
+		$p = Plaintext::getBoundariesPosition($body_info['html'], "<a", ">", $occurence);
 	}
 }
 
@@ -302,7 +301,7 @@ function frio_acl_lookup(App $a, &$results)
 		$search_txt = dbesc(protect_sprintf(preg_quote($results["search"])));
 		$searching = true;
 	}
-	
+
 	$sql_extra = '';
 	if ($searching) {
 		$sql_extra .= " AND (`attag` LIKE '%%" . dbesc($search_txt) . "%%' OR `name` LIKE '%%" . dbesc($search_txt) . "%%' OR `nick` LIKE '%%" . dbesc($search_txt) . "%%') ";
diff --git a/view/theme/frost/theme.php b/view/theme/frost/theme.php
index 1692560341..dd1a0cebaf 100644
--- a/view/theme/frost/theme.php
+++ b/view/theme/frost/theme.php
@@ -10,9 +10,9 @@
  */
 
 use Friendica\App;
+use Friendica\Content\Text\Plaintext;
 use Friendica\Core\Addon;
 use Friendica\Core\System;
-use Friendica\Object\Image;
 
 function frost_init(App $a) {
 	$a->videowidth = 400;
@@ -49,10 +49,8 @@ function frost_uninstall() {
 
 function frost_item_photo_links(App $a, &$body_info)
 {
-	$phototypes = Image::supportedTypes();
-
-	$occurence = 1;
-	$p = bb_find_open_close($body_info['html'], "<a", ">");
+	$occurence = 0;
+	$p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>');
 	while($p !== false && ($occurence++ < 500)) {
 		$link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
 
@@ -73,7 +71,7 @@ function frost_item_photo_links(App $a, &$body_info)
 
 		}
 
-		$p = bb_find_open_close($body_info['html'], "<a", ">", $occurence);
+		$p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>', $occurence);
 	}
 }