/** * Internal method to get the default replacements for multisite's URLs * * @param AngieModelWordpressConfiguration $config The configuration model * * @return array Any replacements to add */ private function getDefaultReplacementsForMultisite($config) { $replacements = array(); $db = $this->getDbo(); if (!$this->isMultisite($db)) { return $replacements; } // These values are stored inside the session, after the setup step $old_url = $config->get('oldurl'); $new_url = $config->get('homeurl'); // If the URL didn't change do nothing if ($old_url == $new_url) { return $replacements; } // Get the old and new base domain and base path $oldUri = new AUri($old_url); $newUri = new AUri($new_url); $newDomain = $this->removeSubdomain($newUri->getHost()); $oldDomain = $config->get('domain_current_site', $oldUri->getHost()); $newPath = $newUri->getPath(); $oldPath = $config->get('path_current_site', $oldUri->getPath()); // If the old and new domain are subdomains of the same root domain (e.g. abc.example.com and xyz.example.com), // or a subdomain and a root domain (e.g. example.com and abc.example.com) we MUST NOT do domain replacement $replaceSubdomains = $this->removeSubdomain($newDomain) != $this->removeSubdomain($oldDomain); // If the old and new paths are the same we MUST NOT do path replacement $replacePaths = $oldPath != $newPath; // Get the multisites information $multiSites = $this->getMultisiteMap($db); // Get other information $mainBlogId = $config->get('blog_id_current_site', 1); $useSubdomains = $config->get('subdomain_install', 0); // Do I have to replace the domain? if ($oldDomain != $newDomain) { $replacements[$oldDomain] = $newUri->getHost(); } // Maybe I have to do... nothing? if ($useSubdomains && !$replaceSubdomains) { return $replacements; } if (!$useSubdomains) { if (!$replacePaths) { return $replacements; } } // Loop for each multisite foreach ($multiSites as $blogId => $info) { // Skip the first site, it is the same as the main site if ($blogId == $mainBlogId) { continue; } // Multisites using subdomains? if ($useSubdomains) { // Extract the subdomain $subdomain = substr($info['domain'], 0, -strlen($oldDomain)); // Add a replacement for this domain $replacements[$info['domain']] = $subdomain . $newDomain; continue; } // Multisites using subdirectories. Let's check if I have to extract the old path. $path = strpos($info['path'], $oldPath) === 0 ? substr($info['path'], strlen($oldPath)) : $info['path']; // Construct the new path and add it to the list of replacements $path = trim($path, '/'); $newMSPath = $newPath . '/' . $path; $newMSPath = trim($newMSPath, '/'); $replacements[$info['path']] = '/' . $newMSPath; } // Important! We have to change subdomains BEFORE the main domain. And for this, we need to reverse the // replacements table. If you're wondering why: old domain example.com, new domain www.example.net. This // makes blog1.example.com => blog1.www.example.net instead of blog1.example.net (note the extra www). Oops! $replacements = array_reverse($replacements); return $replacements; }
/** * 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 . '/wp-config.php'; } $new_config = ''; $old_config = file_get_contents($file); // Check if the file is UTF encoded with BOM. We have to remove it or we will get a white page // Sadly several editors are setting the flag automatically; since they are not visible, the user has // no easy method to remove them $bom = pack("CCC", 0xef, 0xbb, 0xbf); if (strncmp($old_config, $bom, 3) === 0) { // Let's strip out any BOM char $old_config = substr($old_config, 3); } $lines = explode("\n", $old_config); foreach ($lines as $line) { $line = trim($line); $matches = array(); // Skip commented lines. However it will get the line between a multiline comment, but that's not a problem if (strpos($line, '#') === 0 || strpos($line, '//') === 0 || strpos($line, '/*') === 0) { // simply do nothing, we will add the line later } elseif (strpos($line, 'define(') !== false) { preg_match('#define\\(["\'](.*?)["\']\\,#', $line, $matches); if (isset($matches[1])) { $key = $matches[1]; switch (strtoupper($key)) { case 'DB_NAME': $value = $this->get('dbname'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'DB_USER': $value = $this->get('dbuser'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'DB_PASSWORD': $value = $this->get('dbpass'); $value = addcslashes($value, "'\\"); $line = "define('" . $key . "', '" . $value . "');"; break; case 'DB_HOST': $value = $this->get('dbhost'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'DB_CHARSET': $value = $this->get('dbcharset'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'DB_COLLATE': $value = $this->get('dbcollate', 'utf8_general_ci'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'AUTH_KEY': $value = $this->get('auth_key'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'SECURE_AUTH_KEY': $value = $this->get('secure_auth_key'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'LOGGED_IN_KEY': $value = $this->get('logged_in_key'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'NONCE_KEY': $value = $this->get('nonce_key'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'AUTH_SALT': $value = $this->get('auth_salt'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'SECURE_AUTH_SALT': $value = $this->get('secure_auth_salt'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'LOGGED_IN_SALT': $value = $this->get('logged_in_salt'); $line = "define('" . $key . "', '" . $value . "');"; break; case 'NONCE_SALT': $value = $this->get('nonce_salt'); $line = "define('" . $key . "', '" . $value . "');"; break; // Multisite variable - Main site's domain // Multisite variable - Main site's domain case 'DOMAIN_CURRENT_SITE': $new_url = $this->get('homeurl'); $newUri = new AUri($new_url); $newDomain = $newUri->getHost(); $line = "define('" . $key . "', '" . $newDomain . "');"; break; // Multisite variable - Main site's path // Multisite variable - Main site's path case 'PATH_CURRENT_SITE': $new_url = $this->get('homeurl'); $newUri = new AUri($new_url); $newPath = $newUri->getPath(); $newPath = trim($newPath, '/'); $newPath = '/' . $newPath . '/'; $line = "define('" . $key . "', '" . $newPath . "');"; break; // I think users shouldn't change the WPLANG define, since they will have // to add several files, it's not automatic // I think users shouldn't change the WPLANG define, since they will have // to add several files, it's not automatic default: // Do nothing, it's a variable we're not interested in break; } } } elseif (strpos($line, '$table_prefix') === 0) { $line = '$table_prefix = ' . "'" . $this->get('dbprefix') . "';"; } $new_config .= $line . "\n"; } return $new_config; }