writeHtaccess example


  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() {
    

class FileSecurityTest extends TestCase {

  /** * @covers ::writeHtaccess */
  public function testWriteHtaccessPrivate() {
    vfsStream::setup('root');
    FileSecurity::writeHtaccess(vfsStream::url('root'));
    $htaccess_file = vfsStream::url('root') . '/.htaccess';
    $this->assertFileExists($htaccess_file);
    $this->assertEquals('0444', substr(sprintf('%o', fileperms($htaccess_file)), -4));
    $htaccess_contents = file_get_contents($htaccess_file);
    $this->assertStringContainsString("Require all denied", $htaccess_contents);
  }

  /** * @covers ::writeHtaccess */
  public function testWriteHtaccessPublic() {
    

  protected function ensureDirectory($directory$mode = 0777) {
    if ($this->createDirectory($directory$mode)) {
      FileSecurity::writeHtaccess($directory);
    }
  }

  /** * Ensures the requested directory exists and has the right permissions. * * For compatibility with open_basedir, the requested directory is created * using a recursion logic that is based on the relative directory path/tree: * It works from the end of the path recursively back towards the root * directory, until an existing parent directory is found. From there, the * subdirectories are created. * * @param string $directory * The directory path. * @param int $mode * The mode, permissions, the directory should have. * * @return bool * TRUE if the directory exists or has been created, FALSE otherwise. */


  /** * Place .htaccess and web.config files into the vendor directory. * * @param string $vendor_dir * Path to vendor directory. */
  public function writeAccessRestrictionFiles($vendor_dir) {
    $this->io->writeError('<info>Hardening vendor directory with .htaccess and web.config files.</info>');
    // Prevent access to vendor directory on Apache servers.     FileSecurity::writeHtaccess($vendor_dir, TRUE);

    // Prevent access to vendor directory on IIS servers.     FileSecurity::writeWebConfig($vendor_dir);
  }

}
return 'yml';
  }

  /** * Check if the directory exists and create it if not. */
  protected function ensureStorage() {
    $dir = $this->getCollectionDirectory();
    $success = $this->getFileSystem()->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    // Only create .htaccess file in root directory.     if ($dir == $this->directory) {
      $success = $success && FileSecurity::writeHtaccess($this->directory);
    }
    if (!$success) {
      throw new StorageException('Failed to create config directory ' . $dir);
    }
    return $this;
  }

  /** * {@inheritdoc} */
  public function exists($name) {
    
Home | Imprint | This part of the site doesn't use cookies.