Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/installlib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00030 define('INSTALL_WELCOME',       0);
00032 define('INSTALL_ENVIRONMENT',   1);
00034 define('INSTALL_PATHS',         2);
00036 define('INSTALL_DOWNLOADLANG',  3);
00038 define('INSTALL_DATABASETYPE',  4);
00040 define('INSTALL_DATABASE',      5);
00042 define('INSTALL_SAVE',          6);
00043 
00048 function install_guess_wwwroot() {
00049     $wwwroot = '';
00050     if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
00051         $wwwroot .= 'http://';
00052     } else {
00053         $wwwroot .= 'https://';
00054     }
00055     $hostport = explode(':', $_SERVER['HTTP_HOST']);
00056     $wwwroot .= reset($hostport);
00057     if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
00058         $wwwroot .= ':'.$_SERVER['SERVER_PORT'];
00059     }
00060     $wwwroot .= $_SERVER['SCRIPT_NAME'];
00061 
00062     list($wwwroot, $xtra) = explode('/install.php', $wwwroot);
00063 
00064     return $wwwroot;
00065 }
00066 
00072 function install_ini_get_bool($ini_get_arg) {
00073     $temp = ini_get($ini_get_arg);
00074 
00075     if ($temp == '1' or strtolower($temp) == 'on') {
00076         return true;
00077     }
00078     return false;
00079 }
00080 
00090 function install_init_dataroot($dataroot, $dirpermissions) {
00091     if (file_exists($dataroot) and !is_dir($dataroot)) {
00092         // file with the same name exists
00093         return false;
00094     }
00095 
00096     umask(0000);
00097     if (!file_exists($dataroot)) {
00098         if (!mkdir($dataroot, $dirpermissions, true)) {
00099             // most probably this does not work, but anyway
00100             return false;
00101         }
00102     }
00103     @chmod($dataroot, $dirpermissions);
00104 
00105     if (!is_writable($dataroot)) {
00106         return false; // we can not continue
00107     }
00108 
00109     // create the directory for $CFG->tempdir
00110     if (!is_dir("$dataroot/temp")) {
00111         if (!mkdir("$dataroot/temp", $dirpermissions, true)) {
00112             return false;
00113         }
00114     }
00115     if (!is_writable("$dataroot/temp")) {
00116         return false; // we can not continue
00117     }
00118 
00119     // create the directory for $CFG->cachedir
00120     if (!is_dir("$dataroot/cache")) {
00121         if (!mkdir("$dataroot/cache", $dirpermissions, true)) {
00122             return false;
00123         }
00124     }
00125     if (!is_writable("$dataroot/cache")) {
00126         return false; // we can not continue
00127     }
00128 
00129     // create the directory for $CFG->langotherroot
00130     if (!is_dir("$dataroot/lang")) {
00131         if (!mkdir("$dataroot/lang", $dirpermissions, true)) {
00132             return false;
00133         }
00134     }
00135     if (!is_writable("$dataroot/lang")) {
00136         return false; // we can not continue
00137     }
00138 
00139     // finally just in case some broken .htaccess that prevents access just in case it is allowed
00140     if (!file_exists("$dataroot/.htaccess")) {
00141         if ($handle = fopen("$dataroot/.htaccess", 'w')) {
00142             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");
00143             fclose($handle);
00144         } else {
00145             return false;
00146         }
00147     }
00148 
00149     return true;
00150 }
00151 
00158 function install_helpbutton($url, $title='') {
00159     if ($title == '') {
00160         $title = get_string('help');
00161     }
00162     echo "<a href=\"javascript:void(0)\" ";
00163     echo "onclick=\"return window.open('$url','Help','menubar=0,location=0,scrollbars,resizable,width=500,height=400')\"";
00164     echo ">";
00165     echo "<img src=\"pix/help.gif\" class=\"iconhelp\" alt=\"$title\" title=\"$title\"/>";
00166     echo "</a>\n";
00167 }
00168 
00181 function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) {
00182     try {
00183         try {
00184             $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
00185         } catch (moodle_exception $e) {
00186             // let's try to create new database
00187             if ($database->create_database($dbhost, $dbuser, $dbpass, $dbname, $dboptions)) {
00188                 $database->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
00189             } else {
00190                 throw $e;
00191             }
00192         }
00193         return '';
00194     } catch (dml_exception $ex) {
00195         return get_string($ex->errorcode, $ex->module, $ex->a).'<br />'.$ex->debuginfo;
00196     }
00197 }
00198 
00208 function install_generate_configphp($database, $cfg) {
00209     $configphp = '<?php  // Moodle configuration file' . PHP_EOL . PHP_EOL;
00210 
00211     $configphp .= 'unset($CFG);' . PHP_EOL;
00212     $configphp .= 'global $CFG;' . PHP_EOL;
00213     $configphp .= '$CFG = new stdClass();' . PHP_EOL . PHP_EOL; // prevent PHP5 strict warnings
00214 
00215     $dbconfig = $database->export_dbconfig();
00216 
00217     foreach ($dbconfig as $key=>$value) {
00218         $key = str_pad($key, 9);
00219         $configphp .= '$CFG->'.$key.' = '.var_export($value, true) . ';' . PHP_EOL;
00220     }
00221     $configphp .= PHP_EOL;
00222 
00223     $configphp .= '$CFG->wwwroot   = '.var_export($cfg->wwwroot, true) . ';' . PHP_EOL ;
00224 
00225     $configphp .= '$CFG->dataroot  = '.var_export($cfg->dataroot, true) . ';' . PHP_EOL;
00226 
00227     $configphp .= '$CFG->admin     = '.var_export($cfg->admin, true) . ';' . PHP_EOL . PHP_EOL;
00228 
00229     if (empty($cfg->directorypermissions)) {
00230         $chmod = '02777';
00231     } else {
00232         $chmod = '0' . decoct($cfg->directorypermissions);
00233     }
00234     $configphp .= '$CFG->directorypermissions = ' . $chmod . ';' . PHP_EOL . PHP_EOL;
00235 
00236     $configphp .= '$CFG->passwordsaltmain = '.var_export(complex_random_string(), true) . ';' . PHP_EOL . PHP_EOL;
00237 
00238     $configphp .= 'require_once(dirname(__FILE__) . \'/lib/setup.php\');' . PHP_EOL . PHP_EOL;
00239     $configphp .= '// There is no php closing tag in this file,' . PHP_EOL;
00240     $configphp .= '// it is intentional because it prevents trailing whitespace problems!' . PHP_EOL;
00241 
00242     return $configphp;
00243 }
00244 
00252 function install_print_help_page($help) {
00253     global $CFG, $OUTPUT; //TODO: MUST NOT USE $OUTPUT HERE!!!
00254 
00255     @header('Content-Type: text/html; charset=UTF-8');
00256     @header('Cache-Control: no-store, no-cache, must-revalidate');
00257     @header('Cache-Control: post-check=0, pre-check=0', false);
00258     @header('Pragma: no-cache');
00259     @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
00260     @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
00261 
00262     echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
00263     echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
00264           <head>
00265           <link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />
00266           <link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
00267           <title>'.get_string('installation','install').'</title>
00268           <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
00269           <meta http-equiv="pragma" content="no-cache" />
00270           <meta http-equiv="expires" content="0" />';
00271 
00272     echo '</head><body>';
00273     switch ($help) {
00274         case 'phpversionhelp':
00275             print_string($help, 'install', phpversion());
00276             break;
00277         case 'memorylimithelp':
00278             print_string($help, 'install', @ini_get('memory_limit'));
00279             break;
00280         default:
00281             print_string($help, 'install');
00282     }
00283     echo $OUTPUT->close_window_button(); //TODO: MUST NOT USE $OUTPUT HERE!!!
00284     echo '</body></html>';
00285     die;
00286 }
00287 
00298 function install_print_header($config, $stagename, $heading, $stagetext) {
00299     global $CFG;
00300 
00301     @header('Content-Type: text/html; charset=UTF-8');
00302     @header('Cache-Control: no-store, no-cache, must-revalidate');
00303     @header('Cache-Control: post-check=0, pre-check=0', false);
00304     @header('Pragma: no-cache');
00305     @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
00306     @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
00307 
00308     echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
00309     echo '<html dir="'.(right_to_left() ? 'rtl' : 'ltr').'">
00310           <head>
00311           <link rel="shortcut icon" href="theme/standard/pix/favicon.ico" />';
00312 
00313     echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
00314           <title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
00315           <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
00316           <meta http-equiv="pragma" content="no-cache" />
00317           <meta http-equiv="expires" content="0" />';
00318 
00319     echo '</head><body class="notloggedin">
00320             <div id="page" class="stage'.$config->stage.'">
00321                 <div id="page-header">
00322                     <div id="header" class=" clearfix">
00323                         <h1 class="headermain">'.get_string('installation','install').'</h1>
00324                         <div class="headermenu">&nbsp;</div>
00325                     </div>
00326                     <div class="navbar clearfix">
00327                         <div class="breadcrumb">
00328                             <ul><li class="first">'.$stagename.'</li></ul>
00329                         </div>
00330                         <div class="navbutton">&nbsp;</div>
00331                     </div>
00332                 </div>
00333           <!-- END OF HEADER -->
00334           <div id="installdiv">';
00335 
00336     echo '<h2>'.$heading.'</h2>';
00337 
00338     if ($stagetext !== '') {
00339         echo '<div class="stage generalbox box">';
00340         echo $stagetext;
00341         echo '</div>';
00342     }
00343     // main
00344     echo '<form id="installform" method="post" action="install.php"><fieldset>';
00345     foreach ($config as $name=>$value) {
00346         echo '<input type="hidden" name="'.$name.'" value="'.s($value).'" />';
00347     }
00348 }
00349 
00358 function install_print_footer($config, $reload=false) {
00359     global $CFG;
00360 
00361     if ($config->stage > INSTALL_WELCOME) {
00362         $first = '<input type="submit" id="previousbutton" name="previous" value="&laquo; '.s(get_string('previous')).'" />';
00363     } else {
00364         $first = '<input type="submit" id="previousbutton" name="next" value="'.s(get_string('reload')).'" />';
00365         $first .= '<script type="text/javascript">
00366 //<![CDATA[
00367     var first = document.getElementById("previousbutton");
00368     first.style.visibility = "hidden";
00369 //]]>
00370 </script>
00371 ';
00372     }
00373 
00374     if ($reload) {
00375         $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('reload')).'" />';
00376     } else {
00377         $next = '<input type="submit" id="nextbutton" name="next" value="'.s(get_string('next')).' &raquo;" />';
00378     }
00379 
00380     echo '</fieldset><fieldset id="nav_buttons">'.$first.$next.'</fieldset>';
00381 
00382     $homelink  = '<div class="sitelink">'.
00383        '<a title="Moodle '. $CFG->target_release .'" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">'.
00384        '<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
00385 
00386     echo '</form></div>';
00387     echo '<div id="footer"><hr />'.$homelink.'</div>';
00388     echo '</div></body></html>';
00389 }
00390 
00399 function install_cli_database(array $options, $interactive) {
00400     global $CFG, $DB;
00401     require_once($CFG->libdir.'/environmentlib.php');
00402     require_once($CFG->libdir.'/upgradelib.php');
00403 
00404     // show as much debug as possible
00405     @error_reporting(1023);
00406     @ini_set('display_errors', '1');
00407     $CFG->debug = 38911;
00408     $CFG->debugdisplay = true;
00409 
00410     $CFG->version = '';
00411     $CFG->release = '';
00412     $version = null;
00413     $release = null;
00414 
00415     // read $version and $release
00416     require($CFG->dirroot.'/version.php');
00417 
00418     if ($DB->get_tables() ) {
00419         cli_error(get_string('clitablesexist', 'install'));
00420     }
00421 
00422     if (empty($options['adminpass'])) {
00423         cli_error('Missing required admin password');
00424     }
00425 
00426     // test environment first
00427     list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
00428     if (!$envstatus) {
00429         $errors = environment_get_errors($environment_results);
00430         cli_heading(get_string('environment', 'admin'));
00431         foreach ($errors as $error) {
00432             list($info, $report) = $error;
00433             echo "!! $info !!\n$report\n\n";
00434         }
00435         exit(1);
00436     }
00437 
00438     if (!$DB->setup_is_unicodedb()) {
00439         if (!$DB->change_db_encoding()) {
00440             // If could not convert successfully, throw error, and prevent installation
00441             cli_error(get_string('unicoderequired', 'admin'));
00442         }
00443     }
00444 
00445     if ($interactive) {
00446         cli_separator();
00447         cli_heading(get_string('databasesetup'));
00448     }
00449 
00450     // install core
00451     install_core($version, true);
00452     set_config('release', $release);
00453 
00454     // install all plugins types, local, etc.
00455     upgrade_noncore(true);
00456 
00457     // set up admin user password
00458     $DB->set_field('user', 'password', hash_internal_user_password($options['adminpass']), array('username' => 'admin'));
00459 
00460     // rename admin username if needed
00461     if (isset($options['adminuser']) and $options['adminuser'] !== 'admin' and $options['adminuser'] !== 'guest') {
00462         $DB->set_field('user', 'username', $options['adminuser'], array('username' => 'admin'));
00463     }
00464 
00465     // indicate that this site is fully configured
00466     set_config('rolesactive', 1);
00467     upgrade_finished();
00468 
00469     // log in as admin - we need do anything when applying defaults
00470     $admins = get_admins();
00471     $admin = reset($admins);
00472     session_set_user($admin);
00473 
00474     // apply all default settings, do it twice to fill all defaults - some settings depend on other setting
00475     admin_apply_default_settings(NULL, true);
00476     admin_apply_default_settings(NULL, true);
00477     set_config('registerauth', '');
00478 
00479     // set the site name
00480     if (isset($options['shortname']) and $options['shortname'] !== '') {
00481         $DB->set_field('course', 'shortname', $options['shortname'], array('format' => 'site'));
00482     }
00483     if (isset($options['fullname']) and $options['fullname'] !== '') {
00484         $DB->set_field('course', 'fullname', $options['fullname'], array('format' => 'site'));
00485     }
00486 }
 All Data Structures Namespaces Files Functions Variables Enumerations