forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.php
589 lines (524 loc) · 20.4 KB
/
install.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?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/>.
/**
* This script creates config.php file and prepares database.
*
* This script is not intended for beginners!
* Potential problems:
* - environment check is not present yet
* - su to apache account or sudo before execution
* - not compatible with Windows platform
*
* @package core
* @subpackage cli
* @copyright 2009 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
// extra execution prevention - we can not just require config.php here
if (isset($_SERVER['REMOTE_ADDR'])) {
exit(1);
}
$help =
"Command line Moodle installer, creates config.php and initializes database.
Please note you must execute this script with the same uid as apache
or use chmod/chown after installation.
Site defaults may be changed via local/defaults.php.
Options:
--chmod=OCTAL-MODE Permissions of new directories created within dataroot.
Default is 2777. You may want to change it to 2770
or 2750 or 750. See chmod man page for details.
--lang=CODE Installation and default site language.
--wwwroot=URL Web address for the Moodle site,
required in non-interactive mode.
--dataroot=DIR Location of the moodle data folder,
must not be web accessible. Default is moodledata
in the parent directory.
--dbtype=TYPE Database type. Default is mysqli
--dbhost=HOST Database host. Default is localhost
--dbname=NAME Database name. Default is moodle
--dbuser=USERNAME Database user. Default is root
--dbpass=PASSWORD Database password. Default is blank
--dbsocket Use database sockets. Available for some databases only.
--prefix=STRING Table prefix for above database tables. Default is mdl_
--adminuser=USERNAME Username for the moodle admin account. Default is admin
--adminpass=PASSWORD Password for the moodle admin account,
required in non-interactive mode.
--non-interactive No interactive questions, installation fails if any
problem encountered.
--agree-license Indicates agreement with software license,
required in non-interactive mode.
-h, --help Print out this help
Example:
\$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
"; //TODO: localize, mark as needed in install - to be translated later when everything is finished
// Nothing to do if config.php exists
$configfile = dirname(dirname(dirname(__FILE__))).'/config.php';
if (file_exists($configfile)) {
require($configfile);
require_once($CFG->libdir.'/clilib.php');
list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help'));
if ($options['help']) {
echo $help;
echo "\n\n";
}
cli_error(get_string('clialreadyinstalled', 'install'));
}
$olddir = getcwd();
// change directory so that includes bellow work properly
chdir(dirname($_SERVER['argv'][0]));
// Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
// This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
@date_default_timezone_set(@date_default_timezone_get());
}
// make sure PHP errors are displayed - helps with diagnosing of problems
@error_reporting(E_ALL);
@ini_set('display_errors', '1');
// we need a lot of memory
@ini_set('memory_limit', '128M');
/** Used by library scripts to check they are being called by Moodle */
define('MOODLE_INTERNAL', true);
// Check that PHP is of a sufficient version
if (version_compare(phpversion(), "5.2.8") < 0) {
$phpversion = phpversion();
// do NOT localise - lang strings would not work here and we CAN NOT move it after installib
echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion).\n";
echo "Please upgrade your server software or install latest Moodle 1.9.x instead.";
die;
}
// set up configuration
$CFG = new stdClass();
$CFG->lang = 'en';
$CFG->dirroot = dirname(dirname(dirname(__FILE__)));
$CFG->libdir = "$CFG->dirroot/lib";
$CFG->wwwroot = "http://localhost";
$CFG->httpswwwroot = $CFG->wwwroot;
$CFG->dataroot = str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__)))).'/moodledata');
$CFG->docroot = 'http://docs.moodle.org';
$CFG->running_installer = true;
$CFG->early_install_lang = true;
$parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__))));
$CFG->admin = array_pop($parts);
require($CFG->dirroot.'/version.php');
$CFG->target_release = $release;
//point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
//the problem is that we need specific version of quickforms and hacked excel files :-(
ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
require_once($CFG->libdir.'/installlib.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/setuplib.php');
require_once($CFG->libdir.'/textlib.class.php');
require_once($CFG->libdir.'/weblib.php');
require_once($CFG->libdir.'/dmllib.php');
require_once($CFG->libdir.'/moodlelib.php');
require_once($CFG->libdir.'/deprecatedlib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/componentlib.class.php');
//Database types
$databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
'oci' => moodle_database::get_driver_instance('oci', 'native'),
'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
);
foreach ($databases as $type=>$database) {
if ($database->driver_installed() !== true) {
unset($databases[$type]);
}
}
if (empty($databases)) {
$defaultdb = '';
} else {
reset($databases);
$defaultdb = key($databases);
}
// now get cli options
list($options, $unrecognized) = cli_get_params(
array(
'chmod' => '2777',
'lang' => $CFG->lang,
'wwwroot' => '',
'dataroot' => $CFG->dataroot,
'dbtype' => $defaultdb,
'dbhost' => 'localhost',
'dbname' => 'moodle',
'dbuser' => 'root',
'dbpass' => '',
'dbsocket' => false,
'prefix' => 'mdl_',
'adminuser' => 'admin',
'adminpass' => '',
'non-interactive' => false,
'agree-license' => false,
'help' => false
),
array(
'h' => 'help'
)
);
$interactive = empty($options['non-interactive']);
// set up language
$lang = clean_param($options['lang'], PARAM_SAFEDIR);
if (file_exists($CFG->dirroot.'/install/lang/'.$lang)) {
$CFG->lang = $lang;
}
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help']) {
echo $help;
die;
}
//Print header
echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n";
//Fist select language
if ($interactive) {
cli_separator();
$languages = get_string_manager()->get_list_of_translations();
// format the langs nicely - 3 per line
$c = 0;
$langlist = '';
foreach ($languages as $key=>$lang) {
$c++;
$length = iconv_strlen($lang, 'UTF-8');
$padded = $lang.str_repeat(' ', 28-$length);
$langlist .= $padded;
if ($c % 3 == 0) {
$langlist .= "\n";
}
}
$default = $CFG->lang;
cli_heading(get_string('availablelangs', 'install'));
echo $langlist."\n";
$prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang);
$error = '';
do {
echo $error;
$input = cli_input($prompt, $default);
$input = clean_param($input, PARAM_SAFEDIR);
if (!file_exists($CFG->dirroot.'/install/lang/'.$input)) {
$error = get_string('cliincorrectvalueretry', 'admin')."\n";
} else {
$error = '';
}
} while ($error !== '');
$CFG->lang = $input;
} else {
// already selected and verified
}
// Set directorypermissions first
$chmod = octdec(clean_param($options['chmod'], PARAM_INT));
if ($interactive) {
cli_separator();
cli_heading('Data directories permission'); // todo localize
$prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod));
$error = '';
do {
echo $error;
$input = cli_input($prompt, decoct($chmod));
$input = octdec(clean_param($input, PARAM_INT));
if (empty($input)) {
$error = get_string('cliincorrectvalueretry', 'admin')."\n";
} else {
$error = '';
}
} while ($error !== '');
$chmod = $input;
} else {
if (empty($chmod)) {
$a = (object)array('option' => 'chmod', 'value' => decoct($chmod));
cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
}
}
$CFG->directorypermissions = $chmod;
//We need wwwroot before we test dataroot
$wwwroot = clean_param($options['wwwroot'], PARAM_URL);
$wwwroot = trim($wwwroot, '/');
if ($interactive) {
cli_separator();
cli_heading(get_string('wwwroot', 'install'));
if (strpos($wwwroot, 'http') === 0) {
$prompt = get_string('clitypevaluedefault', 'admin', $wwwroot);
} else {
$wwwroot = null;
$prompt = get_string('clitypevalue', 'admin');
}
$error = '';
do {
echo $error;
$input = cli_input($prompt, $wwwroot);
$input = clean_param($input, PARAM_URL);
$input = trim($input, '/');
if (strpos($input, 'http') !== 0) {
$error = get_string('cliincorrectvalueretry', 'admin')."\n";
} else {
$error = '';
}
} while ($error !== '');
$wwwroot = $input;
} else {
if (strpos($wwwroot, 'http') !== 0) {
$a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot);
cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
}
}
$CFG->wwwroot = $wwwroot;
$CFG->httpswwwroot = $CFG->wwwroot;
//We need dataroot before lang download
if (!empty($options['dataroot'])) {
$CFG->dataroot = $options['dataroot'];
}
if ($interactive) {
cli_separator();
$i=0;
while(is_dataroot_insecure()) {
$parrent = dirname($CFG->dataroot);
$i++;
if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
$CFG->dataroot = ''; //can not find secure location for dataroot
break;
}
$CFG->dataroot = dirname($parrent).'/moodledata';
}
cli_heading(get_string('dataroot', 'install'));
$error = '';
do {
if ($CFG->dataroot !== '') {
$prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
echo $error;
$CFG->dataroot = cli_input($prompt, $CFG->dataroot);
if ($CFG->dataroot === '') {
$error = get_string('cliincorrectvalueretry', 'admin')."\n";
} else if (is_dataroot_insecure()) {
$CFG->dataroot = '';
$error = get_string('pathsunsecuredataroot', 'install')."\n";
} else {
if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
$error = '';
} else {
$a = (object)array('dataroot' => $CFG->dataroot);
$error = get_string('pathserrcreatedataroot', 'install', $a)."\n";
}
}
} while ($error !== '');
} else {
if (is_dataroot_insecure()) {
cli_error(get_string('pathsunsecuredataroot', 'install'));
}
if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
$a = (object)array('dataroot' => $CFG->dataroot);
cli_error(get_string('pathserrcreatedataroot', 'install', $a));
}
}
//download lang pack with optional notification
if ($CFG->lang != 'en') {
if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
if ($cd->install() == COMPONENT_ERROR) {
if ($cd->get_error() == 'remotedownloaderror') {
$a = new stdClass();
$a->url = 'http://download.moodle.org/langpack/2.0/'.$CFG->lang.'.zip';
$a->dest = $CFG->dataroot.'/lang';
cli_problem(get_string($cd->get_error(), 'error', $a));
} else {
cli_problem(get_string($cd->get_error(), 'error'));
}
} else {
// install parent lang if defined
if ($parentlang = get_parent_language()) {
if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
$cd->install();
}
}
}
}
}
// switch the string_manager instance to stop using install/lang/
$CFG->early_install_lang = false;
$CFG->langotherroot = $CFG->dataroot.'/lang';
$CFG->langlocalroot = $CFG->dataroot.'/lang';
get_string_manager(true);
// ask for db type - show only drivers available
if ($interactive) {
$options['dbtype'] = strtolower($options['dbtype']);
cli_separator();
cli_heading(get_string('databasetypehead', 'install'));
foreach ($databases as $type=>$database) {
echo " $type \n";
}
if (!empty($databases[$options['dbtype']])) {
$prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
$CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases));
} else {
if (empty($databases[$options['dbtype']])) {
$a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']);
cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
}
$CFG->dbtype = $options['dbtype'];
}
$database = $databases[$CFG->dbtype];
// ask for db host
if ($interactive) {
cli_separator();
cli_heading(get_string('databasehost', 'install'));
if ($options['dbhost'] !== '') {
$prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
$CFG->dbhost = cli_input($prompt, $options['dbhost']);
} else {
$CFG->dbhost = $options['dbhost'];
}
// ask for db name
if ($interactive) {
cli_separator();
cli_heading(get_string('databasename', 'install'));
if ($options['dbname'] !== '') {
$prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
$CFG->dbname = cli_input($prompt, $options['dbname']);
} else {
$CFG->dbname = $options['dbname'];
}
// ask for db prefix
if ($interactive) {
cli_separator();
cli_heading(get_string('dbprefix', 'install'));
//TODO: solve somehow the prefix trouble for oci
if ($options['prefix'] !== '') {
$prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
$CFG->prefix = cli_input($prompt, $options['prefix']);
} else {
$CFG->prefix = $options['prefix'];
}
// ask for db user
if ($interactive) {
cli_separator();
cli_heading(get_string('databaseuser', 'install'));
if ($options['dbuser'] !== '') {
$prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
$CFG->dbuser = cli_input($prompt, $options['dbuser']);
} else {
$CFG->dbuser = $options['dbuser'];
}
// ask for db password
if ($interactive) {
cli_separator();
cli_heading(get_string('databasepass', 'install'));
do {
if ($options['dbpass'] !== '') {
$prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
$CFG->dbpass = cli_input($prompt, $options['dbpass']);
$hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbsocket'=>$options['dbsocket']));
} while ($hint_database !== '');
} else {
$CFG->dbpass = $options['dbpass'];
$hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbsocket'=>$options['dbsocket']));
if ($hint_database !== '') {
cli_error(get_string('dbconnectionerror', 'install'));
}
}
// ask for admin user name
if ($interactive) {
cli_separator();
cli_heading(get_string('cliadminusername', 'install'));
if (!empty($options['adminuser'])) {
$prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']);
} else {
$prompt = get_string('clitypevalue', 'admin');
}
do {
$options['adminuser'] = cli_input($prompt, $options['adminuser']);
} while (empty($options['adminuser']) or $options['adminuser'] === 'guest');
} else {
if (empty($options['adminuser']) or $options['adminuser'] === 'guest') {
$a = (object)array('option'=>'adminuser', 'value'=>$options['adminuser']);
cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
}
}
// ask for admin user password
if ($interactive) {
cli_separator();
cli_heading(get_string('cliadminpassword', 'install'));
$prompt = get_string('clitypevalue', 'admin');
do {
$options['adminpass'] = cli_input($prompt);
} while (empty($options['adminpass']) or $options['adminpass'] === 'admin');
} else {
if (empty($options['adminpass']) or $options['adminpass'] === 'admin') {
$a = (object)array('option'=>'adminpass', 'value'=>$options['adminpass']);
cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
}
}
if ($interactive) {
if (!$options['agree-license']) {
cli_separator();
cli_heading(get_string('copyrightnotice'));
echo "Moodle - Modular Object-Oriented Dynamic Learning Environment\n";
echo get_string('gpl3')."\n\n";
echo get_string('doyouagree')."\n";
$prompt = get_string('cliyesnoprompt', 'admin');
$input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
if ($input == get_string('clianswerno', 'admin')) {
exit(1);
}
}
} else {
if (!$options['agree-license']) {
cli_error(get_string('climustagreelicense', 'install'));
}
}
// Finally we have all info needed for config.php
$configphp = install_generate_configphp($database, $CFG);
umask(0137);
if (($fh = fopen($configfile, 'w')) !== false) {
fwrite($fh, $configphp);
fclose($fh);
}
if (!file_exists($configfile)) {
cli_error('Can not create config file.');
}
// remember selected language
$installlang = $CFG->lang;
// return back to original dir before executing setup.php which changes the dir again
chdir($olddir);
// We have config.php, it is a real php script from now on :-)
require($configfile);
// use selected language
$CFG->lang = $installlang;
$SESSION->lang = $CFG->lang;
install_cli_database($options, $interactive);
echo get_string('cliinstallfinished', 'install')."\n";
exit(0); // 0 means success