<?php
/**
 * This sunrise.php file is meant to separate the user tables to each
 * blog inside a Multisite Network install.
 * 
 * 
 * 
 * NOTE: Don't use this file in a production environment.
 * Not stable. Use only for test purposes.
 * 
 * 
 * 
 * Instalation Procedure: 
 * ----------------------
 * 1. Put inside wp-contents/sunrise.php
 * 2. Write <code>define('SUNRISE', true);</code> in wp-contents.php
 * 3. Put blog-user-tables.php inside wp-contents/mu-plugins/
 * 4. Test and have fun! :)
 * 
 * 
 * @package WordPress
 * @since 3.4
 */


/**
 * Define an important constant
 */
if ( !defined( 'SUNRISE_LOADED' ) )
	define( 'SUNRISE_LOADED', 1 );
	
	
/**
 * Let's check for the presence of 
 * wp-content/mu-plugins/blog-user-tables.php plugin which is necessary
 * to create the required custom user tables for each blog.
 */
if ( defined( 'WPMU_PLUGIN_DIR' ) ) :
	$mu_plugins_dir = WPMU_PLUGIN_DIR;

else :
	$mu_plugins_dir = WP_CONTENT_DIR . '/mu-plugins';

endif;


if ( !file_exists( $mu_plugins_dir . '/blog-user-tables.php' ) )
	die( 'The file blog-user-tables.php must be present on Mu-plugins dir or you should remove the code from wp-contents/sunrise.php' );



/**
 * Load $wpdb object if not already loaded
 */
if ( function_exists( 'require_wp_db' ) ) {
	require_wp_db();
	
}


/**
 * Now its time to fill $blog_id and $site_id variables with the correct
 * ID because, as multisite is not yet fully loaded, we can't know what
 * blog was requested., so we have to fetch it from the DB.
 * 
 * Why do we need this?
 * The goal is to dynamically set CUSTOM_USER_TABLE and 
 * CUSTOM_USER_META_TABLE constants with values like:
 * wp_2_users and wp_2_usermeta for blog #2
 * wp_3_users and wp_3_usermeta for blog #3
 * ... and so on!
 * 
 * We need to know the current blog ID to achieve this.
 * Meanwhile, we will set global variables related to multisite
 * $blog_id, $site_id and $current_blog too.
 * 
 */

global $blog_id, $site_id, $current_blog, $table_prefix;


 
/**
 * Lets take the Domain value from the server. We test HTTP_HOST and
 * SERVER_NAME elements, depending on the server configuration either
 * the first or the second (or even both) are set.
 */
if ( isset( $_SERVER['HTTP_HOST'] ) ) :
	$current_domain = $_SERVER['HTTP_HOST'];

elseif ( isset( $_SERVER['SERVER_NAME'] ) ) :
	$current_domain = $_SERVER['SERVER_NAME'];

endif;



/**
 * Lets check if requested domain has www. if so there is a possibility
 * that the registered domain in thd DB starts with www.
 * Let's check both...
 */
if ( ( $nowww = preg_replace( '|^www\.|', '', $current_domain ) ) != $current_domain ) :
	$where = $wpdb->prepare( "domain IN ( %s, %s )", $wpdb->escape( $current_domain ), $wpdb->escape( $nowww ) );

else :
	$where = $wpdb->prepare( "domain = %s", $wpdb->escape( $current_domain ) );

endif;



/**
 * If the domain is set and the $table_prefix variable set in 
 * wp-config.php then
 * Everything is prepared to get the blog ID. Let's Rock! :)
 */
if ( $current_domain && $table_prefix ) {
	
	/** Get the ID from the blogs table */
	$current_blog = $wpdb->get_row( "SELECT * FROM {$table_prefix}blogs WHERE {$where} LIMIT 1" );
	
	/** If the domain was found in the table, let's set the variables and proceed */
	if ( !empty( $current_blog ) ) {
		
		/** Fill the global variables! :) */
		$blog_id = $current_blog->blog_id;
		$site_id = $current_blog->site_id;
		
		
		/**
		 * Let's change the cookie domain for this domain
		 * TODO: Test if this can lead into issues
		 */
		if ( !defined( 'COOKIE_DOMAIN' ) )
			define( 'COOKIE_DOMAIN', $current_domain );
		
		/**
		 * Last but not least:
		 * Set the custom user tables but only if it is not the primary blog
		 * We will use the main wp_users and wp_usermeta tables just for
		 * the primary blog.
		 */
		if ( $blog_id != 1 ) {
			if ( !defined( 'CUSTOM_USER_TABLE' ) )
				define( 'CUSTOM_USER_TABLE', $table_prefix.$blog_id.'_users' );
				
			if ( !defined( 'CUSTOM_USER_META_TABLE' ) )
				define( 'CUSTOM_USER_META_TABLE', $table_prefix.$blog_id.'_usermeta' );
		
			/** 
			 * We have to hack and change the global variables in $wpdb
			 * object because at this time they are already set.
			 */
			$wpdb->users = CUSTOM_USER_TABLE;
			$wpdb->usermeta = CUSTOM_USER_META_TABLE;
			
			$wpdb->global_tables[0] = $blog_id.'_users';
			$wpdb->global_tables[1] = $blog_id.'_usermeta';
			
			/** 
			 * For compatibility with plugins, I think event
			 * if they are not necessary, we set this just
			 * to organize this!
			 */
			$wpdb->tables[] = 'users';
			$wpdb->tables[] = 'usermeta';
		
		}
		
	}
	
}
