/**
 * Uninstall the custom infrastsructure set up here by the plugin
 */
function clean_out_dictionary_data()
{
    $delete_taxonomies = $_POST['delete_taxonomies'];
    //deletes the xhtml file, if still there because import didn't get completed
    $import = new sil_pathway_xhtml_Import();
    $file = $import->get_latest_xhtmlfile();
    if (isset($file->ID)) {
        wp_delete_attachment($file->ID);
    }
    // Remove all the old dictionary entries.
    remove_entries();
    // Uninstall the custom table(s) and taxonomies.
    if ($delete_taxonomies == 1) {
        unregister_custom_taxonomies();
    }
    uninstall_custom_tables();
    // Reinstall custom table(s) and taxonomies.
    create_search_tables();
    if ($delete_taxonomies == 1) {
        register_semantic_domains_taxonomy();
        register_part_of_speech_taxonomy();
        register_language_taxonomy();
    }
}
#
#
#
require "settings.php";
require "core-settings.php";
if (isset($_POST["update"])) {
    $OUTPUT = save_entries($_POST);
} elseif (isset($_POST["process"])) {
    if (isset($_POST["supinv"]) and strlen($_POST["supinv"]) > 0) {
        $OUTPUT = save_entries($_POST);
    } else {
        $OUTPUT = process_entries($_POST);
    }
} else {
    if (isset($_POST["rem_selected"])) {
        $OUTPUT = remove_entries($_POST);
    } elseif (isset($_POST["remcost"])) {
        $OUTPUT = remove_cost_entries($_POST);
    } else {
        $OUTPUT = get_items($_POST);
    }
}
require "template.php";
function get_items($_POST, $err = "")
{
    extract($_POST);
    $old_entries = "";
    $new_entry = "";
    $i = 0;
    $total = 0;
    $counter = 0;
 public function import($_headers)
 {
     $authenticated = $this->verifyAdminPrivileges($email);
     if ($authenticated) {
         $arrDirectory = wp_upload_dir();
         $uploadPath = $arrDirectory['path'];
         $destinationPath = $uploadPath . "/" . str_replace(".zip", "", $_FILES['file']['name']);
         $unzipped = $this->unzip($_FILES['file'], $uploadPath, $destinationPath);
         //program can be closed now, the import will run in the background
         flush();
         if ($unzipped) {
             $fileConfigured = $destinationPath . "/configured.xhtml";
             $xhtmlConfigured = file_get_contents($fileConfigured);
             $fileReversal1 = $destinationPath . "/reversal1.xhtml";
             $xhtmlReversal1 = file_get_contents($fileReversal1);
             $fileReversal2 = $destinationPath . "/reversal2.xhtml";
             $xhtmlReversal2 = file_get_contents($fileReversal2);
             //moving style sheet file
             if (file_exists($destinationPath . "/configured.css")) {
                 copy($destinationPath . "/configured.css", $uploadPath . "/imported-with-xhtml.css");
                 error_log("Renamed configured.css to " . $uploadPath . "/imported-with-xhtml.css");
                 unlink($destinationPath . "/configured.css");
             }
             //copy folder files (which includes audio and image folders and files)
             if (file_exists($destinationPath . "/files")) {
                 //first delete any existing files
                 $this->recursiveRemoveDir($uploadPath . "/images/thumbnail");
                 $this->recursiveRemoveDir($uploadPath . "/images/original");
                 $this->recursiveRemoveDir($uploadPath . "/audio");
                 //then copy everything under files
                 $this->recursiveCopy($destinationPath . "/files", $uploadPath);
                 $this->recursiveRemoveDir($destinationPath . "/files");
             }
         }
         if (isset($xhtmlConfigured)) {
             //we first delete all existing posts (in category Webonary)
             remove_entries('flexlinks');
             //deletes data that comes with the posts, but gets stored separately (e.g. "parts of speech")
             clean_out_dictionary_data();
             $filetype = "configured";
             $xhtmlFileURL = $fileConfigured;
             require "run_import.php";
         }
         if (isset($xhtmlReversal1)) {
             $filetype = "reversal";
             $xhtmlFileURL = $fileReversal1;
             require "run_import.php";
         }
         if (isset($xhtmlReversal2)) {
             $filetype = "reversal";
             $xhtmlFileURL = $fileReversal2;
             require "run_import.php";
         }
         if (file_exists($destinationPath)) {
             //deletes the extracted zip folder
             //update 21 April 2015: We no longer remove the directory, instead just the individual files
             //get deleted as we are now running the import in an external process and the files would otherwise be missing
             //$this->recursiveRemoveDir($destinationPath);
         }
         $message = "The export to Webonary is completed.\n";
         $message .= "Go here to configure more settings: " . get_site_url() . "/wp-admin/admin.php?page=webonary";
         wp_mail($email, 'Webonary Export complete', $message);
         return "import completed";
     } else {
         echo "authentication failed\n";
         flush();
     }
     return;
 }
    function start()
    {
        global $wpdb;
        /* @todo See if there is a better way to do this than these steps */
        if (empty($_GET['step'])) {
            $step = 0;
        } else {
            $step = (int) $_GET['step'];
        }
        $this->header();
        $this->verbose = false;
        if (isset($_POST['chkShowProgress'])) {
            $this->verbose = true;
        }
        if (isset($_POST['btnRestartImport'])) {
            $filetype = "configured";
            remove_entries('');
            remove_entries('flexlinks');
            echo "Restarting Import...<br>";
            if ($this->api == false && $this->verbose == false) {
                echo "You can now close the browser window. <a href=\"../wp-admin/admin.php?import=pathway-xhtml\">Click here to view the import status</a><br>";
            }
            flush();
            $file = $this->get_latest_xhtmlfile();
            $xhtmlFileURL = $file->url;
            require "run_import.php";
            return;
        }
        if (isset($_POST['btnReindex'])) {
            ?>
			<DIV ID="flushme">Indexing Search Strings... </DIV>
			<?php 
            $this->verbose = true;
            $this->index_searchstrings();
            $file = $this->get_latest_xhtmlfile();
            wp_delete_attachment($file->ID);
        }
        if (isset($_POST['btnRestartReversalImport'])) {
            $filetype = "reversal";
            echo "Restarting Import of Reversal Entries...<br>";
            if ($this->api == false && $this->verbose == false) {
                echo "You can now close the browser window. <a href=\"../wp-admin/admin.php?import=pathway-xhtml\">Click here to view the import status</a><br>";
            }
            flush();
            $file = $this->get_latest_xhtmlfile();
            $xhtmlFileURL = $file->url;
            require "run_import.php";
            return;
        }
        if (isset($_POST['btnIndexReversals'])) {
            ?>
		<DIV ID="flushme">Indexing Reversal Entries... </DIV>
		<?php 
            $this->verbose = true;
            $this->index_reversals();
        }
        if (isset($_POST['btnConvertFLExLinks'])) {
            ?>
			<DIV ID="flushme">Converting FLEx links to Webonary links... </DIV>
		<?php 
            $this->verbose = true;
            $this->convert_fieldworks_links_to_wordpress($_POST['pinged']);
        }
        if (isset($_POST['btnMakeLinks'])) {
            ?>
			<DIV ID="flushme">Converting headwords to links... </DIV>
		<?php 
            $this->verbose = true;
            $this->convert_fields_to_links();
        }
        switch ($step) {
            /*
             * First, greet the user and prompt for files.
             */
            case 0:
                $this->hello();
                $this->get_user_input();
                echo '</div>';
                break;
                /*
                 * Second, upload and import files
                 */
            /*
             * Second, upload and import files
             */
            case 1:
                check_admin_referer('import-upload');
                flush();
                // Get the XHMTL file
                $result = $this->upload_files('xhtml');
                if (is_wp_error($result)) {
                    echo $result->get_error_message();
                }
                $xhtml_file = $result['file'];
                // Get the CSS file
                $result = $this->upload_files('css');
                if (is_wp_error($result)) {
                    echo $result->get_error_message();
                }
                $css_file = $result['file'];
                if (isset($_POST['filetype'])) {
                    $filetype = $_POST['filetype'];
                }
                if ($this->api == false && $this->verbose == false) {
                    echo "You can now close the browser window. <a href=\"../wp-admin/admin.php?import=pathway-xhtml\">Click here to view the import status</a><br>";
                }
                flush();
                $file = $this->get_latest_xhtmlfile();
                $xhtmlFileURL = $file->url;
                require "run_import.php";
                /*
                ?>
                <DIV ID="flushme">importing...</DIV>
                <?php
                
                $file = $this->get_latest_xhtmlfile();
                if(isset($file))
                {
                	$xhtml_file = file_get_contents($file->url);
                }
                $result = $this->import_xhtml($xhtml_file, false, $this->verbose);
                
                $this->goodbye($xhtml_file, $css_file);
                
                if(isset($file))
                {
                	//##wp_delete_attachment( $file->ID );
                }
                */
                break;
                /*
                 * for indexing the search strings (configured dictionary)
                 */
            /*
             * for indexing the search strings (configured dictionary)
             */
            case 2:
                ?>
				<DIV ID="flushme">indexing...</DIV>
				<?php 
                $this->index_searchstrings();
                $xhtml_file = $result['file'];
                $this->goodbye($xhtml_file, $css_file);
                $message = "The import of the vernacular (configured) xhtml export is completed.\n";
                $message .= "Go here to configure more settings: " . get_site_url() . "/wp-admin/admin.php?page=webonary";
                break;
            case 3:
                ?>
				<DIV ID="flushme">converting links...</DIV>
				<?php 
                $this->convert_fields_to_links();
                echo '<p>' . __('Finished!', 'sil_dictionary') . '</p>';
                echo '<p>&nbsp;</p>';
                echo '<p>After importing, go to <strong><a href="../wp-admin/admin.php?page=webonary">Webonary</a></strong> to configure more settings.</p>';
                ?>
			<?php 
                break;
        }
        $this->footer();
    }