lib.php 26.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
    // Valid import formats
    const IMPORT_FORMAT_FILE = 'file';
    const IMPORT_FORMAT_LEAP = 'leap';

53
54
55
56
57
    protected $id;
    protected $data;
    protected $expirytime;
    protected $usr;
    protected $usrobj;
58

59
60
61
    /** the ImporterTransport object to use */
    protected $importertransport;

62
63
64
65
    public static function get_plugintype_name() {
        return 'import';
    }

66
67
68
69
    /**
     * @param int $id the queue record id
     * @param stdclass $record (optional, pass this to save db queries)
     */
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    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);
84
85
86
87
88
89
90

        $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'),
        );
91
    }
92

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

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

    /**
111
    * process the files and adds them to the user's artefact area
112
    */
113
    public abstract function process($step = PluginImport::STEP_NON_INTERACTIVE);
114

115
116
117
    /**
     * perform cleanup tasks, delete temp files etc
     */
118
119
120
121
    public function cleanup() {
        $this->importertransport->cleanup();
    }

122
123
124
125
    /**
     * helper method to return member variables
     * @todo maybe refactor this to just use __get
     */
126
127
128
129
130
131
132
    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};
    }

133
    /**
134
135
136
137
     * 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.
     * If the format is not valid, it will raise an error.
138
     */
139
    public static function class_from_format($format) {
140
        $format = self::validate_import_format($format);
141
142
143
144
145
146
147
148
149
150
151
152
        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 $host      wwwroot of mnet host if applicable
    * @param int    $ready     whether the import is ready to start (usually no)
    */
153
    public static function create_new_queue($userid, $host=null, $ready=0) {
154
155
156
157
158
159
160
161
        // 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)),
162
            'format'     => null,
163
164
165
166
167
        );
        $queue->id = insert_record('import_queue', $queue);
        return $queue;
    }

168
169
170
171
172
173
174
175
176
177
    /**
     * 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) {
178
179
180
181
182
183
        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);
184
185
186
187
        $i =  new $class($id,$record);
        $i->set_transport($transporter);
        $transporter->set_importer($i);
        return $i;
188
189
    }

190
191
192
193
194
195
196
    /**
     * 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
     */
197
198
199
200
201
202
203
204
205
206
207
208
209
    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();
    }
210
211

    /**
212
     * Add an import request of an interactive import entry as an Mahara view+collection or artefact.
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
     * 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
245
246
247
248
249
250
            if (isset($entrydata['content']['type']) && in_array($entrydata['content']['type'], array('dashboard', 'profile'))) {
                $decision = PluginImport::DECISION_IGNORE;
            }
            else {
                $decision = PluginImport::DECISION_ADDNEW;
            }
251
252
253
254
255
256
257
        }
        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;
            }
258
259
260
261
262
263
            // 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'];
            }
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
            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,
296
                'ctime' => db_format_timestamp(time()),
297
298
299
300
301
            ));
        }
        return false;
    }

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    /**
     * helper function to return all the valid formats allowed.
     *
     * @return an array of valid import formats.
     */
    public static function get_valid_import_formats() {
        $installed = plugins_installed('import', false);
        $valid_formats = array();
        if ($installed != false) {
            foreach ($installed as $i) {
                $valid_formats[] = $i->name;
            }
        }
        return $valid_formats;
    }

    /**
     * Given an import format, validate it. Ensure the format is installed and active.
     * If it's not, raise an error.
     *
     * @param string $format - the input format to validate.
     *
     * @return string a valid format.
     */
    public static function validate_import_format($format) {
        $format = trim($format);
        $corr = array(
            'files'  => self::IMPORT_FORMAT_FILE,
            'leap2a' => self::IMPORT_FORMAT_LEAP,
        );
        foreach ($corr as $bad => $good) {
            if ($format == $bad) {
                $format = $good;
                break;
            }
        }
        // Now check to make sure that the format is correct
        // and is installed. If not, raise an error.
        $corr = self::get_valid_import_formats();
        if (!in_array($format, $corr)) {
            throw new ImportException(null, "Invalid import format $format");
        }
        return $format;
    }

347
348
}

349
350
351
/**
 * cron job to process the queue and wake up and finish imports
 */
352
353
function import_process_queue() {

354
355
    raise_memory_limit('512M');

356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
    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;
        }
371
372
373
374
375
376
377
        $tr = null;
        if (!empty($item->host)) {
            $tr = new MnetImporterTransport($item);
        }
        else {
            $tr = new LocalImporterTransport($item);
        }
378
        try {
379
            $importer = PluginImport::create_importer($item->id, $tr, $item);
380
381
382
383
384
385
            $importer->prepare();
            $importer->process();
            $importer->cleanup();
            $processed[] = $item->id;
        }
        catch (Exception $e) {
386
            log_debug('an error occurred on import: ' . $e->getMessage());
387
388
389
            if (isset($importer)) {
                $importer->get('importertransport')->cleanup();
            }
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        }
    }

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

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

404
405
406
407
/**
 * base class for transport layers.
 * Implements helper methods and makes some abstract stuff
 */
408
409
abstract class ImporterTransport {

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
    /** 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;

434
435
436
    /** the import queue record **/
    protected $importrecord;

437
    /** indicates whether the file has been extracted already */
438
439
440
441
442
    protected $extracted;

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

443
444
445
446
447
    /**
     * @param stdclass $import the import record. This should correspond to a record in import_queue, but can be faked
     */
    public abstract function __construct($import);

448
449
450
451
452
453
454
455
456
457
    /**
     * 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);
        }
    }

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
    /**
     * 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};
    }

491
492
493
494
495
    /**
    * 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
    */
496
497
498
499
500
501
502
503
    public function files_info() {
        return array(
            'importfile'   => $this->importfile,
            'tempdir'      => $this->tempdir,
            'relativepath' => $this->relativepath,
            'manifestfile' => $this->manifestfile,
        );
    }
504
505
506
507
508
509
510
511
512
513

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

    /**
    * cleanup temporary working area
    */
    public function cleanup() {
514
515
516
517
518
        if (empty($this->tempdir)) {
            return;
        }
        require_once('file.php');
        rmdirr($this->tempdir);
519
520
    }

521
522
523
524
525
526
527
528
    /*
     * 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;
529
530
531
    }

    /**
532
533
     * 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
534
     *
535
     * @throws ImportException
536
     */
537
538
539
540
    public function extract_file() {
        if ($this->extracted) {
            return;
        }
541
        $this->prepare_tempdir();
542
        if ($this->expectedsha1 &&  sha1_file($this->importfile) != $this->expectedsha1) {
543
544
545
546
547
548
549
550
551
            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');
552
        if (empty($this->mimetype)) {
553
554
            require_once('file.php');
            $this->mimetype = file_mime_type($this->importfile);
555
        }
556
        // if we don't have a zipfile, just move the import file to the extract location
557
        if (!in_array($this->mimetype, $ziptypes)) {
558
559
560
561
            if (strpos($this->importfile, $todir) !== 0) {
                rename($this->importfile, $todir . $this->importfilename);
            }
            $this->manifestfile = $this->importfilename;
562
            $this->extracted = true;
563
564
565
            return;
        }

566
567
568
569
        $archive = new ZipArchive();
        if ($archive->open($this->importfile) && $archive->extractTo($todir)) {
            // successfully extracted
            $archive->close();
570
        }
571
572
        else {
            throw new ImportException($this, 'Failed to unzip the file recieved from the transport object');
573
        }
574
        $this->extracted = true;
575
    }
576
577
578
579
580

    /**
     * validate data to be imported
     */
    public abstract function validate_import_data();
581
582
}

583
584
585
586
/**
 * class to handle 'local' transport - eg uploaded files
*/
class LocalImporterTransport extends ImporterTransport {
587

588
589
590
591
    /**
     * @param stdclass $import the import record
     */
    public function __construct($import) {
592
        $this->set_import_data($import);
593
594
        if (isset($this->importrecord->data['extracted']) && $this->importrecord->data['extracted']) {
            $this->importid = $this->importrecord->data['importid'];
595
            $this->manifestfile = $this->importrecord->data['manifestfile'];
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
            $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];
616
617
            }
        }
618
    }
619

620
621
622
    public function validate_import_data() { }


623
624
625
    public function cleanup() {
        parent::cleanup();
    }
626

627
628
629
    // nothing to do, unzipping is handled elsewhere
    public function prepare_files() { }
}
630
631


632
633
634
635
636
/**
 * transport layer for mnet based imports
 * this just overrides prepare_files to do an xmlrpc request
 */
class MnetImporterTransport extends ImporterTransport {
637

638
639
640
641
642
643
644
645
646
647
648
    /** 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
649
650
        $this->set_import_data($import);
        $this->expectedsha1 = $this->importrecord->data['zipfilesha1'];
651
652
    }

653
654
655
    /**
     * retrieves the files from the remote host
     */
656
    public function prepare_files() {
657
658
659
660
661
        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');
662
663
664
665
666
667
668
        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) {
669
            throw new ImportException($this->importer, 'Failed to retrieve zipfile from remote server: ' . $e->getMessage());
670
671
        }
        if (!$filecontents = base64_decode($client->response)) {
672
            throw new ImportException($this->importer, 'Failed to retrieve zipfile from remote server');
673
674
        }

675
676
677
        $this->importfilename = 'import.zip';
        $this->importfile = $this->tempdir . $this->importfilename;
        if (!file_put_contents($this->tempdir  . $this->importfilename, $filecontents)) {
678
            throw new ImportException($this->importer, 'Failed to write out the zipfile to local temporary storage');
679
        }
680
681
        // detect the filetype and bail if it's not a zip file
        safe_require('artefact', 'file');
682
        require_once('file.php');
683
        $ziptypes = PluginArtefactFile::get_mimetypes_from_description('zip');
684
        $this->mimetype = file_mime_type($this->tempdir . $this->importfilename);
685
686
687
        if (!in_array($this->mimetype, $ziptypes)) {
            throw new ImportException($this->importer, 'Not a valid zipfile - mimetype was ' . $this->mimetype);
        }
688
689
690
    }


691
692
693
694
695
696
    /**
     * used for appending to the description of created data
     * eg "imported from blah server"
     *
     * @return string
     */
697
698
699
    public function get_description() {
        return get_string('remotehost', 'mahara', $this->host->name);
    }
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717

    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;
    }
718
719
}

720
721
722
/**
 * Looks in the import staging area in dataroot and deletes old, unneeded
 * import.
723
 * Also cleans up old import entries in the 'import_entry_requests' table
724
725
726
727
728
 */
function import_cleanup_old_imports() {
    require_once('file.php');
    $mintime = time() - (12 * 60 * 60); // delete imports older than 12 hours

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
    // remove entries from db older than 12 hours
    delete_records_select('import_entry_requests', 'ctime < ?', array(db_format_timestamp($mintime)));

    // Delete old files from the import staging area in dataroot
    $basedir = get_config('dataroot') . 'import/';
    if (check_dir_exists($basedir, false)) {
        $importdir = new DirectoryIterator($basedir);

        // 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());
            }
744
745
746
        }
    }
}