<?php

/**
 * All plugin functions are defined here.
 *
 * @package EASYFUA
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Generate  htaccess rules.
 *
 * @return void
 */
function easyfua_htaccess_rules() {
    $path = easyfua_dir();
    $htaccess = trailingslashit( $path['dir'] ) . '.htaccess';
    $supported_types = str_replace( array(', ', ','), '|', easyfua_settings( 'supported_types' ) );
    // Htaccess not exists.
    if ( !file_exists( $htaccess ) ) {
        return;
    }
    $types = ( $supported_types ? $supported_types : easyfua_ext() );
    $file_types = explode( '|', $types );
    // Sanitize and build regex pattern.
    $file_types = array_map( function ( $ext ) {
        $ext = ltrim( strtolower( sanitize_text_field( $ext ) ), '.' );
        return preg_quote( $ext, '/' );
    }, $file_types );
    $pattern = implode( '|', $file_types );
    $rules = "Options -Indexes\n\n";
    $rules .= "<IfModule mod_headers.c>\n";
    $rules .= "  <FilesMatch \"\\.({$pattern})\$\">\n";
    $rules .= "    Header set X-Robots-Tag \"noindex, nofollow, noarchive\"\n";
    $rules .= "  </FilesMatch>\n";
    $rules .= "</IfModule>\n\n";
    // 🚫 Block direct access (PRO) - user cannot view files in the browser
    // write the new htaccess file.
    file_put_contents( $htaccess, $rules );
}

/**
 * This function will return current language if using wpml or polylang.
 *
 * @return string $lang
 */
function easyfua_lang() {
    $lang = '';
    if ( function_exists( 'pll_count_posts' ) ) {
        $lang = pll_current_language();
    } elseif ( function_exists( 'icl_object_id' ) ) {
        $lang = ICL_LANGUAGE_CODE;
    }
    $lang = ( $lang && strpos( get_option( 'WPLANG' ), $lang ) !== false || !$lang ? '' : '_' . $lang );
    return $lang;
}

/**
 * Get option settings.
 *
 * @param mixed $option_name Option name or key.
 * @return string $option    The setting value or an empty if not found.
 */
function easyfua_option(  $option_name  ) {
    $lang = easyfua_lang();
    $option = get_option( $option_name );
    if ( $lang ) {
        $option = get_option( $option_name . $lang );
    }
    return $option;
}

/**
 * Get plugin settings by name.
 *
 * @param mixed $name A plugin settings name.
 * @return mixed The settings value.
 */
function easyfua_settings(  $name  ) {
    $lang = easyfua_lang();
    $settings = get_option( 'easyfua_settings' . $lang );
    $default_settings = get_option( 'easyfua_settings' );
    if ( isset( $settings[$name] ) ) {
        // Load array settings.
        if ( is_array( $settings[$name] ) ) {
            return array_map( 'trim', $settings[$name] );
        }
        // Load default settings.
        if ( empty( $settings[$name] ) && isset( $default_settings[$name] ) ) {
            return $default_settings[$name];
        } else {
            return $settings[$name];
        }
    }
}

/**
 * Build a custom attributes.
 *
 * @param mixed $attribute_name A list of custom attributes.
 * @return string|array         File upload html attributes.
 */
function easyfua_attributes(  $attribute_name = ''  ) {
    // Allow other plugins/devs to add custom attributes.
    $allowed_types = str_replace( array(', ', ','), '|', easyfua_settings( 'supported_types' ) );
    $max_file_size = preg_replace( '/\\s*mb$/i', '', easyfua_settings( 'max_file_size' ) );
    // Default field name.
    $upload_name = apply_filters( 'easyfua_field_name', 'easyfua-file-upload' );
    // Custom attributes data options.
    $atts = array(
        'name'       => $upload_name,
        'data-name'  => $upload_name,
        'data-max'   => easyfua_settings( 'max_upload' ),
        'data-min'   => easyfua_settings( 'min_upload' ),
        'data-type'  => $allowed_types,
        'data-limit' => (int) trim( ( $max_file_size ? $max_file_size : 5 ) ) * 1024 * 1024,
    );
    // Get specific attributes.
    if ( isset( $atts[$attribute_name] ) ) {
        return esc_attr( $atts[$attribute_name] );
        // Escape attributes.
    }
    return $atts;
}

/**
 * Get all pages.
 *
 * @return array
 */
function easyfua_get_pages() {
    $pages = array();
    $limit = apply_filters( 'easyfua_page_query_limit', 50 );
    $page = 1;
    while ( true ) {
        // Fetch products in batches.
        $batch = get_posts( array(
            'post_type'      => 'page',
            'post_status'    => 'publish',
            'posts_per_page' => $limit,
            'paged'          => $page,
            'fields'         => 'ids',
            'orderby'        => 'title',
            'order'          => 'ASC',
        ) );
        if ( empty( $batch ) ) {
            break;
            // No more products.
        }
        // Merge the batch into the products array.
        $pages = array_merge( $pages, $batch );
        // Move to the next page.
        ++$page;
    }
    return $pages;
    // Return all pages objects.
}

/**
 * This function will define all the default errors.
 *
 * @param mixed $error_name Error key name.
 * @return string|void
 */
function easyfua_error(  $error_name  ) {
    // Lists of default errors.
    $errors = array(
        'server_limit'   => esc_html__( 'The uploaded file exceeds the maximum upload size of your server.', 'easy-file-upload-approval' ),
        'failed_upload'  => esc_html__( 'Uploading a file fails for any reason', 'easy-file-upload-approval' ),
        'large_file'     => esc_html__( 'Uploaded file is too large', 'easy-file-upload-approval' ),
        'invalid_type'   => esc_html__( 'Uploaded file is not allowed for file type', 'easy-file-upload-approval' ),
        'minFileUpload'  => esc_html__( 'Minimum file upload at least', 'easy-file-upload-approval' ),
        'required'       => esc_html__( 'Please upload a file before submitting.', 'easy-file-upload-approval' ),
        'restriction'    => esc_html__( 'You are not allowed to upload files.', 'easy-file-upload-approval' ),
        'maxNumFiles'    => esc_html__( 'You have reached the maximum number of files ( Only %s files allowed )', 'easy-file-upload-approval' ),
        'maxTotalSize'   => esc_html__( 'The total file(s) size exceeding the max size limit of %s.', 'easy-file-upload-approval' ),
        'max_file_limit' => esc_html__( 'Note: Some of the files are not uploaded ( Only %count% files allowed )', 'easy-file-upload-approval' ),
    );
    // Get specific error message.
    if ( isset( $errors[$error_name] ) ) {
        return $errors[$error_name];
    }
}

/**
 * Get folder name of the plugin.
 * If upload folder added in the admin then use it or else use the defaul "defined" in the plugin main file.
 *
 * @return string A sanitized folder name.
 */
function easyfua_foldername() {
    $folder = easyfua_settings( 'upload_folder_name' );
    // get folder name from the settings.
    $folder_name = ( $folder ? sanitize_title( $folder ) : EASYFUA_UPLOAD_FOLDER );
    // if not set use default defined upload folder.
    return $folder_name;
}

/**
 * Modify default Upload path or directory.
 * @param mixed $dir Newly modified path.
 */
function easyfua_modify_upload_dir(  $dir  ) {
    $folder_name = easyfua_foldername();
    $dir['subdir'] = $folder_name;
    $dir['path'] = trailingslashit( $dir['basedir'] ) . $folder_name;
    $dir['url'] = trailingslashit( $dir['baseurl'] ) . $folder_name;
    return $dir;
}

/**
 * Get plugin upload dir and url.
 *
 * @return bool|array{dir: string, url: string|bool}
 */
function easyfua_dir() {
    $wp_upload_dir = wp_upload_dir();
    $folder_name = easyfua_foldername();
    // Return upload dir and url.
    return array(
        'dir' => trailingslashit( $wp_upload_dir['basedir'] ) . $folder_name,
        'url' => trailingslashit( $wp_upload_dir['baseurl'] ) . $folder_name,
    );
}

/**
 * Plugin list of all default extensions.
 *
 * @return string
 */
function easyfua_ext() {
    return apply_filters( 'easyfua_extensions', 'jpg|jpeg|JPG|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|xls' );
}

/**
 * This function will create a regex pattern for file types.
 *
 * @param mixed $types List of all plugin file types.
 * @return string
 */
function easyfua_file_types(  $types  ) {
    $file_type_pattern = '';
    $allowed_file_types = array();
    $file_types = explode( '|', $types );
    foreach ( $file_types as $file_type ) {
        $file_type = trim( $file_type, '.' );
        $file_type = str_replace( array(
            '.',
            '+',
            '*',
            '?'
        ), array(
            '\\.',
            '\\+',
            '\\*',
            '\\?'
        ), $file_type );
        $allowed_file_types[] = $file_type;
    }
    $allowed_file_types = array_unique( $allowed_file_types );
    $file_type_pattern = implode( '|', $allowed_file_types );
    $file_type_pattern = trim( $file_type_pattern, '|' );
    $file_type_pattern = '(' . $file_type_pattern . ')';
    $file_type_pattern = '/\\.' . $file_type_pattern . '$/i';
    return $file_type_pattern;
}

/**
 * Validate File Type and blocked specific file types.
 *
 * @param string $extension       File extension name.
 * @param string $supported_types Supported file types.
 * @return bool
 */
function easyfua_validate_type(  $extension, $supported_types  ) {
    $valid = true;
    $extension = preg_replace( '/[^A-Za-z0-9,|]/', '', $extension );
    // not allowed file types.
    $not_allowed = array(
        'php',
        'php3',
        'php4',
        'phtml',
        'exe',
        'script',
        'app',
        'asp',
        'bas',
        'bat',
        'cer',
        'cgi',
        'chm',
        'cmd',
        'com',
        'cpl',
        'crt',
        'csh',
        'csr',
        'dll',
        'drv',
        'fxp',
        'flv',
        'hlp',
        'hta',
        'htaccess',
        'htm',
        'htpasswd',
        'inf',
        'ins',
        'isp',
        'jar',
        'js',
        'jse',
        'jsp',
        'ksh',
        'lnk',
        'mdb',
        'mde',
        'mdt',
        'mdw',
        'msc',
        'msi',
        'msp',
        'mst',
        'ops',
        'pcd',
        'pif',
        'pl',
        'prg',
        'ps1',
        'ps2',
        'py',
        'rb',
        'reg',
        'scr',
        'sct',
        'sh',
        'shb',
        'shs',
        'sys',
        'swf',
        'tmp',
        'torrent',
        'url',
        'vb',
        'vbe',
        'vbs',
        'vbscript',
        'wsc',
        'wsf',
        'wsf',
        'wsh'
    );
    // allowed ext.
    $allowed_ext = array('ipt');
    // Search in $not_allowed extension and match.
    foreach ( $not_allowed as $single_ext ) {
        if ( strpos( $single_ext, $extension, 0 ) !== false && !in_array( $extension, $allowed_ext, true ) ) {
            $valid = false;
            break;
        }
    }
    // If pass on first validation - check extension if exists in allowed types.
    if ( true === $valid ) {
        $extensions = explode( '|', strtolower( $supported_types ) );
        if ( !in_array( $extension, $extensions ) ) {
            $valid = false;
        }
    }
    return $valid;
}

/**
 * Check wheather a string is ASCII or not.
 *
 * @param string $string String name.
 * @return bool
 */
function easyfua_ascii(  $string  ) {
    if ( function_exists( 'mb_check_encoding' ) ) {
        if ( mb_check_encoding( $string, 'ASCII' ) ) {
            return true;
        }
    } elseif ( !preg_match( '/[^\\x00-\\x7F]/', $string ) ) {
        return true;
    }
    return false;
}

/**
 * Converts a file name to one that is not executable as a script. (from contact form 7).
 *
 * @param string $filename File raw filename.
 * @return array|string|null
 */
function easyfua_anti_script(  $filename  ) {
    $filename = wp_basename( $filename );
    // Apply part of protection logic from sanitize_file_name().
    $filename = str_replace( array(
        '?',
        '[',
        ']',
        '/',
        '\\',
        '=',
        '<',
        '>',
        ':',
        ';',
        ',',
        "'",
        '"',
        '&',
        '$',
        '#',
        '*',
        '(',
        ')',
        '|',
        '~',
        '`',
        '!',
        '{',
        '}',
        '%',
        '+',
        '’',
        '«',
        '»',
        '”',
        '“',
        chr( 0 )
    ), '', $filename );
    $filename = preg_replace( '/[\\r\\n\\t -]+/', '-', $filename );
    $filename = preg_replace( '/[\\pC\\pZ]+/iu', '', $filename );
    $parts = explode( '.', $filename );
    if ( count( $parts ) < 2 ) {
        return $filename;
    }
    $script_pattern = '/^(php|phtml|pl|py|rb|cgi|asp|aspx)\\d?$/i';
    $filename = array_shift( $parts );
    $extension = array_pop( $parts );
    foreach ( $parts as $part ) {
        if ( preg_match( $script_pattern, $part ) ) {
            $filename .= '.' . $part . '_';
        } else {
            $filename .= '.' . $part;
        }
    }
    if ( preg_match( $script_pattern, $extension ) ) {
        $filename .= '.' . $extension . '_.txt';
    } else {
        $filename .= '.' . $extension;
    }
    return $filename;
}

/**
 * This function will check if the user can upload, if plugin settings 'login required' is enabled.
 *
 * @return bool
 */
function easyfua_can_upload() {
    // Only check if login require settings is checked.
    if ( 'yes' === easyfua_settings( 'login_required' ) ) {
        // If not logged-in bail early.
        if ( !is_user_logged_in() ) {
            return false;
        }
        // Get allowed roles from the settings.
        $allowed_roles = easyfua_settings( 'roles' );
        $user = wp_get_current_user();
        $user_roles = $user->roles;
        // Check if current user match the allowed roles.
        if ( array_intersect( $allowed_roles, $user_roles ) || in_array( 'all', $allowed_roles, true ) ) {
            return true;
        }
        return false;
    }
    return true;
}

/**
 * Get max upload size from WordPress media library and convert it to MB's.
 *
 * @return string
 */
function easyfua_max_upload() {
    $max = wp_max_upload_size();
    $max_mb = $max / 1024 / 1024;
    // Hide message if max is greater than 50MB.
    if ( $max_mb > 50 ) {
        return false;
    }
    if ( $max_mb > 1024 ) {
        return round( $max_mb / 1024, 2 ) . ' GB';
    }
    return round( $max_mb, 2 ) . ' MB';
}

/**
 * Format file size to kb, mb, kb.
 *
 * @param integer $bytes Invididual file size.
 * @return string
 */
function easyfua_format_file_size(  $bytes  ) {
    if ( $bytes < 0 ) {
        return '0 KB';
    }
    // Format for KB.
    if ( $bytes < 1048576 ) {
        return number_format( $bytes / 1024, 2 ) . ' KB';
    }
    // Format for MB.
    if ( $bytes < 1073741824 ) {
        return number_format( $bytes / 1048576, 2 ) . ' MB';
    }
    // Format for GB.
    return number_format( $bytes / 1073741824, 2 ) . ' GB';
}

/**
 * Custom date format
 *
 * @param mixed $date   Provide date value.
 * @param mixed $format A new date format.
 * @return void|string  A newly formatted data.
 */
function easyfua_date(  $date, $format = null  ) {
    if ( !$date ) {
        return;
    }
    $format = ( $format ? $format : get_option( 'date_format' ) );
    $date_format = apply_filters( 'easyfua_date_format', $format );
    return mysql2date( $date_format, $date, true );
}

/**
 * Convert file URL to absolute server path.
 *
 * @param string $url File URL.
 * @return string|void Server path.
 */
function easyfua_url_to_path(  $url  ) {
    if ( !$url ) {
        return;
    }
    $upload_dir = wp_upload_dir();
    return str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $url );
}

/**
 * This function check if safe to delete file and if it's under /uploads directory.
 *
 * @param string $file_url File full url.
 * @return void|bool Wheather the file is safe to delete.
 */
function easyfua_safe_to_delete(  $file_url = null  ) {
    if ( !$file_url ) {
        return;
    }
    // Getting file url and convert url to abs path.
    $file_path = easyfua_url_to_path( $file_url );
    $upload_dir = easyfua_dir();
    // Check if dir is exists.
    if ( !empty( $file_path ) && file_exists( $file_path ) ) {
        // Normalize Path.
        $real_path = realpath( $file_path );
        $real_dir = realpath( $upload_dir['dir'] );
        // Skip if realpath is failed.
        if ( !$real_path || !$real_dir ) {
            return false;
        }
        // Old folder settings.
        $path_folder = wp_basename( dirname( $real_path ) );
        $dir_folder = wp_basename( $real_dir );
        // Check if both folder matched, if not remove parent folder in $dir.
        if ( $path_folder !== $dir_folder ) {
            $real_dir = realpath( dirname( $real_dir ) );
        }
        // Check if $real_path is main $real_dir ('wp-content/uploads/easyfup-folder') upload folder.
        // Return `0` if found at the beginning of the string otherwise is false.
        if ( strpos( $real_path, $real_dir ) === 0 ) {
            if ( !preg_match( '/\\.php$/i', $real_path ) ) {
                if ( wp_delete_file( $real_path ) ) {
                    return true;
                }
            }
        }
        return false;
    }
}

/**
 * Displaying admin notice.
 *
 * @return bool|void
 */
function easyfua_admin_notices() {
    if ( empty( $_REQUEST['result'] ) ) {
        return false;
    }
    // Check nonce.
    check_admin_referer( 'easyfua-nonce' );
    // Default admin notices/confirmation message.
    $messages = apply_filters( 'easyfua_admin_notices', array(
        'approved'       => esc_html__( 'File approved.', 'easy-file-upload-approval' ),
        'approved-multi' => esc_html__( 'Selected files approved successfully.', 'easy-file-upload-approval' ),
        'deleted'        => esc_html__( 'File deleted.', 'easy-file-upload-approval' ),
        'deleted-multi'  => esc_html__( 'Selected files deleted successfully.', 'easy-file-upload-approval' ),
        'rejected'       => esc_html__( 'File rejected.', 'easy-file-upload-approval' ),
        'rejected-multi' => esc_html__( 'Selected files rejected successfully.', 'easy-file-upload-approval' ),
        'downloaded'     => esc_html__( 'Selected files downloaded successfully.', 'easy-file-upload-approval' ),
        'notified-user'  => esc_html__( 'Notification sent to user.', 'easy-file-upload-approval' ),
    ) );
    $class_name = 'updated';
    // Find message.
    $result = sanitize_key( $_REQUEST['result'] );
    if ( isset( $messages[$result] ) ) {
        printf( '<div id="message" class="%2$s fade"><p>%1$s</p></div>', wp_kses_post( $messages[$result] ), esc_attr( $class_name ) );
        return true;
    }
}

/**
 * Load and return the contents of an email template.
 *
 * @param string $template_name Template file name (without extension).
 * @return string|null Template content or null if not found.
 */
function easyfua_email_template(  $template_name = ''  ) {
    if ( empty( $template_name ) ) {
        return null;
    }
    // Email template path.
    $template_path = EASYFUA_PLUGIN_DIR_INC . 'email/templates/email-' . sanitize_file_name( $template_name ) . '.php';
    if ( !file_exists( $template_path ) ) {
        return null;
    }
    // Return html.
    ob_start();
    include $template_path;
    return trim( ob_get_clean() );
}

/**
 * Check if the data is not empty.
 *
 * @param mixed  $data An array containing all the data.
 * @param string $key Specific name or key to check.
 * @return bool
 */
function easyfua_isset(  $data = array(), $key = ''  ) {
    // No data? bail early.
    if ( !$data || !is_array( $data ) ) {
        return false;
    }
    // Check if data is set and not empty.
    if ( isset( $data[$key] ) && !empty( $data[$key] ) ) {
        return true;
    }
    return false;
}

/**
 * Simple way to log messsage.
 *
 * @param mixed $message A string or message to log in debug file.
 * @return void
 */
function easyfua_log(  $message  ) {
    $timestamp = gmdate( 'Y-m-d H:i:s' );
    $message = ( is_array( $message ) ? json_encode( $message ) : $message );
    $dir = easyfua_dir();
    $formatted = "[{$timestamp}] {$message}\n";
    file_put_contents( $dir['dir'] . '/debug.log', $formatted, FILE_APPEND | LOCK_EX );
}

/**
 * Admin query data for file & submission data, it intended for admin WP List Table.
 *
 * @param mixed $table_name Table name where to query.
 * @param mixed $args       Query fields/args like status, submission_id, per_page, id
 * @return void|array{items: array|object, total_items: int|null}
 */
function easyfua_admin_query(  $table_name = '', $args = array()  ) {
    global $wpdb;
    if ( empty( $table_name ) || empty( $args ) ) {
        return;
    }
    // Pagination
    $per_page = ( isset( $args['per_page'] ) ? (int) $args['per_page'] : 20 );
    $current_page = ( isset( $args['current_page'] ) ? (int) $args['current_page'] : '' );
    // Build WHERE clause for filters/search.
    $placeholders = array();
    $params = array();
    // Default placeholder - set initial status query.
    if ( isset( $args['status'] ) ) {
        $placeholders[] = 'status != %s';
        $params[] = 'deleted';
    }
    // Filter by status.
    $status = array(
        'pending',
        'approved',
        'rejected',
        'partial'
    );
    if ( !empty( $args['status'] ) && in_array( $args['status'], $status, true ) ) {
        $placeholders[] = "status = %s";
        $params[] = $args['status'];
    }
    // Search String.
    if ( !empty( $args['s'] ) ) {
        if ( is_numeric( $args['s'] ) ) {
            $placeholders[] = "id = %d";
            $params[] = absint( $args['s'] );
        } elseif ( true === $args['has_filename'] ) {
            $placeholders[] = "file_name LIKE %s";
            $params[] = '%' . $wpdb->esc_like( $args['s'] ) . '%';
        }
    }
    // Filter by Submission ID.
    if ( isset( $args['submission_id'] ) && !empty( $args['submission_id'] ) ) {
        $placeholders[] = "submission_id = %d";
        $params[] = absint( $args['submission_id'] );
    }
    // Combine WHERE conditions.
    $placeholder = ( !empty( $placeholders ) ? 'WHERE ' . implode( ' AND ', $placeholders ) : '' );
    // Getting total items.
    $total_items = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}{$table_name} {$placeholder}", $params ) );
    // Default Order by & order.
    $allowed_order_by = array('id', 'submitted_at');
    $order_by = ( isset( $args['orderby'] ) && in_array( $args['orderby'], $allowed_order_by, true ) ? $args['orderby'] : 'id' );
    $order = ( isset( $args['order'] ) && 'ASC' === strtoupper( $args['order'] ) ? 'ASC' : 'DESC' );
    // Fetch only items for the current page.
    $offset = ($current_page - 1) * $per_page;
    // Prepare values.
    $prepare_values = array_merge( $params, array((int) $per_page, (int) $offset) );
    // Get submissions.
    $items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}{$table_name} {$placeholder} ORDER BY {$order_by} {$order} LIMIT %d OFFSET %d", $prepare_values ), ARRAY_A );
    // Get and return the items.
    if ( $items ) {
        return array(
            'total_items' => (int) $total_items,
            'items'       => $items,
        );
    }
}

/**
 * Check if user can use premium code or is premium plan.
 */
function easyfua_is_premium() {
    if ( function_exists( 'easyfua_fs' ) ) {
        return easyfua_fs()->can_use_premium_code();
    }
}

/**
 * Check existing plan.
 */
function easyfua_is_plan(  $plan  ) {
    if ( function_exists( 'easyfua_fs' ) ) {
        return easyfua_fs()->is_plan( $plan );
    }
}

/**
* Setup media file on json response after the successfull upload.
*/
function easyfua_media_response(  $path, $file_name  ) {
    // Default placeholder
    $preview = false;
    // Match to images ext
    $is_image = preg_match( '/\\.(jpg|jpeg|png|gif|tiff|svg|webp)$/i', $file_name );
    // Get file type
    $file_type = wp_check_filetype( $file_name );
    // Apply preview on images only
    if ( easyfua_settings( 'image_preview' ) === 'yes' ) {
        $preview = ( $is_image ? true : wp_mime_type_icon( $file_type['type'] ) );
    }
    $media_files = array(
        'path'    => $path,
        'file'    => $file_name,
        'preview' => $preview,
    );
    return $media_files;
}

/**
 * Get current date/timestamp.
 */
function __easyfua_date(  $format  ) {
    $string = date( 'Y-m-d H:i:s' );
    return get_date_from_gmt( $string, $format );
}
