From 58bb71535f6c66fc713eda70fae5a05163a1ca7a Mon Sep 17 00:00:00 2001
From: Aske Ertmann <aske@moc.net>
Date: Tue, 26 Nov 2013 13:35:54 +0100
Subject: [PATCH] [BUGFIX] Folder tree in popup throws JS error

When using the folder tree (file, and folder navigation) popup
(browse_links) a JS error occurs when opening new folders.

This is due to incorrect handling of scope.
This patch fixes the error for both RTE and normal link browser.

Change-Id: I25fa0871405e06d6fc084985b3beba6396b872ad
Resolves: #53826
Releases: 6.0, 6.1, 6.2
Reviewed-on: https://review.typo3.org/25633
Reviewed-by: Jan-Erik Revsbech
Tested-by: Jan-Erik Revsbech
Reviewed-by: Markus Klein
Tested-by: Markus Klein
---
 typo3/class.browse_links.php                  | 30 +------------------
 .../Classes/Tree/View/FolderTreeView.php      | 27 ++++++++++++-----
 .../sysext/rtehtmlarea/Classes/FolderTree.php | 29 ++++++++++++++++--
 3 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/typo3/class.browse_links.php b/typo3/class.browse_links.php
index 6161ccc311e3..cd6f9ed5c071 100644
--- a/typo3/class.browse_links.php
+++ b/typo3/class.browse_links.php
@@ -283,11 +283,6 @@ class TBE_FolderTree extends localFolderTree {
 	 */
 	public $ext_noTempRecyclerDirs = 0;
 
-	/**
-	 * @var array
-	 */
-	protected $scope;
-
 	/**
 	 * Returns TRUE if the input "record" contains a folder which can be linked.
 	 *
@@ -320,27 +315,4 @@ class TBE_FolderTree extends localFolderTree {
 		}
 	}
 
-	/**
-	 * Wrap the plus/minus icon in a link
-	 *
-	 * @param string $icon HTML string to wrap, probably an image tag.
-	 * @param string $cmd Command for 'PM' get var
-	 * @param boolean $isExpand Whether to be expanded
-	 * @return string Link-wrapped input string
-	 * @internal
-	 */
-	public function PMiconATagWrap($icon, $cmd, $isExpand = TRUE) {
-		$this->scope = array(
-			'class' => get_class($this),
-			'script' => $this->thisScript,
-			'ext_noTempRecyclerDirs' => $this->ext_noTempRecyclerDirs,
-			'browser' => array(
-				'mode' => $GLOBALS['SOBE']->browser->mode,
-				'act' => $GLOBALS['SOBE']->browser->act,
-			),
-		);
-
-		return parent::PMiconATagWrap($icon, $cmd, $isExpand);
-	}
-
-}
+}
\ No newline at end of file
diff --git a/typo3/sysext/backend/Classes/Tree/View/FolderTreeView.php b/typo3/sysext/backend/Classes/Tree/View/FolderTreeView.php
index 963ad6798791..f600e084b77e 100644
--- a/typo3/sysext/backend/Classes/Tree/View/FolderTreeView.php
+++ b/typo3/sysext/backend/Classes/Tree/View/FolderTreeView.php
@@ -60,6 +60,11 @@ class FolderTreeView extends \TYPO3\CMS\Backend\Tree\View\AbstractTreeView {
 	 */
 	protected $ajaxStatus = FALSE;
 
+	/**
+	 * @var array
+	 */
+	protected $scope;
+
 	/**
 	 * Constructor function of the class
 	 */
@@ -105,15 +110,23 @@ class FolderTreeView extends \TYPO3\CMS\Backend\Tree\View\AbstractTreeView {
 	 * @internal
 	 */
 	public function PMiconATagWrap($icon, $cmd, $isExpand = TRUE) {
+
+		if (empty($this->scope)) {
+			$this->scope = array(
+				'class' => get_class($this),
+				'script' => $this->thisScript,
+				'ext_noTempRecyclerDirs' => $this->ext_noTempRecyclerDirs,
+				'browser' => array(
+					'mode' => $GLOBALS['SOBE']->browser->mode,
+					'act' => $GLOBALS['SOBE']->browser->act,
+				),
+			);
+		}
+
 		if ($this->thisScript) {
 			// Activates dynamic AJAX based tree
-			$scopeData = '';
-			$scopeHash = '';
-			// $this->scope is defined in TBE_FolderTree
-			if (!empty($this->scope)) {
-				$scopeData = serialize($this->scope);
-				$scopeHash = GeneralUtility::hmac($scopeData);
-			}
+			$scopeData = serialize($this->scope);
+			$scopeHash = GeneralUtility::hmac($scopeData);
 			$js = htmlspecialchars('Tree.load(\'' . $cmd . '\', ' . intval($isExpand) . ', this, \'' . $scopeData . '\', \'' . $scopeHash . '\');');
 			return '<a class="pm" onclick="' . $js . '">' . $icon . '</a>';
 		} else {
diff --git a/typo3/sysext/rtehtmlarea/Classes/FolderTree.php b/typo3/sysext/rtehtmlarea/Classes/FolderTree.php
index e20db8978a0d..29a7411d3567 100644
--- a/typo3/sysext/rtehtmlarea/Classes/FolderTree.php
+++ b/typo3/sysext/rtehtmlarea/Classes/FolderTree.php
@@ -9,6 +9,15 @@ namespace TYPO3\CMS\Rtehtmlarea;
  */
 class FolderTree extends \rteFolderTree {
 
+	/**
+	 * Constructor function of the class
+	 */
+	public function __construct() {
+		// The backpath is set her to fix problems with relatives path when used in ajax scope
+		$GLOBALS['BACK_PATH'] = isset($GLOBALS['ajaxID']) ? '../../../' : $GLOBALS['BACK_PATH'];
+		parent::__construct();
+	}
+
 	/**
 	 * Wrapping the title in a link, if applicable.
 	 *
@@ -36,12 +45,28 @@ class FolderTree extends \rteFolderTree {
 	 * @access private
 	 */
 	public function PMiconATagWrap($icon, $cmd, $isExpand = TRUE) {
+
+		if (empty($this->scope)) {
+			$this->scope = array(
+				'class' => get_class($this),
+				'script' => $this->thisScript,
+				'ext_noTempRecyclerDirs' => $this->ext_noTempRecyclerDirs,
+				'browser' => array(
+					'mode' => $GLOBALS['SOBE']->browser->mode,
+					'act' => $GLOBALS['SOBE']->browser->act,
+				),
+			);
+		}
+
 		if ($this->thisScript) {
-			$js = htmlspecialchars('Tree.thisScript=\'' . $GLOBALS['BACK_PATH'] . 'ajax.php\',Tree.load(\'' . $cmd . '\', ' . intval($isExpand) . ', this);');
+			// Activates dynamic AJAX based tree
+			$scopeData = serialize($this->scope);
+			$scopeHash = \TYPO3\CMS\Core\Utility\GeneralUtility::hmac($scopeData);
+			$js = htmlspecialchars('Tree.thisScript=\'' . $this->backPath . 'ajax.php\',Tree.load(\'' . $cmd . '\', ' . intval($isExpand) . ', this, ' . json_encode($scopeData) . ', \'' . $scopeHash . '\');');
 			return '<a class="pm" onclick="' . $js . '">' . $icon . '</a>';
 		} else {
 			return $icon;
 		}
 	}
 
-}
+}
\ No newline at end of file
-- 
GitLab