MDL-27171 messages: implement new syntax in messages.php and update processing
[moodle.git] / lib / installlib.php
CommitLineData
95feaf96 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/>.
2f13f94c 17
2f13f94c 18/**
9d068cd6 19 * Functions to support installation process
95feaf96 20 *
b7315f35 21 * @package core
95feaf96 22 * @subpackage install
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2f13f94c 25 */
2f13f94c 26
460ebd6c
PS
27defined('MOODLE_INTERNAL') || die();
28
226617ea 29/** INSTALL_WELCOME = 0 */
3b093310 30define('INSTALL_WELCOME', 0);
226617ea 31/** INSTALL_ENVIRONMENT = 1 */
64c368e3 32define('INSTALL_ENVIRONMENT', 1);
226617ea 33/** INSTALL_PATHS = 2 */
64c368e3 34define('INSTALL_PATHS', 2);
226617ea 35/** INSTALL_DOWNLOADLANG = 3 */
64c368e3 36define('INSTALL_DOWNLOADLANG', 3);
226617ea 37/** INSTALL_DATABASETYPE = 4 */
64c368e3 38define('INSTALL_DATABASETYPE', 4);
226617ea 39/** INSTALL_DATABASE = 5 */
64c368e3 40define('INSTALL_DATABASE', 5);
226617ea 41/** INSTALL_SAVE = 6 */
64c368e3 42define('INSTALL_SAVE', 6);
3b093310 43
11e7b506 44/**
226617ea 45 * Tries to detect the right www root setting.
11e7b506 46 * @return string detected www root
47 */
48function install_guess_wwwroot() {
49 $wwwroot = '';
50 if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
51 $wwwroot .= 'http://';
52 } else {
53 $wwwroot .= 'https://';
54 }
55 $hostport = explode(':', $_SERVER['HTTP_HOST']);
56 $wwwroot .= reset($hostport);
57 if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
58 $wwwroot .= ':'.$_SERVER['SERVER_PORT'];
59 }
60 $wwwroot .= $_SERVER['SCRIPT_NAME'];
61
62 list($wwwroot, $xtra) = explode('/install.php', $wwwroot);
63
64 return $wwwroot;
65}
66
95feaf96 67/**
68 * Copy of @see{ini_get_bool()}
69 * @param string $ini_get_arg
70 * @return bool
71 */
3b093310 72function install_ini_get_bool($ini_get_arg) {
73 $temp = ini_get($ini_get_arg);
74
75 if ($temp == '1' or strtolower($temp) == 'on') {
76 return true;
77 }
78 return false;
79}
80
0bda7031
PS
81/**
82 * Creates dataroot if not exists yet,
83 * makes sure it is writable, add lang directory
84 * and add .htaccess just in case it works.
85 *
86 * @param string $dataroot full path to dataroot
87 * @param int $dirpermissions
88 * @return bool success
89 */
90function install_init_dataroot($dataroot, $dirpermissions) {
91 if (file_exists($dataroot) and !is_dir($dataroot)) {
92 // file with the same name exists
93 return false;
94 }
95
96 umask(0000);
97 if (!file_exists($dataroot)) {
98 if (!mkdir($dataroot, $dirpermissions, true)) {
99 // most probably this does not work, but anyway
100 return false;
101 }
102 }
103 @chmod($dataroot, $dirpermissions);
104
105 if (!is_writable($dataroot)) {
106 return false; // we can not continue
107 }
108
109 // now create the lang folder - we need it and it makes sure we can really write in dataroot
110 if (!is_dir("$dataroot/lang")) {
111 if (!mkdir("$dataroot/lang", $dirpermissions, true)) {
112 return false;
113 }
114 }
115 if (!is_writable("$dataroot/lang")) {
116 return false; // we can not continue
117 }
118
119 // finally just in case some broken .htaccess that prevents access just in case it is allowed
120 if (!file_exists("$dataroot/.htaccess")) {
121 if ($handle = fopen("$dataroot/.htaccess", 'w')) {
122 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");
123 fclose($handle);
124 } else {
125 return false;
126 }
127 }
128
129 return true;
130}
131
95feaf96 132/**
133 * Print help button
134 * @param string $url
135 * @param string $titel
136 * @return void
137 */
3b093310 138function install_helpbutton($url, $title='') {
139 if ($title == '') {
140 $title = get_string('help');
141 }
142 echo "<a href=\"javascript:void(0)\" ";
143 echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
144 echo ">";
145 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
146 echo "</a>\n";
147}
148
95feaf96 149/**
150 * This is in function because we want the /install.php to parse in PHP4
226617ea 151 *
152 * @param object $database
153 * @param string $dbhsot
154 * @param string $dbuser
155 * @param string $dbpass
156 * @param string $dbname
157 * @param string $prefix
158 * @param mixed $dboptions
159 * @return string
95feaf96 160 */
768408e8 161function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) {
3b093310 162 try {
163 try {
164 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
165 } catch (moodle_exception $e) {
166 // let's try to create new database
167 if ($database->create_database($dbhost, $dbuser, $dbpass, $dbname, $dboptions)) {
168 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
169 } else {
170 throw $e;
171 }
172 }
173 return '';
174 } catch (dml_exception $ex) {
175 return get_string($ex->errorcode, $ex->module, $ex->a).'<br />'.$ex->debuginfo;
176 }
177}
178
dc47d704 179/**
180 * Returns content of config.php file.
6189e63d
DM
181 *
182 * Uses PHP_EOL for generating proper end of lines for the given platform.
183 *
dc47d704 184 * @param moodle_database $database database instance
185 * @param object $cfg copy of $CFG
dc47d704 186 * @return string
187 */
3b5ff37f 188function install_generate_configphp($database, $cfg) {
59ba2bb0 189 $configphp = '<?php // Moodle configuration file' . PHP_EOL . PHP_EOL;
dc47d704 190
6189e63d 191 $configphp .= 'unset($CFG);' . PHP_EOL;
b30fa335 192 $configphp .= 'global $CFG;' . PHP_EOL;
6189e63d 193 $configphp .= '$CFG = new stdClass();' . PHP_EOL . PHP_EOL; // prevent PHP5 strict warnings
dc47d704 194
195 $dbconfig = $database->export_dbconfig();
196
197 foreach ($dbconfig as $key=>$value) {
198 $key = str_pad($key, 9);
6189e63d 199 $configphp .= '$CFG->'.$key.' = '.var_export($value, true) . ';' . PHP_EOL;
dc47d704 200 }
6189e63d 201 $configphp .= PHP_EOL;
dc47d704 202
6189e63d 203 $configphp .= '$CFG->wwwroot = '.var_export($cfg->wwwroot, true) . ';' . PHP_EOL ;
dc47d704 204
3b5ff37f 205 $configphp .= '$CFG->dataroot = '.var_export($cfg->dataroot, true) . ';' . PHP_EOL;
dc47d704 206
6189e63d 207 $configphp .= '$CFG->admin = '.var_export($cfg->admin, true) . ';' . PHP_EOL . PHP_EOL;
dc47d704 208
7be67681
DM
209 if (empty($cfg->directorypermissions)) {
210 $chmod = '02777';
211 } else {
212 $chmod = '0' . decoct($cfg->directorypermissions);
213 }
214 $configphp .= '$CFG->directorypermissions = ' . $chmod . ';' . PHP_EOL . PHP_EOL;
dc47d704 215
6189e63d 216 $configphp .= '$CFG->passwordsaltmain = '.var_export(complex_random_string(), true) . ';' . PHP_EOL . PHP_EOL;
4c1ba3ff 217
3b5ff37f 218 $configphp .= 'require_once(dirname(__FILE__) . \'/lib/setup.php\');' . PHP_EOL . PHP_EOL;
6189e63d
DM
219 $configphp .= '// There is no php closing tag in this file,' . PHP_EOL;
220 $configphp .= '// it is intentional because it prevents trailing whitespace problems!' . PHP_EOL;
dc47d704 221
222 return $configphp;
223}
224
95feaf96 225/**
226 * Prints complete help page used during installation.
227 * Does not return.
226617ea 228 *
229 * @global object
95feaf96 230 * @param string $help
231 */
3b093310 232function install_print_help_page($help) {
3a915b06 233 global $CFG, $OUTPUT; //TODO: MUST NOT USE $OUTPUT HERE!!!
3b093310 234
235 @header('Content-Type: text/html; charset=UTF-8');
236 @header('Cache-Control: no-store, no-cache, must-revalidate');
237 @header('Cache-Control: post-check=0, pre-check=0', false);
238 @header('Pragma: no-cache');
239 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
240 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
241
242 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
243 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
244 <head>
78946b9b 245 <link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />
3b093310 246 <link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install.php?css=1" />
247 <title>'.get_string('installation','install').'</title>
248 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
249 <meta http-equiv="pragma" content="no-cache" />
250 <meta http-equiv="expires" content="0" />';
251
252 echo '</head><body>';
253 switch ($help) {
254 case 'phpversionhelp':
255 print_string($help, 'install', phpversion());
256 break;
257 case 'memorylimithelp':
8d3f06f7 258 print_string($help, 'install', @ini_get('memory_limit'));
3b093310 259 break;
260 default:
261 print_string($help, 'install');
2f13f94c 262 }
3a915b06 263 echo $OUTPUT->close_window_button(); //TODO: MUST NOT USE $OUTPUT HERE!!!
3b093310 264 echo '</body></html>';
265 die;
2f13f94c 266}
267
95feaf96 268/**
8bb70111 269 * Prints installation page header, we can no use weblib yet in installer.
226617ea 270 *
271 * @global object
95feaf96 272 * @param array $config
273 * @param string $stagename
274 * @param string $heading
275 * @param string $stagetext
276 * @return void
277 */
3b093310 278function install_print_header($config, $stagename, $heading, $stagetext) {
279 global $CFG;
2f13f94c 280
3b093310 281 @header('Content-Type: text/html; charset=UTF-8');
282 @header('Cache-Control: no-store, no-cache, must-revalidate');
283 @header('Cache-Control: post-check=0, pre-check=0', false);
284 @header('Pragma: no-cache');
285 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
286 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
287
288 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
289 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
290 <head>
78946b9b 291 <link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />';
3b093310 292
90723839 293 $sheets = array('pagelayout','core');
3b093310 294 $csss = array();
295 foreach ($sheets as $sheet) {
90723839 296 $csss[] = $CFG->wwwroot.'/theme/base/style/'.$sheet.'.css';
3b093310 297 }
90723839 298 $sheets = array('core', 'css3');
3b093310 299 foreach ($sheets as $sheet) {
90723839 300 $csss[] = $CFG->wwwroot.'/theme/standard/style/'.$sheet.'.css';
2f13f94c 301 }
3b093310 302 foreach ($csss as $css) {
303 echo '<link rel="stylesheet" type="text/css" href="'.$css.'" />'."\n";
304 }
305
306 echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install.php?css=1" />
9ace5094 307 <title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
3b093310 308 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
309 <meta http-equiv="pragma" content="no-cache" />
310 <meta http-equiv="expires" content="0" />';
311
312 echo '</head><body class="notloggedin">
313 <div id="page" class="stage'.$config->stage.'">
90723839
SH
314 <div id="page-header">
315 <div id="header" class=" clearfix">
316 <h1 class="headermain">'.get_string('installation','install').'</h1>
317 <div class="headermenu">&nbsp;</div>
318 </div>
319 <div class="navbar clearfix">
320 <div class="breadcrumb">
321 <ul><li class="first">'.$stagename.'</li></ul>
322 </div>
323 <div class="navbutton">&nbsp;</div>
3b093310 324 </div>
3b093310 325 </div>
326 <!-- END OF HEADER -->
327 <div id="installdiv">';
2f13f94c 328
3b093310 329 echo '<h2>'.$heading.'</h2>';
330
331 if ($stagetext !== '') {
332 echo '<div class="stage generalbox box">';
333 echo $stagetext;
334 echo '</div>';
335 }
336 // main
337 echo '<form id="installform" method="post" action="install.php"><fieldset>';
338 foreach ($config as $name=>$value) {
339 echo '<input type="hidden" name="'.$name.'" value="'.s($value).'" />';
340 }
2f13f94c 341}
342
95feaf96 343/**
344 * Prints installation page header, we can no use weblib yet in isntaller.
226617ea 345 *
346 * @global object
95feaf96 347 * @param array $config
348 * @param bool $reload print reload button instead of next
349 * @return void
350 */
3b093310 351function install_print_footer($config, $reload=false) {
352 global $CFG;
353
354 if ($config->stage > INSTALL_WELCOME) {
4d928665 355 $first = '<input type="submit" id="previousbutton" name="previous" value="&laquo; '.s(get_string('previous')).'" />';
3b093310 356 } else {
85ed076b 357 $first = '<input type="submit" id="previousbutton" name="next" value="'.s(get_string('reload')).'" />';
4d928665 358 $first .= '<script type="text/javascript">
359//<![CDATA[
360 var first = document.getElementById("previousbutton");
361 first.style.visibility = "hidden";
362//]]>
363</script>
364';
3b093310 365 }
366
367 if ($reload) {
85ed076b 368 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('reload')).'" />';
3b093310 369 } else {
4d928665 370 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('next')).' &raquo;" />';
3b093310 371 }
372
373 echo '</fieldset><fieldset id="nav_buttons">'.$first.$next.'</fieldset>';
374
375 $homelink = '<div class="sitelink">'.
9ace5094 376 '<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'.
3b093310 377 '<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
378
379 echo '</form></div>';
380 echo '<div id="footer"><hr />'.$homelink.'</div>';
381 echo '</div></body></html>';
382}
383
384
95feaf96 385/**
386 * Prints css needed on installation page, tries to look like the rest of installation.
387 * Does not return.
226617ea 388 *
389 * @global object
95feaf96 390 */
3b093310 391function install_css_styles() {
392 global $CFG;
393
394 @header('Content-type: text/css'); // Correct MIME type
395 @header('Cache-Control: no-store, no-cache, must-revalidate');
396 @header('Cache-Control: post-check=0, pre-check=0', false);
397 @header('Pragma: no-cache');
398 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
399 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
400
da0c0e25 401//TODO: add rtl support here, make it match new default theme MDL-21149
3b093310 402
403 echo '
404
405h2 {
406 text-align:center;
407}
408
409#installdiv {
410 width: 800px;
411 margin-left:auto;
412 margin-right:auto;
413}
414
415#installdiv dt {
416 font-weight: bold;
417}
418
419#installdiv dd {
420 padding-bottom: 0.5em;
421}
422
423.stage {
424 margin-top: 2em;
425 margin-bottom: 2em;
426 width: 100%;
427 padding:25px;
428}
429
430#installform {
431 width: 100%;
432}
433
434#nav_buttons input {
435 margin: 5px;
436}
437
438#envresult {
439 text-align:left;
440 width: auto;
441 margin-left:10em;
442}
443
444#envresult dd {
445 color: red;
446}
447
448.formrow {
449 clear:both;
450 text-align:left;
451 padding: 8px;
452}
453
454.formrow label.formlabel {
455 display:block;
456 float:left;
457 width: 260px;
458 margin-right:5px;
459 text-align:right;
460}
461
462.formrow .forminput {
463 display:block;
464 float:left;
465}
466
467fieldset {
468 text-align:center;
469 border:none;
470}
471
472.hint {
473 display:block;
474 clear:both;
475 padding-left: 265px;
476 color: red;
477}
478
479.configphp {
480 text-align:left;
481 background-color:white;
482 padding:1em;
483 width:95%;
484}
485
64c368e3 486.stage6 .stage {
3b093310 487 font-weight: bold;
488 color: red;
489}
490
491';
492
493 die;
2f13f94c 494}
b7315f35
PS
495
496/**
497 * Install Moodle DB,
498 * config.php must exist, there must not be any tables in db yet.
499 *
500 * @param array $options adminpass is mandatory
501 * @param bool $interactive
502 * @return void
503 */
504function install_cli_database(array $options, $interactive) {
505 global $CFG, $DB;
506 require_once($CFG->libdir.'/environmentlib.php');
507 require_once($CFG->libdir.'/upgradelib.php');
508
509 // show as much debug as possible
510 @error_reporting(1023);
511 @ini_set('display_errors', '1');
512 $CFG->debug = 38911;
513 $CFG->debugdisplay = true;
514
515 $CFG->version = '';
516 $CFG->release = '';
e71ea28f 517 $version = null;
6ab00cce 518 $release = null;
b7315f35
PS
519
520 // read $version and $release
521 require($CFG->dirroot.'/version.php');
522
523 if ($DB->get_tables() ) {
524 cli_error(get_string('clitablesexist', 'install'));
525 }
526
527 if (empty($options['adminpass'])) {
528 cli_error('Missing required admin password');
529 }
530
531 // test environment first
1c71748d 532 if (!check_moodle_environment(normalize_version($release), $environment_results, false, ENV_SELECT_RELEASE)) {
b7315f35
PS
533 $errors = environment_get_errors($environment_results);
534 cli_heading(get_string('environment', 'admin'));
535 foreach ($errors as $error) {
536 list($info, $report) = $error;
537 echo "!! $info !!\n$report\n\n";
538 }
b7315f35
PS
539 exit(1);
540 }
541
542 if (!$DB->setup_is_unicodedb()) {
543 if (!$DB->change_db_encoding()) {
544 // If could not convert successfully, throw error, and prevent installation
545 cli_error(get_string('unicoderequired', 'admin'));
546 }
547 }
548
549 if ($interactive) {
550 cli_separator();
551 cli_heading(get_string('databasesetup'));
552 }
553
554 // install core
555 install_core($version, true);
556 set_config('release', $release);
557
558 // install all plugins types, local, etc.
559 upgrade_noncore(true);
560
561 // set up admin user password
562 $DB->set_field('user', 'password', hash_internal_user_password($options['adminpass']), array('username' => 'admin'));
563
564 // rename admin username if needed
565 if (isset($options['adminuser']) and $options['adminuser'] !== 'admin' and $options['adminuser'] !== 'guest') {
566 $DB->set_field('user', 'username', $options['adminuser'], array('username' => 'admin'));
567 }
568
569 // indicate that this site is fully configured
570 set_config('rolesactive', 1);
571 upgrade_finished();
572
573 // log in as admin - we need do anything when applying defaults
574 $admins = get_admins();
575 $admin = reset($admins);
576 session_set_user($admin);
b7315f35
PS
577
578 // apply all default settings, do it twice to fill all defaults - some settings depend on other setting
579 admin_apply_default_settings(NULL, true);
580 admin_apply_default_settings(NULL, true);
581 set_config('registerauth', '');
582
9632db7a
DM
583 // set the site name
584 if (isset($options['shortname']) and $options['shortname'] !== '') {
585 $DB->set_field('course', 'shortname', $options['shortname'], array('format' => 'site'));
586 }
587 if (isset($options['fullname']) and $options['fullname'] !== '') {
588 $DB->set_field('course', 'fullname', $options['fullname'], array('format' => 'site'));
589 }
590}