Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 1.8.1: media directory bug if using symlink

I am having trouble with my Magento install, in the CMS when I go to insert an image with the wysiwig editor the folder keeps opening repeatedly.

The folder structure should be:

- infortis
    - brands
    - fortis
    - ultimo

But what I get is:

-infortis
    -infortis
        -infortis
            -infortis
                -infortis

And this just keeps repeating.

Magento Version 1.8.1. Any help appreciated.

like image 208
ConquestXD Avatar asked Jan 29 '14 15:01

ConquestXD


2 Answers

I have found that the following edits makes it work as expected, and works with non-symlinked (dev) resources as well:

In the same class as mentiond Mage_Cms_Helper_Wysiwyg_Images, apply these patches:

# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- <html>Images.php (<b>Today 4:14:50 PM</b>)</html>
+++ <html><b>Current File</b></html>
@@ -223,7 +223,7 @@
     public function getCurrentUrl()
     {
         if (!$this->_currentUrl) {
-            $path = str_replace(Mage::getConfig()->getOptions()->getMediaDir(), '', $this->getCurrentPath());
+            $path = str_replace(realpath(Mage::getConfig()->getOptions()->getMediaDir()), '', $this->getCurrentPath());
             $path = trim($path, DS);
             $this->_currentUrl = Mage::app()->getStore($this->_storeId)->getBaseUrl('media') .
                                  $this->convertPathToUrl($path) . '/';



# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- <html>Images.php (<b>f47f0ff</b>)</html>
+++ <html><b>Current File</b></html>
@@ -68,7 +68,7 @@
      */
     public function getStorageRoot()
     {
-        return Mage::getConfig()->getOptions()->getMediaDir() . DS . Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY
+        return realpath(Mage::getConfig()->getOptions()->getMediaDir()) . DS . Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY
             . DS;
     }
like image 161
proxiblue Avatar answered Sep 17 '22 19:09

proxiblue


Found the issue in Mage_Cms_Helper_Wysiwyg_Images::convertIdToPath

The core code is as follows.

public function convertIdToPath($id)
{
    $path = $this->idDecode($id);
    if (!strstr($path, $this->getStorageRoot())) {
        $path = $this->getStorageRoot() . $path;
    }
    return $path;
}

And the fix is to use realpath when getting the storage root as follows.

public function convertIdToPath($id)
{
    $path = $this->idDecode($id);
    $realpath = $this->getStorageRoot();
    if (is_link(rtrim($realpath,'/'))) {
        $realpath = realpath($realpath);
    }
    if (!strstr($path, $realpath)) {
        $path = $realpath . $path;
    }
    return $path;
}

So what we have done is to rewrite Mage_Cms_Helper_Wysiwyg_Images and use the updated converIdToPath function. I found the original solution on a German website, but that will break if say you have a dev system without links and another system with a link.

like image 22
dmanners Avatar answered Sep 20 '22 19:09

dmanners