"MDL-19272, fixed undefined ->pixpath in google docs plugin"
[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 *
21 * @package moodlecore
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
3b093310 27define('INSTALL_WELCOME', 0);
64c368e3 28define('INSTALL_ENVIRONMENT', 1);
29define('INSTALL_PATHS', 2);
30define('INSTALL_DOWNLOADLANG', 3);
31define('INSTALL_DATABASETYPE', 4);
32define('INSTALL_DATABASE', 5);
33define('INSTALL_SAVE', 6);
3b093310 34
11e7b506 35/**
36 *Tries to detect the right www root setting.
11e7b506 37 * @return string detected www root
38 */
39function install_guess_wwwroot() {
40 $wwwroot = '';
41 if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
42 $wwwroot .= 'http://';
43 } else {
44 $wwwroot .= 'https://';
45 }
46 $hostport = explode(':', $_SERVER['HTTP_HOST']);
47 $wwwroot .= reset($hostport);
48 if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
49 $wwwroot .= ':'.$_SERVER['SERVER_PORT'];
50 }
51 $wwwroot .= $_SERVER['SCRIPT_NAME'];
52
53 list($wwwroot, $xtra) = explode('/install.php', $wwwroot);
54
55 return $wwwroot;
56}
57
95feaf96 58/**
59 * Copy of @see{ini_get_bool()}
60 * @param string $ini_get_arg
61 * @return bool
62 */
3b093310 63function install_ini_get_bool($ini_get_arg) {
64 $temp = ini_get($ini_get_arg);
65
66 if ($temp == '1' or strtolower($temp) == 'on') {
67 return true;
68 }
69 return false;
70}
71
95feaf96 72/**
73 * Print help button
74 * @param string $url
75 * @param string $titel
76 * @return void
77 */
3b093310 78function install_helpbutton($url, $title='') {
79 if ($title == '') {
80 $title = get_string('help');
81 }
82 echo "<a href=\"javascript:void(0)\" ";
83 echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
84 echo ">";
85 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
86 echo "</a>\n";
87}
88
95feaf96 89/**
90 * This is in function because we want the /install.php to parse in PHP4
91 */
768408e8 92function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) {
3b093310 93 try {
94 try {
95 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
96 } catch (moodle_exception $e) {
97 // let's try to create new database
98 if ($database->create_database($dbhost, $dbuser, $dbpass, $dbname, $dboptions)) {
99 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
100 } else {
101 throw $e;
102 }
103 }
104 return '';
105 } catch (dml_exception $ex) {
106 return get_string($ex->errorcode, $ex->module, $ex->a).'<br />'.$ex->debuginfo;
107 }
108}
109
2f13f94c 110/**
9d068cd6 111 * This function returns a list of languages and their full names. The
112 * list of available languages is fetched from install/lang/xx/installer.php
113 * and it's used exclusively by the installation process
2f13f94c 114 * @return array An associative array with contents in the form of LanguageCode => LanguageName
115 */
3b093310 116function install_get_list_of_languages() {
2f13f94c 117 global $CFG;
118
119 $languages = array();
120
dc47d704 121 // Get raw list of lang directories
2f13f94c 122 $langdirs = get_list_of_plugins('install/lang');
123 asort($langdirs);
dc47d704 124 // Get some info from each lang
2f13f94c 125 foreach ($langdirs as $lang) {
3b093310 126 if ($lang == 'en') {
127 continue;
128 }
129 if (file_exists($CFG->dirroot.'/install/lang/'.$lang.'/installer.php')) {
130 $string = array();
131 include($CFG->dirroot.'/install/lang/'.$lang.'/installer.php');
132 if (substr($lang, -5) === '_utf8') { //Remove the _utf8 suffix from the lang to show
2f13f94c 133 $shortlang = substr($lang, 0, -5);
134 } else {
135 $shortlang = $lang;
136 }
2f13f94c 137 if (!empty($string['thislanguage'])) {
3b093310 138 $languages[$lang] = $string['thislanguage'].' ('.$shortlang.')';
2f13f94c 139 }
2f13f94c 140 }
141 }
dc47d704 142 // Return array
2f13f94c 143 return $languages;
144}
2f13f94c 145
dc47d704 146/**
147 * Returns content of config.php file.
148 * @param moodle_database $database database instance
149 * @param object $cfg copy of $CFG
150 * @param bool $userealpath allows symbolic links in dirroot
151 * @return string
152 */
153function install_generate_configphp($database, $cfg, $userealpath=false) {
154 $configphp = '<?php // Moodle Configuration File ' . "\r\n\r\n";
155
156 $configphp .= 'unset($CFG);'."\r\n";
157 $configphp .= '$CFG = new stdClass();'."\r\n\r\n"; // prevent PHP5 strict warnings
158
159 $dbconfig = $database->export_dbconfig();
160
161 foreach ($dbconfig as $key=>$value) {
162 $key = str_pad($key, 9);
163 $configphp .= '$CFG->'.$key.' = '.var_export($value, true).";\r\n";
164 }
165 $configphp .= "\r\n";
166
167 $configphp .= '$CFG->wwwroot = '.var_export($cfg->wwwroot, true).";\r\n";
168
169 if ($userealpath) {
170 $dirroot = str_replace('\\', '/', $cfg->dirroot); // win32 fix
171 $dirroot = rtrim($dirroot, '/'); // no trailing /
172 $configphp .= '$CFG->dirroot = realpath('.var_export($dirroot, true).");\r\n"; // fix for sym links
173 } else {
174 $dirroot = str_replace('\\', '/', $cfg->dirroot); // win32 fix
175 $dirroot = rtrim($dirroot, '/'); // no trailing /
176 $configphp .= '$CFG->dirroot = '.var_export($dirroot, true).";\r\n";
177 }
178
179 $dataroot = str_replace('\\', '/', $cfg->dataroot); // win32 fix
180 $dataroot = rtrim($dataroot, '/'); // no trailing /
181 $configphp .= '$CFG->dataroot = '.var_export($dataroot, true).";\r\n";
182
183 $configphp .= '$CFG->admin = '.var_export($cfg->admin, true).";\r\n\r\n";
184
185 $configphp .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
186 $configphp .= "\r\n";
187
188 $configphp .= 'require_once("$CFG->dirroot/lib/setup.php");'."\r\n\r\n";
189 $configphp .= '// There is no php closing tag in this file,'."\r\n";
190 $configphp .= '// it is intentional because it prevents trailing whitespace problems!'."\r\n";
191
192 return $configphp;
193}
194
95feaf96 195/**
196 * Prints complete help page used during installation.
197 * Does not return.
198 * @param string $help
199 */
3b093310 200function install_print_help_page($help) {
201 global $CFG;
202
203 @header('Content-Type: text/html; charset=UTF-8');
204 @header('Cache-Control: no-store, no-cache, must-revalidate');
205 @header('Cache-Control: post-check=0, pre-check=0', false);
206 @header('Pragma: no-cache');
207 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
208 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
209
210 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
211 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
212 <head>
213 <link rel="shortcut icon" href="theme/standard/favicon.ico" />
214 <link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install.php?css=1" />
215 <title>'.get_string('installation','install').'</title>
216 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
217 <meta http-equiv="pragma" content="no-cache" />
218 <meta http-equiv="expires" content="0" />';
219
220 echo '</head><body>';
221 switch ($help) {
222 case 'phpversionhelp':
223 print_string($help, 'install', phpversion());
224 break;
225 case 'memorylimithelp':
226 print_string($help, 'install', get_memory_limit());
227 break;
228 default:
229 print_string($help, 'install');
2f13f94c 230 }
3b093310 231 close_window_button();
232 echo '</body></html>';
233 die;
2f13f94c 234}
235
95feaf96 236/**
237 * Prints installation page header, we can no use weblib yet in isntaller.
238 * @param array $config
239 * @param string $stagename
240 * @param string $heading
241 * @param string $stagetext
242 * @return void
243 */
3b093310 244function install_print_header($config, $stagename, $heading, $stagetext) {
245 global $CFG;
2f13f94c 246
3b093310 247 @header('Content-Type: text/html; charset=UTF-8');
248 @header('Cache-Control: no-store, no-cache, must-revalidate');
249 @header('Cache-Control: post-check=0, pre-check=0', false);
250 @header('Pragma: no-cache');
251 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
252 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
253
254 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
255 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
256 <head>
257 <link rel="shortcut icon" href="theme/standard/favicon.ico" />';
258
259 $sheets = array('styles_layout', 'styles_fonts', 'styles_color', 'styles_moz');
260 $csss = array();
261 foreach ($sheets as $sheet) {
262 $csss[] = $CFG->wwwroot.'/theme/standard/'.$sheet.'.css';
263 }
264 $sheets = array('gradients');
265 foreach ($sheets as $sheet) {
266 $csss[] = $CFG->wwwroot.'/theme/standardwhite/'.$sheet.'.css';
2f13f94c 267 }
3b093310 268 foreach ($csss as $css) {
269 echo '<link rel="stylesheet" type="text/css" href="'.$css.'" />'."\n";
270 }
271
272 echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install.php?css=1" />
9ace5094 273 <title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
3b093310 274 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
275 <meta http-equiv="pragma" content="no-cache" />
276 <meta http-equiv="expires" content="0" />';
277
278 echo '</head><body class="notloggedin">
279 <div id="page" class="stage'.$config->stage.'">
280 <div id="header" class=" clearfix"><h1 class="headermain">'.get_string('installation','install').'</h1>
281 <div class="headermenu">&nbsp;</div></div><div class="navbar clearfix">
282 <div class="breadcrumb">
283 <ul><li class="first">'.$stagename.'</li></ul>
284 </div>
285 <div class="navbutton">&nbsp;</div>
286 </div>
287 <!-- END OF HEADER -->
288 <div id="installdiv">';
2f13f94c 289
3b093310 290 echo '<h2>'.$heading.'</h2>';
291
292 if ($stagetext !== '') {
293 echo '<div class="stage generalbox box">';
294 echo $stagetext;
295 echo '</div>';
296 }
297 // main
298 echo '<form id="installform" method="post" action="install.php"><fieldset>';
299 foreach ($config as $name=>$value) {
300 echo '<input type="hidden" name="'.$name.'" value="'.s($value).'" />';
301 }
2f13f94c 302}
303
95feaf96 304/**
305 * Prints installation page header, we can no use weblib yet in isntaller.
306 * @param array $config
307 * @param bool $reload print reload button instead of next
308 * @return void
309 */
3b093310 310function install_print_footer($config, $reload=false) {
311 global $CFG;
312
313 if ($config->stage > INSTALL_WELCOME) {
4d928665 314 $first = '<input type="submit" id="previousbutton" name="previous" value="&laquo; '.s(get_string('previous')).'" />';
3b093310 315 } else {
4d928665 316 $first = '<input type="submit" id="previousbutton" name="next" value="'.s(get_string('reload', 'admin')).'" />';
317 $first .= '<script type="text/javascript">
318//<![CDATA[
319 var first = document.getElementById("previousbutton");
320 first.style.visibility = "hidden";
321//]]>
322</script>
323';
3b093310 324 }
325
326 if ($reload) {
4d928665 327 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('reload', 'admin')).'" />';
3b093310 328 } else {
4d928665 329 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('next')).' &raquo;" />';
3b093310 330 }
331
332 echo '</fieldset><fieldset id="nav_buttons">'.$first.$next.'</fieldset>';
333
334 $homelink = '<div class="sitelink">'.
9ace5094 335 '<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'.
3b093310 336 '<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
337
338 echo '</form></div>';
339 echo '<div id="footer"><hr />'.$homelink.'</div>';
340 echo '</div></body></html>';
341}
342
343
95feaf96 344/**
345 * Prints css needed on installation page, tries to look like the rest of installation.
346 * Does not return.
347 */
3b093310 348function install_css_styles() {
349 global $CFG;
350
351 @header('Content-type: text/css'); // Correct MIME type
352 @header('Cache-Control: no-store, no-cache, must-revalidate');
353 @header('Cache-Control: post-check=0, pre-check=0', false);
354 @header('Pragma: no-cache');
355 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
356 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
357
358//TODO: add rtl support here
359
360 echo '
361
362h2 {
363 text-align:center;
364}
365
366#installdiv {
367 width: 800px;
368 margin-left:auto;
369 margin-right:auto;
370}
371
372#installdiv dt {
373 font-weight: bold;
374}
375
376#installdiv dd {
377 padding-bottom: 0.5em;
378}
379
380.stage {
381 margin-top: 2em;
382 margin-bottom: 2em;
383 width: 100%;
384 padding:25px;
385}
386
387#installform {
388 width: 100%;
389}
390
391#nav_buttons input {
392 margin: 5px;
393}
394
395#envresult {
396 text-align:left;
397 width: auto;
398 margin-left:10em;
399}
400
401#envresult dd {
402 color: red;
403}
404
405.formrow {
406 clear:both;
407 text-align:left;
408 padding: 8px;
409}
410
411.formrow label.formlabel {
412 display:block;
413 float:left;
414 width: 260px;
415 margin-right:5px;
416 text-align:right;
417}
418
419.formrow .forminput {
420 display:block;
421 float:left;
422}
423
424fieldset {
425 text-align:center;
426 border:none;
427}
428
429.hint {
430 display:block;
431 clear:both;
432 padding-left: 265px;
433 color: red;
434}
435
436.configphp {
437 text-align:left;
438 background-color:white;
439 padding:1em;
440 width:95%;
441}
442
64c368e3 443.stage6 .stage {
3b093310 444 font-weight: bold;
445 color: red;
446}
447
448';
449
450 die;
2f13f94c 451}