MDL-19247 Finished cleaning up Sniffer files
[moodle.git] / lib / installlib.php
CommitLineData
9d068cd6 1<?php //$Id$
2f13f94c 2
2f13f94c 3/**
9d068cd6 4 * Functions to support installation process
2f13f94c 5 */
2f13f94c 6
3b093310 7define('INSTALL_WELCOME', 0);
64c368e3 8define('INSTALL_ENVIRONMENT', 1);
9define('INSTALL_PATHS', 2);
10define('INSTALL_DOWNLOADLANG', 3);
11define('INSTALL_DATABASETYPE', 4);
12define('INSTALL_DATABASE', 5);
13define('INSTALL_SAVE', 6);
3b093310 14
11e7b506 15/**
16 *Tries to detect the right www root setting.
17 *
18 * @return string detected www root
19 */
20function install_guess_wwwroot() {
21 $wwwroot = '';
22 if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
23 $wwwroot .= 'http://';
24 } else {
25 $wwwroot .= 'https://';
26 }
27 $hostport = explode(':', $_SERVER['HTTP_HOST']);
28 $wwwroot .= reset($hostport);
29 if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
30 $wwwroot .= ':'.$_SERVER['SERVER_PORT'];
31 }
32 $wwwroot .= $_SERVER['SCRIPT_NAME'];
33
34 list($wwwroot, $xtra) = explode('/install.php', $wwwroot);
35
36 return $wwwroot;
37}
38
3b093310 39function install_ini_get_bool($ini_get_arg) {
40 $temp = ini_get($ini_get_arg);
41
42 if ($temp == '1' or strtolower($temp) == 'on') {
43 return true;
44 }
45 return false;
46}
47
48function install_helpbutton($url, $title='') {
49 if ($title == '') {
50 $title = get_string('help');
51 }
52 echo "<a href=\"javascript:void(0)\" ";
53 echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
54 echo ">";
55 echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
56 echo "</a>\n";
57}
58
768408e8 59function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) {
3b093310 60 // this is in function because we want the /install.php to parse in PHP4
6724b059 61
3b093310 62 try {
63 try {
64 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
65 } catch (moodle_exception $e) {
66 // let's try to create new database
67 if ($database->create_database($dbhost, $dbuser, $dbpass, $dbname, $dboptions)) {
68 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
69 } else {
70 throw $e;
71 }
72 }
73 return '';
74 } catch (dml_exception $ex) {
75 return get_string($ex->errorcode, $ex->module, $ex->a).'<br />'.$ex->debuginfo;
76 }
77}
78
2f13f94c 79/**
9d068cd6 80 * This function returns a list of languages and their full names. The
81 * list of available languages is fetched from install/lang/xx/installer.php
82 * and it's used exclusively by the installation process
2f13f94c 83 * @return array An associative array with contents in the form of LanguageCode => LanguageName
84 */
3b093310 85function install_get_list_of_languages() {
2f13f94c 86 global $CFG;
87
88 $languages = array();
89
9d068cd6 90/// Get raw list of lang directories
2f13f94c 91 $langdirs = get_list_of_plugins('install/lang');
92 asort($langdirs);
9d068cd6 93/// Get some info from each lang
2f13f94c 94 foreach ($langdirs as $lang) {
3b093310 95 if ($lang == 'en') {
96 continue;
97 }
98 if (file_exists($CFG->dirroot.'/install/lang/'.$lang.'/installer.php')) {
99 $string = array();
100 include($CFG->dirroot.'/install/lang/'.$lang.'/installer.php');
101 if (substr($lang, -5) === '_utf8') { //Remove the _utf8 suffix from the lang to show
2f13f94c 102 $shortlang = substr($lang, 0, -5);
103 } else {
104 $shortlang = $lang;
105 }
2f13f94c 106 if (!empty($string['thislanguage'])) {
3b093310 107 $languages[$lang] = $string['thislanguage'].' ('.$shortlang.')';
2f13f94c 108 }
2f13f94c 109 }
110 }
9d068cd6 111/// Return array
2f13f94c 112 return $languages;
113}
2f13f94c 114
3b093310 115function install_print_help_page($help) {
116 global $CFG;
117
118 @header('Content-Type: text/html; charset=UTF-8');
119 @header('Cache-Control: no-store, no-cache, must-revalidate');
120 @header('Cache-Control: post-check=0, pre-check=0', false);
121 @header('Pragma: no-cache');
122 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
123 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
124
125 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
126 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
127 <head>
128 <link rel="shortcut icon" href="theme/standard/favicon.ico" />
129 <link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install.php?css=1" />
130 <title>'.get_string('installation','install').'</title>
131 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
132 <meta http-equiv="pragma" content="no-cache" />
133 <meta http-equiv="expires" content="0" />';
134
135 echo '</head><body>';
136 switch ($help) {
137 case 'phpversionhelp':
138 print_string($help, 'install', phpversion());
139 break;
140 case 'memorylimithelp':
141 print_string($help, 'install', get_memory_limit());
142 break;
143 default:
144 print_string($help, 'install');
2f13f94c 145 }
3b093310 146 close_window_button();
147 echo '</body></html>';
148 die;
2f13f94c 149}
150
3b093310 151function install_print_header($config, $stagename, $heading, $stagetext) {
152 global $CFG;
2f13f94c 153
3b093310 154 @header('Content-Type: text/html; charset=UTF-8');
155 @header('Cache-Control: no-store, no-cache, must-revalidate');
156 @header('Cache-Control: post-check=0, pre-check=0', false);
157 @header('Pragma: no-cache');
158 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
159 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
160
161 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
162 echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
163 <head>
164 <link rel="shortcut icon" href="theme/standard/favicon.ico" />';
165
166 $sheets = array('styles_layout', 'styles_fonts', 'styles_color', 'styles_moz');
167 $csss = array();
168 foreach ($sheets as $sheet) {
169 $csss[] = $CFG->wwwroot.'/theme/standard/'.$sheet.'.css';
170 }
171 $sheets = array('gradients');
172 foreach ($sheets as $sheet) {
173 $csss[] = $CFG->wwwroot.'/theme/standardwhite/'.$sheet.'.css';
2f13f94c 174 }
3b093310 175 foreach ($csss as $css) {
176 echo '<link rel="stylesheet" type="text/css" href="'.$css.'" />'."\n";
177 }
178
179 echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install.php?css=1" />
9ace5094 180 <title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
3b093310 181 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
182 <meta http-equiv="pragma" content="no-cache" />
183 <meta http-equiv="expires" content="0" />';
184
185 echo '</head><body class="notloggedin">
186 <div id="page" class="stage'.$config->stage.'">
187 <div id="header" class=" clearfix"><h1 class="headermain">'.get_string('installation','install').'</h1>
188 <div class="headermenu">&nbsp;</div></div><div class="navbar clearfix">
189 <div class="breadcrumb">
190 <ul><li class="first">'.$stagename.'</li></ul>
191 </div>
192 <div class="navbutton">&nbsp;</div>
193 </div>
194 <!-- END OF HEADER -->
195 <div id="installdiv">';
2f13f94c 196
3b093310 197 echo '<h2>'.$heading.'</h2>';
198
199 if ($stagetext !== '') {
200 echo '<div class="stage generalbox box">';
201 echo $stagetext;
202 echo '</div>';
203 }
204 // main
205 echo '<form id="installform" method="post" action="install.php"><fieldset>';
206 foreach ($config as $name=>$value) {
207 echo '<input type="hidden" name="'.$name.'" value="'.s($value).'" />';
208 }
2f13f94c 209}
210
3b093310 211function install_print_footer($config, $reload=false) {
212 global $CFG;
213
214 if ($config->stage > INSTALL_WELCOME) {
4d928665 215 $first = '<input type="submit" id="previousbutton" name="previous" value="&laquo; '.s(get_string('previous')).'" />';
3b093310 216 } else {
4d928665 217 $first = '<input type="submit" id="previousbutton" name="next" value="'.s(get_string('reload', 'admin')).'" />';
218 $first .= '<script type="text/javascript">
219//<![CDATA[
220 var first = document.getElementById("previousbutton");
221 first.style.visibility = "hidden";
222//]]>
223</script>
224';
3b093310 225 }
226
227 if ($reload) {
4d928665 228 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('reload', 'admin')).'" />';
3b093310 229 } else {
4d928665 230 $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('next')).' &raquo;" />';
3b093310 231 }
232
233 echo '</fieldset><fieldset id="nav_buttons">'.$first.$next.'</fieldset>';
234
235 $homelink = '<div class="sitelink">'.
9ace5094 236 '<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'.
3b093310 237 '<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
238
239 echo '</form></div>';
240 echo '<div id="footer"><hr />'.$homelink.'</div>';
241 echo '</div></body></html>';
242}
243
244
245function install_css_styles() {
246 global $CFG;
247
248 @header('Content-type: text/css'); // Correct MIME type
249 @header('Cache-Control: no-store, no-cache, must-revalidate');
250 @header('Cache-Control: post-check=0, pre-check=0', false);
251 @header('Pragma: no-cache');
252 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
253 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
254
255//TODO: add rtl support here
256
257 echo '
258
259h2 {
260 text-align:center;
261}
262
263#installdiv {
264 width: 800px;
265 margin-left:auto;
266 margin-right:auto;
267}
268
269#installdiv dt {
270 font-weight: bold;
271}
272
273#installdiv dd {
274 padding-bottom: 0.5em;
275}
276
277.stage {
278 margin-top: 2em;
279 margin-bottom: 2em;
280 width: 100%;
281 padding:25px;
282}
283
284#installform {
285 width: 100%;
286}
287
288#nav_buttons input {
289 margin: 5px;
290}
291
292#envresult {
293 text-align:left;
294 width: auto;
295 margin-left:10em;
296}
297
298#envresult dd {
299 color: red;
300}
301
302.formrow {
303 clear:both;
304 text-align:left;
305 padding: 8px;
306}
307
308.formrow label.formlabel {
309 display:block;
310 float:left;
311 width: 260px;
312 margin-right:5px;
313 text-align:right;
314}
315
316.formrow .forminput {
317 display:block;
318 float:left;
319}
320
321fieldset {
322 text-align:center;
323 border:none;
324}
325
326.hint {
327 display:block;
328 clear:both;
329 padding-left: 265px;
330 color: red;
331}
332
333.configphp {
334 text-align:left;
335 background-color:white;
336 padding:1em;
337 width:95%;
338}
339
64c368e3 340.stage6 .stage {
3b093310 341 font-weight: bold;
342 color: red;
343}
344
345';
346
347 die;
2f13f94c 348}