class HtaccessWriter

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/File/HtaccessWriter.php \Drupal\Core\File\HtaccessWriter
  2. 10 core/lib/Drupal/Core/File/HtaccessWriter.php \Drupal\Core\File\HtaccessWriter
  3. 11.x core/lib/Drupal/Core/File/HtaccessWriter.php \Drupal\Core\File\HtaccessWriter

Provides functions to manage Apache .htaccess files.

Hierarchy

Expanded class hierarchy of HtaccessWriter

1 string reference to 'HtaccessWriter'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses HtaccessWriter
file.htaccess_writer in core/core.services.yml
Drupal\Core\File\HtaccessWriter

File

core/lib/Drupal/Core/File/HtaccessWriter.php, line 15

Namespace

Drupal\Core\File
View source
class HtaccessWriter implements HtaccessWriterInterface {
    
    /**
     * The stream wrapper manager.
     *
     * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
     */
    protected $streamWrapperManager;
    
    /**
     * The logger.
     *
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;
    
    /**
     * Htaccess constructor.
     *
     * @param \Psr\Log\LoggerInterface $logger
     *   The logger.
     * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
     *   The stream wrapper manager.
     */
    public function __construct(LoggerInterface $logger, StreamWrapperManagerInterface $stream_wrapper_manager) {
        $this->logger = $logger;
        $this->streamWrapperManager = $stream_wrapper_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public function ensure() {
        try {
            foreach ($this->defaultProtectedDirs() as $protected_dir) {
                $this->write($protected_dir->getPath(), $protected_dir->isPrivate());
            }
            $staging = Settings::get('config_sync_directory', FALSE);
            if ($staging) {
                // Note that we log an error here if we can't write the .htaccess file.
                // This can occur if the staging directory is read-only. If it is then
                // it is the user's responsibility to create the .htaccess file.
                $this->write($staging, TRUE);
            }
        } catch (\Exception $e) {
            $this->logger
                ->error($e->getMessage());
        }
    }
    
    /**
     * Creates a .htaccess file in the given directory.
     *
     * @param string $directory
     *   The directory.
     * @param bool $deny_public_access
     *   (Optional) FALSE indicates that $directory should be a web-accessible
     *   directory. Defaults to TRUE which indicates a private directory.
     * @param bool $force_overwrite
     *   (Optional) Set to TRUE to attempt to overwrite the existing .htaccess
     *   file if one is already present. Defaults to FALSE.
     *
     * @internal
     *
     * @return bool
     *   TRUE if the .htaccess file was saved or already exists, FALSE otherwise.
     *
     * @see \Drupal\Component\FileSecurity\FileSecurity::writeHtaccess()
     */
    public function write($directory, $deny_public_access = TRUE, $force_overwrite = FALSE) {
        if (StreamWrapperManager::getScheme($directory)) {
            $directory = $this->streamWrapperManager
                ->normalizeUri($directory);
        }
        else {
            $directory = rtrim($directory, '/\\');
        }
        if (FileSecurity::writeHtaccess($directory, $deny_public_access, $force_overwrite)) {
            return TRUE;
        }
        $this->logger
            ->error("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <pre><code>@htaccess</code></pre>", [
            '%directory' => $directory,
            '@htaccess' => FileSecurity::htaccessLines($deny_public_access),
        ]);
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function defaultProtectedDirs() {
        $protected_dirs[] = new ProtectedDirectory('Public files directory', 'public://');
        if (PrivateStream::basePath()) {
            $protected_dirs[] = new ProtectedDirectory('Private files directory', 'private://', TRUE);
        }
        $protected_dirs[] = new ProtectedDirectory('Temporary files directory', 'temporary://');
        return $protected_dirs;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
HtaccessWriter::$logger protected property The logger.
HtaccessWriter::$streamWrapperManager protected property The stream wrapper manager.
HtaccessWriter::defaultProtectedDirs public function Returns a list of the default protected directories. Overrides HtaccessWriterInterface::defaultProtectedDirs
HtaccessWriter::ensure public function Creates a .htaccess file in each Drupal files directory if it is missing. Overrides HtaccessWriterInterface::ensure
HtaccessWriter::write public function Creates a .htaccess file in the given directory.
HtaccessWriter::__construct public function Htaccess constructor.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.