/** * Returns content of config.php file. * * Uses PHP_EOL for generating proper end of lines for the given platform. * * @param moodle_database $database database instance * @param object $cfg copy of $CFG * @return string */ function install_generate_configphp($database, $cfg) { $configphp = '<?php // Moodle configuration file' . PHP_EOL . PHP_EOL; $configphp .= 'unset($CFG);' . PHP_EOL; $configphp .= 'global $CFG;' . PHP_EOL; $configphp .= '$CFG = new stdClass();' . PHP_EOL . PHP_EOL; // prevent PHP5 strict warnings $dbconfig = $database->export_dbconfig(); foreach ($dbconfig as $key => $value) { $key = str_pad($key, 9); $configphp .= '$CFG->' . $key . ' = ' . var_export($value, true) . ';' . PHP_EOL; } $configphp .= PHP_EOL; $configphp .= '$CFG->wwwroot = ' . var_export($cfg->wwwroot, true) . ';' . PHP_EOL; $configphp .= '$CFG->dataroot = ' . var_export($cfg->dataroot, true) . ';' . PHP_EOL; $configphp .= '$CFG->admin = ' . var_export($cfg->admin, true) . ';' . PHP_EOL . PHP_EOL; if (empty($cfg->directorypermissions)) { $chmod = '02777'; } else { $chmod = '0' . decoct($cfg->directorypermissions); } $configphp .= '$CFG->directorypermissions = ' . $chmod . ';' . PHP_EOL . PHP_EOL; $configphp .= '$CFG->passwordsaltmain = ' . var_export(complex_random_string(), true) . ';' . PHP_EOL . PHP_EOL; $configphp .= 'require_once(dirname(__FILE__) . \'/lib/setup.php\');' . PHP_EOL . PHP_EOL; $configphp .= '// There is no php closing tag in this file,' . PHP_EOL; $configphp .= '// it is intentional because it prevents trailing whitespace problems!' . PHP_EOL; return $configphp; }
/** * Test function for creation of complex random strings. */ public function test_complex_random_string() { $pool = preg_quote('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#%^&*()_+-=[];,./<>?:{} ', '/'); $result = complex_random_string(10); $this->assertSame(10, strlen($result)); $this->assertRegExp('/^[' . $pool . ']+$/', $result); $this->assertNotSame($result, complex_random_string(10)); $result = complex_random_string(21); $this->assertSame(21, strlen($result)); $this->assertRegExp('/^[' . $pool . ']+$/', $result); $this->assertNotSame($result, complex_random_string(21)); $result = complex_random_string(666); $this->assertSame(666, strlen($result)); $this->assertRegExp('/^[' . $pool . ']+$/', $result); $result = complex_random_string(); $this->assertEquals(28, strlen($result), '', 4); // Expected length is 24 - 32. $this->assertRegExp('/^[' . $pool . ']+$/', $result); $this->assertDebuggingNotCalled(); $result = complex_random_string(0); $this->assertSame('', $result); $this->assertDebuggingCalled(); $result = complex_random_string(-1); $this->assertSame('', $result); $this->assertDebuggingCalled(); }
/** * Returns a random string to be used as the authorization token * * @return string */ protected function generate_password() { return complex_random_string(); }
$str .= '$CFG->dbname = \'' . $INSTALL['dbname'] . "';\r\n"; // support single quotes in db user/passwords $str .= '$CFG->dbuser = \'' . addsingleslashes($INSTALL['dbuser']) . "';\r\n"; $str .= '$CFG->dbpass = \'' . addsingleslashes($INSTALL['dbpass']) . "';\r\n"; } $str .= '$CFG->dbpersist = false;' . "\r\n"; $str .= '$CFG->prefix = \'' . $INSTALL['prefix'] . "';\r\n"; $str .= "\r\n"; $str .= '$CFG->wwwroot = \'' . s($INSTALL['wwwrootform'], true) . "';\r\n"; $str .= '$CFG->dirroot = \'' . s($INSTALL['dirrootform'], true) . "';\r\n"; $str .= '$CFG->dataroot = \'' . s($INSTALL['dataroot'], true) . "';\r\n"; $str .= '$CFG->admin = \'' . s($INSTALL['admindirname'], true) . "';\r\n"; $str .= "\r\n"; $str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode' . "\r\n"; $str .= "\r\n"; $str .= '$CFG->passwordsaltmain = \'' . addsingleslashes(complex_random_string()) . '\';' . "\r\n"; $str .= "\r\n"; $str .= 'require_once("$CFG->dirroot/lib/setup.php");' . "\r\n"; $str .= '// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,' . "\r\n"; $str .= '// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.' . "\r\n"; $str .= '?>'; umask(0137); if (($configsuccess = $fh = @fopen($configfile, 'w')) !== false) { fwrite($fh, $str); fclose($fh); } $INSTALL['config'] = $str; } //==========================================================================// ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">