From 5fd8fdc92b42ca96318f475e8b47e08012302b5b Mon Sep 17 00:00:00 2001
From: Cameron <e107inc@gmail.com>
Date: Sun, 26 Sep 2021 10:43:33 -0700
Subject: [PATCH] Fixed e107 v1 to v2 forum migration adding check for
 deprecated files. Fixed breadcrumb subforum visibility and URL when SEFURL is
 disabled.

---
 e107_plugins/forum/forum_class.php     |  3 +-
 e107_plugins/forum/forum_update.php    | 44 +++++++++++++++++++++++---
 e107_plugins/forum/forum_viewforum.php |  2 +-
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/e107_plugins/forum/forum_class.php b/e107_plugins/forum/forum_class.php
index fc9faa43d..2666f871b 100644
--- a/e107_plugins/forum/forum_class.php
+++ b/e107_plugins/forum/forum_class.php
@@ -2351,10 +2351,11 @@ class e107forum
 			$replace 	= array($tp->toHTML($forumInfo['parent_name']), e107::url('forum','index')."#".$frm->name2id($forumInfo['parent_name']));
 			$FORUM_CRUMB['parent']['value'] = str_replace($search, $replace, $FORUM_CRUMB['parent']['value']);
 
+
 			if($forumInfo['forum_sub'])
 			{
 				$search 	= array('{SUBPARENT_TITLE}', '{SUBPARENT_HREF}');
-				$replace 	= array(ltrim($forumInfo['sub_parent'], '*'), e107::url('forum', 'forum', array('forum_id'=>$forumInfo['parent_id'],'forum_sef'=>$forumInfo['parent_sef'])));
+				$replace 	= array(ltrim($forumInfo['sub_parent'], '*'), e107::url('forum', 'forum', array('forum_id'=>$forumInfo['forum_sub'],'forum_sef'=>$forumInfo['parent_sef'])));
 				$FORUM_CRUMB['subparent']['value'] = str_replace($search, $replace, $FORUM_CRUMB['subparent']['value']);
 			}
 			else
diff --git a/e107_plugins/forum/forum_update.php b/e107_plugins/forum/forum_update.php
index a0e4f8b1b..10cb2964a 100644
--- a/e107_plugins/forum/forum_update.php
+++ b/e107_plugins/forum/forum_update.php
@@ -1314,6 +1314,9 @@ class forumUpgrade
 	public $updateInfo = array();
 	private $attachmentData;
 	private $logf;
+	private $deprecated = array(
+				'e_latest.php','e_status.php', 'forum_post_shortcodes.php','forum_shortcodes.php',
+				'forum_update_check.php', 'newforumposts_menu_config.php');
 
 	public function __construct()
 	{
@@ -1322,6 +1325,34 @@ class forumUpgrade
 		$this->attachmentData = array();
 		$this->logf = e_LOG . 'forum_upgrade.log';
 		$this->getUpdateInfo();
+
+		if(empty($updateInfo))
+		{
+			$this->removeDeprecatedFiles();
+		}
+
+	}
+
+	private function removeDeprecatedFiles()
+	{
+		foreach($this->deprecated as $file)
+		{
+			$path = e_PLUGIN."forum/".$file;
+			if(file_exists($path))
+			{
+				if(unlink($path))
+				{
+					e107::getMessage()->addDebug("Deprecated file deleted: <b>".$path."</b>");
+				}
+				else
+				{
+					e107::getMessage()->addWarning("Was unable to delete the following deprecated file: <b>".$path."</b> (please remove manually)");
+				}
+
+			}
+		}
+
+
 	}
 
 	public function log($msg, $append = true)
@@ -1511,24 +1542,29 @@ class forumUpgrade
 		$thread['thread_user'] = $userInfo['user_id'];
 		$thread['thread_user_anon'] = $userInfo['anon_name'];
 
+		$sql = e107::getDb();
+
 		//  If thread marked as 'tracked by starter', we must convert to using
 		// forum_track table
 		if($thread['thread_active'] == 99 && $thread['thread_user'] > 0)
 		{
-			$forum->track('add', $thread['thread_user'], $thread['thread_id'], true);
+			if(!$forum->track('add', $thread['thread_user'], $thread['thread_id'], true))
+			{
+				$this->log(__METHOD__." (track): ".$sql->getLastErrorText(), true);
+			}
 			$thread['thread_active'] = 1;
 		}
 
 		//		$thread['_FIELD_TYPES'] = $forum->fieldTypes['forum_thread'];
 		//		$thread['_FIELD_TYPES']['thread_name'] = 'escape'; //use escape to prevent
 		// double entities
-		$sql = e107::getDb();
+
 
 		$result = $sql->insert('forum_thread', $thread);
 
 		if($result === false)
 		{
-			$this->log($sql->getLastErrorText(), true);
+			$this->log(__METHOD__.": ".$sql->getLastErrorText(), true);
 		}
 
 		return $result;
@@ -1584,7 +1620,7 @@ class forumUpgrade
 
 		if($result === false)
 		{
-			$this->log($sql->getLastErrorText(), true);
+			$this->log(__METHOD__.": ".$sql->getLastErrorText(), true);
 		}
 
 		return $result;
diff --git a/e107_plugins/forum/forum_viewforum.php b/e107_plugins/forum/forum_viewforum.php
index b6995042b..317b08292 100644
--- a/e107_plugins/forum/forum_viewforum.php
+++ b/e107_plugins/forum/forum_viewforum.php
@@ -67,7 +67,7 @@ function init()
 	// todo - remove all these globals.
 	global $FORUM_VIEW_START, $urlparms, $doNothing;
 	global $sc, $FORUM_VIEW_FORUM, $FORUM_VIEW_FORUM_STICKY, $FORUM_VIEW_FORUM_ANNOUNCE;
-	global $forum_info, $FORUM_CRUMB;
+	global $forum_info, $FORUM_CRUMB, $forumInfo;
 	global $forum;
 
 	$sql = e107::getDb();