Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - set $root_path and $root_url from $_SERVER['REQUEST_URI'] #1251

Open
devnull4242 opened this issue Nov 15, 2024 · 0 comments

Comments

@devnull4242
Copy link

devnull4242 commented Nov 15, 2024

Tiny File Manager is very cool. Thanks.

Unfortunately, in many cases it is necessary to adjust the variables $root_path and $root_url within the code. This is due to the use of subdirectories. Even calling the same file https://domain.tld/subdomain/tinyfilemanager.php or https://subdomain.domain.tld/tinymfilemanager.php leads to side effects and therefore to security problems under certain circumstances.

I would therefore like to suggest automatically extending the values of $root_path and $root_url via $_SERVER['REQUEST_URI'] in such a way that it works for directories and subdirectories as well as for rewrites from e.g. tinyfilemanager.php to /admin. I think than it works in the most cases without modifications in the code.

function extractBasePath($requestUri) {
    $path = preg_replace('/[?#].*$/', '', $requestUri);
    $path = urldecode($path);
    $path = '/' . trim($path, '/');
    $path = preg_replace('|/+|', '/', $path);
    $lastSlashPosition = strrpos($path, '/');
    
    if ($lastSlashPosition === false || $lastSlashPosition === 0) {
        return '/';
    }
    return substr($path, 0, $lastSlashPosition);
}

$base_path = extractBasePath($_SERVER['REQUEST_URI']);

$base_path is the folder /path or /path1/path2 ... where the phpfilemanager.php is located. From the point of view of PHP File Manager it is /. If phpfilemanager.php is located in a subdirectory and you want to access directories above it, you would have to set $base_path once manually.
$base_path='';

// Root path for file manager
// use absolute path of directory i.e: '/var/www/folder' or $_SERVER['DOCUMENT_ROOT'].'/folder'
//make sure update $root_url in next section
$root_path = $_SERVER['DOCUMENT_ROOT'] . $base_path;

// Root url for links in file manager.Relative to $http_host. Variants: '', 'path/to/subfolder'
// Will not working if $root_path will be outside of server document root
$root_url = $base_path;

You could also dispense with $base_path in the implementation and just use the variable $root_url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants