function _imce_unzip_file_unzip in IMCE unzip 6
Same name and namespace in other branches
- 7 imce_unzip.module \_imce_unzip_file_unzip()
Unzip the files and check extensions.
1 call to _imce_unzip_file_unzip()
- imce_unzip_file in ./
imce_unzip.module - unzip a file in the file list.
File
- ./
imce_unzip.module, line 116 - Main functions for IMCE unzip module
Code
function _imce_unzip_file_unzip($file, $imce, $delete_zip_file) {
if ($file->filemime != 'application/zip') {
drupal_set_message(t("No zip file"), 'error');
return;
}
$zip = zip_open(realpath($file->filepath));
if (!is_resource($zip)) {
drupal_set_message(t('Error for %filepath = %error', array(
'%filepath' => $file->filepath,
'%error' => imce_unzip_err($zip),
)), 'error');
return;
}
if ($zip) {
$nb = 0;
$dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']);
$allowed_extensions = "";
if ($imce['extensions'] != '*') {
$allowed_extensions = explode(" ", $imce['extensions']);
}
else {
$allowed_extensions = $imce['extensions'];
}
while ($zip_entry = zip_read($zip)) {
if ($nb < $imce['maxfilestoextract'] || $imce['maxfilestoextract'] == 0) {
$filename = zip_entry_name($zip_entry);
$zdir = dirname(zip_entry_name($zip_entry));
$ext = drupal_strtolower(array_pop(explode('.', $filename)));
// Check for directory.
if ($zdir != '.' && $zdir != '__MACOSX') {
$newdirpath = $dirpath . '/' . $zdir;
if (!file_exists($newdirpath)) {
if (module_exists('imce_mkdir') && $imce['perm']['mkdir'] && (!$imce['mkdirnum'] || $imce['mkdirnum'] > count($imce['subdirectories']))) {
drupal_load('module', 'imce_mkdir');
module_load_include('inc', 'imce_mkdir', 'imce_mkdir');
$tmp = '';
$tmp1 = '';
$imcedir = $imce['dir'];
foreach (explode('/', $zdir) as $k) {
$tmp .= $k . '/';
if (!file_exists($dirpath . '/' . $tmp)) {
$imce['dir'] .= $tmp1;
imce_mkdir_batch($imce, array(
$k,
));
}
$tmp1 .= '/' . $k;
}
$imce['dir'] = $imcedir;
}
else {
$filename = basename(zip_entry_name($zip_entry));
}
}
}
if (drupal_strtolower($filename) != $ext && !preg_match('/^\\._/', $filename) && !preg_match('/^__MACOSX/', $filename)) {
$afile = new stdClass();
$afile->filepath = $dirpath . '/' . $filename;
$afile->filename = basename($afile->filepath);
$replace = variable_get('imce_settings_replace', FILE_EXISTS_RENAME);
$afile->destination = file_destination(file_create_path($afile->filepath), $replace);
$validators = imce_validate_all($afile, $imce);
if (!empty($validators)) {
drupal_set_message(check_plain($validators), 'error');
}
else {
// Validation ok.
$new_file = new stdClass();
$new_file->filepath = $afile->destination;
$new_file->filename = basename($new_file->filepath);
$new_file->filemime = file_get_mimetype($new_file->filename);
$new_file->uid = $imce['uid'];
$new_file->timestamp = time();
$new_file->status = FILE_STATUS_PERMANENT;
$new_file->filesize = zip_entry_filesize($zip_entry);
$width = 0;
$height = 0;
// Check for image.
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$image = @imagecreatefromstring($buf);
if ($image) {
$width = imagesx($image);
$height = imagesy($image);
}
zip_entry_close($zip_entry);
}
// Process image.
if ($width && $height) {
list($maxw, $maxh) = explode('x', $imce['dimensions']);
if ($width > $maxw && $maxw != 0 || $height > $maxh && $maxh != 0) {
$scaled_file = _imce_unzip_scale_image($imce, $new_file, $width, $maxw, $height, $maxh, $buf);
if ($scaled_file) {
drupal_set_message(t('Extraction and scaling of %filename.', array(
'%filename' => $scaled_file->filename,
), 'succes'));
}
}
else {
$new_file->width = $width;
$new_file->height = $height;
$fp = fopen($new_file->filepath, "w+");
fwrite($fp, $buf);
fclose($fp);
$update = array();
if ($_file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'", $new_file->filepath))) {
$new_file->fid = $_file->fid;
$update[] = 'fid';
}
drupal_write_record('files', $new_file, $update);
imce_add_file($new_file, $imce);
drupal_set_message(t('Extraction of %filename.', array(
'%filename' => $new_file->filename,
), 'succes'));
}
}
else {
$fp = @fopen($new_file->filepath, "w+");
if ($fp) {
fwrite($fp, $buf);
fclose($fp);
$update = array();
if ($_file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'", $new_file->filepath))) {
$new_file->fid = $_file->fid;
$update[] = 'fid';
}
drupal_write_record('files', $new_file, $update);
imce_add_file($new_file, $imce);
drupal_set_message(t('Extraction of %filename', array(
'%filename' => $new_file->filename,
), 'succes'));
}
else {
drupal_set_message(t("Can't create %filename", array(
'%filename' => $new_file->filename,
), 'error'));
}
}
$nb++;
// End validator empty.
}
// End ext != filename.
}
// End nb extracted files.
}
// End while $zip_entry = zip_read($zip).
}
if ($imce['perm']['delete'] && $delete_zip_file) {
$deleted = imce_process_files(array(
$file->filename,
), $imce, 'imce_delete_file');
if (!empty($deleted)) {
drupal_set_message(t('File deletion successful: %files.', array(
'%files' => utf8_encode(implode(', ', $deleted)),
)));
}
}
zip_close($zip);
}
else {
drupal_set_message(t('Error for %filepath', array(
'%files' => $file->filepath,
)), 'error');
}
return;
}