Ticket #19796: 19796-hack-day.17.diff

File 19796-hack-day.17.diff, 28.3 KB (added by markjaquith, 8 months ago)
Line 
1Index: wp-admin/includes/ms.php
2===================================================================
3--- wp-admin/includes/ms.php    (revision 21802)
4+++ wp-admin/includes/ms.php    (working copy)
5@@ -674,7 +674,7 @@ function choose_primary_blog() {
6        <?php if ( in_array( get_site_option( 'registration' ), array( 'all', 'blog' ) ) ) : ?>
7                <tr>
8                        <th scope="row" colspan="2" class="th-full">
9-                               <a href="<?php echo apply_filters( 'wp_signup_location', network_home_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
10+                               <a href="<?php echo apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); ?>"><?php _e( 'Create a New Site' ); ?></a>
11                        </th>
12                </tr>
13        <?php endif; ?>
14Index: wp-admin/network.php
15===================================================================
16--- wp-admin/network.php        (revision 21802)
17+++ wp-admin/network.php        (working copy)
18@@ -51,8 +51,8 @@ function network_domain_check() {
19  * @return bool Whether subdomain install is allowed
20  */
21 function allow_subdomain_install() {
22-       $domain = preg_replace( '|https?://([^/]+)|', '$1', get_option( 'siteurl' ) );
23-       if( false !== strpos( $domain, '/' ) || 'localhost' == $domain || preg_match( '|[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|', $domain ) )
24+       $domain = preg_replace( '|https?://([^/]+)|', '$1', get_option( 'home' ) );
25+       if( parse_url( get_option( 'home' ), PHP_URL_PATH ) || 'localhost' == $domain || preg_match( '|^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|', $domain ) )
26                return false;
27 
28        return true;
29@@ -144,13 +144,6 @@ include( ABSPATH . 'wp-admin/admin-header.php' );
30 function network_step1( $errors = false ) {
31        global $is_apache;
32 
33-       if ( get_option( 'siteurl' ) != get_option( 'home' ) ) {
34-               echo '<div class="error"><p><strong>' . __('ERROR:') . '</strong> ' . sprintf( __( 'Your <strong>WordPress address</strong> must match your <strong>Site address</strong> before creating a Network. See <a href="%s">General Settings</a>.' ), esc_url( admin_url( 'options-general.php' ) ) ) . '</p></div>';
35-               echo '</div>';
36-               include ( ABSPATH . 'wp-admin/admin-footer.php' );
37-               die();
38-       }
39-
40        if ( defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
41                echo '<div class="error"><p><strong>' . __('ERROR:') . '</strong> ' . __( 'The constant DO_NOT_UPGRADE_GLOBAL_TABLES cannot be defined when creating a network.' ) . '</p></div>';
42                echo '</div>';
43@@ -314,21 +307,25 @@ function network_step1( $errors = false ) {
44  * @since 3.0.0
45  */
46 function network_step2( $errors = false ) {
47-       global $base, $wpdb;
48-       $hostname = get_clean_basedomain();
49+       global $wpdb;
50 
51-       if ( ! isset( $base ) )
52-               $base = trailingslashit( stripslashes( dirname( dirname( $_SERVER['SCRIPT_NAME'] ) ) ) );
53+       $hostname          = get_clean_basedomain();
54+       $slashed_home      = trailingslashit( get_option( 'home'    ) );
55+       $slashed_siteurl   = trailingslashit( get_option( 'siteurl' ) );
56+       $wp_siteurl_subdir = '/' . str_replace( $slashed_home, '', $slashed_siteurl );
57+       $rewrite_base      = ! empty( $wp_siteurl_subdir ) ? ltrim( trailingslashit( $wp_siteurl_subdir ), '/' ) : '';
58+       $base              = parse_url( $slashed_home, PHP_URL_PATH );
59 
60        // Wildcard DNS message.
61        if ( is_wp_error( $errors ) )
62                echo '<div class="error">' . $errors->get_error_message() . '</div>';
63 
64-       if ( $_POST ) {
65-               if ( allow_subdomain_install() )
66+       if ( ! empty( $_POST ) ) {
67+               if ( allow_subdomain_install() ) {
68                        $subdomain_install = allow_subdirectory_install() ? ! empty( $_POST['subdomain_install'] ) : true;
69-               else
70+               } else {
71                        $subdomain_install = false;
72+               }
73        } else {
74                if ( is_multisite() ) {
75                        $subdomain_install = is_subdomain_install();
76@@ -337,6 +334,10 @@ function network_step2( $errors = false ) {
77 <?php
78                } else {
79                        $subdomain_install = (bool) $wpdb->get_var( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = 1 AND meta_key = 'subdomain_install'" );
80+
81+       $subdir_match       = $subdomain_install ? '' : '(?:[_0-9a-zA-Z-]+/)?';
82+       $subdir_replacement = $subdomain_install ? '' : '$1';
83+
84 ?>
85        <div class="error"><p><strong><?php _e('Warning:'); ?></strong> <?php _e( 'An existing WordPress network was detected.' ); ?></p></div>
86        <p><?php _e( 'Please complete the configuration steps. To create a new network, you will need to empty or remove the network database tables.' ); ?></p>
87@@ -344,7 +345,7 @@ function network_step2( $errors = false ) {
88                }
89        }
90 
91-       if ( $_POST || ! is_multisite() ) {
92+       if ( ! empty( $_POST ) || ! is_multisite() ) {
93 ?>
94                <h3><?php esc_html_e( 'Enabling the Network' ); ?></h3>
95                <p><?php _e( 'Complete the following steps to enable the features for creating a network of sites.' ); ?></p>
96@@ -369,20 +370,25 @@ function network_step2( $errors = false ) {
97                                <textarea class="code" readonly="readonly" cols="100" rows="7">
98 define('MULTISITE', true);
99 define('SUBDOMAIN_INSTALL', <?php echo $subdomain_install ? 'true' : 'false'; ?>);
100-$base = '<?php echo $base; ?>';
101 define('DOMAIN_CURRENT_SITE', '<?php echo $hostname; ?>');
102 define('PATH_CURRENT_SITE', '<?php echo $base; ?>');
103 define('SITE_ID_CURRENT_SITE', 1);
104-define('BLOG_ID_CURRENT_SITE', 1);</textarea>
105+define('BLOG_ID_CURRENT_SITE', 1);
106+<?php if ( $wp_siteurl_subdir ): ?>
107+define('WP_SITEURL_SUBDIR', '<?php echo $wp_siteurl_subdir; ?>');
108+<?php endif; // $wp_siteurl_subdir ?>
109+</textarea>
110 <?php
111        $keys_salts = array( 'AUTH_KEY' => '', 'SECURE_AUTH_KEY' => '', 'LOGGED_IN_KEY' => '', 'NONCE_KEY' => '', 'AUTH_SALT' => '', 'SECURE_AUTH_SALT' => '', 'LOGGED_IN_SALT' => '', 'NONCE_SALT' => '' );
112        foreach ( $keys_salts as $c => $v ) {
113-               if ( defined( $c ) )
114+               if ( defined( $c ) ) {
115                        unset( $keys_salts[ $c ] );
116+               }
117        }
118+
119        if ( ! empty( $keys_salts ) ) {
120                $keys_salts_str = '';
121-               $from_api = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
122+               $from_api       = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
123                if ( is_wp_error( $from_api ) ) {
124                        foreach ( $keys_salts as $c => $v ) {
125                                $keys_salts_str .= "\ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
126@@ -404,41 +410,13 @@ define('BLOG_ID_CURRENT_SITE', 1);</textarea>
127 </li>
128 <?php
129        if ( iis7_supports_permalinks() ) :
130+               // IIS doesn't support RewriteBase, all your RewriteBase are belong to us
131+               $iis_subdir_match = ltrim( $base, '/' ) . $subdir_match;
132+               $iis_rewrite_base = ltrim( $base, '/' ) . $rewrite_base;
133+               $iis_subdir_replacement = $subdomain_install ? '' : '{R:1}';
134 
135-                       if ( $subdomain_install ) {
136-                               $web_config_file =
137-'<?xml version="1.0" encoding="UTF-8"?>
138-<configuration>
139-    <system.webServer>
140-        <rewrite>
141-            <rules>
142-                <rule name="WordPress Rule 1" stopProcessing="true">
143-                    <match url="^index\.php$" ignoreCase="false" />
144-                    <action type="None" />
145-                </rule>
146-                <rule name="WordPress Rule 2" stopProcessing="true">
147-                    <match url="^files/(.+)" ignoreCase="false" />
148-                    <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
149-                </rule>
150-                <rule name="WordPress Rule 3" stopProcessing="true">
151-                    <match url="^" ignoreCase="false" />
152-                    <conditions logicalGrouping="MatchAny">
153-                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
154-                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
155-                    </conditions>
156-                    <action type="None" />
157-                </rule>
158-                <rule name="WordPress Rule 4" stopProcessing="true">
159-                    <match url="." ignoreCase="false" />
160-                    <action type="Rewrite" url="index.php" />
161-                </rule>
162-            </rules>
163-        </rewrite>
164-    </system.webServer>
165-</configuration>';
166-                       } else {
167-                               $web_config_file =
168-'<?xml version="1.0" encoding="UTF-8"?>
169+               $web_config_file = <<<EOF
170+<?xml version="1.0" encoding="UTF-8"?>
171 <configuration>
172     <system.webServer>
173         <rewrite>
174@@ -448,12 +426,12 @@ define('BLOG_ID_CURRENT_SITE', 1);</textarea>
175                     <action type="None" />
176                 </rule>
177                 <rule name="WordPress Rule 2" stopProcessing="true">
178-                    <match url="^([_0-9a-zA-Z-]+/)?files/(.+)" ignoreCase="false" />
179-                    <action type="Rewrite" url="wp-includes/ms-files.php?file={R:2}" appendQueryString="false" />
180+                    <match url="^{$iis_subdir_match}files/(.+)" ignoreCase="false" />
181+                    <action type="Rewrite" url="{$iis_rewrite_base}wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
182                 </rule>
183                 <rule name="WordPress Rule 3" stopProcessing="true">
184-                    <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
185-                    <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
186+                    <match url="^{$iis_subdir_match}wp-admin$" ignoreCase="false" />
187+                    <action type="Redirect" url="{$iis_subdir_replacement}wp-admin/" redirectType="Permanent" />
188                 </rule>
189                 <rule name="WordPress Rule 4" stopProcessing="true">
190                     <match url="^" ignoreCase="false" />
191@@ -464,12 +442,12 @@ define('BLOG_ID_CURRENT_SITE', 1);</textarea>
192                     <action type="None" />
193                 </rule>
194                 <rule name="WordPress Rule 5" stopProcessing="true">
195-                    <match url="^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*)" ignoreCase="false" />
196-                    <action type="Rewrite" url="{R:1}" />
197+                    <match url="^{$iis_subdir_match}(wp-(content|admin|includes).*)" ignoreCase="false" />
198+                    <action type="Rewrite" url="{$iis_rewrite_base}{R:1}" />
199                 </rule>
200                 <rule name="WordPress Rule 6" stopProcessing="true">
201-                    <match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
202-                    <action type="Rewrite" url="{R:2}" />
203+                    <match url="^{$iis_subdir_match}([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
204+                    <action type="Rewrite" url="{$iis_rewrite_base}{R:2}" />
205                 </rule>
206                 <rule name="WordPress Rule 7" stopProcessing="true">
207                     <match url="." ignoreCase="false" />
208@@ -478,40 +456,39 @@ define('BLOG_ID_CURRENT_SITE', 1);</textarea>
209             </rules>
210         </rewrite>
211     </system.webServer>
212-</configuration>';
213-                       }
214+</configuration>
215+EOF;
216+
217        ?>
218-               <li><p><?php printf( __( 'Add the following to your <code>web.config</code> file in <code>%s</code>, replacing other WordPress rules:' ), ABSPATH ); ?></p>
219+               <li><p><?php printf( __( 'Add the following to your <code>web.config</code> file in <code>%s</code>, replacing other WordPress rules:' ), trailingslashit( str_replace( trailingslashit( $wp_siteurl_subdir ), '', ABSPATH ) ) ); ?></p>
220                <textarea class="code" readonly="readonly" cols="100" rows="20">
221-               <?php echo esc_textarea( $web_config_file ); ?>
222+<?php echo esc_textarea( $web_config_file ); ?>
223                </textarea></li>
224                </ol>
225 
226        <?php else : // end iis7_supports_permalinks(). construct an htaccess file instead:
227-
228-               $htaccess_file = 'RewriteEngine On
229-RewriteBase ' . $base . '
230+               $htaccess_file = <<<EOF
231+RewriteEngine On
232+RewriteBase {$base}
233 RewriteRule ^index\.php$ - [L]
234 
235 # uploaded files
236-RewriteRule ^' . ( $subdomain_install ? '' : '([_0-9a-zA-Z-]+/)?' ) . 'files/(.+) wp-includes/ms-files.php?file=$' . ( $subdomain_install ? 1 : 2 ) . ' [L]' . "\n";
237+RewriteRule ^{$subdir_match}files/(.+) {$rewrite_base}wp-includes/ms-files.php?file=$1 [L]
238 
239-               if ( ! $subdomain_install )
240-                       $htaccess_file .= "\n# add a trailing slash to /wp-admin\n" . 'RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]' . "\n";
241+# add a trailing slash to /wp-admin
242+RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement}wp-admin/ [R=301,L]
243 
244-               $htaccess_file .= "\n" . 'RewriteCond %{REQUEST_FILENAME} -f [OR]
245+RewriteCond %{REQUEST_FILENAME} -f [OR]
246 RewriteCond %{REQUEST_FILENAME} -d
247-RewriteRule ^ - [L]';
248-
249-               // @todo custom content dir.
250-               if ( ! $subdomain_install )
251-                       $htaccess_file .= "\nRewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]\nRewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]";
252-
253-               $htaccess_file .= "\nRewriteRule . index.php [L]";
254+RewriteRule ^ - [L]
255+RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}$1 [L]
256+RewriteRule ^{$subdir_match}(.*\.php)$ {$rewrite_base}$1 [L]
257+RewriteRule . index.php [L]
258+EOF;
259 
260                ?>
261-               <li><p><?php printf( __( 'Add the following to your <code>.htaccess</code> file in <code>%s</code>, replacing other WordPress rules:' ), ABSPATH ); ?></p>
262-               <textarea class="code" readonly="readonly" cols="100" rows="<?php echo $subdomain_install ? 11 : 16; ?>">
263+               <li><p><?php printf( __( 'Add the following to your <code>.htaccess</code> file in <code>%s</code>, replacing other WordPress rules:' ), trailingslashit( str_replace( trailingslashit( $wp_siteurl_subdir ), '', ABSPATH ) ) ); ?></p>
264+               <textarea class="code" readonly="readonly" cols="100" rows="16">
265 <?php echo esc_textarea( $htaccess_file ); ?></textarea></li>
266                </ol>
267 
268@@ -523,24 +500,26 @@ RewriteRule ^ - [L]';
269        }
270 }
271 
272-if ( $_POST ) {
273-
274-       $base = trailingslashit( stripslashes( dirname( dirname( $_SERVER['SCRIPT_NAME'] ) ) ) );
275+if ( ! empty( $_POST ) ) {
276 
277        check_admin_referer( 'install-network-1' );
278 
279        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
280+
281        // create network tables
282        install_network();
283-       $hostname = get_clean_basedomain();
284+
285+       $base              = parse_url( trailingslashit( get_option( 'home' ) ), PHP_URL_PATH );
286        $subdomain_install = allow_subdomain_install() ? !empty( $_POST['subdomain_install'] ) : false;
287+
288        if ( ! network_domain_check() ) {
289                $result = populate_network( 1, get_clean_basedomain(), sanitize_email( $_POST['email'] ), stripslashes( $_POST['sitename'] ), $base, $subdomain_install );
290                if ( is_wp_error( $result ) ) {
291-                       if ( 1 == count( $result->get_error_codes() ) && 'no_wildcard_dns' == $result->get_error_code() )
292+                       if ( 1 == count( $result->get_error_codes() ) && 'no_wildcard_dns' == $result->get_error_code() ) {
293                                network_step2( $result );
294-                       else
295+                       } else {
296                                network_step1( $result );
297+                       }
298                } else {
299                        network_step2();
300                }
301Index: wp-admin/network/site-new.php
302===================================================================
303--- wp-admin/network/site-new.php       (revision 21802)
304+++ wp-admin/network/site-new.php       (working copy)
305@@ -62,10 +62,10 @@ if ( isset($_REQUEST['action']) && 'add-site' == $_REQUEST['action'] ) {
306 
307        if ( is_subdomain_install() ) {
308                $newdomain = $domain . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
309-               $path = $base;
310+               $path      = $current_site->path;
311        } else {
312                $newdomain = $current_site->domain;
313-               $path = $base . $domain . '/';
314+               $path      = $current_site->path . $domain . '/';
315        }
316 
317        $password = 'N/A';
318Index: wp-includes/canonical.php
319===================================================================
320--- wp-includes/canonical.php   (revision 21802)
321+++ wp-includes/canonical.php   (working copy)
322@@ -18,7 +18,7 @@
323  * one or the other.
324  *
325  * Prevents redirection for feeds, trackbacks, searches, comment popup, and
326- * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7,
327+ * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7,
328  * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
329  * requests.
330  *
331@@ -282,7 +282,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
332 
333                if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
334                        if ( is_multisite() )
335-                               $redirect_url = apply_filters( 'wp_signup_location', site_url( 'wp-signup.php' ) );
336+                               $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
337                        else
338                                $redirect_url = site_url( 'wp-login.php?action=register' );
339                        wp_redirect( $redirect_url, 301 );
340@@ -517,7 +517,7 @@ function wp_redirect_admin_locations() {
341                site_url( 'dashboard', 'relative' ),
342                site_url( 'admin', 'relative' ),
343        );
344-       if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
345+       if ( parse_url( admin_url( '', 'relative' ), PHP_URL_PATH ) !== $_SERVER['REQUEST_URI'] && in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
346                wp_redirect( admin_url() );
347                exit;
348        }
349Index: wp-includes/formatting.php
350===================================================================
351--- wp-includes/formatting.php  (revision 21802)
352+++ wp-includes/formatting.php  (working copy)
353@@ -1355,6 +1355,46 @@ function untrailingslashit($string) {
354 }
355 
356 /**
357+ * Removes trailing and leading strings from elements (array), then combines elements
358+ * with a string in between each element. Leading and trailing strings should be
359+ * manually concatenated.
360+ *
361+ * @since  3.5.0
362+ *
363+ * @param  array $parts Items to be combined
364+ * @param  string $string String to be used to combine items
365+ * @return  string Concatenated parts with given strings in between each element.
366+ */
367+function join_with_string( $parts, $string ) {
368+       $members = array();
369+       foreach ( (array) $parts as $part ) {
370+               if ( is_array( $part ) ) {
371+                       foreach ( $part as $member ) {
372+                               $members[] = trim( $member, $string );
373+                       }
374+               } else {
375+                       $members[] = trim( $part, $string );
376+               }
377+       }
378+
379+       return implode( $string, $members );
380+}
381+
382+/**
383+ * Removes trailing and leading slashes from elements (array or string args), then combines elements
384+ * with a slash in between each element. Leading and trailing slashes should be
385+ * manually concatenated.
386+ *
387+ * @since  3.5.0
388+ *
389+ * @return  string Concatenated string with slashes in between each element.
390+ */
391+function join_with_slashes() {
392+       $args = func_get_args();
393+       return join_with_string( $args, '/' );
394+}
395+
396+/**
397  * Adds slashes to escape strings.
398  *
399  * Slashes will first be removed if magic_quotes_gpc is set, see {@link
400Index: wp-includes/functions.php
401===================================================================
402--- wp-includes/functions.php   (revision 21802)
403+++ wp-includes/functions.php   (working copy)
404@@ -1434,6 +1434,8 @@ function get_temp_dir() {
405 function wp_upload_dir( $time = null ) {
406        global $_wp_switched;
407        $siteurl = get_option( 'siteurl' );
408+       $homeurl = get_option( 'home' );
409+       $baseurl = is_multisite() && defined( 'MULTISITE' ) ? $homeurl : $siteurl;
410        $upload_path = get_option( 'upload_path' );
411        $upload_path = trim($upload_path);
412        $main_override = is_multisite() && defined( 'MULTISITE' ) && is_main_site();
413@@ -1453,12 +1455,12 @@ function wp_upload_dir( $time = null ) {
414                if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
415                        $url = WP_CONTENT_URL . '/uploads';
416                else
417-                       $url = trailingslashit( $siteurl ) . $upload_path;
418+                       $url = trailingslashit( $baseurl ) . $upload_path;
419        }
420 
421        if ( defined('UPLOADS') && ! $main_override && ! $_wp_switched ) {
422                $dir = ABSPATH . UPLOADS;
423-               $url = trailingslashit( $siteurl ) . UPLOADS;
424+               $url = trailingslashit( $baseurl ) . UPLOADS;
425        }
426 
427        if ( is_multisite() && ! $main_override && ! $_wp_switched  ) {
428Index: wp-includes/link-template.php
429===================================================================
430--- wp-includes/link-template.php       (revision 21802)
431+++ wp-includes/link-template.php       (working copy)
432@@ -1947,11 +1947,14 @@ function site_url( $path = '', $scheme = null ) {
433  * @return string Site url link with optional path appended.
434 */
435 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
436-       if ( empty( $blog_id ) || !is_multisite() ) {
437-               $url = get_option( 'siteurl' );
438+       if ( empty( $blog_id ) ) {
439+               if ( is_multisite() )
440+                       $url = get_option( 'home' );
441+               else
442+                       $url = get_option( 'siteurl' );
443        } else {
444                switch_to_blog( $blog_id );
445-               $url = get_option( 'siteurl' );
446+               $url = get_option( 'home' );
447                restore_current_blog();
448        }
449 
450@@ -2057,7 +2060,7 @@ function plugins_url($path = '', $plugin = '') {
451        else
452                $url = WP_PLUGIN_URL;
453 
454-       
455+
456        $url = set_url_scheme( $url );
457 
458        if ( !empty($plugin) && is_string($plugin) ) {
459Index: wp-includes/ms-functions.php
460===================================================================
461--- wp-includes/ms-functions.php        (revision 21802)
462+++ wp-includes/ms-functions.php        (working copy)
463@@ -586,8 +586,8 @@ function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
464                $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
465                $path = $base;
466        } else {
467-               $mydomain = "$domain";
468-               $path = $base.$blogname.'/';
469+               $mydomain = $domain;
470+               $path = '/' . join_with_slashes( $base, $blogname ) . '/';
471        }
472        if ( domain_exists($mydomain, $path) )
473                $errors->add('blogname', __('Sorry, that site already exists!'));
474@@ -1133,7 +1133,7 @@ function install_blog($blog_id, $blog_title = '') {
475 
476        $wpdb->suppress_errors(false);
477 
478-       $url = get_blogaddress_by_id($blog_id);
479+       $homeurl = $siteurl = untrailingslashit( get_blogaddress_by_id( $blog_id ) );
480 
481        // Set everything up
482        make_db_current_silent( 'blog' );
483@@ -1141,20 +1141,21 @@ function install_blog($blog_id, $blog_title = '') {
484        populate_roles();
485        $wp_roles->_init();
486 
487-       $url = untrailingslashit( $url );
488-       // fix url.
489-       update_option('siteurl', $url);
490-       update_option('home', $url);
491-       update_option('fileupload_url', $url . "/files" );
492-       update_option('upload_path', UPLOADBLOGSDIR . "/$blog_id/files");
493+       // Maybe fix url
494+       if ( defined( 'WP_SITEURL_SUBDIR' ) && WP_SITEURL_SUBDIR )
495+               $siteurl = join_with_slashes( $siteurl, WP_SITEURL_SUBDIR );
496+
497+       update_option('siteurl', $siteurl);
498+       update_option('home', $homeurl);
499+       update_option('fileupload_url', join_with_slashes( $homeurl, 'files' ) );
500+       update_option('upload_path', join_with_slashes( UPLOADBLOGSDIR, $blog_id, 'files' ) );
501        update_option('blogname', stripslashes( $blog_title ) );
502        update_option('admin_email', '');
503        $wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
504 
505        // remove all perms
506-       $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix.'user_level' ) );
507-
508-       $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix.'capabilities' ) );
509+       $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix . 'user_level'   ) );
510+       $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix . 'capabilities' ) );
511 
512        $wpdb->suppress_errors( false );
513 }
514Index: wp-includes/ms-load.php
515===================================================================
516--- wp-includes/ms-load.php     (revision 21802)
517+++ wp-includes/ms-load.php     (working copy)
518@@ -142,6 +142,7 @@ function wpmu_current_site() {
519                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
520                $current_site->domain = DOMAIN_CURRENT_SITE;
521                $current_site->path   = $path = PATH_CURRENT_SITE;
522+
523                if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
524                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
525                elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
526Index: wp-includes/ms-settings.php
527===================================================================
528--- wp-includes/ms-settings.php (revision 21802)
529+++ wp-includes/ms-settings.php (working copy)
530@@ -86,7 +86,7 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) {
531                        if ( '%siteurl%' == $destination )
532                                $destination = "http://" . $current_site->domain . $current_site->path;
533                } else {
534-                       $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
535+                       $destination = 'http://' . $current_site->domain . $current_site->wp_siteurl_subdir . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
536                }
537                header( 'Location: ' . $destination );
538                die();
539Index: wp-includes/rewrite.php
540===================================================================
541--- wp-includes/rewrite.php     (revision 21802)
542+++ wp-includes/rewrite.php     (working copy)
543@@ -1726,75 +1726,17 @@ class WP_Rewrite {
544                <rewrite>
545                        <rules>';
546                }
547-               if ( !is_multisite() ) {
548-                       $rules .= '
549-                               <rule name="wordpress" patternSyntax="Wildcard">
550-                                       <match url="*" />
551-                                               <conditions>
552-                                                       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
553-                                                       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
554-                                               </conditions>
555-                                       <action type="Rewrite" url="index.php" />
556-                               </rule>';
557-               } else {
558-                       if (is_subdomain_install()) {
559-                               $rules .= '
560-                               <rule name="wordpress - Rule 1" stopProcessing="true">
561-                                       <match url="^index\.php$" ignoreCase="false" />
562-                                       <action type="None" />
563-                               </rule>
564-                               <rule name="wordpress - Rule 2" stopProcessing="true">
565-                                       <match url="^files/(.+)" ignoreCase="false" />
566-                                       <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
567-                               </rule>
568-                               <rule name="wordpress - Rule 3" stopProcessing="true">
569-                                       <match url="^" ignoreCase="false" />
570-                                       <conditions logicalGrouping="MatchAny">
571-                                               <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
572-                                               <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
573-                                       </conditions>
574-                                       <action type="None" />
575-                               </rule>
576-                               <rule name="wordpress - Rule 4" stopProcessing="true">
577-                                       <match url="." ignoreCase="false" />
578-                                       <action type="Rewrite" url="index.php" />
579-                               </rule>';
580-                       } else {
581-                               $rules .= '
582-                               <rule name="wordpress - Rule 1" stopProcessing="true">
583-                                       <match url="^index\.php$" ignoreCase="false" />
584-                                       <action type="None" />
585-                               </rule>
586-                               <rule name="wordpress - Rule 2" stopProcessing="true">
587-                                       <match url="^([_0-9a-zA-Z-]+/)?files/(.+)" ignoreCase="false" />
588-                                       <action type="Rewrite" url="wp-includes/ms-files.php?file={R:2}" appendQueryString="false" />
589-                               </rule>
590-                               <rule name="wordpress - Rule 3" stopProcessing="true">
591-                                       <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
592-                                       <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
593-                               </rule>
594-                               <rule name="wordpress - Rule 4" stopProcessing="true">
595-                                       <match url="^" ignoreCase="false" />
596-                                       <conditions logicalGrouping="MatchAny">
597-                                               <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
598-                                               <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
599+
600+               $rules .= '
601+                       <rule name="wordpress" patternSyntax="Wildcard">
602+                               <match url="*" />
603+                                       <conditions>
604+                                               <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
605+                                               <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
606                                        </conditions>
607-                                       <action type="None" />
608-                               </rule>
609-                               <rule name="wordpress - Rule 5" stopProcessing="true">
610-                                       <match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
611-                                       <action type="Rewrite" url="{R:1}" />
612-                               </rule>
613-                               <rule name="wordpress - Rule 6" stopProcessing="true">
614-                                       <match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
615-                                       <action type="Rewrite" url="{R:2}" />
616-                               </rule>
617-                               <rule name="wordpress - Rule 7" stopProcessing="true">
618-                                       <match url="." ignoreCase="false" />
619-                                       <action type="Rewrite" url="index.php" />
620-                               </rule>';
621-                       }
622-               }
623+                               <action type="Rewrite" url="index.php" />
624+                       </rule>';
625+
626                if ( $add_parent_tags ) {
627                        $rules .= '
628                        </rules>
629Index: wp-login.php
630===================================================================
631--- wp-login.php        (revision 21802)
632+++ wp-login.php        (working copy)
633@@ -509,7 +509,7 @@ break;
634 case 'register' :
635        if ( is_multisite() ) {
636                // Multisite uses wp-signup.php
637-               wp_redirect( apply_filters( 'wp_signup_location', site_url('wp-signup.php') ) );
638+               wp_redirect( apply_filters( 'wp_signup_location', network_site_url('wp-signup.php') ) );
639                exit;
640        }
641 
642Index: wp-signup.php
643===================================================================
644--- wp-signup.php       (revision 21802)
645+++ wp-signup.php       (working copy)
646@@ -390,7 +390,7 @@ $current_user = wp_get_current_user();
647 if ( $active_signup == 'none' ) {
648        _e( 'Registration has been disabled.' );
649 } elseif ( $active_signup == 'blog' && !is_user_logged_in() ) {
650-       $login_url = site_url( 'wp-login.php?redirect_to=' . urlencode( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . '/wp-signup.php' ) ) );
651+       $login_url = site_url( 'wp-login.php?redirect_to=' . urlencode( set_url_scheme( network_site_url( 'wp-signup.php' ) ) ) );
652        echo sprintf( __( 'You must first <a href="%s">log in</a>, and then you can create a new site.' ), $login_url );
653 } else {
654        $stage = isset( $_POST['stage'] ) ?  $_POST['stage'] : 'default';