You are here

class Local in Flysystem 8

Same name and namespace in other branches
  1. 7 src/Flysystem/Local.php \Drupal\flysystem\Flysystem\Local
  2. 3.x src/Flysystem/Local.php \Drupal\flysystem\Flysystem\Local
  3. 2.0.x src/Flysystem/Local.php \Drupal\flysystem\Flysystem\Local
  4. 3.0.x src/Flysystem/Local.php \Drupal\flysystem\Flysystem\Local

Drupal plugin for the "Local" Flysystem adapter.

Plugin annotation


@Adapter(id = "local")

Hierarchy

Expanded class hierarchy of Local

1 file declares its use of Local
LocalTest.php in tests/src/Unit/Flysystem/LocalTest.php

File

src/Flysystem/Local.php, line 21

Namespace

Drupal\flysystem\Flysystem
View source
class Local implements FlysystemPluginInterface, ContainerFactoryPluginInterface {
  use FlysystemUrlTrait {
    getExternalUrl as getDownloadlUrl;
  }

  /**
   * The permissions to create directories with.
   *
   * @var int
   */
  protected $directoryPerm;

  /**
   * Whether the root is in the public path.
   *
   * @var bool
   */
  protected $isPublic;

  /**
   * The root of the local adapter.
   *
   * @var string
   */
  protected $root;

  /**
   * Whether the root exists and is readable.
   *
   * @var bool
   */
  protected $rootExists;

  /**
   * Constructs a Local object.
   *
   * @param string $root
   *   The of the adapter's filesystem.
   * @param bool $is_public
   *   (optional) Whether this is a public file system. Defaults to false.
   * @param int $directory_permission
   *   (optional) The permissions to create directories with.
   */
  public function __construct($root, $is_public = FALSE, $directory_permission = FileSystem::CHMOD_DIRECTORY) {
    $this->isPublic = $is_public;
    $this->root = $root;
    $this->directoryPerm = $directory_permission;
    $this->rootExists = $this
      ->ensureDirectory();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration['root'], !empty($configuration['public']), $container
      ->get('settings')
      ->get('file_chmod_directory', FileSystem::CHMOD_DIRECTORY));
  }

  /**
   * {@inheritdoc}
   */
  public function getAdapter() {
    return $this->rootExists ? new LocalAdapter($this->root) : new MissingAdapter();
  }

  /**
   * {@inheritdoc}
   */
  public function getExternalUrl($uri) {
    if ($this->isPublic === FALSE) {
      return $this
        ->getDownloadlUrl($uri);
    }
    $path = str_replace('\\', '/', $this->root . '/' . $this
      ->getTarget($uri));
    return $GLOBALS['base_url'] . '/' . UrlHelper::encodePath($path);
  }

  /**
   * {@inheritdoc}
   */
  public function ensure($force = FALSE) {
    if (!$this->rootExists) {
      return [
        [
          'severity' => RfcLogLevel::ERROR,
          'message' => 'The %root directory either does not exist or is not readable and attempts to create it have failed.',
          'context' => [
            '%root' => $this->root,
          ],
        ],
      ];
    }
    if (!$this
      ->writeHtaccess($force)) {
      return [
        [
          'severity' => RfcLogLevel::ERROR,
          'message' => 'See <a href="@url">@url</a> for information about the recommended .htaccess file which should be added to the %directory directory to help protect against arbitrary code execution.',
          'context' => [
            '%directory' => $this->root,
            '@url' => 'https://www.drupal.org/SA-CORE-2013-003',
          ],
        ],
      ];
    }
    return [
      [
        'severity' => RfcLogLevel::INFO,
        'message' => 'The directory %root exists and is readable.',
        'context' => [
          '%root' => $this->root,
        ],
      ],
    ];
  }

  /**
   * Checks that the directory exists and is readable.
   *
   * This will attempt to create the directory if it doesn't exist.
   *
   * @return bool
   *   True on success, false on failure.
   */
  protected function ensureDirectory() {

    // Go for the success case first.
    if (is_dir($this->root) && is_readable($this->root)) {
      return TRUE;
    }
    if (!file_exists($this->root)) {
      mkdir($this->root, $this->directoryPerm, TRUE);
    }
    if (is_dir($this->root) && chmod($this->root, $this->directoryPerm)) {
      clearstatcache(TRUE, $this->root);
      $this
        ->writeHtaccess(TRUE);
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Writes an .htaccess file.
   *
   * @param bool $force
   *   Whether to overwrite an existing file.
   *
   * @return bool
   *   True on success, false on failure.
   */
  protected function writeHtaccess($force) {
    $htaccess_path = $this->root . '/.htaccess';
    if (file_exists($htaccess_path) && !$force) {

      // Short circuit if the .htaccess file already exists.
      return TRUE;
    }

    // Make file writable so that we can overwrite it.
    if (file_exists($htaccess_path)) {
      chmod($htaccess_path, 0666);
    }
    return @file_put_contents($htaccess_path, FileStorage::htaccessLines(!$this->isPublic)) && chmod($htaccess_path, 0444);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlysystemUrlTrait::getExternalUrl public function Returns a web accessible URL for the resource. Aliased as: getDownloadlUrl
FlysystemUrlTrait::getScheme protected function Returns the scheme from the internal URI.
FlysystemUrlTrait::getTarget protected function Returns the target file path of a URI.
Local::$directoryPerm protected property The permissions to create directories with.
Local::$isPublic protected property Whether the root is in the public path.
Local::$root protected property The root of the local adapter.
Local::$rootExists protected property Whether the root exists and is readable.
Local::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Local::ensure public function Checks the sanity of the filesystem. Overrides FlysystemPluginInterface::ensure
Local::ensureDirectory protected function Checks that the directory exists and is readable.
Local::getAdapter public function Returns the Flysystem adapter. Overrides FlysystemPluginInterface::getAdapter
Local::getExternalUrl public function Returns a web accessible URL for the resource. Overrides FlysystemPluginInterface::getExternalUrl
Local::writeHtaccess protected function Writes an .htaccess file.
Local::__construct public function Constructs a Local object.