You are here

function elfinder_connector in elFinder file manager 6.2

Same name and namespace in other branches
  1. 6 elfinder.module \elfinder_connector()

File browser to filesystem php connector callback

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

File

./elfinder.module, line 473

Code

function elfinder_connector($mode = 'default') {
  global $base_root, $user;
  $p = elfinder_get_user_profile();
  $path = drupal_get_path('module', 'elfinder');

  /**
   * 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');
    }
  }
  include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderConnector.class.php';
  include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderVolumeDriver.class.php';
  include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderVolumeLocalFileSystem.class.php';
  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';
  if (is_readable(dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderPlugin.php')) {
    include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'elFinderPlugin.php';
  }
  if (is_readable(dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'editors' . DIRECTORY_SEPARATOR . 'editor.php')) {
    include_once dirname(elfinder_connector_path()) . DIRECTORY_SEPARATOR . 'editors' . DIRECTORY_SEPARATOR . 'editor.php';
  }
  $disabled_commands = elfinder_get_disabled_commands();
  $acl = new elFinderDrupalACL();
  $roots = array();
  if ($p) {
    $profile = $p->settings;
  }
  else {
    $profile = array();
  }
  $options_defs = array(
    'disabled' => $disabled_commands,
    // list of not allowed commands
    'debug' => FALSE,
    'dirSize' => FALSE,
    'tmbSize' => isset($profile['tmbsize']) ? $profile['tmbsize'] : variable_get('elfinder_settings_thumbnail_size', '48'),
    // thumbnail image size
    'tmbPath' => elfinder_file_directory_path(TRUE) . DIRECTORY_SEPARATOR . variable_get('elfinder_settings_thumbnail_dirname', '.tmb'),
    // thumbnail images directory (tmbPath in 2.x)
    'tmbURL' => elfinder_file_directory_url() . '/' . variable_get('elfinder_settings_thumbnail_dirname', '.tmb') . '/',
    // thumbnail images directory url
    '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,
  );

  /*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' ? '/' . $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 (user_access('access private files')) {
    $roots[] = array_merge($options_defs, array(
      'driver' => 'Drupal',
      '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_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 ($volume['path'][0] != DIRECTORY_SEPARATOR || strpos($volume['path'], elfinder_file_directory_path(TRUE) == 0)) {
        $is_subdir = true;
      }
      if ($is_subdir) {
        $root['driver'] = 'Drupal';
        $rootpath = $volume['path'][0];
        $url = elfinder_file_directory_url();
        if ($volume['path'][0] != DIRECTORY_SEPARATOR) {
          $rootpath = elfinder_file_directory_path(TRUE) . DIRECTORY_SEPARATOR . ltrim($volume['path'], './');
        }
        $rootpath = elfinder_parse_path_tokens($rootpath);
        if (!file_check_directory($rootpath, FILE_CREATE_DIRECTORY)) {
          drupal_set_message(t('Error. Cannot initialize directory %dir', array(
            '%dir' => $rootpath,
          )), 'error');
        }
        $root['path'] = $rootpath;
        $drupalfiles = file_directory_path();
        $drupalpprefix = substr($rootpath, strpos($rootpath, $drupalfiles) + strlen($drupalfiles) + 1);
        $root['URL'] = elfinder_file_directory_url($mode == 'relative' ? TRUE : FALSE) . $drupalpprefix;
      }
      else {
        $root['driver'] = 'LocalFileSystem';
        $root['path'] = $volume['path'];
      }
      $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(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;
  }
}