function _imce_unzip_file_unzip in IMCE unzip 7
Same name and namespace in other branches
- 6 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 120 - 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"), NULL, array(
'context' => 'error',
));
return;
}
$zip = zip_open(realpath($file->uri));
if (!is_resource($zip)) {
drupal_set_message(t('Error for %uri = %error', array(
'%uri' => $file->uri,
'%error' => imce_unzip_err($zip),
)), array(
'context' => 'error',
));
return;
}
if ($zip) {
$nb = 0;
$dirpath = drupal_realpath(imce_dir_uri($imce));
$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);
// zipped filename of file
$zdir = dirname(zip_entry_name($zip_entry));
// zipped directory of file
$ext = drupal_strtolower(array_pop(explode('.', $filename)));
// zipped extension of file
// Validate paths; ignore / do not extract Mac system files.
if (drupal_strtolower($filename) != $ext && !preg_match('/^\\._/', $filename) && !preg_match('/\\.DS_Store$/', $filename) && !preg_match('/^__MACOSX/', $filename)) {
// if $filename does not begin with '__MACOSX'
// Create directory paths.
$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));
}
}
// Create file paths.
$afile = new stdClass();
$afile->uri = $dirpath . '/' . $filename;
$afile->filename = basename($afile->uri);
$afile->filesize = zip_entry_filesize($zip_entry);
$replace = variable_get('imce_settings_replace', FILE_EXISTS_RENAME);
$afile->destination = file_destination(file_prepare_directory(dirname($afile->uri), FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY) ? $afile->uri : file_directory_temp() . '/_imce_unzip/' . $filename, $replace);
// TODO: return error if file_prepare_directory() returns FALSE, rather than specifying sub-directory of temp directory as destination
$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->uri = $afile->destination;
$new_file->filename = basename($new_file->uri);
$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 && $imce['dimensions'] != 0) {
list($maxw, $maxh) = explode('x', $imce['dimensions']);
if ($width > $maxw || $height > $maxh) {
$scaled_file = _imce_unzip_scale_image($imce, $new_file, $maxw, $maxh, $buf, $zdir);
if ($scaled_file) {
drupal_set_message(t('Extraction and scaling of %filename to fit maximum dimensions of ' . $maxw . 'x' . $maxh . ' pixels.', array(
'%filename' => $scaled_file->filename,
), array(
'context' => 'success',
)));
}
}
else {
$fp = fopen($new_file->uri, "w+");
fwrite($fp, $buf);
fclose($fp);
$new_file->width = $width;
$new_file->height = $height;
$new_file->uri = imce_dir_uri($imce) . ($zdir != '.' ? $zdir . '/' : '') . $new_file->filename;
// stream wrapper URI relative to IMCE working directory
// If entry for file already exists in database, register update
$update = array();
if ($_file = db_query("SELECT f.* FROM {file_managed} f WHERE f.uri = :path", array(
':path' => $new_file->uri,
))
->fetchObject()) {
$new_file->fid = $_file->fid;
$update[] = 'fid';
}
// Register file in database
drupal_write_record('file_managed', $new_file, $update);
$new_file->fid = db_query("SELECT fid FROM {file_managed} f WHERE f.uri = :path", array(
':path' => $new_file->uri,
))
->fetchField();
imce_add_file($new_file, $imce);
imce_file_register($new_file);
drupal_set_message(t('Extraction of %filename.', array(
'%filename' => $new_file->filename,
), array(
'context' => 'success',
)));
}
}
else {
$fp = fopen($new_file->uri, "w+");
if ($fp) {
fwrite($fp, $buf);
fclose($fp);
$new_file->width = 0;
$new_file->height = 0;
$new_file->uri = imce_dir_uri($imce) . ($zdir != '.' ? $zdir . '/' : '') . $new_file->filename;
// stream wrapper URI relative to IMCE working directory
// If entry for file already exists in database, register update
$update = array();
if ($_file = db_query("SELECT f.* FROM {file_managed} f WHERE f.uri = :path", array(
':path' => $new_file->uri,
))
->fetchObject()) {
$new_file->fid = $_file->fid;
$update[] = 'fid';
}
// Register file in database
drupal_write_record('file_managed', $new_file, $update);
$new_file->fid = db_query("SELECT fid FROM {file_managed} f WHERE f.uri = :path", array(
':path' => $new_file->uri,
))
->fetchField();
imce_add_file($new_file, $imce);
imce_file_register($new_file);
drupal_set_message(t('Extraction of %filename', array(
'%filename' => $new_file->filename,
), array(
'context' => 'success',
)));
}
else {
drupal_set_message(t("Can't create %filename", array(
'%filename' => $new_file->filename,
), array(
'context' => '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(check_plain(t('Error for ' . $file->uri)), array(
'context' => 'error',
));
}
return;
}