Esempio n. 1
0
function ninja_forms_field_upload_move_uploads($field_id, $file_data, $multi = false)
{
    global $ninja_forms_processing;
    if ($ninja_forms_processing->get_form_setting('create_post')) {
        $create_post = $ninja_forms_processing->get_form_setting('create_post');
    } else {
        $create_post = 0;
    }
    $plugin_settings = get_option('ninja_forms_settings');
    $field_data = $ninja_forms_processing->get_field_settings($field_id);
    $field_data = $field_data['data'];
    if (isset($field_data['upload_types'])) {
        $upload_types = $field_data['upload_types'];
    } else {
        $upload_types = '';
    }
    if (isset($field_data['upload_rename'])) {
        $upload_rename = $field_data['upload_rename'];
    } else {
        $upload_rename = '';
    }
    if (isset($field_data['email_attachment'])) {
        $email_attachment = $field_data['email_attachment'];
    } else {
        $email_attachment = '';
    }
    if (isset($plugin_settings['base_upload_dir'])) {
        $base_upload_dir = $plugin_settings['base_upload_dir'];
    }
    if (isset($plugin_settings['base_upload_url'])) {
        $base_upload_url = $plugin_settings['base_upload_url'];
    } else {
        $base_upload_url = '';
    }
    if (isset($plugin_settings['custom_upload_dir'])) {
        $custom_upload_dir = $plugin_settings['custom_upload_dir'];
    } else {
        $custom_upload_dir = '';
    }
    $random_string = ninja_forms_random_string(5);
    $tmp_upload_file = $base_upload_dir . '/tmp/' . $random_string . '/';
    if (is_dir($tmp_upload_file)) {
        rmdir($tmp_upload_file);
    }
    mkdir($tmp_upload_file);
    $tmp_upload_file .= 'ninja_forms_field_' . $field_id;
    $file_name = '';
    move_uploaded_file($file_data['tmp_name'], $tmp_upload_file);
    $user_file_name = $file_data['name'];
    $orig_user_file_name = $user_file_name;
    if ($multi) {
        $update_array = $ninja_forms_processing->get_field_value($field_id);
        if (!is_array($update_array) or empty($update_array)) {
            $update_array = array();
        }
    } else {
        $update_array = array();
    }
    $user_file_array = array();
    if ($user_file_name != '') {
        //Trim whitespace and replace special characters from our file name.
        $user_file_name = stripslashes(trim($user_file_name));
        $user_file_name = strtolower($user_file_name);
        $user_file_name = preg_replace("/[\\/\\&%#\$]/", "", $user_file_name);
        $user_file_name = preg_replace("/[\"\\']/", "", $user_file_name);
        $user_file_name = preg_replace("/\\s+/", "", $user_file_name);
        $user_file_array = explode(".", $user_file_name);
        $ext = array_pop($user_file_array);
        if (isset($upload_types) and !empty($upload_types)) {
            if (strpos($upload_types, $ext) === false) {
                $ninja_forms_processing->add_error('upload_' . $field_id, __('File type is not allowed: ' . $user_file_name, 'ninja-forms'), $field_id);
            }
        }
        $form_title = strtolower(stripslashes(trim($ninja_forms_processing->get_form_setting('form_title'))));
        $form_title = preg_replace("/[\\/\\&%#\$]/", "", $form_title);
        $form_title = preg_replace("/[\"\\']/", "", $form_title);
        $form_title = preg_replace('/\\s+/', '', $form_title);
        if (is_user_logged_in()) {
            $current_user = wp_get_current_user();
            $user_name = $current_user->user_nicename;
            $user_id = $current_user->ID;
            $display_name = $current_user->display_name;
            $first_name = $current_user->user_firstname;
            $last_name = $current_user->user_lastname;
        } else {
            $user_name = '';
            $display_name = '';
            $first_name = '';
            $last_name = '';
            $user_id = '';
        }
        //If we have a file naming convention, use it to change our file name.
        if (!empty($upload_rename)) {
            if (is_array($user_file_array) and !empty($user_file_array)) {
                $user_file_name = implode($user_file_array);
            }
            $user_file_name = stripslashes(trim($user_file_name));
            $user_file_name = preg_replace('/[^a-zA-Z0-9_ %\\[\\]\\.\\(\\)%&-]/s', '', $user_file_name);
            $file_name = str_replace("%filename%", $user_file_name, $upload_rename);
            $file_name = str_replace("%formtitle%", $form_title, $file_name);
            $file_name = str_replace("%date%", date('Y-m-d'), $file_name);
            $file_name = str_replace("%month%", date('m'), $file_name);
            $file_name = str_replace("%day%", date('d'), $file_name);
            $file_name = str_replace("%year%", date('Y'), $file_name);
            $file_name = str_replace("%username%", $user_name, $file_name);
            $file_name = str_replace("%userid%", $user_id, $file_name);
            $file_name = str_replace("%displayname%", $display_name, $file_name);
            $file_name = str_replace("%firstname%", $first_name, $file_name);
            $file_name = str_replace("%lastname%", $last_name, $file_name);
            $file_name = str_replace("%random%", ninja_forms_random_string(5), $file_name);
            $file_name .= '.' . $ext;
        } else {
            $user_file_name = stripslashes(trim($user_file_name));
            $user_file_name = preg_replace('/[^a-zA-Z0-9_ %\\[\\]\\.\\(\\)%&-]/s', '', $user_file_name);
            $file_name = $user_file_name;
        }
        if ($custom_upload_dir != '') {
            $custom_upload_dir = stripslashes(trim($custom_upload_dir));
            $custom_upload_dir = str_replace("%filename%", $user_file_name, $custom_upload_dir);
            $custom_upload_dir = str_replace("%formtitle%", $form_title, $custom_upload_dir);
            $custom_upload_dir = str_replace("%date%", date('Y-m-d'), $custom_upload_dir);
            $custom_upload_dir = str_replace("%month%", date('m'), $custom_upload_dir);
            $custom_upload_dir = str_replace("%day%", date('d'), $custom_upload_dir);
            $custom_upload_dir = str_replace("%year%", date('Y'), $custom_upload_dir);
            $custom_upload_dir = str_replace("%username%", $user_name, $custom_upload_dir);
            $custom_upload_dir = str_replace("%userid%", $user_id, $custom_upload_dir);
            $custom_upload_dir = str_replace("%displayname%", $display_name, $custom_upload_dir);
            $custom_upload_dir = str_replace("%firstname%", $first_name, $custom_upload_dir);
            $custom_upload_dir = str_replace("%lastname%", $last_name, $custom_upload_dir);
        }
        $upload_path = $base_upload_dir . $custom_upload_dir;
        $custom_upload_url = str_replace("\\", "/", $custom_upload_dir);
        $file_url = $base_upload_url . $custom_upload_url;
        $file_url = apply_filters('ninja_forms_uploads_url', $file_url, $field_id);
        $file_url = trailingslashit($file_url);
        $file_dir = $upload_path . '/' . $file_name;
        // Check to see if the file already exists. If it does, add numbers to the end until we find one that doesn't.
        $file_name_array = ninja_forms_check_file_exists($upload_path, $file_name);
        $file_name = $file_name_array['file_name'];
        $base_name = $file_name_array['base_name'];
        if (ninja_forms_check_file_name_in_update_array($file_name, $update_array)) {
            $x = 1;
            while (ninja_forms_check_file_name_in_update_array($file_name, $update_array)) {
                if ($x < 9) {
                    $num = "00" . $x;
                } else {
                    if ($x > 9 and $x < 99) {
                        $num = "0" . $x;
                    } else {
                        $num = $x;
                    }
                }
                $name = $base_name . '-' . $num;
                if ($ext != '') {
                    $tmp_name = $name . '.' . $ext;
                } else {
                    $tmp_name = $name;
                }
                $file_name_array = ninja_forms_check_file_exists($upload_path, $tmp_name, $base_name, $x);
                $file_name = $file_name_array['file_name'];
                $x++;
            }
        }
    }
    $file_url .= $file_name;
    $tmp_array = array('user_file_name' => $orig_user_file_name, 'file_name' => $file_name, 'file_path' => $tmp_upload_file, 'file_url' => $file_url, 'complete' => 0);
    //$tmp_array['user_file_name'] = $orig_user_file_name;
    //$tmp_array['file_name'] = $file_name;
    //$tmp_array['file_path'] = $tmp_upload_file;
    //$tmp_array['file_url'] = $file_url;
    //$tmp_array['complete'] = 0;
    //array_push( $update_array, $tmp_array );
    if (isset($file_data['key'])) {
        $file_key = $file_data['key'];
    } else {
        $file_key = ninja_forms_field_uploads_create_key($update_array);
    }
    $update_array[$file_key] = $tmp_array;
    $update_array = apply_filters('ninja_forms_upload_pre_process_array', $update_array);
    $ninja_forms_processing->update_field_value($field_id, $update_array);
}
Esempio n. 2
0
function ninja_forms_post_edit_file_attachment_filter($data, $field_id)
{
    global $post;
    $field_row = ninja_forms_get_field_by_id($field_id);
    $field_type = $field_row['type'];
    if ($field_type == '_upload' and is_object($post)) {
        $args = array('post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => $post->ID);
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {
                $attach_field = get_post_meta($attachment->ID, 'ninja_forms_field_id', true);
                $file_key = get_post_meta($attachment->ID, 'ninja_forms_file_key', true);
                $upload_id = get_post_meta($attachment->ID, 'ninja_forms_upload_id', true);
                if ($attach_field == $field_id) {
                    $filename = basename(get_attached_file($attachment->ID));
                    $filepath = str_replace($filename, '', get_attached_file($attachment->ID));
                    $data['default_value'][$file_key] = array('user_file_name' => $filename, 'file_name' => $filename, 'file_path' => $filepath, 'file_url' => wp_get_attachment_url($attachment->ID), 'complete' => 1, 'upload_id' => $upload_id);
                }
            }
        }
        if (isset($field_row['data']['featured_image']) and $field_row['data']['featured_image'] == 1) {
            $post_thumbnail_id = get_post_thumbnail_id($post->ID);
            if ($post_thumbnail_id != '') {
                $attach_field = get_post_meta($post_thumbnail_id, 'ninja_forms_field_id', true);
                if ($attach_field == '') {
                    $file_key = ninja_forms_field_uploads_create_key(array());
                    $upload_id = '';
                    $filename = basename(get_attached_file($post_thumbnail_id));
                    $filepath = str_replace($filename, '', get_attached_file($post_thumbnail_id));
                    $data['default_value'][$file_key] = array('user_file_name' => $filename, 'file_name' => $filename, 'file_path' => $filepath, 'file_url' => wp_get_attachment_url($post_thumbnail_id), 'complete' => 1, 'upload_id' => $upload_id);
                }
            }
        }
        if (isset($data['default_value']) and is_array($data['default_value'])) {
            uasort($data['default_value'], 'ninja_forms_compare_file_name');
        }
    }
    return $data;
}