# user-access-manager/2.3.13/src/File/NginxFileProtection.php

User Access Manager, version 2.3.13. 71 lines.

- Page: https://pluginprobe.com/plugins/user-access-manager/2.3.13/code/src/File/NginxFileProtection.php
- Raw: https://pluginprobe.com/plugins/user-access-manager/2.3.13/raw/src/File/NginxFileProtection.php
- Modified: 2026-07-14T06:51:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/user-access-manager/2.3.13/code/src/File/NginxFileProtection.php#L10-L20`.

```php
<?php

declare(strict_types=1);

namespace UserAccessManager\File;

use Exception;
use UserAccessManager\Object\ObjectHandler;

class NginxFileProtection extends FileProtection implements FileProtectionInterface
{
    public const FILE_NAME = 'uam.conf';

    protected function getLocation(string $directory): string
    {
        if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') {
            return "^$directory" . $this->getDirectoryMatch();
        }

        $directoryMatch = $this->getDirectoryMatch();
        return $directoryMatch === null ? $directory : $directoryMatch;
    }

    private function getFileContent(string $absolutePath, string $directory, ?string $objectType): string
    {
        if ($objectType === null) {
            $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE;
        }

        $location = $this->getLocation(str_replace($absolutePath, '/', $directory));

        $content = "location ~ \"$location\" {\n";
        $content .= "rewrite ^([^?]*)$ /index.php?uamfiletype=$objectType&uamgetfile=$1 last;\n";
        $content .= "rewrite ^(.*)\\?(((?!uamfiletype).)*)$ ";
        $content .= "/index.php?uamfiletype=$objectType&uamgetfile=$1&$2 last;\n";
        $content .= "rewrite ^(.*)\\?(.*)$ /index.php?uamgetfile=$1&$2 last;\n";
        $content .= "}\n";

        return $content;
    }

    public function getFileNameWithPath(?string $directory = null): string
    {
        return ABSPATH . self::FILE_NAME;
    }

    public function create(string $directory, ?string $objectType = null, ?string $absolutePath = ABSPATH): bool
    {
        $directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        $content = $this->getFileContent($absolutePath, $directory, $objectType);

        // save files
        $fileWithPath = $absolutePath . self::FILE_NAME;

        try {
            return file_put_contents($fileWithPath, $content) !== false;
        } catch (Exception) {
            // Because file_put_contents can throw exceptions, we use this try catch block
            // to return the success result instead of an exception
        }

        return false;
    }

    public function delete(string $directory): bool
    {
        return $this->deleteFiles($directory);
    }
}

```
