root/tags/release-1.0/index-install.php

Revision 747, 17.8 kB (checked in by donncha, 2 years ago)

Make config file world readable

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