added elfinder module

This commit is contained in:
2018-12-04 20:35:06 +01:00
parent 1b53514906
commit ccab226e12
223 changed files with 97254 additions and 0 deletions

View File

@@ -0,0 +1,401 @@
<?php
/**
* @file
* file manager admin settings page
*/
/**
* Settings form callback
*/
function elfinder_admin() {
$obj = drupal_get_form('elfinder_d7_admin_form');
return drupal_render($obj);
}
/**
* Settings form definition for D7
*/
function elfinder_d7_admin_form($form_id, &$form_state) {
return elfinder_admin_form($form_state);
}
function elfinder_admin_profile_links($profile_name) {
$links = l(t('Edit'), 'admin/config/media/elfinder/profile/' . $profile_name . '/edit') . ' ' . l(t('Delete'), 'admin/config/media/elfinder/profile/' . $profile_name . '/delete');
return $links;
}
/**
* Settings form definition
*/
function elfinder_admin_form(&$form_state) {
global $user, $language;
$path = drupal_get_path('module', 'elfinder');
// require_once $path .'/inc/' . 'elfinder.admin.profiles.inc';
$langCode = isset($language->language) ? $language->language : 'en';
$form['profiles'] = array(
'#type' => 'fieldset',
'#title' => t('Profiles'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$profiles = elfinder_admin_profile_get();
$roles = user_roles();
$rows = array();
if ($profiles) {
if (is_array($profiles)) {
foreach ($profiles as $profile) {
$rows[] = array($profile->pid, $profile->name, $profile->description, implode(', ', array_intersect_key($roles, $profile->settings['profile_role'])), elfinder_admin_profile_links($profile->name));
}
} else {
$profile = $profiles;
$rows[] = array($profile->pid, $profile->name, $profile->description, implode(', ', array_intersect_key($roles, $profile->settings['profile_role'])), elfinder_admin_profile_links($profile->name));
}
}
$profile_table = theme('table', array(
'header' => array(t('Id'), t('Name'), t('Description'), t('Role'), ''),
'rows' => $rows
));
$addprofile = l(t('Add profile'), 'admin/config/media/elfinder/profile');
$form['profiles']['profile_list'] = array(
'#type' => 'markup',
'#markup' => "$profile_table<br/>$addprofile",
);
$form['filesystem_settings'] = array(
'#type' => 'fieldset',
'#title' => t('File system settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['filesystem_settings']['filesystem_public_root_label'] = array(
'#prefix' => '<div class="custom-container">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Public files root directory label'),
'#default_value' => variable_get('elfinder_settings_filesystem_public_root_label', ''),
'#description' => t('Root directory label in directory tree'),
);
$form['filesystem_settings']['filesystem_private_root_label'] = array(
'#prefix' => '<div class="custom-container">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Private files root directory label'),
'#default_value' => variable_get('elfinder_settings_filesystem_private_root_label', ''),
'#description' => t('Root directory label in directory tree'),
);
$form['filesystem_settings']['filesystem_unmanaged_root_label'] = array(
'#prefix' => '<div class="custom-container">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Unmanaged files root directory label'),
'#default_value' => variable_get('elfinder_settings_filesystem_unmanaged_root_label', ''),
'#description' => t('Root directory label in directory tree'),
);
$form['filesystem_settings']['filesystem_root_custom'] = array(
'#prefix' => '<div class="custom-container">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Custom unmanaged files path'),
'#default_value' => variable_get('elfinder_settings_filesystem_root_custom', ''),
'#description' => t('Custom filesystem root path.') . '<br/>' . t('Available tokens: <code>%files</code> (base path, eg: <code>/</code>), <code>%name</code> (current username, eg: <code>@u</code>, <b>NOTE:</b> it is not unique - users can have same username, so better to combine it with user id value), <code>%uid</code> (current user id, eg: <code>@uid</code>), <code>%lang</code> (current language code, eg: <code>@lang</code>), plus all tokens provided by token module', array('@u' => $user->name, '@uid' => $user->uid, '@lang' => $langCode)),
);
$form['filesystem_settings']['filesystem_url_custom'] = array(
'#prefix' => '<div class="custom-container">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Custom unmanaged files URL'),
'#default_value' => variable_get('elfinder_settings_filesystem_url_custom', ''),
'#description' => t('Custom filesystem url.') . '<br/>' . t('Available tokens: <code>%files</code> (base path, eg: <code>/</code>), <code>%name</code> (current username, eg: <code>@u</code>, <b>NOTE:</b> it is not unique - users can have same username, so better to combine it with user id value), <code>%uid</code> (current user id, eg: <code>@uid</code>), <code>%lang</code> (current language code, eg: <code>@lang</code>), plus all tokens provided by token module', array('@u' => $user->name, '@uid' => $user->uid, '@lang' => $langCode)),
);
$form['filesystem_settings']['mime_detect'] = array(
'#type' => 'radios',
'#title' => t('File type detection'),
'#default_value' => variable_get('elfinder_settings_filesystem_mimedetect', 'auto'),
'#options' => array(
'auto' => t('Automatical detection'),
),
);
$form['filesystem_settings']['filesystem_allowed_extensions'] = array(
'#prefix' => '<div class="custom-container">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Allowed file extensions'),
'#maxlength' => 1000,
'#default_value' => variable_get('elfinder_settings_filesystem_allowed_extensions', ''),
'#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
);
if (function_exists('finfo_open')) {
$form['filesystem_settings']['mime_detect']['#options']['finfo'] = t('php finfo');
}
if (function_exists('mime_content_type')) {
$form['filesystem_settings']['mime_detect']['#options']['php'] = t('php mime_content_type()');
}
$form['filesystem_settings']['mime_detect']['#options']['linux'] = t('file -ib (linux)');
$form['filesystem_settings']['mime_detect']['#options']['bsd'] = t('file -Ib (bsd)');
$form['filesystem_settings']['mime_detect']['#options']['internal'] = t('By file extension (built-in)');
$form['filesystem_settings']['mime_detect']['#options']['drupal'] = t('Drupal API');
$form['filesystem_settings']['file_url_type'] = array(
'#type' => 'radios',
'#title' => t('Selected file url type'),
'#default_value' => variable_get('elfinder_settings_filesystem_fileurl', 'true'),
'#options' => array(
'true' => t('Absolute'),
'false' => t('Relative'),
),
);
$form['filesystem_settings']['file_perm'] = array(
'#type' => 'textfield',
'#title' => t('Created file permissions'),
'#default_value' => variable_get('elfinder_settings_filesystem_fileperm', '0666'),
'#size' => 4,
);
$form['filesystem_settings']['dir_perm'] = array(
'#type' => 'textfield',
'#title' => t('Created directory permissions'),
'#default_value' => variable_get('elfinder_settings_filesystem_dirperm', '0777'),
'#size' => 4,
);
$form['filesystem_settings']['max_filesize'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size'),
'#default_value' => variable_get('elfinder_settings_filesystem_maxfilesize', ''),
'#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', array('%limit' => format_size(file_upload_max_size()))),
'#size' => 10,
'#weight' => 5,
);
$form['filesystem_settings']['max_filecount'] = array(
'#type' => 'textfield',
'#title' => t('Maximum folder size'),
'#default_value' => variable_get('elfinder_settings_filesystem_maxfilecount', ''),
'#description' => t('The maximum number of files allowed in a directory. 0 for unlimited.'),
'#size' => 10,
'#weight' => 5,
);
$form['filesystem_settings']['handleprivate'] = array(
'#type' => 'radios',
'#title' => t('Handle private downloads'),
'#default_value' => variable_get('elfinder_settings_filesystem_handleprivate', 'true'),
'#options' => array(
'true' => t('Yes'),
'false' => t('No'),
),
'#description' => t('Use elFinder to handle private file downloads'),
);
$form['thumbnail_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Image settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['thumbnail_settings']['tmbsize'] = array(
'#type' => 'textfield',
'#title' => t('Thumbnail size'),
'#default_value' => variable_get('elfinder_settings_thumbnail_size', '48'),
'#size' => 4,
);
$form['thumbnail_settings']['tmbdirname'] = array(
'#type' => 'textfield',
'#title' => t('Thumbnail directory name'),
'#default_value' => variable_get('elfinder_settings_thumbnail_dirname', 'tmb'),
'#size' => 10,
);
$form['thumbnail_settings']['imglib'] = array(
'#type' => 'radios',
'#title' => t('Image manipulation library'),
'#default_value' => variable_get('elfinder_settings_thumbnail_imglib', 'auto'),
'#options' => array(
'auto' => t('Automatical detection'),
'imagick' => t('Image Magick'),
'gd' => t('GD'),
),
);
$form['thumbnail_settings']['tmbcrop'] = array(
'#type' => 'radios',
'#title' => t('Image crop'),
'#default_value' => variable_get('elfinder_settings_thumbnail_tmbcrop', 'true'),
'#options' => array(
'true' => t('Yes'),
'false' => t('No'),
),
'#description' => t('Crop image to fit thumbnail size. Yes - crop, No - scale image to fit thumbnail size.'),
);
$form['misc_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Miscellaneous settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['misc_settings']['rememberlastdir'] = array(
'#type' => 'radios',
'#title' => t('Remember last opened directory'),
'#default_value' => variable_get('elfinder_settings_misc_rememberlastdir', 'true'),
'#options' => array(
'true' => t('Yes'),
'false' => t('No'),
),
'#description' => t('Creates a cookie. Disable if you have issues with caching.'),
);
$form['misc_settings']['usesystemjquery'] = array(
'#type' => 'radios',
'#title' => t('Use system jQuery'),
'#default_value' => variable_get('elfinder_settings_misc_usesystemjquery', 'true'),
'#options' => array(
'true' => t('Yes'),
'false' => t('No'),
),
'#description' => t('Use system jQuery and jQuery UI when possible. If set to \'No\' jQuery hosted at Google will be uses.'),
);
$form['misc_settings']['manager_width'] = array(
'#type' => 'textfield',
'#title' => t('File manager width'),
'#default_value' => variable_get('elfinder_settings_misc_manager_width', ''),
'#size' => 4,
);
$form['misc_settings']['manager_height'] = array(
'#type' => 'textfield',
'#title' => t('File manager height'),
'#default_value' => variable_get('elfinder_settings_misc_manager_height', ''),
'#size' => 4,
);
$form['misc_settings']['hidden_folders'] = array(
'#type' => 'textfield',
'#title' => t('Hidden folders'),
'#maxlength' => 1000,
'#default_value' => variable_get('elfinder_settings_misc_hidden_folders', ''),
'#description' => t('Folders listed here will be hidden. Separate with a comma. Example: "color, css_injector, ctools, styles".'),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
$form['#submit'][] = 'elfinder_admin_submit';
return $form;
}
/**
* Save form data
*/
function elfinder_admin_submit($form, &$form_state) {
variable_set('elfinder_settings_thumbnail_size', $form_state['values']['tmbsize']);
variable_set('elfinder_settings_thumbnail_dirname', $form_state['values']['tmbdirname']);
if ($form_state['values']['filesystem_root_custom'] != '') {
variable_set('elfinder_settings_filesystem_root_custom', $form_state['values']['filesystem_root_custom']);
}
variable_set('elfinder_settings_filesystem_url_custom', $form_state['values']['filesystem_url_custom']);
variable_set('elfinder_settings_filesystem_mimedetect', $form_state['values']['mime_detect']);
variable_set('elfinder_settings_filesystem_fileurl', $form_state['values']['file_url_type']);
variable_set('elfinder_settings_thumbnail_imglib', $form_state['values']['imglib']);
variable_set('elfinder_settings_filesystem_fileperm', $form_state['values']['file_perm']);
variable_set('elfinder_settings_filesystem_dirperm', $form_state['values']['dir_perm']);
variable_set('elfinder_settings_misc_rememberlastdir', $form_state['values']['rememberlastdir']);
variable_set('elfinder_settings_misc_usesystemjquery', $form_state['values']['usesystemjquery']);
variable_set('elfinder_settings_thumbnail_tmbcrop', $form_state['values']['tmbcrop']);
variable_set('elfinder_settings_filesystem_maxfilesize', $form_state['values']['max_filesize']);
variable_set('elfinder_settings_filesystem_maxfilecount', $form_state['values']['max_filecount']);
variable_set('elfinder_settings_filesystem_handleprivate', $form_state['values']['handleprivate']);
variable_set('elfinder_settings_filesystem_public_root_label', $form_state['values']['filesystem_public_root_label']);
variable_set('elfinder_settings_filesystem_private_root_label', $form_state['values']['filesystem_private_root_label']);
variable_set('elfinder_settings_filesystem_unmanaged_root_label', $form_state['values']['filesystem_unmanaged_root_label']);
variable_set('elfinder_settings_misc_manager_width', $form_state['values']['manager_width']);
variable_set('elfinder_settings_misc_manager_height', $form_state['values']['manager_height']);
variable_set('elfinder_settings_misc_hidden_folders', $form_state['values']['hidden_folders']);
variable_set('elfinder_settings_filesystem_allowed_extensions', $form_state['values']['filesystem_allowed_extensions']);
drupal_set_message(t('Changes have been saved.'));
}
/**
* Validate form data
*/
function elfinder_admin_form_validate($form, &$form_state) {
$tmbsize = $form_state['values']['tmbsize'];
$mwidth = $form_state['values']['manager_width'];
$mheight = $form_state['values']['manager_height'];
if ($form_state['values']['filesystem_root'] == 'custom') {
$customroot = $form_state['values']['filesystem_root_custom'];
if ($customroot == '') {
form_set_error('filesystem_root_custom', t('Filesystem root cannot be empty'));
}
$customroot = elfinder_parse_path_tokens($customroot);
// checking directory accessibility by creating test file
if (!elfinder_prepare_directory($customroot)) {
form_set_error('filesystem_root_custom', t('Filesystem root is not writable'));
}
}
if (!preg_match('/^\d{3,4}$/', $form_state['values']['file_perm'])) {
form_set_error('file_perm', t('Created file permissions should be a numeric'));
}
if (!preg_match('/^\d{3,4}?$/', $form_state['values']['dir_perm'])) {
form_set_error('dir_perm', t('Created directory permissions should be a numeric'));
}
if (!empty($form_state['values']['max_filesize']) && !is_numeric(parse_size($form_state['values']['max_filesize']))) {
form_set_error('max_filesize', t('The file size option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).'));
}
if ($mwidth && !is_numeric($mwidth)) {
form_set_error('manager_width', t('Manager width size should be a number'));
}
if ($mheight && !is_numeric($mheight)) {
form_set_error('manager_height', t('Manager height size should be a number'));
}
if ($tmbsize && !is_numeric($tmbsize)) {
form_set_error('tmbsize', t('Thumbnail size should be a number'));
}
}

View File

@@ -0,0 +1,563 @@
<?php
/**
* @file
* file manager admin profile settings page
*/
function elfinder_admin_profile($form, &$form_state) {
global $user, $language;
$action = $form_state['build_info']['args'][0];
$profile_name = $form_state['build_info']['args'][1];
drupal_add_css(drupal_get_path('module', 'elfinder') . '/css/elfinder.admin.css');
$profiles = elfinder_admin_profile_get();
$p = NULL;
if (!is_array($profiles)) {
$profiles = array($profiles);
}
foreach ($profiles as $prof) {
if (is_object($prof) && $prof->name == $profile_name && $p == NULL) {
$p = $prof;
}
}
$profile = isset($p->settings) ? $p->settings : NULL;
if ($action == 'profile_add') {
$form['profile_name'] = array(
'#type' => 'textfield',
'#title' => t('Profile Name'),
'#default_value' => '',
'#size' => 14,
);
} else {
$form['profile_name_label'] = array(
'#title' => t('Profile Name'),
'#markup' => $profile_name,
'#size' => 14,
'#type' => 'item',
);
$form['profile_name'] = array(
'#default_value' => $profile_name,
'#type' => 'hidden',
);
}
$form['profile_description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => isset($p->description) ? $p->description : '',
);
$form['profile_role'] = array(
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#default_value' => isset($profile['profile_role']) ? array_keys($profile['profile_role']) : array(),
'#options' => user_roles(),
'#description' => t('Roles for which profile settings will be applied'),
);
$form['profile_action'] = array(
'#default_value' => $action,
'#type' => 'hidden',
);
$form['filesystem_settings'] = array(
'#type' => 'fieldset',
'#title' => t('File system'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['volumes'] = array(
'#type' => 'fieldset',
'#title' => t('Volumes'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['volumes']['volumes_wrapper'] = array(
'#weight' => -4,
'#prefix' => '<div class="clear-block" id="poll-choice-wrapper">',
'#suffix' => '</div>',
);
$form['volumes']['volumes_wrapper']['volume'] = array(
'#prefix' => '<div id="profile-volumes">',
'#suffix' => '</div>',
'#type' => 'markup',
'#value' => '&nbsp;',
);
$dirs = $profile['volume'];
if (isset($form_state['values']['volume'])) {
$choices = $form_state['values']['volume'];
$lasti = 0;
for ($i = 0; $i < count($choices); $i++) {
$form['volumes']['volumes_wrapper']['volume'][$i] = elfinder_admin_dir_form($i, $choices[$i], 0);
$lasti = $i;
}
if (isset($form_state['clicked_button']['#name']) && $form_state['clicked_button']['#name'] == 'addfield') {
$form['volumes']['volumes_wrapper']['volume'][] = elfinder_admin_dir_form(count($choices), '', 0);
}
} else {
for ($i = 0; $i < count($dirs); $i++) {
$form['volumes']['volumes_wrapper']['volume'][$i] = elfinder_admin_dir_form($i, $dirs[$i], 0);
}
if (isset($form_state['clicked_button']['#name']) && $form_state['clicked_button']['#name'] == 'addfield') {
$form['volumes']['volumes_wrapper']['volume'][] = elfinder_admin_dir_form(count($dirs), '', 0);
}
}
$ckeditor_volumes = array('' => t('Select One'));
$i = 0;
foreach ($form['volumes']['volumes_wrapper']['volume'] as $volume) {
if (is_array($volume) && isset($volume['volume_settings'])) {
$ckeditor_volumes[] = t('Volume @i (@path)', array(
'@i' => $i,
'@path' => $volume['volume_settings']['path']['#default_value'],
));
$i++;
}
}
$form['volumes']['volumes_wrapper']['volume_add'] = array(
'#type' => 'submit',
'#value' => t('Add volume'),
'#weight' => 1,
'#name' => 'addfield',
'#submit' => array('elfinder_admin_profile_change_volume'),
'#ajax' => array(
'wrapper' => 'profile-volumes',
'callback' => 'elfinder_admin_profile_callback',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['filesystem_settings']['mime_detect'] = array(
'#type' => 'radios',
'#title' => t('File type detection'),
'#default_value' => isset($profile['mimedetect']) ? $profile['mimedetect'] : 'auto',
'#options' => array(
'auto' => t('Automatical detection'),
),
);
if (function_exists('finfo_open')) {
$form['filesystem_settings']['mime_detect']['#options']['finfo'] = t('php finfo');
}
if (function_exists('mime_content_type')) {
$form['filesystem_settings']['mime_detect']['#options']['php'] = t('php mime_content_type()');
}
$form['filesystem_settings']['mime_detect']['#options']['linux'] = t('file -ib (linux)');
$form['filesystem_settings']['mime_detect']['#options']['bsd'] = t('file -Ib (bsd)');
$form['filesystem_settings']['mime_detect']['#options']['internal'] = t('By file extension (built-in)');
$form['filesystem_settings']['mime_detect']['#options']['drupal'] = t('Drupal API');
$form['filesystem_settings']['file_url_type'] = array(
'#type' => 'radios',
'#title' => t('Selected file url type'),
'#default_value' => $profile['file_url_type'] == 'true' ? 'true' : 'false',
'#options' => array(
'true' => t('Absolute'),
'false' => t('Relative'),
),
);
$form['filesystem_settings']['file_perm'] = array(
'#type' => 'textfield',
'#title' => t('Created file permissions'),
'#default_value' => isset($profile['file_perm']) ? $profile['file_perm'] : '0666',
'#size' => 4,
);
$form['filesystem_settings']['dir_perm'] = array(
'#type' => 'textfield',
'#title' => t('Created directory permissions'),
'#default_value' => isset($profile['dir_perm']) ? $profile['dir_perm'] : '0777',
'#size' => 4,
);
$form['filesystem_settings']['max_filesize'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size'),
'#default_value' => isset($profile['max_filesize']) ? $profile['max_filesize'] : '',
'#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', array('%limit' => format_size(file_upload_max_size()))),
'#size' => 10,
'#weight' => 5,
);
$form['filesystem_settings']['user_quota'] = array(
'#type' => 'textfield',
'#title' => t('User quota'),
'#default_value' => isset($profile['user_quota']) ? $profile['user_quota'] : '',
'#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be unlimited.'),
'#size' => 10,
'#weight' => 5,
);
$form['thumbnail_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Thumbnails'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['thumbnail_settings']['tmbsize'] = array(
'#type' => 'textfield',
'#title' => t('Thumbnail size'),
'#default_value' => isset($profile['tmbsize']) ? $profile['tmbsize'] : '48',
'#size' => 4,
);
$form['thumbnail_settings']['tmbdirname'] = array(
'#type' => 'textfield',
'#title' => t('Thumbnail directory name'),
'#default_value' => isset($profile['tmbdirname']) ? $profile['tmbdirname'] : 'tmb',
'#size' => 10,
);
$form['thumbnail_settings']['imglib'] = array(
'#type' => 'radios',
'#title' => t('Image manipulation library'),
'#default_value' => isset($profile['imglib']) ? $profile['imglib'] : 'auto',
'#options' => array(
'auto' => t('Automatical detection'),
'imagick' => t('Image Magick'),
'gd' => t('GD'),
),
);
$form['thumbnail_settings']['tmbcrop'] = array(
'#type' => 'radios',
'#title' => t('Image crop'),
'#default_value' => $profile['tmbcrop'] == 'true' ? 'true' : 'false',
'#options' => array(
'true' => t('Yes'),
'false' => t('No'),
),
'#description' => t('Crop image to fit thumbnail size. Yes - crop, No - scale image to fit thumbnail size.'),
);
$form['misc_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Miscellaneous'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['misc_settings']['rememberlastdir'] = array(
'#type' => 'radios',
'#title' => t('Remember last opened directory'),
'#default_value' => $profile['rememberlastdir'] == 'true' ? 'true' : 'false',
'#options' => array(
'true' => t('Yes'),
'false' => t('No'),
),
'#description' => t('Creates a cookie. Disable if you have issues with caching.'),
);
$form['misc_settings']['manager_width'] = array(
'#type' => 'textfield',
'#title' => t('File manager width'),
'#default_value' => isset($profile['manager_width']) ? $profile['manager_width'] : '',
'#size' => 4,
);
$form['misc_settings']['manager_height'] = array(
'#type' => 'textfield',
'#title' => t('File manager height'),
'#default_value' => isset($profile['manager_height']) ? $profile['manager_height'] : '',
'#size' => 4,
);
$form['misc_settings']['ckeditor_upload_settings'] = array(
'#type' => 'fieldset',
'#title' => t('CKEditor/FCKeditor Upload Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('CKEditor and FCKeditor allowing to upload and insert image files directly from Insert Image dialog.'),
);
$form['misc_settings']['ckeditor_upload_settings']['ckeditor_upload_directory'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => isset($profile['ckeditor_upload_directory']) ? $profile['ckeditor_upload_directory'] : '',
'#size' => 40,
'#description' => t('Image upload path. Default file uri is used if no uri prefix specified. Examples: public://ckeditor - image will be uploaded into public://ckeditor; images/ckeditor - image will be uploaded to :uriimages/ckeditor', array(':uri' => file_build_uri(''))),
);
$form['submit'] = array(
'#type' => 'submit',
'#name' => 'save_profile',
'#value' => t('Save configuration')
);
$form['#submit'][] = 'elfinder_admin_submit';
return $form;
}
function elfinder_admin_profile_callback($form, $form_state) {
$form_state['rebuild'] = TRUE;
return $form['volumes']['volumes_wrapper']['volume'];
}
/**
* Settings form callback
*/
function elfinder_admin_profiles($action = '', $profile_name = '') {
$obj = NULL;
if ($action == 'profile_delete') {
$obj = drupal_get_form('elfinder_admin_profiles_delete_form', $action, $profile_name);
} else {
$obj = drupal_get_form('elfinder_admin_profile', $action, $profile_name);
}
return drupal_render($obj);
}
function elfinder_admin_profiles_delete_form($form_id, &$form_state, $action, $profile_name) {
$form = array();
$form['profile_name'] = array(
'#type' => 'hidden',
'#default_value' => $profile_name,
);
$form['#submit'][] = 'elfinder_admin_profile_delete_submit';
return confirm_form($form, t('Are you sure you want to delete the profile %profile?', array('%profile' => $profile_name)), 'admin/config/media/elfinder', '', t('Delete'), t('Cancel')
);
return $form;
}
function elfinder_admin_profile_delete_submit($form, &$form_state) {
if ($form_state['values']['confirm'] == 1) {
drupal_set_message(t('Profile deleted'));
$pid = elfinder_admin_profile_get_pid($form_state['values']['profile_name']);
if ($pid > 0) {
db_delete('elfinder_profile')
->condition('pid', $pid)
->execute();
}
}
drupal_goto('admin/config/media/elfinder');
}
function elfinder_admin_dir_form($delta, $values = array(), $votes = 0) {
$form = array();
$pvtfiles = file_stream_wrapper_get_instance_by_uri('private://');
$pubfiles = file_stream_wrapper_get_instance_by_uri('public://');
if (is_object($pvtfiles)) {
$defaulturl = $pvtfiles->getExternalUrl();
} elseif (is_object($pubfiles)) {
$defaulturl = $pubfiles->getExternalUrl();
}
$form['volume_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Volume @n (@p)', array('@n' => ($delta + 1), '@p' => $values['path'])),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['volume_settings']['path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => isset($values['path']) ? $values['path'] : '',
'#parents' => array('volume', $delta, 'path'),
'#prefix' => '<div class="elfinder-field-wrapper-volume-path">',
'#suffix' => '</div>',
'#size' => 40,
);
$form['volume_settings']['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#size' => 15,
'#default_value' => isset($values['label']) ? $values['label'] : '',
'#description' => t('Root directory label in directory tree'),
'#parents' => array('volume', $delta, 'label'),
);
$form['volume_settings']['url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => isset($values['url']) ? $values['url'] : '',
'#parents' => array('volume', $delta, 'url'),
'#prefix' => '<div class="elfinder-field-wrapper-volume-path">',
'#suffix' => '</div>',
'#description' => t('Custom URL prefix (default %def)', array('%def' => $defaulturl)),
'#size' => 40,
);
$form['volume_settings']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#name' => 'rmfield-' . $delta,
'#submit' => array('elfinder_admin_profile_change_volume'),
'#ajax' => array(
'callback' => 'elfinder_admin_profile_callback',
'wrapper' => 'profile-volumes',
'method' => 'replace',
'effect' => 'fade',
),
);
return $form;
}
function elfinder_admin_profile_change_volume($form, &$form_state) {
$clickedbutton = $form_state['clicked_button']['#name'];
if (preg_match("/^rmfield-(\d+)/", $clickedbutton, $m)) {
$itemid = $m[1];
unset($form_state['values']['volume'][$itemid]);
$form_state['values']['volume'] = array_values($form_state['values']['volume']);
unset($form_state['input']['volume'][$itemid]);
$form_state['input']['volume'] = array_values($form_state['input']['volume']);
} elseif ($clickedbutton = 'addfield') {
$form_state['input']['volume'][] = '';
}
$form_state['rebuild'] = TRUE;
}
function elfinder_admin_profile_get_pid($name) {
$pid = db_select('elfinder_profile', 'p')->condition('name', $name)->fields('p', array('pid'))->execute()->fetchField();
return $pid;
}
/**
* Save form data
*/
function elfinder_admin_submit($form, &$form_state) {
$settings = array('tmbsize', 'tmbdirname', 'mime_detect', 'file_url_type', 'imglib', 'file_perm', 'dir_perm', 'rememberlastdir', 'usesystemjquery', 'tmbcrop', 'max_filesize', 'manager_width', 'manager_height', 'volume', 'user_quota', 'ckeditor_upload_volume', 'ckeditor_upload_directory');
$profile_settings = array();
foreach ($settings as $setting) {
if (isset($form_state['values'][$setting])) {
/* Excluding empty volumes */
if ($setting == 'volume') {
for ($i = 0; $i < count($form_state['values']['volume']); $i++) {
if (empty($form_state['values']['volume'][$i]['path'])) {
unset($form_state['values']['volume'][$i]);
}
}
}
$profile_settings[$setting] = $form_state['values'][$setting];
}
}
// Save roles.
$profile_settings['profile_role'] = array();
foreach($form_state['values']['profile_role'] as $rid) {
if ($rid != 0) {
$profile_settings['profile_role'][$rid] = $rid;
}
}
$profile_name = $form_state['values']['profile_name'];
$profile_description = $form_state['values']['profile_description'];
$pid = 0;
if ($form_state['values']['profile_action'] == 'profile_edit' && $profile_name) {
$pid = elfinder_admin_profile_get_pid($profile_name);
}
$profile = new StdClass;
$profile->pid = $pid;
$profile->name = $profile_name;
$profile->description = $profile_description;
$profile->settings = serialize($profile_settings);
if (isset($form_state['values']['save_profile'])) {
drupal_write_record('elfinder_profile', $profile, $pid > 0 ? array('pid') : array());
drupal_set_message(t('Profile have been saved.'));
drupal_goto('admin/config/media/elfinder');
}
}
/**
* Validate form data
*/
function elfinder_admin_form_validate($form, &$form_state) {
$tmbsize = $form_state['values']['tmbsize'];
$mwidth = $form_state['values']['manager_width'];
$mheight = $form_state['values']['manager_height'];
if ($form_state['values']['filesystem_root'] == 'custom') {
$customroot = $form_state['values']['filesystem_root_custom'];
if ($customroot == '') {
form_set_error('filesystem_root_custom', t('Filesystem root cannot be empty'));
}
$customroot = elfinder_parse_path_tokens($customroot);
// checking directory accessibility by creating test file
if (!elfinder_prepare_directory($customroot)) {
form_set_error('filesystem_root_custom', t('Filesystem root is not writable'));
}
}
if (!preg_match('/^\d{3,4}$/', $form_state['values']['file_perm'])) {
form_set_error('file_perm', t('Created file permissions should be a numeric'));
}
if (!preg_match('/^\d{3,4}?$/', $form_state['values']['dir_perm'])) {
form_set_error('dir_perm', t('Created directory permissions should be a numeric'));
}
if (!empty($form_state['values']['max_filesize']) && !is_numeric(parse_size($form_state['values']['max_filesize']))) {
form_set_error('max_filesize', t('The file size option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).'));
}
if (!empty($form_state['values']['user_quota']) && !is_numeric(parse_size($form_state['values']['user_quota']))) {
form_set_error('user_quota', t('The file size option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).'));
}
if ($mwidth && !is_numeric($mwidth)) {
form_set_error('manager_width', t('Manager width size should be a number'));
}
if ($mheight && !is_numeric($mheight)) {
form_set_error('manager_height', t('Manager height size should be a number'));
}
if ($tmbsize && !is_numeric($tmbsize)) {
form_set_error('tmbsize', t('Thumbnail size should be a number'));
}
}

View File

@@ -0,0 +1,265 @@
<?php
/**
* @file
*
* elFinder conenctor class
*/
class elFinderDrupal extends elFinder {
public function __construct($opts) {
if (is_callable(array($this, 'version'))) {
$ver = floatval($this->version());
if (isset(elFinder::$ApiVersion)) {
$ver = elFinder::$ApiVersion;
}
if ($ver == 2.1 && is_callable(array($this, 'revision')) && $this->revision() < 37) {
$this->connector_unsupported_error();
}
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);
} else {
$this->connector_unsupported_error();
}
}
public function connector_unsupported_error() {
$this->connector_error(t('Unsupported elFinder library version. Please upgrade.'));
}
public function connector_error($message) {
exit(drupal_json_encode(array('error' => array(strip_tags($message)))));
}
/* Overriding search query argument name 'q' since it's already used in Drupal */
public function commandArgsList($cmd) {
$this->commands['search']['elfinder_search_q'] = TRUE;
return $this->commandExists($cmd) ? $this->commands[$cmd] : 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);
}
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;
}
}

View File

@@ -0,0 +1,108 @@
<?php
/**
* @file
* elfinder ACL class
*/
class elFinderDrupalACL {
public function fsAccessPublic($attr, $path, $data, $volume) {
if (strpos(basename($path), '.') === 0 && $attr == 'hidden') {
return TRUE;
}
/* Hiding thumbnail folder */
if (strstr($path, DIRECTORY_SEPARATOR . variable_get('elfinder_settings_thumbnail_dirname', 'tmb')) && $attr == 'hidden') {
return TRUE;
}
// Hide folders hidden by config settings.
$f = drupal_realpath("public://") . "/";
$folder = str_replace($f, "", $path);
$hidden_folder = preg_split("/[\s,]+/", variable_get('elfinder_settings_misc_hidden_folders', ''));
if (in_array($folder, $hidden_folder) && $attr == 'hidden') {
return TRUE;
}
if (strstr($path, DIRECTORY_SEPARATOR . '.quarantine') && $attr == 'hidden') {
return TRUE;
}
if ($attr == 'read') {
return TRUE;
}
if ($attr == 'write') {
return TRUE;
}
/* if ($attr == 'write' && user_access('write public files')) {
return TRUE;
}
if ($attr == 'locked' && !user_access('write public files')) {
return TRUE;
} */
return FALSE;
}
public function fsAccessPrivate($attr, $path, $data, $volume) {
if (strpos(basename($path), '.') === 0 && $attr == 'hidden') {
return TRUE;
}
/* Hiding thumbnail folder */
if (strstr($path, DIRECTORY_SEPARATOR . variable_get('elfinder_settings_thumbnail_dirname', 'tmb')) && $attr == 'hidden') {
return TRUE;
}
if (strstr($path, DIRECTORY_SEPARATOR . '.quarantine') && $attr == 'hidden') {
return TRUE;
}
if ($attr == 'read') {
return TRUE;
}
if ($attr == 'write') {
return TRUE;
}
/* if ($attr == 'write' && user_access('write public files')) {
return TRUE;
}
if ($attr == 'locked' && !user_access('write public files')) {
return TRUE;
} */
return FALSE;
}
public function fsAccessUnmanaged($attr, $path, $data, $volume) {
if (strpos(basename($path), '.') === 0 && $attr == 'hidden') {
return TRUE;
}
/* Hiding thumbnail folder */
if (strstr($path, DIRECTORY_SEPARATOR . variable_get('elfinder_settings_thumbnail_dirname', 'tmb')) && $attr == 'hidden') {
return TRUE;
}
if (strstr($path, DIRECTORY_SEPARATOR . '.quarantine') && $attr == 'hidden') {
return TRUE;
}
if ($attr == 'read') {
return TRUE;
}
return FALSE;
}
}

View File

@@ -0,0 +1,652 @@
<?php
/**
* elFinder Integration
*
* Copyright (c) 2010-2018, Alexey Sukhotin. All rights reserved.
*/
/**
* @file
*
* elFinder driver for Drupal filesystem.
*
* @author Alexey Sukhotin
* */
class elFinderVolumeDrupal extends elFinderVolumeLocalFileSystem {
protected $DrupalFilesACL = NULL;
/**
* Create Drupal file object
*
* @param string $path file path
* @return object
* @author Alexey Sukhotin
* */
protected function _drupalfileobject($path) {
$uri = $this->drupalpathtouri($path);
return elfinder_get_drupal_file_obj($uri);
}
/**
* Convert path to Drupal file URI
*
* @param string $path file path
* @return string
* @author Alexey Sukhotin
* */
public function drupalpathtouri($path) {
$pvtpath = drupal_realpath('private://');
$pubpath = drupal_realpath('public://');
$tmppath = drupal_realpath('temporary://');
$uri = '';
if (strpos($path, $pvtpath) === 0) {
$uri = 'private://' . substr($path, strlen($pvtpath) + 1);
} elseif (strpos($path, $tmppath) === 0) {
$uri = 'temporary://' . substr($path, strlen($tmppath) + 1);
} else {
$uri = 'public://' . substr($path, strlen($pubpath) + 1);
}
return @file_stream_wrapper_uri_normalize($uri);
}
/**
* Check if file extension is allowed
*
* @param stdClass $file file object
* @return array
* @author Alexey Sukhotin
**/
protected function CheckExtension(stdClass $file) {
$allowed_extensions = variable_get('elfinder_settings_filesystem_allowed_extensions', '');
if (!empty($allowed_extensions)) {
$errors = file_validate_extensions($file, $allowed_extensions);
if (!empty($errors)) {
$this->setError(strip_tags(implode(' ', $errors)));
return FALSE;
}
}
return TRUE;
}
/**
* Create dir
*
* @param string $path parent dir path
* @param string $name new directory name
* @return bool
* @author Alexey Sukhotin
* */
protected function _mkdir($path, $name) {
$path = $path . DIRECTORY_SEPARATOR . $name;
if (@drupal_mkdir($path)) {
return $path;
}
return FALSE;
}
/**
* Create file
*
* @param string $path parent dir path
* @param string $name new file name
* @return bool
* @author Alexey Sukhotin
* */
protected function _mkfile($path, $name) {
$path = $path . DIRECTORY_SEPARATOR . $name;
$uri = $this->drupalpathtouri($path);
if (!$this->CheckExtension($this->_drupalfileobject($path))) {
return FALSE;
}
$file = file_save_data("", $uri);
$this->FileUsageAdd($file);
if (isset($file->fid)) {
return $path;
}
return FALSE;
}
/**
* Copy file into another file
*
* @param string $source source file path
* @param string $targetDir target directory path
* @param string $name new file name
* @return bool
* @author Alexey Sukhotin
* */
protected function _copy($source, $targetDir, $name) {
$target = $targetDir . DIRECTORY_SEPARATOR . (!empty($name) ? $name : basename($source));
if (!is_dir($target) && !$this->CheckExtension($this->_drupalfileobject($target))) {
return FALSE;
}
if (!$this->CheckUserQuota()) {
return FALSE;
}
if (file_copy($this->_drupalfileobject($source), $this->drupalpathtouri($target))) {
$this->FileUsageAdd($this->_drupalfileobject($target));
return TRUE;
}
return FALSE;
}
/**
* Move file into another parent dir
* Return new file path or false
*
* @param string $source source file path
* @param string $target target dir path
* @param string $name new name
* @return bool|string
* @author Alexey Sukhotin
* */
protected function _move($source, $targetDir, $name) {
$target = $targetDir . DIRECTORY_SEPARATOR . (!empty($name) ? $name : basename($source));
if (!is_dir($target) && !$this->CheckExtension($this->_drupalfileobject($target))) {
return FALSE;
}
if (is_dir($source)) {
$srcuri = $this->drupalpathtouri($source);
$dsturi = $this->drupalpathtouri($target);
$children = db_select('file_managed', 'f')
->condition('uri', $srcuri . '/%', 'LIKE')
->fields('f', array('fid', 'uri'))
->execute()
->fetchAll();
foreach ($children as $child) {
$newuri = str_replace("$srcuri/", "$dsturi/", $child->uri);
db_update('file_managed')->fields(array('uri' => $newuri))->condition('fid', $child->fid)->execute();
}
return @rename($source, $target);
} elseif (@file_move($this->_drupalfileobject($source), $this->drupalpathtouri($target))) {
return TRUE;
}
return FALSE;
}
/**
* Remove file
*
* @param string $path file path
* @return bool
* @author Alexey Sukhotin
* */
protected function _unlink($path) {
$file = $this->_drupalfileobject($path);
$this->FileUsageDelete($file);
$result = @file_delete($file);
if ($result === TRUE) {
return TRUE;
}
if (is_array($result)) {
return $result['file'];
} else {
return FALSE;
}
}
/**
* Remove dir
*
* @param string $path dir path
* @return bool
* @author Alexey Sukhotin
* */
protected function _rmdir($path) {
return @drupal_rmdir($path);
}
/**
* Delete dirctory trees and included files.
*
* Clone of elfinderVolumeDriver::delTree().
*
* Using elFinderVolumeLocalFileSystem::delTree to delete a folder with files
* in it would not update file_usage and file_managed tables. Using
* elfinderVolumeDriver::delTree makes it work better.
*/
protected function delTree($localpath) {
foreach ($this->_scandir($localpath) as $p) {
elFinder::extendTimeLimit();
$stat = $this->stat($this->convEncOut($p));
$this->convEncIn();
($stat['mime'] === 'directory') ? $this->delTree($p) : $this->_unlink($p);
}
$res = $this->_rmdir($localpath);
$res && $this->clearstatcache();
return $res;
}
/**
* Create new file and write into it from file pointer.
* Return new file path or false on error.
*
* @param resource $fp file pointer
* @param string $dir target dir path
* @param string $name file name
* @return bool|string
* @author Dmitry (dio) Levashov, Alexey Sukhotin
* */
protected function _save($fp, $dir, $name, $stat) {
$tmpname = $name;
$bu_ret = module_invoke_all('elfinder_beforeupload', array('name' => $name, 'dir' => $dir, 'stat' => $stat));
if (isset($bu_ret)) {
if (!is_array($bu_ret)) {
$bu_ret = array($bu_ret);
}
$tmpname = end($bu_ret);
}
$path = $dir . DIRECTORY_SEPARATOR . (!empty($tmpname) ? $tmpname : $name);
if (!$this->CheckUserQuota()) {
return FALSE;
}
if (!$this->CheckFolderCount($dir)) {
return FALSE;
}
if (!$this->CheckExtension($this->_drupalfileobject($path))) {
return FALSE;
}
if (!$this->FileValidate($name)) {
return FALSE;
}
if (!($target = @fopen($path, 'wb'))) {
return FALSE;
}
while (!feof($fp)) {
fwrite($target, fread($fp, 8192));
}
fclose($target);
@chmod($path, $this->options['fileMode']);
$file = $this->_drupalfileobject($path);
@file_save($file);
$this->FileUsageAdd($file);
return $path;
}
protected function CheckUserQuota() {
$space = $this->CalculateUserAllowedSpace();
if ($space == 0) {
$this->setError(t('Quota exceeded'));
return FALSE;
}
return TRUE;
}
protected function CheckFolderCount($dir) {
$max_allowed = variable_get('elfinder_settings_filesystem_maxfilecount', 0);
if ($max_allowed > 0) {
$options = array(
'recurse' => FALSE,
);
// Match name.extension. This won't count files with no extension.
$files = file_scan_directory($dir, '/.*\..*/', $options);
if (count($files) >= $max_allowed) {
$this->setError(t('Max directory file count of %count reached', array('%count' => $max_allowed)));
return FALSE;
}
}
return TRUE;
}
/**
* Let other Drupal modules perform validation on the uploaded file.
* See hook_file_validate().
*
* @param string $name file name
* @return bool
*/
protected function FileValidate($name) {
// The uploaded file is still in temp. Fetch it's name & path from $_FILES.
$index = array_search($name, $_FILES['upload']['name']);
if ($index !== FALSE) {
$file = $this->_drupalfileobject($_FILES['upload']['tmp_name'][$index]);
$validation_errors = module_invoke_all('file_validate', $file);
if (!empty($validation_errors)) {
$this->setError(strip_tags(implode(' ', $validation_errors)));
return FALSE;
}
} else {
watchdog('elfinder', 'File upload "' . $name . '" not found in $_FILES');
}
return TRUE;
}
/**
* Return files list in directory.
*
* @param string $path dir path
* @return array
* @author Dmitry (dio) Levashov
* */
protected function _scandir($path) {
$files = array();
foreach (scandir($path) as $name) {
if ($name != '.' && $name != '..') {
$files[] = $path . DIRECTORY_SEPARATOR . $name;
}
}
return $files;
}
public function owner($target) {
$path = $this->decode($target);
$file = $this->_drupalfileobject($path);
if ($file->fid) {
$owneraccount = user_load($file->uid);
/* AS */
$owner = $owneraccount->name;
$ownerformat = variable_get('elfinder_settings_filesystem_owner_format', '');
if ($ownerformat != '') {
$owner = token_replace($ownerformat, array('user' => $owneraccount));
}
return $owner;
}
return FALSE;
}
public function desc($target, $newdesc = NULL) {
$path = $this->decode($target);
$file = $this->_drupalfileobject($path);
if ($file->fid) {
$finfo = db_select('elfinder_file_extinfo', 'f')
->condition('fid', $file->fid)
->fields('f', array('fid', 'description'))
->execute()
->fetchObject();
$descobj = new StdClass;
$descobj->fid = $file->fid;
$descobj->description = $newdesc;
if ($newdesc != NULL && user_access('edit file description')) {
if (($rc = drupal_write_record('elfinder_file_extinfo', $descobj, isset($finfo->fid) ? array('fid') : array())) == 0) {
return -1;
}
} else {
return $finfo->description;
}
}
return $newdesc;
}
public function downloadcount($target) {
$path = $this->decode($target);
$file = $this->_drupalfileobject($path);
if ($file->fid && module_exists('elfinder_stats')) {
$downloads = db_select('elfinder_stats', 's')
->fields('s', array('fid'))
->condition('s.fid', $file->fid)
->condition('s.type', 'download')
->countQuery()
->execute()
->fetchField();
return $downloads ? $downloads : 0;
}
return 0;
}
protected function _archive($dir, $files, $name, $arc) {
if (!$this->CheckUserQuota()) {
return FALSE;
}
$ret = parent::_archive($dir, $files, $name, $arc);
if ($ret != FALSE) {
$file = $this->_drupalfileobject($ret);
@file_save($file);
$this->FileUsageAdd($file);
}
return $ret;
}
/**
* Extract files from archive.
*
* Run the parent extract() then add the files to the Drupal db.
*
* @param string $hash
* Archive filename hash.
* @param bool $makedir
* Extract the files into a new folder.
* @return array|bool
*/
public function extract($hash, $makedir = NULL) {
if (!$this->CheckUserQuota()) {
return FALSE;
}
$fstat = array();
if ($makedir == NULL) {
$fstat = parent::extract($hash);
} else {
$fstat = parent::extract($hash, $makedir);
}
if ($fstat != FALSE) {
$path = $this->decode($fstat['hash']);
$this->AddToDrupalDB($path);
$file = $this->_drupalfileobject($path);
if ($fstat['mime'] !== 'directory') {
$this->FileUsageAdd($file);
}
}
return $fstat;
}
/**
* Recursive function to add new files to Drupal's db.
*
* TODO: If a file with the same name already exists anywhere else, this will
* not create a new entry.
*/
protected function AddToDrupalDB($files) {
foreach($files AS $file) {
if($file['mime'] == 'directory') {
$newfiles = $this->scandir($file['hash']);
$this->AddToDrupalDB($newfiles);
} else {
$filepath = $this->decode($file['hash']);
$file_object = $this->_drupalfileobject($filepath);
@file_save($file_object);
$this->FileUsageAdd($file_object);
}
}
return TRUE;
}
protected function CalculateUserAllowedSpace($checkuser = NULL) {
global $user;
$realUser = isset($checkuser) ? $checkuser : $user;
$currentSpace = $this->CalculateUserUsedSpace($realUser);
$maxSpace = isset($this->options['userProfile']->settings['user_quota']) ? parse_size($this->options['userProfile']->settings['user_quota']) : NULL;
$diff = $maxSpace - $currentSpace;
if (isset($maxSpace) && $maxSpace > 0) {
if ($diff > 0) {
return $diff;
} else {
return 0;
}
}
return -1;
}
protected function CalculateUserUsedSpace($checkuser = NULL) {
global $user;
$realUser = isset($checkuser) ? $checkuser : $user;
$q = db_query("SELECT sum(filesize) FROM {file_managed} WHERE uid = :uid", array(':uid' => $realUser->uid));
$result = $q->fetchField();
return $result;
}
protected function FileUsageAdd($file) {
// Record that the module elfinder is using the file.
@file_usage_add($file, 'elfinder', 'elfinderFileFetcher', 0); // 0 : means that there is no reference at the moment.
}
protected function FileUsageDelete($file) {
// Delete record that the module elfinder is using the file.
@file_usage_delete($file, 'elfinder', 'elfinderFileFetcher', 0); // 0 : means that there is no reference at the moment.
}
protected function _checkArchivers() {
$this->archivers = variable_get('elfinder_settings_misc_archivers', array());
if (count($this->archivers) == 0) {
parent::_checkArchivers();
variable_set('elfinder_settings_misc_archivers', $this->archivers);
}
}
/**
* Rename file and return file info
*
* @param string $hash file hash
* @param string $name new file name
* @return array|false
**/
public function rename($hash, $name) {
$results = parent::rename($hash, $name);
// Update any fields that point to this file.
field_cache_clear();
return $results;
}
/**
* Taken from elFinderVolumeDriver::remove().
*
* Adds a message if the file is in use.
*/
protected function remove($path, $force = false) {
$stat = $this->stat($path);
if (empty($stat)) {
return $this->setError(elFinder::ERROR_RM, $path, elFinder::ERROR_FILE_NOT_FOUND);
}
$stat['realpath'] = $path;
$this->rmTmb($stat);
$this->clearcache();
if (!$force && !empty($stat['locked'])) {
return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash']));
}
if ($stat['mime'] == 'directory' && empty($stat['thash'])) {
$ret = $this->delTree($this->convEncIn($path));
$this->convEncOut();
if (!$ret) {
return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash']));
}
} else {
$results = $this->_unlink($this->convEncIn($path));
if (!$results) {
return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash']));
}
if (is_array($results)) {
// File is in use and is being protected by Drupal. Fetch the first
// entity where it's used.
foreach($results AS $entity_type => $entity) {
if(is_array($entity)) {
foreach($entity AS $id => $count) {
if($entity_type == 'node' && is_integer($id)) {
$node = node_load($id);
if(!empty($node->title)) {
return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash']), '', t('File is used in @title', array('@title' => $node->title)));
}
}
}
}
}
return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash']), t('File is in use.'));
}
$this->clearstatcache();
}
$this->removed[] = $stat;
return true;
}
}