Jump to content

Custom Upload Names


adrian

Recommended Posts

@PWaddict - it might also be useful to see what these return. Some are repeats from before, but I would like to see all of these please.

$filename = '"Test ä".png';
$pageFiles = new Pagefiles($page);
d($pageFiles->cleanBasename($filename));
d($pageFiles->cleanBasename($filename, false, true, true));
d($sanitizer->filename($filename, Sanitizer::translate));
d($sanitizer->pageName($filename, Sanitizer::translate));

image.png.9c26f034a93d342bab47c33ffd5ff43a.png

Link to comment
Share on other sites

  On 12/20/2018 at 6:10 AM, adrian said:

@PWaddict - it might also be useful to see what these return. Some are repeats from before, but I would like to see all of these please.

$filename = '"Test ä".png';
$pageFiles = new Pagefiles($page);
d($pageFiles->cleanBasename($filename));
d($pageFiles->cleanBasename($filename, false, true, true));
d($sanitizer->filename($filename, Sanitizer::translate));
d($sanitizer->pageName($filename, Sanitizer::translate));

 

Expand  

I've just tested it:

tracy.jpg.4ed4bc007b4aa0e0ebb9bffbfe8c0e70.jpg

Link to comment
Share on other sites

  On 12/20/2018 at 10:45 AM, matjazp said:

I get the same results as Adrian. Are there any hooks in the game? Tried on fresh install?

Expand  

Yes I have hooks but those same hooks are on live server too. Btw I switched from XAMPP to Laragon (I wanted to do that for quite some time) and the problem remains. I will try later with a fresh install.

  On 12/20/2018 at 10:45 AM, matjazp said:

Could you also d(basename($filename));

Expand  

Here it is:

Untitled-1.jpg.24ff162c55a331c5fd9e1b6c59f0feec.jpg

Link to comment
Share on other sites

  On 12/20/2018 at 3:50 AM, adrian said:

I don't have access to a Windows machine dev environment at the moment. It might be related I suppose. I would have thought this issue would have come up before though because the cleanBasename method is what PW uses when uploading images, which is the reason I'd like to stay with this approach if I can, rather than the old pageName I was using before. 

In terms of how you could debug this, in that block of code I linked to, I'd want to know that mb_strpos, mb_eregi_replace and iconv calls are returning the same values on the Windows box that they are on linux. 

I would try this:

        if($beautify && $needsWork) {
            if($beautify === self::translate && $this->multibyteSupport) {
                $value = mb_strtolower($value);

                if(empty($replacements)) {
                    $configData = $this->wire('modules')->getModuleConfigData('InputfieldPageName');
                    $replacements = empty($configData['replacements']) ? InputfieldPageName::$defaultReplacements : $configData['replacements'];
                }

                foreach($replacements as $from => $to) {
                    if(mb_strpos($value, $from) !== false) {
                        bd($from .' => ' . $to, $value);
                        $value = mb_eregi_replace($from, $to, $value);
                        bd($value);
                    }
                }
            }

            if(function_exists("\\iconv")) {
                $v = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value);
                if($v) {
                    bd($value, 'ICONV');
                    $value = $v;
                }
            }
            $needsWork = strlen(str_replace($allowed, '', $value));
        }

 

Expand  

After I replace that code on Sanitizer.php what code should I input on Tracy console?

Link to comment
Share on other sites

  On 12/20/2018 at 11:21 AM, PWaddict said:

After I replace that code on Sanitizer.php what code should I input on Tracy console?

Expand  

The code block I posted doesn't change any behavior - it just adds bd() calls so that will can see what is going on in that function.

 

  On 12/20/2018 at 1:38 PM, PWaddict said:

On the fresh PW installation sometimes doesn't work. I have to reload Apache on Laragon to get proper result on:

d($pageFiles->cleanBasename($filename, false, true, true));

 

Expand  

Once you restart does it continue to work until you do something specific or does it seem to randomly stop working?

Link to comment
Share on other sites

  On 12/20/2018 at 1:38 PM, PWaddict said:

On the fresh PW installation sometimes doesn't work. I have to reload Apache on Laragon to get proper result on:

Expand  

Now that's odd. Please do:

d(pathinfo($filename));

on both affected instalations.

@adrian off topic: when I do bd() call in Sanitizer.php in the name() method (line 245) I get:

Fatal error: Uncaught Error: Call to undefined function ProcessWire\bd() in C:\inetpub\wwwroot\wire\core\Sanitizer.php:246

Link to comment
Share on other sites

  On 12/20/2018 at 5:27 PM, matjazp said:

@adrian off topic: when I do bd() call in Sanitizer.php in the name() method (line 245) I get:

Fatal error: Uncaught Error: Call to undefined function ProcessWire\bd() in C:\inetpub\wwwroot\wire\core\Sanitizer.php:246

Expand  

Yeah, it depends on when the bd() call is made. Tracy is now always the first site module loaded by PW (thanks to a recent update by Ryan), but when it comes to the PW core and core modules I don't have any control over that - sometimes you'll have success and other times you'll get that error. For me though they have been working in those spots in Sanitizer.php. I guess the name() method is being called by the core at some point before Tracy is loaded which is why it's not working for you?

Link to comment
Share on other sites

  On 12/20/2018 at 3:35 PM, adrian said:

Once you restart does it continue to work until you do something specific or does it seem to randomly stop working?

Expand  

I didn't noticed a specific pattern.

 

  On 12/20/2018 at 5:27 PM, matjazp said:

Now that's odd. Please do:

d(pathinfo($filename));

on both affected instalations.

Expand  

On both installations I'm getting the same results:

dirname => "."
basename => ""Test ä".png" (13)
extension => "png" (3)
filename => ""Test ä"" (9)

 

Link to comment
Share on other sites

@PWaddict - did you have any success with that modified code in Sanitizer.php - did the bd() calls work?

Another thing to try would be to comment out that iconv code completely:

           /* if(function_exists("\\iconv")) {
                $v = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value);
                if($v) {
                    $value = $v;
                }
            }*/

so we can see what is returned without it getting involved.

Link to comment
Share on other sites

  On 12/20/2018 at 7:21 PM, adrian said:

did you have any success with that modified code in Sanitizer.php - did the bd() calls work?

Expand  

I just tried that and I'm getting this on Dumps (redirect):

ICONV
"test ?_1545258287" (17)
in C:\laragon\www\mysite\wire\core\Sanitizer.php:188

 

  On 12/20/2018 at 7:21 PM, adrian said:

Another thing to try would be to comment out that iconv code completely:

           /* if(function_exists("\\iconv")) {
                $v = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value);
                if($v) {
                    $value = $v;
                }
            }*/

so we can see what is returned without it getting involved.

Expand  

I comment it out that part but I don't know how to view the result. 

Link to comment
Share on other sites

  On 12/20/2018 at 7:33 PM, PWaddict said:

I just tried that and I'm getting this on Dumps (redirect):

ICONV
"test ?_1545258287" (17)
in C:\laragon\www\mysite\wire\core\Sanitizer.php:188

 

Expand  

If that is the only thing that is being dumped, then it sounds like there is an issue with the: 

foreach($replacements as $from => $to) {

loop because there are a couple of bd() calls in there also. It would be helpful to figure out what is failing there. This should help:

        if($beautify && $needsWork) {
            bd(1);
            if($beautify === self::translate && $this->multibyteSupport) {
                bd(2);
                $value = mb_strtolower($value);

                if(empty($replacements)) {
                    bd(3);
                    $configData = $this->wire('modules')->getModuleConfigData('InputfieldPageName');
                    $replacements = empty($configData['replacements']) ? InputfieldPageName::$defaultReplacements : $configData['replacements'];
                }

                foreach($replacements as $from => $to) {
                    bd(4);
                    if(mb_strpos($value, $from) !== false) {
                        bd(5);
                        bd($from .' => ' . $to, $value);
                        $value = mb_eregi_replace($from, $to, $value);
                        bd($value);
                    }
                }
            }

            if(function_exists("\\iconv")) {
                $v = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value);
                if($v) {
                    bd($value, 'ICONV');
                    $value = $v;
                }
            }
            $needsWork = strlen(str_replace($allowed, '', $value));
        }

Can you try that and let me know which of the 1, 2, 3, 4, 5 bd() calls are output?

 

  On 12/20/2018 at 7:33 PM, PWaddict said:

I comment it out that part but I don't know how to view the result. 

Expand  

We are looking to see if commenting that out changes the returned filename, so you could test an actual file upload or you could run that cleanBasename test in the Console again. Either will be fine.

Link to comment
Share on other sites

  On 12/20/2018 at 7:53 PM, adrian said:

Can you try that and let me know which of the 1, 2, 3, 4, 5 bd() calls are output?

Expand  

This is what I get:

  Reveal hidden contents

 

  On 12/20/2018 at 7:53 PM, adrian said:

We are looking to see if commenting that out changes the returned filename, so you could test an actual file upload or you could run that cleanBasename test in the Console again. Either will be fine.

Expand  

I'm still getting the same results from cleanBasename.

Link to comment
Share on other sites

OK, so it's pretty clear that the:

if(mb_strpos($value, $from) !== false) {

is failing.When I do:

$filename = '"Test ä".png';
$pageFiles = new Pagefiles($page);
d($pageFiles->cleanBasename($filename, false, true, true));

I get a dump returned showing:

image.png.e4da82e7dadffff532ad154d0d75b85f.png

This is because it is finding the ä in the filename and doing a mb_eregi_replace on it to convert from "ä" to "a".

Does it make any difference if you change "mb_strpos" to "strpos" ? 

What about removing that conditional altogether so that all replacements are executed regardless of whether the $from is found. So it would be like this - note that I have commented out the conditional.

                foreach($replacements as $from => $to) {
                    //if(mb_strpos($value, $from) !== false) {
                        bd($from .' => ' . $to, $value);
                        $value = mb_eregi_replace($from, $to, $value);
                    //}
                }

 

Link to comment
Share on other sites

  On 12/20/2018 at 8:20 PM, adrian said:

Does it make any difference if you change "mb_strpos" to "strpos" ? 

 

Expand  

No

  On 12/20/2018 at 8:20 PM, adrian said:

What about removing that conditional altogether so that all replacements are executed regardless of whether the $from is found.

Expand  

Here are the results:

  Reveal hidden contents

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...