index.php 5.14 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
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
 *
 * 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-2008 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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

32
33
34
35
36
37
$importplugins = plugins_installed('import');

if (!$importplugins) {
    die_info(get_string('noimportpluginsenabled', 'import'));
}

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
$form = pieform(array(
    'name' => 'import',
    'elements' => array(
        'file' => array(
            'type' => 'file',
            'title' => 'LEAP2A file',
            'description' => 'Either a .zip file or just the LEAP2A XML file',
            'rules' => array(
                'required' => true,
            ),
        ),
        'submit' => array(
            'type' => 'submit',
            'value' => 'Import',
        ),
    ),
));

function import_validate(Pieform $form, $values) {
    if ($values['file']['type'] != 'application/zip'
        && $values['file']['type'] != 'text/xml') {
        $form->set_error('file', 'The file must be a .zip or LEAP2A XML file');
    }
}

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

    $date = time();
    $nicedate = date('Y/m/d h:i:s', $date);

    $uploaddir = get_config('dataroot') . 'import/test-' . $date . '/';
    $filename = $uploaddir . $values['file']['name'];
    check_dir_exists($uploaddir);
    move_uploaded_file($values['file']['tmp_name'], $filename);

    if ($values['file']['type'] == 'application/zip') {
        // Unzip here
        $command = sprintf('%s %s %s %s',
            escapeshellcmd(get_config('pathtounzip')),
            escapeshellarg($filename),
            get_config('unzipdirarg'),
            escapeshellarg($uploaddir)
        );
        $output = array();
        exec($command, $output, $returnvar);
        if ($returnvar != 0) {
            $SESSION->add_error_msg('Unable to unzip the file');
            redirect('/import/');
        }

        $filename = $uploaddir . 'leap2a.xml';
        if (!is_file($filename)) {
            $SESSION->add_error_msg('No leap2a.xml file detected - please check your export file again');
            redirect('/import/');
        }
    }

    // Create dummy user
    $user = (object)array(
        'username' => 'import_' . $date,
        'password' => 'import1',
        'firstname' => 'Imported',
        'lastname' => 'User (' . $nicedate .')',
        'email'    => 'imported@example.org',
    );
    $userid = create_user($user);

    // And we're good to go
    echo '<pre>';
    $filename = substr($filename, strlen(get_config('dataroot')));
    require_once(dirname(dirname(__FILE__)) . '/import/lib.php');
    safe_require('import', 'leap');
111
    db_begin();
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    $importer = PluginImport::create_importer(null, (object)array(
        'token'      => '',
        //'host'       => '',
        'usr'        => $userid,
        'queue'      => (int)!(PluginImport::import_immediately_allowed()), // import allowed straight away? Then don't queue
        'ready'      => 0, // maybe 1?
        'expirytime' => db_format_timestamp(time()+(60*60*24)),
        'format'     => 'leap',
        'data'       => array('filename' => $filename),
        'loglevel'   => PluginImportLeap::LOG_LEVEL_VERBOSE,
        'logtargets' => LOG_TARGET_STDOUT,
        'profile'    => true,
    ));
    $importer->process();

    // Now done, delete the temporary e-mail address if there's a new one
    // A bit sucky, presumes only one email in the import
    $email = artefact_instance_from_id(get_field('artefact', 'id', 'title', 'imported@example.org', 'artefacttype', 'email', 'owner', $userid));
    $email->delete();
    execute_sql('UPDATE {artefact_internal_profile_email} SET principal = 1 WHERE "owner" = ?', array($userid));
132
    db_commit();
133
134
135
136
137
138
139
140
141

    echo "\n\n";
    echo 'Done. You can <a href="' . get_config('wwwroot') . '/admin/users/changeuser.php?id=' . $userid . '">change to this user</a> to inspect the result, ';
    echo 'or <a href="' . get_config('wwwroot') . 'import/">try importing again</a>';
    echo '</pre>';
    exit;
}

$smarty = smarty();
Nigel McNie's avatar
Nigel McNie committed
142
$smarty->assign('PAGEHEADING', hsc(get_string('import')));
143
144
145
146
147
$smarty->assign('form', $form);
$smarty->assign('previouslyimporteduser', param_integer('user', null));
$smarty->display('import/index.tpl');

?>