PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/cli/install.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 757 lines | 603 code | 81 blank | 73 comment | 132 complexity | 87ca4c65c2777642791330bb8ba0b222 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This script creates config.php file and prepares database.
  18. *
  19. * This script is not intended for beginners!
  20. * Potential problems:
  21. * - su to apache account or sudo before execution
  22. * - not compatible with Windows platform
  23. *
  24. * @package core
  25. * @subpackage cli
  26. * @copyright 2009 Petr Skoda (http://skodak.org)
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. define('CLI_SCRIPT', true);
  30. // extra execution prevention - we can not just require config.php here
  31. if (isset($_SERVER['REMOTE_ADDR'])) {
  32. exit(1);
  33. }
  34. // Force OPcache reset if used, we do not want any stale caches
  35. // when preparing test environment.
  36. if (function_exists('opcache_reset')) {
  37. opcache_reset();
  38. }
  39. $help =
  40. "Command line Moodle installer, creates config.php and initializes database.
  41. Please note you must execute this script with the same uid as apache
  42. or use chmod/chown after installation.
  43. Site defaults may be changed via local/defaults.php.
  44. Options:
  45. --chmod=OCTAL-MODE Permissions of new directories created within dataroot.
  46. Default is 2777. You may want to change it to 2770
  47. or 2750 or 750. See chmod man page for details.
  48. --lang=CODE Installation and default site language.
  49. --wwwroot=URL Web address for the Moodle site,
  50. required in non-interactive mode.
  51. --dataroot=DIR Location of the moodle data folder,
  52. must not be web accessible. Default is moodledata
  53. in the parent directory.
  54. --dbtype=TYPE Database type. Default is mysqli
  55. --dbhost=HOST Database host. Default is localhost
  56. --dbname=NAME Database name. Default is moodle
  57. --dbuser=USERNAME Database user. Default is root
  58. --dbpass=PASSWORD Database password. Default is blank
  59. --dbport=NUMBER Use database port.
  60. --dbsocket=PATH Use database socket, 1 means default. Available for some databases only.
  61. --prefix=STRING Table prefix for above database tables. Default is mdl_
  62. --fullname=STRING The fullname of the site
  63. --shortname=STRING The shortname of the site
  64. --adminuser=USERNAME Username for the moodle admin account. Default is admin
  65. --adminpass=PASSWORD Password for the moodle admin account,
  66. required in non-interactive mode.
  67. --non-interactive No interactive questions, installation fails if any
  68. problem encountered.
  69. --agree-license Indicates agreement with software license,
  70. required in non-interactive mode.
  71. --allow-unstable Install even if the version is not marked as stable yet,
  72. required in non-interactive mode.
  73. -h, --help Print out this help
  74. Example:
  75. \$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
  76. "; //TODO: localize, mark as needed in install - to be translated later when everything is finished
  77. // distro specific customisation
  78. $distrolibfile = dirname(dirname(dirname(__FILE__))).'/install/distrolib.php';
  79. $distro = null;
  80. if (file_exists($distrolibfile)) {
  81. require_once($distrolibfile);
  82. if (function_exists('distro_get_config')) {
  83. $distro = distro_get_config();
  84. }
  85. }
  86. // Nothing to do if config.php exists
  87. $configfile = dirname(dirname(dirname(__FILE__))).'/config.php';
  88. if (file_exists($configfile)) {
  89. require($configfile);
  90. require_once($CFG->libdir.'/clilib.php');
  91. list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help'));
  92. if ($options['help']) {
  93. echo $help;
  94. echo "\n\n";
  95. }
  96. if ($DB->get_manager()->table_exists('config')) {
  97. cli_error(get_string('clialreadyinstalled', 'install'));
  98. } else {
  99. cli_error(get_string('clialreadyconfigured', 'install'));
  100. }
  101. }
  102. $olddir = getcwd();
  103. // change directory so that includes below work properly
  104. chdir(dirname($_SERVER['argv'][0]));
  105. // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
  106. // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
  107. if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
  108. @date_default_timezone_set(@date_default_timezone_get());
  109. }
  110. // make sure PHP errors are displayed - helps with diagnosing of problems
  111. @error_reporting(E_ALL);
  112. @ini_set('display_errors', '1');
  113. // we need a lot of memory
  114. @ini_set('memory_limit', '128M');
  115. /** Used by library scripts to check they are being called by Moodle */
  116. define('MOODLE_INTERNAL', true);
  117. // Disables all caching.
  118. define('CACHE_DISABLE_ALL', true);
  119. define('PHPUNIT_TEST', false);
  120. define('IGNORE_COMPONENT_CACHE', true);
  121. // Check that PHP is of a sufficient version
  122. if (version_compare(phpversion(), "5.3.3") < 0) {
  123. $phpversion = phpversion();
  124. // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
  125. fwrite(STDERR, "Moodle 2.5 or later requires at least PHP 5.3.3 (currently using version $phpversion).\n");
  126. fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
  127. exit(1);
  128. }
  129. // set up configuration
  130. global $CFG;
  131. $CFG = new stdClass();
  132. $CFG->lang = 'en';
  133. $CFG->dirroot = dirname(dirname(dirname(__FILE__)));
  134. $CFG->libdir = "$CFG->dirroot/lib";
  135. $CFG->wwwroot = "http://localhost";
  136. $CFG->httpswwwroot = $CFG->wwwroot;
  137. $CFG->docroot = 'http://docs.moodle.org';
  138. $CFG->running_installer = true;
  139. $CFG->early_install_lang = true;
  140. $CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
  141. $CFG->dboptions = array();
  142. $CFG->debug = (E_ALL | E_STRICT);
  143. $CFG->debugdisplay = true;
  144. $CFG->debugdeveloper = true;
  145. $parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__))));
  146. $CFG->admin = array_pop($parts);
  147. //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
  148. //the problem is that we need specific version of quickforms and hacked excel files :-(
  149. ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
  150. require_once($CFG->libdir.'/classes/component.php');
  151. require_once($CFG->libdir.'/classes/text.php');
  152. require_once($CFG->libdir.'/classes/string_manager.php');
  153. require_once($CFG->libdir.'/classes/string_manager_install.php');
  154. require_once($CFG->libdir.'/classes/string_manager_standard.php');
  155. require_once($CFG->libdir.'/installlib.php');
  156. require_once($CFG->libdir.'/clilib.php');
  157. require_once($CFG->libdir.'/setuplib.php');
  158. require_once($CFG->libdir.'/weblib.php');
  159. require_once($CFG->libdir.'/dmllib.php');
  160. require_once($CFG->libdir.'/moodlelib.php');
  161. require_once($CFG->libdir.'/deprecatedlib.php');
  162. require_once($CFG->libdir.'/adminlib.php');
  163. require_once($CFG->libdir.'/componentlib.class.php');
  164. require_once($CFG->dirroot.'/cache/lib.php');
  165. // Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
  166. // Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes.
  167. if (defined('COMPONENT_CLASSLOADER')) {
  168. spl_autoload_register(COMPONENT_CLASSLOADER);
  169. } else {
  170. spl_autoload_register('core_component::classloader');
  171. }
  172. require($CFG->dirroot.'/version.php');
  173. $CFG->target_release = $release;
  174. \core\session\manager::init_empty_session();
  175. global $SESSION;
  176. global $USER;
  177. global $COURSE;
  178. $COURSE = new stdClass();
  179. $COURSE->id = 1;
  180. global $SITE;
  181. $SITE = $COURSE;
  182. define('SITEID', 1);
  183. //Database types
  184. $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
  185. 'mariadb'=> moodle_database::get_driver_instance('mariadb', 'native'),
  186. 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
  187. 'oci' => moodle_database::get_driver_instance('oci', 'native'),
  188. 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
  189. 'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
  190. );
  191. foreach ($databases as $type=>$database) {
  192. if ($database->driver_installed() !== true) {
  193. unset($databases[$type]);
  194. }
  195. }
  196. if (empty($databases)) {
  197. $defaultdb = '';
  198. } else {
  199. reset($databases);
  200. $defaultdb = key($databases);
  201. }
  202. // now get cli options
  203. list($options, $unrecognized) = cli_get_params(
  204. array(
  205. 'chmod' => isset($distro->directorypermissions) ? sprintf('%04o',$distro->directorypermissions) : '2777', // let distros set dir permissions
  206. 'lang' => $CFG->lang,
  207. 'wwwroot' => '',
  208. 'dataroot' => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__)))).'/moodledata'): $distro->dataroot, // initialised later after including libs or by distro
  209. 'dbtype' => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, // let distro skip dbtype selection
  210. 'dbhost' => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, // let distros set dbhost
  211. 'dbname' => 'moodle',
  212. 'dbuser' => empty($distro->dbuser) ? 'root' : $distro->dbuser, // let distros set dbuser
  213. 'dbpass' => '',
  214. 'dbport' => '',
  215. 'dbsocket' => '',
  216. 'prefix' => 'mdl_',
  217. 'fullname' => '',
  218. 'shortname' => '',
  219. 'adminuser' => 'admin',
  220. 'adminpass' => '',
  221. 'non-interactive' => false,
  222. 'agree-license' => false,
  223. 'allow-unstable' => false,
  224. 'help' => false
  225. ),
  226. array(
  227. 'h' => 'help'
  228. )
  229. );
  230. $interactive = empty($options['non-interactive']);
  231. // set up language
  232. $lang = clean_param($options['lang'], PARAM_SAFEDIR);
  233. if (file_exists($CFG->dirroot.'/install/lang/'.$lang)) {
  234. $CFG->lang = $lang;
  235. }
  236. if ($unrecognized) {
  237. $unrecognized = implode("\n ", $unrecognized);
  238. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  239. }
  240. if ($options['help']) {
  241. echo $help;
  242. die;
  243. }
  244. //Print header
  245. echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n";
  246. //Fist select language
  247. if ($interactive) {
  248. cli_separator();
  249. $languages = get_string_manager()->get_list_of_translations();
  250. // Do not put the langs into columns because it is not compatible with RTL.
  251. $langlist = implode("\n", $languages);
  252. $default = $CFG->lang;
  253. cli_heading(get_string('availablelangs', 'install'));
  254. echo $langlist."\n";
  255. $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang);
  256. $error = '';
  257. do {
  258. echo $error;
  259. $input = cli_input($prompt, $default);
  260. $input = clean_param($input, PARAM_SAFEDIR);
  261. if (!file_exists($CFG->dirroot.'/install/lang/'.$input)) {
  262. $error = get_string('cliincorrectvalueretry', 'admin')."\n";
  263. } else {
  264. $error = '';
  265. }
  266. } while ($error !== '');
  267. $CFG->lang = $input;
  268. } else {
  269. // already selected and verified
  270. }
  271. // Set directorypermissions first
  272. $chmod = octdec(clean_param($options['chmod'], PARAM_INT));
  273. if ($interactive) {
  274. cli_separator();
  275. cli_heading(get_string('datarootpermission', 'install'));
  276. $prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod));
  277. $error = '';
  278. do {
  279. echo $error;
  280. $input = cli_input($prompt, decoct($chmod));
  281. $input = octdec(clean_param($input, PARAM_INT));
  282. if (empty($input)) {
  283. $error = get_string('cliincorrectvalueretry', 'admin')."\n";
  284. } else {
  285. $error = '';
  286. }
  287. } while ($error !== '');
  288. $chmod = $input;
  289. } else {
  290. if (empty($chmod)) {
  291. $a = (object)array('option' => 'chmod', 'value' => decoct($chmod));
  292. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  293. }
  294. }
  295. $CFG->directorypermissions = $chmod;
  296. $CFG->filepermissions = ($CFG->directorypermissions & 0666);
  297. $CFG->umaskpermissions = (($CFG->directorypermissions & 0777) ^ 0777);
  298. //We need wwwroot before we test dataroot
  299. $wwwroot = clean_param($options['wwwroot'], PARAM_URL);
  300. $wwwroot = trim($wwwroot, '/');
  301. if ($interactive) {
  302. cli_separator();
  303. cli_heading(get_string('wwwroot', 'install'));
  304. if (strpos($wwwroot, 'http') === 0) {
  305. $prompt = get_string('clitypevaluedefault', 'admin', $wwwroot);
  306. } else {
  307. $wwwroot = null;
  308. $prompt = get_string('clitypevalue', 'admin');
  309. }
  310. $error = '';
  311. do {
  312. echo $error;
  313. $input = cli_input($prompt, $wwwroot);
  314. $input = clean_param($input, PARAM_URL);
  315. $input = trim($input, '/');
  316. if (strpos($input, 'http') !== 0) {
  317. $error = get_string('cliincorrectvalueretry', 'admin')."\n";
  318. } else {
  319. $error = '';
  320. }
  321. } while ($error !== '');
  322. $wwwroot = $input;
  323. } else {
  324. if (strpos($wwwroot, 'http') !== 0) {
  325. $a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot);
  326. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  327. }
  328. }
  329. $CFG->wwwroot = $wwwroot;
  330. $CFG->httpswwwroot = $CFG->wwwroot;
  331. //We need dataroot before lang download
  332. $CFG->dataroot = $options['dataroot'];
  333. if ($interactive) {
  334. cli_separator();
  335. $i=0;
  336. while(is_dataroot_insecure()) {
  337. $parrent = dirname($CFG->dataroot);
  338. $i++;
  339. if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
  340. $CFG->dataroot = ''; //can not find secure location for dataroot
  341. break;
  342. }
  343. $CFG->dataroot = dirname($parrent).'/moodledata';
  344. }
  345. cli_heading(get_string('dataroot', 'install'));
  346. $error = '';
  347. do {
  348. if ($CFG->dataroot !== '') {
  349. $prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot);
  350. } else {
  351. $prompt = get_string('clitypevalue', 'admin');
  352. }
  353. echo $error;
  354. $CFG->dataroot = cli_input($prompt, $CFG->dataroot);
  355. if ($CFG->dataroot === '') {
  356. $error = get_string('cliincorrectvalueretry', 'admin')."\n";
  357. } else if (is_dataroot_insecure()) {
  358. $CFG->dataroot = '';
  359. $error = get_string('pathsunsecuredataroot', 'install')."\n";
  360. } else {
  361. if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
  362. $error = '';
  363. } else {
  364. $a = (object)array('dataroot' => $CFG->dataroot);
  365. $error = get_string('pathserrcreatedataroot', 'install', $a)."\n";
  366. }
  367. }
  368. } while ($error !== '');
  369. } else {
  370. if (is_dataroot_insecure()) {
  371. cli_error(get_string('pathsunsecuredataroot', 'install'));
  372. }
  373. if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
  374. $a = (object)array('dataroot' => $CFG->dataroot);
  375. cli_error(get_string('pathserrcreatedataroot', 'install', $a));
  376. }
  377. }
  378. $CFG->tempdir = $CFG->dataroot.'/temp';
  379. $CFG->cachedir = $CFG->dataroot.'/cache';
  380. $CFG->localcachedir = $CFG->dataroot.'/localcache';
  381. // download required lang packs
  382. if ($CFG->lang !== 'en') {
  383. $installer = new lang_installer($CFG->lang);
  384. $results = $installer->run();
  385. foreach ($results as $langcode => $langstatus) {
  386. if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
  387. $a = new stdClass();
  388. $a->url = $installer->lang_pack_url($langcode);
  389. $a->dest = $CFG->dataroot.'/lang';
  390. cli_problem(get_string('remotedownloaderror', 'error', $a));
  391. }
  392. }
  393. }
  394. // switch the string_manager instance to stop using install/lang/
  395. $CFG->early_install_lang = false;
  396. $CFG->langotherroot = $CFG->dataroot.'/lang';
  397. $CFG->langlocalroot = $CFG->dataroot.'/lang';
  398. get_string_manager(true);
  399. // make sure we are installing stable release or require a confirmation
  400. if (isset($maturity)) {
  401. if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
  402. $maturitylevel = get_string('maturity'.$maturity, 'admin');
  403. if ($interactive) {
  404. cli_separator();
  405. cli_heading(get_string('notice'));
  406. echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
  407. echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
  408. echo get_string('continue') . PHP_EOL;
  409. $prompt = get_string('cliyesnoprompt', 'admin');
  410. $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
  411. if ($input == get_string('clianswerno', 'admin')) {
  412. exit(1);
  413. }
  414. } else {
  415. cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
  416. cli_error(get_string('maturityallowunstable', 'admin'));
  417. }
  418. }
  419. }
  420. // ask for db type - show only drivers available
  421. if ($interactive) {
  422. $options['dbtype'] = strtolower($options['dbtype']);
  423. cli_separator();
  424. cli_heading(get_string('databasetypehead', 'install'));
  425. foreach ($databases as $type=>$database) {
  426. echo " $type \n";
  427. }
  428. if (!empty($databases[$options['dbtype']])) {
  429. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']);
  430. } else {
  431. $prompt = get_string('clitypevalue', 'admin');
  432. }
  433. $CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases));
  434. } else {
  435. if (empty($databases[$options['dbtype']])) {
  436. $a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']);
  437. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  438. }
  439. $CFG->dbtype = $options['dbtype'];
  440. }
  441. $database = $databases[$CFG->dbtype];
  442. // ask for db host
  443. if ($interactive) {
  444. cli_separator();
  445. cli_heading(get_string('databasehost', 'install'));
  446. if ($options['dbhost'] !== '') {
  447. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']);
  448. } else {
  449. $prompt = get_string('clitypevalue', 'admin');
  450. }
  451. $CFG->dbhost = cli_input($prompt, $options['dbhost']);
  452. } else {
  453. $CFG->dbhost = $options['dbhost'];
  454. }
  455. // ask for db name
  456. if ($interactive) {
  457. cli_separator();
  458. cli_heading(get_string('databasename', 'install'));
  459. if ($options['dbname'] !== '') {
  460. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']);
  461. } else {
  462. $prompt = get_string('clitypevalue', 'admin');
  463. }
  464. $CFG->dbname = cli_input($prompt, $options['dbname']);
  465. } else {
  466. $CFG->dbname = $options['dbname'];
  467. }
  468. // ask for db prefix
  469. if ($interactive) {
  470. cli_separator();
  471. cli_heading(get_string('dbprefix', 'install'));
  472. //TODO: solve somehow the prefix trouble for oci
  473. if ($options['prefix'] !== '') {
  474. $prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']);
  475. } else {
  476. $prompt = get_string('clitypevalue', 'admin');
  477. }
  478. $CFG->prefix = cli_input($prompt, $options['prefix']);
  479. } else {
  480. $CFG->prefix = $options['prefix'];
  481. }
  482. // ask for db port
  483. if ($interactive) {
  484. cli_separator();
  485. cli_heading(get_string('databaseport', 'install'));
  486. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbport']);
  487. $CFG->dboptions['dbport'] = (int)cli_input($prompt, $options['dbport']);
  488. } else {
  489. $CFG->dboptions['dbport'] = (int)$options['dbport'];
  490. }
  491. if ($CFG->dboptions['dbport'] <= 0) {
  492. $CFG->dboptions['dbport'] = '';
  493. }
  494. // ask for db socket
  495. if ($CFG->ostype === 'WINDOWS') {
  496. $CFG->dboptions['dbsocket'] = '';
  497. } else if ($interactive and empty($CFG->dboptions['dbport'])) {
  498. cli_separator();
  499. cli_heading(get_string('databasesocket', 'install'));
  500. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbsocket']);
  501. $CFG->dboptions['dbsocket'] = cli_input($prompt, $options['dbsocket']);
  502. } else {
  503. $CFG->dboptions['dbsocket'] = $options['dbsocket'];
  504. }
  505. // ask for db user
  506. if ($interactive) {
  507. cli_separator();
  508. cli_heading(get_string('databaseuser', 'install'));
  509. if ($options['dbuser'] !== '') {
  510. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']);
  511. } else {
  512. $prompt = get_string('clitypevalue', 'admin');
  513. }
  514. $CFG->dbuser = cli_input($prompt, $options['dbuser']);
  515. } else {
  516. $CFG->dbuser = $options['dbuser'];
  517. }
  518. // ask for db password
  519. if ($interactive) {
  520. cli_separator();
  521. cli_heading(get_string('databasepass', 'install'));
  522. do {
  523. if ($options['dbpass'] !== '') {
  524. $prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']);
  525. } else {
  526. $prompt = get_string('clitypevalue', 'admin');
  527. }
  528. $CFG->dbpass = cli_input($prompt, $options['dbpass']);
  529. if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
  530. $distro = distro_pre_create_db($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']), $distro);
  531. }
  532. $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']));
  533. } while ($hint_database !== '');
  534. } else {
  535. $CFG->dbpass = $options['dbpass'];
  536. $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']));
  537. if ($hint_database !== '') {
  538. cli_error(get_string('dbconnectionerror', 'install'));
  539. }
  540. }
  541. // ask for fullname
  542. if ($interactive) {
  543. cli_separator();
  544. cli_heading(get_string('fullsitename', 'moodle'));
  545. if ($options['fullname'] !== '') {
  546. $prompt = get_string('clitypevaluedefault', 'admin', $options['fullname']);
  547. } else {
  548. $prompt = get_string('clitypevalue', 'admin');
  549. }
  550. do {
  551. $options['fullname'] = cli_input($prompt, $options['fullname']);
  552. } while (empty($options['fullname']));
  553. } else {
  554. if (empty($options['fullname'])) {
  555. $a = (object)array('option'=>'fullname', 'value'=>$options['fullname']);
  556. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  557. }
  558. }
  559. // ask for shortname
  560. if ($interactive) {
  561. cli_separator();
  562. cli_heading(get_string('shortsitename', 'moodle'));
  563. if ($options['shortname'] !== '') {
  564. $prompt = get_string('clitypevaluedefault', 'admin', $options['shortname']);
  565. } else {
  566. $prompt = get_string('clitypevalue', 'admin');
  567. }
  568. do {
  569. $options['shortname'] = cli_input($prompt, $options['shortname']);
  570. } while (empty($options['shortname']));
  571. } else {
  572. if (empty($options['shortname'])) {
  573. $a = (object)array('option'=>'shortname', 'value'=>$options['shortname']);
  574. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  575. }
  576. }
  577. // ask for admin user name
  578. if ($interactive) {
  579. cli_separator();
  580. cli_heading(get_string('cliadminusername', 'install'));
  581. if (!empty($options['adminuser'])) {
  582. $prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']);
  583. } else {
  584. $prompt = get_string('clitypevalue', 'admin');
  585. }
  586. do {
  587. $options['adminuser'] = cli_input($prompt, $options['adminuser']);
  588. } while (empty($options['adminuser']) or $options['adminuser'] === 'guest');
  589. } else {
  590. if (empty($options['adminuser']) or $options['adminuser'] === 'guest') {
  591. $a = (object)array('option'=>'adminuser', 'value'=>$options['adminuser']);
  592. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  593. }
  594. }
  595. // ask for admin user password
  596. if ($interactive) {
  597. cli_separator();
  598. cli_heading(get_string('cliadminpassword', 'install'));
  599. $prompt = get_string('clitypevalue', 'admin');
  600. do {
  601. $options['adminpass'] = cli_input($prompt);
  602. } while (empty($options['adminpass']) or $options['adminpass'] === 'admin');
  603. } else {
  604. if (empty($options['adminpass']) or $options['adminpass'] === 'admin') {
  605. $a = (object)array('option'=>'adminpass', 'value'=>$options['adminpass']);
  606. cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
  607. }
  608. }
  609. if ($interactive) {
  610. if (!$options['agree-license']) {
  611. cli_separator();
  612. cli_heading(get_string('copyrightnotice'));
  613. echo "Moodle - Modular Object-Oriented Dynamic Learning Environment\n";
  614. echo get_string('gpl3')."\n\n";
  615. echo get_string('doyouagree')."\n";
  616. $prompt = get_string('cliyesnoprompt', 'admin');
  617. $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
  618. if ($input == get_string('clianswerno', 'admin')) {
  619. exit(1);
  620. }
  621. }
  622. } else {
  623. if (!$options['agree-license']) {
  624. cli_error(get_string('climustagreelicense', 'install'));
  625. }
  626. }
  627. // Finally we have all info needed for config.php
  628. $configphp = install_generate_configphp($database, $CFG);
  629. umask(0137);
  630. if (($fh = fopen($configfile, 'w')) !== false) {
  631. fwrite($fh, $configphp);
  632. fclose($fh);
  633. }
  634. if (!file_exists($configfile)) {
  635. cli_error('Can not create config file.');
  636. }
  637. // remember selected language
  638. $installlang = $CFG->lang;
  639. // return back to original dir before executing setup.php which changes the dir again
  640. chdir($olddir);
  641. // We have config.php, it is a real php script from now on :-)
  642. require($configfile);
  643. // use selected language
  644. $CFG->lang = $installlang;
  645. $SESSION->lang = $CFG->lang;
  646. require("$CFG->dirroot/version.php");
  647. // Test environment first.
  648. require_once($CFG->libdir . '/environmentlib.php');
  649. list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
  650. if (!$envstatus) {
  651. $errors = environment_get_errors($environment_results);
  652. cli_heading(get_string('environment', 'admin'));
  653. foreach ($errors as $error) {
  654. list($info, $report) = $error;
  655. echo "!! $info !!\n$report\n\n";
  656. }
  657. exit(1);
  658. }
  659. // Test plugin dependencies.
  660. $failed = array();
  661. if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
  662. cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
  663. cli_error(get_string('pluginschecktodo', 'admin'));
  664. }
  665. install_cli_database($options, $interactive);
  666. echo get_string('cliinstallfinished', 'install')."\n";
  667. exit(0); // 0 means success