Jako/AjaxUpload

Upload not working for anonymous users in multiple contexts installations

Closed this issue · 7 comments

Actually I am not sure if this is a bug, or if I am missing some configurations here.

I have a MODX installation with multiple contexts. Uploading is not working for anonymous users. (controller simply returns nothing, empty response) Once I authenticated in manager uploading is working. I tried to debug this issue using the controller.php file and imho the problem is in line 28:

$modx->user->getUserToken($modx->context->get('key'));

This is returning empty when used with multiple contexts whereas in single context installations this is returning a token. As I said before, I am not sure if this is a bug or a misconfiguration of my contexts, but I was able to reproduce this in two different installations both using multiple contexts.

MODX advanced installation 2.7.3
PHP 7.4

Jako commented

AjaxUpload needs a session. Could that be the issue?

I also considered a problem with a non-existing session, but the anonymous_sessions setting is set to true globally. I am using a gateway plugin and nginx.

Okay, this is very very strange. When the upload fails one time and then I switch the language/context via BabelLinks the upload is working afterwards. Maybe my gateway plugin is the issue, but this is the one from the tutorials...

Attached to OnMODXInit event:

<?php
if($modx->context->get('key') != "mgr"){
    /* grab the current language from the cultureKey request var */
    switch ($_REQUEST['cultureKey']) {
        case 'de':
            /* switch the context */
            $modx->switchContext('web');
            break;
        case 'us':
            /* switch the context */
            $modx->switchContext('us');
            break;
        default:
            /* Set the default context here */
            $modx->switchContext('intl');
            break;
    }
    /* unset GET var to avoid
     * appending cultureKey=xy to URLs by other components */
    unset($_GET['cultureKey']);
}

Okay, found the issue. I had three contexts with the following base_urls:

  • /de/
  • /en/
  • /intl/

Turns out I either had to add an empty context with base_url set to / or change the /de/ context to /. Then it is working...

Jako commented

Ok. You have to add a rule in .htaccess, that rewrites everything in assets/ with your context prefixes to assets/

# redirect all requests from /de/assets*, /us/assets* etc. to /assets*
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(de|us|intl)/assets(.*)$ assets$2 [L,QSA]

POST requests can't be redirected, they have to sent directly to the target.

Correct...that was the problem. I now changed my /de/ base_url to / and everything works as expected.

POST requests can't be redirected, they have to sent directly to the target.

So the problem is/was that no context was available under / in my first setup and that's why the post request was failing. So the solution is to either keep the default web context and use a redirect to the language contexts or set the default language context to / as base_url.

Jako commented

The issue is caused by the base href tag, that is including the context/language key. So everything is accessed with this key and would be handled inside of MODX with the index.php instead of using the connector in the worst case.