Пример #1
0
                    if ($fileinfo == "application/x-gzip") {
                        $buffer_size = 4096;
                        // read 4kb at a time
                        $file_tmp = BASE_DIR . '/data/Import/iou-web-import-' . date('YmdHis') . '.db';
                        if (file_exists($file_tmp)) {
                            unlink($file_tmp);
                        }
                        $sfp = gzopen($_FILES['import_file']['tmp_name'], "rb");
                        $fp = fopen($file_tmp, "w");
                        while ($string = gzread($sfp, 4096)) {
                            fwrite($fp, $string, strlen($string));
                        }
                        gzclose($sfp);
                        fclose($fp);
                        database_update($file_tmp);
                        database_optimize($file_tmp);
                        $imported_db = new PDO('sqlite:' . $file_tmp);
                        $imported_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        ?>
			<FORM id="import_form" action="<?php 
                        print BASE_WWW;
                        ?>
/manage.php?action=lab_import" method="post">
				<h2>Select labs you want to import:</h2>
				<TABLE id='templatemo_table'>
					<TR>
						<th colspan='2'>Name</th>
						<th>Description</th>
						<th>Folder</th>
					</TR>
					<TR>
Пример #2
0
/**
 * Export labs, configs and devices
 *
 * @param	array	$labs			List of labs to be exported
 * @param	array	$configs		List of configs to be exported
 * @param	string	$file_export	File where export data
 * @return	bool					True if successfully exported
 */
function export($labs, $configs, $file_export)
{
    $file_tmp = '/tmp/iou-web-export.db';
    // Delete destination file if exists
    if (file_exists($file_export) && !unlink($file_export)) {
        error_log('DB: cannot delete the database file' . $file_export . '.');
        return false;
    }
    // Create a database
    if (!database_init($file_tmp)) {
        error_log('DB: cannot reinitializate database file' . $file_tmp . '.');
        return false;
    }
    try {
        global $db;
        // Attach the temporary database
        $query = "ATTACH '" . $file_tmp . "' AS export_db;";
        $statement = $db->prepare($query);
        $statement->execute();
        $db->beginTransaction();
        // Current version is stored
        $query = "CREATE TABLE export_db.data (data_name TEXT, data_value TEXT)";
        $db->query($query);
        $query = "INSERT INTO export_db.data (data_name, data_value) VALUES ('db_version', '" . VERSION . "')";
        $db->query($query);
        // Export data
        if ($configs != false) {
            $query = "INSERT INTO export_db.configs (cfg_id, cfg_name, cfg_config) SELECT cfg_id, cfg_name, cfg_config FROM main.configs WHERE cfg_name LIKE '" . implode(" - %' OR cfg_name LIKE '", $configs) . " - %' ORDER BY cfg_id";
            $db->query($query);
        }
        if ($labs != false) {
            $query = "INSERT INTO export_db.devices (dev_id, lab_id, dev_name, bin_name, dev_ram, dev_nvram, dev_ethernet, dev_serial, dev_picture, cfg_id, dev_top, dev_left, dev_l2keepalive, dev_watchdog) SELECT dev_id, lab_id, dev_name, bin_name, dev_ram, dev_nvram, dev_ethernet, dev_serial, dev_picture, cfg_id, dev_top, dev_left, dev_l2keepalive, dev_watchdog FROM devices WHERE lab_id=" . implode(" OR lab_id=", $labs) . " ORDER BY lab_id";
            $db->query($query);
            $query = "INSERT INTO export_db.labs (lab_id, lab_name, lab_description, lab_info, lab_netmap, lab_diagram, lab_time, lab_points) SELECT lab_id, lab_name, lab_description, lab_info, lab_netmap, lab_diagram, lab_time, lab_points FROM labs WHERE lab_id=" . implode(" OR lab_id=", $labs) . " ORDER BY lab_id";
            $db->query($query);
            $query = "INSERT INTO export_db.rel_img_lab (img_id, lab_id) SELECT img_id, lab_id FROM main.rel_img_lab WHERE main.rel_img_lab.lab_id=" . implode(" OR main.rel_img_lab.lab_id=", $labs) . ";";
            $db->query($query);
            // INSERT OR REPLACE used because multiple labs can be linked to the same image (Integrity constraint violation: 19 PRIMARY KEY must be unique")
            $query = "INSERT OR REPLACE INTO export_db.images (img_id, img_name, img_info, img_content, img_map) SELECT DISTINCT img_id, img_name, img_info, img_content, img_map FROM main.images NATURAL JOIN main.rel_img_lab WHERE main.rel_img_lab.lab_id=" . implode(" OR main.rel_img_lab.lab_id=", $labs) . ";";
            $db->query($query);
        }
        $db->commit();
        $query = "DETACH 'export_db';";
        $statement = $db->prepare($query);
        $statement->execute();
    } catch (PDOException $e) {
        error_log('DB: cannot export data with error "' . $e->getMessage() . '" (query was "' . $query . '".');
        return false;
    }
    // Now optimize the DB
    if (!database_optimize($file_tmp)) {
        return false;
    }
    $exported_db = new PDO('sqlite:' . $file_tmp);
    $exported_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        $fp = gzopen($file_export, 'w9');
        gzwrite($fp, file_get_contents($file_tmp));
        gzclose($fp);
        return true;
    } catch (Exception $e) {
        error_log('DB: cannot compress the database with error "' . $e->getMessage() . '".');
        return false;
    }
}
Пример #3
0
<?php 
        } else {
            $status = 'Error';
            $message = 'Cannot check updates.';
            xml_message($status, $message);
        }
        break;
        /*************************************************************************
         * Database: optimize                                                    *
         *************************************************************************/
    /*************************************************************************
     * Database: optimize                                                    *
     *************************************************************************/
    case 'db_optimize':
        if (is_admin()) {
            if (database_optimize(DATABASE)) {
                $status = 'Informational';
                $message = 'Database optimized.';
            } else {
                $status = 'Error';
                $message = 'Cannot optimize the database. Check logs (under Downloads page) for additional informations.';
            }
        } else {
            $status = 'Error';
            $message = 'Admin privileges required.';
        }
        xml_message('AJAX ' . $action, $status, $message);
        break;
        /*************************************************************************
         * Database: wipe                                                        *
         *************************************************************************/