MDL-30381 install_init_dataroot() now creates temp and cache
[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
fbe33209
DM
109 // create the directory for $CFG->tempdir
110 if (!is_dir("$dataroot/temp")) {
111 if (!mkdir("$dataroot/temp", $dirpermissions, true)) {
112 return false;
113 }
114 }
115 if (!is_writable("$dataroot/temp")) {
116 return false; // we can not continue
117 }
118
119 // create the directory for $CFG->cachedir
120 if (!is_dir("$dataroot/cache")) {
121 if (!mkdir("$dataroot/cache", $dirpermissions, true)) {
122 return false;
123 }
124 }
125 if (!is_writable("$dataroot/cache")) {
126 return false; // we can not continue
127 }
128
129 // create the directory for $CFG->langotherroot
0bda7031
PS
130 if (!is_dir("$dataroot/lang")) {
131 if (!mkdir("$dataroot/lang", $dirpermissions, true)) {
132 return false;
133 }
134 }
135 if (!is_writable("$dataroot/lang")) {
136 return false; // we can not continue
137 }
138
139 // finally just in case some broken .htaccess that prevents access just in case it is allowed
140 if (!file_exists("$dataroot/.htaccess")) {
141 if ($handle = fopen("$dataroot/.htaccess", 'w')) {
142 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");
143 fclose($handle);
144 } else {
145 return false;
146 }
147 }
148
149 return true;
150}
151
95feaf96 152/**
153 * Print help button
154 * @param string $url
155 * @param string $titel
156 * @return void
157 */
3b093310 158function install_helpbutton($url, $title='') {
159 if ($title == '') {
160 $title = get_string('help');
161 }
162 echo "<a href=\"javascript:void(0)\" ";
163 echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
164 echo ">";
165 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
166 echo "</a>\n";
167}
168
95feaf96 169/**
170 * This is in function because we want the /install.php to parse in PHP4
226617ea 171 *
172 * @param object $database
173 * @param string $dbhsot
174 * @param string $dbuser
175 * @param string $dbpass
176 * @param string $dbname
177 * @param string $prefix
178 * @param mixed $dboptions
179 * @return string
95feaf96 180 */
768408e8 181function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) {
3b093310 182 try {
183 try {
184 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
185 } catch (moodle_exception $e) {
186 // let's try to create new database
187 if ($database->create_database($dbhost, $dbuser, $dbpass, $dbname, $dboptions)) {
188 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
189 } else {
190 throw $e;
191 }
192 }
193 return '';
194 } catch (dml_exception $ex) {
195 return get_string($ex->errorcode, $ex->module, $ex->a).'<br />'.$ex->debuginfo;
196 }
197}
198
dc47d704 199/**
200 * Returns content of config.php file.
6189e63d
DM
201 *
202 * Uses PHP_EOL for generating proper end of lines for the given platform.
203 *
dc47d704 204 * @param moodle_database $database database instance
205 * @param object $cfg copy of $CFG
dc47d704 206 * @return string
207 */
3b5ff37f 208function install_generate_configphp($database, $cfg) {
59ba2bb0 209 $configphp = '<?php // Moodle configuration file' . PHP_EOL . PHP_EOL;
dc47d704 210
6189e63d 211 $configphp .= 'unset($CFG);' . PHP_EOL;
b30fa335 212 $configphp .= 'global $CFG;' . PHP_EOL;
6189e63d 213 $configphp .= '$CFG = new stdClass();' . PHP_EOL . PHP_EOL; // prevent PHP5 strict warnings
dc47d704 214
215 $dbconfig = $database->export_dbconfig();
216
217 foreach ($dbconfig as $key=>$value) {
218 $key = str_pad($key, 9);
6189e63d 219 $configphp .= '$CFG->'.$key.' = '.var_export($value, true) . ';' . PHP_EOL;
dc47d704 220 }
6189e63d 221 $configphp .= PHP_EOL;
dc47d704 222
6189e63d 223 $configphp .= '$CFG->wwwroot = '.var_export($cfg->wwwroot, true) . ';' . PHP_EOL ;
dc47d704 224
3b5ff37f 225 $configphp .= '$CFG->dataroot = '.var_export($cfg->dataroot, true) . ';' . PHP_EOL;
dc47d704 226
6189e63d 227 $configphp .= '$CFG->admin = '.var_export($cfg->admin, true) . ';' . PHP_EOL . PHP_EOL;
dc47d704 228
7be67681
DM
229 if (empty($cfg->directorypermissions)) {
230 $chmod = '02777';
231 } else {
232 $chmod = '0' . decoct($cfg->directorypermissions);
233 }
234 $configphp .= '$CFG->directorypermissions = ' . $chmod . ';' . PHP_EOL . PHP_EOL;
dc47d704 235
6189e63d 236 $configphp .= '$CFG->passwordsaltmain = '.var_export(complex_random_string(), true) . ';' . PHP_EOL . PHP_EOL;
4c1ba3ff 237
3b5ff37f 238 $configphp .= 'require_once(dirname(__FILE__) . \'/lib/setup.php\');' . PHP_EOL . PHP_EOL;
6189e63d
DM
239 $configphp .= '// There is no php closing tag in this file,' . PHP_EOL;
240 $configphp .= '// it is intentional because it prevents trailing whitespace problems!' . PHP_EOL;
dc47d704 241
242 return $configphp;
243}
244
95feaf96 245/**
246 * Prints complete help page used during installation.
247 * Does not return.
226617ea 248 *
249 * @global object
95feaf96 250 * @param string $help
251 */
3b093310 252function install_print_help_page($help) {
3a915b06 253 global $CFG, $OUTPUT; //TODO: MUST NOT USE $OUTPUT HERE!!!
3b093310 254
255 @header('Content-Type: text/html; charset=UTF-8');
256 @header('Cache-Control: no-store, no-cache, must-revalidate');
257 @header('Cache-Control: post-check=0, pre-check=0', false);
258 @header('Pragma: no-cache');
259 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
260 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
261
262 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
263 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
264 <head>
78946b9b 265 <link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />
e5d2c577 266 <link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
3b093310 267 <title>'.get_string('installation','install').'</title>
268 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
269 <meta http-equiv="pragma" content="no-cache" />
270 <meta http-equiv="expires" content="0" />';
271
272 echo '</head><body>';
273 switch ($help) {
274 case 'phpversionhelp':
275 print_string($help, 'install', phpversion());
276 break;
277 case 'memorylimithelp':
8d3f06f7 278 print_string($help, 'install', @ini_get('memory_limit'));
3b093310 279 break;
280 default:
281 print_string($help, 'install');
2f13f94c 282 }
3a915b06 283 echo $OUTPUT->close_window_button(); //TODO: MUST NOT USE $OUTPUT HERE!!!
3b093310 284 echo '</body></html>';
285 die;
2f13f94c 286}
287
95feaf96 288/**
8bb70111 289 * Prints installation page header, we can no use weblib yet in installer.
226617ea 290 *
291 * @global object
95feaf96 292 * @param array $config
293 * @param string $stagename
294 * @param string $heading
295 * @param string $stagetext
296 * @return void
297 */
3b093310 298function install_print_header($config, $stagename, $heading, $stagetext) {
299 global $CFG;
2f13f94c 300
3b093310 301 @header('Content-Type: text/html; charset=UTF-8');
302 @header('Cache-Control: no-store, no-cache, must-revalidate');
303 @header('Cache-Control: post-check=0, pre-check=0', false);
304 @header('Pragma: no-cache');
305 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
306 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
307
308 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
309 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
310 <head>
78946b9b 311 <link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />';
3b093310 312
e5d2c577 313 echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
9ace5094 314 <title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
3b093310 315 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
316 <meta http-equiv="pragma" content="no-cache" />
317 <meta http-equiv="expires" content="0" />';
318
319 echo '</head><body class="notloggedin">
320 <div id="page" class="stage'.$config->stage.'">
90723839
SH
321 <div id="page-header">
322 <div id="header" class=" clearfix">
323 <h1 class="headermain">'.get_string('installation','install').'</h1>
324 <div class="headermenu">&nbsp;</div>
325 </div>
326 <div class="navbar clearfix">
327 <div class="breadcrumb">
328 <ul><li class="first">'.$stagename.'</li></ul>
329 </div>
330 <div class="navbutton">&nbsp;</div>
3b093310 331 </div>
3b093310 332 </div>
333 <!-- END OF HEADER -->
334 <div id="installdiv">';
2f13f94c 335
3b093310 336 echo '<h2>'.$heading.'</h2>';
337
338 if ($stagetext !== '') {
339 echo '<div class="stage generalbox box">';
340 echo $stagetext;
341 echo '</div>';
342 }
343 // main
344 echo '<form id="installform" method="post" action="install.php"><fieldset>';
345 foreach ($config as $name=>$value) {
346 echo '<input type="hidden" name="'.$name.'" value="'.s($value).'" />';
347 }
2f13f94c 348}
349
95feaf96 350/**
351 * Prints installation page header, we can no use weblib yet in isntaller.
226617ea 352 *
353 * @global object
95feaf96 354 * @param array $config
355 * @param bool $reload print reload button instead of next
356 * @return void
357 */
3b093310 358function install_print_footer($config, $reload=false) {
359 global $CFG;
360
361 if ($config->stage > INSTALL_WELCOME) {
4d928665 362 $first = '<input type="submit" id="previousbutton" name="previous" value="&laquo; '.s(get_string('previous')).'" />';
3b093310 363 } else {
85ed076b 364 $first = '<input type="submit" id="previousbutton" name="next" value="'.s(get_string('reload')).'" />';
4d928665 365 $first .= '<script type="text/javascript">
366//<![CDATA[
367 var first = document.getElementById("previousbutton");
368 first.style.visibility = "hidden";
369//]]>
370</script>
371';
3b093310 372 }
373
374 if ($reload) {
85ed076b 375 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('reload')).'" />';
3b093310 376 } else {
4d928665 377 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('next')).' &raquo;" />';
3b093310 378 }
379
380 echo '</fieldset><fieldset id="nav_buttons">'.$first.$next.'</fieldset>';
381
382 $homelink = '<div class="sitelink">'.
9ace5094 383 '<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'.
3b093310 384 '<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
385
386 echo '</form></div>';
387 echo '<div id="footer"><hr />'.$homelink.'</div>';
388 echo '</div></body></html>';
389}
390
b7315f35
PS
391/**
392 * Install Moodle DB,
393 * config.php must exist, there must not be any tables in db yet.
394 *
395 * @param array $options adminpass is mandatory
396 * @param bool $interactive
397 * @return void
398 */
399function install_cli_database(array $options, $interactive) {
400 global $CFG, $DB;
401 require_once($CFG->libdir.'/environmentlib.php');
402 require_once($CFG->libdir.'/upgradelib.php');
403
404 // show as much debug as possible
405 @error_reporting(1023);
406 @ini_set('display_errors', '1');
407 $CFG->debug = 38911;
408 $CFG->debugdisplay = true;
409
410 $CFG->version = '';
411 $CFG->release = '';
e71ea28f 412 $version = null;
6ab00cce 413 $release = null;
b7315f35
PS
414
415 // read $version and $release
416 require($CFG->dirroot.'/version.php');
417
418 if ($DB->get_tables() ) {
419 cli_error(get_string('clitablesexist', 'install'));
420 }
421
422 if (empty($options['adminpass'])) {
423 cli_error('Missing required admin password');
424 }
425
426 // test environment first
cc359566
TH
427 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
428 if (!$envstatus) {
b7315f35
PS
429 $errors = environment_get_errors($environment_results);
430 cli_heading(get_string('environment', 'admin'));
431 foreach ($errors as $error) {
432 list($info, $report) = $error;
433 echo "!! $info !!\n$report\n\n";
434 }
b7315f35
PS
435 exit(1);
436 }
437
438 if (!$DB->setup_is_unicodedb()) {
439 if (!$DB->change_db_encoding()) {
440 // If could not convert successfully, throw error, and prevent installation
441 cli_error(get_string('unicoderequired', 'admin'));
442 }
443 }
444
445 if ($interactive) {
446 cli_separator();
447 cli_heading(get_string('databasesetup'));
448 }
449
450 // install core
451 install_core($version, true);
452 set_config('release', $release);
453
454 // install all plugins types, local, etc.
455 upgrade_noncore(true);
456
457 // set up admin user password
458 $DB->set_field('user', 'password', hash_internal_user_password($options['adminpass']), array('username' => 'admin'));
459
460 // rename admin username if needed
461 if (isset($options['adminuser']) and $options['adminuser'] !== 'admin' and $options['adminuser'] !== 'guest') {
462 $DB->set_field('user', 'username', $options['adminuser'], array('username' => 'admin'));
463 }
464
465 // indicate that this site is fully configured
466 set_config('rolesactive', 1);
467 upgrade_finished();
468
469 // log in as admin - we need do anything when applying defaults
470 $admins = get_admins();
471 $admin = reset($admins);
472 session_set_user($admin);
b7315f35
PS
473
474 // apply all default settings, do it twice to fill all defaults - some settings depend on other setting
475 admin_apply_default_settings(NULL, true);
476 admin_apply_default_settings(NULL, true);
477 set_config('registerauth', '');
478
9632db7a
DM
479 // set the site name
480 if (isset($options['shortname']) and $options['shortname'] !== '') {
481 $DB->set_field('course', 'shortname', $options['shortname'], array('format' => 'site'));
482 }
483 if (isset($options['fullname']) and $options['fullname'] !== '') {
484 $DB->set_field('course', 'fullname', $options['fullname'], array('format' => 'site'));
485 }
486}