View source
<?php
function imce_page($user = FALSE) {
module_invoke('admin_menu', 'suppress');
$jsop = isset($_GET['jsop']) ? $_GET['jsop'] : NULL;
print theme('imce_page', imce_content($user === FALSE ? $GLOBALS['user'] : $user, $jsop));
exit;
}
function imce_user_page($account) {
return imce_page($account);
}
function imce_content($user, $jsop = '') {
if ($jsop) {
return imce_js($user, $jsop);
}
if (!($imce = imce_initiate_profile($user))) {
return '';
}
imce_process_profile($imce);
drupal_add_js('misc/jquery.form.js');
drupal_add_js(drupal_get_path('module', 'imce') . '/js/imce.js');
$imce_ref = array(
'imce' => &$imce,
);
$forms = '';
if (!$imce['error']) {
if (imce_perm_exists($imce, 'upload')) {
$forms .= drupal_get_form('imce_upload_form', $imce_ref);
}
$forms .= drupal_get_form('imce_fileop_form', $imce_ref);
}
foreach (variable_get('imce_custom_content', array()) as $func => $state) {
if ($state && function_exists($func) && ($output = $func($imce))) {
$forms .= $output;
}
}
$content = theme('imce_content', imce_create_tree($imce), $forms, $imce_ref);
$imce['dir'] = str_replace('%2F', '/', rawurlencode($imce['dir']));
unset($imce['files'], $imce['name'], $imce['directories'], $imce['subdirectories'], $imce['filesize'], $imce['quota'], $imce['tuquota'], $imce['thumbnails'], $imce['uid'], $imce['usertab']);
drupal_add_js($imce_ref, 'setting');
return $content;
}
function imce_js($user, $jsop = '') {
$response = array();
if ($imce = imce_initiate_profile($user)) {
imce_process_profile($imce);
if (!$imce['error']) {
include_once './' . drupal_get_path('module', 'imce') . '/inc/js.inc';
if (function_exists($func = 'imce_js_' . $jsop)) {
$response['data'] = $func($imce);
}
}
}
$response['messages'] = drupal_get_messages();
drupal_set_header('Content-Type: text/' . ($jsop == 'upload' ? 'html' : 'javascript') . '; charset=utf-8');
print drupal_to_js($response);
exit;
}
function imce_upload_form(&$form_state, $ref) {
$imce =& $ref['imce'];
$form['imce'] = array(
'#type' => 'file',
'#title' => t('File'),
'#size' => 30,
'#prefix' => '<div class="container-inline">',
);
$form['upload'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
'#submit' => $imce['perm']['upload'] ? array(
'imce_upload_submit',
) : NULL,
'#suffix' => '</div>',
);
if (!empty($imce['thumbnails'])) {
$form['thumbnails'] = array(
'#type' => 'checkboxes',
'#title' => t('Create thumbnails'),
'#options' => imce_thumbnail_options($imce['thumbnails']),
);
}
$form = array(
'fset_upload' => array(
'#type' => 'fieldset',
'#title' => t('Upload file'),
) + $form,
);
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#action'] = $imce['url'];
return $form;
}
function imce_fileop_form(&$form_state, $ref) {
$imce =& $ref['imce'];
$form['filenames'] = array(
'#type' => 'textfield',
'#title' => t('Selected files'),
'#maxlength' => $imce['filenum'] ? $imce['filenum'] * 255 : NULL,
);
if (!empty($imce['thumbnails']) && imce_perm_exists($imce, 'thumb')) {
$form['fset_thumb'] = array(
'#type' => 'fieldset',
'#title' => t('Thumbnails'),
) + imce_thumb_form($imce);
}
if (imce_perm_exists($imce, 'delete')) {
$form['fset_delete'] = array(
'#type' => 'fieldset',
'#title' => t('Delete'),
) + imce_delete_form($imce);
}
if (imce_perm_exists($imce, 'resize')) {
$form['fset_resize'] = array(
'#type' => 'fieldset',
'#title' => t('Resize'),
) + imce_resize_form($imce);
}
$form['#action'] = $imce['url'];
return $form;
}
function imce_thumb_form(&$imce) {
$form['thumbnails'] = array(
'#type' => 'checkboxes',
'#title' => t('Thumbnails'),
'#options' => imce_thumbnail_options($imce['thumbnails']),
);
$form['thumb'] = array(
'#type' => 'submit',
'#value' => t('Create thumbnails'),
'#submit' => $imce['perm']['thumb'] ? array(
'imce_thumb_submit',
) : NULL,
);
return $form;
}
function imce_delete_form(&$imce) {
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => $imce['perm']['delete'] ? array(
'imce_delete_submit',
) : NULL,
);
return $form;
}
function imce_resize_form(&$imce) {
$form['width'] = array(
'#type' => 'textfield',
'#title' => t('Width x Height'),
'#size' => 5,
'#maxlength' => 4,
'#prefix' => '<div class="container-inline">',
);
$form['height'] = array(
'#type' => 'textfield',
'#size' => 5,
'#maxlength' => 4,
'#prefix' => 'x',
);
$form['resize'] = array(
'#type' => 'submit',
'#value' => t('Resize'),
'#submit' => $imce['perm']['resize'] ? array(
'imce_resize_submit',
) : NULL,
'#suffix' => '</div>',
);
$form['copy'] = array(
'#type' => 'checkbox',
'#title' => t('Create a new image'),
'#default_value' => 1,
);
return $form;
}
function imce_fileop_form_validate($form, &$form_state) {
$imce =& $form['#parameters'][2]['imce'];
if ($form_state['values']['filenames'] == '') {
return form_error($form['filenames'], t('Please select a file.'));
}
$filenames = explode(':', $form_state['values']['filenames']);
$cnt = count($filenames);
if ($imce['filenum'] && $cnt > $imce['filenum']) {
return form_error($form['filenames'], t('You are not allowed to operate on more than %num files.', array(
'%num' => $imce['filenum'],
)));
}
for ($i = 0; $i < $cnt; $i++) {
$filenames[$i] = $filename = rawurldecode($filenames[$i]);
if (!isset($imce['files'][$filename])) {
watchdog('imce', 'Illegal choice %choice in !name element.', array(
'%choice' => $filename,
'!name' => t('directory (%dir)', array(
'%dir' => file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']),
)),
), WATCHDOG_ERROR);
return form_error($form['filenames'], t('An illegal choice has been detected. Please contact the site administrator.'));
}
}
$form_state['values']['filenames'] = $filenames;
}
function imce_upload_submit($form, &$form_state) {
$form_state['redirect'] = FALSE;
$imce =& $form['#parameters'][2]['imce'];
$validators = array(
'imce_validate_all' => array(
&$imce,
),
);
$dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']);
$replace = variable_get('imce_settings_replace', FILE_EXISTS_RENAME);
if ($file = file_save_upload('imce', $validators, $dirpath, $replace)) {
@chmod($file->filepath, 0664);
if ($replace == FILE_EXISTS_RENAME) {
$name = basename($file->filepath);
if ($name != $file->filename) {
$file->filename = $name;
drupal_set_message(t('The file is renamed to %filename.', array(
'%filename' => $file->filename,
)));
}
}
else {
if ($replace == FILE_EXISTS_REPLACE) {
if ($_file = db_fetch_object(db_query("SELECT fid FROM {files} WHERE filepath = '%s' AND fid <> %d", $file->filepath, $file->fid))) {
db_query("DELETE FROM {files} WHERE fid = %d", $file->fid);
$file->fid = $_file->fid;
}
}
}
$file->uid = $imce['uid'];
$file->status = FILE_STATUS_PERMANENT;
drupal_write_record('files', $file, array(
'fid',
));
drupal_set_message(t('%filename is uploaded.', array(
'%filename' => $file->filename,
)));
$img = imce_image_info($file->filepath);
$file->width = $img ? $img['width'] : 0;
$file->height = $img ? $img['height'] : 0;
imce_add_file($file, $imce);
if (isset($form_state['values']['thumbnails']) && $img) {
imce_create_thumbnails($file->filename, $imce, $form_state['values']['thumbnails']);
}
}
else {
drupal_set_message(t('Upload failed.'), 'error');
}
}
function imce_thumb_submit($form, &$form_state) {
$form_state['redirect'] = FALSE;
$imce =& $form['#parameters'][2]['imce'];
imce_process_files($form_state['values']['filenames'], $imce, 'imce_create_thumbnails', array(
$form_state['values']['thumbnails'],
));
}
function imce_delete_submit($form, &$form_state) {
$form_state['redirect'] = FALSE;
$imce =& $form['#parameters'][2]['imce'];
$deleted = imce_process_files($form_state['values']['filenames'], $imce, 'imce_delete_file');
if (!empty($deleted)) {
drupal_set_message(t('File deletion successful: %files.', array(
'%files' => utf8_encode(implode(', ', $deleted)),
)));
}
}
function imce_resize_submit($form, &$form_state) {
$form_state['redirect'] = FALSE;
$imce =& $form['#parameters'][2]['imce'];
$width = (int) $form_state['values']['width'];
$height = (int) $form_state['values']['height'];
list($maxw, $maxh) = explode('x', $imce['dimensions']);
if ($width < 1 || $height < 1 || $maxw && ($width > $maxw || $height > $maxh)) {
drupal_set_message(t('Please specify dimensions within the allowed range that is from 1x1 to @dimensions.', array(
'@dimensions' => $imce['dimensions'] ? $imce['dimensions'] : t('unlimited'),
)), 'error');
return;
}
$resized = imce_process_files($form_state['values']['filenames'], $imce, 'imce_resize_image', array(
$width,
$height,
$form_state['values']['copy'],
));
if (!empty($resized)) {
drupal_set_message(t('File resizing successful: %files.', array(
'%files' => utf8_encode(implode(', ', $resized)),
)));
}
}
function imce_process_files($filenames, &$imce, $function, $args = array()) {
$args = array_merge(array(
'',
&$imce,
), $args);
$processed = array();
foreach ($filenames as $filename) {
$args[0] = $filename;
if (call_user_func_array($function, $args)) {
$processed[] = $filename;
}
}
return $processed;
}
function imce_delete_file($filename, &$imce) {
$filepath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']) . '/' . $filename;
if (!file_delete($filepath)) {
return FALSE;
}
db_query("DELETE FROM {files} WHERE filepath = '%s'", $filepath);
imce_remove_file($filename, $imce);
return TRUE;
}
function imce_create_thumbnails($filename, &$imce, $values) {
$created = array();
foreach ($imce['thumbnails'] as $thumbnail) {
if ($values[$thumbnail['name']] && imce_create_thumbnail($filename, $imce, $thumbnail)) {
$created[] = $thumbnail['name'];
}
}
if (!empty($created)) {
drupal_set_message(t('Thumbnail creation (%thumbnames) successful for %filename.', array(
'%thumbnames' => implode(', ', $created),
'%filename' => utf8_encode($filename),
)));
}
return $created;
}
function imce_create_thumbnail($filename, &$imce, $thumbnail) {
$name = $thumbnail['prefix'];
if ($thumbnail['suffix'] != '' && ($dot = strrpos($filename, '.'))) {
$name .= substr($filename, 0, $dot);
$name .= $thumbnail['suffix'];
$name .= substr($filename, $dot);
}
else {
$name .= $filename;
}
list($width, $height) = explode('x', $thumbnail['dimensions']);
return imce_resize_image($filename, $imce, $width, $height, TRUE, $name, variable_get('imce_settings_thumb_method', 'scale_and_crop'));
}
function imce_resize_image($filename, &$imce, $width, $height, $copy = TRUE, $dest = FALSE, $op = 'resize') {
$dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']);
$filepath = $dirpath . '/' . $filename;
if (!$imce['files'][$filename]['width'] || !($img = imce_image_info($filepath))) {
drupal_set_message(t('%filename is not an image.', array(
'%filename' => utf8_encode($filename),
)), 'error', FALSE);
return FALSE;
}
if (substr($op, 0, 5) == 'scale' && !($width < $img['width'] || $height < $img['height'])) {
drupal_set_message(t('Scaling up is not allowed.'), 'error', FALSE);
return FALSE;
}
$file = new stdClass();
$file->filepath = $dirpath . '/' . $dest;
if (!$dest || $dest == $filename) {
$file->filepath = $copy ? file_create_filename($filename, $dirpath) : $filepath;
}
$file->filename = basename($file->filepath);
if (isset($imce['files'][$file->filename])) {
if (($f = $imce['files'][$file->filename]) && $f['width'] == $width && $f['height'] == $height) {
drupal_set_message(t('%filename(%dimensions) already exists.', array(
'%filename' => utf8_encode($file->filename),
'%dimensions' => $width . 'x' . $height,
)), 'error');
return FALSE;
}
}
$errors = file_validate_name_length($file);
if (!empty($errors)) {
drupal_set_message($errors[0], 'error');
return FALSE;
}
$temp = tempnam(realpath(file_directory_temp()), 'imc');
register_shutdown_function('file_delete', $temp);
$function = 'image_' . $op;
if (!$function($filepath, $temp, $width, $height)) {
drupal_set_message(t('%filename cannot be resized to %dimensions', array(
'%filename' => utf8_encode($filename),
'%dimensions' => $width . 'x' . $height,
)), 'error', FALSE);
return FALSE;
}
$file->filesize = filesize($temp);
$overwrite = $file->filename == $filename;
if (!imce_validate_quotas($file, $imce, $overwrite ? -$imce['files'][$filename]['size'] : 0)) {
return FALSE;
}
if (!@copy($temp, $file->filepath)) {
drupal_set_message(t('The selected file %file could not be copied.', array(
'%file' => utf8_encode($file->filename),
)), 'error', FALSE);
return FALSE;
}
@chmod($file->filepath, 0664);
$file->uid = $imce['uid'];
$file->filemime = $img['mime'];
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
$update = array();
if ($overwrite && ($_file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'", $file->filepath)))) {
$file->fid = $_file->fid;
$file->uid = $_file->uid;
$update[] = 'fid';
}
drupal_write_record('files', $file, $update);
if ($op == 'scale') {
$img = imce_image_info($file->filepath);
$width = $img['width'];
$height = $img['height'];
}
$file->width = $width;
$file->height = $height;
imce_add_file($file, $imce);
return $file;
}
function imce_add_file($file, &$imce) {
$imce['dirsize'] += $file->filesize;
if (isset($imce['files'][$file->filename])) {
$imce['dirsize'] -= $imce['files'][$file->filename]['size'];
}
$imce['files'][$file->filename] = array(
'name' => $file->filename,
'size' => $file->filesize,
'width' => $file->width,
'height' => $file->height,
'date' => $file->timestamp,
);
if (isset($_GET['jsop'])) {
$add = $imce['files'][$file->filename];
$add['name'] = rawurlencode($file->filename);
$add['fsize'] = format_size($file->filesize);
$add['fdate'] = format_date($file->timestamp, 'small');
$add['id'] = $file->fid;
$imce['added'][] = $add;
}
}
function imce_remove_file($filename, &$imce) {
if (isset($imce['files'][$filename])) {
$imce['dirsize'] -= $imce['files'][$filename]['size'];
unset($imce['files'][$filename]);
if (isset($_GET['jsop'])) {
$imce['removed'][] = rawurlencode($filename);
}
}
}
function imce_validate_all(&$file, $imce) {
if (!$file->destination && variable_get('imce_settings_replace', FILE_EXISTS_RENAME) == FILE_EXISTS_ERROR) {
return array(
t('File browser is set to reject the upload of existing files.'),
);
}
$errors = imce_validate_filesize($file, $imce['filesize']);
if (empty($errors) && imce_image_info($file->filepath)) {
$errors = array_merge($errors, file_validate_image_resolution($file, $imce['dimensions']));
}
if ($imce['quota']) {
$errors = array_merge($errors, imce_validate_quota($file, $imce['quota'], $imce['dirsize']));
}
if ($imce['extensions'] != '*' && !preg_match('/\\.(' . str_replace(' ', '|', $imce['extensions']) . ')$/i', $file->filename)) {
$errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array(
'%files-allowed' => $imce['extensions'],
));
}
if (empty($errors) && $imce['tuquota']) {
$errors = array_merge($errors, imce_validate_quota($file, $imce['tuquota'], file_space_used($imce['uid']), 1));
}
return $errors;
}
function imce_validate_filesize($file, $maxsize = 0) {
$errors = array();
if ($maxsize && $file->filesize > $maxsize) {
$errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array(
'%filesize' => format_size($file->filesize),
'%maxsize' => format_size($maxsize),
));
}
return $errors;
}
function imce_validate_quota($file, $quota = 0, $current = 0, $type = 0) {
$errors = array();
if ($quota && $current + $file->filesize > $quota) {
$qtypes = array(
'Directory quota',
'Total user quota',
);
$errors[] = t('%filename is %filesize which would exceed your %quota_type. You are currently using %size of %quota.', array(
'%size' => format_size($current),
'%quota' => format_size($quota),
'%filesize' => format_size($file->filesize),
'%filename' => utf8_encode($file->filename),
'%quota_type' => t(isset($qtypes[$type]) ? $qtypes[$type] : 'quota'),
));
}
return $errors;
}
function imce_validate_quotas($file, &$imce, $add = 0) {
$errors = imce_validate_quota($file, $imce['quota'], $imce['dirsize'] + $add);
if (empty($errors) && $imce['tuquota']) {
$errors = imce_validate_quota($file, $imce['tuquota'], file_space_used($imce['uid']) + $add, 1);
}
if (!empty($errors)) {
drupal_set_message($errors[0], 'error');
return FALSE;
}
return TRUE;
}
function imce_image_info($file) {
if (is_file($file) && ($dot = strrpos($file, '.')) && in_array(strtolower(substr($file, $dot + 1)), array(
'jpg',
'jpeg',
'gif',
'png',
)) && ($info = @getimagesize($file)) && in_array($info[2], array(
IMAGETYPE_JPEG,
IMAGETYPE_GIF,
IMAGETYPE_PNG,
))) {
return array(
'width' => $info[0],
'height' => $info[1],
'type' => $info[2],
'mime' => $info['mime'],
);
}
return FALSE;
}
function imce_thumbnail_options($thumbs = array()) {
$options = array();
foreach ($thumbs as $thumb) {
$options[$thumb['name']] = $thumb['name'] . ' (' . $thumb['dimensions'] . ')';
}
return $options;
}
function imce_initiate_profile($user) {
if ($imce = imce_user_profile($user)) {
$imce['directories'] = imce_process_directories($imce['directories'], $user);
if (!empty($imce['directories'])) {
$imce['uid'] = (int) $user->uid;
$imce['url'] = url($_GET['q']);
$imce['clean'] = variable_get('clean_url', 0) == 1;
$imce['prvt'] = variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE;
$imce['furl'] = $imce['prvt'] ? url('system/files') : base_path() . file_directory_path();
if (variable_get('imce_settings_absurls', 0)) {
$imce['furl'] = $GLOBALS['base_root'] . $imce['furl'];
}
$imce['filesize'] *= 1048576;
$imce['quota'] *= 1048576;
$imce['tuquota'] *= 1048576;
$imce['filenum'] = (int) $imce['filenum'];
if ($info = imce_working_directory($imce)) {
$imce['direct'] = isset($imce['directories'][$info['name']]);
$imce['directories'][$info['name']] = $info;
$imce['dir'] = $info['name'];
$imce['perm'] = $info;
unset($imce['perm']['name']);
}
else {
drupal_set_message(t('Unable to get a working directory for the file browser!'), 'error');
$imce['dir'] = FALSE;
$imce['error'] = TRUE;
}
return $imce;
}
drupal_set_message(t('There is no valid directory specified for the file browser!'), 'error');
}
else {
drupal_set_message(t('You don\'t have access to any configuration profile to use the file browser!'), 'error');
}
return FALSE;
}
function imce_process_profile(&$imce) {
$scan = ($scan = variable_get('imce_custom_scan', '')) && function_exists($scan) ? $scan : 'imce_scan_directory';
$imce += $scan($imce['dir'], $imce);
foreach (variable_get('imce_custom_process', array()) as $func => $state) {
if ($state && function_exists($func)) {
$func($imce);
}
}
if (!$imce['error'] && !imce_subdirectories_accessible($imce)) {
$imce['subdirectories'] = array();
}
}
function imce_process_directories($directories, $user) {
$paths = array();
$translate = array(
'%uid' => $user->uid,
);
foreach ($directories as $directory) {
if (substr($directory['name'], 0, 4) == 'php:') {
$directory['name'] = eval(substr($directory['name'], 4));
if (is_array($directory['name'])) {
foreach ($directory['name'] as $name) {
$paths[$name] = array(
'name' => $name,
) + $directory;
}
continue;
}
}
else {
$directory['name'] = strtr($directory['name'], $translate);
}
if ($directory['name']) {
$paths[$directory['name']] = $directory;
}
}
return $paths;
}
function imce_working_directory($imce) {
if (isset($_GET['dir'])) {
if ($info = imce_directory_info($_GET['dir'], $imce)) {
if (imce_check_directory($_GET['dir'])) {
$_SESSION['imce_directory'] = rawurlencode($info['name']);
}
else {
$info = FALSE;
}
}
else {
imce_inaccessible_directory($_GET['dir']);
}
return $info;
}
if (isset($_SESSION['imce_directory'])) {
$dirname = rawurldecode($_SESSION['imce_directory']);
if ($info = imce_directory_info($dirname, $imce)) {
if (imce_check_directory($dirname)) {
return $info;
}
}
}
foreach ($imce['directories'] as $dirname => $info) {
if (imce_check_directory($dirname)) {
$_SESSION['imce_directory'] = rawurlencode($dirname);
return $info;
}
}
return FALSE;
}
function imce_check_directory($dirname) {
$root = file_directory_path();
$dirpath = $root . '/' . $dirname;
if (!file_check_directory($dirpath)) {
$path = $root;
foreach (explode('/', $dirname) as $arg) {
$path .= '/' . $arg;
if (!file_check_location($path, $root) || !file_check_directory($path, FILE_CREATE_DIRECTORY)) {
return imce_inaccessible_directory($dirname);
}
}
}
else {
if (!file_check_location($dirpath, $root)) {
return imce_inaccessible_directory($dirname);
}
}
return TRUE;
}
function imce_inaccessible_directory($dirname) {
if (is_string($dirname)) {
$msg = 'Directory "%dir" is not accessible under file system!';
$token = array(
'%dir' => utf8_encode($dirname),
);
drupal_set_message(t($msg, $token), 'error');
watchdog('imce', $msg, $token, WATCHDOG_ERROR);
}
return FALSE;
}
function imce_directory_info($dirname, $imce) {
if (isset($imce['directories'][$dirname])) {
return $imce['directories'][$dirname];
}
$info = FALSE;
$root = file_directory_path();
$dirpath = $root . '/' . $dirname;
if (imce_reg_dir($dirname) && file_check_directory($dirpath)) {
foreach ($imce['directories'] as $name => $prop) {
if ($prop['subnav'] && file_check_location($dirpath, $root . '/' . $name)) {
$info = $prop;
$info['name'] = $dirname;
}
}
}
return $info;
}
function imce_subdirectories_accessible(&$imce) {
if (!empty($imce['subdirectories'])) {
$root = file_directory_path() . '/';
$dirname = ($imce['dir'] == '.' ? '' : $imce['dir'] . '/') . $imce['subdirectories'][0];
$dirpath = $root . $dirname;
foreach ($imce['directories'] as $name => $info) {
if ($info['subnav'] && $dirname != $name && file_check_location($dirpath, $root . $name)) {
return TRUE;
}
}
}
return FALSE;
}
function imce_perm_exists(&$imce, $perm) {
static $perms = array();
if (isset($perms[$perm])) {
return $perms[$perm];
}
if (isset($imce['perm'][$perm]) && $imce['perm'][$perm]) {
return $perms[$perm] = TRUE;
}
foreach ($imce['directories'] as $name => $info) {
if (isset($info[$perm]) && $info[$perm]) {
return $perms[$perm] = TRUE;
}
}
return $perms[$perm] = FALSE;
}
function imce_scan_directory($dirname) {
$directory = array(
'dirsize' => 0,
'files' => array(),
'subdirectories' => array(),
'error' => FALSE,
);
$dirpath = file_directory_path() . '/' . $dirname;
if (!is_string($dirname) || $dirname == '' || !($handle = opendir($dirpath))) {
imce_inaccessible_directory($dirname);
$directory['error'] = TRUE;
return $directory;
}
$exclude = array(
'.' => 1,
'..' => 1,
'CVS' => 1,
'.svn' => 1,
'.htaccess' => 1,
);
while (($file = readdir($handle)) !== FALSE) {
if (isset($exclude[$file])) {
continue;
}
$path = $dirpath . '/' . $file;
if (is_dir($path)) {
$directory['subdirectories'][] = $file;
continue;
}
$width = $height = 0;
if ($img = imce_image_info($path)) {
$width = $img['width'];
$height = $img['height'];
}
$size = filesize($path);
$date = filemtime($path);
$directory['files'][$file] = array(
'name' => $file,
'size' => $size,
'width' => $width,
'height' => $height,
'date' => $date,
);
$directory['dirsize'] += $size;
}
closedir($handle);
sort($directory['subdirectories']);
return $directory;
}
function imce_create_tree(&$imce) {
$paths = array();
foreach ($imce['directories'] as $path => $arr) {
$tmp =& $paths;
if ($path != '.') {
$args = explode('/', $path);
foreach ($args as $arg) {
if (!isset($tmp[$arg])) {
$tmp[$arg] = array();
}
$tmp =& $tmp[$arg];
}
$tmp[':access:'] = TRUE;
}
if ("{$path}" == $imce['dir']) {
$tmp[':active:'] = TRUE;
foreach ($imce['subdirectories'] as $arg) {
$tmp[$arg][':access:'] = TRUE;
}
}
}
$root = variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE && !user_access('administer imce(execute PHP)') ? t('Directory root') : file_directory_path();
$q = $imce['clean'] ? '?' : '&';
if (isset($imce['directories']['.'])) {
$root = '<a href="' . $imce['url'] . $q . 'dir=." title="." class="folder' . ($imce['dir'] == '.' ? ' active' : '') . '">' . $root . '</a>';
}
else {
$root = '<a title="." class="folder disabled">' . $root . '</a>';
}
return $root . imce_tree_html($imce, $paths, $q);
}
function imce_tree_html(&$imce, $paths, $q = '?', $prefix = '', $eprefix = '') {
unset($paths[':access:'], $paths[':active:']);
$html = '';
foreach ($paths as $arg => $children) {
$path = $prefix . $arg;
$earg = rawurlencode($arg);
$epath = $eprefix . $earg;
if (isset($children[':access:']) || imce_directory_info($path, $imce)) {
$a = '<a href="' . $imce['url'] . $q . 'dir=' . $epath . '" title="' . $epath . '" class="folder' . (isset($children[':active:']) ? ' active' : '') . '">' . $earg . '</a>';
}
else {
$a = '<a title="' . $epath . '" class="folder disabled">' . $earg . '</a>';
}
$ul = imce_tree_html($imce, $children, $q, $path . '/', $epath . '/');
$html .= '<li class="' . ($ul ? 'expanded' : (isset($children[':active:']) ? 'leaf' : '')) . '">' . $a . $ul . '</li>';
}
if ($html) {
$html = '<ul>' . $html . '</ul>';
}
return $html;
}