summaryrefslogtreecommitdiffstats
path: root/index.php
blob: 3b4b26c372202b4ca61341099083ae6f2ff7cff9 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
define('WP_INSTALLING', true);

function printheader() {
    print '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <title>WordPress &rsaquo; Installation</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style media="screen" type="text/css">
        <!--
        html {
                background: #eee;
        }
        body {
                background: #fff;
                color: #000;
                font-family: Georgia, "Times New Roman", Times, serif;
                margin-left: 20%;
                margin-right: 20%;
                padding: .2em 2em;
        }
        
        h1 {
                color: #006;
                font-size: 18px;
                font-weight: lighter;
        }
        
        h2 {
                font-size: 16px;
        }
        
        p, li, dt {
                line-height: 140%;
                padding-bottom: 2px;
        }

        ul, ol {
                padding: 5px 5px 5px 20px;
        }
        #logo {
                margin-bottom: 2em;
        }
.step a, .step input {
        font-size: 2em;
}
.step, th {
        text-align: right;
}
#footer {
text-align: center; border-top: 1px solid #ccc; padding-top: 1em; font-style: italic;
}
.fakelink {
    color: #00a;
    text-decoration: underline;
}
        -->
        </style>
</head>
<body>

<div align="center"><img src="wp-inst/wp-images/wordpress-mu.png"></div><br>
Welcome to WordPress MU, the Multi User Weblog System built on WordPress.<br><br>
';
}

function check_writeable_dir( $dir, $ret )
{
    if( is_writeable( $dir  ) == false )
    {
        print $dir." : <b style='color: #f55'>FAILED</b><br>Quick Fix: <code>chmod 777 $dir</code><br>";
        return false;
    }
    else
    {
        if( $ret == true )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

function filestats( $err ) {
    print "<h1>Server Summary</h1>";
    print "<p>If you post a message to the MU support forum at <a target='_blank' href='http://mu.wordpress.org/forums/'>http://mu.wordpress.org/forums/</a> then copy and paste the following information into your message:</p>";

    print "<blockquote style='background: #eee; border: 1px solid #333; padding: 5px;'>";
    print "<br /><strong>ERROR: $err</strong></br >";
    clearstatcache();
    $files = array( "htaccess.dist", ".htaccess" );
    while( list( $key, $val ) = each( $files ) ) { 
	$stats = @stat( $val );
	if( $stats ) {
	    print "<h2>$val</h2>";
	    print "&nbsp;&nbsp;&nbsp;&nbsp;uid/gid: " . $stats[ 'uid' ] . "/" . $stats[ 'gid' ] . "<br />\n";
	    print "&nbsp;&nbsp;&nbsp;&nbsp;size: " . $stats[ 'size' ] . "<br />";
	    print "&nbsp;&nbsp;&nbsp;&nbsp;perms: " . substr( sprintf('%o', fileperms( $val ) ), -4 ) . "<br />";
	    print "&nbsp;&nbsp;&nbsp;&nbsp;readable: ";
	    print is_readable( $val ) == true ? "yes" : "no";
	    print "<br />";
	    print "&nbsp;&nbsp;&nbsp;&nbsp;writeable: ";
	    print is_writeable( $val ) == true ? "yes" : "no";
	    print "<br />";
	    
	} elseif( file_exists( $val ) == false ) {
	    print "<h2>$val</h2>";
	    print "&nbsp;&nbsp;&nbsp;&nbsp;FILE NOT FOUND: $val<br>";
	}
    }
    print "</blockquote>";

}

function do_htaccess( $oldfilename, $newfilename, $realpath, $base, $url )
{
    // remove ending slash from $base and $url
    $htaccess = '';
    if( substr($base, -1 ) == '/') {
	$base = substr($base, 0, -1);
    }

    if( substr($url, -1 ) == '/') {
	$url = substr($url, 0, -1);
    }
    $err = '';
    if( is_file( $oldfilename ) ) {
        $fp = @fopen( $oldfilename, "r" );
        if( $fp ) {
            while( !feof( $fp ) )
            {
                $htaccess .= fgets( $fp, 4096 );
            }
            fclose( $fp );
            $htaccess = str_replace( "REALPATH", $realpath, $htaccess );
            $htaccess = str_replace( "BASE", $base, $htaccess );
            $htaccess = str_replace( "HOST", $url, $htaccess );
	    if( touch( $newfilename ) ) {
		    $fp = fopen( $newfilename, "w" );
		    if( $fp ) {
			    fwrite( $fp, $htaccess );
			    fclose( $fp );
		    } else {
			    $err = "could not open $newfilename for writing";
		    }
	    } else {
		    $err = "could not open $newfilename for writing";
	    }
        } else {
	    $err = "could not open $oldfilename for reading";
	}
    } else {
	$err = "$oldfilename not found";
    }

    if( $err != '' ) {
	    print "<h1>Warning!</h1>";
	    print "<p><strong>There was a problem creating the .htaccess file in $realpath.</strong> </p>";
	    print "<p style='color: #900'>Error: ";
	    if( $err == "could not open $newfilename for writing" ) {
		print "Could Not Write To $newfilename.";
	    } elseif( $err == "could not open $oldfilename for reading" ) {
		print "I could not read from $oldfilename. ";
	    } elseif( $err == "$oldfilename not found" ) {
		print "The file, $oldfilename, is missing.";
	    }
	    print "</p>";
	    filestats( $err );

	    print "<p>Please ensure that the webserver can write to this directory.</p>";
	    print "<p>If you use Cpanel then read <a href='http://mu.wordpress.org/forums/topic/99'>this post</a>. Cpanel creates files that I need to overwrite and you have to fix that.</p>";
	    print "<p>If all else fails then you'll have to create it by hand:";
	    print "<ul><li> Download htaccess.dist to your computer and open it in your favourite text editor.</li>
		<li> Replace the following text:<ul><li>REALPATH by '$realpath'</li><li>BASE by '$base'</li><li>HOST by '$url'</li></li>
		<li> Rename htaccess.dist to .htaccess and upload it back to the same directory.</li></ul>";
	    die( "Installation Aborted!" );
    }
}

function checkdirs() {
    $ret = true;
    $ret = check_writeable_dir( dirname(__FILE__), $ret );
    $ret = check_writeable_dir( dirname(__FILE__) . "/wp-inst", $ret );
    $ret = check_writeable_dir( dirname(__FILE__) . "/wp-inst/wp-content/", $ret );

    if( $ret == false )
    {
        print "<h2>Warning!</h2>";
        print "<div style='border: 1px solid #ccc'>";
        print "<p style='font-weight: bold; padding-left: 10px'>One or more of the above directories must be made writeable by the webserver.<br>";
        print "Please <code>chmod 777 <q>directory-name</q></code> or <code>chown</code> that directory to the user the web server runs as (usually nobody, apache, or www-data)<br>";
        print "Refresh this page when you're done!<br></p>";
        print "</div>";
    }

    return $ret;
}

function step1() {
    print "You're probably seeing this message because you're installing WPMU!<br><br>";
    print "If it's already installed, then <b>mod_rewrite</b> is disabled. Ask your administrator to enable that module, or look at the <a href='http://httpd.apache.org/docs/mod/mod_rewrite.html'>Apache documentation</a> or <a href='http://www.google.com/search?q=apache+mod_rewrite'>elsewhere</a> for help setting it up.<br><br>";
    if( checkdirs() == false ) {
	return false;
    }

    // Create default template cache dirs
    @mkdir( dirname(__FILE__) . "/wp-inst/wp-content/smarty-cache" , 0777 );
    @mkdir( dirname(__FILE__) . "/wp-inst/wp-content/smarty-templates_c" , 0777 );

    // Create Blogs living area.
    @mkdir( dirname(__FILE__) . "/wp-inst/wp-content/blogs.dir", 0777 );


    $url = "http://".$_SERVER["SERVER_NAME"] . dirname( $_SERVER[ "SCRIPT_NAME" ] );
    if( substr( $url, -1 ) == '/' )
        $url = substr( $url, 0, -1 );
    $base = dirname( $_SERVER["SCRIPT_NAME"] );
    if( $base == "/")
    {
           $base = "";
    } 
    $realpath = dirname(__FILE__);

    if( is_file( dirname(__FILE__) . "./wp-inst/wpmu-settings.php" ) == false )
    {
        $configfile = '';
        $fp = fopen( "./wp-inst/wpmu-settings.php.dist", "r" );
        if( $fp )
        {
            while( !feof( $fp ) )
            {
                $configfile .= fgets( $fp, 4096 );
            }
            fclose( $fp );
        }
        $configfile = str_replace( "BASE", $base."/", $configfile );
        $fp = fopen( "./wp-inst/wpmu-settings.php", "w" );
        fwrite( $fp, $configfile );
        fclose( $fp );
    }
    return true;
}

function printstep1form( $dbname = 'wordpress', $uname = 'username', $pwd = 'password', $dbhost = 'localhost', $prefix = 'wp_' ) {
    print "
    <form method='post' action='index.php'> 
    <input type='hidden' name='action' value='step2'>
    <h1>Virtual Server Support</h1>
    <p><strong>Do you want to enable virtual server support?&nbsp;&nbsp;&nbsp;Yes <input type='radio' name='vhost' value='yes'>   No <input type='radio' name='vhost' value='no' checked></strong></p>
    <p>If you do, your blog addresses will appear like <span class='fakelink'>http://joesblog.example.com/</span> instead of <span class='fakelink'>http://www.example.com/joesblog/</span> but you need to do a few more things to Apache and your DNS settings before it'll work.<br />
    If this is your first time installing WPMU then leave this at <em>No</em>.</p>
    <br />
    <h1>Database</h1>
<p>We need some information on the database. You will need to know the following items before proceeding.</p> 
<ol> 
  <li>Database name</li> 
  <li>Database username</li> 
  <li>Database password</li> 
  <li>Database host</li> 
  <li>Table prefix (if you want to run more than one WordPress in a single database) </li>
</ol> 
<p><strong>If for any reason this automatic file creation doesn't work, don't worry. All this does is fill in the database information to a configuration file. You may also simply open <code>wp-config-sample.php</code> in a text editor, fill in your information, and save it as <code>wp-config.php</code>. </strong></p>

  <p>Below you should enter your database connection details. If you're not sure about these, contact your host. </p>
  <table> 
    <tr> 
      <th scope='row'>Database Name</th> 
      <td><input name='dbname' type='text' size='45' value='".$dbname."' /></td> 
      <td>The name of the database you want to run WP in. </td> 
    </tr> 
    <tr> 
      <th scope='row'>User Name</th> 
      <td><input name='uname' type='text' size='45' value='".$uname."' /></td> 
      <td>Your MySQL username</td> 
    </tr> 
    <tr> 
      <th scope='row'>Password</th> 
      <td><input name='pwd' type='text' size='45' value='".$pwd."' /></td> 
      <td>...and MySQL password.</td> 
    </tr> 
    <tr> 
      <th scope='row'>Database Host</th> 
      <td><input name='dbhost' type='text' size='45' value='".$dbhost."' /></td> 
      <td>99% chance you won't need to change this value.</td> 
    </tr>
    <tr>
      <th scope='row'>Table Prefix</th>
      <td><input name='prefix' type='text' id='prefix' value='".$prefix."' size='45' /></td>
      <td>If you want to run multiple WordPress installations in a single database, change this.</td>
    </tr> 
  </table> 
  <input name='submit' type='submit' value='Submit' /> 
</form> ";
}

function step2() {
    $dbname  = $_POST['dbname'];
    $uname   = $_POST['uname'];
    $passwrd = $_POST['pwd'];
    $dbhost  = $_POST['dbhost'];
    $vhost   = $_POST['vhost' ]; 
    $prefix  = $_POST['prefix'];
    if (empty($prefix)) $prefix = 'wp_';

    // Test the db connection.
    define('DB_NAME', $dbname);
    define('DB_USER', $uname);
    define('DB_PASSWORD', $passwrd);
    define('DB_HOST', $dbhost);

    if (!file_exists('wp-inst/wp-config-sample.php'))
	die('Sorry, I need a wp-config-sample.php file to work from. Please re-upload this file from your WordPress installation.');

    $configFile = file('wp-inst/wp-config-sample.php');
    // We'll fail here if the values are no good.
    require_once('wp-inst/wp-includes/wp-db.php');
    printheader();

    print "Creating Database Config File: ";
    
    $handle = fopen('wp-inst/wp-config.php', 'w');

    foreach ($configFile as $line_num => $line) {
	switch (substr($line,0,16)) {
	    case "define('DB_NAME'":
		fwrite($handle, str_replace("wordpress", $dbname, $line));
	    break;
	    case "define('DB_USER'":
		fwrite($handle, str_replace("'username'", "'$uname'", $line));
	    break;
	    case "define('DB_PASSW":
		fwrite($handle, str_replace("'password'", "'$passwrd'", $line));
	    break;
	    case "define('DB_HOST'":
		fwrite($handle, str_replace("localhost", $dbhost, $line));
	    break;
	    case "define('VHOST', ":
		fwrite($handle, str_replace("VHOSTSETTING", $vhost, $line));
	    break;
	    case '$table_prefix  =':
	    fwrite($handle, str_replace('wp_', $prefix, $line));
	    break;
	    default:
	    fwrite($handle, $line);
	}
    }
    fclose($handle);
    chmod('wp-inst/wp-config.php', 0666);
    print "<b style='color: #00aa00; font-weight: bold'>DONE</b><br />";
}

function printuserdetailsform( $weblog_title = 'My new Blog', $username = '', $email = '' ) {
    print " 
	<form method='post' action='index.php'> 
	<input type='hidden' name='action' value='step3'>
	<p>To finish setting up your blog, please fill in the folling form and click <q>Submit</q>.</p>
	<table width='100%'> 
	<tr> 
	<th scope='row'>Weblog&nbsp;Title</th> 
	<td><input name='weblog_title' type='text' size='45' value='".$weblog_title."' /></td> 
	<td>What would you like to call your weblog? </td> 
	</tr> 
	<tr> 
	<th scope='row'>Email</th> 
	<td><input name='email' type='text' size='45' value='".$email."' /></td> 
	<td>Your email address.</td> 
	</tr> 
	</table> 
	<input name='submit' type='submit' value='Submit' /> 
	</form> 
	<br />
	You will be sent an email with your password and login links and details.";
}

function step3() {
    global $wpdb;
    $base = dirname( $_SERVER["SCRIPT_NAME"] );
    $domain = $_SERVER[ 'HTTP_HOST' ];
    if( substr( $domain, 0, 4 ) == 'www.' )
	$domain = substr( $domain, 4 );

    $email = $wpdb->escape( $_POST[ 'email' ] );
    $weblog_title = $wpdb->escape( $_POST[ 'weblog_title' ] );

    // set up site tables
    $query = "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (1, 1, 'admin_email', '".$email."')";
    $wpdb->query( $query );
    $query = "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (2, 1, 'admin_user_id', '1')";
    $wpdb->query( $query );
    $wpdb->query( "INSERT INTO ".$wpdb->site." ( id, domain, path ) VALUES ( '', '$domain', '$base' )" );

    $res = createBlog( $domain, $base, 'admin', $weblog_title, $email );
    if( $res == 'ok' ) {
	if( $base == '/' ) {
		$url = "http://".$_SERVER["HTTP_HOST"] . '/';
	} else {
		$url = "http://".$_SERVER["HTTP_HOST"] . $base . '/';
	}
	$realpath = dirname(__FILE__);
	do_htaccess( "htaccess.dist", ".htaccess", $realpath, $base, $url );
	do_htaccess( "wp-inst/htaccess.dist", "wp-inst/.htaccess", $realpath, $base, $url );

	$illegal_names = array( "www", "web", "root", "admin", "main", "invite", "administrator" );
	add_site_settings( "illegal_names", $illegal_names );

	print "<p>Well Done! Your blog has been set up and you have been sent details of your login and password in an email.</p>";
	print "<p>You may view your new blog by visiting <a href='".$url."'>".$url."</a>!</p>";
    } else {
	if( $res == 'error: problem creating blog entry' ) {
	    print "The <q>main</q> blog has already been created. Edit your blogs table and delete the entry for this domain!";
	} elseif( $res == 'error: username used' ) {
	    print "The username you chose is already in use, please select another one.";
	}
	print "<br>result: $res<br>";
	printuserdetailsform( $_POST[ 'weblog_title' ], $_POST[ 'username' ], $_POST[ 'email' ] );
    }
}

switch( $_POST[ 'action' ] ) {
    case "step2":
	// get blog username
	// create wp-inst/wp-config.php 
	step2();
	printuserdetailsform();
    break;
    case "step3":
	// call createBlog();
	// create .htaccess
	// print login info and links.
	require_once('./wp-inst/wp-config.php');
        require_once('./wp-inst/wp-admin/upgrade-functions.php');
	make_db_current_silent();
	populate_options();
        printheader();
	step3();
    break;
    default:
        // check that directories are writeable.
        // create wp-inst/wpmu-settings.php
        // get db auth info.
        printheader();
        if( step1() ) {
	    printstep1form();
	}
    break;
}
?>
<br /><br />
<div align='center'>
<a href="http://mu.wordpress.org/">WPMU</a> | <a href="http://mu.wordpress.org/forums/">Support Forums</a>
</div>
</body>
</html>