You are here

elfinder.drupal.inc in elFinder file manager 7.3

Same filename and directory in other branches
  1. 6.2 inc/elfinder.drupal.inc
  2. 7.2 inc/elfinder.drupal.inc

elFinder conenctor class

File

inc/elfinder.drupal.inc
View source
<?php

/**
 * @file
 *
 * elFinder conenctor class
 */
class elFinderDrupal extends elFinder {

  // Run the parent constructor and register 'desc' as a new command
  public function __construct($opts) {
    parent::__construct($opts);
    $this->commands['desc'] = array(
      'target' => TRUE,
      'content' => FALSE,
    );
    $this->commands['owner'] = array(
      'target' => TRUE,
      'content' => FALSE,
    );

    //$this->commands['downloadcount'] = array('target' => TRUE, 'content' => FALSE);
  }

  /**
   * Return command required arguments info.
   *
   * @param  string  command name
   * @return array
   */
  public function commandArgsList($cmd) {

    // Override search query argument 'q' since it's already used in Drupal
    $this->commands['search']['elfinder_search_q'] = TRUE;
    return $this
      ->commandExists($cmd) ? $this->commands[$cmd] : array();
  }

  /**
   * Search files
   *
   * @param  array  $args  command arguments
   * @return array
   */
  protected function search($args) {
    $q = trim($args['elfinder_search_q']);
    $mimes = !empty($args['mimes']) && is_array($args['mimes']) ? $args['mimes'] : array();
    $target = !empty($args['target']) ? $args['target'] : null;
    $result = array();
    $errors = array();
    if ($target) {
      if ($volume = $this
        ->volume($target)) {
        $result = $volume
          ->search($q, $mimes, $target);
        $errors = array_merge($errors, $volume
          ->error());
      }
    }
    else {
      foreach ($this->volumes as $volume) {
        $result = array_merge($result, $volume
          ->search($q, $mimes));
        $errors = array_merge($errors, $volume
          ->error());
      }
    }

    // Exclude hidden folders from search results.
    $hidden_folders = variable_get('elfinder_settings_misc_hidden_folders', '');
    if ($hidden_folders) {
      $hidden_folders = explode(',', $hidden_folders);
      foreach ($result as $key => $file) {
        $parts = explode('/', $file['path']);
        if (in_array($parts[1], $hidden_folders)) {
          unset($result[$key]);
        }
      }
    }
    $result = array(
      'files' => $result,
    );
    if ($errors) {
      $result['warning'] = $errors;
    }
    return $result;
  }
  protected function desc($args) {
    $target = $args['target'];
    $desc = $args['content'];
    $error = array(
      self::ERROR_UNKNOWN,
      '#' . $target,
    );
    if (($volume = $this
      ->volume($target)) == FALSE || ($file = $volume
      ->file($target)) == FALSE) {
      return array(
        'error' => $this
          ->error($error, self::ERROR_FILE_NOT_FOUND),
      );
    }
    $error[1] = $file['name'];
    if ($volume
      ->driverId() == 'f') {
      return array(
        'desc' => '',
      );
    }
    if ($volume
      ->commandDisabled('desc')) {
      return array(
        'error' => $this
          ->error($error, self::ERROR_ACCESS_DENIED),
      );
    }
    if (($desc = $volume
      ->desc($target, $desc)) == -1) {
      return array(
        'error' => $this
          ->error($error, $volume
          ->error()),
      );
    }
    return array(
      'desc' => $desc,
    );
  }
  protected function owner($args) {
    $target = $args['target'];
    $error = array(
      self::ERROR_UNKNOWN,
      '#' . $target,
    );
    if (($volume = $this
      ->volume($target)) == FALSE || ($file = $volume
      ->file($target)) == FALSE) {
      return array(
        'error' => $this
          ->error($error, self::ERROR_FILE_NOT_FOUND),
      );
    }
    $error[1] = $file['name'];
    if ($volume
      ->driverId() == 'f') {
      return array(
        'owner' => '',
      );
    }
    if ($volume
      ->commandDisabled('owner')) {
      return array(
        'error' => $this
          ->error($error, self::ERROR_ACCESS_DENIED),
      );
    }
    if (($owner = $volume
      ->owner($target)) == FALSE) {
      return array(
        'error' => $this
          ->error($error, $volume
          ->error()),
      );
    }
    return array(
      'owner' => $owner,
    );
  }

  // Incomplete backport from D8. Not in use (yet)
  protected function downloadcount($args) {
    $target = $args['target'];
    $error = array(
      self::ERROR_UNKNOWN,
      '#' . $target,
    );
    if (($volume = $this
      ->volume($target)) == FALSE || ($file = $volume
      ->file($target)) == FALSE) {
      return array(
        'error' => $this
          ->error($error, self::ERROR_FILE_NOT_FOUND),
      );
    }
    $error[1] = $file['name'];
    if ($volume
      ->driverId() == 'f') {
      return array(
        'downloadcount' => '',
      );
    }
    if ($volume
      ->commandDisabled('downloadcount')) {
      return array(
        'error' => $this
          ->error($error, self::ERROR_ACCESS_DENIED),
      );
    }
    if (($downloadcount = $volume
      ->downloadcount($target)) == FALSE) {
      return array(
        'error' => $this
          ->error($error, $volume
          ->error()),
      );
    }
    return array(
      'downloadcount' => $downloadcount,
    );
  }

  /**
   * Required to output file in browser when volume URL is not set
   * Return array contains opened file pointer, root itself and required headers
   *
   * @param  array  command arguments
   * @return array
   * @author Dmitry (dio) Levashov
   * */
  protected function file($args) {
    $target = $args['target'];
    $download = !empty($args['download']);
    $h403 = 'HTTP/1.x 403 Access Denied';
    $h404 = 'HTTP/1.x 404 Not Found';
    if (($volume = $this
      ->volume($target)) == FALSE) {
      return array(
        'error' => self::$errors[self::ERROR_FILE_NOT_FOUND],
        'header' => $h404,
        'raw' => TRUE,
      );
    }
    if (($file = $volume
      ->file($target)) == FALSE) {
      return array(
        'error' => self::$errors[self::ERROR_FILE_NOT_FOUND],
        'header' => $h404,
        'raw' => TRUE,
      );
    }
    if (!$file['read']) {
      return array(
        'error' => self::$errors[self::ERROR_ACCESS_DENIED],
        'header' => $h403,
        'raw' => TRUE,
      );
    }
    if ($volume
      ->driverId() != 'f' && ($fp = $volume
      ->open($target)) == FALSE) {
      return array(
        'error' => self::$errors[self::ERROR_FILE_NOT_FOUND],
        'header' => $h404,
        'raw' => TRUE,
      );
    }
    $mime = $download ? 'application/octet-stream' : $file['mime'];
    $result = array(
      'volume' => $volume,
      'pointer' => $fp,
      'info' => $file,
      'header' => array(
        "Content-Type: " . $mime,
        "Content-Disposition: " . $this
          ->GetContentDisposition($file['name'], $mime, $download),
        "Content-Location: " . $file['name'],
        'Content-Transfer-Encoding: binary',
        "Content-Length: " . $file['size'],
        "Connection: close",
      ),
    );
    $real_path = $this
      ->realpath($target);
    module_invoke_all('file_download', $volume
      ->drupalpathtouri($real_path));
    return $result;
  }

  /**
   * Generating Content-Disposition HTTP header
   *
   * @param  string  $file     Filename
   * @param  string  $filemime MIME Type
   * @param  bool    $download Disposition type (true = download file, false = open file in browser)
   * @return string
   * @author Dmitry (dio) Levashov, Alexey Sukhotin
   * */
  public static function GetContentDisposition($file, $filemime, $download = FALSE) {
    $disp = '';
    $filename = $file;
    $ua = $_SERVER["HTTP_USER_AGENT"];
    $mime = $filemime;
    if ($download) {
      $disp = 'attachment';
      $mime = 'application/octet-stream';
    }
    else {
      $disp = preg_match('/^(image|text)/i', $mime) || $mime == 'application/x-shockwave-flash' ? 'inline' : 'attachment';
    }
    $disp .= '; ';
    if (preg_match("/MSIE ([0-9]{1,}[\\.0-9]{0,})/", $ua)) {
      $filename = rawurlencode($filename);
      $filename = str_replace("+", "%20", $filename);

      //$filename = str_replace(" ", "%20", $filename);
      $disp .= "filename=" . $filename;
    }
    elseif (preg_match("/Firefox\\/(\\d+)/", $ua, $m)) {
      if ($m[1] >= 8) {
        $disp .= "filename*=?UTF-8''" . rawurlencode($filename);
      }
      else {
        $disp .= "filename*=\"?UTF-8''" . rawurlencode($filename) . "\";";
      }
    }
    else {
      $disp .= "filename=" . $filename;
    }
    return $disp;
  }

}

Classes

Namesort descending Description
elFinderDrupal @file