forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
installlib.php
495 lines (429 loc) · 16.3 KB
/
installlib.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
296
297
298
299
300
301
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
491
492
493
494
495
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
/**
* Functions to support installation process
*
* @package core
* @subpackage install
* @copyright 2009 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/** INSTALL_WELCOME = 0 */
define('INSTALL_WELCOME', 0);
/** INSTALL_ENVIRONMENT = 1 */
define('INSTALL_ENVIRONMENT', 1);
/** INSTALL_PATHS = 2 */
define('INSTALL_PATHS', 2);
/** INSTALL_DOWNLOADLANG = 3 */
define('INSTALL_DOWNLOADLANG', 3);
/** INSTALL_DATABASETYPE = 4 */
define('INSTALL_DATABASETYPE', 4);
/** INSTALL_DATABASE = 5 */
define('INSTALL_DATABASE', 5);
/** INSTALL_SAVE = 6 */
define('INSTALL_SAVE', 6);
/**
* Tries to detect the right www root setting.
* @return string detected www root
*/
function install_guess_wwwroot() {
$wwwroot = '';
if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
$wwwroot .= 'http://';
} else {
$wwwroot .= 'https://';
}
$hostport = explode(':', $_SERVER['HTTP_HOST']);
$wwwroot .= reset($hostport);
if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
$wwwroot .= ':'.$_SERVER['SERVER_PORT'];
}
$wwwroot .= $_SERVER['SCRIPT_NAME'];
list($wwwroot, $xtra) = explode('/install.php', $wwwroot);
return $wwwroot;
}
/**
* Copy of @see{ini_get_bool()}
* @param string $ini_get_arg
* @return bool
*/
function install_ini_get_bool($ini_get_arg) {
$temp = ini_get($ini_get_arg);
if ($temp == '1' or strtolower($temp) == 'on') {
return true;
}
return false;
}
/**
* Creates dataroot if not exists yet,
* makes sure it is writable, add lang directory
* and add .htaccess just in case it works.
*
* @param string $dataroot full path to dataroot
* @param int $dirpermissions
* @return bool success
*/
function install_init_dataroot($dataroot, $dirpermissions) {
if (file_exists($dataroot) and !is_dir($dataroot)) {
// file with the same name exists
return false;
}
umask(0000);
if (!file_exists($dataroot)) {
if (!mkdir($dataroot, $dirpermissions, true)) {
// most probably this does not work, but anyway
return false;
}
}
@chmod($dataroot, $dirpermissions);
if (!is_writable($dataroot)) {
return false; // we can not continue
}
// create the directory for $CFG->tempdir
if (!is_dir("$dataroot/temp")) {
if (!mkdir("$dataroot/temp", $dirpermissions, true)) {
return false;
}
}
if (!is_writable("$dataroot/temp")) {
return false; // we can not continue
}
// create the directory for $CFG->cachedir
if (!is_dir("$dataroot/cache")) {
if (!mkdir("$dataroot/cache", $dirpermissions, true)) {
return false;
}
}
if (!is_writable("$dataroot/cache")) {
return false; // we can not continue
}
// create the directory for $CFG->langotherroot
if (!is_dir("$dataroot/lang")) {
if (!mkdir("$dataroot/lang", $dirpermissions, true)) {
return false;
}
}
if (!is_writable("$dataroot/lang")) {
return false; // we can not continue
}
// finally just in case some broken .htaccess that prevents access just in case it is allowed
if (!file_exists("$dataroot/.htaccess")) {
if ($handle = fopen("$dataroot/.htaccess", 'w')) {
fwrite($handle, "deny from all\r\nAllowOverride None\r\nNote: this file is broken intentionally, we do not want anybody to undo it in subdirectory!\r\n");
fclose($handle);
} else {
return false;
}
}
return true;
}
/**
* Print help button
* @param string $url
* @param string $titel
* @return void
*/
function install_helpbutton($url, $title='') {
if ($title == '') {
$title = get_string('help');
}
echo "<a href=\"javascript:void(0)\" ";
echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
echo ">";
echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
echo "</a>\n";
}
/**
* This is in function because we want the /install.php to parse in PHP4
*
* @param object $database
* @param string $dbhsot
* @param string $dbuser
* @param string $dbpass
* @param string $dbname
* @param string $prefix
* @param mixed $dboptions
* @return string
*/
function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) {
try {
try {
$database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
} catch (moodle_exception $e) {
// let's try to create new database
if ($database->create_database($dbhost, $dbuser, $dbpass, $dbname, $dboptions)) {
$database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
} else {
throw $e;
}
}
return '';
} catch (dml_exception $ex) {
return get_string($ex->errorcode, $ex->module, $ex->a).'<br />'.$ex->debuginfo;
}
}
/**
* Returns content of config.php file.
*
* Uses PHP_EOL for generating proper end of lines for the given platform.
*
* @param moodle_database $database database instance
* @param object $cfg copy of $CFG
* @return string
*/
function install_generate_configphp($database, $cfg) {
$configphp = '<?php // Moodle configuration file' . PHP_EOL . PHP_EOL;
$configphp .= 'unset($CFG);' . PHP_EOL;
$configphp .= 'global $CFG;' . PHP_EOL;
$configphp .= '$CFG = new stdClass();' . PHP_EOL . PHP_EOL; // prevent PHP5 strict warnings
$dbconfig = $database->export_dbconfig();
foreach ($dbconfig as $key=>$value) {
$key = str_pad($key, 9);
$configphp .= '$CFG->'.$key.' = '.var_export($value, true) . ';' . PHP_EOL;
}
$configphp .= PHP_EOL;
$configphp .= '$CFG->wwwroot = '.var_export($cfg->wwwroot, true) . ';' . PHP_EOL ;
$configphp .= '$CFG->dataroot = '.var_export($cfg->dataroot, true) . ';' . PHP_EOL;
$configphp .= '$CFG->admin = '.var_export($cfg->admin, true) . ';' . PHP_EOL . PHP_EOL;
if (empty($cfg->directorypermissions)) {
$chmod = '02777';
} else {
$chmod = '0' . decoct($cfg->directorypermissions);
}
$configphp .= '$CFG->directorypermissions = ' . $chmod . ';' . PHP_EOL . PHP_EOL;
$configphp .= '$CFG->passwordsaltmain = '.var_export(complex_random_string(), true) . ';' . PHP_EOL . PHP_EOL;
$configphp .= 'require_once(dirname(__FILE__) . \'/lib/setup.php\');' . PHP_EOL . PHP_EOL;
$configphp .= '// There is no php closing tag in this file,' . PHP_EOL;
$configphp .= '// it is intentional because it prevents trailing whitespace problems!' . PHP_EOL;
return $configphp;
}
/**
* Prints complete help page used during installation.
* Does not return.
*
* @global object
* @param string $help
*/
function install_print_help_page($help) {
global $CFG, $OUTPUT; //TODO: MUST NOT USE $OUTPUT HERE!!!
@header('Content-Type: text/html; charset=UTF-8');
@header('X-UA-Compatible: IE=edge');
@header('Cache-Control: no-store, no-cache, must-revalidate');
@header('Cache-Control: post-check=0, pre-check=0', false);
@header('Pragma: no-cache');
@header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
@header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
<head>
<link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />
<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
<title>'.get_string('installation','install').'</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />';
echo '</head><body>';
switch ($help) {
case 'phpversionhelp':
print_string($help, 'install', phpversion());
break;
case 'memorylimithelp':
print_string($help, 'install', @ini_get('memory_limit'));
break;
default:
print_string($help, 'install');
}
echo $OUTPUT->close_window_button(); //TODO: MUST NOT USE $OUTPUT HERE!!!
echo '</body></html>';
die;
}
/**
* Prints installation page header, we can no use weblib yet in installer.
*
* @global object
* @param array $config
* @param string $stagename
* @param string $heading
* @param string $stagetext
* @return void
*/
function install_print_header($config, $stagename, $heading, $stagetext) {
global $CFG;
@header('Content-Type: text/html; charset=UTF-8');
@header('X-UA-Compatible: IE=edge');
@header('Cache-Control: no-store, no-cache, must-revalidate');
@header('Cache-Control: post-check=0, pre-check=0', false);
@header('Pragma: no-cache');
@header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
@header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
<head>
<link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />';
echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
<title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />';
echo '</head><body class="notloggedin">
<div id="page" class="stage'.$config->stage.'">
<div id="page-header">
<div id="header" class=" clearfix">
<h1 class="headermain">'.get_string('installation','install').'</h1>
<div class="headermenu"> </div>
</div>
<div class="navbar clearfix">
<div class="breadcrumb">
<ul><li class="first">'.$stagename.'</li></ul>
</div>
<div class="navbutton"> </div>
</div>
</div>
<!-- END OF HEADER -->
<div id="installdiv">';
echo '<h2>'.$heading.'</h2>';
if ($stagetext !== '') {
echo '<div class="stage generalbox box">';
echo $stagetext;
echo '</div>';
}
// main
echo '<form id="installform" method="post" action="install.php"><fieldset>';
foreach ($config as $name=>$value) {
echo '<input type="hidden" name="'.$name.'" value="'.s($value).'" />';
}
}
/**
* Prints installation page header, we can no use weblib yet in isntaller.
*
* @global object
* @param array $config
* @param bool $reload print reload button instead of next
* @return void
*/
function install_print_footer($config, $reload=false) {
global $CFG;
if ($config->stage > INSTALL_WELCOME) {
$first = '<input type="submit" id="previousbutton" name="previous" value="« '.s(get_string('previous')).'" />';
} else {
$first = '<input type="submit" id="previousbutton" name="next" value="'.s(get_string('reload')).'" />';
$first .= '<script type="text/javascript">
//<![CDATA[
var first = document.getElementById("previousbutton");
first.style.visibility = "hidden";
//]]>
</script>
';
}
if ($reload) {
$next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('reload')).'" />';
} else {
$next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('next')).' »" />';
}
echo '</fieldset><fieldset id="nav_buttons">'.$first.$next.'</fieldset>';
$homelink = '<div class="sitelink">'.
'<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'.
'<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
echo '</form></div>';
echo '<div id="footer"><hr />'.$homelink.'</div>';
echo '</div></body></html>';
}
/**
* Install Moodle DB,
* config.php must exist, there must not be any tables in db yet.
*
* @param array $options adminpass is mandatory
* @param bool $interactive
* @return void
*/
function install_cli_database(array $options, $interactive) {
global $CFG, $DB;
require_once($CFG->libdir.'/environmentlib.php');
require_once($CFG->libdir.'/upgradelib.php');
// show as much debug as possible
@error_reporting(E_ALL | E_STRICT);
@ini_set('display_errors', '1');
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = true;
$CFG->version = '';
$CFG->release = '';
$CFG->branch = '';
$version = null;
$release = null;
$branch = null;
// read $version and $release
require($CFG->dirroot.'/version.php');
if ($DB->get_tables() ) {
cli_error(get_string('clitablesexist', 'install'));
}
if (empty($options['adminpass'])) {
cli_error('Missing required admin password');
}
// test environment first
list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
if (!$envstatus) {
$errors = environment_get_errors($environment_results);
cli_heading(get_string('environment', 'admin'));
foreach ($errors as $error) {
list($info, $report) = $error;
echo "!! $info !!\n$report\n\n";
}
exit(1);
}
if (!$DB->setup_is_unicodedb()) {
if (!$DB->change_db_encoding()) {
// If could not convert successfully, throw error, and prevent installation
cli_error(get_string('unicoderequired', 'admin'));
}
}
if ($interactive) {
cli_separator();
cli_heading(get_string('databasesetup'));
}
// install core
install_core($version, true);
set_config('release', $release);
set_config('branch', $branch);
if (PHPUNIT_TEST) {
// mark as test database as soon as possible
set_config('phpunittest', 'na');
}
// install all plugins types, local, etc.
upgrade_noncore(true);
// set up admin user password
$DB->set_field('user', 'password', hash_internal_user_password($options['adminpass']), array('username' => 'admin'));
// rename admin username if needed
if (isset($options['adminuser']) and $options['adminuser'] !== 'admin' and $options['adminuser'] !== 'guest') {
$DB->set_field('user', 'username', $options['adminuser'], array('username' => 'admin'));
}
// indicate that this site is fully configured
set_config('rolesactive', 1);
upgrade_finished();
// log in as admin - we need do anything when applying defaults
session_set_user(get_admin());
// apply all default settings, do it twice to fill all defaults - some settings depend on other setting
admin_apply_default_settings(NULL, true);
admin_apply_default_settings(NULL, true);
set_config('registerauth', '');
// set the site name
if (isset($options['shortname']) and $options['shortname'] !== '') {
$DB->set_field('course', 'shortname', $options['shortname'], array('format' => 'site'));
}
if (isset($options['fullname']) and $options['fullname'] !== '') {
$DB->set_field('course', 'fullname', $options['fullname'], array('format' => 'site'));
}
}