Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #13 from veloper/phpunit_with_composer

Phpunit with composer
  • Loading branch information...
commit 2b710f20cd0920d7ac4ace47ffc16e8030543157 2 parents da2424b + 393280d
@veloper authored
View
1  .gitignore
@@ -0,0 +1 @@
+vendor/
View
44 README.md
@@ -1,49 +1,45 @@
# WordPress Domain Changer
-## Purpose
-
-A self-contained script/tool developed to help ease migration of WordPress sites from one domain to another.
+A dependency-free tool developed to help ease migration of WordPress sites from one domain to another.
## Usage
-Please Visit [This Page](http://dan.doezema.com/2010/04/wordpress-domain-change/) for a complete overview.
-
1. Backup your WordPress database.
2. Seriously, [Back Up Your Database!](http://codex.wordpress.org/Backing_Up_Your_Database)
3. Situation One: You have a new server where you intend to upload your existing WordPress site files.
- 1. Export the current WordPress database data into a sql dump file.
- 2. Create a MySQL database on the new server.
- 3. Import the WordPress database dump file into the newly created database.
- 4. Open up the wp-config.php file and set DB\_HOST, DB\_USER, DB\_PASSWORD, and DB\_NAME to the correct values for the new server.
- 5. Upload the WordPress directory contents to the domain directory on the new server.
+ 1. Export the current WordPress database data into a SQL dump file.
+ 2. Create a MySQL database on the new server.
+ 3. Import the WordPress database dump file into the newly created database.
+ 4. Open up the wp-config.php file and set the `DB_HOST`, `DB_USER`, `DB_PASSWORD`, and `DB_NAME` constants to the correct values for the new server.
+ 5. Upload the WordPress directory contents to the domain directory on the new server.
4. Situation Two: The WordPress files and database locations have not changed — just the domain name.
- 1. Skip to step 5.
-5. Open up wp-change-domain.php in a text editor and scroll down to the “CONFIG” section.
-6. Under “Authentication Password” replace the default password with a VERY secure password of your choice.
-7. Upload wp-change-domain.php to the root directory of the WordPress site.
- 1. the root directory is where the wp-config.php is located.
-8. In a web browser go to: http://www.yourNewDomain.com/wp-change-domain.php
-9. Type in your password that you set in step 6 at the authentication prompt.
-10. You will now be presented with the domain changer form.
- 1. The script will try and auto-detect all of the settings, but it’s up to you to confirm they are all correct.
-11. Take one last look at the settings to verify that they are correct… then click the “Submit!” button.
-12. Go to your site’s home page at the new domain — all should be working!
-13. Once the domain has been changed remove this script from the server!
+ * Coninue on to Step 5...
+5. Open up `wpdc/config.php` file in a text editor and replace the default password with a **VERY** secure password of your choice.
+6. Upload the entire `wpdc/` directory to the root directory of your WordPress site.
+ * _Note:_ The root directory is where the wp-config.php is located.
+7. In a web browser go to: `http://www.your-new-domain.com/wpdc`
+8. Type in the password that you set in step 6 at the authentication prompt.
+9. You will now be presented with the domain changer form.
+ 1. The script will try and auto-detect all of the settings, but it’s up to you to confirm they are all correct.
+10. Take one last look at the settings to verify that they are correct... then click the "Change Domain!"" button.
+11. Go to your site’s home page at the new domain — all should be working!
+12. Once the domain has been changed remove this `wpdc/` directory from the server!
## Website
-http://dan.doezema.com/2010/04/wordpress-domain-change/
+[Blog Post Overview](http://dan.doezema.com/2010/04/wordpress-domain-change/)
## License
Wordpress Domain Changer is released under the New BSD license.
+
http://dan.doezema.com/licenses/new-bsd/
## Author
[Daniel Doezema](http://dan.doezema.com)
-## Contributors
+## Contributors
* [Kevin deLeon](http://www.kevin-deleon.com/)
* [mike-rsi](https://github.com/mike-rsi)
View
1  VERSION
@@ -0,0 +1 @@
+0.1.0
View
10 composer.json
@@ -0,0 +1,10 @@
+{
+ "require-dev": {
+ "phpunit/phpunit": "3.7.14"
+ },
+ "autoload": {
+ "psr-0": {
+ "wpdc": ""
+ }
+ }
+}
View
433 composer.lock
@@ -0,0 +1,433 @@
+{
+ "_readme": [
+ "This file locks the dependencies of your project to a known state",
+ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
+ ],
+ "hash": "b27fbcf0d39a48b831877abdee9f2bab",
+ "packages": [
+
+ ],
+ "packages-dev": [
+ {
+ "name": "phpunit/php-code-coverage",
+ "version": "1.2.16",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/php-code-coverage.git",
+ "reference": "69e55e68481cf708a6db43aff0b504e31402fe27"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/69e55e68481cf708a6db43aff0b504e31402fe27",
+ "reference": "69e55e68481cf708a6db43aff0b504e31402fe27",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3",
+ "phpunit/php-file-iterator": ">=1.3.0@stable",
+ "phpunit/php-text-template": ">=1.2.0@stable",
+ "phpunit/php-token-stream": ">=1.1.3@stable"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "3.7.*@dev"
+ },
+ "suggest": {
+ "ext-dom": "*",
+ "ext-xdebug": ">=2.0.5"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.2.x-dev"
+ }
+ },
+ "autoload": {
+ "classmap": [
+ "PHP/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ ""
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sb@sebastian-bergmann.de",
+ "role": "lead"
+ }
+ ],
+ "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.",
+ "homepage": "https://github.com/sebastianbergmann/php-code-coverage",
+ "keywords": [
+ "coverage",
+ "testing",
+ "xunit"
+ ],
+ "time": "2014-02-25 03:34:05"
+ },
+ {
+ "name": "phpunit/php-file-iterator",
+ "version": "1.3.4",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/php-file-iterator.git",
+ "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb",
+ "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3"
+ },
+ "type": "library",
+ "autoload": {
+ "classmap": [
+ "File/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ ""
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sb@sebastian-bergmann.de",
+ "role": "lead"
+ }
+ ],
+ "description": "FilterIterator implementation that filters files based on a list of suffixes.",
+ "homepage": "https://github.com/sebastianbergmann/php-file-iterator/",
+ "keywords": [
+ "filesystem",
+ "iterator"
+ ],
+ "time": "2013-10-10 15:34:57"
+ },
+ {
+ "name": "phpunit/php-text-template",
+ "version": "1.2.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/php-text-template.git",
+ "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/206dfefc0ffe9cebf65c413e3d0e809c82fbf00a",
+ "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3"
+ },
+ "type": "library",
+ "autoload": {
+ "classmap": [
+ "Text/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ ""
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sb@sebastian-bergmann.de",
+ "role": "lead"
+ }
+ ],
+ "description": "Simple template engine.",
+ "homepage": "https://github.com/sebastianbergmann/php-text-template/",
+ "keywords": [
+ "template"
+ ],
+ "time": "2014-01-30 17:20:04"
+ },
+ {
+ "name": "phpunit/php-timer",
+ "version": "1.0.5",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/php-timer.git",
+ "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/19689d4354b295ee3d8c54b4f42c3efb69cbc17c",
+ "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3"
+ },
+ "type": "library",
+ "autoload": {
+ "classmap": [
+ "PHP/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ ""
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sb@sebastian-bergmann.de",
+ "role": "lead"
+ }
+ ],
+ "description": "Utility class for timing",
+ "homepage": "https://github.com/sebastianbergmann/php-timer/",
+ "keywords": [
+ "timer"
+ ],
+ "time": "2013-08-02 07:42:54"
+ },
+ {
+ "name": "phpunit/php-token-stream",
+ "version": "1.2.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/php-token-stream.git",
+ "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/5220af2a7929aa35cf663d97c89ad3d50cf5fa3e",
+ "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e",
+ "shasum": ""
+ },
+ "require": {
+ "ext-tokenizer": "*",
+ "php": ">=5.3.3"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.2-dev"
+ }
+ },
+ "autoload": {
+ "classmap": [
+ "PHP/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ ""
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sb@sebastian-bergmann.de",
+ "role": "lead"
+ }
+ ],
+ "description": "Wrapper around PHP's tokenizer extension.",
+ "homepage": "https://github.com/sebastianbergmann/php-token-stream/",
+ "keywords": [
+ "tokenizer"
+ ],
+ "time": "2013-09-13 04:58:23"
+ },
+ {
+ "name": "phpunit/phpunit",
+ "version": "3.7.14",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/phpunit.git",
+ "reference": "84750788a409c3d8236153417dc7ca155a4713d2"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/84750788a409c3d8236153417dc7ca155a4713d2",
+ "reference": "84750788a409c3d8236153417dc7ca155a4713d2",
+ "shasum": ""
+ },
+ "require": {
+ "ext-dom": "*",
+ "ext-pcre": "*",
+ "ext-reflection": "*",
+ "ext-spl": "*",
+ "php": ">=5.3.3",
+ "phpunit/php-code-coverage": ">=1.2.1,<1.3.0",
+ "phpunit/php-file-iterator": ">=1.3.1",
+ "phpunit/php-text-template": ">=1.1.1",
+ "phpunit/php-timer": ">=1.0.2,<1.1.0",
+ "phpunit/phpunit-mock-objects": ">=1.2.0,<1.3.0",
+ "symfony/yaml": ">=2.1.0,<2.2.0"
+ },
+ "suggest": {
+ "ext-json": "*",
+ "ext-simplexml": "*",
+ "ext-tokenizer": "*",
+ "phpunit/php-invoker": ">=1.1.0,<1.2.0"
+ },
+ "bin": [
+ "composer/bin/phpunit"
+ ],
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "3.7.x-dev"
+ }
+ },
+ "autoload": {
+ "classmap": [
+ "PHPUnit/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ "",
+ "../../symfony/yaml/"
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sebastian@phpunit.de",
+ "role": "lead"
+ }
+ ],
+ "description": "The PHP Unit Testing framework.",
+ "homepage": "http://www.phpunit.de/",
+ "keywords": [
+ "phpunit",
+ "testing",
+ "xunit"
+ ],
+ "time": "2013-02-14 08:07:17"
+ },
+ {
+ "name": "phpunit/phpunit-mock-objects",
+ "version": "1.2.3",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git",
+ "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5794e3c5c5ba0fb037b11d8151add2a07fa82875",
+ "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3",
+ "phpunit/php-text-template": ">=1.1.1@stable"
+ },
+ "suggest": {
+ "ext-soap": "*"
+ },
+ "type": "library",
+ "autoload": {
+ "classmap": [
+ "PHPUnit/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "include-path": [
+ ""
+ ],
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sebastian Bergmann",
+ "email": "sb@sebastian-bergmann.de",
+ "role": "lead"
+ }
+ ],
+ "description": "Mock Object library for PHPUnit",
+ "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/",
+ "keywords": [
+ "mock",
+ "xunit"
+ ],
+ "time": "2013-01-13 10:24:48"
+ },
+ {
+ "name": "symfony/yaml",
+ "version": "v2.1.13",
+ "target-dir": "Symfony/Component/Yaml",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/symfony/Yaml.git",
+ "reference": "347a7a02204433c6926ecc3f13e805bdc30e8f9f"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/symfony/Yaml/zipball/347a7a02204433c6926ecc3f13e805bdc30e8f9f",
+ "reference": "347a7a02204433c6926ecc3f13e805bdc30e8f9f",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "Symfony\\Component\\Yaml": ""
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Fabien Potencier",
+ "email": "fabien@symfony.com",
+ "homepage": "http://fabien.potencier.org",
+ "role": "Lead Developer"
+ },
+ {
+ "name": "Symfony Community",
+ "homepage": "http://symfony.com/contributors"
+ }
+ ],
+ "description": "Symfony Yaml Component",
+ "homepage": "http://symfony.com",
+ "time": "2013-05-10 00:09:46"
+ }
+ ],
+ "aliases": [
+
+ ],
+ "minimum-stability": "stable",
+ "stability-flags": [
+
+ ],
+ "platform": [
+
+ ],
+ "platform-dev": [
+
+ ]
+}
View
8 phpunit.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit colors="true">
+ <testsuites>
+ <testsuite name="Application Test Suite">
+ <directory>./tests/</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
View
43 tests/classes/DDWordPressDomainChangerTest.php
@@ -0,0 +1,43 @@
+<?php
+
+require 'wpdc/classes/class.DDWordPressDomainChanger.php';
+
+class DDWordPressDomainChangerTest extends PHPUnit_Framework_TestCase
+{
+ public function testSerializedStrReplaceUnserialize()
+ {
+ $find_replace = array(
+ "www.mysite.com" => "www.example.com",
+ "www.apple.com" => "www.microsoft.com",
+ "a.b.c.d.example.com" => "www.example.com/a/b/c/d",
+ "www.mysite.com/dan/doezema" => "dan.doezema.com",
+ "staging.mysite.com" => "www.mysite.com"
+ );
+
+ foreach ($find_replace as $find => $replace) {
+
+ $array = array(
+ "opt_brochure" => "http://$find/wp-content/uploads/2013/05/file1.pdf",
+ "opt_contrat" => "http://$find/wp-content/uploads/2013/05/file2.pdf",
+ "opt_plan" => "http://$find/wp-content/uploads/2013/05/file3.pdf",
+ "opt_tarifs" => "http://$find/wp-content/uploads/2013/05/file4.pdf",
+ "opt_image" => ""
+ );
+
+ $expected_array = array(
+ "opt_brochure" => "http://$replace/wp-content/uploads/2013/05/file1.pdf",
+ "opt_contrat" => "http://$replace/wp-content/uploads/2013/05/file2.pdf",
+ "opt_plan" => "http://$replace/wp-content/uploads/2013/05/file3.pdf",
+ "opt_tarifs" => "http://$replace/wp-content/uploads/2013/05/file4.pdf",
+ "opt_image" => ""
+ );
+
+
+ $modified_serialize_array = DDWordPressDomainChanger::serializedStrReplace($find, $replace, serialize($array));
+
+ $modified_array = unserialize($modified_serialize_array);
+
+ $this->assertEquals($modified_array, $expected_array);
+ }
+ }
+}
View
201 wpdc/classes/class.DDWordPressDomainChanger.php
@@ -0,0 +1,201 @@
+<?php
+
+class DDWordPressDomainChanger {
+
+ /**
+ * Actions that occurred during request.
+ *
+ * @var array
+ */
+ public $actions = array();
+
+ /**
+ * Notices that occurred during request.
+ *
+ * @var array
+ */
+ public $notices = array();
+
+ /**
+ * Errors that occurred during request.
+ *
+ * @var array
+ */
+ public $errors = array();
+
+ /**
+ * File contents of the wp-config.php file.
+ *
+ * @var string
+ */
+ private $config = '';
+
+ /**
+ * Class Constructor
+ *
+ * @return void
+ */
+ public function __construct() {
+ $this->loadConfigFile();
+ }
+
+ /**
+ * Gets a constant's value from the wp-config.php file (if loaded).
+ *
+ * @return mixed; false if not found.
+ */
+ public function getConfigConstant($constant) {
+ if($this->isConfigLoaded()) {
+ preg_match("!define\('".$constant."',[^']*'(.+?)'\);!", $this->config, $matches);
+ return (isset($matches[1])) ? $matches[1] : false;
+ }
+ return false;
+ }
+
+ /**
+ * Gets $table_prefix value from the wp-config.php file (if loaded).
+ *
+ * @return string;
+ */
+ public function getConfigTablePrefix() {
+ if($this->isConfigLoaded()) {
+ preg_match("!table_prefix[^=]*=[^']*'(.+?)';!", $this->config, $matches);
+ return (isset($matches[1])) ? $matches[1] : '';
+ }
+ return '';
+ }
+
+ /**
+ * Gets the best guess of the "New Domain" based on this files location at runtime.
+ *
+ * @return string;
+ */
+ public function getNewDomain() {
+ $new_domain = str_replace('http://','', $_SERVER['SERVER_NAME']);
+ if(isset($_SERVER['SERVER_PORT']) && strlen($_SERVER['SERVER_PORT']) > 0 && $_SERVER['SERVER_PORT'] != 80) {
+ $new_domain .= ':'.$_SERVER['SERVER_PORT'];
+ }
+ return $new_domain;
+ }
+
+ /**
+ * Gets the "siteurl" WordPress option (if possible).
+ *
+ * @return mixed; false if not found.
+ */
+ public function getOldDomain() {
+ if($this->isConfigLoaded()) {
+ $mysqli = @new mysqli($this->getConfigConstant('DB_HOST'), $this->getConfigConstant('DB_USER'), $this->getConfigConstant('DB_PASSWORD'), $this->getConfigConstant('DB_NAME'));
+ if(mysqli_connect_error()) {
+ $this->notices[] = 'Unable to connect to this server\'s database using the settings from wp-config.php; check that it\'s properly configured.';
+ } else {
+ $result = $mysqli->query('SELECT * FROM '.$this->getConfigTablePrefix().'options WHERE option_name="siteurl";');
+ if(is_object($result) && ($result->num_rows > 0)) {
+ $row = $result->fetch_assoc();
+ return str_replace('http://','', $row['option_value']);
+ } else {
+ $this->error[] = 'The WordPress option_name "siteurl" does not exist in the "'.$this->getConfigTablePrefix().'options" table!';
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if the wp-config.php file was loaded successfully.
+ *
+ * @return bool;
+ */
+ public function isConfigLoaded() {
+ return (strlen($this->config) > 0);
+ }
+
+ /**
+ * Replace $find with $replace in a string segment and still keep the integrity of the PHP serialized string.
+ *
+ * Example:
+ * ... s:13:"look a string"; ...
+ * serializedReplace('string', 'function', $serialized_string)
+ * ... s:15:"look a function"; ...
+ *
+ * @param string;
+ * @param string;
+ * @param string;
+ * @return string;
+ */
+ public static function serializedStrReplace($find, $replace, $haystack) {
+ $length_diff = strlen($replace) - strlen($find);
+ $find_escaped = self::preg_quote($find, '!');
+ if(preg_match_all('!s:([0-9]+):"([^"]*?'.$find_escaped.'{1}.*?)";!', self::regExpSerializeEncode($haystack), $matches)) {
+ $matches = array_map(array(__CLASS__,'regExpSerializeDecode'), $matches);
+ $match_count = count($matches[0]);
+ for($i=0;$i<$match_count;$i++) {
+ $new_string = str_replace($find, $replace, $matches[2][$i], $replace_count);
+ $new_length = ((int) $matches[1][$i]) + ($length_diff * $replace_count);
+ $haystack = str_replace($matches[0][$i], 's:'.$new_length.':"'.$new_string.'";', $haystack);
+ }
+ }
+ return $haystack;
+ }
+
+ /**
+ * Enhanced version of preg_quote() that works properly in PHP < 5.3
+ *
+ * @param string;
+ * @param mixed; string, null default
+ * @return string;
+ */
+ public static function preg_quote($string, $delimiter = null) {
+ $string = preg_quote($string, $delimiter);
+ if(phpversion() < 5.3) $string = str_replace('-', '\-', $string);
+ return $string;
+ }
+
+ /**
+ * Attempts to load the wp-config.php file into $this->config
+ *
+ * @return void;
+ */
+ private function loadConfigFile() {
+ $this->config = file_get_contents(dirname(__FILE__).'/wp-config.php');
+ if(!$this->isConfigLoaded()) {
+ $this->notices[] = 'Unable to find "wp-config.php" ... Make sure the '.basename(__FILE__).' file is in the root WordPress directory.';
+ } else {
+ $this->actions[] = 'wp-config.php file successfully loaded.';
+ }
+ }
+
+
+ /**
+ * Replaces any occurrence of " (double quote character) within the value
+ * of a serialized string segment with [DOUBLE_QUOTE]. This allows for RegExp
+ * to properly capture string segment values in self::serializedStrReplace().
+ *
+ * Example:
+ * ... s:13:"look "a" string"; ...
+ * regExpSerializeEncode($serialized_string)
+ * ... s:13:"look [DOUBLE_QUOTE]a[DOUBLE_QUOTE] string"; ...
+ *
+ * @param string;
+ * @return string;
+ */
+ private static function regExpSerializeEncode($string) {
+ if(preg_match_all('!s:[0-9]+:"(.+?)";!', $string, $matches)) {
+ foreach($matches[1] as $match) {
+ $string = str_replace($match, str_replace('"', '[DOUBLE_QUOTE]', $match), $string);
+ }
+ }
+ return $string;
+ }
+
+ /**
+ * Undoes the changes that self::regExpSerializeEncode() made to a string.
+ *
+ * @see self::regExpSerializeEncode();
+ * @param string;
+ * @return string;
+ */
+ private static function regExpSerializeDecode($string) {
+ return str_replace('[DOUBLE_QUOTE]', '"', $string);
+ }
+}
View
14 wpdc/config.php
@@ -0,0 +1,14 @@
+<?php
+
+// Authentication Password
+define('DDWPDC_PASSWORD', 'Replace-This-Password');
+
+// Cookie: Name: Authentication
+define('DDWPDC_COOKIE_NAME_AUTH', 'DDWPDC_COOKIE_AUTH');
+
+// Cookie: Name: Expiration
+define('DDWPDC_COOKIE_NAME_EXPIRE', 'DDWPDC_COOKIE_EXPIRE');
+
+// Cookie: Timeout (Default: 5 minutes)
+define('DDWPDC_COOKIE_LIFETIME', 60 * 5);
+
View
237 wp-change-domain.php → wpdc/index.php
@@ -2,10 +2,10 @@
/**
* Author: Daniel Doezema
* Author URI: http://dan.doezema.com
- * Version: 0.2 (Beta)
+ * Version: 0.1.0
* Description: This script was developed to help ease migration of WordPress sites from one domain to another.
*
- * Copyright (c) 2010, Daniel Doezema
+ * Copyright (c) 2014, Daniel Doezema
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -31,225 +31,12 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
- * @copyright Copyright (c) 2010 Daniel Doezema. (http://dan.doezema.com)
+ * @copyright Copyright (c) 2014 Daniel Doezema. (http://dan.doezema.com)
* @license http://dan.doezema.com/licenses/new-bsd New BSD License
*/
-/* == CONFIG ======================================================= */
-
-// Authentication Password
-define('DDWPDC_PASSWORD', 'Replace-This-Password');
-
-// Cookie: Name: Authentication
-define('DDWPDC_COOKIE_NAME_AUTH', 'DDWPDC_COOKIE_AUTH');
-
-// Cookie: Name: Expiration
-define('DDWPDC_COOKIE_NAME_EXPIRE', 'DDWPDC_COOKIE_EXPIRE');
-
-// Cookie: Timeout (Default: 5 minutes)
-define('DDWPDC_COOKIE_LIFETIME', 60 * 5);
-
-/* == NAMESPACE CLASS ============================================== */
-
-class DDWordPressDomainChanger {
-
- /**
- * Actions that occurred during request.
- *
- * @var array
- */
- public $actions = array();
-
- /**
- * Notices that occurred during request.
- *
- * @var array
- */
- public $notices = array();
-
- /**
- * Errors that occurred during request.
- *
- * @var array
- */
- public $errors = array();
-
- /**
- * File contents of the wp-config.php file.
- *
- * @var string
- */
- private $config = '';
-
- /**
- * Class Constructor
- *
- * @return void
- */
- public function __construct() {
- $this->loadConfigFile();
- }
-
- /**
- * Gets a constant's value from the wp-config.php file (if loaded).
- *
- * @return mixed; false if not found.
- */
- public function getConfigConstant($constant) {
- if($this->isConfigLoaded()) {
- preg_match("!define\('".$constant."',[^']*'(.+?)'\);!", $this->config, $matches);
- return (isset($matches[1])) ? $matches[1] : false;
- }
- return false;
- }
-
- /**
- * Gets $table_prefix value from the wp-config.php file (if loaded).
- *
- * @return string;
- */
- public function getConfigTablePrefix() {
- if($this->isConfigLoaded()) {
- preg_match("!table_prefix[^=]*=[^']*'(.+?)';!", $this->config, $matches);
- return (isset($matches[1])) ? $matches[1] : '';
- }
- return '';
- }
-
- /**
- * Gets the best guess of the "New Domain" based on this files location at runtime.
- *
- * @return string;
- */
- public function getNewDomain() {
- $new_domain = str_replace('http://','', $_SERVER['SERVER_NAME']);
- if(isset($_SERVER['SERVER_PORT']) && strlen($_SERVER['SERVER_PORT']) > 0 && $_SERVER['SERVER_PORT'] != 80) {
- $new_domain .= ':'.$_SERVER['SERVER_PORT'];
- }
- return $new_domain;
- }
-
- /**
- * Gets the "siteurl" WordPress option (if possible).
- *
- * @return mixed; false if not found.
- */
- public function getOldDomain() {
- if($this->isConfigLoaded()) {
- $mysqli = @new mysqli($this->getConfigConstant('DB_HOST'), $this->getConfigConstant('DB_USER'), $this->getConfigConstant('DB_PASSWORD'), $this->getConfigConstant('DB_NAME'));
- if(mysqli_connect_error()) {
- $this->notices[] = 'Unable to connect to this server\'s database using the settings from wp-config.php; check that it\'s properly configured.';
- } else {
- $result = $mysqli->query('SELECT * FROM '.$this->getConfigTablePrefix().'options WHERE option_name="siteurl";');
- if(is_object($result) && ($result->num_rows > 0)) {
- $row = $result->fetch_assoc();
- return str_replace('http://','', $row['option_value']);
- } else {
- $this->error[] = 'The WordPress option_name "siteurl" does not exist in the "'.$this->getConfigTablePrefix().'options" table!';
- }
- }
- }
- return false;
- }
-
- /**
- * Returns true if the wp-config.php file was loaded successfully.
- *
- * @return bool;
- */
- public function isConfigLoaded() {
- return (strlen($this->config) > 0);
- }
-
- /**
- * Replace $find with $replace in a string segment and still keep the integrity of the PHP serialized string.
- *
- * Example:
- * ... s:13:"look a string"; ...
- * serializedReplace('string', 'function', $serialized_string)
- * ... s:15:"look a function"; ...
- *
- * @param string;
- * @param string;
- * @param string;
- * @return string;
- */
- public static function serializedStrReplace($find, $replace, $haystack) {
- $length_diff = strlen($replace) - strlen($find);
- $find_escaped = self::preg_quote($find, '!');
- if(preg_match_all('!s:([0-9]+):"([^"]*?'.$find_escaped.'{1}.*?)";!', self::regExpSerializeEncode($haystack), $matches)) {
- $matches = array_map(array(__CLASS__,'regExpSerializeDecode'), $matches);
- $match_count = count($matches[0]);
- for($i=0;$i<$match_count;$i++) {
- $new_string = str_replace($find, $replace, $matches[2][$i], $replace_count);
- $new_length = ((int) $matches[1][$i]) + ($length_diff * $replace_count);
- $haystack = str_replace($matches[0][$i], 's:'.$new_length.':"'.$new_string.'"', $haystack);
- }
- }
- return $haystack;
- }
-
- /**
- * Enhanced version of preg_quote() that works properly in PHP < 5.3
- *
- * @param string;
- * @param mixed; string, null default
- * @return string;
- */
- public static function preg_quote($string, $delimiter = null) {
- $string = preg_quote($string, $delimiter);
- if(phpversion() < 5.3) $string = str_replace('-', '\-', $string);
- return $string;
- }
-
- /**
- * Attempts to load the wp-config.php file into $this->config
- *
- * @return void;
- */
- private function loadConfigFile() {
- $this->config = file_get_contents(dirname(__FILE__).'/wp-config.php');
- if(!$this->isConfigLoaded()) {
- $this->notices[] = 'Unable to find "wp-config.php" ... Make sure the '.basename(__FILE__).' file is in the root WordPress directory.';
- } else {
- $this->actions[] = 'wp-config.php file successfully loaded.';
- }
- }
-
-
- /**
- * Replaces any occurrence of " (double quote character) within the value
- * of a serialized string segment with [DOUBLE_QUOTE]. This allows for RegExp
- * to properly capture string segment values in self::serializedStrReplace().
- *
- * Example:
- * ... s:13:"look "a" string"; ...
- * regExpSerializeEncode($serialized_string)
- * ... s:13:"look [DOUBLE_QUOTE]a[DOUBLE_QUOTE] string"; ...
- *
- * @param string;
- * @return string;
- */
- private static function regExpSerializeEncode($string) {
- if(preg_match_all('!s:[0-9]+:"(.+?)";!', $string, $matches)) {
- foreach($matches[1] as $match) {
- $string = str_replace($match, str_replace('"', '[DOUBLE_QUOTE]', $match), $string);
- }
- }
- return $string;
- }
-
- /**
- * Undoes the changes that self::regExpSerializeEncode() made to a string.
- *
- * @see self::regExpSerializeEncode();
- * @param string;
- * @return string;
- */
- private static function regExpSerializeDecode($string) {
- return str_replace('[DOUBLE_QUOTE]', '"', $string);
- }
-}
+require 'config.php';
+require 'classes/class.DDWordPressDomainChanger.php';
/* == START PROCEDURAL CODE ============================================== */
@@ -261,10 +48,10 @@ private static function regExpSerializeDecode($string) {
// Password Check -> Set Cookie -> Redirect
if(isset($_POST['auth_password'])) {
/**
- * Try and obstruct brute force attacks by making each login attempt
- * take 5 seconds.This is total security-through-obscurity and can be
+ * Try and obstruct brute force attacks by making each login attempt
+ * take 5 seconds.This is total security-through-obscurity and can be
* worked around fairly easily, it's just one more step.
- *
+ *
* MAKE SURE you remove this script after the domain change is complete.
*/
sleep(5);
@@ -326,11 +113,11 @@ private static function regExpSerializeDecode($string) {
if($result->num_rows > 0) {
// Build dataset
while(is_array($row = $result->fetch_assoc())) $serialized_options[] = $row;
-
+
// Build Exclude SQL
foreach($serialized_options as $record) $options_to_exclude .= $record['option_id'].',';
$options_to_exclude = ' WHERE option_id NOT IN('.rtrim($options_to_exclude, ',').')';
-
+
// Update Serialized Options
foreach($serialized_options as $record) {
$new_option_value = DDWordPressDomainChanger::serializedStrReplace($data['old_domain'], $data['new_domain'], $record['option_value']);
@@ -339,9 +126,9 @@ private static function regExpSerializeDecode($string) {
}
$DDWPDC->actions[] = '[Serialize Replace] Old domain ('.$data['old_domain'].') replaced with new domain ('.$data['new_domain'].') in option_name="'.$record['option_name'].'"';
}
-
+
}
-
+
// Update Options
if(!$mysqli->query('UPDATE '.$data['prefix'].'options SET option_value = REPLACE(option_value,"'.$data['old_domain'].'","'.$data['new_domain'].'")'.$options_to_exclude.';')) {
throw new Exception($mysqli->error);
Please sign in to comment.
Something went wrong with that request. Please try again.