-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathimportfile.php
120 lines (99 loc) · 4.91 KB
/
importfile.php
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
117
118
119
120
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Import strings from uploaded file and stage them.
*
* @package local_amos
* @copyright 2010 David Mudrak <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/local/amos/locallib.php');
require_once($CFG->dirroot . '/local/amos/mlanglib.php');
require_once($CFG->dirroot . '/local/amos/mlangparser.php');
require_once($CFG->dirroot . '/local/amos/importfile_form.php');
require_login(SITEID, false);
require_capability('local/amos:importfile', context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_url('/local/amos/importfile.php');
navigation_node::override_active_url(new moodle_url('/local/amos/stage.php'));
$PAGE->set_title('AMOS ' . get_string('importfile', 'local_amos'));
$PAGE->set_heading('AMOS ' . get_string('importfile', 'local_amos'));
$importform = new local_amos_importfile_form(null, local_amos_importfile_options());
if (($data = $importform->get_data()) and has_capability('local/amos:stage', context_system::instance())) {
$tmpdir = $CFG->dataroot . '/amos/temp/import-uploads/' . $USER->id;
check_dir_exists($tmpdir);
$filenameorig = basename($importform->get_new_filename('importfile'));
$filename = $filenameorig . '-' . md5(time() . '-' . $USER->id . '-'. random_string(20));
$pathname = $tmpdir . '/' . $filename;
if ($importform->save_file('importfile', $pathname)) {
// Prepare the list of files to import.
$stringfiles = array();
if (strtolower(substr($filenameorig, -4)) === '.zip') {
$tmpzipdir = $pathname . '-content';
check_dir_exists($tmpzipdir);
$fp = get_file_packer('application/zip');
$zipcontents = $fp->extract_to_pathname($pathname, $tmpzipdir);
if (!$zipcontents) {
notice(get_string('novalidzip', 'local_amos'), new moodle_url('/local/amos/stage.php'));
@remove_dir($tmpzipdir);
} else {
foreach ($zipcontents as $zipfilename => $zipfilestatus) {
// We want PHP files in the root of the ZIP only.
if ($zipfilestatus === true and basename($zipfilename) === $zipfilename &&
strtolower(substr($zipfilename, -4)) === '.php') {
$stringfiles[$zipfilename] = $tmpzipdir . '/' . $zipfilename;
}
}
}
} else if (strtolower(substr($filenameorig, -4)) === '.php') {
$stringfiles = array($filenameorig => $pathname);
}
if (empty($stringfiles)) {
notice(get_string('nostringtoimport', 'local_amos'), new moodle_url('/local/amos/stage.php'));
} else {
$stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey());
$version = mlang_version::by_code($data->version);
$parser = mlang_parser_factory::get_parser('php');
foreach ($stringfiles as $filenameorig => $pathname) {
$name = mlang_component::name_from_filename($filenameorig);
$component = new mlang_component($name, $data->language, $version);
try {
$parser->parse(file_get_contents($pathname), $component);
} catch (mlang_parser_exception $e) {
notice($e->getMessage(), new moodle_url('/local/amos/stage.php'));
}
$encomponent = mlang_component::from_snapshot($component->name, 'en', $version);
$component->intersect($encomponent);
if ($component->has_string()) {
$stage->add($component, true);
$component->clear();
$stage->store();
}
}
mlang_stash::autosave($stage);
}
if (!empty($tmpzipdir)) {
@remove_dir($tmpzipdir);
}
} else {
notice(get_string('nofiletoimport', 'local_amos'), new moodle_url('/local/amos/stage.php'));
}
}
if (!isset($stage) or !$stage->has_component()) {
notice(get_string('nostringtoimport', 'local_amos'), new moodle_url('/local/amos/stage.php'));
}
redirect(new moodle_url('/local/amos/stage.php'));