Index: trunk/xpressme_integration_kit/include/oninstall.php
===================================================================
--- trunk/include/oninstall.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/oninstall.php	(revision 232)
@@ -3,5 +3,5 @@
 $mydirname = basename($mydirpath);
 $lang = @$GLOBALS["xoopsConfig"]['language'];
-global $wp_db_version;
+global $wp_db_version,$wp_rewrite;
 include_once $mydirpath .'/wp-includes/version.php' ;
 
@@ -39,8 +39,13 @@
 //xpress
 	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles, $wp_query;
+	global $xoops_config;
+		
 	define("WP_INSTALLING", true);
 	$site_url= XOOPS_URL."/modules/".$mydirname;
 	$mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
 	$path = $mydirpath . '/';
+	$site_name = ucfirst($mydirname) . ' ' . _MI_XP2_NAME;
+	
+// install WordPress
 	if (file_exists($path . 'wp-load.php')) {
 		require_once $path . 'wp-load.php';
@@ -54,65 +59,11 @@
 	
 	$option_desc = __('WordPress web address');
-	$wpdb->query("INSERT INTO $wpdb->options (option_id, blog_id, option_name,option_value, autoload) VALUES ('1', '0', 'siteurl','$site_url', 'yes')");	
-	$wpdb->query("INSERT INTO $wpdb->options (option_id, blog_id, option_name,option_value, autoload) VALUES ('1', '0', 'home','$site_url', 'yes')");
+	$wpdb->query("INSERT INTO $wpdb->options (blog_id, option_name,option_value, autoload) VALUES ('0', 'siteurl','$site_url', 'yes')");	
+	$wpdb->query("INSERT INTO $wpdb->options (blog_id, option_name,option_value, autoload) VALUES ('0', 'home','$site_url', 'yes')");
 
 	populate_options();
 	populate_roles();
 	
-// Rewrite Option for Xpress
-//	update_option("blog_charset", wp_blog_charset());
-	update_option('blogname', ucfirst($mydirname) . ' ' . _MI_XPRESS_NAME );	
-	update_option('blogdescription', 'WordPress for XOOPS');
-	update_option("admin_email", $GLOBALS["xoopsConfig"]['adminmail']);
-	update_option("ping_sites", "http://rpc.pingomatic.com/\nhttp://ping.xoopsforge.com/");
-	update_option("home", $site_url);
-	update_option("siteurl", $site_url);
-	update_option("what_to_show", "posts");
-	update_option('default_pingback_flag', 0);
-	$ret[] = 'The initial data was written in the data base of wordpress.';
-	
-	update_option("template", "xpress_default");
-	update_option("stylesheet", "xpress_default");
-	$ret[] = 'The default theme of wordpress was set to xpress_default.';
-	/* add new option for uploads */
-//	update_option('uploads_use_yearmonth_folders', 1);
-	update_option('upload_path', 'wp-content/uploads');
-	
-		
-// Admin User Data write
-	// Change uid field
-	$wpdb->query("ALTER TABLE $wpdb->posts CHANGE `post_author` `post_author` mediumint(8) NOT NULL DEFAULT '0'");
-	$user_name = is_object($GLOBALS["xoopsUser"])?$GLOBALS["xoopsUser"]->getVar("uname"):'admin';
-	$email = is_object($GLOBALS["xoopsUser"])?$GLOBALS["xoopsUser"]->getVar("email"):'foo@exsample.com';
-	$pass_md5 = is_object($GLOBALS["xoopsUser"])?$GLOBALS["xoopsUser"]->getVar("pass"):'';
-	
-	$user_id = username_exists($user_name);
-	if ( !$user_id ) {
-		$random_password = 'admin';
-		$user_id = wp_create_user($user_name, $random_password, $email);
-	} else {
-		$random_password = __('User already exists.  Password inherited.');
-	}
-
-	$user = new WP_User($user_id);
-	$user->set_role('administrator');
-	'User ' . $user_name . ' of the administrator was made.';
-	// over write xoops md5 password 
-	$sql = "UPDATE $wpdb->users SET user_pass ='$pass_md5' WHERE ID = $user_id";
-	$wpdb->query($sql);
-	$ret[] = 'The password of XOOPS was copied.';
-	// Set Default data
-	wp_install_defaults($user_id);
-	$ret[] = 'The first sample post & comment was written.';
-//	    generate_page_uri_index();
-
-	/* activate the XPressME plugin */
-	$plugin_current = "xpressme/xpressme.php";
-	update_option('active_plugins', array($plugin_current));
-	include_once(dirname(__FILE__) . '/../wp-content/plugins/'.$plugin_current);
-	do_action('activate_'.$plugin_current);
-	$ret[] = 'The xpressme plug-in was activated.';
-	
-	// create views table
+// create XPressME table
 	$xp_prefix = $mydirname;
 	if ($xp_prefix == 'wordpress'){
@@ -164,7 +115,80 @@
 	$wpdb->query($sql);
 	
-	// make templates
+// make templates
 	include_once XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/include/xpress_templates_make.php' ;
 	$t_mess = xpress_templates_make($mid,$mydirname);
+	
+// Admin User Data write
+	// Change uid field
+	$wpdb->query("ALTER TABLE $wpdb->posts CHANGE `post_author` `post_author` mediumint(8) NOT NULL DEFAULT '0'");
+	$user_name = is_object($GLOBALS["xoopsUser"])?$GLOBALS["xoopsUser"]->getVar("uname"):'admin';
+	$email = is_object($GLOBALS["xoopsUser"])?$GLOBALS["xoopsUser"]->getVar("email"):'foo@exsample.com';
+	$pass_md5 = is_object($GLOBALS["xoopsUser"])?$GLOBALS["xoopsUser"]->getVar("pass"):'';
+	
+	$user_id = username_exists($user_name);
+	if ( !$user_id ) {
+		$random_password = 'admin';
+		if (!$xoops_config->is_wpmu){
+			$user_id = wp_create_user($user_name, $random_password, $email);
+		} else {
+			$user_id = wpmu_create_user($user_name, $random_password, $email);
+		}
+	} else {
+		$random_password = __('User already exists.  Password inherited.');
+	}
+
+	$user = new WP_User($user_id);
+	$user->set_role('administrator');
+	'User ' . $user_name . ' of the administrator was made.';
+	// over write xoops md5 password 
+	$sql = "UPDATE $wpdb->users SET user_pass ='$pass_md5' WHERE ID = $user_id";
+	$wpdb->query($sql);
+	$ret[] = 'The password of XOOPS was copied.';
+	
+	
+// Set Default data
+	if (!$xoops_config->is_wpmu){	// for WordPress 
+		// make WordPress Default data	
+		wp_install_defaults($user_id);
+		$ret[] = 'The first sample post & comment was written.';
+		
+		// Rewrite Option for Xpress
+		//	update_option("blog_charset", wp_blog_charset());
+			update_option('blogname', $site_name );	
+			update_option('blogdescription', 'WordPress for XOOPS');
+			update_option("admin_email", $GLOBALS["xoopsConfig"]['adminmail']);
+			update_option("ping_sites", "http://rpc.pingomatic.com/\nhttp://ping.xoopsforge.com/");
+			update_option("home", $site_url);
+			update_option("siteurl", $site_url);
+			update_option("what_to_show", "posts");
+			update_option('default_pingback_flag', 0);
+			$ret[] = 'The initial data was written in the data base of wordpress.';
+			
+			update_option("template", "xpress_default");
+			update_option("stylesheet", "xpress_default");
+			$ret[] = 'The default theme of wordpress was set to xpress_default.';
+		//	update_option('uploads_use_yearmonth_folders', 1);
+			update_option('upload_path', 'wp-content/uploads');
+			
+	} else {	// for WordPress MU
+		global $base,$current_site, $dirs, $wpmu_version;
+		
+		$base =  $xoops_config->mu_path_current_site;
+		$domain = $xoops_config->mu_domain_current_site;
+		$admin_email = $GLOBALS["xoopsConfig"]['adminmail'];
+		$admin_user_name = $user_name;
+		$admin_user_id = $user_id;
+		include_once($mydirpath . '/include/xpress_mu_function.php');
+		
+		do_htaccess( $xoops_config->module_path . '/htaccess.dist', $xoops_config->module_path . '/.htaccess', $base, '');
+		xpress_mu_install_defaults($domain,$base,$site_name,$admin_user_id,$admin_user_name,$admin_email);
+	}
+	
+// activate the xpressme plugin
+	require_once dirname( __FILE__ ).'/xpress_active_plugin.php';
+	if (xpress_pulugin_activation('xpressme/xpressme.php')){
+		$ret[] = 'The xpressme plug-in was activated.';
+	}
+
 	$ret = array_merge($ret,$t_mess);
 
Index: trunk/xpressme_integration_kit/include/onuninstall.php
===================================================================
--- trunk/include/onuninstall.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/onuninstall.php	(revision 232)
@@ -24,23 +24,34 @@
 	$db =& Database::getInstance() ;
 	$mid = $module->getVar('mid') ;
-/*
-	// TABLES (loading mysql.sql)
-	$sql_file_path = dirname(__FILE__).'/sql/mysql.sql' ;
-	$prefix_mod = $db->prefix() . '_' . $mydirname ;
-	if( file_exists( $sql_file_path ) ) {
-		$ret[] = "SQL file found at <b>".htmlspecialchars($sql_file_path)."</b>.<br  /> Deleting tables...<br />";
-		$sql_lines = file( $sql_file_path ) ;
-		foreach( $sql_lines as $sql_line ) {
-			if( preg_match( '/^CREATE TABLE \`?([a-zA-Z0-9_-]+)\`? /i' , $sql_line , $regs ) ) {
-				$sql = 'DROP TABLE '.$prefix_mod.'_'.$regs[1] ;
-				if (!$db->query($sql)) {
-					$ret[] = '<span style="color:#ff0000;">ERROR: Could not drop table <b>'.htmlspecialchars($prefix_mod.'_'.$regs[1]).'<b>.</span><br />';
-				} else {
-					$ret[] = 'Table <b>'.htmlspecialchars($prefix_mod.'_'.$regs[1]).'</b> dropped.<br />';
-				}
+	
+	$xp_prefix = $mydirname;
+	if ($xp_prefix == 'wordpress'){
+	 	$xp_prefix = 'wp';
+	}
+	
+	$xoops_prefix = $db->prefix();
+
+	if (empty($xoops_prefix) || empty($xp_prefix)) {
+		$ret[] = '<span style="color:#ff0000;">ERROR: Empty Prefix.</span><br />';
+		return false;
+	}
+	
+	$prefix_mod = $xoops_prefix  . '_' . $xp_prefix;
+	$sql = "SHOW TABLES LIKE '$prefix_mod%'";
+	if ($result = $db->query($sql)) {
+		while ($table = $db->fetchRow($result)){
+			$drop_sql = 'DROP TABLE '. $table[0] ;
+			if (!$db->queryF($drop_sql)) {
+				$ret[] = '<span style="color:#ff0000;">ERROR: Could not drop table <b>'.htmlspecialchars($table[0]).'<b>.</span><br />';
+			} else {
+				$ret[] = 'Table <b>'.htmlspecialchars($table[0]).'</b> dropped.<br />';
 			}
 		}
+	} else {
+		$ret[] = '<span style="color:#ff0000;">ERROR: Table not found of prefix <b>'.htmlspecialchars($prefix_mod).'<b> .</span><br />';
+		return false;
 	}
-*/
+
+
 	// TEMPLATES (Not necessary because modulesadmin removes all templates)
 	/* $tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
Index: trunk/xpressme_integration_kit/include/onupdate.php
===================================================================
--- trunk/include/onupdate.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/onupdate.php	(revision 232)
@@ -23,9 +23,21 @@
 	$mid = $module->getVar('mid') ;
 
-//XPress TABLE UPGRADE
+
+
+//XPressME Update
 	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles,$wp_query;
 	define('WP_INSTALLING', true);
 	$mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
 	$path = $mydirpath . '/';
+	
+/* 
+ * xpress_put_siteurl($mydirname,''); 
+ * Call to a member function register() The on a non-object error is evaded.
+ * It happens by register_widget() that exists in wp-includes/widgets.php. 
+ * The blog is not installed. It is cheating to function wp_maybe_load_widgets(). 
+ * As a result, default-widgets.php is not loaded.
+ */
+	xpress_put_siteurl($mydirname,''); 				// Site_url is temporarily deleted. 
+	
 	if (file_exists($path . 'wp-load.php')) {
 		require_once $path . 'wp-load.php';
@@ -33,29 +45,6 @@
 		require_once $path . 'wp-config.php';
 	}
-	require_once($mydirpath . '/wp-admin/upgrade-functions.php');
-
-	if ( get_db_version($mydirname) != $wp_db_version ){
-		if( function_exists( 'wp_upgrade' ) )	{	
-			wp_upgrade();			
-		} else {
-			wp_cache_flush();
-			make_db_current_silent();
-			upgrade_all();
-			wp_cache_flush();
-		}				
-			$msgs[] = 'Upgrade Wordpress Database Ver.' . get_db_version($mydirname) .' to' . $wp_db_version;
-	}
-
-	/* activate the tag plugin */
-	$plugin_current = "xpressme/xpressme.php";
-	update_option('active_plugins', array($plugin_current));
-	include_once(dirname(__FILE__) . '/../wp-content/plugins/'.$plugin_current);
-	do_action('activate_'.$plugin_current);
-	$msgs[] = 'The xpressme plug-in was activated.';
-	
-	
-//	update_option("blog_charset", wp_blog_charset());
-	$xpress_version = $module->modinfo['version'];
-	
+
+// XPressME orignal table update
 	if (! enhanced_table_check($mydirname,'views')){
 		$xp_prefix = $mydirname;
@@ -142,11 +131,57 @@
 	}
 
-	// make templates
+// make templates
 	include_once XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/include/xpress_templates_make.php' ;
 	$t_mess = xpress_templates_make($mid,$mydirname);
 	
 	$msgs = array_merge($msgs,$t_mess);
+
+// upgrade WordPress
+	
+	$site_url= XOOPS_URL."/modules/".$mydirname;		
+	update_option("home", $site_url);				// Site_url is set again. 
+	update_option("siteurl", $site_url);
+
+	require_once($mydirpath . '/wp-admin/upgrade-functions.php');
+
+	if ( get_db_version($mydirname) != $wp_db_version ){
+		if( function_exists( 'wp_upgrade' ) )	{	
+			wp_upgrade();			
+		} else {
+			wp_cache_flush();
+			make_db_current_silent();
+			upgrade_all();
+			wp_cache_flush();
+		}				
+			$msgs[] = 'Upgrade Wordpress Database Ver.' . get_db_version($mydirname) .' to' . $wp_db_version;
+	}
+
+	/* activate the xpressme plugin */
+	require_once dirname( __FILE__ ).'/xpress_active_plugin.php';
+	if (xpress_pulugin_activation('xpressme/xpressme.php')){
+		$msgs[] = 'The xpressme plug-in was activated.';
+	}
+	
+//	update_option("blog_charset", wp_blog_charset());
+	$xpress_version = $module->modinfo['version'];
+	
+
 	
 	return true ;
+}
+endif;
+
+if( ! function_exists( 'xpress_put_siteurl' ) ) :
+function xpress_put_siteurl($mydirname,$url){
+		global $xoopsModule;
+		$wp_prefix = $mydirname;
+		if ($wp_prefix == 'wordpress'){
+			$wp_prefix = 'wp';
+		}
+		$xoopsDB =& Database::getInstance();
+		$db_xpress_option = $xoopsDB->prefix($wp_prefix . '_options');
+
+		$sql = "UPDATE $db_xpress_option SET option_value = '$url' WHERE option_name = 'siteurl'";
+		$res = $xoopsDB->queryF($sql, 0, 0);
 }
 endif;
Index: trunk/xpressme_integration_kit/include/request_url.php
===================================================================
--- trunk/include/request_url.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/request_url.php	(revision 232)
@@ -22,9 +22,14 @@
 	// index.php check
 	if (strstr($php_query_string,'preview') === false) {
-		if (strstr($php_query_string,'feed') === false) {		
+		if (strstr($php_query_string,'feed') === false) {
+			// Because the judgment is difficult, the feed to which the permalink is set is confirmed here by the after processing. 
 			return true;
 		}
 	}
 	return false;
+}
+
+function is_xpress_comments_post_call(){
+	return check_page_call('wp-comments-post.php');
 }
 
Index: trunk/xpressme_integration_kit/include/set_cash_cookie_path.php
===================================================================
--- trunk/include/set_cash_cookie_path.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/set_cash_cookie_path.php	(revision 232)
@@ -66,3 +66,11 @@
 	define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH);
 
+/**
+ * It is possible to define this in wp-config.php
+ * @since 2.0.0
+ */
+if ( !defined('COOKIE_DOMAIN') )
+	define('COOKIE_DOMAIN', false);
+
+
 ?>
Index: trunk/xpressme_integration_kit/include/xpress_active_plugin.php
===================================================================
--- trunk/xpressme_integration_kit/include/xpress_active_plugin.php	(revision 232)
+++ trunk/xpressme_integration_kit/include/xpress_active_plugin.php	(revision 232)
@@ -0,0 +1,29 @@
+<?php
+if ( !function_exists('xpress_pulugin_activation') ){
+	function xpress_pulugin_activation($activation_plugin = '')
+	{
+		global $wpdb;
+		if (empty($activation_plugin)) return false;
+			
+		$plugins = get_option('active_plugins');
+		$is_active = false;
+		if (!empty($plugins)){
+			foreach($plugins as $plugin){
+				if ($plugin == $activation_plugin) {
+					$is_active = true;
+					break;
+				}
+			}
+		} else {
+			$plugins = array();
+		}
+		if (!$is_active){
+			array_push($plugins, $activation_plugin);
+			update_option('active_plugins', $plugins);
+			include_once(dirname(dirname(__FILE__) ) . '/wp-content/plugins/'.$activation_plugin);
+			do_action('activate_'.$activation_plugin);
+			return true;
+		}
+		return false;
+	}
+}
Index: trunk/xpressme_integration_kit/include/xpress_block_render.php
===================================================================
--- trunk/include/xpress_block_render.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/xpress_block_render.php	(revision 232)
@@ -181,5 +181,5 @@
 				$block = $xml['block'];
 			} else {
-				$block['err_message'] = sprintf(_MB_XPRESS_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
+				$block['err_message'] = sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
 			}
 		}
Index: trunk/xpressme_integration_kit/include/xpress_mu_function.php
===================================================================
--- trunk/xpressme_integration_kit/include/xpress_mu_function.php	(revision 232)
+++ trunk/xpressme_integration_kit/include/xpress_mu_function.php	(revision 232)
@@ -0,0 +1,281 @@
+<?php
+if( ! function_exists( 'xpress_mu_install_defaults' ) ) :
+function xpress_mu_install_defaults($domain,$base,$site_name,$admin_user_id,$admin_user_name,$admin_email){
+		global $xoops_config,$wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles, $wp_query;
+		
+		// set up MU site tables
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'site_name', '" . $site_name . "')" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'admin_email', '".$admin_email."')" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'admin_user_id', '". $admin_user_id ."')" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'registration', 'none')" );
+		$wpdb->query( "INSERT INTO ".$wpdb->site." ( id, domain, path ) VALUES ( NULL, '$domain', '$base' )" );
+		$wpdb->query( "INSERT INTO " . $wpdb->sitecategories . " ( cat_ID, cat_name, category_nicename, last_updated ) VALUES (1, 'Uncategorized', 'uncategorized', NOW())" );
+		$wpdb->query( "INSERT INTO " . $wpdb->sitecategories . " ( cat_ID, cat_name, category_nicename, last_updated ) VALUES (2, 'Blogroll', 'blogroll', NOW())" );
+		$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' )" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'blog_upload_space', '10' )" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'fileupload_maxk', '1500' )" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'site_admins', '" . serialize( array( $admin_user_name ) ) . "' )" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'allowedthemes', '" . serialize( array( 'classic' => 1, 'default' => 1, 'xpress_default' => 1 ) ) . "' )" );
+		$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" ) ) . "' )" );
+		$wpdb->query( "INSERT INTO ".$wpdb->sitemeta." (meta_id, site_id, meta_key, meta_value) VALUES (NULL, 1, 'welcome_email', 'Dear User,
+
+Your new $site_name blog has been successfully set up at:
+BLOG_URL
+
+You can log in to the administrator account with the following information:
+Username: USERNAME
+Password: PASSWORD
+Login Here: BLOG_URLwp-login.php
+
+We hope you enjoy your new blog.
+Thanks!
+
+--The Team @ SITE_NAME')" );
+
+		$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!' )" );
+		$weblog_title = $site_name;
+
+
+		$current_site->domain = $domain;
+		$current_site->path = $base;
+		$current_site->site_name = $site_name;
+
+		update_blog_option( 1, 'template', 'xpress_mu_home');
+		update_blog_option( 1, 'stylesheet', 'xpress_mu_home');
+		
+		mu_create_blog( $domain, $base, $weblog_title, $user_id, array( 'blog_public' => 1, 'public' => 1 ) );
+}
+endif;
+
+if( ! function_exists( 'do_htaccess' ) ) :
+function do_htaccess( $oldfilename, $newfilename, $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( "BASE", $base, $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 "<h2>Warning!</h2>";
+		print "<p><strong>There was a problem creating the .htaccess file.</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.php?id=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>BASE by '$base'</li>
+			<li>HOST by '$url'</li>
+			</ul>
+			</li>
+			<li> Rename htaccess.dist to .htaccess and upload it back to the same directory.</li>
+			</ul>";
+		die( "Installation Aborted!" );
+	}
+}
+endif;
+
+// this function is used to change wpmu_create_blog()
+// wpmu_create_blog() of wp_includes/wpmu_functions.php has the bug 
+// when install_blog_defaults is called.
+if( ! function_exists( 'mu_create_blog' ) ) :
+function mu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
+	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles, $wp_query;
+	global $xoops_config;
+	global $base,$current_site, $dirs, $wpmu_version;
+
+	if(!is_object($wp_rewrite)){
+		$wp_rewrite   =& new WP_Rewrite();
+	}
+	
+	$domain = preg_replace( "/\s+/", '', sanitize_user( $domain, true ) );
+	if( constant( 'VHOST' ) == 'yes' )
+		$domain = str_replace( '@', '', $domain );
+	$title = strip_tags( $title );
+	$user_id = (int) $user_id;
+
+	if( empty($path) )
+		$path = '/';
+
+	// Check if the domain has been used already. We should return an error message.
+	if ( domain_exists($domain, $path, $site_id) )
+		return new WP_Error('blog_taken', __('Blog already exists.'));
+
+	if ( !defined("WP_INSTALLING") )
+		define( "WP_INSTALLING", true );
+
+	if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
+		return new WP_Error('insert_blog', __('Could not create blog.'));
+
+	switch_to_blog($blog_id);
+
+	install_blog($blog_id, $title);
+
+	install_mu_blog_defaults($blog_id, $user_id);
+
+	add_user_to_blog($blog_id, $user_id, 'administrator');
+
+	if ( is_array($meta) ) foreach ($meta as $key => $value) {
+		if( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' ) {
+			update_blog_status( $blog_id, $key, $value );
+		} else {
+			update_option( $key, $value );
+		}
+	}
+
+	add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
+
+	update_option( 'blog_public', $meta['public'] );
+
+	if(get_usermeta( $user_id, 'primary_blog' ) == 1 )
+		update_usermeta( $user_id, 'primary_blog', $blog_id );
+
+
+	restore_current_blog();
+
+	do_action( 'wpmu_new_blog', $blog_id, $user_id );
+
+	return $blog_id;
+}
+endif;
+
+// this function is used to change install_blog_defaults()
+// When install_blog_defaults is called, $wp_rewrite is not object. 
+if( ! function_exists( 'install_mu_blog_defaults' ) ) :
+function install_mu_blog_defaults($blog_id, $user_id) {
+	global $wpdb, $wp_rewrite, $current_site, $table_prefix;
+	if(!is_object($wp_rewrite)){
+		$wp_rewrite   =& new WP_Rewrite();
+	}
+
+	$wpdb->suppress_errors();
+
+	// Cast for security
+	$user_id = (int) $user_id;
+	$blog_id = (int) $blog_id;
+
+	// Default links
+	$wpdb->insert( $wpdb->links,  array('link_url' => 'http://wordpress.com/', 'link_name' => 'WordPress.com', 'link_category' => '1356', 'link_owner' => $user_id, 'link_rss' => 'http://wordpress.com/feed/') );
+	$wpdb->insert( $wpdb->links,  array('link_url' => 'http://wordpress.org/', 'link_name' => 'WordPress.org', 'link_category' => '1356', 'link_owner' => $user_id, 'link_rss' => 'http://wordpress.org/development/feed/') );
+	$wpdb->insert( $wpdb->term_relationships, array('object_id' => 1, 'term_taxonomy_id' => 2));
+	$wpdb->insert( $wpdb->term_relationships, array('object_id' => 2, 'term_taxonomy_id' => 2));
+
+	// First post
+	$now = date('Y-m-d H:i:s');
+	$now_gmt = gmdate('Y-m-d H:i:s');
+	$first_post = get_site_option( 'first_post' );
+	if( $first_post == false ) {
+		$first_post = stripslashes( __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' ) );
+	}
+	$first_post = str_replace( "SITE_URL", clean_url("http://" . $current_site->domain . $current_site->path), $first_post );
+	$first_post = str_replace( "SITE_NAME", $current_site->site_name, $first_post );
+	$wpdb->insert( $wpdb->posts, array(
+		'post_author' => $user_id, 
+		'post_date' => $now, 
+		'post_date_gmt' => $now_gmt,
+		'post_content' => stripslashes( $first_post ), 
+		'post_excerpt' => '', 
+		'post_title' => __('Hello world!'),
+		'post_category' => 0, 
+		'post_name' => __('hello-world'),
+		'post_modified' => $now,
+		'post_modified_gmt' => $now_gmt,
+		'comment_count' => 1
+	) );	
+	$wpdb->insert( $wpdb->term_relationships, array('object_id' => 1, 'term_taxonomy_id' => 1));
+	update_option( "post_count", 1 );
+
+	// First page
+	$wpdb->insert( $wpdb->posts, array(
+		'post_author' => $user_id, 
+		'post_date' => $now, 
+		'post_date_gmt' => $now_gmt,
+		'post_content' => __('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'), 
+		'post_excerpt' => '', 
+		'post_title' => __('About'),
+		'post_category' => 0, 
+		'post_name' => __('about'),
+		'post_modified' => $now,
+		'post_modified_gmt' => $now_gmt,
+		'post_status' => 'publish',
+		'post_type' => 'page',
+		'to_ping' => '',
+		'pinged' => '',
+		'post_content_filtered' => ''
+	) );
+	
+	// Flush rules to pick up the new page.
+	$wp_rewrite->init();
+	$wp_rewrite->flush_rules();
+
+	// Default comment
+	$wpdb->insert( $wpdb->comments, array(
+		'comment_post_ID' => '1', 
+		'comment_author' => __('Mr WordPress'), 
+		'comment_author_email' => '',
+		'comment_author_url' => 'http://' . $current_site->domain . $current_site->path, 
+		'comment_author_IP' => '127.0.0.1', 
+		'comment_date' => $now,
+		'comment_date_gmt' => $now_gmt, 
+		'comment_content' => __("Hi, this is a comment.<br />To delete a comment, just log in, and view the posts' comments, there you will have the option to edit or delete them.") 
+	) );
+	
+	$user = new WP_User($user_id);
+	$wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
+
+	// Remove all perms except for the login user.
+	$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
+	$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
+	
+	// Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
+	if ( !is_site_admin( $user->user_login ) && $user_id != 1 )
+		$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $wpdb->base_prefix.'1_capabilities') );
+
+	$wpdb->suppress_errors( false );
+}
+endif;
+
+?>
Index: trunk/xpressme_integration_kit/include/xpress_render.php
===================================================================
--- trunk/include/xpress_render.php	(revision 172)
+++ trunk/xpressme_integration_kit/include/xpress_render.php	(revision 232)
@@ -4,36 +4,56 @@
 function get_mod_header($contents)
 {
+	global $xpress_config;
 	$pattern = "<head[^>]*?>(.*)<\/head>";
 	preg_match("/".$pattern."/s",  $contents, $head_matches);
 	$head_str = $head_matches[1];
 	
-	$pattern = "<style[^>]*?>(.*)<\/style>";
-	preg_match("/".$pattern."/s",  $head_str, $style_matches);
-	if (empty($style_matches[0]))
-		$style = '';
-	else
-		$style = $style_matches[0];
- 
-	$pattern = "<link(.*)>";
-	preg_match_all("/".$pattern."/",  $head_str, $link_match,PREG_PATTERN_ORDER);
-	$links = $link_match[0];
-	$link_str ='';
-	foreach ( $links as $link){
-		ob_start();
-			echo $link . "\n";
-			
-			$link_str .= ob_get_contents();
-		ob_end_clean();
-	}
-	
-	$pattern = "<script[^>]*?>(.*)<\/script>";
-	preg_match_all("/".$pattern."/s",  $head_str, $script_match,PREG_PATTERN_ORDER);
-	$scripts = $script_match[0];
-	$script_str ='';
-	foreach ( $scripts as $script){		
-		if (($GLOBALS["xoopsModuleConfig"]['use_d3forum'] != 1) || (strpos($script,'function wpopen') ===false))
-			$script_str .= $script;
-	}
-	return $link_str."\n".$style . "\n" . $script_str ."\n";
+	$pattern = '<head[^>]*?>';
+	$head_str = preg_replace("/".$pattern."/s" , '' , $head_str);
+	$pattern = '<\/head>';
+	$head_str = preg_replace("/".$pattern."/s" , '' , $head_str);
+	$pattern = '<\s*html\s+xmlns[^>]*?>';
+	$head_str = preg_replace("/".$pattern."/s" , '' , $head_str);
+	$pattern = '<\s*head\s+profile[^>]*?>';
+	$head_str = preg_replace("/".$pattern."/s" , '' , $head_str);
+	$pattern = '<\s*meta\s+http-equiv[^>]*?>';
+	$head_str = preg_replace("/".$pattern."/s" , '' , $head_str);
+	$pattern = '<title[^>]*?>(.*)<\s*\/\s*title\s*>';
+	$head_str = preg_replace("/".$pattern."/s" , '' , $head_str);
+
+	$head_str = meta_name_cut('robots',$head_str);
+	$head_str = meta_name_cut('keywords',$head_str);
+	$head_str = meta_name_cut('description',$head_str);
+	$head_str = meta_name_cut('rating',$head_str);
+	$head_str = meta_name_cut('author',$head_str);
+	$head_str = meta_name_cut('copyright',$head_str);
+	$head_str = meta_name_cut('generator',$head_str);
+
+	$head_str = preg_replace("/^(\s)*(\r|\n|\r\n)/m", "", $head_str);	
+	return $head_str;
+}
+
+function meta_name_cut($name = '', $head_str)
+{
+	$pattern = '<\s*meta\s+name\s*=\s*["\']' . $name . '["\'][^>]*?>';
+	$head_str = preg_replace("/".$pattern."/i" , '' , $head_str);
+	return $head_str;
+}
+
+// for title reprace plugin (all in one seo pack)
+function get_xpress_title($contents)
+{
+	$pattern = '<title[^>]*?>(.*)<\s*\/\s*title\s*>';
+	preg_match("/".$pattern."/i",  $contents, $head_matches);
+	$title_str = $head_matches[1];
+	return $title_str;
+}
+
+function get_xpress_meta_name($name = '',$contents)
+{
+	$pattern = '<\s*meta\s+name\s*=\s*["\']' . $name . '["\']\s*content\s*=\s*[\'"](.*)[\'"]\s*\/\s*>';
+	preg_match("/".$pattern."/i",  $contents, $head_matches);
+	$meta = $head_matches[1];
+	return $meta;
 }
 
@@ -96,12 +116,92 @@
 }
 
+//PHP_SELF and GET are remake for the XOOPS event notification.
+function xpress_remake_global_for_permlink(){
+	global $wp_db,$wp_query;
+	$php_self = $_SERVER['PHP_SELF'];
+	$get = $_GET;
+
+	if (preg_match('/\/$/',$php_self) && !preg_match('/index.php/',$php_self)) {
+		$php_self = $php_self . 'index.php';
+		$_SERVER['PHP_SELF'] = $php_self;
+	}
+	if (empty($_GET)){
+		$query_vars = $wp_query->query_vars;
+		$post = $wp_query->post;
+		if ($wp_query->is_single) {
+			$_GET = array('p'=>$post->ID);
+		} else if($wp_query->is_category){
+			$_GET = array('cat'=>$query_vars['cat']);
+		} else if($wp_query->is_author){
+			$_GET = array('author'=>$query_vars['author']);
+		}
+	}
+}
+
 //rendering for the module header and the body
 function xpress_render($contents){
-	global $xoops_config , $xoopsTpl;
+	global $xoops_config , $xoopsTpl,$xpress_config;
+	xpress_remake_global_for_permlink();
 	$mydirname = basename(dirname(dirname(__FILE__)));
 	include $xoops_config->xoops_root_path ."/header.php";
-	$page_title = $GLOBALS["xoopsModule"]->getVar("name")." ".wp_title('&raquo;', false);
 	$xoopsTpl->assign('xoops_module_header', get_xpress_module_header($contents));
+	$page_title = $GLOBALS["xoopsModule"]->getVar("name"). ' &raquo;'. get_xpress_title($contents);
 	$xoopsTpl->assign('xoops_pagetitle', $page_title);
+	
+	$xoops_keywords = $xoopsTpl->get_template_vars('xoops_meta_keywords');
+	$wp_keyword = get_xpress_meta_name('keywords',$contents);
+	switch ($xpress_config->meta_keyword_type){
+		case 'xoops':
+			break;
+		case 'wordpress':
+			if (!empty($wp_keyword))
+				$xoopsTpl->assign('xoops_meta_keywords', $wp_keyword);
+			break;
+		case 'wordpress_xoops':
+			if (!empty($wp_keyword)){
+				if (!empty($xoops_keywords)){
+					$keywords = $wp_keyword . ', ' . $xoops_keywords;
+				} else {
+					$keywords = $wp_keyword;
+				}
+				$xoopsTpl->assign('xoops_meta_keywords', $keywords);
+			} 
+			break;
+		default :
+	}
+
+	$xoops_description = $xoopsTpl->get_template_vars('xoops_meta_description');
+	$wp_description = get_xpress_meta_name('description',$contents);
+	switch ($xpress_config->meta_description_type){
+		case 'xoops':
+			break;
+		case 'wordpress':
+			if (!empty($wp_description))
+				$xoopsTpl->assign('xoops_meta_description', $wp_description);
+			break;
+		case 'wordpress_xoops':
+			if (!empty($wp_description)){
+				if (!empty($xoops_description)){
+					$description = $wp_description . ' ' . $xoops_description;
+				} else {
+					$description = $wp_description;
+				}
+				$xoopsTpl->assign('xoops_meta_description', $description);
+			} 
+			break;
+		default :
+	}
+
+	$wp_robots = get_xpress_meta_name('robots',$contents);
+	switch ($xpress_config->meta_robot_type){
+		case 'xoops':
+			break;
+		case 'wordpress':
+			if (!empty($wp_robots))
+				$xoopsTpl->assign('xoops_meta_robots', $wp_robots);
+			break;
+		default :
+	}
+	
 	$xpress_data['body_contents'] = get_body($contents);
 	// used $GLOBALS. becose xpress_left_arrow_post_link() and xpress_right_arrow_post_link() is other loop in this position
