[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/cli/ -> install.php (source)

   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/>.
  17  
  18  /**
  19   * This script creates config.php file and prepares database.
  20   *
  21   * This script is not intended for beginners!
  22   * Potential problems:
  23   * - su to apache account or sudo before execution
  24   * - not compatible with Windows platform
  25   *
  26   * @package    core
  27   * @subpackage cli
  28   * @copyright  2009 Petr Skoda (http://skodak.org)
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  
  32  define('CLI_SCRIPT', true);
  33  
  34  // extra execution prevention - we can not just require config.php here
  35  if (isset($_SERVER['REMOTE_ADDR'])) {
  36      exit(1);
  37  }
  38  
  39  // Force OPcache reset if used, we do not want any stale caches
  40  // when preparing test environment.
  41  if (function_exists('opcache_reset')) {
  42      opcache_reset();
  43  }
  44  
  45  $help =
  46  "Command line Moodle installer, creates config.php and initializes database.
  47  Please note you must execute this script with the same uid as apache
  48  or use chmod/chown after installation.
  49  
  50  Site defaults may be changed via local/defaults.php.
  51  
  52  Options:
  53  --chmod=OCTAL-MODE    Permissions of new directories created within dataroot.
  54                        Default is 2777. You may want to change it to 2770
  55                        or 2750 or 750. See chmod man page for details.
  56  --lang=CODE           Installation and default site language.
  57  --wwwroot=URL         Web address for the Moodle site,
  58                        required in non-interactive mode.
  59  --dataroot=DIR        Location of the moodle data folder,
  60                        must not be web accessible. Default is moodledata
  61                        in the parent directory.
  62  --dbtype=TYPE         Database type. Default is mysqli
  63  --dbhost=HOST         Database host. Default is localhost
  64  --dbname=NAME         Database name. Default is moodle
  65  --dbuser=USERNAME     Database user. Default is root
  66  --dbpass=PASSWORD     Database password. Default is blank
  67  --dbport=NUMBER       Use database port.
  68  --dbsocket=PATH       Use database socket, 1 means default. Available for some databases only.
  69  --prefix=STRING       Table prefix for above database tables. Default is mdl_
  70  --fullname=STRING     The fullname of the site
  71  --shortname=STRING    The shortname of the site
  72  --adminuser=USERNAME  Username for the moodle admin account. Default is admin
  73  --adminpass=PASSWORD  Password for the moodle admin account,
  74                        required in non-interactive mode.
  75  --non-interactive     No interactive questions, installation fails if any
  76                        problem encountered.
  77  --agree-license       Indicates agreement with software license,
  78                        required in non-interactive mode.
  79  --allow-unstable      Install even if the version is not marked as stable yet,
  80                        required in non-interactive mode.
  81  -h, --help            Print out this help
  82  
  83  Example:
  84  \$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
  85  "; //TODO: localize, mark as needed in install - to be translated later when everything is finished
  86  
  87  
  88  // distro specific customisation
  89  $distrolibfile = dirname(dirname(dirname(__FILE__))).'/install/distrolib.php';
  90  $distro = null;
  91  if (file_exists($distrolibfile)) {
  92      require_once($distrolibfile);
  93      if (function_exists('distro_get_config')) {
  94          $distro = distro_get_config();
  95      }
  96  }
  97  
  98  // Nothing to do if config.php exists
  99  $configfile = dirname(dirname(dirname(__FILE__))).'/config.php';
 100  if (file_exists($configfile)) {
 101      require($configfile);
 102      require_once($CFG->libdir.'/clilib.php');
 103      list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help'));
 104  
 105      if ($options['help']) {
 106          echo $help;
 107          echo "\n\n";
 108      }
 109  
 110      if ($DB->get_manager()->table_exists('config')) {
 111          cli_error(get_string('clialreadyinstalled', 'install'));
 112      } else {
 113          cli_error(get_string('clialreadyconfigured', 'install'));
 114      }
 115  }
 116  
 117  $olddir = getcwd();
 118  
 119  // change directory so that includes below work properly
 120  chdir(dirname($_SERVER['argv'][0]));
 121  
 122  // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
 123  // This is a quick hack.  Ideally we should ask the admin for a value.  See MDL-22625 for more on this.
 124  if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
 125      @date_default_timezone_set(@date_default_timezone_get());
 126  }
 127  
 128  // make sure PHP errors are displayed - helps with diagnosing of problems
 129  @error_reporting(E_ALL);
 130  @ini_set('display_errors', '1');
 131  // we need a lot of memory
 132  @ini_set('memory_limit', '128M');
 133  
 134  /** Used by library scripts to check they are being called by Moodle */
 135  define('MOODLE_INTERNAL', true);
 136  
 137  // Disables all caching.
 138  define('CACHE_DISABLE_ALL', true);
 139  
 140  define('PHPUNIT_TEST', false);
 141  
 142  define('IGNORE_COMPONENT_CACHE', true);
 143  
 144  // Check that PHP is of a sufficient version
 145  if (version_compare(phpversion(), "5.4.4") < 0) {
 146      $phpversion = phpversion();
 147      // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
 148      fwrite(STDERR, "Moodle 2.7 or later requires at least PHP 5.4.4 (currently using version $phpversion).\n");
 149      fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
 150      exit(1);
 151  }
 152  
 153  // set up configuration
 154  global $CFG;
 155  $CFG = new stdClass();
 156  $CFG->lang                 = 'en';
 157  $CFG->dirroot              = dirname(dirname(dirname(__FILE__)));
 158  $CFG->libdir               = "$CFG->dirroot/lib";
 159  $CFG->wwwroot              = "http://localhost";
 160  $CFG->httpswwwroot         = $CFG->wwwroot;
 161  $CFG->docroot              = 'http://docs.moodle.org';
 162  $CFG->running_installer    = true;
 163  $CFG->early_install_lang   = true;
 164  $CFG->ostype               = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
 165  $CFG->dboptions            = array();
 166  $CFG->debug                = (E_ALL | E_STRICT);
 167  $CFG->debugdisplay         = true;
 168  $CFG->debugdeveloper       = true;
 169  
 170  $parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__))));
 171  $CFG->admin                = array_pop($parts);
 172  
 173  //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
 174  //the problem is that we need specific version of quickforms and hacked excel files :-(
 175  ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
 176  
 177  require_once($CFG->libdir.'/classes/component.php');
 178  require_once($CFG->libdir.'/classes/text.php');
 179  require_once($CFG->libdir.'/classes/string_manager.php');
 180  require_once($CFG->libdir.'/classes/string_manager_install.php');
 181  require_once($CFG->libdir.'/classes/string_manager_standard.php');
 182  require_once($CFG->libdir.'/installlib.php');
 183  require_once($CFG->libdir.'/clilib.php');
 184  require_once($CFG->libdir.'/setuplib.php');
 185  require_once($CFG->libdir.'/weblib.php');
 186  require_once($CFG->libdir.'/dmllib.php');
 187  require_once($CFG->libdir.'/moodlelib.php');
 188  require_once($CFG->libdir.'/deprecatedlib.php');
 189  require_once($CFG->libdir.'/adminlib.php');
 190  require_once($CFG->libdir.'/componentlib.class.php');
 191  require_once($CFG->dirroot.'/cache/lib.php');
 192  
 193  // Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
 194  // Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes.
 195  if (defined('COMPONENT_CLASSLOADER')) {
 196      spl_autoload_register(COMPONENT_CLASSLOADER);
 197  } else {
 198      spl_autoload_register('core_component::classloader');
 199  }
 200  
 201  require($CFG->dirroot.'/version.php');
 202  $CFG->target_release = $release;
 203  
 204  \core\session\manager::init_empty_session();
 205  global $SESSION;
 206  global $USER;
 207  
 208  global $COURSE;
 209  $COURSE = new stdClass();
 210  $COURSE->id = 1;
 211  
 212  global $SITE;
 213  $SITE = $COURSE;
 214  define('SITEID', 1);
 215  
 216  //Database types
 217  $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
 218                     'mariadb'=> moodle_database::get_driver_instance('mariadb', 'native'),
 219                     'pgsql'  => moodle_database::get_driver_instance('pgsql',  'native'),
 220                     'oci'    => moodle_database::get_driver_instance('oci',    'native'),
 221                     'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
 222                     'mssql'  => moodle_database::get_driver_instance('mssql',  'native'), // FreeTDS driver
 223                    );
 224  foreach ($databases as $type=>$database) {
 225      if ($database->driver_installed() !== true) {
 226          unset($databases[$type]);
 227      }
 228  }
 229  if (empty($databases)) {
 230      $defaultdb = '';
 231  } else {
 232      reset($databases);
 233      $defaultdb = key($databases);
 234  }
 235  
 236  // now get cli options
 237  list($options, $unrecognized) = cli_get_params(
 238      array(
 239          'chmod'             => isset($distro->directorypermissions) ? sprintf('%04o',$distro->directorypermissions) : '2777', // let distros set dir permissions
 240          'lang'              => $CFG->lang,
 241          'wwwroot'           => '',
 242          'dataroot'          => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__)))).'/moodledata'): $distro->dataroot, // initialised later after including libs or by distro
 243          'dbtype'            => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, // let distro skip dbtype selection
 244          'dbhost'            => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, // let distros set dbhost
 245          'dbname'            => 'moodle',
 246          'dbuser'            => empty($distro->dbuser) ? 'root' : $distro->dbuser, // let distros set dbuser
 247          'dbpass'            => '',
 248          'dbport'            => '',
 249          'dbsocket'          => '',
 250          'prefix'            => 'mdl_',
 251          'fullname'          => '',
 252          'shortname'         => '',
 253          'adminuser'         => 'admin',
 254          'adminpass'         => '',
 255          'non-interactive'   => false,
 256          'agree-license'     => false,
 257          'allow-unstable'    => false,
 258          'help'              => false
 259      ),
 260      array(
 261          'h' => 'help'
 262      )
 263  );
 264  
 265  $interactive = empty($options['non-interactive']);
 266  
 267  // set up language
 268  $lang = clean_param($options['lang'], PARAM_SAFEDIR);
 269  if (file_exists($CFG->dirroot.'/install/lang/'.$lang)) {
 270      $CFG->lang = $lang;
 271  }
 272  
 273  if ($unrecognized) {
 274      $unrecognized = implode("\n  ", $unrecognized);
 275      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
 276  }
 277  
 278  if ($options['help']) {
 279      echo $help;
 280      die;
 281  }
 282  
 283  //Print header
 284  echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n";
 285  
 286  //Fist select language
 287  if ($interactive) {
 288      cli_separator();
 289      $languages = get_string_manager()->get_list_of_translations();
 290      // Do not put the langs into columns because it is not compatible with RTL.
 291      $langlist = implode("\n", $languages);
 292      $default = $CFG->lang;
 293      cli_heading(get_string('availablelangs', 'install'));
 294      echo $langlist."\n";
 295      $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang);
 296      $error = '';
 297      do {
 298          echo $error;
 299          $input = cli_input($prompt, $default);
 300          $input = clean_param($input, PARAM_SAFEDIR);
 301  
 302          if (!file_exists($CFG->dirroot.'/install/lang/'.$input)) {
 303              $error = get_string('cliincorrectvalueretry', 'admin')."\n";
 304          } else {
 305              $error = '';
 306          }
 307      } while ($error !== '');
 308      $CFG->lang = $input;
 309  } else {
 310      // already selected and verified
 311  }
 312  
 313  // Set directorypermissions first
 314  $chmod = octdec(clean_param($options['chmod'], PARAM_INT));
 315  if ($interactive) {
 316      cli_separator();
 317      cli_heading(get_string('datarootpermission', 'install'));
 318      $prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod));
 319      $error = '';
 320      do {
 321          echo $error;
 322          $input = cli_input($prompt, decoct($chmod));
 323          $input = octdec(clean_param($input, PARAM_INT));
 324          if (empty($input)) {
 325              $error = get_string('cliincorrectvalueretry', 'admin')."\n";
 326          } else {
 327              $error = '';
 328          }
 329      } while ($error !== '');
 330      $chmod = $input;
 331  
 332  } else {
 333      if (empty($chmod)) {
 334          $a = (object)array('option' => 'chmod', 'value' => decoct($chmod));
 335          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 336      }
 337  }
 338  $CFG->directorypermissions = $chmod;
 339  $CFG->filepermissions      = ($CFG->directorypermissions & 0666);
 340  $CFG->umaskpermissions     = (($CFG->directorypermissions & 0777) ^ 0777);
 341  
 342  //We need wwwroot before we test dataroot
 343  $wwwroot = clean_param($options['wwwroot'], PARAM_URL);
 344  $wwwroot = trim($wwwroot, '/');
 345  if ($interactive) {
 346      cli_separator();
 347      cli_heading(get_string('wwwroot', 'install'));
 348      if (strpos($wwwroot, 'http') === 0) {
 349          $prompt = get_string('clitypevaluedefault', 'admin', $wwwroot);
 350      } else {
 351          $wwwroot = null;
 352          $prompt = get_string('clitypevalue', 'admin');
 353      }
 354      $error = '';
 355      do {
 356          echo $error;
 357          $input = cli_input($prompt, $wwwroot);
 358          $input = clean_param($input, PARAM_URL);
 359          $input = trim($input, '/');
 360          if (strpos($input, 'http') !== 0) {
 361              $error = get_string('cliincorrectvalueretry', 'admin')."\n";
 362          } else {
 363              $error = '';
 364          }
 365      } while ($error !== '');
 366      $wwwroot = $input;
 367  
 368  } else {
 369      if (strpos($wwwroot, 'http') !== 0) {
 370          $a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot);
 371          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 372      }
 373  }
 374  $CFG->wwwroot       = $wwwroot;
 375  $CFG->httpswwwroot  = $CFG->wwwroot;
 376  
 377  
 378  //We need dataroot before lang download
 379  $CFG->dataroot = $options['dataroot'];
 380  if ($interactive) {
 381      cli_separator();
 382      $i=0;
 383      while(is_dataroot_insecure()) {
 384          $parrent = dirname($CFG->dataroot);
 385          $i++;
 386          if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
 387              $CFG->dataroot = ''; //can not find secure location for dataroot
 388              break;
 389          }
 390          $CFG->dataroot = dirname($parrent).'/moodledata';
 391      }
 392      cli_heading(get_string('dataroot', 'install'));
 393      $error = '';
 394      do {
 395          if ($CFG->dataroot !== '') {
 396              $prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot);
 397          } else {
 398              $prompt = get_string('clitypevalue', 'admin');
 399          }
 400          echo $error;
 401          $CFG->dataroot = cli_input($prompt, $CFG->dataroot);
 402          if ($CFG->dataroot === '') {
 403              $error = get_string('cliincorrectvalueretry', 'admin')."\n";
 404          } else if (is_dataroot_insecure()) {
 405              $CFG->dataroot = '';
 406              $error = get_string('pathsunsecuredataroot', 'install')."\n";
 407          } else {
 408              if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
 409                  $error = '';
 410              } else {
 411                  $a = (object)array('dataroot' => $CFG->dataroot);
 412                  $error = get_string('pathserrcreatedataroot', 'install', $a)."\n";
 413              }
 414          }
 415  
 416      } while ($error !== '');
 417  
 418  } else {
 419      if (is_dataroot_insecure()) {
 420          cli_error(get_string('pathsunsecuredataroot', 'install'));
 421      }
 422      if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
 423          $a = (object)array('dataroot' => $CFG->dataroot);
 424          cli_error(get_string('pathserrcreatedataroot', 'install', $a));
 425      }
 426  }
 427  $CFG->tempdir       = $CFG->dataroot.'/temp';
 428  $CFG->cachedir      = $CFG->dataroot.'/cache';
 429  $CFG->localcachedir = $CFG->dataroot.'/localcache';
 430  
 431  // download required lang packs
 432  if ($CFG->lang !== 'en') {
 433      $installer = new lang_installer($CFG->lang);
 434      $results = $installer->run();
 435      foreach ($results as $langcode => $langstatus) {
 436          if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
 437              $a       = new stdClass();
 438              $a->url  = $installer->lang_pack_url($langcode);
 439              $a->dest = $CFG->dataroot.'/lang';
 440              cli_problem(get_string('remotedownloaderror', 'error', $a));
 441          }
 442      }
 443  }
 444  
 445  // switch the string_manager instance to stop using install/lang/
 446  $CFG->early_install_lang = false;
 447  $CFG->langotherroot      = $CFG->dataroot.'/lang';
 448  $CFG->langlocalroot      = $CFG->dataroot.'/lang';
 449  get_string_manager(true);
 450  
 451  // make sure we are installing stable release or require a confirmation
 452  if (isset($maturity)) {
 453      if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
 454          $maturitylevel = get_string('maturity'.$maturity, 'admin');
 455  
 456          if ($interactive) {
 457              cli_separator();
 458              cli_heading(get_string('notice'));
 459              echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
 460              echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
 461              echo get_string('continue') . PHP_EOL;
 462              $prompt = get_string('cliyesnoprompt', 'admin');
 463              $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
 464              if ($input == get_string('clianswerno', 'admin')) {
 465                  exit(1);
 466              }
 467          } else {
 468              cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
 469              cli_error(get_string('maturityallowunstable', 'admin'));
 470          }
 471      }
 472  }
 473  
 474  // ask for db type - show only drivers available
 475  if ($interactive) {
 476      $options['dbtype'] = strtolower($options['dbtype']);
 477      cli_separator();
 478      cli_heading(get_string('databasetypehead', 'install'));
 479      foreach ($databases as $type=>$database) {
 480          echo " $type \n";
 481      }
 482      if (!empty($databases[$options['dbtype']])) {
 483          $prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']);
 484      } else {
 485          $prompt = get_string('clitypevalue', 'admin');
 486      }
 487      $CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases));
 488  
 489  } else {
 490      if (empty($databases[$options['dbtype']])) {
 491          $a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']);
 492          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 493      }
 494      $CFG->dbtype = $options['dbtype'];
 495  }
 496  $database = $databases[$CFG->dbtype];
 497  
 498  
 499  // ask for db host
 500  if ($interactive) {
 501      cli_separator();
 502      cli_heading(get_string('databasehost', 'install'));
 503      if ($options['dbhost'] !== '') {
 504          $prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']);
 505      } else {
 506          $prompt = get_string('clitypevalue', 'admin');
 507      }
 508      $CFG->dbhost = cli_input($prompt, $options['dbhost']);
 509  
 510  } else {
 511      $CFG->dbhost = $options['dbhost'];
 512  }
 513  
 514  // ask for db name
 515  if ($interactive) {
 516      cli_separator();
 517      cli_heading(get_string('databasename', 'install'));
 518      if ($options['dbname'] !== '') {
 519          $prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']);
 520      } else {
 521          $prompt = get_string('clitypevalue', 'admin');
 522      }
 523      $CFG->dbname = cli_input($prompt, $options['dbname']);
 524  
 525  } else {
 526      $CFG->dbname = $options['dbname'];
 527  }
 528  
 529  // ask for db prefix
 530  if ($interactive) {
 531      cli_separator();
 532      cli_heading(get_string('dbprefix', 'install'));
 533      //TODO: solve somehow the prefix trouble for oci
 534      if ($options['prefix'] !== '') {
 535          $prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']);
 536      } else {
 537          $prompt = get_string('clitypevalue', 'admin');
 538      }
 539      $CFG->prefix = cli_input($prompt, $options['prefix']);
 540  
 541  } else {
 542      $CFG->prefix = $options['prefix'];
 543  }
 544  
 545  // ask for db port
 546  if ($interactive) {
 547      cli_separator();
 548      cli_heading(get_string('databaseport', 'install'));
 549      $prompt = get_string('clitypevaluedefault', 'admin', $options['dbport']);
 550      $CFG->dboptions['dbport'] = (int)cli_input($prompt, $options['dbport']);
 551  
 552  } else {
 553      $CFG->dboptions['dbport'] = (int)$options['dbport'];
 554  }
 555  if ($CFG->dboptions['dbport'] <= 0) {
 556      $CFG->dboptions['dbport'] = '';
 557  }
 558  
 559  // ask for db socket
 560  if ($CFG->ostype === 'WINDOWS') {
 561      $CFG->dboptions['dbsocket'] = '';
 562  
 563  } else if ($interactive and empty($CFG->dboptions['dbport'])) {
 564      cli_separator();
 565      cli_heading(get_string('databasesocket', 'install'));
 566      $prompt = get_string('clitypevaluedefault', 'admin', $options['dbsocket']);
 567      $CFG->dboptions['dbsocket'] = cli_input($prompt, $options['dbsocket']);
 568  
 569  } else {
 570      $CFG->dboptions['dbsocket'] = $options['dbsocket'];
 571  }
 572  
 573  // ask for db user
 574  if ($interactive) {
 575      cli_separator();
 576      cli_heading(get_string('databaseuser', 'install'));
 577      if ($options['dbuser'] !== '') {
 578          $prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']);
 579      } else {
 580          $prompt = get_string('clitypevalue', 'admin');
 581      }
 582      $CFG->dbuser = cli_input($prompt, $options['dbuser']);
 583  
 584  } else {
 585      $CFG->dbuser = $options['dbuser'];
 586  }
 587  
 588  // ask for db password
 589  if ($interactive) {
 590      cli_separator();
 591      cli_heading(get_string('databasepass', 'install'));
 592      do {
 593          if ($options['dbpass'] !== '') {
 594              $prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']);
 595          } else {
 596              $prompt = get_string('clitypevalue', 'admin');
 597          }
 598  
 599          $CFG->dbpass = cli_input($prompt, $options['dbpass']);
 600          if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
 601              $distro = distro_pre_create_db($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']), $distro);
 602          }
 603          $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']));
 604      } while ($hint_database !== '');
 605  
 606  } else {
 607      $CFG->dbpass = $options['dbpass'];
 608      $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']));
 609      if ($hint_database !== '') {
 610          cli_error(get_string('dbconnectionerror', 'install'));
 611      }
 612  }
 613  
 614  // ask for fullname
 615  if ($interactive) {
 616      cli_separator();
 617      cli_heading(get_string('fullsitename', 'moodle'));
 618  
 619      if ($options['fullname'] !== '') {
 620          $prompt = get_string('clitypevaluedefault', 'admin', $options['fullname']);
 621      } else {
 622          $prompt = get_string('clitypevalue', 'admin');
 623      }
 624  
 625      do {
 626          $options['fullname'] = cli_input($prompt, $options['fullname']);
 627      } while (empty($options['fullname']));
 628  } else {
 629      if (empty($options['fullname'])) {
 630          $a = (object)array('option'=>'fullname', 'value'=>$options['fullname']);
 631          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 632      }
 633  }
 634  
 635  // ask for shortname
 636  if ($interactive) {
 637      cli_separator();
 638      cli_heading(get_string('shortsitename', 'moodle'));
 639  
 640      if ($options['shortname'] !== '') {
 641          $prompt = get_string('clitypevaluedefault', 'admin', $options['shortname']);
 642      } else {
 643          $prompt = get_string('clitypevalue', 'admin');
 644      }
 645  
 646      do {
 647          $options['shortname'] = cli_input($prompt, $options['shortname']);
 648      } while (empty($options['shortname']));
 649  } else {
 650      if (empty($options['shortname'])) {
 651          $a = (object)array('option'=>'shortname', 'value'=>$options['shortname']);
 652          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 653      }
 654  }
 655  
 656  // ask for admin user name
 657  if ($interactive) {
 658      cli_separator();
 659      cli_heading(get_string('cliadminusername', 'install'));
 660      if (!empty($options['adminuser'])) {
 661          $prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']);
 662      } else {
 663          $prompt = get_string('clitypevalue', 'admin');
 664      }
 665      do {
 666          $options['adminuser'] = cli_input($prompt, $options['adminuser']);
 667      } while (empty($options['adminuser']) or $options['adminuser'] === 'guest');
 668  } else {
 669      if (empty($options['adminuser']) or $options['adminuser'] === 'guest') {
 670          $a = (object)array('option'=>'adminuser', 'value'=>$options['adminuser']);
 671          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 672      }
 673  }
 674  
 675  // ask for admin user password
 676  if ($interactive) {
 677      cli_separator();
 678      cli_heading(get_string('cliadminpassword', 'install'));
 679      $prompt = get_string('clitypevalue', 'admin');
 680      do {
 681          $options['adminpass'] = cli_input($prompt);
 682      } while (empty($options['adminpass']) or $options['adminpass'] === 'admin');
 683  } else {
 684      if (empty($options['adminpass']) or $options['adminpass'] === 'admin') {
 685          $a = (object)array('option'=>'adminpass', 'value'=>$options['adminpass']);
 686          cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 687      }
 688  }
 689  
 690  if ($interactive) {
 691      if (!$options['agree-license']) {
 692          cli_separator();
 693          cli_heading(get_string('copyrightnotice'));
 694          echo "Moodle  - Modular Object-Oriented Dynamic Learning Environment\n";
 695          echo get_string('gpl3')."\n\n";
 696          echo get_string('doyouagree')."\n";
 697          $prompt = get_string('cliyesnoprompt', 'admin');
 698          $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
 699          if ($input == get_string('clianswerno', 'admin')) {
 700              exit(1);
 701          }
 702      }
 703  } else {
 704      if (!$options['agree-license']) {
 705          cli_error(get_string('climustagreelicense', 'install'));
 706      }
 707  }
 708  
 709  // Finally we have all info needed for config.php
 710  $configphp = install_generate_configphp($database, $CFG);
 711  umask(0137);
 712  if (($fh = fopen($configfile, 'w')) !== false) {
 713      fwrite($fh, $configphp);
 714      fclose($fh);
 715  }
 716  
 717  if (!file_exists($configfile)) {
 718      cli_error('Can not create config file.');
 719  }
 720  
 721  // remember selected language
 722  $installlang = $CFG->lang;
 723  // return back to original dir before executing setup.php which changes the dir again
 724  chdir($olddir);
 725  // We have config.php, it is a real php script from now on :-)
 726  require($configfile);
 727  
 728  // use selected language
 729  $CFG->lang = $installlang;
 730  $SESSION->lang = $CFG->lang;
 731  
 732  require("$CFG->dirroot/version.php");
 733  
 734  // Test environment first.
 735  require_once($CFG->libdir . '/environmentlib.php');
 736  list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
 737  if (!$envstatus) {
 738      $errors = environment_get_errors($environment_results);
 739      cli_heading(get_string('environment', 'admin'));
 740      foreach ($errors as $error) {
 741          list($info, $report) = $error;
 742          echo "!! $info !!\n$report\n\n";
 743      }
 744      exit(1);
 745  }
 746  
 747  // Test plugin dependencies.
 748  $failed = array();
 749  if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
 750      cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
 751      cli_error(get_string('pluginschecktodo', 'admin'));
 752  }
 753  
 754  install_cli_database($options, $interactive);
 755  
 756  echo get_string('cliinstallfinished', 'install')."\n";
 757  exit(0); // 0 means success


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1