Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * This script creates config.php file and prepares database.
20
 *
21
 * This script is not intended for beginners!
22
 * Potential problems:
23
 * - su to apache account or sudo before execution
24
 * - not compatible with Windows platform
25
 *
26
 * @package    core
27
 * @subpackage cli
28
 * @copyright  2009 Petr Skoda (http://skodak.org)
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
 
32
define('CLI_SCRIPT', true);
1441 ariadna 33
define('NO_DEBUG_DISPLAY', false);
1 efrain 34
 
35
// extra execution prevention - we can not just require config.php here
36
if (isset($_SERVER['REMOTE_ADDR'])) {
37
    exit(1);
38
}
39
 
40
// Force OPcache reset if used, we do not want any stale caches
41
// when preparing test environment.
42
if (function_exists('opcache_reset')) {
43
    opcache_reset();
44
}
45
 
46
$help =
47
"Command line Moodle installer, creates config.php and initializes database.
48
Please note you must execute this script with the same uid as apache
49
or use chmod/chown after installation.
50
 
51
Site defaults may be changed via local/defaults.php.
52
 
53
Options:
54
--chmod=OCTAL-MODE    Permissions of new directories created within dataroot.
55
                      Default is 2777. You may want to change it to 2770
56
                      or 2750 or 750. See chmod man page for details.
57
--lang=CODE           Installation and default site language.
58
--wwwroot=URL         Web address for the Moodle site,
59
                      required in non-interactive mode.
60
--dataroot=DIR        Location of the moodle data folder,
61
                      must not be web accessible. Default is moodledata
62
                      in the parent directory.
63
--dbtype=TYPE         Database type. Default is mysqli
64
--dbhost=HOST         Database host. Default is localhost
65
--dbname=NAME         Database name. Default is moodle
66
--dbuser=USERNAME     Database user. Default is root
67
--dbpass=PASSWORD     Database password. Default is blank
68
--dbport=NUMBER       Use database port.
69
--dbsocket=PATH       Use database socket, 1 means default. Available for some databases only.
70
--prefix=STRING       Table prefix for above database tables. Default is mdl_
71
--fullname=STRING     The fullname of the site
72
--shortname=STRING    The shortname of the site
73
--summary=STRING      The summary to be displayed on the front page
74
--adminuser=USERNAME  Username for the moodle admin account. Default is admin
75
--adminpass=PASSWORD  Password for the moodle admin account,
76
                      required in non-interactive mode.
77
--adminemail=STRING   Email address for the moodle admin account.
78
--sitepreset=STRING   Admin site preset to be applied during the installation process.
79
--supportemail=STRING Email address for support and help.
80
--upgradekey=STRING   The upgrade key to be set in the config.php, leave empty to not set it.
81
--non-interactive     No interactive questions, installation fails if any
82
                      problem encountered.
83
--agree-license       Indicates agreement with software license,
84
                      required in non-interactive mode.
85
--allow-unstable      Install even if the version is not marked as stable yet,
86
                      required in non-interactive mode.
87
--skip-database       Stop the installation before installing the database.
88
-h, --help            Print out this help
89
 
90
Example:
91
\$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
92
"; //TODO: localize, mark as needed in install - to be translated later when everything is finished
93
 
94
 
95
// distro specific customisation
96
$distrolibfile = __DIR__.'/../../install/distrolib.php';
97
$distro = null;
98
if (file_exists($distrolibfile)) {
99
    require_once($distrolibfile);
100
    if (function_exists('distro_get_config')) {
101
        $distro = distro_get_config();
102
    }
103
}
104
 
105
// Nothing to do if config.php exists
106
$configfile = __DIR__.'/../../config.php';
107
if (file_exists($configfile)) {
108
    require($configfile);
109
    require_once($CFG->libdir.'/clilib.php');
110
    list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help'));
111
 
112
    if ($options['help']) {
113
        echo $help;
114
        echo "\n\n";
115
    }
116
 
117
    if ($DB->get_manager()->table_exists('config')) {
118
        cli_error(get_string('clialreadyinstalled', 'install'));
119
    } else {
120
        cli_error(get_string('clialreadyconfigured', 'install'));
121
    }
122
}
123
 
124
$olddir = getcwd();
125
 
126
// change directory so that includes below work properly
127
chdir(dirname($_SERVER['argv'][0]));
128
 
129
// Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
130
if (!function_exists('date_default_timezone_set') or !function_exists('date_default_timezone_get')) {
131
    fwrite(STDERR, "Timezone functions are not available.\n");
132
    exit(1);
133
}
134
date_default_timezone_set(@date_default_timezone_get());
135
 
136
// make sure PHP errors are displayed - helps with diagnosing of problems
137
@error_reporting(E_ALL);
138
@ini_set('display_errors', '1');
139
// we need a lot of memory
140
@ini_set('memory_limit', '128M');
141
 
142
/** Used by library scripts to check they are being called by Moodle */
143
define('MOODLE_INTERNAL', true);
144
 
145
// Disables all caching.
146
define('CACHE_DISABLE_ALL', true);
147
 
148
define('PHPUNIT_TEST', false);
149
 
150
define('IGNORE_COMPONENT_CACHE', true);
151
 
152
// Check that PHP is of a sufficient version as soon as possible.
153
require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
154
moodle_require_minimum_php_version();
155
 
156
// set up configuration
157
global $CFG;
158
$CFG = new stdClass();
159
$CFG->lang                 = 'en';
160
$CFG->dirroot              = dirname(dirname(__DIR__));
161
$CFG->libdir               = "$CFG->dirroot/lib";
162
$CFG->wwwroot              = "http://localhost";
163
$CFG->httpswwwroot         = $CFG->wwwroot;
164
$CFG->docroot              = 'http://docs.moodle.org';
165
$CFG->running_installer    = true;
166
$CFG->early_install_lang   = true;
167
$CFG->ostype               = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
168
$CFG->dboptions            = array();
1441 ariadna 169
$CFG->debug                = (E_ALL);
1 efrain 170
$CFG->debugdisplay         = true;
171
$CFG->debugdeveloper       = true;
172
 
173
$parts = explode('/', str_replace('\\', '/', dirname(__DIR__)));
174
$CFG->admin                = array_pop($parts);
175
 
1441 ariadna 176
// Point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
177
// the problem is that we need specific version of quickforms and hacked excel files :-(.
1 efrain 178
ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
179
 
1441 ariadna 180
// The core_component class can be used in any scripts, it does not need anything else.
1 efrain 181
require_once($CFG->libdir.'/classes/component.php');
1441 ariadna 182
 
183
// Register our classloader.
184
\core\component::register_autoloader();
185
 
1 efrain 186
require_once($CFG->libdir.'/classes/text.php');
187
require_once($CFG->libdir.'/classes/string_manager.php');
188
require_once($CFG->libdir.'/classes/string_manager_install.php');
189
require_once($CFG->libdir.'/classes/string_manager_standard.php');
190
require_once($CFG->libdir.'/installlib.php');
191
require_once($CFG->libdir.'/clilib.php');
192
require_once($CFG->libdir.'/setuplib.php');
193
require_once($CFG->libdir.'/weblib.php');
194
require_once($CFG->libdir.'/dmllib.php');
195
require_once($CFG->libdir.'/moodlelib.php');
196
require_once($CFG->libdir.'/deprecatedlib.php');
197
require_once($CFG->libdir.'/adminlib.php');
198
require_once($CFG->libdir.'/componentlib.class.php');
199
 
200
require($CFG->dirroot.'/version.php');
201
$CFG->target_release = $release;
202
 
203
\core\session\manager::init_empty_session();
204
global $SESSION;
205
global $USER;
206
 
207
global $COURSE;
208
$COURSE = new stdClass();
209
$COURSE->id = 1;
210
 
211
global $SITE;
212
$SITE = $COURSE;
213
define('SITEID', 1);
214
 
215
//Database types
216
$databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
217
                   'auroramysql' => moodle_database::get_driver_instance('auroramysql', 'native'),
218
                   'mariadb'=> moodle_database::get_driver_instance('mariadb', 'native'),
219
                   'pgsql'  => moodle_database::get_driver_instance('pgsql',  'native'),
220
                   'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
221
                  );
222
foreach ($databases as $type=>$database) {
223
    if ($database->driver_installed() !== true) {
224
        unset($databases[$type]);
225
    }
226
}
227
if (empty($databases)) {
228
    $defaultdb = '';
229
} else {
230
    reset($databases);
231
    $defaultdb = key($databases);
232
}
233
 
234
// now get cli options
235
list($options, $unrecognized) = cli_get_params(
236
    array(
237
        'chmod'             => isset($distro->directorypermissions) ? sprintf('%04o',$distro->directorypermissions) : '2777', // let distros set dir permissions
238
        'lang'              => $CFG->lang,
239
        'wwwroot'           => '',
240
        'dataroot'          => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(__DIR__))).'/moodledata'): $distro->dataroot, // initialised later after including libs or by distro
241
        'dbtype'            => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, // let distro skip dbtype selection
242
        'dbhost'            => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, // let distros set dbhost
243
        'dbname'            => 'moodle',
244
        'dbuser'            => empty($distro->dbuser) ? 'root' : $distro->dbuser, // let distros set dbuser
245
        'dbpass'            => '',
246
        'dbport'            => '',
247
        'dbsocket'          => '',
248
        'prefix'            => 'mdl_',
249
        'fullname'          => '',
250
        'shortname'         => '',
251
        'summary'           => '',
252
        'adminuser'         => 'admin',
253
        'adminpass'         => '',
254
        'adminemail'        => '',
255
        'sitepreset'        => '',
256
        'supportemail'      => '',
257
        'upgradekey'        => '',
258
        'non-interactive'   => false,
259
        'agree-license'     => false,
260
        'allow-unstable'    => false,
261
        'skip-database'     => false,
262
        'help'              => false
263
    ),
264
    array(
265
        'h' => 'help'
266
    )
267
);
268
 
269
$interactive = empty($options['non-interactive']);
270
$skipdatabase = $options['skip-database'];
271
 
272
// set up language
273
$lang = clean_param($options['lang'], PARAM_SAFEDIR);
274
$languages = get_string_manager()->get_list_of_translations();
275
if (array_key_exists($lang, $languages)) {
276
    $CFG->lang = $lang;
277
}
278
 
279
// Set up site admin preset.
280
$sitepreset = clean_param($options['sitepreset'], PARAM_RAW);
281
if (!empty($sitepreset)) {
282
    $CFG->setsitepresetduringinstall = $sitepreset;
283
}
284
 
285
if ($unrecognized) {
286
    $unrecognized = implode("\n  ", $unrecognized);
287
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
288
}
289
 
290
if ($options['help']) {
291
    echo $help;
292
    die;
293
}
294
 
295
//Print header
296
cli_logo();
297
echo PHP_EOL;
298
echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n";
299
 
300
//Fist select language
301
if ($interactive) {
302
    cli_separator();
303
    // Do not put the langs into columns because it is not compatible with RTL.
304
    $default = $CFG->lang;
305
    cli_heading(get_string('chooselanguagehead', 'install'));
306
    if (array_key_exists($default, $languages)) {
307
        echo $default.' - '.$languages[$default]."\n";
308
    }
309
    if ($default !== 'en') {
310
        echo 'en - English (en)'."\n";
311
    }
312
    echo '? - '.get_string('availablelangs', 'install')."\n";
313
    $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang);
314
    $error = '';
315
    do {
316
        echo $error;
317
        $input = cli_input($prompt, $default);
318
 
319
        if ($input === '?') {
320
            echo implode("\n", $languages)."\n";
321
            $error = "\n";
322
 
323
        } else {
324
            $input = clean_param($input, PARAM_SAFEDIR);
325
 
326
            if (!array_key_exists($input, $languages)) {
327
                $error = get_string('cliincorrectvalueretry', 'admin')."\n";
328
            } else {
329
                $error = '';
330
            }
331
        }
332
    } while ($error !== '');
333
    $CFG->lang = $input;
334
} else {
335
    // already selected and verified
336
}
337
 
338
// Set directorypermissions first
339
$chmod = octdec(clean_param($options['chmod'], PARAM_INT));
340
if ($interactive) {
341
    cli_separator();
342
    cli_heading(get_string('datarootpermission', 'install'));
343
    $prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod));
344
    $error = '';
345
    do {
346
        echo $error;
347
        $input = cli_input($prompt, decoct($chmod));
348
        $input = octdec(clean_param($input, PARAM_INT));
349
        if (empty($input)) {
350
            $error = get_string('cliincorrectvalueretry', 'admin')."\n";
351
        } else {
352
            $error = '';
353
        }
354
    } while ($error !== '');
355
    $chmod = $input;
356
 
357
} else {
358
    if (empty($chmod)) {
359
        $a = (object)array('option' => 'chmod', 'value' => decoct($chmod));
360
        cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
361
    }
362
}
363
$CFG->directorypermissions = $chmod;
364
$CFG->filepermissions      = ($CFG->directorypermissions & 0666);
365
$CFG->umaskpermissions     = (($CFG->directorypermissions & 0777) ^ 0777);
366
 
367
//We need wwwroot before we test dataroot
368
$wwwroot = clean_param($options['wwwroot'], PARAM_URL);
369
$wwwroot = trim($wwwroot, '/');
370
if ($interactive) {
371
    cli_separator();
372
    cli_heading(get_string('wwwroot', 'install'));
373
    if (strpos($wwwroot, 'http') === 0) {
374
        $prompt = get_string('clitypevaluedefault', 'admin', $wwwroot);
375
    } else {
376
        $wwwroot = null;
377
        $prompt = get_string('clitypevalue', 'admin');
378
    }
379
    $error = '';
380
    do {
381
        echo $error;
382
        $input = cli_input($prompt, $wwwroot);
383
        $input = clean_param($input, PARAM_URL);
384
        $input = trim($input, '/');
385
        if (strpos($input, 'http') !== 0) {
386
            $error = get_string('cliincorrectvalueretry', 'admin')."\n";
387
        } else {
388
            $error = '';
389
        }
390
    } while ($error !== '');
391
    $wwwroot = $input;
392
 
393
} else {
394
    if (strpos($wwwroot, 'http') !== 0) {
395
        $a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot);
396
        cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
397
    }
398
}
399
$CFG->wwwroot       = $wwwroot;
400
$CFG->httpswwwroot  = $CFG->wwwroot;
401
 
402
//We need dataroot before lang download
403
$CFG->dataroot = $options['dataroot'];
404
if ($interactive) {
405
    cli_separator();
406
    $i=0;
407
    while(is_dataroot_insecure()) {
408
        $parrent = dirname($CFG->dataroot);
409
        $i++;
410
        if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
411
            $CFG->dataroot = ''; //can not find secure location for dataroot
412
            break;
413
        }
414
        $CFG->dataroot = dirname($parrent).'/moodledata';
415
    }
416
    cli_heading(get_string('dataroot', 'install'));
417
    $error = '';
418
    do {
419
        if ($CFG->dataroot !== '') {
420
            $prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot);
421
        } else {
422
            $prompt = get_string('clitypevalue', 'admin');
423
        }
424
        echo $error;
425
        $CFG->dataroot = cli_input($prompt, $CFG->dataroot);
426
        if ($CFG->dataroot === '') {
427
            $error = get_string('cliincorrectvalueretry', 'admin')."\n";
428
        } else if (is_dataroot_insecure()) {
429
            $CFG->dataroot = '';
430
            $error = get_string('pathsunsecuredataroot', 'install')."\n";
431
        } else {
432
            if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
433
                $error = '';
434
            } else {
435
                $a = (object)array('dataroot' => $CFG->dataroot);
436
                $error = get_string('pathserrcreatedataroot', 'install', $a)."\n";
437
            }
438
        }
439
 
440
    } while ($error !== '');
441
 
442
} else {
443
    if (is_dataroot_insecure()) {
444
        cli_error(get_string('pathsunsecuredataroot', 'install'));
445
    }
446
    if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
447
        $a = (object)array('dataroot' => $CFG->dataroot);
448
        cli_error(get_string('pathserrcreatedataroot', 'install', $a));
449
    }
450
}
451
$CFG->tempdir       = $CFG->dataroot.'/temp';
452
$CFG->backuptempdir = $CFG->tempdir.'/backup';
453
$CFG->cachedir      = $CFG->dataroot.'/cache';
454
$CFG->localcachedir = $CFG->dataroot.'/localcache';
455
 
456
// download required lang packs
457
if ($CFG->lang !== 'en') {
458
    $installer = new lang_installer($CFG->lang);
459
    $results = $installer->run();
460
    foreach ($results as $langcode => $langstatus) {
461
        if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
462
            $a       = new stdClass();
463
            $a->url  = $installer->lang_pack_url($langcode);
464
            $a->dest = $CFG->dataroot.'/lang';
465
            cli_problem(get_string('remotedownloaderror', 'error', $a));
466
        }
467
    }
468
}
469
 
470
// switch the string_manager instance to stop using install/lang/
471
$CFG->early_install_lang = false;
472
$CFG->langotherroot      = $CFG->dataroot.'/lang';
473
$CFG->langlocalroot      = $CFG->dataroot.'/lang';
474
get_string_manager(true);
475
 
476
// make sure we are installing stable release or require a confirmation
477
if (isset($maturity)) {
478
    if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
479
        $maturitylevel = get_string('maturity'.$maturity, 'admin');
480
 
481
        if ($interactive) {
482
            cli_separator();
483
            cli_heading(get_string('notice'));
484
            echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
485
            echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
486
            echo get_string('continue') . PHP_EOL;
487
            $prompt = get_string('cliyesnoprompt', 'admin');
488
            $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
489
            if ($input == get_string('clianswerno', 'admin')) {
490
                exit(1);
491
            }
492
        } else {
493
            cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
494
            cli_error(get_string('maturityallowunstable', 'admin'));
495
        }
496
    }
497
}
498
 
499
// ask for db type - show only drivers available
500
if ($interactive) {
501
    $options['dbtype'] = strtolower($options['dbtype']);
502
    cli_separator();
503
    cli_heading(get_string('databasetypehead', 'install'));
504
    foreach ($databases as $type=>$database) {
505
        echo " $type \n";
506
    }
507
    if (!empty($databases[$options['dbtype']])) {
508
        $prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']);
509
    } else {
510
        $prompt = get_string('clitypevalue', 'admin');
511
    }
512
    $CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases));
513
 
514
} else {
515
    if (empty($databases[$options['dbtype']])) {
516
        $a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']);
517
        cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
518
    }
519
    $CFG->dbtype = $options['dbtype'];
520
}
521
$database = $databases[$CFG->dbtype];
522
 
523
 
524
// We cannot do any validation until all DB connection data is provided.
525
$hintdatabase = '';
526
do {
527
    echo $hintdatabase;
528
 
529
    // Ask for db host.
530
    if ($interactive) {
531
        cli_separator();
532
        cli_heading(get_string('databasehost', 'install'));
533
        if ($options['dbhost'] !== '') {
534
            $prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']);
535
        } else {
536
            $prompt = get_string('clitypevalue', 'admin');
537
        }
538
        $CFG->dbhost = cli_input($prompt, $options['dbhost']);
539
 
540
    } else {
541
        $CFG->dbhost = $options['dbhost'];
542
    }
543
 
544
    // Ask for db name.
545
    if ($interactive) {
546
        cli_separator();
547
        cli_heading(get_string('databasename', 'install'));
548
        if ($options['dbname'] !== '') {
549
            $prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']);
550
        } else {
551
            $prompt = get_string('clitypevalue', 'admin');
552
        }
553
        $CFG->dbname = cli_input($prompt, $options['dbname']);
554
 
555
    } else {
556
        $CFG->dbname = $options['dbname'];
557
    }
558
 
559
    // Ask for db prefix.
560
    if ($interactive) {
561
        cli_separator();
562
        cli_heading(get_string('dbprefix', 'install'));
563
        if ($options['prefix'] !== '') {
564
            $prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']);
565
        } else {
566
            $prompt = get_string('clitypevalue', 'admin');
567
        }
568
        $CFG->prefix = cli_input($prompt, $options['prefix']);
569
 
570
    } else {
571
        $CFG->prefix = $options['prefix'];
572
    }
573
 
574
    // Ask for db port.
575
    if ($interactive) {
576
        cli_separator();
577
        cli_heading(get_string('databaseport', 'install'));
578
        $prompt = get_string('clitypevaluedefault', 'admin', $options['dbport']);
579
        $CFG->dboptions['dbport'] = (int) cli_input($prompt, $options['dbport']);
580
 
581
    } else {
582
        $CFG->dboptions['dbport'] = (int) $options['dbport'];
583
    }
584
    if ($CFG->dboptions['dbport'] <= 0) {
585
        $CFG->dboptions['dbport'] = '';
586
    }
587
 
588
    // Ask for db socket.
589
    if ($CFG->ostype === 'WINDOWS') {
590
        $CFG->dboptions['dbsocket'] = '';
591
 
592
    } else if ($interactive and empty($CFG->dboptions['dbport'])) {
593
        cli_separator();
594
        cli_heading(get_string('databasesocket', 'install'));
595
        $prompt = get_string('clitypevaluedefault', 'admin', $options['dbsocket']);
596
        $CFG->dboptions['dbsocket'] = cli_input($prompt, $options['dbsocket']);
597
 
598
    } else {
599
        $CFG->dboptions['dbsocket'] = $options['dbsocket'];
600
    }
601
 
602
    // Ask for db user.
603
    if ($interactive) {
604
        cli_separator();
605
        cli_heading(get_string('databaseuser', 'install'));
606
        if ($options['dbuser'] !== '') {
607
            $prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']);
608
        } else {
609
            $prompt = get_string('clitypevalue', 'admin');
610
        }
611
        $CFG->dbuser = cli_input($prompt, $options['dbuser']);
612
 
613
    } else {
614
        $CFG->dbuser = $options['dbuser'];
615
    }
616
 
617
    // Ask for db password.
618
    if ($interactive) {
619
        cli_separator();
620
        cli_heading(get_string('databasepass', 'install'));
621
 
622
        if ($options['dbpass'] !== '') {
623
            $prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']);
624
        } else {
625
            $prompt = get_string('clitypevalue', 'admin');
626
        }
627
 
628
        $CFG->dbpass = cli_input($prompt, $options['dbpass']);
629
        if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation.
630
            $distro = distro_pre_create_db($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix,
631
                    array('dbpersist' => 0, 'dbport' => $CFG->dboptions['dbport'], 'dbsocket' => $CFG->dboptions['dbsocket']),
632
                    $distro);
633
        }
634
        $hintdatabase = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix,
635
                array('dbpersist' => 0, 'dbport' => $CFG->dboptions['dbport'], 'dbsocket' => $CFG->dboptions['dbsocket']));
636
 
637
    } else {
638
        $CFG->dbpass = $options['dbpass'];
639
        $hintdatabase = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix,
640
                array('dbpersist' => 0, 'dbport' => $CFG->dboptions['dbport'], 'dbsocket' => $CFG->dboptions['dbsocket']));
641
        if ($hintdatabase !== '') {
642
            cli_error(get_string('dbconnectionerror', 'install'));
643
        }
644
    }
645
} while ($hintdatabase !== '');
646
 
647
// If --skip-database option is provided, we do not need to ask for site fullname, shortname, adminuser, adminpass, adminemail.
648
// These fields will be requested during the database install part.
649
if (!$skipdatabase) {
650
    // Ask for fullname.
651
    if ($interactive) {
652
        cli_separator();
653
        cli_heading(get_string('fullsitename', 'moodle'));
654
 
655
        if ($options['fullname'] !== '') {
656
            $prompt = get_string('clitypevaluedefault', 'admin', $options['fullname']);
657
        } else {
658
            $prompt = get_string('clitypevalue', 'admin');
659
        }
660
 
661
        do {
662
            $options['fullname'] = cli_input($prompt, $options['fullname']);
663
        } while (empty($options['fullname']));
664
    } else {
665
        if (empty($options['fullname'])) {
666
            $a = (object)['option' => 'fullname', 'value' => $options['fullname']];
667
            cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
668
        }
669
    }
670
 
671
    // Ask for shortname.
672
    if ($interactive) {
673
        cli_separator();
674
        cli_heading(get_string('shortsitename', 'moodle'));
675
 
676
        if ($options['shortname'] !== '') {
677
            $prompt = get_string('clitypevaluedefault', 'admin', $options['shortname']);
678
        } else {
679
            $prompt = get_string('clitypevalue', 'admin');
680
        }
681
 
682
        do {
683
            $options['shortname'] = cli_input($prompt, $options['shortname']);
684
        } while (empty($options['shortname']));
685
    } else {
686
        if (empty($options['shortname'])) {
687
            $a = (object)['option' => 'shortname', 'value' => $options['shortname']];
688
            cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
689
        }
690
    }
691
 
692
    // Ask for admin user name.
693
    if ($interactive) {
694
        cli_separator();
695
        cli_heading(get_string('cliadminusername', 'install'));
696
        if (!empty($options['adminuser'])) {
697
            $prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']);
698
        } else {
699
            $prompt = get_string('clitypevalue', 'admin');
700
        }
701
        do {
702
            $options['adminuser'] = cli_input($prompt, $options['adminuser']);
703
        } while (empty($options['adminuser']) or $options['adminuser'] === 'guest');
704
    } else {
705
        if ((empty($options['adminuser']) || $options['adminuser'] === 'guest')) {
706
            $a = (object)['option' => 'adminuser', 'value' => $options['adminuser']];
707
            cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
708
        }
709
    }
710
 
711
    // Ask for admin user password.
712
    if ($interactive) {
713
        cli_separator();
714
        cli_heading(get_string('cliadminpassword', 'install'));
715
        $prompt = get_string('clitypevalue', 'admin');
716
        do {
717
            $options['adminpass'] = cli_input($prompt);
718
        } while (empty($options['adminpass']) or $options['adminpass'] === 'admin');
719
    } else {
720
        if ((empty($options['adminpass']) or $options['adminpass'] === 'admin')) {
721
            $a = (object)['option' => 'adminpass', 'value' => $options['adminpass']];
722
            cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
723
        }
724
    }
725
 
726
    // Ask for the admin email address.
727
    if ($interactive) {
728
        cli_separator();
729
        cli_heading(get_string('cliadminemail', 'install'));
730
        $prompt = get_string('clitypevaluedefault', 'admin', $options['adminemail']);
731
        $options['adminemail'] = cli_input($prompt, $options['adminemail']);
732
    }
733
 
734
    // Validate that the address provided was an e-mail address.
735
    if (!empty($options['adminemail']) && !validate_email($options['adminemail'])) {
736
        $a = (object)['option' => 'adminemail', 'value' => $options['adminemail']];
737
        cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
738
    }
739
 
740
    // Ask for the support email address.
741
    if ($interactive) {
742
        cli_separator();
743
        cli_heading(get_string('clisupportemail', 'install'));
744
        $prompt = get_string('clitypevaluedefault', 'admin', $options['supportemail']);
745
        $options['supportemail'] = cli_input($prompt, $options['supportemail']);
746
    }
747
 
748
    // Validate that the support email address provided is valid.
749
    if (!empty($options['supportemail']) && !validate_email($options['supportemail'])) {
750
        $a = (object)['option' => 'supportemail', 'value' => $options['supportemail']];
751
        cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
752
    }
753
}
754
 
755
// Ask for the upgrade key.
756
if ($interactive) {
757
    cli_separator();
758
    cli_heading(get_string('upgradekeyset', 'admin'));
759
    if ($options['upgradekey'] !== '') {
760
        $prompt = get_string('clitypevaluedefault', 'admin', $options['upgradekey']);
761
        $options['upgradekey'] = cli_input($prompt, $options['upgradekey']);
762
    } else {
763
        $prompt = get_string('clitypevalue', 'admin');
764
        $options['upgradekey'] = cli_input($prompt);
765
    }
766
}
767
 
768
// Set the upgrade key if it was provided.
769
if ($options['upgradekey'] !== '') {
770
    $CFG->upgradekey = $options['upgradekey'];
771
}
772
 
773
// The user does not also need to pass agree-license when --skip-database is provided as the user will need to accept
774
// the license again in the database install part.
775
if (!$skipdatabase) {
776
    if ($interactive) {
777
        if (!$options['agree-license']) {
778
            cli_separator();
779
            cli_heading(get_string('copyrightnotice'));
780
            echo "Moodle  - Modular Object-Oriented Dynamic Learning Environment\n";
781
            echo get_string('gpl3')."\n\n";
782
            echo get_string('doyouagree')."\n";
783
            $prompt = get_string('cliyesnoprompt', 'admin');
784
            $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
785
            if ($input == get_string('clianswerno', 'admin')) {
786
                exit(1);
787
            }
788
        }
789
    } else {
790
        if (!$options['agree-license'] && !$skipdatabase) {
791
            cli_error(get_string('climustagreelicense', 'install'));
792
        }
793
    }
794
}
795
 
796
// Finally we have all info needed for config.php
797
$configphp = install_generate_configphp($database, $CFG);
798
umask(0137);
799
if (($fh = fopen($configfile, 'w')) !== false) {
800
    fwrite($fh, $configphp);
801
    fclose($fh);
802
}
803
 
804
if (!file_exists($configfile)) {
805
    cli_error('Can not create config file.');
806
}
807
 
808
// remember selected language
809
$installlang = $CFG->lang;
810
// return back to original dir before executing setup.php which changes the dir again
811
chdir($olddir);
812
// We have config.php, it is a real php script from now on :-)
813
require($configfile);
814
 
815
// use selected language
816
$CFG->lang = $installlang;
817
$SESSION->lang = $CFG->lang;
818
 
819
require("$CFG->dirroot/version.php");
820
 
821
// Test environment first.
822
require_once($CFG->libdir . '/environmentlib.php');
823
list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
824
if (!$envstatus) {
825
    $errors = environment_get_errors($environment_results);
826
    cli_heading(get_string('environment', 'admin'));
827
    foreach ($errors as $error) {
828
        list($info, $report) = $error;
829
        echo "!! $info !!\n$report\n\n";
830
    }
831
    exit(1);
832
}
833
 
834
// Test plugin dependencies.
835
$failed = array();
836
if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
837
    cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
838
    cli_error(get_string('pluginschecktodo', 'admin'));
839
}
840
 
841
if (!$skipdatabase) {
842
    install_cli_database($options, $interactive);
843
    // This needs to happen at the end to ensure it occurs after all caches
844
    // have been purged for the last time.
845
    // This will build a cached version of the current theme for the user
846
    // to immediately start browsing the site.
847
    require_once($CFG->libdir.'/upgradelib.php');
848
    upgrade_themes();
849
} else {
850
    echo get_string('cliskipdatabase', 'install')."\n";
851
}
852
 
853
echo get_string('cliinstallfinished', 'install')."\n";
854
exit(0); // 0 means success