From 3f1d186106139c7fb1dfcf79e87ca4851c748880 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sun, 1 Mar 2015 20:40:38 +0100
Subject: [PATCH] Remove the old hashtag functions, change it to the new
 functionality.

---
 include/diaspora.php | 83 +-------------------------------------------
 include/items.php    | 11 +++++-
 mod/item.php         | 42 +++++++---------------
 3 files changed, 23 insertions(+), 113 deletions(-)

diff --git a/include/diaspora.php b/include/diaspora.php
index d464b5d679..2c19cd4c48 100755
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -833,32 +833,6 @@ function diaspora_post($importer,$xml,$msg) {
 
 	$str_tags = '';
 
-	$tags = get_tags($body);
-	rsort($tags);
-
-	if(count($tags)) {
-		foreach($tags as $tag) {
-			if(strpos($tag,'#') === 0) {
-				if(strpos($tag,'[url='))
-					continue;
-
-				// don't link tags that are already embedded in links
-
-				if(preg_match('/\[(\S*?)' . preg_quote($tag,'/') . '(\S*?)\]/',$body))
-					continue;
-				if(preg_match('/\[(\S*?)\]\((\S*?)' . preg_quote($tag,'/') . '(\S*?)\)/',$body))
-					continue;
-
-				$basetag = str_replace('_',' ',substr($tag,1));
-				$body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
-				if(strlen($str_tags))
-					$str_tags .= ',';
-				$str_tags .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-				continue;
-			}
-		}
-	}
-
 	$cnt = preg_match_all('/@\[url=(.*?)\[\/url\]/ism',$body,$matches,PREG_SET_ORDER);
 	if($cnt) {
 		foreach($matches as $mtch) {
@@ -1055,34 +1029,8 @@ function diaspora_fetch_message($guid, $server, $level = 0) {
 		return false;
 
 	$item["tag"] = '';
-
-	$tags = get_tags($body);
-
-	if(count($tags)) {
-		foreach($tags as $tag) {
-			if(strpos($tag,'#') === 0) {
-				if(strpos($tag,'[url='))
-					continue;
-
-				// don't link tags that are already embedded in links
-
-				if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
-					continue;
-				if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
-					continue;
-
-
-				$basetag = str_replace('_',' ',substr($tag,1));
-				$body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
-				if(strlen($item["tag"]))
-					$item["tag"] .= ',';
-				$item["tag"] .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-				continue;
-			}
-		}
-	}
-
 	$item["body"] = $body;
+
 	return $item;
 }
 
@@ -1482,34 +1430,6 @@ function diaspora_comment($importer,$xml,$msg) {
 
 	$datarray = array();
 
-	$str_tags = '';
-
-	$tags = get_tags($body);
-
-	if(count($tags)) {
-		foreach($tags as $tag) {
-			if(strpos($tag,'#') === 0) {
-				if(strpos($tag,'[url='))
-					continue;
-
-				// don't link tags that are already embedded in links
-
-				if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
-					continue;
-				if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
-					continue;
-
-
-				$basetag = str_replace('_',' ',substr($tag,1));
-				$body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
-				if(strlen($str_tags))
-					$str_tags .= ',';
-				$str_tags .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-				continue;
-			}
-		}
-	}
-
 	$datarray['uid'] = $importer['uid'];
 	$datarray['contact-id'] = $contact['id'];
 	$datarray['type'] = 'remote-comment';
@@ -1533,7 +1453,6 @@ function diaspora_comment($importer,$xml,$msg) {
 	$datarray['author-link'] = $person['url'];
 	$datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
 	$datarray['body'] = $body;
-	$datarray['tag'] = $str_tags;
 
 	// We can't be certain what the original app is if the message is relayed.
 	if(($parent_item['origin']) && (! $parent_author_signature))
diff --git a/include/items.php b/include/items.php
index faeea1eafe..e56b17aec2 100644
--- a/include/items.php
+++ b/include/items.php
@@ -1610,7 +1610,16 @@ function item_body_set_hashtags(&$item) {
 			continue;
 
 		$basetag = str_replace('_',' ',substr($tag,1));
-		$item["body"] = str_replace($tag,'#[url='.$a->get_baseurl().'/search?tag='.rawurlencode($basetag).']'.$basetag.'[/url]', $item["body"]);
+
+		$newtag = '#[url='.$a->get_baseurl().'/search?tag='.rawurlencode($basetag).']'.$basetag.'[/url]';
+
+		$item["body"] = str_replace($tag, $newtag, $item["body"]);
+
+		if(!stristr($item["tag"],"/search?tag=".$basetag."]".$basetag."[/url]")) {
+			if(strlen($item["tag"]))
+				$item["tag"] = ','.$item["tag"];
+			$item["tag"] = $newtag.$item["tag"];
+		}
 	}
 
 	// Convert back the masked hashtags
diff --git a/mod/item.php b/mod/item.php
index a3a8dd938e..c5fc4ff2c3 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -23,6 +23,7 @@ require_once('include/tags.php');
 require_once('include/files.php');
 require_once('include/threads.php');
 require_once('include/text.php');
+require_once('include/items.php');
 
 function item_post(&$a) {
 
@@ -34,7 +35,6 @@ function item_post(&$a) {
 	$uid = local_user();
 
 	if(x($_REQUEST,'dropitems')) {
-		require_once('include/items.php');
 		$arr_drop = explode(',',$_REQUEST['dropitems']);
 		drop_items($arr_drop);
 		$json = array('success' => 1);
@@ -569,7 +569,7 @@ function item_post(&$a) {
 	 * and we are replying, and there isn't one already
 	 */
 
-	if(($parent_contact) && ($parent_contact['network'] === NETWORK_OSTATUS) 
+	if(($parent_contact) && ($parent_contact['network'] === NETWORK_OSTATUS)
 		&& ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
 		$body = '@' . $parent_contact['nick'] . ' ' . $body;
 		$tags[] = '@' . $parent_contact['nick'];
@@ -582,6 +582,9 @@ function item_post(&$a) {
 	if(count($tags)) {
 		foreach($tags as $tag) {
 
+			if(strpos($tag,'#') === 0)
+				continue;
+
 			// If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
 			// Robert Johnson should be first in the $tags array
 
@@ -713,6 +716,9 @@ function item_post(&$a) {
 	if($orig_post)
 		$datarray['edit']      = true;
 
+	// Search for hashtags
+	item_body_set_hashtags($datarray);
+
 	// preview mode - prepare the body for display and send it via json
 
 	if($preview) {
@@ -1035,10 +1041,9 @@ function item_content(&$a) {
 
 	$o = '';
 	if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
-		require_once('include/items.php'); 
 		$o = drop_item($a->argv[2], !is_ajax());
 		if (is_ajax()){
-			// ajax return: [<item id>, 0 (no perm) | <owner id>] 
+			// ajax return: [<item id>, 0 (no perm) | <owner id>]
 			echo json_encode(array(intval($a->argv[2]), intval($o)));
 			killme();
 		}
@@ -1047,9 +1052,9 @@ function item_content(&$a) {
 }
 
 /**
- * This function removes the tag $tag from the text $body and replaces it with 
- * the appropiate link. 
- * 
+ * This function removes the tag $tag from the text $body and replaces it with
+ * the appropiate link.
+ *
  * @param unknown_type $body the text to replace the tag in
  * @param unknown_type $inform a comma-seperated string containing everybody to inform
  * @param unknown_type $str_tags string to add the tag to
@@ -1063,29 +1068,6 @@ function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $netwo
 	$replaced = false;
 	$r = null;
 
-	//is it a hash tag?
-	if(strpos($tag,'#') === 0) {
-		//if the tag is replaced...
-		if(strpos($tag,'[url='))
-			//...do nothing
-			return $replaced;
-		//base tag has the tags name only
-		$basetag = str_replace('_',' ',substr($tag,1));
-		//create text for link
-		$newtag = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-		//replace tag by the link
-		$body = str_replace($tag, $newtag, $body);
-		$replaced = true;
-
-		//is the link already in str_tags?
-		if(! stristr($str_tags,$newtag)) {
-			//append or set str_tags
-			if(strlen($str_tags))
-				$str_tags .= ',';
-			$str_tags .= $newtag;
-		}
-		return $replaced;
-	}
 	//is it a person tag?
 	if(strpos($tag,'@') === 0) {
 		//is it already replaced?