Esempio n. 1
0
 function import()
 {
     $options = get_option('html_import');
     if ($_POST['import_files'] == 'file') {
         // preserve original file name so we can use it for slugs later ( maybe )
         $this->filename = $_FILES['import']['name'];
         // upload the file
         $file = wp_import_handle_upload();
         if (isset($file['error'])) {
             echo $file['error'];
             return;
         }
         echo '<h2>' . __('Importing HTML file...', 'import-html-pages') . '</h2>';
         $this->file = $file['file'];
         $this->get_single_file();
         $this->print_results($options['type']);
         wp_import_cleanup($file['id']);
         if ($options['import_images']) {
             $this->find_images();
         }
         if ($options['import_documents']) {
             $this->find_documents();
         }
         if ($options['fix_links']) {
             $this->find_internal_links();
         }
     } elseif ($_POST['import_files'] == 'directory') {
         // in case they entered something dumb and didn't fix it when we showed an error on the options page...
         if (validate_import_file($options['root_directory']) > 0) {
             wp_die(__("The beginning directory you entered is not an absolute path. Relative paths are not allowed here.", 'import-html-pages'));
         }
         $this->table = '';
         $this->redirects = '';
         $this->filearr = array();
         $skipdirs = explode(",", $options['skipdirs']);
         $this->skip = array_merge($skipdirs, array('.', '..', '_vti_cnf', '_notes'));
         $this->allowed = explode(",", $options['file_extensions']);
         echo '<h2>' . __('Importing HTML files...', 'import-html-pages') . '</h2>';
         $this->get_files_from_directory($options['root_directory']);
         $this->print_results($options['type']);
         if (isset($options['import_images']) && $options['import_images']) {
             $this->find_images();
         }
         if (isset($options['import_documents']) && $options['import_documents']) {
             $this->find_documents();
         }
         if (isset($options['fix_links']) && $options['fix_links']) {
             $this->find_internal_links();
         }
     } else {
         _e("Your file upload didn't work. Try again?", 'html-import-pages');
     }
     do_action('import_done', 'html');
 }
function html_import_validate_options($input)
{
    // Validation/sanitization. Add errors to $msg[].
    $msg = array();
    $linkmsg = '';
    $msgtype = 'error';
    // sanitize path for Win32
    $input['root_directory'] = str_replace('\\', '/', $input['root_directory']);
    $input['root_directory'] = preg_replace('|/+|', '/', $input['root_directory']);
    if (validate_import_file($input['root_directory']) > 0) {
        $msg[] = __("The beginning directory you entered is not an absolute path. Relative paths are not allowed here.", 'import-html-pages');
        $input['root_directory'] = ABSPATH . __('html-files-to-import', 'import-html-pages');
    }
    $input['root_directory'] = rtrim($input['root_directory'], '/');
    $input['old_url'] = esc_url(rtrim($input['old_url'], '/'));
    // trim the extensions, skipped dirs, allowed attributes. Invalid ones will not cause problems.
    $input['file_extensions'] = str_replace('.', '', $input['file_extensions']);
    $input['file_extensions'] = str_replace(' ', '', $input['file_extensions']);
    $input['file_extensions'] = strtolower($input['file_extensions']);
    $input['document_mimes'] = str_replace('.', '', $input['document_mimes']);
    $input['document_mimes'] = str_replace(' ', '', $input['document_mimes']);
    $input['document_mimes'] = strtolower($input['document_mimes']);
    $input['skipdirs'] = str_replace(' ', '', $input['skipdirs']);
    $input['allow_tags'] = str_replace('/', '', $input['allow_tags']);
    $input['allow_tags'] = str_replace(' ', '', $input['allow_tags']);
    $input['allow_attributes'] = str_replace(' ', '', $input['allow_attributes']);
    $input['index_files'] = str_replace(' ', '', $input['index_files']);
    if (!in_array($input['status'], get_post_stati())) {
        $input['status'] = 'publish';
    }
    $post_types = get_post_types(array('public' => true), 'names');
    if (!in_array($input['type'], $post_types)) {
        $input['type'] = 'page';
    }
    if (!in_array($input['timestamp'], array('now', 'filemtime', 'customfield'))) {
        $input['timestamp'] = 'filemtime';
    }
    if (!in_array($input['import_content'], array('tag', 'region', 'file'))) {
        $input['import_content'] = 'tag';
    }
    if (!in_array($input['import_title'], array('tag', 'region', 'filename'))) {
        $input['import_title'] = 'tag';
    }
    // trim region/tag/attr/value
    if (!empty($input['content_region'])) {
        $input['content_region'] = sanitize_text_field($input['content_region']);
    }
    if (!empty($input['content_tag'])) {
        $input['content_tag'] = sanitize_text_field($input['content_tag']);
    }
    if (!empty($input['content_tagatt'])) {
        $input['content_tagatt'] = sanitize_text_field($input['content_tagatt']);
    }
    if (!empty($input['content_attval'])) {
        $input['content_attval'] = sanitize_text_field($input['content_attval']);
    }
    if (!empty($input['title_region'])) {
        $input['title_region'] = sanitize_text_field($input['title_region']);
    }
    if (!empty($input['title_tag'])) {
        $input['title_tag'] = sanitize_text_field($input['title_tag']);
    }
    if (!empty($input['title_tagatt'])) {
        $input['title_tagatt'] = sanitize_text_field($input['title_tagatt']);
    }
    if (!empty($input['title_attval'])) {
        $input['title_attval'] = sanitize_text_field($input['title_attval']);
    }
    if (!empty($input['date_region'])) {
        $input['date_region'] = sanitize_text_field($input['date_region']);
    }
    if (!empty($input['date_tag'])) {
        $input['date_tag'] = sanitize_text_field($input['date_tag']);
    }
    if (!empty($input['date_tagatt'])) {
        $input['date_tagatt'] = sanitize_text_field($input['date_tagatt']);
    }
    if (!empty($input['date_attval'])) {
        $input['date_attval'] = sanitize_text_field($input['date_attval']);
    }
    // We could have many custom fields. For now, let's just make it an array. Deal with it in the importer.
    if (!is_array($input['customfield_name'])) {
        $input['customfield_name'] = array($input['customfield_name']);
    }
    if (!is_array($input['import_field'])) {
        $input['import_field'] = array($input['import_field']);
    }
    if (!is_array($input['customfield_region'])) {
        $input['customfield_region'] = array($input['customfield_region']);
    }
    if (!is_array($input['customfield_tag'])) {
        $input['customfield_tag'] = array($input['customfield_tag']);
    }
    if (!is_array($input['customfield_tagatt'])) {
        $input['customfield_tagatt'] = array($input['customfield_tagatt']);
    }
    if (!is_array($input['customfield_attval'])) {
        $input['customfield_attval'] = array($input['customfield_attval']);
    }
    if (!is_array($input['customfield_html'])) {
        $input['customfield_html'] = array($input['customfield_html']);
    }
    // must have something to look for in the HTML
    if ($input['import_content'] == 'tag' && empty($input['content_tag'])) {
        $msg[] = __("You did not enter an HTML content tag to import.", 'import-html-pages');
    }
    if ($input['import_content'] == 'region' && empty($input['content_region'])) {
        $msg[] = __("You did not enter a Dreamweaver content template region to import.", 'import-html-pages');
    }
    if ($input['import_title'] == 'tag' && empty($input['title_tag'])) {
        $msg[] = __("You did not enter an HTML title tag to import.", 'import-html-pages');
    }
    if ($input['import_title'] == 'region' && empty($input['title_region'])) {
        $msg[] = __("You did not enter a Dreamweaver title template region to import.", 'import-html-pages');
    }
    if (!isset($input['root_parent'])) {
        $input['root_parent'] = 0;
    }
    // $input['remove_from_title'] could be anything, including unencoded characters or HTML tags
    // it's a search pattern; leave it alone
    // these should all be zero or one
    $input['clean_html'] = absint($input['clean_html']);
    if ($input['clean_html'] > 1) {
        $input['clean_html'] = 0;
    }
    $input['encode'] = absint($input['encode']);
    if ($input['encode'] > 1) {
        $input['encode'] = 0;
    }
    $input['meta_desc'] = absint($input['meta_desc']);
    if ($input['meta_desc'] > 1) {
        $input['meta_desc'] = 1;
    }
    $input['title_inside'] = absint($input['title_inside']);
    if ($input['title_inside'] > 1) {
        $input['title_inside'] = 0;
    }
    // see if this is a real user
    $input['user'] = absint($input['user']);
    $user_info = get_userdata($input['user']);
    if ($user_info === false) {
        $msg[] = "The author you specified does not exist.";
        $currentuser = wp_get_current_user();
        $input['user'] = $currentuser->ID;
    }
    // If settings have been saved at least once, we can turn this off.
    $input['firstrun'] = false;
    // Send custom updated message
    $msg = implode('<br />', $msg);
    if (empty($msg)) {
        $linkstructure = get_option('permalink_structure');
        if (empty($linkstructure)) {
            $linkmsg = sprintf(__('If you intend to <a href="%s">set a permalink structure</a>, you should do it 
				before importing so the <kbd>.htaccess</kbd> redirects will be accurate.', 'import-html-pages'), 'options-permalink.php');
        }
        $msg = sprintf(__('Settings saved. %s <a href="%s">Ready to import files?</a>', 'import-html-pages'), $linkmsg, 'admin.php?import=html');
        // $msg .= '<pre>'. print_r( $input, false ) .'</pre>';
        $msgtype = 'updated';
    }
    add_settings_error('html_import', 'html_import', $msg, $msgtype);
    return $input;
}