index.php 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2013 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage import
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2013 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

/*
 * Interactive user self-import of LEAP
 */

define('INTERNAL', 1);
define('MENUITEM', 'myportfolio/import');
require(dirname(dirname(__FILE__)) . '/init.php');

//TODO: Optimize!
raise_memory_limit("512M");

define('PRINTUPLOADFORM_ACT', 0);
define('PRINTIMPORTITEMSFORM_ACT', 1);
define('DOIMPORT_ACT', 2);

$TRANSPORTER = null;
$IMPORTER = null;

// Check if leap import plugin is enabled
$importplugins = plugins_installed('import');

if (!$importplugins) {
    die_info(get_string('noimportpluginsenabled', 'import'));
}
if (!array_key_exists('leap', $importplugins)) {
    die_info(get_string('noleapimportpluginsenabled', 'import'));
}
// Check if unzip is available
// This is required for extracting leap2a zip file
if (!is_executable(get_config('pathtounzip'))) {
    die_info(get_string('unzipnotinstalled', 'admin'));
}

$action = param_integer('action', PRINTUPLOADFORM_ACT);

switch ($action) {
    case PRINTUPLOADFORM_ACT:
    default:
        print_upload_form();
        break;
    case PRINTIMPORTITEMSFORM_ACT:
        print_import_items_form();
        break;
    case DOIMPORT_ACT:
        db_begin();
        if (isset($_POST['import_submit'])) {
            save_decisions();
            // Do import and print the results
            do_import();
        }
        else if (isset($_POST['cancel_import_submit'])) {
            cancel_import();
        }
        db_commit();
        break;
}

function print_upload_form() {

    $form = pieform(array(
        'name'        => 'import',
        'method'      => 'post',
        'plugintype ' => 'core',
        'pluginname'  => 'import',
        'elements'    => array(
            'leap2afile' => array(
                'type'  => 'file',
                'title' => get_string('uploadleap2afile', 'admin'),
                'rules' => array(
                    'required' => true
                )
            ),
            'submit' => array(
                'type'  => 'submit',
                'value' => get_string('Import', 'import'),
            ),
        )
    ));
    $smarty = smarty();
    $smarty->assign('PAGEHEADING', get_string('importyourportfolio', 'import'));
    $smarty->assign('pagedescription', get_string('importportfoliodescription', 'import'));
    $smarty->assign('form', $form);
    $smarty->display('form.tpl');
}


function import_validate(Pieform $form, $values) {
    global $USER, $TRANSPORTER;

117
    if (!isset($values['leap2afile'])) {
118 119 120 121 122 123 124 125 126
        $form->set_error('leap2afile', $form->i18n('rule', 'required', 'required'));
        return;
    }

    if ($values['leap2afile']['type'] == 'application/octet-stream') {
        require_once('file.php');
        $mimetype = file_mime_type($values['leap2afile']['tmp_name']);
    }
    else {
127
        $mimetype = trim($values['leap2afile']['type'], '"');
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    }
    $date = time();
    $niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $USER->get('username'));
    safe_require('import', 'leap');
    $fakeimportrecord = (object)array(
        'data' => array(
            'importfile'     => $values['leap2afile']['tmp_name'],
            'importfilename' => $values['leap2afile']['name'],
            'importid'       => $niceuser . '-' . $date,
            'mimetype'       => $mimetype,
        )
    );

    $TRANSPORTER = new LocalImporterTransport($fakeimportrecord);
    try {
        $TRANSPORTER->extract_file();
        PluginImportLeap::validate_transported_data($TRANSPORTER);
    }
    catch (Exception $e) {
        $form->set_error('leap2afile', $e->getMessage());
        $TRANSPORTER->cleanup();
    }

    // Check if import data may exceed the user's file quota
    $importdata = $TRANSPORTER->files_info();
    require_once('function.dirsize.php');
    $importdatasize = dirsize($importdata['tempdir'] . 'extract/files');
    if (($USER->get('quotaused') + $importdatasize) > $USER->get('quota')) {
        $form->set_error('leap2afile', get_string('importexceedquota', 'import'));
        $TRANSPORTER->cleanup();
    }
}

function import_submit(Pieform $form, $values) {
    global $SESSION, $USER, $TRANSPORTER, $IMPORTER;

    safe_require('import', 'leap');

    $importdata = (object)array(
        'token'      => '',
        'usr'        => $USER->get('id'),
        'queue'      => (int) false,
        'ready'      => 0, // set this to 0 so that if it gets queued, the cron won't process it
        'expirytime' => db_format_timestamp(time()+(60*60*24)),
        'format'     => 'leap',
        'loglevel'   => PluginImportLeap::LOG_LEVEL_STANDARD,
        'logtargets' => LOG_TARGET_FILE,
        'profile'    => true,
    );
    $IMPORTER = PluginImport::create_importer(null, $TRANSPORTER, $importdata);

    try {
        $IMPORTER->process(PluginImport::STEP_INTERACTIVE_IMPORT_FORM);
    }
    catch (ImportException $e) {
        log_info("Leap2A import failed: " . $e->getMessage());
        die_info(get_string('importfailed', 'import'));
    }
    if ($TRANSPORTER) {
        $SESSION->set('importid', $TRANSPORTER->get('importid'));
        $SESSION->set('extracted', $TRANSPORTER->get('extracted'));
        $SESSION->set('mimetype', $TRANSPORTER->get('mimetype'));
    }
    redirect('/import/index.php?action=' . PRINTIMPORTITEMSFORM_ACT);
}

function print_import_items_form() {
    global $SESSION, $USER, $TRANSPORTER, $IMPORTER;

    safe_require('import', 'leap');
    // Get $TRANSPORTER and $IMPORTER from $SESSION
    $importrecord = (object)array(
        'data' => array (
            'importid'     => $SESSION->get('importid'),
            'extracted'    => $SESSION->get('extracted'),
            'mimetype'     => $SESSION->get('mimetype'),
    ));
    $TRANSPORTER = new LocalImporterTransport($importrecord);

    $importdata = (object)array(
        'token'      => '',
        'usr'        => $USER->get('id'),
        'queue'      => (int) false,
        'ready'      => 0, // set this to 0 so that if it gets queued, the cron won't process it
        'expirytime' => db_format_timestamp(time()+(60*60*24)),
        'format'     => 'leap',
        'loglevel'   => PluginImportLeap::LOG_LEVEL_STANDARD,
        'logtargets' => LOG_TARGET_FILE,
        'profile'    => true,
    );
    $IMPORTER = PluginImport::create_importer(null, $TRANSPORTER, $importdata);

    try {
        $form = $IMPORTER->build_import_entry_requests_form();
    }
    catch (ImportException $e) {
        log_info("Leap2A import failed: " . $e->getMessage());
        die_info(get_string('importfailed', 'import'));
    }

    $smarty = smarty();
    $smarty->assign('PAGEHEADING', get_string('howimportyourportfolio', 'import'));
    $smarty->assign('pagedescription', get_string('howimportportfoliodescription', 'import'));
    $smarty->assign('form', $form);
    $smarty->display('form.tpl');
}

/**
 * Process the results of the user's decisions on the import items form, and update the import entry requests
 * in the database to reflect those decisions.
 */
function save_decisions() {
    global $USER;

    safe_require('import', 'leap');
    // Accessing $_POST directly here because it's the most efficient way to handle the
    // many dynamically-generated fields created by the import items form.
    foreach ($_POST as $key => $value) {
        if (
247
                preg_match('/^decision_(\d+)$/', $key, $m)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                && in_array(
                        $value,
                        array(PluginImport::DECISION_ADDNEW, PluginImport::DECISION_APPEND, PluginImport::DECISION_REPLACE, PluginImport::DECISION_IGNORE)
                )
        ) {
            update_record(
                    'import_entry_requests',
                    (object) array(
                        'decision' => $value,
                    ),
                    array(
                        'id' => $m[1],
                        'ownerid' => $USER->id,
                    )
            );
        }
    }
}

function do_import() {
    global $SESSION, $USER, $TRANSPORTER, $IMPORTER;

    safe_require('import', 'leap');
    // Get $TRANSPORTER and $IMPORTER from $SESSION
    $importrecord = (object)array(
        'data' => array (
            'importid'     => $SESSION->get('importid'),
            'extracted'    => $SESSION->get('extracted'),
            'mimetype'     => $SESSION->get('mimetype'),
        ));
    $TRANSPORTER = new LocalImporterTransport($importrecord);

    $importdata = (object)array(
        'token'      => '',
        'usr'        => $USER->get('id'),
        'queue'      => (int) false,
        'ready'      => 0, // set this to 0 so that if it gets queued, the cron won't process it
        'expirytime' => db_format_timestamp(time()+(60*60*24)),
        'format'     => 'leap',
        'loglevel'   => PluginImportLeap::LOG_LEVEL_STANDARD,
        'logtargets' => LOG_TARGET_FILE,
        'profile'    => true,
    );
    $IMPORTER = PluginImport::create_importer(null, $TRANSPORTER, $importdata);

    try {
        $result = $IMPORTER->do_import_from_requests();
    }
    catch (ImportException $e) {
        log_info("Leap2A import failed: " . $e->getMessage());
        die_info(get_string('importfailed', 'import'));
    }

    $smarty = smarty();
    $smarty->assign('PAGEHEADING', get_string('importresult', 'import'));
    $smarty->assign('form', $result);
    $smarty->display('form.tpl');
}
/**
 * Remove all import  entry requests
 */
function cancel_import() {
    global $SESSION, $USER;

    delete_records('import_entry_requests', 'importid', $SESSION->get('importid'), 'ownerid', $USER->get('id'));
    redirect('/import/index.php');
}