Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Helpers/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server
<?php
namespace KS_PAC_DCFH\Helpers;
if (!defined('ABSPATH')) {
exit;
}
class FileUtils
{
private static $_instance;
private static string $folderName = 'pwh-dcfh-uploads';
public static function instance(): self
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
public static function create_dir(string $folder, bool $in_base = true): bool
{
$wp_upload_dir = wp_upload_dir();
$dir = $in_base ? path_join($wp_upload_dir['basedir'], $folder) : path_join($wp_upload_dir['path'], $folder);
if (!is_dir($dir)) {
wp_mkdir_p($dir);
self::create_security_files($dir);
return true;
}
return false;
}
public static function get_subdir(): array
{
$time = current_time('mysql');
return [substr($time, 0, 4), substr($time, 5, 2)];
}
public static function get_wp_upload_dir(string $folder, ?string $base = null)
{
$wp_upload_dir = wp_upload_dir();
$dirs = [
'basedir' => path_join($wp_upload_dir['basedir'], $folder),
'baseurl' => path_join($wp_upload_dir['baseurl'], $folder),
];
return $base ? ($dirs[$base] ?? '') : $dirs;
}
public static function upload_files_to_dir(string $contact_form_id, string $filename, bool $is_increment = false)
{
[$year, $month] = self::get_subdir();
$relative_path = path_join(self::$folderName, "forms/$contact_form_id/$year/$month/");
$base_dir = self::get_wp_upload_dir($relative_path, 'basedir');
$base_url = self::get_wp_upload_dir($relative_path, 'baseurl');
if (!is_dir($base_dir) && !wp_mkdir_p($base_dir)) {
return false;
}
self::create_security_files($base_dir);
$file_ext = '.'.pathinfo($filename, PATHINFO_EXTENSION);
$base_name = sanitize_file_name(wp_basename($filename, $file_ext));
$file_path = path_join($base_dir, $base_name.$file_ext);
$counter = 1;
while ($is_increment && file_exists($file_path)) {
$file_path = path_join($base_dir, $base_name.$counter.$file_ext);
$counter++;
}
$file_url = str_replace($base_dir, $base_url, $file_path);
return ['basedir' => $file_path, 'baseurl' => $file_url];
}
public static function upload_files_to_media_library(string $file, string $title = ''): array
{
$attachments = ['attachment_id' => '', 'attachment_url' => ''];
if (!et_()->WPFS()->is_file($file) || !et_()->WPFS()->exists($file)) {
return $attachments;
}
require_once ABSPATH.'wp-admin/includes/image.php';
$upload = wp_upload_dir();
$filename = pathinfo($file, PATHINFO_FILENAME);
$basename = wp_basename($file);
$upload_path = path_join($upload['path'], $basename);
$upload_url = path_join($upload['url'], $basename);
if (!copy($file, $upload_path)) {
return $attachments;
}
$file_type = wp_check_filetype($basename);
$attachment = [
'post_title' => $filename,
'post_name' => $filename,
'post_content' => $title ?: __('Divi Contact Form Helper', 'divi-contact-form-helper'),
'post_excerpt' => $title,
'post_mime_type' => $file_type['type'],
'guid' => $upload_url,
'post_status' => 'inherit',
'post_author' => get_current_user_id(),
'comment_status' => 'closed',
'ping_status' => 'closed',
];
$attachmentId = wp_insert_attachment($attachment, $upload_path);
if (!is_wp_error($attachmentId)) {
$meta = wp_generate_attachment_metadata($attachmentId, $upload_path);
wp_update_attachment_metadata($attachmentId, $meta);
update_post_meta($attachmentId, '_wp_attachment_image_alt', $title);
$attachments = [
'attachment_id' => $attachmentId,
'attachment_url' => wp_get_attachment_url($attachmentId),
];
}
return $attachments;
}
public static function get_tmp_upload_dir(): string
{
return self::get_wp_upload_dir(path_join(self::$folderName, 'tmp'), 'basedir');
}
public static function get_tmp_upload_url(): string
{
return self::get_wp_upload_dir(path_join(self::$folderName, 'tmp'), 'baseurl');
}
public static function get_form_upload_dir(string $contact_form_id, string $subdir, string $file): string
{
$folder = path_join(self::$folderName, "forms/$contact_form_id/$subdir");
return path_join(self::get_wp_upload_dir($folder, 'basedir'), $file);
}
public static function get_form_upload_url(string $contact_form_id, string $subdir, string $file): string
{
$folder = path_join(self::$folderName, "forms/$contact_form_id/$subdir");
return path_join(self::get_wp_upload_dir($folder, 'baseurl'), $file);
}
public static function delete_post_attached_file(string $filename, string $contact_form_id, string $subdir): void
{
$paths = [
self::get_form_upload_dir($contact_form_id, $subdir, $filename),
path_join(self::get_tmp_upload_dir(), $filename),
];
foreach ($paths as $path) {
if (et_()->WPFS()->is_file($path) && et_()->WPFS()->exists($path)) {
wp_delete_file($path);
}
}
}
public static function is_temp_dir_exists(): bool
{
return is_dir(self::get_tmp_upload_dir());
}
public static function get_wp_allowed_mime_types(): array
{
$mime_types = [];
foreach (get_allowed_mime_types() as $key => $value) {
$mime_types[$key] = $value;
if ('css' === $key) {
$mime_types['htm|html'] = 'text/html';
} elseif ('rtf' === $key) {
$mime_types['js'] = 'application/javascript';
}
}
return $mime_types;
}
private static function create_security_files(string $dir): void
{
$files = [
'index.php' => "<?php // Silence is golden.",
'.htaccess' => "## Prevent Directory Indexes\n\nOptions -Indexes",
];
foreach ($files as $file => $content) {
$path = path_join($dir, $file);
if (!et_()->WPFS()->exists($path)) {
et_()->WPFS()->put_contents($path, $content);
}
}
}
}