summaryrefslogtreecommitdiffstats
path: root/wp-inst/wp-newblog.php
blob: 25eed5f01ec1c29dab95fd5f30dc79d181fa32e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
require('wp-config.php');

do_action( "newblogheader", "" );

get_header();
?>
<div id="content" class="widecolumn">
<style type="text/css">
form { margin-top: 2em; }
td input {
	width: 90%;
	font-size: 24px;
}
.error {
	background-color: #FF6666;
}
</style>
<?php

function displayInitialForm( $weblog_id = '', $weblog_title = '', $admin_email = '', $admin_login = '', $form = '', $errormsg = '' ) {
    print "<h2>Get your own blog</h2>";
    if( is_array( $errormsg ) ) {
	print "<p>There was a problem, please correct the form below and try again.</p>";
    }
    print '
	<form name="setup" id="setup" method="post" action="wp-newblog.php">
	<input type="hidden" name="stage" value="1">
	<table border="0" width="100%">';
    if( isset( $errormsg[ 'weblog_id' ] ) == true ) {
	print '<tr class="error">';
    } else {
	print '<tr>';
    }
    print '
	<th valign="top">Username:</th>
	<td><input name="weblog_id" type="text" id="weblog_id" value="'.$weblog_id.'" maxlength="50" /><br />(This will also be your blog address. Letters and numbers only, please.)</td>
	</tr>';
    if( $errormsg[ 'weblog_title' ] != '' ) {
	print '<tr class="error">';
    } else {
	print '<tr>';
    }
    print '
	<th valign="top">Blog Name:</th>
	<td><input name="weblog_title" type="text" id="weblog_title" value="'.wp_specialchars( $weblog_title, 1 ).'" /><br /> (Don\'t worry, you can change it later.)</td>
	</tr>';
    if( $errormsg[ 'admin_email' ] != '' ) {
	print '<tr class="error">';
    } else {
	print '<tr>';
    }
    print '
	<th valign="top">Email&nbsp;Address:</th>
	<td><input name="admin_email" type="text" id="admin_email" value="'.wp_specialchars( $admin_email, 1 ).'" maxlength="200" /><br /> (We\'ll send a password to this address, so double-check it.)</td>
	</tr>';
    if( $form == 'adminform' ) {
	print '
	<tr class="error">
	<th colspan="2">You have tried to setup a new domain. Please enter the administrator password for this site.</th>
	</tr>
	<tr>
	<th>Admin Password:</th>
	<td><input name="admin_pw" type="password" id="admin_pw" value="" /></td>
	</tr>
	';
    }
    print '
   	<tr>
   		<th scope="row">&nbsp;</th>
   		<td><input type="submit" name="Submit" class="submit" value="Sign Up &raquo;" /></td>
  		</tr>';
    do_action( "newblogform", "" );
    print '
	</table>
	</form>';
}

function displaySecondForm() {
    global $url;
    print "<h2>You've got a new blog!</h2>";
    print "<h3>Your new address is <a href='".$url."'>".$url."</a></h3>
	<p>You should receive an email with the login details shortly.</p>";
    print "<p>Visit: <a href='$url'>$url</a><br>";
    print "Login: <a href='".$url."wp-login.php'>".$url."wp-login.php</a></p>";
    do_action( "newblogfinished", "" );
}

/*
   Determines the directory path - using the current script
 */
function determineDirPath() {
    global $_SERVER;

    $result = dirname( $_SERVER["SCRIPT_NAME"] );	
    $result = str_replace("wp-inst","",$result);
    if( strlen( $result > 1 ) && substr($result, -1 ) == '/') {
	$result = substr($result, 0, -1);
    }

    return $result;
}

switch( $_POST[ 'stage' ] )
{
    case "1":
	$newBlogID = sanitize_title($_POST['weblog_id']);
	$weblog_title = stripslashes(  $_POST[ 'weblog_title' ] );
	$admin_email = $_POST[ 'admin_email' ];

	if( is_email( $admin_email ) == false )
	    $errormsg[ 'admin_email' ] = true;

	if( empty( $newBlogID ) )
	    $errormsg[ 'weblog_id' ] = true;

	if( empty( $weblog_title ) )
	    $errormsg[ 'weblog_title' ] = true;

	if( is_array( $errormsg ) ) {
	    displayInitialForm( $_POST[ 'weblog_id' ], $weblog_title, $_POST[ 'admin_email' ], $_POST[ 'admin_login' ], 'userform', $errormsg );
	} elseif( isset($newBlogID) && ($newBlogID !='' )) {
	    $scriptBaseName = determineDirPath();
	    $serverName = $_SERVER[ 'SERVER_NAME' ];
	    define( "WP_INSTALLING", true );
	    require_once('./wp-config.php');
	    // check if "main" being installed. ask for admin pw if not defined..
	    $setup = true;
	    if( $newBlogID == 'main' && isset( $_POST[ 'admin_pw' ] ) == false ) {
		displayInitialForm( $_POST[ 'weblog_id' ], $weblog_title, $_POST[ 'admin_email' ], $_POST[ 'admin_login' ], 'adminform' );
		$setup = false;
	    } elseif( $newBlogID == 'main' && isset( $_POST[ 'admin_pw' ] ) == true ) {
		$query = "SELECT ID
		          FROM   ".$wpdb->users."
			  WHERE  user_pass = '".md5( $_POST[ 'admin_pw' ] )."'
			  AND    user_login = 'admin'";
		$admin_id = $wpdb->get_var( $query );
		if( $admin_id != 1 ) {
		    displayInitialForm( $_POST[ 'weblog_id' ], $_POST[ 'weblog_title' ], $_POST[ 'admin_email' ], $_POST[ 'admin_login' ], 'adminform' );
		    $setup = false;
		}
	    }


	    if( substr( $domain, 0, 4 ) == 'www.' )
		$domain = substr( $domain, 4 );
	    if( $setup == true ) {
		if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
		    if( $newBlogID == 'main' ) {
			$url = 'http://www'.$domain.$scriptBaseName;
		    } else {
			$url = 'http://'.$newBlogID.".".$domain.$scriptBaseName;
		    }
		} else {
		    $url = 'http://'.$serverName.$scriptBaseName.$newBlogID."/";
		}
		$err = createBlog( $_SERVER[ 'HTTP_HOST' ], $domain, $scriptBaseName, $newBlogID, $weblog_title, $admin_email, $newBlogID );
		if( $err == 'ok' ) {
		    displaySecondForm();
		} else {
		    if( $err == 'error: username used' ) {
			$errormsg[ 'weblog_id' ] = "Sorry, that blog already exists!";
		    } else {
			$errormsg[ 'weblog_id' ] = "Sorry, that blog already exists!";
		    }
		    displayInitialForm( $_POST[ 'weblog_id' ], $weblog_title, $_POST[ 'admin_email' ], $_POST[ 'admin_login' ], 'userform', $errormsg );
		}
	    }
	} else {
	    $errormsg[ 'weblog_id' ] = "Sorry, your blog ID may only contain the characters a-z, A-Z, or 0-9!";
	    displayInitialForm( $_POST[ 'weblog_id' ], $weblog_title, $_POST[ 'admin_email' ], $_POST[ 'admin_login' ], 'userform', $errormsg );
	}
        break;
    default:
	displayInitialForm();
	break;
}
?>
</div>
<?php get_footer(); ?>