public function updateAliases($aliases, $directories) { // Initialise to empty, so we can blank them out if we don't want them $sites = ''; if ($aliases && $directories) { $aliases = str_replace("\r", '', $aliases); $directories = str_replace("\r", '', $directories); $aliases = explode("\n", $aliases); $directories = explode("\n", $directories); // Aliases and directories don't match? Stop here if (count($aliases) != count($directories)) { return; } $sites = "\$sites = array(\n"; for ($i = 0; $i < count($aliases); $i++) { $sites .= " '" . $aliases[$i] . "' => '" . $directories[$i] . "',\n"; } $sites .= ");\n"; } $replace_sites[] = $sites; $contents = file_get_contents(APATH_SITE . '/sites/sites.php'); $out = $contents; $tokenizer = new AUtilsPhptokenizer($contents); $error = false; $skip = 0; while (!$error) { try { // First time I really want to replace data, in the next loops I simply want to wipe out everything if ($replace_sites) { $replace = array_shift($replace_sites); } else { $replace = ''; } $out = $tokenizer->replaceToken('T_VARIABLE', '$sites', $skip, $replace); $tokenizer->setCode($out); $info = $tokenizer->searchToken('T_VARIABLE', '$sites', $skip); $skip = $info['endLine'] + 1; } catch (RuntimeException $e) { $error = true; } } file_put_contents(APATH_SITE . '/sites/sites.php', $out); }
/** * Creates the string that will be put inside the new configuration file. * This is a separate function so we can show the content if we're unable to write to the filesystem * and ask the user to manually do that. */ public function getFileContents($file = null) { if (!$file) { $file = APATH_ROOT . '/sites/default/settings.php'; } $out = file_get_contents($file); // First of all let's write the database info section $tokenizer = new AUtilsPhptokenizer($out); $key = $this->input->getCmd('substep', 'default'); $dbDriver = $this->get('dbtype', null, $key); $dbDriver = strtolower($dbDriver); if (in_array($dbDriver, array('mysql', 'mysqli', 'pdomysql'))) { // There's only a "mysql" driver in Drupal $dbDriver = 'mysql'; } $replace_db[] = <<<PHP \$databases['default']['default'] = array( 'driver' => '{$dbDriver}', 'database' => '{$this->get('db', null, $key)}', 'username' => '{$this->get('user', null, $key)}', 'password' => '{$this->get('password', null, $key)}', 'host' => '{$this->get('host', null, $key)}', 'prefix' => '{$this->get('dbprefix', null, $key)}', ); PHP; $skip = 0; $error = false; // In Drupal you can have several database configuration (one master + several slaves). Of course we can't modify // those info, too, so we're going to wipe out everything and use only the master configuration while (!$error) { try { // First time I really want to replace data, in the next loops I simply want to wipe out everything if ($replace_db) { $replace = array_shift($replace_db); } else { $replace = ''; } $out = $tokenizer->replaceToken('T_VARIABLE', '$databases', $skip, $replace); $tokenizer->setCode($out); $info = $tokenizer->searchToken('T_VARIABLE', '$databases', $skip); $skip = $info['endLine'] + 1; } catch (RuntimeException $e) { $error = true; } } // Now let's try to change some other params stored inside the settings file $random = new AUtilsRandval(); // New Salt $new_salt = '$drupal_hash_salt = \'' . substr(base64_encode($random->generate(43)), 0, 43) . '\';'; $out = $tokenizer->replaceToken('T_VARIABLE', '$drupal_hash_salt', 0, $new_salt); $tokenizer->setCode($out); // --- Base Url $old_url = $this->get('old_live_site', null, $key); $new_url = $this->get('live_site', null, $key); // Previously there was a base url and now there isn't if ($old_url && !$new_url) { $out = $tokenizer->replaceToken('T_VARIABLE', '$base_url', 0, '# $base_url = \'\';'); } elseif ($old_url && $new_url && $old_url != $new_url) { $out = $tokenizer->replaceToken('T_VARIABLE', '$base_url', 0, '$base_url = \'' . $new_url . '\';'); } elseif (!$old_url && $new_url) { $out .= "\n" . '$base_url = \'' . $new_url . '\';'; } $tokenizer->setCode($out); // --- Cookie domain $old_domain = $this->get('old_cookie_domain', null, $key); $new_domain = $this->get('cookie_domain', null, $key); // Previously there was a cookie domain and now there isn't if ($old_domain && !$new_domain) { $out = $tokenizer->replaceToken('T_VARIABLE', '$cookie_domain', 0, '# $cookie_domain = \'\';'); } elseif ($old_domain && $new_domain && $old_domain != $new_domain) { $out = $tokenizer->replaceToken('T_VARIABLE', '$cookie_domain', 0, '$cookie_domain = \'' . $new_domain . '\';'); } elseif (!$old_domain && $new_domain) { $out .= "\n" . '$cookie_domain = \'' . $new_domain . '\';'; } return $out; }