Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/File/HtaccessWriter.php
- 9 core/lib/Drupal/Core/File/HtaccessWriter.php
Namespace
Drupal\Core\File
File
core/lib/Drupal/Core/File/HtaccessWriter.php
View source
<?php
namespace Drupal\Core\File;
use Drupal\Component\FileSecurity\FileSecurity;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Psr\Log\LoggerInterface;
class HtaccessWriter implements HtaccessWriterInterface {
protected $streamWrapperManager;
protected $logger;
public function __construct(LoggerInterface $logger, StreamWrapperManagerInterface $stream_wrapper_manager) {
$this->logger = $logger;
$this->streamWrapperManager = $stream_wrapper_manager;
}
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) {
$this
->write($staging, TRUE);
}
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
}
}
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. 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;
}
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://');
$public_path = Settings::get('file_public_path', 'sites/default/files');
$assets_path = Settings::get('file_assets_path', $public_path);
if ($assets_path !== $public_path) {
$protected_dirs[] = new ProtectedDirectory('Optimized assets directory', $assets_path);
}
return $protected_dirs;
}
}
Classes