Index: branches/XPressMU/xpressme_integration_kit/include/add_xpress_process.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/add_xpress_process.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/add_xpress_process.php	(revision 488)
@@ -7,13 +7,46 @@
 }
 
+function safe_site_url(){
+	global $xoops_config;
+	
+	if (is_xpress_index_page_call()){
+		$siteurl = get_option('siteurl');
+		$home = get_option('home');
+		$module_url = $xoops_config->module_url;
+		$module_name = $xoops_config->module_name;
+		$schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
+		$guess_url = preg_replace('|/' . $module_name . '/.*|i', '/' . $module_name, $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
+
+		if(strcmp($siteurl,$module_url) !== 0) {
+			if (!@fclose(@fopen($siteurl . '/xoops_version.php', "r"))){
+				update_option('siteurl' , $module_url);
+			}
+		}
+
+		if(strcmp($guess_ur,$home) !== 0) {
+			if (@fclose(@fopen($guess_url . '/xoops_version.php', "r"))){
+				update_option('home' , $guess_url);
+			}
+		}
+	}
+}
+
 require_once( dirname( __FILE__ ).'/request_url.php');
 if (is_xpress_index_page_call()){
+	//$_SERVER['REQUEST_METHOD'] = 'POST' is
 	//When notifying by a private message, Notification_reserve_send();
 	//it is evaded that the data base becomes read-only as a result of the check on the referrer and the method. 
+	$request_method =  (isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : '';
 	$_SERVER['REQUEST_METHOD'] = 'POST';
+	require_once $xoops_config->xoops_mainfile_path; //It is necessary to execute it for the user attestation before wp-settings.php. 
+	$_SERVER['REQUEST_METHOD'] = $request_method;
+	
+	unset($offset);		//This Trap is provides the case where $offset is defined on the XOOPS side.
+	require_once(ABSPATH.'wp-settings.php');
+	
+	//When it is not possible to connect it correctly at site home URL on the WordPress side, 
+	//URL is corrected based on accessed URL. 
+	safe_site_url();
 
-	require_once $xoops_config->xoops_mainfile_path; //It is necessary to execute it for the user attestation before wp-settings.php. 
-	unset($offset);
-	require_once(ABSPATH.'wp-settings.php');
 	if (!is_object($xoopsUser)){	// before login auth cookie clear
 		wp_logout();
Index: branches/XPressMU/xpressme_integration_kit/include/general_functions.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/general_functions.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/general_functions.php	(revision 488)
@@ -126,4 +126,6 @@
 		$table_list = array();
 		$ret = array();
+		$pattern = $wp_prefix . $table_name . '|' . $wp_prefix . '[0-9]*_' . $table_name;
+
 		if (!empty($wp_prefix) && !empty($table_name)){
 			$sql = "SHOW TABLES LIKE '" . $wp_prefix  . '%' . $table_name . "'";
@@ -132,5 +134,7 @@
 				if($result = $xoopsDB->queryF($sql)){
 					while($row = $xoopsDB->fetchRow($result)){
-						$table_list[] = $row[0];
+						if(preg_match('/' . $pattern . '/' , $row[0])){
+							$table_list[] = $row[0];
+						}
 					}
 				}
@@ -138,5 +142,7 @@
 				$rows = $xoops_db->get_results($sql, ARRAY_N);
 				foreach ($rows as $row){
-					$table_list[] = $row[0];
+					if(preg_match('/' . $pattern . '/' , $row[0])){
+						$table_list[] = $row[0];
+					}
 				}
 			}			
Index: branches/XPressMU/xpressme_integration_kit/include/oninstall.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/oninstall.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/oninstall.php	(revision 488)
@@ -45,5 +45,5 @@
 
 //xpress
-	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles, $wp_query;
+	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles, $wp_query,$wp_embed;
 	global $xoops_config;
 		
Index: branches/XPressMU/xpressme_integration_kit/include/onupdate.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/onupdate.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/onupdate.php	(revision 488)
@@ -42,6 +42,13 @@
 	$site_url= XOOPS_URL."/modules/".$mydirname;
 	xpress_put_siteurl($mydirname,$site_url);
-	xpress_put_home($mydirname,$site_url);
-
+	$home = get_xpress_option($mydirname,'home');
+	$home_check = 'home option is right';
+	if (strcmp($site_url,$home) !== 0 ){
+		if (!@fclose(@fopen($home . '/xoops_version.php', "r"))){
+			xpress_put_home($mydirname,$site_url);
+			$home_check = 'Change home option $home to $site_url';
+		}
+	}
+	$msgs[] = $home_check;
 // XPressME orignal table update
 	$t_mess = xpress_table_make($module , $mydirname);
@@ -50,6 +57,10 @@
 // make templates
 	include_once XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/include/xpress_templates_make.php' ;
+	$mod_version = $module->getVar('version') ;
+
+	$t_mess = xpress_clean_templates_file($mydirname,$mod_version);
+	$msgs = array_merge($msgs,$t_mess);
+	
 	$t_mess = xpress_templates_make($mid,$mydirname);
-	
 	$msgs = array_merge($msgs,$t_mess);
 
@@ -97,4 +108,24 @@
 endif;
 
+if( ! function_exists( 'get_xpress_option' ) ) {
+	function get_xpress_option($mydirname,$option_name){
+		global $xoopsModule;
+		$wp_prefix = $mydirname;
+		if ($wp_prefix == 'wordpress'){
+			$wp_prefix = 'wp';
+		}
+		$xoopsDB =& Database::getInstance();
+		$option_table = $xoopsDB->prefix($wp_prefix . '_options');
+
+		$sql = "SELECT option_value FROM $option_table WHERE option_name = '" . $option_name . "'";
+		
+		$result =  $xoopsDB->query($sql, 0, 0);
+		if ($xoopsDB->getRowsNum($result)  > 0){
+			$row = $xoopsDB->fetchArray($result);
+			return $row['option_value'];
+		}
+		return 0;
+	}
+}
 
 if( ! function_exists( 'xpress_message_append_onupdate' ) ) :
@@ -190,4 +221,22 @@
 			$db->queryF( $queries ) ;
 			$msgs[] = "$views_table  ADD blog_id .";
+		}
+		
+		// The table is repaired.
+		$non_blogid_sql ="SELECT * FROM $views_table WHERE blog_id IS NULL OR blog_id < 1";
+		$non_blogid_res = $db->query($non_blogid_sql, 0, 0);
+		while($row = $db->fetchArray($non_blogid_res)){
+			$total_view = $row['post_views'];
+			$post_id = $row['post_id'];
+			$new_blogid_sql ="SELECT SUM(post_views) as post_views_sum FROM $views_table WHERE post_id = $post_id AND blog_id = 1 GROUP BY post_id";
+			$new_blogid_res = $db->query($new_blogid_sql, 0, 0);
+			if ($db->getRowsNum($new_blogid_res)  > 0){
+				$new_row = $db->fetchArray($new_blogid_res);
+				$total_view = $total_view + $new_row['post_views_sum'];
+				$del_sql = "DELETE FROM $views_table WHERE post_id = $post_id AND blog_id = 1";
+				$db->queryF( $del_sql ) ;
+			}
+			$update_sql = "UPDATE $views_table SET post_views = $total_view , blog_id = 1 WHERE post_id = $post_id AND (blog_id IS NULL OR blog_id < 1)";
+			$db->queryF( $update_sql ) ;
 		}
 	}
Index: branches/XPressMU/xpressme_integration_kit/include/set_cash_cookie_path.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/set_cash_cookie_path.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/set_cash_cookie_path.php	(revision 488)
@@ -8,5 +8,5 @@
  */
 if ( !defined('USER_COOKIE') )
-	define('USER_COOKIE', $modname . 'user_' . $hash);
+	define('USER_COOKIE', 'wordpress_' . $modname . '_user_' . $hash);
 
 /**
@@ -15,5 +15,5 @@
  */
 if ( !defined('PASS_COOKIE') )
-	define('PASS_COOKIE', $modname . 'pass_' . $hash);
+	define('PASS_COOKIE', 'wordpress_' . $modname . '_pass_' . $hash);
 
 /**
@@ -22,5 +22,5 @@
  */
 if ( !defined('AUTH_COOKIE') )
-	define('AUTH_COOKIE', $modname . '_auth_' . $hash);
+	define('AUTH_COOKIE', 'wordpress_' . $modname . '_auth_' . $hash);
 
 /**
@@ -29,5 +29,5 @@
  */
 if ( !defined('SECURE_AUTH_COOKIE') )
-	define('SECURE_AUTH_COOKIE', $modname . '_sec_' . $hash);
+	define('SECURE_AUTH_COOKIE', 'wordpress_' . $modname . '_sec_' . $hash);
 
 /**
@@ -36,5 +36,5 @@
  */
 if ( !defined('LOGGED_IN_COOKIE') )
-	define('LOGGED_IN_COOKIE', $modname . '_logged_in_' . $hash);
+	define('LOGGED_IN_COOKIE', 'wordpress_' . $modname . '_logged_in_' . $hash);
 
 /**
@@ -43,5 +43,5 @@
  */
 if ( !defined('TEST_COOKIE') )
-	define('TEST_COOKIE', $modname . '_test_cookie');
+	define('TEST_COOKIE', 'wordpress_' . $modname . '_test_cookie');
 
 /**
Index: branches/XPressMU/xpressme_integration_kit/include/xpress_block_render.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/xpress_block_render.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/xpress_block_render.php	(revision 488)
@@ -29,5 +29,7 @@
 			$ret = @xpress_XML_unserialize($xml_data);
 			if (strstr($xml_data, '<?xml version="1.0" encoding="EUC-JP" ?>') !== false){
-				$ans = mb_convert_variables('EUC-JP' , 'UTF-8', &$ret);
+				if (version_compare(PHP_VERSION, '5.0.0', '>')) {
+					$ans = mb_convert_variables('EUC-JP' , 'UTF-8', &$ret); //xpress_XML_unserialize() Return UTF-8 at PHP5
+				}
 			}
 			return $ret;
@@ -52,4 +54,18 @@
 			}
 			return $block_id;
+	}
+
+	function get_block_mid($mydirname)
+	{
+			$xoopsDB =& Database::getInstance();
+			$modules_tbl = $xoopsDB->prefix('modules');	
+
+			$sql = "SELECT mid FROM $modules_tbl WHERE dirname = '$mydirname'";
+			$result =  $xoopsDB->query($sql, 0, 0);
+			if ($xoopsDB->getRowsNum($result)  > 0){
+				$row = $xoopsDB->fetchArray($result);
+				$mid = $row['mid'];
+			}
+			return $mid;
 	}
 
@@ -160,5 +176,5 @@
 	function xpress_block_render($mydirname,$block_function_name,$options)
 	{
-		global $wpdb;
+		global $wpdb,$xoops_config;
 		$func_file = $block_function_name;
 		$call_theme_function_name = str_replace(".php", "", $block_function_name);
@@ -167,9 +183,9 @@
 		$blockID =get_block_id($mydirname,$func_file,$options);		
 
-		$this_url = '/modules/'. $mydirname;
+		$this_block_url = '/' . $mydirname . '/';
 		$call_url = $_SERVER['REQUEST_URI'];
 		
 		xpress_block_css_set($mydirname);
-		if (strstr($call_url,$this_url)){			
+		if (strstr($call_url , $this_block_url) !== false){
 			$block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
 			require_once $block_theme_file['file_path'];
@@ -185,10 +201,47 @@
 			}
 		}
-
-		$templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
+		$temp_option = explode(':' , $options[1]);
+		
+		if (isset($temp_option[1])) {
+			$templates_file = $temp_option[1];
+		} else {
+			$templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
+		}
+		
 		$tpl =& new XoopsTpl() ;
+		$tpl->template_dir = $xoops_config->module_path . '/templates';
+		if (!$tpl->template_exists($templates_file)){
+			$src_file_path = $xoops_config->module_path . '/templates/' .$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
+			$templates_file = add_xpress_tpl($mydirname,$templates_file,$src_file_path);
+		}
 		$tpl->assign( 'block' , $block ) ;
 		$ret['content'] = $tpl->fetch( $templates_file ) ;
 		return $ret ;
+	}
+	
+	function add_xpress_tpl($mydirname,$templates='',$src_file_path){
+		global $wpdb,$xoops_config , $xoops_db;
+		
+		$mid = get_block_mid($mydirname);
+
+		$temp_parm = explode(':' , $templates);
+		if (empty($temp_parm[1])) {
+			$filename=$temp_parm[0];
+			$type = 'db';
+		} else  {
+			$filename=$temp_parm[1];
+			$type = $temp_parm[0];
+		}
+		$temp_file_path = $xoops_config->module_path . '/templates/'. $filename;
+		$pattern = '^' . $mydirname . '_';
+		if (preg_match('/' . $pattern . '/' , $filename, $match)){ // file prefix check
+			if (!file_exists($temp_file_path)){		// Repetition check
+				if (file_exists($src_file_path)){	// source file check
+					$rcd = copy($src_file_path, $temp_file_path);
+				}
+			}
+			return  'file:' . $filename;
+		}
+		return $templates;
 	}
 	
@@ -243,4 +296,5 @@
 	{
 		global $xoops_db,$xoops_config;
+		
 		$mid = get_xpress_modid();
 		$sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
@@ -260,5 +314,5 @@
         if ($dh = opendir($cache_dir)) {
             while (($file = readdir($dh)) !== false) {
-                if (preg_match('/^' . preg_quote($mydirname) . '/', $file)) {
+                if (preg_match('/^' . preg_quote($mydirname) . '_/', $file)) {
                 	if(! preg_match('/' . $pattern . '/', $file)) {
                     	unlink($cache_dir.$file);
@@ -287,6 +341,5 @@
 			return $xu_row['conf_value'];
 		}
-	}
-	
+	}	
 }	
 ?>
Index: branches/XPressMU/xpressme_integration_kit/include/xpress_templates_make.php
===================================================================
--- branches/XPressMU/xpressme_integration_kit/include/xpress_templates_make.php	(revision 451)
+++ branches/XPressMU/xpressme_integration_kit/include/xpress_templates_make.php	(revision 488)
@@ -7,4 +7,6 @@
 	$tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
 	$tpl_path = XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/templates' ;
+	
+	//copy  template file from source
 	if( $handler = @opendir( $tpl_path . '/source/' ) ) {
 		while( ( $file = readdir( $handler ) ) !== false ) {
@@ -26,34 +28,19 @@
 					$msgs[] = '<span style="color:#ff0000;">ERROR: Could not copy template <b>'.htmlspecialchars($target_file_name).'</b> from ' . htmlspecialchars($file). '(check templates directory permision (777))</span><br />';
 				}					
-				$tplfile =& $tplfile_handler->create() ;
-				if ($rcd) {
-					$mtime = intval( @filemtime( $target_file_path ) ) ;
-					$tplfile->setVar( 'tpl_source' , file_get_contents( $target_file_path ) , true ) ;
-				} else {
-					$mtime = intval( @filemtime( $file_path ) ) ;
-					$tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
-				}
-
-				$tplfile->setVar( 'tpl_refid' , $mid ) ;
-				$tplfile->setVar( 'tpl_tplset' , 'default' ) ;
-				$tplfile->setVar( 'tpl_file' , $target_file_name ) ;
-				$tplfile->setVar( 'tpl_desc' , '' , true ) ;
-				$tplfile->setVar( 'tpl_module' , $mydirname ) ;
-				$tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
-				$tplfile->setVar( 'tpl_lastimported' , 0 ) ;
-				$tplfile->setVar( 'tpl_type' , 'module' ) ;
-				if( ! $tplfile_handler->insert( $tplfile ) ) {
-					$msgs[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>'.htmlspecialchars($target_file_name).'</b> to the database.</span><br />';
-				} else {
-					$tplid = $tplfile->getVar( 'tpl_id' ) ;
-					$msgs[] = 'Template <b>'.htmlspecialchars($target_file_name) .'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
-					// generate compiled file
-					include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
-					include_once XOOPS_ROOT_PATH.'/class/template.php' ;
-					if( ! xoops_template_touch( $tplid ) ) {
-						$msgs[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($target_file_name).'</b>.</span><br />';
-					} else {
-						$msgs[] = 'Template <b>'.htmlspecialchars($target_file_name).'</b> compiled.</span><br />';
-					}
+			}
+		}
+		closedir( $handler ) ;
+	}
+	
+	// template added to the database.
+	if( $handler = @opendir( $tpl_path . '/' ) ) {
+		while( ( $file = readdir( $handler ) ) !== false ) {
+			if( substr( $file , 0 , 1 ) == '.' ) continue ;
+			$file_path = $tpl_path . '/' . $file ;
+			$file_name = $file ;
+			$pattern = '^' . $mydirname . '_';
+			if (preg_match('/' . $pattern . '/' , $file_name, $match)){
+				if( is_file( $file_path ) ) {
+					$msgs[] = xpress_create_db_template($file_name,$file_path,$mydirname,$mid);
 				}
 			}
@@ -61,4 +48,5 @@
 		closedir( $handler ) ;
 	}
+	
 	include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
 	include_once XOOPS_ROOT_PATH.'/class/template.php' ;
@@ -68,4 +56,35 @@
 }
 
+function xpress_create_db_template($file_name,$file_path,$mydirname,$mid){
+	if( is_file( $file_path ) ) {
+		$tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
+		$tplfile =& $tplfile_handler->create() ;
+		$mtime = intval( @filemtime( $file_path ) ) ;
+		$tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
+		$tplfile->setVar( 'tpl_refid' , $mid ) ;
+		$tplfile->setVar( 'tpl_tplset' , 'default' ) ;
+		$tplfile->setVar( 'tpl_file' ,  $file_name ) ;
+		$tplfile->setVar( 'tpl_desc' , '' , true ) ;
+		$tplfile->setVar( 'tpl_module' , $mydirname ) ;
+		$tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
+		$tplfile->setVar( 'tpl_lastimported' , 0 ) ;
+		$tplfile->setVar( 'tpl_type' , 'module' ) ;
+		if( ! $tplfile_handler->insert( $tplfile ) ) {
+			$msg = '<span style="color:#ff0000;">ERROR: Template Could not added to the database. <b>'.htmlspecialchars($file_name).'</b> to the database.</span><br />';
+		} else {
+			$tplid = $tplfile->getVar( 'tpl_id' ) ;
+			$msg = 'Template <b>'.htmlspecialchars($file_name) .'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
+			// generate compiled file
+			include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
+			include_once XOOPS_ROOT_PATH.'/class/template.php' ;
+			if( ! xoops_template_touch( $tplid ) ) {
+				$msg = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($file_name).'</b>.</span><br />';
+			} else {
+				$msg = 'Template <b>'.htmlspecialchars($file_name).'</b> compiled.</span><br />';
+			}
+		}
+	}
+	return $msg;
+}
 
 function is_template($file_name){
@@ -96,4 +115,46 @@
 }
 
+function xpress_clean_templates_file($mydirname,$mod_version)
+{
+	$tpl_path = XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/templates/' ;
+	$msgs = array();
+	if( $handler = @opendir( $tpl_path) ) {
+		while( ( $file = readdir( $handler ) ) !== false ) {
+			if( substr( $file , 0 , 1 ) == '.' ) continue ;
+			if ( strcmp($file,'source')==0 ) continue ;
+			if ( strcmp($file,'index.html')==0 ) continue ;
+			$target_file_path = $tpl_path . $file;
+			if (is_dir($target_file_path)){
+				rmDirectory($target_file_path);
+				$msgs[] = 'Template <b>'.htmlspecialchars($file).'</b> directory deleted.</span><br />';
+			} else {
+				if ($mod_version >= 200){
+					$pattern = '^' . $mydirname . '_';
+					if (preg_match('/' . $pattern . '/' , $file, $match))  continue ;
+				}
+				$rcd = @unlink($target_file_path);
+				$msgs[] = 'Template <b>'.htmlspecialchars($file).'</b> file deleted.</span><br />';
+			}
+		}
+		closedir( $handler ) ;
+	}
+	return $msgs;
+}
+
+function rmDirectory($dir){
+	if ($handle = opendir("$dir")){
+		while (false !== ($item = readdir($handle))){
+			if ($item != "." && $item != ".."){
+				if (is_dir("$dir/$item")){
+					rmDirectory("$dir/$item");
+				}else{
+					unlink("$dir/$item");
+				}
+			}
+		}
+		closedir($handle);
+		rmdir($dir);
+	}
+}
 
 ?>
