/**
 * Import/Export form data.
 * 
 * @return type 
 */
function wpcf_admin_import_export_form()
{
    $form = array();
    $form['wpnonce'] = array('#type' => 'hidden', '#name' => '_wpnonce', '#value' => wp_create_nonce('wpcf_import'));
    if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wpcf_import')) {
        if (isset($_POST['import-final'])) {
            if ($_POST['mode'] == 'file' && !empty($_POST['file']) && file_exists(urldecode($_POST['file']))) {
                $info = pathinfo(urldecode($_POST['file']));
                $is_zip = $info['extension'] == 'zip' ? true : false;
                if ($is_zip) {
                    $zip = zip_open(urldecode($_POST['file']));
                    if (is_resource($zip)) {
                        while (($zip_entry = zip_read($zip)) !== false) {
                            if (zip_entry_name($zip_entry) == 'settings.xml') {
                                $data = @zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                            }
                        }
                    } else {
                        echo '<div class="message error"><p>' . __('Unable to open zip file', 'wpcf') . '</p></div>';
                        return array();
                    }
                } else {
                    $data = @file_get_contents(urldecode($_POST['file']));
                }
                if ($data) {
                    wpcf_admin_import_data($data);
                } else {
                    echo '<div class="message error"><p>' . __('Unable to process file', 'wpcf') . '</p></div>';
                    return array();
                }
            }
            if ($_POST['mode'] == 'text' && !empty($_POST['text'])) {
                $charset = !empty($_POST['text-encoding']) ? $_POST['text-encoding'] : get_option('blog_charset');
                wpcf_admin_import_data(stripslashes(html_entity_decode($_POST['text'], ENT_QUOTES, $charset)));
            }
        } else {
            if (isset($_POST['step'])) {
                $mode = 'none';
                $data = '';
                if (!empty($_POST['import-file']) && !empty($_FILES['file']['tmp_name'])) {
                    if ($_FILES['file']['type'] == 'text/xml') {
                        $_FILES['file']['name'] .= '.txt';
                    }
                    $_POST['action'] = 'wp_handle_upload';
                    $uploaded_file = wp_handle_upload($_FILES['file'], array('test_form' => false, 'upload_error_handler' => 'wpcf_admin_import_export_file_upload_error'));
                    if (isset($uploaded_file['error'])) {
                        return array();
                    }
                    if (empty($uploaded_file['file'])) {
                        echo '<div class="message error"><p>' . __('Error uploading file', 'wpcf') . '</p></div>';
                        return array();
                    }
                    $info = pathinfo($uploaded_file['file']);
                    $is_zip = $info['extension'] == 'zip' ? true : false;
                    if ($is_zip) {
                        $zip = zip_open($uploaded_file['file']);
                        if (is_resource($zip)) {
                            while (($zip_entry = zip_read($zip)) !== false) {
                                if (zip_entry_name($zip_entry) == 'settings.xml') {
                                    $data = @zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                                }
                            }
                        } else {
                            echo '<div class="message error"><p>' . __('Unable to open zip file', 'wpcf') . '</p></div>';
                            return array();
                        }
                    } else {
                        $data = @file_get_contents($uploaded_file['file']);
                    }
                    $form['file'] = array('#type' => 'hidden', '#name' => 'file', '#value' => urlencode($uploaded_file['file']));
                    $mode = 'file';
                } else {
                    if (!empty($_POST['import-text']) && !empty($_POST['text'])) {
                        $data = stripslashes($_POST['text']);
                        if (preg_match('/encoding=("[^"]*"|\'[^\']*\')/s', $data, $match)) {
                            $charset = trim($match[1], '"');
                        } else {
                            $charset = !empty($_POST['text-encoding']) ? $_POST['text-encoding'] : get_option('blog_charset');
                        }
                        $form['text'] = array('#type' => 'hidden', '#name' => 'text', '#value' => htmlentities(stripslashes($_POST['text']), ENT_QUOTES, $charset));
                        $form['text-encoding'] = array('#type' => 'hidden', '#name' => 'text-encoding', '#value' => $charset);
                        $mode = 'text';
                    }
                }
                if (empty($data)) {
                    echo '<div class="message error"><p>' . __('Data not valid', 'wpcf') . '</p></div>';
                    return array();
                }
                $data = wpcf_admin_import_export_settings($data);
                if (empty($data)) {
                    echo '<div class="message error"><p>' . __('Data not valid', 'wpcf') . '</p></div>';
                    return array();
                }
                $form = array_merge($form, $data);
                $form['mode'] = array('#type' => 'hidden', '#name' => 'mode', '#value' => $mode);
                $form['import-final'] = array('#type' => 'hidden', '#name' => 'import-final', '#value' => 1);
                $form['submit'] = array('#type' => 'submit', '#name' => 'import', '#value' => __('Import', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
            }
        }
    } else {
        $form['embedded-settings'] = array('#type' => 'radios', '#name' => 'embedded-settings', '#title' => __('When importing to theme:', 'wpcf'), '#options' => array(__('ask user for approval', 'wpcf') => 'ask', __('import automatically', 'wpcf') => 'auto'), '#inline' => true, '#before' => '<h2>' . __('Export Types data', 'wpcf') . '</h2>' . __('Download all custom fields, custom post types and taxonomies created by Types plugin.', 'wpcf') . '<br /><br />');
        $form['submit'] = array('#type' => 'submit', '#name' => 'export', '#value' => __('Export', 'wpcf'), '#attributes' => array('class' => 'button-primary'), '#after' => '<br /><br />');
        if (extension_loaded('simplexml')) {
            $attributes = !wpcf_admin_import_dir() ? array('disabled' => 'disabled') : array();
            $form['file'] = array('#type' => 'file', '#name' => 'file', '#prefix' => __('Upload XML file', 'wpcf') . '<br />', '#before' => '<h2>' . __('Import Types data file', 'wpcf') . '</h2>', '#inline' => true, '#attributes' => $attributes);
            $form['submit-file'] = array('#type' => 'submit', '#name' => 'import-file', '#value' => __('Import file', 'wpcf'), '#attributes' => array_merge($attributes, array('class' => 'button-primary')), '#prefix' => '<br />', '#suffix' => '<br /><br />');
            $form['text'] = array('#type' => 'textarea', '#title' => __('Paste code here', 'wpcf'), '#name' => 'text', '#attributes' => array('rows' => 20), '#before' => '<h2>' . __('Import Types data text input', 'wpcf') . '</h2>');
            $form['text-encoding'] = array('#type' => 'textfield', '#title' => __('Encoding', 'wpcf'), '#name' => 'text-encoding', '#value' => get_option('blog_charset'), '#description' => __('If encoding is set in text input, it will override this setting.', 'wpcf'));
            $form['submit-text'] = array('#type' => 'submit', '#name' => 'import-text', '#value' => __('Import text', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
            $form['step'] = array('#type' => 'hidden', '#name' => 'step', '#value' => 1);
        } else {
            echo '<div class="message error"><p>' . __('PHP SimpleXML extension not loaded: Importing not available', 'wpcf') . '</p></div>';
        }
    }
    return $form;
}
/**
 * Import/Export form data.
 *
 * @return type
 *
 * @deprecated in 2.0, to remove
 */
function wpcf_admin_import_export_form()
{
    $form = array();
    $form['wpnonce'] = array('#type' => 'hidden', '#name' => '_wpnonce', '#value' => wp_create_nonce('wpcf_import'));
    $form_base = $form;
    $show_first_screen = true;
    if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'wpcf_import')) {
        $show_first_screen = false;
        if (isset($_POST['import-final'])) {
            if ($_POST['mode'] == 'file' && !empty($_POST['file'])) {
                $file = get_transient(sanitize_text_field($_POST['file']));
                if (file_exists($file)) {
                    $info = pathinfo($file);
                    $is_zip = $info['extension'] == 'zip' ? true : false;
                    if ($is_zip) {
                        $zip = zip_open($file);
                        if (is_resource($zip)) {
                            while (($zip_entry = zip_read($zip)) !== false) {
                                if (zip_entry_name($zip_entry) == 'settings.xml') {
                                    $data = @zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                                }
                            }
                        } else {
                            echo '<div class="message error"><p>' . __('Unable to open zip file', 'wpcf') . '</p></div>';
                            return array();
                        }
                    } else {
                        $data = @file_get_contents($file);
                    }
                    @unlink($file);
                    if ($data) {
                        wpcf_admin_import_data($data);
                    } else {
                        echo '<div class="message error"><p>' . __('Unable to process file', 'wpcf') . '</p></div>';
                        return array();
                    }
                } else {
                    echo '<div class="message error"><p>' . __('Unable to process file', 'wpcf') . '</p></div>';
                    return array();
                }
            }
            if ($_POST['mode'] == 'text' && !empty($_POST['text'])) {
                $charset = !empty($_POST['text-encoding']) ? sanitize_text_field($_POST['text-encoding']) : get_option('blog_charset');
                wpcf_admin_import_data(stripslashes(html_entity_decode($_POST['text'], ENT_QUOTES, $charset)));
            }
        } elseif (isset($_POST['step'])) {
            $mode = 'none';
            $data = '';
            if (!empty($_POST['import-file']) && !empty($_FILES['file']['tmp_name'])) {
                if ($_FILES['file']['type'] == 'text/xml') {
                    $_FILES['file']['name'] .= '.txt';
                }
                /*
                 *
                 * We need to move uploaded file manually
                 */
                if (!empty($_FILES['file']['error'])) {
                    echo '<div class="message error"><p>' . __('Error uploading file', 'wpcf') . '</p></div>';
                    return array();
                }
                $wp_upload_dir = wp_upload_dir();
                $new_file = $wp_upload_dir['basedir'] . '/' . sanitize_file_name($_FILES['file']['name']);
                $move = move_uploaded_file($_FILES['file']['tmp_name'], $new_file);
                if (!$move) {
                    echo '<div class="message error"><p>' . __('Error moving uploaded file', 'wpcf') . '</p></div>';
                    return array();
                }
                $uploaded_file = array('file' => $new_file);
                $info = pathinfo($uploaded_file['file']);
                $is_zip = $info['extension'] == 'zip' ? true : false;
                if ($is_zip) {
                    $zip = zip_open($uploaded_file['file']);
                    if (is_resource($zip)) {
                        while (($zip_entry = zip_read($zip)) !== false) {
                            if (zip_entry_name($zip_entry) == 'settings.xml') {
                                $data = @zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                            }
                        }
                    } else {
                        echo '<div class="message error"><p>' . __('Unable to open zip file', 'wpcf') . '</p></div>';
                        return array();
                    }
                } else {
                    $data = @file_get_contents($uploaded_file['file']);
                }
                /**
                 * use Transients API to store file fullpath
                 */
                $current_user = wp_get_current_user();
                $cache_key = md5($current_user->user_email . $uploaded_file['file']);
                set_transient($cache_key, $uploaded_file['file'], 60 * 60);
                $form['file'] = array('#type' => 'hidden', '#name' => 'file', '#value' => $cache_key);
                $mode = 'file';
            } elseif (!empty($_POST['import-text']) && !empty($_POST['text'])) {
                $data = stripslashes($_POST['text']);
                if (preg_match('/encoding=("[^"]*"|\'[^\']*\')/s', $data, $match)) {
                    $charset = trim($match[1], '"');
                } else {
                    $charset = !empty($_POST['text-encoding']) ? sanitize_text_field($_POST['text-encoding']) : get_option('blog_charset');
                }
                $form['text'] = array('#type' => 'hidden', '#name' => 'text', '#value' => htmlentities(stripslashes($_POST['text']), ENT_QUOTES, $charset));
                $form['text-encoding'] = array('#type' => 'hidden', '#name' => 'text-encoding', '#value' => $charset);
                $mode = 'text';
            }
            if (empty($data)) {
                echo '<div class="message error"><p>' . __('Data not valid', 'wpcf') . '</p></div>';
                $show_first_screen = true;
            } else {
                $data = wpcf_admin_import_export_settings($data);
                if (empty($data)) {
                    echo '<div class="message error"><p>' . __('Data not valid', 'wpcf') . '</p></div>';
                    $show_first_screen = true;
                } else {
                    $form = array_merge($form, $data);
                    $form['mode'] = array('#type' => 'hidden', '#name' => 'mode', '#value' => $mode);
                    $form['import-final'] = array('#type' => 'hidden', '#name' => 'import-final', '#value' => 1);
                    $form['submit'] = array('#type' => 'submit', '#name' => 'import', '#value' => __('Import', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
                }
            }
        }
    }
    if ($show_first_screen) {
        $form = $form_base;
        $form['submit'] = array('#type' => 'submit', '#name' => 'export', '#value' => __('Export', 'wpcf'), '#attributes' => array('class' => 'button-primary'), '#after' => '<br /><br />', '#before' => '<h3>' . __('Export Types data', 'wpcf') . '</h3>' . __('Download all custom fields, post types and taxonomies created by Types plugin.', 'wpcf') . '<br /><br />');
        /**
         * check is temp folder available?
         */
        $temp = wpcf_get_temporary_directory();
        if (empty($temp)) {
            unset($form['submit']);
        }
        if (extension_loaded('simplexml')) {
            $attributes = !wpcf_admin_import_dir() ? array('disabled' => 'disabled') : array();
            $form['file'] = array('#type' => 'file', '#name' => 'file', '#prefix' => __('Upload XML file', 'wpcf') . '<br />', '#before' => '<h3>' . __('Import Types data file', 'wpcf') . '</h3>', '#inline' => true, '#attributes' => $attributes);
            $form['submit-file'] = array('#type' => 'submit', '#name' => 'import-file', '#value' => __('Import file', 'wpcf'), '#attributes' => array_merge($attributes, array('class' => 'button-primary', 'disabled' => 'disabled')), '#prefix' => '<br />', '#suffix' => '<br /><br />');
            $form['text'] = array('#type' => 'textarea', '#title' => __('Paste code here', 'wpcf'), '#name' => 'text', '#attributes' => array('rows' => 20), '#before' => '<h3>' . __('Import Types data text input', 'wpcf') . '</h3>');
            $form['text-encoding'] = array('#type' => 'textfield', '#title' => __('Encoding', 'wpcf'), '#name' => 'text-encoding', '#value' => get_option('blog_charset'), '#description' => __('If encoding is set in text input, it will override this setting.', 'wpcf'));
            $form['submit-text'] = array('#type' => 'submit', '#name' => 'import-text', '#value' => __('Import text', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
            $form['step'] = array('#type' => 'hidden', '#name' => 'step', '#value' => 1);
        } else {
            echo '<div class="message error"><p>' . __('PHP SimpleXML extension not loaded: Importing not available', 'wpcf') . '</p></div>';
        }
    }
    return $form;
}