-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_cfg.php
196 lines (159 loc) · 6.83 KB
/
export_cfg.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
121
122
123
124
125
126
127
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
<?php
define('CLI_SCRIPT', true);
require('config.php');
require('lib/clilib.php');
$usage = "
php export_cfg.php --all
Export the config for Moodle core components and the plugins.
Can easily go up to 3,000 commands. You may want to > it into a file.
php export_cfg.php --plugins
Export the config for the plugins.
php export_cfg.php --component=xxx
Export the config for this particular component.
php export_cfg.php --all > settings.conf
On Linux, use the > operator to redirect the output to a file.
";
list($options, $unrecognized) = cli_get_params(
[
'all' => false,
'component' => false,
'help' => false,
'plugins' =>false,
'verbose' => false
],
['h' => 'help', 'v' => 'verbose', 'a' => 'all']
);
// If no param is passed or help is requested.
if (($options['component'] === false && $options['plugins'] === false && $options['all'] === false) || ($options['help'])) {
exit($usage);
}
// Process the passed parameter.
if ($options['all']) {
$components = get_config_plugins('core');
}
if ($options['plugins']) {
$components = get_config_plugins();
}
if ($options['component']) {
if (strpos($options['component'], ',') !== false) {
$components = explode(',', $options['component']);
} else {
$components = array($options['component']);
}
}
///////////////////////////////////////////
///////////// S E T T I N G S /////////////
// PHP localtion.
$php = '/usr/bin/php';
// Exit code.
$status = -1;
///////////////////////////////////////////
// Contains all the commands to output.
$output = '';
if (is_array($components)) {
$totalComponents = count($components);
foreach($components as $key => $value) {
$output .= output_settings($value, $key, $totalComponents);
}
}
echo $output;
function output_settings($component, $index, $totalComponents) {
global $php, $options, $status;
// Contains the output of the cfg.php
$cmdLineOutput = array();
// Contains the individual PHP commands.
$commands = '';
// Get the cfg.php Moodle script to output the component settings in JSON format.
// We export the settings in JSON so it is easier to manage the multi-line settings.
exec($php.' admin/cli/cfg.php --component='.$component.' --json', $cmdLineOutput, $status);
if ($status === 0) {
$objCmdLineOutput = json_decode($cmdLineOutput[0]);
// Do not print the component if it only has a version setting and no other settings.
if (count(get_object_vars($objCmdLineOutput)) == 1 && isset($objCmdLineOutput->version)) {
// Alert user that the module is skipped.
if ($options['verbose']) {
// Output the component being exported when in verbose mode.
cli_writeln('Skipping : ('.$index.'/'.$totalComponents.') ' . $component, STDERR);
}
return "";
}
// Make sure the incoming and the local components are at the same version.
// $localVersion = $DB->get_record('config_plugins', ['plugin' => $component, 'name' => 'version']);
// if ($localVersion->version == $objCmdLineOutput->version) {
// return "";
// }
$settingsToIgnore = array(
// Core
'siteidentifier', 'supportemail', 'siteadmins', 'themerev', 'jsrev', 'langrev', 'localcachedirpurged', 'scheduledtaskreset', 'allversionshash',
'fileslastcleanup', 'digestmailtimelast', 'scorm_updatetimelast', 'templaterev', 'noemailever', 'auth', 'enrol_plugins_enabled',
'cronremotepassword', 'secretphrase', 'recaptchapublickey', 'recaptchaprivatekey', 'allowedip', 'blockedip',
// Core - database
'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'wwwroot',
// Core - file permissions
'directorypermissions', 'dirroot', 'filepermissions', 'umaskpermissions',
// Core - path
'dataroot', 'libdir', 'tempdir', 'backuptempdir', 'cachedir', 'localcachedir', 'localrequestdir', 'langotherroot', 'langlocalroot',
'noreplyaddress', 'chat_serverhost', 'pathtogs', 'geoip2file', 'auth_instructions',
// Core - OS path
'pathtounoconv',
// Core - SMTP
'smtphosts', 'smtpsecure', 'smtpauthtype', 'smtpuser', 'smtppass', 'smtpmaxbulk', 'proxypassword',
// Cookie
'sessioncookie','sessioncookiepath', 'sessioncookiedomain',
// mod_lti
'kid', 'privatekey',
// filter_tex
'pathconvert', 'pathdvips', 'pathdvisvgm', 'pathlatex',
// Poodll user and secret
'cpapiuser', 'cpapisecret',
// auth_econcordia
'jwt_key', 'token_validation_url', 'login_validation_url', 'host',
// Moodle features
'enablestats', 'allowindexing', 'allowguestmymoodle', 'debug', 'debugdisplay', 'perfdebug', 'debugstringids', 'debugvalidators', 'debugpageinfo', 'loglifetime',
// Cron
'lastcroninterval', 'lastcronstart',
// H5P
'site_uuid', 'recentfetch', 'recentresponse',
// custom theme
'adfsurl'
);
foreach ($objCmdLineOutput as $name => $set) {
if (!in_array($name, $settingsToIgnore)) {
// Create the command to set the value.
$commands .= sprintf(
"%s admin/cli/cfg.php --component=%s --name=%s --set=%s\n",
$php, $component, $name, escapeshellarg($set)
);
// Display the new value for the setting when importing.
if ($options['verbose']) {
$commands .= 'echo -n "' . $name . ' of ' . $component . ' is set to: "' . "\n";
$commands .= sprintf("%s admin/cli/cfg.php --component=%s --name=%s\n\n", $php, $component, $name);
}
}
}
}
// Output exported module if verbose is on.
if ($options['verbose']) {
// Output the component being exported when in verbose mode.
cli_writeln('Exporting : ('.$index.'/'.$totalComponents.') ' . $component, STDERR);
}
return $commands;
}
/**
* Get the individual plugin name.
*
* @param str $extra Extra plugin to include such as core.
*
* @return array $components The plugins list.
*/
function get_config_plugins($extra = false) {
global $CFG, $DB;
if ($extra !== false) {
$components[] = $extra;
}
$plugins = $DB->get_records_sql("SELECT * FROM {$CFG->prefix}config_plugins GROUP BY plugin");
foreach ($plugins as $plugin) {
$components[] = $plugin->plugin;
}
return $components;
}