我有一个使用电子邮件管道的插件。处理电子邮件时,我希望将电子邮件中的所有附件添加到WordPress媒体库中。
目前我正在使用wp_insert_attachment
, wp_generate_attachment_metadata
和wp_update_attachment_metadata
但是,使用这些功能可以将任何附加到电子邮件的文件添加到WP媒体库中。让我担心的是,它可能会让人们附加恶意文件,这些文件很容易被访问并用于恶意目的。
因此,我希望在处理附件时能够识别附件是否是受支持的文件类型。我已经调查过了wp_check_filetype
, wp_check_filetype_and_ext
和get_allowed_mime_types
我可以用它来确认文件的安全性,但除非我遗漏了什么,否则我看不到如何通过函数传递文件以确认它是否是受支持的文件类型。
如果我能识别出不受支持的文件,那么我可以创建这些文件的zip文件来上传,而不是原始文件,在我的书中,这应该可以弥补我所看到的安全漏洞。我这样做是为了通过表单上传文件,支持的文件上传media_handle_upload
和不受支持的文件通过PHP处理,添加到zip文件,然后使用wp_insert_attachment
, wp_generate_attachment_metadata
和wp_update_attachment_metadata
.
任何帮助都将不胜感激。
$upload_dir = wp_upload_dir();
$tmp_pathfile = \'/home/user/public_html/wp-content/uploads/tmp_file.js\';
$tmp_filename = \'tmp_file.js\';
$filetype = wp_check_filetype( $tmp_pathfile );
if ( is file allowed for WordPress Media Library ) { // This is where I want to check if the file is supported by the WordPress Media Library
$data = array(
\'guid\' => $upload_dir[\'url\'] . \'/\' . $tmp_filename,
\'post_title\' => $tmp_filename,
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'post_mime_type\' => $filetype[\'type\']
);
$theID = wp_insert_attachment( $data, $tmp_pathfile );
$attach_data = wp_generate_attachment_metadata( $theID, $tmp_pathfile );
wp_update_attachment_metadata( $theID, $attach_data );
} else { // File not supported by the WordPress Media Library
$zip_pathfile = $tmp_pathfile . \'.zip\';
while ( file_exists( $zip_file ) ) {
$zip_file = $tmp_pathfile . \'_\' . time() . \'.zip\';
}
$zip = new ZipArchive();
if ( $zip->open( $zip_file, ZipArchive::CREATE ) ) {
$zip->addFile( $tmp_pathfile, $tmp_filename );
$zip->close();
$filetype = wp_check_filetype( $zip_file );
$data = array(
\'guid\' => $zip_file,
\'post_mime_type\' => $filetype[\'type\'],
\'post_title\' => basename( $zip_file ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$theID = wp_insert_attachment( $data, $zip_file );
$attach_data = wp_generate_attachment_metadata( $theID, $zip_file );
wp_update_attachment_metadata( $theID, $attach_data );
unlink( $tmp_pathfile);
}
}
最合适的回答,由SO网友:David Clough 整理而成
好吧,看来我走对了方向。。。我还没有找到一个WordPress函数来满足我的需要,但我找到的最接近的是使用get_allowed_mime_types
作用
我创建了以下函数,用于检查文件是否在get_allowed_mime_types
数组,如果是,则返回true(使用wp_insert_attachment
, wp_generate_attachment_metadata
和wp_update_attachment_metadata
) 或false(文件压缩后使用wp_insert_attachment
, wp_generate_attachment_metadata
和wp_update_attachment_metadata
)
function is_upload_allowed( $file ) {
$filetype = wp_check_filetype( $file );
$file_ext = $filetype[\'ext\'];
$mimes = get_allowed_mime_types();
foreach ( $mimes as $type => $mime ) {
if ( strpos( $type, $file_ext ) !== false ) {
return true;
}
}
return false;
}
因此,我更新的代码如下所示:
$upload_dir = wp_upload_dir();
$tmp_pathfile = \'/home/user/public_html/wp-content/uploads/tmp_file.js\';
$tmp_filename = \'tmp_file.js\';
$filetype = wp_check_filetype( $tmp_pathfile );
if ( is_upload_allowed( tmp_pathfile ) ) { // This is where I want to check if the file is supported by the WordPress Media Library
$data = array(
\'guid\' => $upload_dir[\'url\'] . \'/\' . $tmp_filename,
\'post_title\' => $tmp_filename,
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'post_mime_type\' => $filetype[\'type\']
);
$theID = wp_insert_attachment( $data, $tmp_pathfile );
$attach_data = wp_generate_attachment_metadata( $theID, $tmp_pathfile );
wp_update_attachment_metadata( $theID, $attach_data );
} else { // File not supported by the WordPress Media Library
$zip_pathfile = $tmp_pathfile . \'.zip\';
while ( file_exists( $zip_file ) ) {
$zip_file = $tmp_pathfile . \'_\' . time() . \'.zip\';
}
$zip = new ZipArchive();
if ( $zip->open( $zip_file, ZipArchive::CREATE ) ) {
$zip->addFile( $tmp_pathfile, $tmp_filename );
$zip->close();
$filetype = wp_check_filetype( $zip_file );
$data = array(
\'guid\' => $zip_file,
\'post_mime_type\' => $filetype[\'type\'],
\'post_title\' => basename( $zip_file ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$theID = wp_insert_attachment( $data, $zip_file );
$attach_data = wp_generate_attachment_metadata( $theID, $zip_file );
wp_update_attachment_metadata( $theID, $attach_data );
unlink( $tmp_pathfile);
}
}
如果有一个内置的WordPress功能可以实现与
is_upload_allowed
我创建的函数。