/** * 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; }
/** * Internal method to get the default replacements for the main site URL * * @param AngieModelBaseConfiguration $config The configuration model * * @return array Any replacements to add */ private function getDefaultReplacementsForMainSite($config) { $replacements = array(); // These values are stored inside the session, after the setup step $old_url = $config->get('old_live_site'); $new_url = $config->get('live_site'); if ($old_url == $new_url) { return $replacements; } // Replace the absolute URL to the site $replacements[$old_url] = $new_url; // If the relative path to the site is different, replace it too. $oldUri = new AUri($old_url); $newUri = new AUri($new_url); $oldPath = $oldUri->getPath(); $newPath = $newUri->getPath(); if ($oldPath != $newPath) { $replacements[$oldPath] = $newPath; return $replacements; } return $replacements; }
public function updatehtaccess() { // Let's build the stack of possible files $files = array(APATH_ROOT . '/.htaccess', APATH_ROOT . '/htaccess.bak'); // Do I want to give more importance to .bak file first? if ($this->input->getInt('bak_first', 0)) { rsort($files); } $fileName = false; foreach ($files as $file) { // Did I found what I'm looking for? if (file_exists($file)) { $fileName = $file; break; } } // No file? Let's stop here if (!$fileName) { return true; } // Get the site's URL /** @var AngieModelWordpressConfiguration $config */ $config = AModel::getAnInstance('Configuration', 'AngieModel', array(), $this->container); $new_url = $config->get('siteurl'); $homeurl = $config->get('homeurl'); $newURI = new AUri($new_url); $path = $newURI->getPath(); // Load the .htaccess in memory $contents = @file_get_contents($fileName); if ($contents === false) { return false; } // Explode its lines $lines = explode("\n", $contents); $contents = ''; $inSection = null; foreach ($lines as $line) { // Fix naughty Windows users' doing $line = rtrim($line, "\r"); // If we are not inside the WordPress section look for the BEGIN signature if (is_null($inSection)) { if (strpos($line, '# BEGIN WordPress') === 0) { $inSection = true; } } elseif ($inSection) { if (strpos($line, '# END WordPress') === 0) { $inSection = false; } elseif (strpos($line, 'RewriteBase ') === 0) { $line = "RewriteBase {$path}"; // If the site is hosted on the domain's root if (empty($path)) { $line = "RewriteBase /"; } } elseif (strpos($line, 'RewriteRule .') === 0) { $line = 'RewriteRule . ' . $path . '/index.php [L]'; } } // Add the line $contents .= "{$line}\n"; } // Write the new .htaccess $fileName = APATH_ROOT . '/.htaccess'; file_put_contents($fileName, $contents); // If the homeurl and siteurl don't match, copy the .htaccess file and index.php in the correct directory if ($new_url != $homeurl) { $homeUri = new AUri($homeurl); $homePath = $homeUri->getPath(); if (strpos($path, $homePath) !== 0) { // I have no clue where to put the files so I'll do nothing at all :s return true; } // $homePath is WITHOUT /wordpress_dir (/foobar); $path is the one WITH /wordpress_dir (/foobar/wordpress_dir) $homePath = ltrim($homePath, '/\\'); $path = ltrim($path, '/\\'); $homeParts = explode('/', $homePath); $siteParts = explode('/', $path); $numHomeParts = count($homeParts); $siteParts = array_slice($siteParts, $numHomeParts); // Relative path from HOME to SITE (WP) root $relPath = implode('/', $siteParts); // How many directories above the root (where we are restoring) is our site's root $levelsUp = count($siteParts); // Determine the path where the index.php and .htaccess files will be written to $targetPath = APATH_ROOT . str_repeat('/..', $levelsUp); $targetPath = realpath($targetPath) ? realpath($targetPath) : $targetPath; // Copy the files if (!@copy(APATH_ROOT . '/.htaccess', $targetPath . '/.htaccess')) { return false; } if (!@copy(APATH_ROOT . '/index.php', $targetPath . '/index.php')) { return false; } // Edit the index.php file $fileName = $targetPath . '/index.php'; $fileContents = @file($fileName); if (empty($fileContents)) { return false; } foreach ($fileContents as $index => $line) { $line = trim($line); if (strstr($line, 'wp-blog-header.php') && strpos($line, 'require') === 0) { $line = "require( dirname( __FILE__ ) . '/{$relPath}/wp-blog-header.php' );"; } $fileContents[$index] = $line; } $fileContents = implode("\n", $fileContents); @file_put_contents($fileName, $fileContents); } return true; }
/** * 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; }