You are here

function elfinder_filesystemconnector in elFinder file manager 7.3

Same name and namespace in other branches
  1. 7 elfinder.module \elfinder_filesystemconnector()
  2. 7.2 elfinder.module \elfinder_filesystemconnector()

File browser to filesystem php connector callback

1 string reference to 'elfinder_filesystemconnector'
elfinder_menu in ./elfinder.module
Implements hook_menu().

File

./elfinder.module, line 344

Code

function elfinder_filesystemconnector($mode = 'default') {
  global $base_root, $user;
  $p = elfinder_get_user_profile();
  $path = drupal_get_path('module', 'elfinder');
  include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'autoload.php';
  if (class_exists('elFinder')) {
    include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.drupal.inc';
    include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.drupalfs.driver.inc';
    include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.drupalfs.acl.inc';
    include_once $path . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'elfinder.admin.profiles.inc';
  }

  //Emulate json_encode required by filesystem connector for old php without JSON support.
  if (!function_exists('json_encode')) {
    function json_encode($arg) {
      return drupal_json($arg);
    }
  }

  // Disabling incompartible devel module features.
  if (module_exists('devel')) {
    $devel_incompat = array(
      'dev_timer',
      'devel_query_display',
      'dev_mem',
      'devel_redirect_page',
    );
    $flag = FALSE;
    foreach ($devel_incompat as $di) {
      if (variable_get($di, 0)) {
        $flag = TRUE;
        variable_set($di, 0);
      }
    }
    if ($flag) {
      drupal_set_message(t('elFinder disabled incompatible devel module features.'), 'warning');
    }
  }

  // Set absolute/relative url variable.
  if (!isset($mode) && variable_get('elfinder_settings_filesystem_fileurl', 'true') == 'false') {
    $mode = 'relative';
  }
  $disabled_commands = elfinder_get_disabled_commands();
  $acl = new elFinderDrupalACL();
  $pubFiles = file_stream_wrapper_get_instance_by_uri('public://');
  $pvtFiles = file_stream_wrapper_get_instance_by_uri('private://');
  $roots = array();
  if ($p) {
    $profile = $p->settings;
  }
  else {
    $profile = array();
  }
  $options_defs = array(
    'disabled' => $disabled_commands,
    // list of not allowed commands
    'debug' => TRUE,
    'dirSize' => FALSE,
    'tmbSize' => isset($profile['tmbsize']) ? $profile['tmbsize'] : variable_get('elfinder_settings_thumbnail_size', '48'),
    // thumbnail image size
    'tmbPath' => variable_get('elfinder_settings_thumbnail_dirname', 'tmb'),
    // thumbnail images directory (tmbPath in 2.x)
    'tmbCrop' => variable_get('elfinder_settings_thumbnail_tmbcrop', 'true') == 'true' ? TRUE : FALSE,
    // crop thumbnail image
    'dateFormat' => variable_get('date_format_long', 'j M Y H:i'),
    // file modification date format
    'mimeDetect' => isset($profile['mime_detect']) ? $profile['mime_detect'] : variable_get('elfinder_settings_filesystem_mimedetect', 'auto'),
    // file type detection method
    'imgLib' => isset($profile['imglib']) ? $profile['imglib'] : variable_get('elfinder_settings_thumbnail_imglib', 'auto'),
    // image manipulation library
    'fileMode' => isset($profile['file_perm']) ? octdec($profile['file_perm']) : octdec(variable_get('elfinder_settings_filesystem_fileperm', '0666')),
    // created file permissions
    'dirMode' => isset($profile['dir_perm']) ? octdec($profile['dir_perm']) : octdec(variable_get('elfinder_settings_filesystem_dirperm', '0777')),
    // created directory permissions
    'accessControlData' => array(
      'uid' => $user->uid,
    ),
    'acceptedName' => '/^[^\\.]+/',
    'uploadMaxSize' => parse_size(variable_get('elfinder_settings_filesystem_maxfilesize', '')),
    'userProfile' => $p,
    'jpgQuality' => variable_get('image_jpeg_quality', 75),
  );
  if (is_object($pubFiles) && user_access('access public files')) {
    $roots[] = array_merge($options_defs, array(
      'driver' => 'Drupal',
      'path' => drupal_realpath('public://'),
      // path to root directory (named 'path' in elFinder 2.0)
      'URL' => $mode == 'relative' ? base_path() . $pubFiles
        ->getDirectoryPath() : $pubFiles
        ->getExternalUrl(),
      // root directory URL
      'alias' => variable_get('elfinder_settings_filesystem_public_root_label', '') != '' ? variable_get('elfinder_settings_filesystem_public_root_label', '') : t('Public Files'),
      // display this instead of root directory name (named 'alias' in elFinder 2.0)
      'accessControl' => array(
        $acl,
        'fsAccessPublic',
      ),
    ));
  }
  if (is_object($pvtFiles) && user_access('access private files')) {
    $roots[] = array_merge($options_defs, array(
      'driver' => 'Drupal',
      'path' => drupal_realpath('private://'),
      // path to root directory (named 'path' in elFinder 2.0)
      'URL' => $mode == 'relative' ? base_path() . $pvtFiles
        ->getDirectoryPath() : $pvtFiles
        ->getExternalUrl(),
      // root directory URL
      'alias' => variable_get('elfinder_settings_filesystem_private_root_label', '') != '' ? variable_get('elfinder_settings_filesystem_private_root_label', '') : t('Private Files'),
      // display this instead of root directory name (named 'alias' in elFinder 2.0)
      'accessControl' => array(
        $acl,
        'fsAccessPrivate',
      ),
    ));
  }
  if (user_access('access unmanaged files')) {
    $roots[] = array_merge($options_defs, array(
      'driver' => 'LocalFileSystem',
      'path' => elfinder_file_directory_path(TRUE),
      // path to root directory (named 'path' in elFinder 2.0)
      'URL' => elfinder_file_directory_url($mode == 'relative' ? TRUE : FALSE),
      // root directory URL
      'alias' => variable_get('elfinder_settings_filesystem_unmanaged_root_label', '') != '' ? variable_get('elfinder_settings_filesystem_unmanaged_root_label', '') : t('Unmanaged Files'),
      // display this instead of root directory name (named 'alias' in elFinder 2.0)
      'accessControl' => array(
        $acl,
        'fsAccessUnmanaged',
      ),
    ));
  }
  if (isset($profile['volume'])) {
    foreach ($profile['volume'] as $volume) {
      $root = array(
        'alias' => $volume['label'],
      );
      $is_subdir = FALSE;
      $rootpath = '';
      if (isset($volume['path']) && substr($volume['path'], 0, 1) != DIRECTORY_SEPARATOR) {
        $is_subdir = TRUE;
      }
      if ($is_subdir) {
        $root['driver'] = 'Drupal';
        $scheme = file_uri_scheme($volume['path']);
        if ($scheme == FALSE) {
          if (is_object(file_stream_wrapper_get_instance_by_uri('private://'))) {
            $scheme = 'private';
          }
          else {
            $scheme = 'public';
          }
        }
        $rootpath = $volume['path'];
        if ($pos = strpos($rootpath, '://')) {
          $rootpath = substr($rootpath, $pos + 3);
        }
        $streamWrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
        if (is_object($streamWrapper)) {
          $volpath = $streamWrapper
            ->realpath();
          $volurl = $mode == 'relative' ? base_path() . $streamWrapper
            ->getDirectoryPath() : $streamWrapper
            ->getExternalUrl();
          $url = isset($volume['url']) && $volume['url'] != '' ? elfinder_parse_path_tokens($volume['url']) : $volurl;
        }
        $rootpath = elfinder_parse_path_tokens($rootpath);
        $trimmedpath = ltrim($rootpath, './');
        $rootpath = $volpath . DIRECTORY_SEPARATOR . $trimmedpath;
        $url = rtrim($url, '/');
        $url .= "/{$trimmedpath}";
        if (!file_prepare_directory($rootpath, FILE_CREATE_DIRECTORY)) {
          drupal_set_message(t('Error. Cannot initialize directory %dir', array(
            '%dir' => $rootpath,
          )), 'error');
        }
        $root['path'] = drupal_realpath($rootpath);
        $root['URL'] = $url;
        $root['tmbPath'] = $volpath . DIRECTORY_SEPARATOR . variable_get('elfinder_settings_thumbnail_dirname', 'tmb');
        $root['tmbURL'] = $volurl . '/' . variable_get('elfinder_settings_thumbnail_dirname', 'tmb');
      }
      else {
        $rootpath = elfinder_parse_path_tokens($volume['path']);
        $root['driver'] = 'LocalFileSystem';
        $root['path'] = $rootpath;
        if (isset($volume['url'])) {
          $root['URL'] = elfinder_parse_path_tokens($volume['url']);
        }
      }
      $root = array_merge($options_defs, $root);
      $roots[] = $root;
    }
  }
  $opts = array(
    'roots' => $roots,
    'driver_defaults' => $options_defs,
  );
  $newopts = module_invoke_all('elfinder_connector_config', $opts);
  if ($newopts) {
    $opts = $newopts;
  }
  class elFinderConnectorDrupal extends elFinderConnector {
    public function test() {
    }

  }
  try {
    $elFinderObj = new elFinderDrupal($opts);
  } catch (Exception $e) {
    module_invoke_all('exit');
    exit(drupal_json_encode(array(
      'error' => array(
        t('Unable to initialize elFinder object. :msg', array(
          ':msg' => basename($e
            ->getFile()) . ": " . $e
            ->getMessage(),
        )),
      ),
    )));
  }
  $bindcmds = module_invoke_all('elfinder_bind');
  foreach ($bindcmds as $cmd => $cmdfunc) {
    $elFinderObj
      ->bind($cmd, $cmdfunc);
  }
  try {
    $elFinderConnectorObj = new elFinderConnectorDrupal($elFinderObj);
  } catch (Exception $e) {
    module_invoke_all('exit');
    exit(drupal_json_encode(array(
      'error' => array(
        t('Unable to initialize elFinder connector object. :msg', array(
          ':msg' => basename($e
            ->getFile()) . ": " . $e
            ->getMessage(),
        )),
      ),
    )));
  }
  module_invoke_all('elfinder_connector_init', $elFinderConnectorObj);
  if (!drupal_valid_token($_REQUEST['token'], 'elFinder', TRUE)) {
    module_invoke_all('exit');
    exit(drupal_json_encode(array(
      'error' => array(
        t('Access denied'),
      ),
    )));
  }
  else {
    try {
      $elFinderConnectorObj
        ->run();
    } catch (Exception $e) {
      module_invoke_all('exit');
      exit(drupal_json_encode(array(
        'error' => array(
          t('Unable to run elFinder connector. :msg', array(
            ':msg' => basename($e
              ->getFile()) . ": " . $e
              ->getMessage(),
          )),
        ),
      )));
    }
    module_invoke_all('exit');
    exit;
  }
}