lib.php 24.3 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage import
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
 *
 */

defined('INTERNAL') || die();

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * Helper interface to hold PluginImport's abstract static methods
 */
interface IPluginImport {
    /**
     * validate the import data that we have after the file has been fetched.
     * This is static, because the data may need to be validated earlier than setting up everything else
     * For example, in the case of the administrator adding a user manually from a Leap2A file,
     * we want to validate the leap data before creating the user record.
     *
     * @param array $importdata usually what ImportTransporter::files_info returns
     * @throws ImportException
     */
    public static function validate_transported_data(ImporterTransport $transporter);
}

30
31
32
33
/**
 * base class for imports.
 * handles queuing and sets up some basic helper functions
 */
34
abstract class PluginImport extends Plugin implements IPluginImport {
35

36
37
38
39
40
41
42
43
44
45
46
47
48
    // How to import a new entry
    const DECISION_IGNORE  = 1;    // ignore imported entries and keep existing artefacts
    const DECISION_REPLACE = 2;    // repalce existing artefacts by imported entries
    const DECISION_ADDNEW  = 3;    // add imported entries as new artefacts
    const DECISION_APPEND  = 4;    // append the content of existing artefacts with imported entries'

    public $displaydecisions = array();

    // Import steps
    const STEP_NON_INTERACTIVE           = 0;    // non interactive
    const STEP_INTERACTIVE_IMPORT_FORM   = 1;    // display import form for users to choose how to import new entries
    const STEP_INTERACTIVE_IMPORT_RESULT = 2;    // display the import result

49
50
51
52
53
    protected $id;
    protected $data;
    protected $expirytime;
    protected $usr;
    protected $usrobj;
54

55
56
57
    /** the ImporterTransport object to use */
    protected $importertransport;

58
59
60
61
    public static function get_plugintype_name() {
        return 'import';
    }

62
63
64
65
    /**
     * @param int $id the queue record id
     * @param stdclass $record (optional, pass this to save db queries)
     */
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    public function __construct($id, $record=null) {
        if (empty($record)) {
            if (!$record = get_record('import_queue', 'id', $id)) {
                throw new NotFoundException("Failed to find import queue record with id $id");
            }
        }
        foreach ((array)$record as $field => $value) {
            if ($field == 'data' && !is_array($value)) {
                $value = unserialize($value);
            }
            $this->{$field} = $value;
        }
        $this->usrobj = new User();
        $this->usrobj->find_by_id($this->usr);
80
81
82
83
84
85
86

        $this->displaydecisions = array(
            PluginImport::DECISION_IGNORE  => get_string('ignore', 'import'),
            PluginImport::DECISION_REPLACE => get_string('replace', 'import'),
            PluginImport::DECISION_ADDNEW  => get_string('addnew', 'import'),
            PluginImport::DECISION_APPEND  => get_string('append', 'import'),
        );
87
    }
88

89
90
91
92
93
94
95
    /**
     * set the importer transport to use for this import
     *
     * @param ImporterTransport $transport
     */
    public function set_transport(ImporterTransport $transport) {
        $this->importertransport = $transport;
96
97
    }

98
99
100
    /**
     * initialisation.  by default just calls the transporter's prepare method
     */
101
102
    public function prepare() {
        $this->importertransport->prepare_files();
103
        $this->importertransport->extract_file();
104
105
106
    }

    /**
107
    * process the files and adds them to the user's artefact area
108
    */
109
    public abstract function process($step = PluginImport::STEP_NON_INTERACTIVE);
110

111
112
113
    /**
     * perform cleanup tasks, delete temp files etc
     */
114
115
116
117
    public function cleanup() {
        $this->importertransport->cleanup();
    }

118
119
120
121
    /**
     * helper method to return member variables
     * @todo maybe refactor this to just use __get
     */
122
123
124
125
126
127
128
    public function get($field) {
        if (!property_exists($this,$field)) {
            throw new ParamOutOfRangeException("Field $field wasn't found in class " . get_class($this));
        }
        return $this->{$field};
    }

129
130
131
132
133
    /**
     * helper function to return the appropriate class name from an import format
     * this will try and resolve inconsistencies (eg file/files, leap/leap2a etc
     * and also pull in the class definition for you
     */
134
135
    public static function class_from_format($format) {
        $format = trim($format);
136
137
138
139
140
141
142
143
144
        $corr = array(
            'files' => 'file',
            'leap2a' => 'leap'
        );
        foreach ($corr as $bad => $good) {
            if ($format == $bad) {
                $format = $good;
                break;
            }
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
        }
        safe_require('import', $format);
        return generate_class_name('import', $format);
    }

    /**
    * @todo check the rest of the queue table for options
    * Generate a new import to be queued
    *
    * @param int    $userid    idof user to import for
    * @param string $plugin    plugin to handle the import
    *                          not always known at this point
    * @param string $host      wwwroot of mnet host if applicable
    * @param int    $ready     whether the import is ready to start (usually no)
    */
    public static function create_new_queue($userid, $plugin=null, $host=null, $ready=0) {
        // generate a token, insert it into the queue table
        $queue = (object)array(
            'token'      => generate_token(),
            'host'       => $host,
            'usr'        => $userid,
            'queue'      => (int)!(PluginImport::import_immediately_allowed()),
            'ready'      => $ready,
            'expirytime' => db_format_timestamp(time()+(60*60*24)),
            'plugin'     => $plugin
        );
        $queue->id = insert_record('import_queue', $queue);
        return $queue;
    }

175
176
177
178
179
180
181
182
183
184
    /**
     * creates an importer object from the queue information
     *
     * @param int               $id the queue record (if there is one, else pass 0)
     * @param ImporterTransport $transport the transporter object to use
     * @param stdclass          $record the queue data (this <b>must</b> be passed when no id is given
     *
     * @return PluginImport
     */
    public static function create_importer($id, ImporterTransport $transporter, $record=null) {
185
186
187
188
189
190
        if (empty($record)) {
            if (!$record = get_record('import_queue', 'id', $id)) {
                throw new NotFoundException("Failed to find import queue record with id $id");
            }
        }
        $class = self::class_from_format($record->format);
191
192
193
194
        $i =  new $class($id,$record);
        $i->set_transport($transporter);
        $transporter->set_importer($i);
        return $i;
195
196
    }

197
198
199
200
201
202
203
    /**
     * Whether imports are allowed immediately or if they must be queued
     * eg if the server is under load or whatever
     * @todo not implemented yet, but <b>use this anyway</b>
     *
     * @return boolean
     */
204
205
206
207
208
209
210
211
212
213
214
215
216
    public static final function import_immediately_allowed() {
        return true;
    }

    /**
    * if we're sending stuff back to wherever we were called from
    * use this method
    * at the moment, the only implementation is for mnet
    * sending back a list of file ids.
    */
    public function get_return_data() {
        return array();
    }
217
218

    /**
219
     * Add an import request of an interactive import entry as an Mahara view+collection or artefact.
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
247
248
249
250
251
252
253
254
255
256
257
258
259
     * For view import
     *    If the entry is for Profile or Dashboard page, the decision is APPEND(default), IGNORE or REPLACE
     *    If there is a duplicated view (same title and description), the decision is APPEND(default), IGNORE, REPLACE, or ADDNEW
     *    If else, the decision is IGNORE, or ADDNEW(default)
     * For artefact import
     *    If there are duplicated artefacts, the decision is IGNORE
     *    If ELSE If there is $entrytype NOT is_singular, e.g. an user may have up to 5 email addresses
     *                the decision is ADDNEW(default) or IGNORE
     *            If there is $entrytype is_singular,
     *                the decision is REPLACE(default) or APPEND
     * Also update the list of
     *   - duplicated artefacts which have same artefacttype and content
     *   - existing artefacts which have same artefacttype but the content may be different to the entry data
     *
     * @param string $importid   ID of the import
     * @param string $entryid    ID of the entry
     * @param string $strategy   Strategy of entry import
     * @param string $plugin
     * @param array  $entrydata  Data the entry including the following fields:
     *     owner     ID of the user who imports the entry (required)
     *     type (required)
     *     parent    ID of the parent entry (e.g. the blog entryid of the blogpost entry).
     *     content (required)
     *         - title  (required)
     * @return updated DB table 'import_entry_requests'
     */
    public static function add_import_entry_request($importid, $entryid, $strategy, $plugin, $entrydata) {
        $duplicatedartefactids = array();
        $existingartefactids = array();
        $title = $entrydata['content']['title'];
        if ($plugin === 'core') {
            // For view import
            $decision = PluginImport::DECISION_ADDNEW;
        }
        else {
            safe_require('artefact', $plugin);
            $classname = generate_artefact_class_name($entrydata['type']);
            if ($duplicatedartefactids = call_static_method($classname, 'get_duplicated_artefacts', $entrydata)) {
                $decision = PluginImport::DECISION_IGNORE;
            }
260
261
262
263
264
265
            // If the import entry comes with a defaultdecision filled in,
            // use that. (This provides a way for the plugins to override the usual
            // decision logic)
            else if (isset($entrydata['defaultdecision'])) {
                $decision = $entrydata['defaultdecision'];
            }
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
            else {
                $existingartefactids = call_static_method($classname, 'get_existing_artefacts', $entrydata);
                if (call_static_method($classname, 'is_singular')
                    && !empty($existingartefactids)) {
                    if ($entrydata['type'] == 'email') {
                        $decision = PluginImport::DECISION_ADDNEW;
                    }
                    else {
                        $decision = PluginImport::DECISION_REPLACE;
                    }
                }
                else {
                    $decision = PluginImport::DECISION_ADDNEW;
                }
            }
        }
        // Update DB table
        if (!record_exists_select('import_entry_requests', 'importid = ? AND entryid = ? AND ownerid = ? AND entrytype = ? AND entrytitle = ?',
                                                    array($importid, $entryid, $entrydata['owner'], $entrydata['type'], $title))) {
            return insert_record('import_entry_requests', (object) array(
                'importid'   => $importid,
                'entryid'    => $entryid,
                'strategy'   => $strategy,
                'plugin'     => $plugin,
                'ownerid'    => $entrydata['owner'],
                'entrytype'  => $entrydata['type'],
                'entryparent'=> isset($entrydata['parent']) ? $entrydata['parent'] : null,
                'entrytitle' => $title,
                'entrycontent'      => serialize($entrydata['content']),
                'duplicateditemids' => serialize($duplicatedartefactids),
                'existingitemids'   => serialize($existingartefactids),
                'decision'   => $decision,
            ));
        }
        return false;
    }

303
304
}

305
306
307
/**
 * cron job to process the queue and wake up and finish imports
 */
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
function import_process_queue() {

    if (!$ready = get_records_select_array('import_queue',
        'ready = ? OR expirytime <  ?', array(1, db_format_timestamp(time())),
        '', '*,' . db_format_tsfield('expirytime', 'ex'))) {
        return true;
    }

    $now = time();

    $processed = array();
    foreach ($ready as $item) {
        if ($item->ex < $now) {
            log_debug('deleting expired import record', $item);
            $processed[] = $item->id;
            continue;
        }
325
326
327
328
329
330
331
332
        $tr = null;
        if (!empty($item->host)) {
            $tr = new MnetImporterTransport($item);
        }
        else {
            $tr = new LocalImporterTransport($item);
        }
        $importer = PluginImport::create_importer($item->id, $tr, $item);
333
334
335
336
337
338
339
        try {
            $importer->prepare();
            $importer->process();
            $importer->cleanup();
            $processed[] = $item->id;
        }
        catch (Exception $e) {
340
            log_debug('an error occurred on import: ' . $e->getMessage());
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
            $importer->get('importertransport')->cleanup();
        }
    }

    if (empty($processed)) {
        return true;
    }

    delete_records_select(
        'import_queue',
        'id IN ( ' . implode(',', db_array_to_ph($processed)) . ')',
        $processed
    );
}

356
357
358
359
/**
 * base class for transport layers.
 * Implements helper methods and makes some abstract stuff
 */
360
361
abstract class ImporterTransport {

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    /** temporary directory to work in if necessary  */
    protected $tempdir;

    /** the importer to eventually handle the import */
    protected $importer;

    /** unique id for the import directories.  usually the import queue id, but sometimes needs to be set manually */
    protected $importid;

    /** relative path inside the temporary directory */
    protected $relativepath;

    /** whether the tempdir has been set up already */
    private $tempdirprepared = false;

    /** the file to import (sometimes a zip file) */
    protected $importfile;

    /** the manifest file, if there is one and we know about it */
    protected $manifestfile;

    /** the mimetype of the file we are importing */
    protected $mimetype;

386
387
388
    /** the import queue record **/
    protected $importrecord;

389
    /** indicates whether the file has been extracted already */
390
391
392
393
394
    protected $extracted;

    /** optional sha1 of the file we expect */
    protected $expectedsha1;

395
396
397
398
399
    /**
     * @param stdclass $import the import record. This should correspond to a record in import_queue, but can be faked
     */
    public abstract function __construct($import);

400
401
402
403
404
405
406
407
408
409
    /**
     * small helper function to set up and unserialize the import data
     */
    protected  function set_import_data($import) {
        $this->importrecord = $import;
        if (is_string($import->data)) {
            $import->data = unserialize($import->data);
        }
    }

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    /**
     * figure out the temporary directory to use
     * and make sure it exists, etc
     */
    public function prepare_tempdir() {
        if ($this->tempdirprepared) {
            return true;
        }
        $this->relativepath = 'temp/import/' . $this->importid . '/';
        if ($tmpdir = get_config('unziptempdir')) {
            $this->tempdir = $tmpdir . $this->relativepath;
        }
        else {
            $this->tempdir = get_config('dataroot') . $this->relativepath;
        }
        if (!check_dir_exists($this->tempdir)) {
            throw new ImportException($this->importer, 'Failed to create the temporary directories to work in');
        }
        $this->tempdirprepared = true;

    }

    /**
     * helper get method
     * @todo maybe refactor this to __get
     */
    public function get($field) {
        if (!property_exists($this,$field)) {
            throw new ParamOutOfRangeException("Field $field wasn't found in class " . get_class($this));
        }
        return $this->{$field};
    }

443
444
445
446
447
    /**
    * this might be a path to a directory containing the files
    * or an array containing some other info
    * or the path to a file, depending on the format
    */
448
449
450
451
452
453
454
455
    public function files_info() {
        return array(
            'importfile'   => $this->importfile,
            'tempdir'      => $this->tempdir,
            'relativepath' => $this->relativepath,
            'manifestfile' => $this->manifestfile,
        );
    }
456
457
458
459
460
461
462
463
464
465

    /**
    * do whatever is necessary to retrieve the file(s)
    */
    public abstract function prepare_files();

    /**
    * cleanup temporary working area
    */
    public function cleanup() {
466
467
468
469
470
        if (empty($this->tempdir)) {
            return;
        }
        require_once('file.php');
        rmdirr($this->tempdir);
471
472
    }

473
474
475
476
477
478
479
480
    /*
     * set the importer object
     * this must be done before prepare_files is called
     *
     * @param PluginImport $importer
     */
    public function set_importer(PluginImport $importer) {
        $this->importer = $importer;
481
482
483
    }

    /**
484
485
     * helper function for import code to use to extract a file
     * it will either unzip a zip file, or move an import file to the destination
486
     *
487
     * @throws ImportException
488
     */
489
490
491
492
    public function extract_file() {
        if ($this->extracted) {
            return;
        }
493
        $this->prepare_tempdir();
494
        if ($this->expectedsha1 &&  sha1_file($this->importfile) != $this->expectedsha1) {
495
496
497
498
499
500
501
502
503
            throw new ImportException($this->importer, 'sha1 of recieved importfile didn\'t match expected sha1');
        }

        $todir = $this->tempdir . 'extract/';
        if (!check_dir_exists($todir)) {
            throw new ImportException($this, 'Failed to create the temporary directories to work in');
        }
        safe_require('artefact', 'file');
        $ziptypes = PluginArtefactFile::get_mimetypes_from_description('zip');
504
        if (empty($this->mimetype)) {
505
506
            require_once('file.php');
            $this->mimetype = file_mime_type($this->importfile);
507
        }
508
        // if we don't have a zipfile, just move the import file to the extract location
509
        if (!in_array($this->mimetype, $ziptypes)) {
510
511
512
513
            if (strpos($this->importfile, $todir) !== 0) {
                rename($this->importfile, $todir . $this->importfilename);
            }
            $this->manifestfile = $this->importfilename;
514
            $this->extracted = true;
515
516
517
            return;
        }

518
519
520
521
        $archive = new ZipArchive();
        if ($archive->open($this->importfile) && $archive->extractTo($todir)) {
            // successfully extracted
            $archive->close();
522
        }
523
524
        else {
            throw new ImportException($this, 'Failed to unzip the file recieved from the transport object');
525
        }
526
        $this->extracted = true;
527
    }
528
529
530
531
532

    /**
     * validate data to be imported
     */
    public abstract function validate_import_data();
533
534
}

535
536
537
538
/**
 * class to handle 'local' transport - eg uploaded files
*/
class LocalImporterTransport extends ImporterTransport {
539

540
541
542
543
    /**
     * @param stdclass $import the import record
     */
    public function __construct($import) {
544
        $this->set_import_data($import);
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
        if (isset($this->importrecord->data['extracted']) && $this->importrecord->data['extracted']) {
            $this->importid = $this->importrecord->data['importid'];
            $this->mimetype = $this->importrecord->data['mimetype'];
            $this->extracted = true;
            $this->relativepath = 'temp/import/' . $this->importid . '/';
            if ($tmpdir = get_config('unziptempdir')) {
                $this->tempdir = $tmpdir . $this->relativepath;
            }
            else {
                $this->tempdir = get_config('dataroot') . $this->relativepath;
            }
            if (!check_dir_exists($this->tempdir)) {
                throw new ImportException($this->importer, 'Failed to access the temporary directories to work in');
            }
            $this->tempdirprepared = true;
        }
        else {
            foreach (array('importfile', 'importfilename', 'importid', 'mimetype') as $reqkey) {
                if (!array_key_exists($reqkey, $this->importrecord->data)) {
                    throw new ImportException("Missing required information $reqkey");
                }
                $this->{$reqkey} = $this->importrecord->data[$reqkey];
567
568
            }
        }
569
    }
570

571
572
573
    public function validate_import_data() { }


574
575
576
    public function cleanup() {
        parent::cleanup();
    }
577

578
579
580
    // nothing to do, unzipping is handled elsewhere
    public function prepare_files() { }
}
581
582


583
584
585
586
587
/**
 * transport layer for mnet based imports
 * this just overrides prepare_files to do an xmlrpc request
 */
class MnetImporterTransport extends ImporterTransport {
588

589
590
591
592
593
594
595
596
597
598
599
    /** xmlrpc host */
    private $host;
    /** token set for retrieiving files */
    private $token;

    /**
     * @param stdclass $import the import record
     */
    public function __construct($import) {
        $this->host = get_record('host', 'wwwroot', $import->host);
        $this->importid = $import->id; // since we have an import record, use the id
600
601
        $this->set_import_data($import);
        $this->expectedsha1 = $this->importrecord->data['zipfilesha1'];
602
603
    }

604
605
606
    /**
     * retrieves the files from the remote host
     */
607
    public function prepare_files() {
608
609
610
611
612
        if (empty($this->importer)) {
            throw new ImportException(null, 'Failed to initialise XMLRPC file retrieval - no importer object');
        }
        $this->prepare_tempdir();
        $this->token = $this->importer->get('token');
613
614
615
616
617
618
619
        require_once(get_config('docroot') . 'api/xmlrpc/client.php');
        $client = new Client();
        try {
            $client->set_method('portfolio/mahara/lib.php/fetch_file')
                    ->add_param($this->token)
                    ->send($this->host->wwwroot);
        } catch (XmlrpcClientException $e) {
620
            throw new ImportException($this->importer, 'Failed to retrieve zipfile from remote server: ' . $e->getMessage());
621
622
        }
        if (!$filecontents = base64_decode($client->response)) {
623
            throw new ImportException($this->importer, 'Failed to retrieve zipfile from remote server');
624
625
        }

626
627
628
        $this->importfilename = 'import.zip';
        $this->importfile = $this->tempdir . $this->importfilename;
        if (!file_put_contents($this->tempdir  . $this->importfilename, $filecontents)) {
629
            throw new ImportException($this->importer, 'Failed to write out the zipfile to local temporary storage');
630
        }
631
632
        // detect the filetype and bail if it's not a zip file
        safe_require('artefact', 'file');
633
        require_once('file.php');
634
        $ziptypes = PluginArtefactFile::get_mimetypes_from_description('zip');
635
        $this->mimetype = file_mime_type($this->tempdir . $this->importfilename);
636
637
638
        if (!in_array($this->mimetype, $ziptypes)) {
            throw new ImportException($this->importer, 'Not a valid zipfile - mimetype was ' . $this->mimetype);
        }
639
640
641
    }


642
643
644
645
646
647
    /**
     * used for appending to the description of created data
     * eg "imported from blah server"
     *
     * @return string
     */
648
649
650
    public function get_description() {
        return get_string('remotehost', 'mahara', $this->host->name);
    }
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668

    public function validate_import_data() {
        $importdata = $this->importrecord->data;
        if (is_string($importdata)) {
            $importdata = unserialize($importdata);
        }
        if (empty($importdata) ||
            !is_array($importdata) ||
            !array_key_exists('filesmanifest', $importdata) ||
            !is_array($importdata['filesmanifest']) ||
            count($importdata['filesmanifest']) == 0) {
            throw new ImportException($this, 'Missing files manifest in import data');
        }
        if (!array_key_exists('zipfilesha1', $importdata)) {
            throw new ImportException($this, 'Missing zipfile sha1 in import data');
        }
        return true;
    }
669
670
}

671
672
673
674
675
676
677
/**
 * Looks in the import staging area in dataroot and deletes old, unneeded
 * import.
 */
function import_cleanup_old_imports() {
    require_once('file.php');
    $basedir = get_config('dataroot') . 'import/';
678
679
680
    if (!check_dir_exists($basedir, false)) {
        return;
    }
681
682
683
684
685
686
687
688
689
690
691
692
    $importdir = new DirectoryIterator($basedir);
    $mintime = time() - (12 * 60 * 60); // delete imports older than 12 hours

    // The import dir contains one directory for each attempted import, named
    // after their username and the import timestamp
    foreach ($importdir as $attemptdir) {
        if ($attemptdir->isDot()) continue;
        if ($attemptdir->getCTime() < $mintime) {
            rmdirr($basedir . $attemptdir->getFilename());
        }
    }
}