root/tags/1_0-rc1/index-install.php

Revision 591, 16.9 kB (checked in by donncha, 2 years ago)

Moved everything in wp-inst down a directory.
Uses's Ryan Boren's htaccess rules and mods
If you're upgrading, try this on a test server first!

  • Property svn:eol-style set to native
Line 
1 <?php
2 define('WP_INSTALLING', true);
3
4 function printheader() {
5     print '
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7 <html xmlns="http://www.w3.org/1999/xhtml">
8 <head>
9         <title>WordPress &rsaquo; Installation</title>
10         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
11         <style media="screen" type="text/css">
12         <!--
13         html {
14                 background: #eee;
15         }
16         body {
17                 background: #fff;
18                 color: #000;
19                 font-family: Georgia, "Times New Roman", Times, serif;
20                 margin-left: 20%;
21                 margin-right: 20%;
22                 padding: .2em 2em;
23         }
24         
25         h1, h2 {
26                 color: #006;
27                 font-size: 18px;
28                 font-weight: lighter;
29         }
30         
31         p, li, dt {
32                 line-height: 140%;
33                 padding-bottom: 2px;
34         }
35
36         ul, ol {
37                 padding: 5px 5px 5px 20px;
38         }
39         #logo {
40                 margin-bottom: 2em;
41         }
42 .step a, .step input {
43         font-size: 2em;
44 }
45 .step, th {
46         text-align: right;
47 }
48 #footer {
49 text-align: center; border-top: 1px solid #ccc; padding-top: 1em; font-style: italic;
50 }
51 .fakelink {
52     color: #00a;
53     text-decoration: underline;
54 }
55         -->
56         </style>
57 </head>
58 <body>
59
60 <h1><img src="wp-includes/images/wordpress-mu.png" alt="WordPress MU" /></h1>
61 ';
62 }
63
64 function check_writeable_dir( $dir, $ret ) {
65     if( is_writeable( $dir  ) == false ) {
66         print $dir." : <b style='color: #f55'>FAILED</b><br />Quick Fix: <code>chmod 777 $dir</code><br />";
67         return false;
68     } else {
69         if( $ret == true ) {
70             return true;
71         } else {
72             return false;
73         }
74     }
75 }
76
77 function filestats( $err ) {
78     print "<h1>Server Summary</h1>";
79     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>";
80
81     print "<blockquote style='background: #eee; border: 1px solid #333; padding: 5px;'>";
82     print "<br /><strong>ERROR: $err</strong></br >";
83     clearstatcache();
84     $files = array( "htaccess.dist", ".htaccess" );
85     while( list( $key, $val ) = each( $files ) ) {
86     $stats = @stat( $val );
87     if( $stats ) {
88         print "<h2>$val</h2>";
89         print "&nbsp;&nbsp;&nbsp;&nbsp;uid/gid: " . $stats[ 'uid' ] . "/" . $stats[ 'gid' ] . "<br />\n";
90         print "&nbsp;&nbsp;&nbsp;&nbsp;size: " . $stats[ 'size' ] . "<br />";
91         print "&nbsp;&nbsp;&nbsp;&nbsp;perms: " . substr( sprintf('%o', fileperms( $val ) ), -4 ) . "<br />";
92         print "&nbsp;&nbsp;&nbsp;&nbsp;readable: ";
93         print is_readable( $val ) == true ? "yes" : "no";
94         print "<br />";
95         print "&nbsp;&nbsp;&nbsp;&nbsp;writeable: ";
96         print is_writeable( $val ) == true ? "yes" : "no";
97         print "<br />";
98         
99     } elseif( file_exists( $val ) == false ) {
100         print "<h2>$val</h2>";
101         print "&nbsp;&nbsp;&nbsp;&nbsp;FILE NOT FOUND: $val<br>";
102     }
103     }
104     print "</blockquote>";
105
106 }
107
108 function do_htaccess( $oldfilename, $newfilename, $base, $url )
109 {
110     // remove ending slash from $base and $url
111     $htaccess = '';
112     if( substr($base, -1 ) == '/') {
113     $base = substr($base, 0, -1);
114     }
115
116     if( substr($url, -1 ) == '/') {
117     $url = substr($url, 0, -1);
118     }
119     $err = '';
120     if( is_file( $oldfilename ) ) {
121         $fp = @fopen( $oldfilename, "r" );
122         if( $fp ) {
123             while( !feof( $fp ) )
124             {
125                 $htaccess .= fgets( $fp, 4096 );
126             }
127             fclose( $fp );
128             $htaccess = str_replace( "BASE", $base, $htaccess );
129         if( touch( $newfilename ) ) {
130             $fp = fopen( $newfilename, "w" );
131             if( $fp ) {
132                 fwrite( $fp, $htaccess );
133                 fclose( $fp );
134             } else {
135                 $err = "could not open $newfilename for writing";
136             }
137         } else {
138             $err = "could not open $newfilename for writing";
139         }
140         } else {
141         $err = "could not open $oldfilename for reading";
142     }
143     } else {
144     $err = "$oldfilename not found";
145     }
146
147     if( $err != '' ) {
148         print "<h1>Warning!</h1>";
149         print "<p><strong>There was a problem creating the .htaccess file.</strong> </p>";
150         print "<p style='color: #900'>Error: ";
151         if( $err == "could not open $newfilename for writing" ) {
152         print "Could Not Write To $newfilename.";
153         } elseif( $err == "could not open $oldfilename for reading" ) {
154         print "I could not read from $oldfilename. ";
155         } elseif( $err == "$oldfilename not found" ) {
156         print "The file, $oldfilename, is missing.";
157         }
158         print "</p>";
159         filestats( $err );
160
161         print "<p>Please ensure that the webserver can write to this directory.</p>";
162         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>";
163         print "<p>If all else fails then you'll have to create it by hand:";
164         print "<ul><li> Download htaccess.dist to your computer and open it in your favourite text editor.</li>
165         <li> Replace the following text:<ul><li>BASE by '$base'</li><li>HOST by '$url'</li></li>
166         <li> Rename htaccess.dist to .htaccess and upload it back to the same directory.</li></ul>";
167         die( "Installation Aborted!" );
168     }
169 }
170
171 function checkdirs() {
172     $ret = true;
173     $ret = check_writeable_dir( dirname(__FILE__), $ret );
174     $ret = check_writeable_dir( dirname(__FILE__) . "/wp-content/", $ret );
175
176     if( $ret == false )
177     {
178         print "<h2>Warning!</h2>";
179         print "<div style='border: 1px solid #ccc'>";
180         print "<p style='font-weight: bold; padding-left: 10px'>One or more of the above directories must be made writeable by the webserver.<br>";
181         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>";
182         print "Refresh this page when you're done!<br></p>";
183         print "</div>";
184     }
185     if( file_exists( "./.htaccess" ) && is_writeable( "./.htaccess" ) == false ) {
186         $ret = false;
187         print "<h2>Warning! .htaccess already exists.</h2>";
188         print "<div style='border: 1px solid #ccc'>";
189         print "<p style='font-weight: bold; padding-left: 10px'>A file with the name '.htaccess' already exists in this directory and I cannot write to it. Please ftp to the server and delete this file from this directory!<br />";
190         print "Offending file: " . realpath( '.htaccess' ) . "</p>";
191         print "</div>";
192     }
193
194
195     return $ret;
196 }
197
198 function step1() {
199     print "<h2>Installing WP&micro;</h2>";
200     print "<p>Please make sure <code>mod_rewrite</code> is installed as it will be activated at the end of this install.</p>
201     <p>If the <code>mod_rewrite</code> module 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.</p>
202     <p>WPMU <strong>must be installed in the root directory of a domain</strong>, otherwise it won't work.";
203     if( checkdirs() == false ) {
204     return false;
205     }
206
207     // Create Blogs living area.
208     @mkdir( dirname(__FILE__) . "/wp-content/blogs.dir", 0777 );
209
210
211     $url = stripslashes( "http://".$_SERVER["SERVER_NAME"] . dirname( $_SERVER[ "SCRIPT_NAME" ] ) );
212     if( substr( $url, -1 ) == '/' )
213         $url = substr( $url, 0, -1 );
214     $base = stripslashes( dirname( $_SERVER["SCRIPT_NAME"] ) );
215     if( $base != "/")
216     {
217            $base .= "/";
218     }
219     $realpath = dirname(__FILE__);
220
221     return true;
222 }
223
224 function printstep1form( $dbname = 'wordpress', $uname = 'username', $pwd = 'password', $dbhost = 'localhost', $prefix = 'wp_' ) {
225     $weblog_title = 'My new WPMU Blog';
226     $email = '';
227     $hostname = str_replace( "www.", "", $_SERVER[ 'HTTP_HOST' ] );
228     print "
229     <form method='post' action='index.php'>
230     <input type='hidden' name='action' value='step2'>
231     <h2>Blog Addresses</h2>
232     <p>Please choose whether you would like blogs for the MU install to use sub-domains or sub-directories. You can not change this later. We recommend sub-domains.</p>
233     <p><label><input type='radio' name='vhost' value='yes' /> Sub-domains (like <code>blog1.example.com</code>)</label><br />
234     <label><input type='radio' name='vhost' value='no' /> Sub-directories (like <code>example.com/blog1</code></label></p>
235     
236     <h2>Database</h2>
237
238   <p>Below you should enter your database connection details. If you're not sure about these, contact your host. </p>
239   <table cellpadding='5'>
240     <tr>
241       <th scope='row' width='33%'>Database Name</th>
242       <td><input name='dbname' type='text' size='45' value='$dbname' /></td> 
243     </tr>
244     <tr>
245       <th scope='row'>User Name</th>
246       <td><input name='uname' type='text' size='45' value='$uname' /></td>
247     </tr>
248     <tr>
249       <th scope='row'>Password</th>
250       <td><input name='pwd' type='text' size='45' value='$pwd' /></td>
251     </tr>
252     <tr>
253       <th scope='row'>Database Host</th>
254       <td><input name='dbhost' type='text' size='45' value='$dbhost' /></td>
255     </tr>
256   </table>
257   <h2>Server Address</h2>
258   <p><label>What is the Internet address of your site? You should enter the shortest address possible. For example, use <em>example.com</em> instead of <em>www.example.com</em> but if you are going to use an address like <em>blogs.example.com</em> then enter that unaltered in the box above.<br /><b>Server Address:</b> <input type='text' name='basedomain' value='{$hostname}'></label></p>
259   <h2>Blog Details</h2>
260   <table width='100%'>
261   <tr>
262   <th scope='row'>Weblog&nbsp;Title</th>
263   <td><input name='weblog_title' type='text' size='45' value='".$weblog_title."' /></td>
264   <td>What would you like to call your weblog? </td>
265   </tr>
266   <tr>
267   <th scope='row'>Email</th>
268   <td><input name='email' type='text' size='45' value='".$email."' /></td>
269   <td>Your email address.</td>
270   </tr>
271   </table>
272   <p class='submit'><input name='submit' type='submit' value='Submit' /> </p>
273 </form> ";
274 }
275
276 function step2() {
277     global $wpdb, $table_prefix, $base, $blog_id;
278     $dbname  = $_POST['dbname'];
279     $uname   = $_POST['uname'];
280     $passwrd = $_POST['pwd'];
281     $dbhost  = $_POST['dbhost'];
282     $vhost   = $_POST['vhost' ];
283     $prefix  = 'wp_';
284     $base = stripslashes( dirname( $_SERVER["SCRIPT_NAME"] ) );
285     if( $base != "/") {
286            $base .= "/";
287     }
288
289     // Test the db connection.
290     define('DB_NAME', $dbname);
291     define('DB_USER', $uname);
292     define('DB_PASSWORD', $passwrd);
293     define('DB_HOST', $dbhost);
294
295     if (!file_exists('wp-config-sample.php'))
296     die('Sorry, I need a wp-config-sample.php file to work from. Please re-upload this file from your WordPress installation.');
297
298     $configFile = file('wp-config-sample.php');
299     // We'll fail here if the values are no good.
300     require_once('wp-includes/wp-db.php');
301     printheader();
302
303     print "Creating Database Config File: ";
304     
305     $handle = fopen('wp-config.php', 'w');
306
307     foreach ($configFile as $line_num => $line) {
308     switch (trim( substr($line,0,16) )) {
309         case "define('DB_NAME'":
310         fwrite($handle, str_replace("wordpress", $dbname, $line));
311         break;
312         case "define('DB_USER'":
313         fwrite($handle, str_replace("'username'", "'$uname'", $line));
314         break;
315         case "define('DB_PASSW":
316         fwrite($handle, str_replace("'password'", "'$passwrd'", $line));
317         break;
318         case "define('DB_HOST'":
319         fwrite($handle, str_replace("localhost", $dbhost, $line));
320         break;
321         case "define('VHOST',":
322         fwrite($handle, str_replace("VHOSTSETTING", $vhost, $line));
323         break;
324         case '$table_prefix  =':
325         fwrite($handle, str_replace('wp_', $prefix, $line));
326         break;
327         case '$base = \'BASE\';':
328         fwrite($handle, str_replace('BASE', $base, $line));
329         break;
330         default:
331         fwrite($handle, $line);
332         break;
333     }
334     }
335     fclose($handle);
336     chmod('wp-config.php', 0666);
337     print "<b style='color: #00aa00; font-weight: bold'>DONE</b><br />";
338 }
339
340 function printuserdetailsform( $weblog_title = 'My new Blog', $username = '', $email = '' ) {
341     $hostname = str_replace( "www.", "", $_SERVER[ 'HTTP_HOST' ] );
342     print "
343     <form method='post' action='index.php'>
344     <input type='hidden' name='action' value='step3'>
345     <p>To finish setting up your blog, please fill in the following form and click <q>Submit</q>.</p>
346     <input name='submit' type='submit' value='Submit' />
347     </form>
348     <br />
349     You will be sent an email with your password and login links and details.";
350 }
351
352 function step3() {
353     global $wpdb, $current_site;
354     $base = stripslashes( dirname( $_SERVER["SCRIPT_NAME"] ) );
355     if( $base != "/") {
356            $base .= "/";
357     }
358     $domain =   $wpdb->escape( $_POST[ 'basedomain' ] );
359     if( substr( $domain, 0, 4 ) == 'www.' )
360     $domain = substr( $domain, 4 );
361
362     $email = $wpdb->escape( $_POST[ 'email' ] );
363     $weblog_title = $wpdb->escape( $_POST[ 'weblog_title' ] );
364
365     // set up site tables
366     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'admin_email', '".$email."')" );
367     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'admin_user_id', '1')" );
368     $wpdb->query( "INSERT INTO ".$wpdb->site." ( id, domain, path ) VALUES ( NULL, '$domain', '$base' )" );
369     $wpdb->query( "INSERT INTO " . $wpdb->sitecategories . " VALUES (1, 'Uncategorized', 'uncategorized', '')" );
370     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'upload_filetypes', 'jpg jpeg png gif mp3 mov avi wmv midi mid pdf' )" );
371     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'blog_upload_space', '10' )" );
372     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'fileupload_maxk', '1500' )" );
373     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'site_admins', '" . serialize( array( 'admin' ) ) . "' )" );
374     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'illegal_names', '" . serialize( array(  "www", "web", "root", "admin", "main", "invite", "administrator" ) ) . "' )" );
375     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'welcome_email', 'Dear User,
376
377 Your new SITE_NAME blog has been successfully set up at:
378 BLOG_URL
379
380 You can log in to the administrator account with the following information:
381  Username: USERNAME
382  Password: PASSWORD
383 Login Here: BLOG_URLwp-login.php
384
385 We hope you enjoy your new weblog.
386  Thanks!
387
388 --The Team @ SITE_NAME')" );
389     $wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'first_post', 'Welcome to <a href=\"SITE_URL\">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' )" );
390
391     $pass = substr( md5( rand() ), 5, 12 );
392     $user_id = wpmu_create_user( 'admin', $pass, $email);
393
394     $current_site->domain = $domain;
395     $current_site->path = $base;
396     $current_site->site_name = ucfirst( $domain );
397     
398     wpmu_create_blog( $domain, $base, $weblog_title, $user_id, array() );
399     update_blog_option( 1, 'template', 'home');
400     update_blog_option( 1, 'stylesheet', 'home');
401     update_blog_option( 1, 'permalink_structure', '/blog/%year%/%monthnum%/%day%/%postname%/');
402     update_blog_option( 1, 'rewrite_rules', '');
403     $msg = "Your new WPMU site has been created at\nhttp://{$domain}{$base}\n\nLogin details:\nUsername: admin\nPassword: $pass\nLogin: http://{$domain}{$base}wp-login.php\n";
404     wp_mail( $email, "Your new WPMU site is ready!", $msg, "From: wordpress@" . $_SERVER[ 'HTTP_HOST' ]  );
405     print "<p>Congrats! Your <a href='http://{$domain}{$base}'>WPMU site</a> has been set up and you have been sent details of your login and password in an email.</p>";
406 }
407
408 switch( $_POST[ 'action' ] ) {
409     case "step2":
410         // get blog username
411         // create wp-config.php
412         step2();
413         // Install Blog!
414         include_once('./wp-config.php');
415         include_once('./wp-admin/upgrade-functions.php');
416         make_db_current_silent();
417         populate_options();
418         do_htaccess( 'htaccess.dist', '.htaccess', $base, '');
419         printheader();
420         step3();
421     break;
422     case "step3":
423         // call createBlog();
424         // create .htaccess
425         // print login info and links.
426         require_once('./wp-config.php');
427         require_once('./wp-admin/upgrade-functions.php');
428         make_db_current_silent();
429         populate_options();
430         do_htaccess( 'htaccess.dist', '.htaccess', $base, '');
431         printheader();
432         step3();
433     break;
434     default:
435         // check that directories are writeable.
436         // create wpmu-settings.php
437         // get db auth info.
438         printheader();
439         if( step1() ) {
440             printstep1form();
441         }
442     break;
443 }
444 ?>
445 <br /><br />
446 <div align='center'>
447 <a href="http://mu.wordpress.org/">WordPress &micro;</a> | <a href="http://mu.wordpress.org/forums/">Support Forums</a>
448 </div>
449 </body>
450 </html>
451
Note: See TracBrowser for help on using the browser.