Index: trunk/wp-content/plugins/xpressme/include/d3forum_comment_disp.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/d3forum_comment_disp.php	(revision 104)
+++ trunk/wp-content/plugins/xpressme/include/d3forum_comment_disp.php	(revision 104)
@@ -0,0 +1,46 @@
+<?php
+		global $xoops_db,$XPressME;
+		$xoops_trust_path = get_xoops_trust_path();
+		$xoops_root_path = get_xoops_root_path();
+		$inc_path = $xoops_trust_path . '/modules/d3forum/include/comment_functions.php';
+		include_once( $inc_path);
+		$xpress_dir = get_xpress_dir_path();
+		$xpress_dirname = get_xpress_dir_name();
+		
+		include_once($xpress_dir . '/class/xpressD3commentContent.class.php');
+		
+		$dir_name = $XPressME->d3forum_module_dir;
+		$forum_id = $XPressME->d3forum_forum_id;
+		$external_link_format = $XPressME->d3forum_external_link_format;
+		$db =& Database::getInstance() ;
+
+	// force UPDATE forums.forum_external_link_format "(dirname)::(classname)::(trustdirname)"
+		$xoops_db->query( "UPDATE ".get_xoops_prefix() . $dir_name."_forums SET forum_external_link_format='".$external_link_format."' WHERE forum_id= $forum_id" ) ;
+		
+		$d3comment =& new xpressD3commentContent( $dir_name , $xpress_dirname ) ;
+
+		$post_title = get_the_title();
+        if (function_exists('get_the_ID')){        // upper wordpress 2.1
+            $post_id=get_the_ID();
+        } else {        // lower wordpress 2.1
+            ob_start();
+                the_ID();
+                $post_id=ob_get_contents();
+            ob_end_clean();
+        }
+        if (empty($_GET['p'])){
+            $_GET['p']= $post_id;
+        } 	
+//		$params = array("dirname" => $dir_name, "forum_id" => $forum_id, "itemname" => "p", "id" => $post_id , "subject" => $post_title);
+		$params = array("forum_id" => $forum_id,  "id" => $post_id , "subject" => $post_title);
+		if(file_exists($xoops_trust_path .'/modules/d3forum/')) {
+			if(file_exists($xoops_root_path .'/modules/' . $dir_name . '/')) {
+				$d3comment->displayCommentsInline($params ) ;
+			} else {
+				echo ('<h3>' . $dir_name . 'is not found </h3>') ;
+			}
+		} else {
+		echo '<h3> d3forum is not install </h3>';
+		}
+
+?>
Index: trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php	(revision 104)
+++ trunk/wp-content/plugins/xpressme/include/d3forum_comment_synchro.php	(revision 104)
@@ -0,0 +1,953 @@
+<?PHP
+// $Id: xoops_version.php,v 0.4 2007/07/21 01:35:02 toemon $
+//  ------------------------------------------------------------------------ //
+//                XOOPS - PHP Content Management System                      //
+//                    Copyright (c) 2000 XOOPS.org                           //
+//                       <http://www.xoops.org/>                             //
+//  ------------------------------------------------------------------------ //
+//  This program is free software; you can redistribute it and/or modify     //
+//  it under the terms of the GNU General Public License as published by     //
+//  the Free Software Foundation; either version 2 of the License, or        //
+//  (at your option) any later version.                                      //
+//                                                                           //
+//  You may not change or alter any portion of this comment or credits       //
+//  of supporting developers from this source code or any supporting         //
+//  source code which is considered copyrighted (c) material of the          //
+//  original comment or credit authors.                                      //
+//                                                                           //
+//  This program is distributed in the hope that it will be useful,          //
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
+//  GNU General Public License for more details.                             //
+//                                                                           //
+//  You should have received a copy of the GNU General Public License        //
+//  along with this program; if not, write to the Free Software              //
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
+//  ------------------------------------------------------------------------ //
+// Author: toemon                                                      //
+// URL:http://www.toemon.com                           //
+// ------------------------------------------------------------------------- //
+function onaction_comment_post($comment_ID  = "")
+{
+	global $wpdb;
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	if (!empty($comment_type)) return ;	
+	return wp_comment_sync_to_d3forum($comment_ID,'post');
+}
+function onaction_edit_comment($comment_ID  = "")
+{
+	global $wpdb;
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	if (!empty($comment_type)) return ;	
+	return wp_comment_sync_to_d3forum($comment_ID,'edit');
+}
+function onaction_delete_comment($comment_ID  = "")
+{
+	global $wpdb;
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	if (!empty($comment_type)) return ;	
+	return wp_comment_sync_to_d3forum($comment_ID,'delete');
+}
+function onaction_delete_post($post_id)
+{
+	wp_post_delete_sync($post_id);
+}
+
+function onaction_comment_close($post_id)
+{
+	global $wpdb;
+	$status = $wpdb->get_var("SELECT comment_status FROM $wpdb->posts WHERE ID = $post_id");
+	
+	if ($status =='open') 
+		$lock = 0; 
+	else
+		$lock = 1;
+	d3forum_topic_rock($post_id,$lock);
+}
+
+function onaction_comment_apobe($commentID){
+	global $wpdb;
+	$comment_type = $wpdb->get_var("SELECT comment_type FROM $wpdb->comments WHERE comment_ID = $commentID");
+	$status = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID = $commentID");
+	if(is_null($status)) return;
+	//	$status = wp_get_comment_status($commentID);
+	switch($status){
+		case 'approved':
+		case 1:
+			if (empty($comment_type)) onaction_edit_comment($commentID);
+			break;
+		case 'unapproved':
+		case 0:
+			if (empty($comment_type)) onaction_edit_comment($commentID);
+			break;
+		default:
+			break;
+	}
+}
+
+function disp_d3forum_comments($template_dir="", $file_name="")
+{
+	if (is_xpress_mobile()) return $file_path;
+	$file_path = dirname(__FILE__) . '/d3forum_comment_disp.php';
+	return $file_path;
+}
+
+
+function d3f_module_found($forum_dir ='d3forum'){		//use admin/admin_enhanced.php
+	return file_exists( get_xoops_root_path() . '/modules/' . $forum_dir);
+}
+
+function d3f_forum_id_found($forum_id = 1 , $forum_dir ='d3forum'){  //use admin/admin_enhanced.php
+	global $xoops_db;
+
+	$d3f_forums = get_xoops_prefix() . $forum_dir . '_forums';
+
+	$sql  = "SELECT forum_id ";
+	$sql .= "FROM $d3f_forums ";
+	$sql .= "WHERE ( forum_id = $forum_id )";
+
+	$forum_id =  $xoops_db->get_var($sql);
+	if (empty($forum_id)){
+		return false;
+	}
+	return $forum_id;
+}
+
+function get_d3forum_post_ID($wp_comment_ID){
+	global $xoops_db;
+
+	$wp_d3forum_link = get_wp_prefix() . 'd3forum_link';
+	
+	$sql  =	"SELECT post_id FROM $wp_d3forum_link WHERE comment_ID = $wp_comment_ID";
+	$post_id = $xoops_db->get_var($sql);
+	return $post_id;
+}		
+
+function get_wp_comment_ID($d3forum_post_ID){
+	global $xoops_db;
+
+	$wp_d3forum_link = get_wp_prefix() . 'd3forum_link';
+	
+	$sql  =	"SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $d3forum_post_ID";
+	$comment_ID = $xoops_db->get_var($sql);
+	return $comment_ID;
+}				
+
+// Next, auto increment ID value used is acquired. 
+function get_next_auto_increment_id($table_name,$id_name){
+	global $xoops_db;
+		$sql = "SELECT MAX($id_name) as last_id FROM $table_name";
+		$get_id = $xoops_db->get_var($sql);
+		if (empty($get_id)){
+			return 1;
+		} else {
+			return  $get_id + 1;
+		}
+}
+
+function is_d3forum_setting(){
+	global $XPressME,$xoops_db;
+
+	$use_d3f = $XPressME->is_use_d3forum;
+	if (empty($use_d3f)) {
+		return false;
+	}
+	$d3f_forum_id = $XPressME->d3forum_forum_id;
+	$d3f_forum_dir  = $XPressME->d3forum_module_dir;
+	if (! d3f_module_found($d3f_forum_dir)) die( "D3Forum Directory ($d3f_forum_dir) not found" ) ;	
+	if (! d3f_forum_id_found($d3f_forum_id , $d3f_forum_dir)) die( "D3Forum ForumID($d3f_forum_id) not found" ) ;
+	
+	$xoops_db->query( "UPDATE ".get_xoops_prefix() . $d3f_forum_dir."_forums" ." SET forum_external_link_format='".addslashes($XPressME->d3forum_external_link_format)."' WHERE forum_id= $d3f_forum_id" ) ;
+	
+	return true;
+}
+
+function d3forum_topic_rock($wp_post_id,$lock = '0')
+{
+	global $XPressME,$xoops_db;
+	
+	if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
+	$d3f_forum_dir  = $XPressME->d3forum_module_dir;
+	
+	$d3f_topic = get_xoops_prefix() . $d3f_forum_dir . '_topics';
+
+		$sql  = "UPDATE $d3f_topic ";
+		$sql .= "SET topic_locked = $lock ";
+		$sql .= "WHERE topic_external_link_id = $wp_post_id" ;
+		$xoops_db->query($sql);
+}
+
+// All comments of WordPress are exported to the D3Forum comment. 
+function wp_to_d3forum($forum_id = 1, $d3f_prefix = 'd3forum' ,$wp_prefix = 'wp'){
+	global $XPressME,$xoops_db;
+
+	if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
+	
+	$d3forum_prefix = get_xoops_prefix() . $d3f_prefix . '_';
+	$xpress_prefix = get_xoops_prefix() . $wp_prefix . '_';
+	
+	$wp_comments = $xpress_prefix . 'comments';
+	$wp_d3forum_link = $xpress_prefix . 'd3forum_link';
+	$wp_posts = $xpress_prefix . 'posts';
+	$d3f_topic = $d3forum_prefix . 'topics';	// delete key forum_id
+	$d3f_forums = $d3forum_prefix . 'forums';	// delete key forum_id
+	$d3f_posts = $d3forum_prefix . 'posts';	// delete key topic_id
+	$d3f_users2topics = $d3forum_prefix . 'users2topics';	// delete key topic_id
+	$d3f_post_histories = $d3forum_prefix . 'post_histories';	// delete key post_id
+	$d3f_post_post_votes = $d3forum_prefix . 'post_votes';	// delete key post_id
+
+	//DELETE D3FORUM_TOPIC & D3FORUM_POSTS
+	$topics = $xoops_db->get_results("SELECT topic_id FROM $d3f_topic WHERE forum_id = $forum_id");
+	foreach($topics as $topic){
+		$now_topic_id = $topic->topic_id;
+		$posts = $xoops_db->get_results("SELECT post_id FROM $d3f_posts WHERE topic_id = $now_topic_id");
+		foreach($posts as $post){
+			$now_post_id = $post->post_id;
+			$xoops_db->query("DELETE FROM $d3f_post_histories WHERE post_id = $now_post_id");
+			$xoops_db->query("DELETE FROM $d3f_post_post_votes WHERE post_id = $now_post_id");
+		}
+		$xoops_db->query("DELETE FROM $d3f_posts WHERE topic_id = $now_topic_id");
+		$xoops_db->query("DELETE FROM $d3f_users2topics WHERE topic_id = $now_topic_id");
+	}
+	$sql  = "UPDATE $d3f_forums ";
+	$sql .= "SET forum_topics_count = 0,forum_posts_count = 0,forum_last_post_id = 0,forum_last_post_time = 0 ";
+	$sql .= "WHERE forum_id = $forum_id" ;
+	$xoops_db->query($sql);
+	
+	$xoops_db->query("DELETE FROM $d3f_topic WHERE forum_id = $forum_id");
+	
+	$next_id = get_next_auto_increment_id($d3f_topic,'topic_id');
+	$xoops_db->query("ALTER TABLE $d3f_topic AUTO_INCREMENT = $next_id");
+	
+	$next_id = get_next_auto_increment_id($d3f_posts,'post_id');
+	$xoops_db->query("ALTER TABLE $d3f_posts AUTO_INCREMENT = $next_id");
+	
+	$next_id = get_next_auto_increment_id($d3f_post_histories,'history_id');
+	$xoops_db->query("ALTER TABLE $d3f_post_histories AUTO_INCREMENT = $next_id");
+
+	$next_id = get_next_auto_increment_id($d3f_post_post_votes,'vote_id');
+	$xoops_db->query("ALTER TABLE $d3f_post_post_votes AUTO_INCREMENT = $next_id");
+	
+	//All the records in the wp_d3forum_link table are deleted.  
+	$xoops_db->query("DELETE FROM $wp_d3forum_link WHERE 1");
+
+	//The comment is copied from the wordpress comment.
+	$sql  = "SELECT comment_ID ";
+	$sql .= "FROM $wp_comments ";
+	$sql .=	"WHERE (comment_approved NOT LIKE 'spam') AND (comment_type = '') ";
+	$sql .= "ORDER BY comment_ID";
+
+	$comment_count = 0;
+	$comments = $xoops_db->get_results($post_sql);
+	foreach($comments as $comment){
+		$comment_ID = $comment->comment_ID;
+		wp_comment_sync_to_d3forum($comment_ID ,'insert');
+		$comment_count++;
+	}
+
+	$return_str = "...Export $comment_count Comment OK ";
+
+	return $return_str;
+}
+
+
+// All comments of D3Forum are import to the WordPress comment. 
+function d3forum_to_wp($forum_id = 1, $d3f_prefix = 'd3forum' ,$wp_prefix = 'wordpress'){
+	global $XPressME,$xoops_db;
+	if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
+	
+	$d3forum_prefix = get_xoops_prefix() . $d3f_prefix . '_';
+	$xpress_prefix = get_xoops_prefix() . $wp_prefix . '_';
+	
+	$wp_comments = $xpress_prefix . 'comments';
+	$wp_d3forum_link = $xpress_prefix . 'd3forum_link';
+	$wp_dummy = $xpress_prefix . 'dummy';
+	$wp_d3forum_link  = $xpress_prefix . 'd3forum_link';
+	
+	$d3f_topic = $d3forum_prefix . 'topics';
+	$d3f_posts = $d3forum_prefix . 'posts';
+	
+	$db_xoops_users = get_xoops_prefix() . 'users';
+
+	// The track back data is taken out of the comment table, and it returns it to the initialized comment table. 	
+	//copies it in the dummy table excluding a usual comment. 
+	$xoops_db->query("CREATE TABLE $wp_dummy SELECT * FROM $wp_comments WHERE comment_type != ''");
+	//comment_ID of the dummy table is adjusted to all 0.
+	$xoops_db->query("UPDATE $wp_dummy SET `comment_ID` = 0");
+	//All the records in the comment table are deleted.  
+	$xoops_db->query("DELETE FROM $wp_comments WHERE 1");
+	//The auto increment value of the comment table is reset in '1'.
+	$xoops_db->query("ALTER TABLE $wp_comments AUTO_INCREMENT =1");
+	//The content of dummy table is returned to the comment table. 
+	$xoops_db->query("INSERT INTO $wp_comments SELECT * FROM $wp_dummy");;
+	//The dummy table is deleted. 
+	$xoops_db->query("DROP TABLE $wp_dummy");
+	//All the records in the wp_d3forum_link table are deleted.  
+	$xoops_db->query("DELETE FROM $wp_d3forum_link WHERE 1");
+
+	//All wp post comment count clear
+	$wp_posts = $xpress_prefix . 'posts';
+	$xoops_db->query("UPDATE $wp_posts SET  comment_count = 0 WHERE 1 ");
+		
+//The comment is copied from the d3forum comment.
+	$d3f_sql  =	"SELECT $d3f_topic.forum_id, $d3f_topic.topic_external_link_id, $d3f_topic.topic_id, $d3f_posts.post_id, $d3f_posts.pid ";
+	$d3f_sql .=	"FROM $d3f_topic LEFT JOIN $d3f_posts ON $d3f_topic.topic_id = $d3f_posts.topic_id ";
+	$d3f_sql .=	"WHERE $d3f_topic.forum_id=$forum_id ";
+	$d3f_sql .= "ORDER BY $d3f_posts.post_id";
+
+	$d3f_res = $xoops_db->get_results($d3f_sql);
+	if (empty($d3f_res )) die( '...Err. OPEN D3Forum Data (' .  $d3f_sql . ')');
+	$import_count = 0;
+	foreach($d3f_res as $d3f_row){
+		$link_id = $d3f_row->topic_external_link_id;
+		$forum_id = $d3f_row->forum_id;
+		$topic_id = $d3f_row->topic_id;
+		$post_id = $d3f_row->post_id;
+					
+		if(empty($link_id)){ echo "<p><font color='#FF0000'>PASS: empty topic.topic_external_link_id in topic_id($topic_id)</font></p>" ; continue;}
+		if(empty($post_id)){ echo "<p><font color='#FF0000'>PASS: empty topic_id=$topic_id in $d3f_posts</font></p>" ; continue;}
+		
+		if ($d3f_row->pid == 0){
+			$mode = 'newtopic';
+		}else{
+			$mode = 'reply';
+		}
+		d3forum_sync_to_wp_comment( $mode , $link_id , $forum_id , $topic_id , $post_id);
+		$import_count++;
+	}
+	$return_str = "...Import $import_count Comment OK ";
+	return $return_str;
+}
+
+//When post of wordpress is deleted, the comment on relating d3forum is deleted. 
+
+function wp_post_delete_sync($post_id){
+	global $XPressME,$xoops_db;
+
+	if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
+	$d3forum_dirname = $XPressME->d3forum_module_dir;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+
+	$post_id = intval( $post_id ) ;
+
+	$d3f_topics = $d3forum_prefix . 'topics';
+	
+	$sql = "SELECT topic_id,topic_first_post_id FROM $d3f_topics WHERE topic_external_link_id = $post_id";
+	$row = $xoops_db->get_row($sql) ;
+	
+	if(empty($row)) return ;
+	$topic_id = $row->topic_id;
+	$topic_first_post_id = $row->topic_first_post_id;
+	wp_d3forum_delete_post_recursive( $d3forum_dirname , $topic_first_post_id ,true);
+//	wp_d3forum_delete_topic( $d3forum_dirname , $topic_id );
+	wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
+}
+
+//  The content is reflected in the WordPress comment when there is a change in the D3Forum comment. 
+
+function d3forum_sync_to_wp_comment( $mode , $link_id , $forum_id , $topic_id , $post_id = 0 ){
+	global $XPressME,$xoops_db;
+
+	if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
+	$d3forum_prefix = get_xoops_prefix() . $XPressME->d3forum_module_dir . '_';
+	$xpress_prefix = get_wp_prefix();
+	
+	$wp_comments = $xpress_prefix . 'comments';
+	$wp_posts = $xpress_prefix . 'posts';
+	$wp_d3forum_link = $xpress_prefix . 'd3forum_link';
+	
+	$d3f_posts = $d3forum_prefix . 'posts';
+	$d3f_topics = $d3forum_prefix . 'topics';
+	$d3f_users2topics  = $d3forum_prefix . 'users2topics';
+	$d3f_post_votes = $d3forum_prefix . 'post_votes';
+
+	$db_xoops_users = get_xoops_prefix() . 'users';
+	
+	$comment_post_ID = $link_id;
+
+	$d3f_sql  =	"SELECT $d3f_posts.guest_name, ";
+	$d3f_sql .=	"$d3f_posts.guest_email, $d3f_posts.guest_url, $d3f_posts.poster_ip, $d3f_posts.post_time, ";
+	$d3f_sql .=	"$d3f_posts.post_text, $d3f_posts.approval, $d3f_posts.uid ,$d3f_posts.pid ";
+	$d3f_sql .=	"FROM $d3f_posts ";
+	$d3f_sql .=	"WHERE $d3f_posts.post_id = $post_id";
+
+	$d3f_row = $xoops_db->get_row($d3f_sql) ;
+	if (empty($d3f_row)) die('...Err. OPEN D3Forum Data (' .  $d3f_sql . ')');
+	$uid = $d3f_row->uid;
+	if (!empty($uid)) {
+		$xu_sql  = "SELECT uid ,name ,uname ,email , url FROM $db_xoops_users WHERE uid = $uid";
+		$xu_row =  $xoops_db->get_row($xu_sql);
+		if (empty($xu_row)){
+			$user_display_name = '';
+		}else {
+			if (empty($xu_row->name)){
+				$user_display_name = $xu_row->uname;
+			} else {
+				$user_display_name = $xu_row->name;
+			}
+			$comment_author_email = "'" . $xu_row->email . "'";
+			$comment_author_url = "'" . $xu_row->url . "'";
+		}
+		$comment_author = "'" . addSlashes($user_display_name) . "'";
+	} else {						
+		$comment_author = "'" . addSlashes($d3f_row->guest_name) . "'";
+		$comment_author_email = "'" . $d3f_row->guest_email . "'";
+		$comment_author_url = "'" . $d3f_row->guest_url . "'";
+	}
+	$comment_author_IP = "'" . $d3f_row->poster_ip . "'";
+	$comment_date = "'" . date('Y-m-d H:i:s' , $d3f_row->post_time) . "'";
+	$comment_content = "'" . addSlashes($d3f_row->post_text) . "'";
+	$comment_approved = "'" . $d3f_row->approval . "'";
+	$user_ID = $d3f_row->uid;
+	$comment_date_gmt = "'" . gmdate('Y-m-d H:i:s' , $d3f_row->post_time) . "'";
+	$comment_type = '';
+	if ($d3f_row->pid > 0) {
+		$comment_parent = get_wp_comment_ID($d3f_row->pid);
+	} else {
+		$comment_parent = 0 ;
+	}
+
+
+		switch($mode){				
+			case 'reply':
+			case 'newtopic' :				
+				$wp_sql  = "INSERT INTO $wp_comments ";
+				$wp_sql .=    "(comment_post_ID , comment_author , comment_author_email , comment_author_url , comment_author_IP , ";
+				$wp_sql .=    "comment_date , comment_content , comment_approved , user_id , comment_date_gmt, comment_parent) ";
+				$wp_sql .=  "VALUES ";
+				$wp_sql .=    "($comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, ";
+				$wp_sql .=    "$comment_date, $comment_content, $comment_approved, $user_ID, $comment_date_gmt, $comment_parent)";
+
+				$wp_res = 	$xoops_db->query($wp_sql);
+				if ($wp_res === false) die( '...Err. INSERT' . $wp_comments . '(' . $wp_sql . ')');
+				$comment_ID = mysql_insert_id();
+				$wp_sql  = "UPDATE $wp_posts SET  comment_count = comment_count +1 WHERE ID = $comment_post_ID";
+				$xoops_db->query($wp_sql);
+				$wp_sql  = "INSERT INTO $wp_d3forum_link ";
+				$wp_sql .=    "(comment_ID , post_id) ";
+				$wp_sql .=  "VALUES ";
+				$wp_sql .=    "($comment_ID, $post_id)";		
+				$xoops_db->query($wp_sql);				
+				if ($comment_approved ==0)	do_CommentWaiting($commentID, $post_id);
+				break;
+			case 'edit':
+				$comment_ID = "SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $post_id";
+				$comment_ID = $xoops_db->get_var("SELECT comment_ID FROM $wp_d3forum_link WHERE post_id = $post_id");
+				if (empty($comment_ID)) die('...Err. EDIT' . $wp_comments . '(' . $wp_sql . ')');
+				$wp_sql  = "UPDATE $wp_comments SET comment_content = $comment_content , comment_date_gmt = $comment_date_gmt WHERE comment_ID = $comment_ID";
+				$wp_res = $xoops_db->query($wp_sql);
+				if (empty($wp_res)) die( '...Err. UPDATE' . $wp_comments . '(' . $wp_sql . ')');
+				break;
+			case 'delete':
+				// wordpress comments delete
+				$comment_ID = get_wp_comment_ID($post_id);
+				if ($comment_ID > 0){
+					$sql= "SELECT comment_type FROM $wp_comments WHERE comment_ID = $comment_ID";
+					$comment_type= $xoops_db->get_var("SELECT comment_type FROM $wp_comments WHERE comment_ID = $comment_ID");
+					if (!empty($comment_type)) break;
+					$xoops_db->query("DELETE FROM $wp_comments WHERE comment_ID = $comment_ID");				
+					$xoops_db->query("DELETE FROM $wp_d3forum_link WHERE post_id = $post_id");				
+					$xoops_db->query("UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID");				
+				}
+				break;
+			default :
+		}				
+		
+	return true ;
+}
+
+//  The content is reflected in the D3Forum comment when there is a change in the WordPress comment. 
+function wp_comment_sync_to_d3forum($comment_ID = 0,$sync_mode){
+	global $XPressME,$xoops_db;
+	
+	if (!is_d3forum_setting()) die('The setting of the D3Forum comment integration is wrong. ');
+
+	$d3f_forum_id = $XPressME->d3forum_forum_id;
+	$d3f_forum_dir  = $XPressME->d3forum_module_dir;
+	
+	$d3forum_prefix = get_xoops_prefix() . $d3f_forum_dir . '_';
+	$xpress_prefix = get_wp_prefix();
+	$wp_comments = $xpress_prefix . 'comments';
+	$wp_posts = $xpress_prefix . 'posts';
+	$wp_d3forum_link = $xpress_prefix . 'd3forum_link';
+	$d3f_topic = $d3forum_prefix . 'topics';
+	$d3f_posts = $d3forum_prefix . 'posts';
+
+	$sql  =	"SELECT $wp_comments.comment_ID,$wp_comments.comment_post_ID, ";
+	$sql .=		"$wp_comments.comment_author, $wp_comments.comment_author_email,  $wp_comments.comment_date, ";
+	$sql .=		"$wp_comments.comment_author_url, $wp_comments.comment_author_IP, ";
+	$sql .=		"$wp_comments.comment_content, $wp_comments.comment_karma, ";
+	$sql .=		"$wp_comments.comment_approved, $wp_comments.comment_agent, ";
+	$sql .=		"$wp_comments.comment_type, $wp_comments.comment_parent, $wp_comments.user_id, ";
+	$sql .=		"$wp_posts.post_title ,$wp_posts.comment_count ";
+	$sql .=	"FROM $wp_comments INNER JOIN  $wp_posts ON $wp_comments.comment_post_ID = $wp_posts.ID ";
+	$sql .=	"WHERE (comment_ID = $comment_ID) AND ($wp_comments.comment_approved NOT LIKE 'spam') ";
+
+	$row = $xoops_db->get_row($sql) ;
+	if(empty($row)) die( 'READ ' . $wp_comments . '_NG...' .$sql);
+	if (! empty($row->comment_type)) return;
+	
+	$forum_id = $d3f_forum_id;
+	$d3forum_dirname =$d3f_forum_dir;
+	$topic_external_link_id = $row->comment_post_ID;
+	$topic_title = 'Re.' . addSlashes($row->post_title);
+	$post_time = strtotime($row->comment_date);		// or	$row2['comment_date_gmt']
+	$modified_time = strtotime($row->comment_date);	// or	$row2['comment_date_gmt']
+	$uid = $row->user_id;
+	$poster_ip = "'" . addslashes($row->comment_author_IP ). "'";
+	$modifier_ip = "'" . addslashes($row->comment_author_IP) . "'";
+	$subject = "'" . $topic_title . "'";
+    $post_text = "'" . addSlashes($row->comment_content) . "'";
+	$guest_name = "'" . addSlashes($row->comment_author) . "'";
+    $guest_email = "'" . $row->comment_author_email . "'";
+    $guest_url = "'" . $row->comment_author_url . "'";
+    $approval = $row->comment_approved;
+    $comment_count = $row->comment_count;
+    $comment_parent = $row->comment_parent;
+    
+	if ($sync_mode == 'delete'){
+		$mode = 'delete';
+		$delete_post_id = $xoops_db->get_var("SELECT post_id FROM $wp_d3forum_link WHERE comment_ID = $comment_ID");
+		if (empty($delete_post_id)) return;
+		$topic_id = $xoops_db->get_var("SELECT topic_id FROM $d3f_topic WHERE topic_external_link_id = $topic_external_link_id AND forum_id = $forum_id");
+		if (empty($topic_id)) return;
+	}else{
+		$sql  =	"SELECT * FROM $d3f_topic WHERE topic_external_link_id = $topic_external_link_id AND forum_id = $forum_id";
+		$row = $xoops_db->get_row($sql) ;
+		if (empty($row)){
+			$mode = $mode = 'newtopic';
+		} else {
+			$topic_id = $row->topic_id;
+			$reply_pid = $row->topic_first_post_id; //reply_first_comment
+			$row = $xoops_db->get_row("SELECT * FROM $wp_d3forum_link WHERE comment_ID = $comment_ID") ;
+			if (!empty($row)){
+				$mode = $mode = 'edit';
+				$edit_post_id = $row->post_id;
+			} else {
+				$mode = $mode = 'reply';
+				$reply_pid = 0;
+				if ($comment_parent > 0) {
+					$reply_pid = get_d3forum_post_ID($comment_parent);
+				}
+				if ($reply_pid == 0) {
+					$reply_pid = $row->topic_first_post_id; //reply_first_comment
+				}
+			}
+		}
+	}
+	$modified_time = $post_time;
+	
+	// make set part of INSERT or UPDATE (refalence d3forum main/post.php)
+	$set4sql = "modified_time= $modified_time , modifier_ip= $modifier_ip " ;
+	$set4sql .= ",subject= $subject " ;
+	$set4sql .= ",post_text= $post_text " ;
+    	
+	if($uid == 0) {
+		@list( $guest_name , $trip_base ) = explode( '#' , $guest_name , 2 ) ;
+		if( ! trim( @$guest_name ) ) $guest_name = get_xoops_config('anonymous_name',$d3f_forum_dir) ;
+		if( ! empty( $trip_base ) && function_exists( 'crypt' ) ) {
+			$salt = strtr( preg_replace( '/[^\.-z]/' , '.' , substr( $trip_base . 'H.' , 1 , 2 ) ) , ':;<=>?@[\]^_`' , 'ABCDEFGabcdef' ) ;
+			$guest_trip = substr( crypt( $trip_base , $salt ) , -10 ) ;
+		} else {
+			$guest_trip = '' ;
+		}
+		$guest_url = preg_match( '#^https?\://#' , $guest_url ) ? $guest_url : '' ;
+		foreach( array('guest_name','guest_email','guest_url','guest_trip') as $key ) {
+			$set4sql .= ",$key='".addslashes($$key)."'" ;
+		}
+		if( ! empty( $guest_pass ) ) {
+			$set4sql .= ",guest_pass_md5='".md5($guest_pass.'d3forum')."'" ;
+		}
+	}
+
+	$hide_uid = get_xoops_config('allow_hideuid',$d3f_forum_dir);
+	
+	switch($mode){
+		case 'edit':
+			$sql = "SELECT pid  FROM $d3f_posts WHERE post_id= $edit_post_id ";
+			$edit_post_pid = $xoops_db->get_var($sql);
+			if(empty($edit_post_pid)) die( 'READ ' . $d3forum_comments . '_NG...' .$sql);
+			// approval
+			if( $approval ) {
+				$set4sql .= ',approval=1' ;
+				$topic_invisible = 0 ;
+				$need_notify = true ;
+			} else {
+				$set4sql .= ',approval=0' ;
+				$topic_invisible = 0 ;
+				$need_admin_notify = true ;
+			}
+			// hide_uid
+			if( $hide_uid ) {
+				$set4sql .= ",uid=0,uid_hidden='$uid'" ;
+			} else {
+				$set4sql .= ",uid='$uid',uid_hidden=0" ;
+			}
+
+			// update post specified post_id
+			wp_d3forum_transact_make_post_history( $d3forum_dirname , $edit_post_id ) ;
+			$sql = "UPDATE ".$d3f_posts." SET $set4sql WHERE post_id=$edit_post_id";
+			$xoops_db->query($sql);
+			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN UPDATE post" ) ;
+			if ($edit_post_pid == 0){
+				$sql = "UPDATE ".$d3f_topic." SET topic_invisible=$topic_invisible WHERE topic_id=$topic_id";
+				if( ! $xoops_db->query($sql) ) die( "DB ERROR IN UPDATE post" ) ;
+			}
+			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , ! $edit_post_pid ) ;
+			break;
+			
+		case 'reply' :
+			// approval
+			if( $approval ) {
+				$set4sql .= ',approval=1' ;
+				$need_notify = true ;
+			} else {
+				$set4sql .= ',approval=0' ;
+				$need_admin_notify = true ;
+			}
+
+			// hide_uid
+			if( $hide_uid ) {
+				$set4sql .= ",uid=0,uid_hidden='$uid'" ;
+			} else {
+				$set4sql .= ",uid='$uid',uid_hidden=0" ;
+			}
+
+			// create post under specified post_id
+			$sql = "INSERT INTO ".$d3f_posts." SET $set4sql,pid=$reply_pid,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
+			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN INSERT post $sql" ) ;
+			$post_id = mysql_insert_id();
+			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
+			
+			$wp_sql  = "INSERT INTO $wp_d3forum_link ";
+			$wp_sql .=    "(comment_ID , post_id) ";
+			$wp_sql .=  "VALUES ";
+			$wp_sql .=    "($comment_ID, $post_id)";
+			$xoops_db->query($sql);
+			
+			break;
+
+		case 'newtopic':
+			// approval
+			if( $approval ) {
+				$set4sql .= ',approval=1' ;
+				$topic_invisible = 0 ;
+				$need_notify = true ;
+			} else {
+				$set4sql .= ',approval=0' ;
+				$topic_invisible = 0 ;
+				$need_admin_notify = true ;
+			}
+
+			// hide_uid
+			if( $hide_uid ) {
+				$set4sql .= ",uid=0,uid_hidden='$uid'" ;
+			} else {
+				$set4sql .= ",uid='$uid',uid_hidden=0" ;
+			}
+
+			// create topic and get a new topic_id
+			$sql = "INSERT INTO ".$d3f_topic." SET forum_id=$forum_id,topic_invisible=$topic_invisible,topic_external_link_id='".addslashes($topic_external_link_id)."'";
+			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN INSERT topic $sql" ) ;
+			$topic_id = mysql_insert_id();
+			// create post in the topic
+			$sql = "INSERT INTO ".$d3f_posts." SET $set4sql,topic_id=$topic_id,post_time=$post_time,poster_ip=$poster_ip";
+			if( ! $xoops_db->query($sql) ) die( "DB ERROR IN INSERT post $sql" ) ;
+			$post_id = mysql_insert_id();
+			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , true , true ) ;
+			
+			$wp_sql  = "INSERT INTO $wp_d3forum_link ";
+			$wp_sql .=    "(comment_ID , post_id , wp_post_ID) ";
+			$wp_sql .=  "VALUES ";
+			$wp_sql .=    "($comment_ID, $post_id, $topic_external_link_id)";		
+			$xoops_db->query($sql);
+
+			break;
+		case 'delete':
+			wp_d3forum_delete_post_recursive( $d3forum_dirname , $delete_post_id );
+			wp_d3forum_sync_topic( $d3forum_dirname , $topic_id ) ;
+			break;
+		default:				
+	}
+	
+	// increment post
+	if( is_object( @$xoopsUser ) && $mode != 'edit' ) {
+		$xoopsUser->incrementPost() ;
+	}
+	// set u2t_marked
+	$allow_mark = get_xoops_config('allow_mark',$d3f_forum_dir);
+
+	if( $uid && $allow_mark) {
+		$u2t_marked = empty( $_POST['u2t_marked'] ) ? 0 : 1 ;
+		$sql = "UPDATE ".$d3forum_prefix."users2topics"." SET u2t_marked=$u2t_marked,u2t_time=UNIX_TIMESTAMP() WHERE uid=$uid AND topic_id=$topic_id" ;
+		if( ! $xoops_db->query($sql)){
+			$sql = "INSERT INTO ".$d3forum_prefix."users2topics"." SET uid=$uid,topic_id=$topic_id,u2t_marked=$u2t_marked,u2t_time=UNIX_TIMESTAMP()" ;
+			$xoops_db->query($sql);
+		}
+	}
+}
+
+
+// ********************** refrence by d3forum *********************************************
+// delete posts recursively
+function wp_d3forum_delete_post_recursive( $d3forum_dirname , $post_id ,$isChild = false)
+{
+	$post_id = intval( $post_id ) ;
+	global $XPressME,$xoops_db;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+	$xpress_prefix = get_wp_prefix();
+	
+	$sql = "SELECT post_id FROM ".$d3forum_prefix ."posts"." WHERE pid=$post_id" ;
+	$found_id = $xoops_db->get_var($sql); 
+	if( empty($found_id))  die( "DB ERROR in wp_d3forum_delete_post_recursive ($sql)" ) ;
+
+	list( $topic_id ) = $xoops_db->get_results( "SELECT topic_id FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" );
+	$sql = "SELECT * FROM ".$d3forum_prefix."posts"." WHERE post_id=$post_id";
+	$topics= $xoops_db->get_results($sql);
+	foreach($topics as $topic){
+		$child_post_id = $topic->post_id ;
+		wp_d3forum_delete_post_recursive( $d3forum_dirname , $child_post_id ,true) ;
+	}
+
+	wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , true ) ;
+	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."_posts WHERE post_id=$post_id" ) ;
+	$xoops_db->query( "DELETE FROM ".$d3forum_prefix."_post_votes WHERE post_id=$post_id" ) ;
+	
+	$wp_comments = $xpress_prefix . 'comments';
+	$wp_posts = $xpress_prefix . 'posts';
+	$wp_d3forum_link = $xpress_prefix . 'd3forum_link';
+
+	$comment_ID = get_wp_comment_ID($post_id);
+	if ($comment_ID > 0){
+		$sql = "SELECT comment_post_ID FROM $wp_comments WHERE comment_ID = $comment_ID";
+		$comment_post_ID = $xoops_db->get_var($sql);
+
+		if ($isChild){		//The first comment is deleted on the WordPress side. 
+			$wp_sql = "DELETE FROM $wp_comments WHERE comment_ID = $comment_ID";
+			$xoops_db->query($wp_sql);
+			if (!empty($comment_post_ID)){
+				$wp_sql  = "UPDATE $wp_posts SET  comment_count = comment_count -1 WHERE ID = $comment_post_ID";
+				$xoops_db->query($wp_sql);
+			}
+		}		
+		$wp_sql = "DELETE FROM $wp_d3forum_link WHERE post_id = $post_id";
+		$xoops_db->query($wp_sql);
+	}
+}
+
+
+// delete a topic 
+function wp_d3forum_delete_topic( $d3forum_dirname , $topic_id , $delete_also_posts = true )
+{
+	global $XPressME,$xoops_db;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+	$xpress_prefix = get_wp_prefix();
+
+	$topic_id = intval( $topic_id ) ;
+
+	// delete posts
+	if( $delete_also_posts ) {
+		$sql = "SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
+		$posts = $xoops_db->query($sql);
+		if( empty($posts)) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+		foreach($posts as $post){
+			wp_d3forum_delete_post_recursive( $d3forum_dirname , $post->post_id ) ;
+		}
+	}
+
+	// delete notifications about this topic
+
+	// delete topic
+	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" )) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+
+	// delete u2t
+	if( ! $xoops_db->query( "DELETE FROM ".$d3forum_prefix."users2topics WHERE topic_id=$topic_id" )) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+}
+
+
+// store redundant informations to a topic from its posts
+// and rebuild tree informations (depth, order_in_tree)
+function wp_d3forum_sync_topic( $d3forum_dirname , $topic_id , $sync_also_forum = true , $sync_topic_title = false )
+{
+	global $XPressME,$xoops_db;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+	$xpress_prefix = get_wp_prefix();
+
+	$topic_id = intval( $topic_id ) ;
+
+	$sql = "SELECT forum_id FROM ".$d3forum_prefix."topics WHERE topic_id=$topic_id" ;
+	if( ! $results = $xoops_db->get_results($sql) ) die( "ERROR SELECT topic in sync topic" ) ;
+	list( $forum_id ) = $results ;
+
+	// get first_post_id
+	$sql = "SELECT post_id FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id AND pid=0" ;
+	if( ! $results = $xoops_db->get_results($sql) ) die( "ERROR SELECT first_post in sync topic" ) ;
+	list( $first_post_id ) = $results ;
+
+	// get last_post_id and total_posts
+	$sql = "SELECT MAX(post_id),COUNT(post_id) FROM ".$d3forum_prefix."posts WHERE topic_id=$topic_id" ;
+	if( ! $results = $xoops_db->get_results($sql) ) die( "ERROR SELECT last_post in sync topic" ) ;
+	list( $last_post_id , $total_posts ) = $results ;
+
+	if( empty( $total_posts ) ) {
+		// this is empty topic should be removed
+		wp_d3forum_delete_topic( $d3forum_dirname , $topic_id ,0) ;
+
+	} else {
+
+		// update redundant columns in topics table
+		list( $first_post_time , $first_uid , $first_subject , $unique_path ) = $xoops_db->get_results( "SELECT post_time,uid,subject,unique_path FROM ".$d3forum_prefix."posts WHERE post_id=$first_post_id" ) ;
+		list( $last_post_time , $last_uid ) = $xoops_db->get_results( "SELECT post_time,uid FROM ".$d3forum_prefix."posts WHERE post_id=$last_post_id" )  ;
+
+		// sync topic_title same as first post's subject if specified
+		$topictitle4set = $sync_topic_title ? "topic_title='".addslashes($first_subject)."'," : "" ;
+
+		if( ! $$xoops_db->query( "UPDATE ".$d3forum_prefix."topics SET {$topictitle4set} topic_posts_count=$total_posts, topic_first_uid=$first_uid, topic_first_post_id=$first_post_id, topic_first_post_time=$first_post_time, topic_last_uid=$last_uid, topic_last_post_id=$last_post_id, topic_last_post_time=$last_post_time WHERE topic_id=$topic_id" ) ) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+
+		// rebuild tree informations
+		$tree_array = wp_d3forum_maketree_recursive( $db->prefix($d3forum_dirname."_posts") , intval( $first_post_id ) , 'post_id' , array() , 0 , empty( $unique_path ) ? '.1' : $unique_path ) ;
+		if( ! empty( $tree_array ) ) foreach( $tree_array as $key => $val ) {
+			$xoops_db->query( "UPDATE ".$d3forum_prefix."posts SET depth_in_tree=".$val['depth'].", order_in_tree=".($key+1).", unique_path='".addslashes($val['unique_path'])."' WHERE post_id=".$val['post_id'] ) ;
+		}
+	}
+
+	if( $sync_also_forum ) return wp_d3forum_sync_forum( $d3forum_dirname , $forum_id ) ;
+	else return true ;
+}
+
+function wp_d3forum_maketree_recursive( $tablename , $post_id , $order = 'post_id' , $parray = array() , $depth = 0 , $unique_path = '.1' )
+{
+	global $XPressME,$xoops_db;
+
+	$parray[] = array( 'post_id' => $post_id , 'depth' => $depth , 'unique_path' => $unique_path ) ;
+
+	$sql = "SELECT post_id,unique_path FROM $tablename WHERE pid=$post_id ORDER BY $order" ;
+	if( ! $result = $xoops_db->get_results( $sql )) {
+		return $parray ;
+	}
+	$new_post_ids = array() ;
+	$max_count_of_last_level = 0 ;
+	while( list( $new_post_id , $new_unique_path ) = $db->fetchRow( $result ) ) {
+		$new_post_ids[ intval( $new_post_id ) ] = $new_unique_path ;
+		if( ! empty( $new_unique_path ) ) {
+			$count_of_last_level = intval( substr( strrchr( $new_unique_path , '.' ) , 1 ) ) ;
+			if( $max_count_of_last_level < $count_of_last_level ) {
+				$max_count_of_last_level = $count_of_last_level ;
+			}
+		}
+	}
+	foreach( $new_post_ids as $new_post_id => $new_unique_path ) {
+		if( empty( $new_unique_path ) ) {
+			$new_unique_path = $unique_path . '.' . ++ $max_count_of_last_level ;
+		}
+		$parray = wp_d3forum_maketree_recursive( $tablename , $new_post_id , $order , $parray , $depth + 1 , $new_unique_path ) ;
+	}
+	return $parray ;
+}
+
+function wp_d3forum_makecattree_recursive( $tablename , $cat_id , $order = 'cat_weight' , $parray = array() , $depth = 0 , $cat_title = '' )
+{
+	global $xoops_db;
+
+	$parray[] = array( 'cat_id' => $cat_id , 'depth' => $depth , 'cat_title' => $cat_title ) ;
+
+	$sql = "SELECT cat_id,cat_title FROM $tablename WHERE pid=$cat_id ORDER BY $order" ;
+	$results = $xoops_db->get_results( $sql ) ;
+	if( empty($results) ) {
+		return $parray ;
+	}
+	while( list( $new_cat_id , $new_cat_title ) = $results ) {
+		$parray = wp_d3forum_makecattree_recursive( $tablename , $new_cat_id , $order , $parray , $depth + 1 , $new_cat_title ) ;
+	}
+	return $parray ;
+}
+
+
+// store redundant informations to a forum from its topics
+function wp_d3forum_sync_forum( $d3forum_dirname , $forum_id , $sync_also_category = true )
+{
+	global $xoops_db;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+
+	$forum_id = intval( $forum_id ) ;
+
+	$sql = "SELECT cat_id FROM ".$d3forum_prefix."forums WHERE forum_id=$forum_id" ;
+	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT forum in sync forum" ) ;
+	list( $cat_id ) = $results ;
+
+	$sql = "SELECT MAX(topic_last_post_id),MAX(topic_last_post_time),COUNT(topic_id),SUM(topic_posts_count) FROM ".$d3forum_prefix."topics WHERE forum_id=$forum_id" ;
+	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT topics in sync forum" ) ;
+	list( $last_post_id , $last_post_time , $topics_count , $posts_count ) = $results ;
+
+	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."forums SET forum_topics_count=".intval($topics_count).",forum_posts_count=".intval($posts_count).", forum_last_post_id=".intval($last_post_id).", forum_last_post_time=".intval($last_post_time)." WHERE forum_id=$forum_id" ) ) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+
+	if( $sync_also_category ) return wp_d3forum_sync_category( $d3forum_dirname , $cat_id ) ;
+	else return true ;
+}
+
+// store redundant informations to a category from its forums
+function wp_d3forum_sync_category( $d3forum_dirname , $cat_id )
+{
+	global $xoops_db;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+
+	$cat_id = intval( $cat_id ) ;
+
+	// get children
+	include_once get_xoops_root_path()."/class/xoopstree.php" ;
+	$mytree = new XoopsTree( $d3forum_prefix."categories" , "cat_id" , "pid" ) ;
+	$children = $mytree->getAllChildId( $cat_id ) ;
+	$children[] = $cat_id ;
+	$children = array_map( 'intval' , $children ) ;
+
+	// topics/posts information belonging this category directly
+	$sql = "SELECT MAX(forum_last_post_id),MAX(forum_last_post_time),SUM(forum_topics_count),SUM(forum_posts_count) FROM ".$d3forum_prefix."forums WHERE cat_id=$cat_id" ;
+	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
+	list( $last_post_id , $last_post_time , $topics_count , $posts_count ) = $results ;
+
+	// topics/posts information belonging this category and/or subcategories
+	$sql = "SELECT MAX(forum_last_post_id),MAX(forum_last_post_time),SUM(forum_topics_count),SUM(forum_posts_count) FROM ".$d3forum_prefix."forums WHERE cat_id IN (".implode(",",$children).")" ;
+	if( ! $results = $xoops_db->get_results( $sql ) ) die( "ERROR SELECT forum in sync category" ) ;
+	list( $last_post_id_in_tree , $last_post_time_in_tree , $topics_count_in_tree , $posts_count_in_tree ) = $results ;
+
+	// update query
+	if( ! $result = $xoops_db->query( "UPDATE ".$d3forum_prefix."categories SET cat_topics_count=".intval($topics_count).",cat_posts_count=".intval($posts_count).", cat_last_post_id=".intval($last_post_id).", cat_last_post_time=".intval($last_post_time).",cat_topics_count_in_tree=".intval($topics_count_in_tree).",cat_posts_count_in_tree=".intval($posts_count_in_tree).", cat_last_post_id_in_tree=".intval($last_post_id_in_tree).", cat_last_post_time_in_tree=".intval($last_post_time_in_tree)." WHERE cat_id=$cat_id" ) ) die( _MD_D3FORUM_ERR_SQL.__LINE__ ) ;
+
+	// do sync parents
+	list( $pid ) = $xoops_db->get_results( "SELECT pid FROM ".$d3forum_prefix."categories WHERE cat_id=$cat_id" )  ;
+	if( $pid != $cat_id && $pid > 0 ) {
+		wp_d3forum_sync_category( $d3forum_dirname , $pid ) ;
+	}
+
+	return true ;
+}
+
+// make a new history entry for a post
+function wp_d3forum_transact_make_post_history( $d3forum_dirname , $post_id , $full_backup = false )
+{
+	global $xoops_db;
+	$d3forum_prefix = get_xoops_prefix() . $d3forum_dirname . '_';
+
+	$post_id = intval( $post_id ) ;
+
+	$results = $xoops_db->get_results( "SELECT * FROM ".$d3forum_prefix."posts WHERE post_id=$post_id" ) ;
+	if(empty($results)) return ;
+	$post_row = $results ;
+	$data = array() ;
+	$indexes = $full_backup ? array_keys( $post_row ) : array( 'subject' , 'post_text' ) ;
+	foreach( $indexes as $index ) {
+		$data[ $index ] = $post_row[ $index ] ;
+	}
+
+	// check the latest data in history
+	$results = $xoops_db->get_results( "SELECT data FROM ".$d3forum_prefix."post_histories WHERE post_id=$post_id ORDER BY history_time DESC" ) ;
+	if( !empty( $results ) ) {
+		list( $old_data_serialized ) = $results ;
+		$old_data = unserialize( $old_data_serialized ) ;
+		if( $old_data == $data ) return ;
+	}
+
+	if( ! $xoops_db->query( "INSERT INTO ".$d3forum_prefix."post_histories SET post_id=$post_id, history_time=UNIX_TIMESTAMP(), data='".mysql_real_escape_string( serialize( $data ) )."'" ) ) die( "DB ERROR ON making post_history".__LINE__ ) ;
+}
+
+?>
Index: trunk/wp-content/plugins/xpressme/include/xpress_common_functions.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/xpress_common_functions.php	(revision 100)
+++ trunk/wp-content/plugins/xpressme/include/xpress_common_functions.php	(revision 104)
@@ -4,4 +4,17 @@
 	require_once dirname(dirname(dirname(dirname(dirname( __FILE__ ))))) .'/include/config_from_xoops.class.php' ;
 	$xoops_config = new ConfigFromXoops;
+}
+
+function get_xoops_config($config_name,$module_dir){
+	global $xoops_db;
+	
+	$modules_db = get_xoops_prefix() . 'modules';
+	$config_db = get_xoops_prefix() . 'config';
+
+	$moduleID = $xoops_db->get_var("SELECT mid FROM $modules_db WHERE dirname = '$module_dir'");
+	if (empty($moduleID)) return null;
+	$conf_value = $xoops_db->get_var("SELECT conf_value FROM $config_db WHERE (conf_modid = $moduleID) AND (conf_name = '$config_name')");
+	if (empty($conf_value)) return null;
+	return  $conf_value;
 }
 
@@ -31,4 +44,11 @@
 	global $xoops_config;
 	$ret =$xoops_config->xoops_db_prefix . '_';
+	return $ret;
+}
+
+function get_xoops_trust_path()
+{
+	global $xoops_config;
+	$ret =$xoops_config->xoops_trust_path;
 	return $ret;
 }
@@ -62,4 +82,38 @@
 	return $wp_db_version;
 }
+
+function is_xpress_mobile()
+{
+	//ktai_style
+ 	if (function_exists('is_ktai')){
+ 		if (is_ktai()) {
+ //			$file_path = $GLOBALS['xoopsModuleConfig']["ktai_style_tmpdir"] . '/comments.php';
+			return true;
+		}
+	}
+	
+	//mobg
+	if (function_exists('is_mobile')) {
+		if (is_mobile()){
+			return true;
+		}
+	}
+	if (
+	  preg_match("/DoCoMo/", $_SERVER['HTTP_USER_AGENT']) ||
+	  preg_match("/softbank/", $_SERVER['HTTP_USER_AGENT']) ||
+	  preg_match("/vodafone/", $_SERVER['HTTP_USER_AGENT']) ||
+	  preg_match("/J-PHONE/", $_SERVER['HTTP_USER_AGENT']) ||
+	  preg_match("/UP\.Browser/", $_SERVER['HTTP_USER_AGENT']) ||
+	  preg_match("/ASTEL/", $_SERVER['HTTP_USER_AGENT']) ||
+	  preg_match("/PDXGW/", $_SERVER['HTTP_USER_AGENT'])
+	) 
+	{
+		return true;
+	} else {
+		return false;
+	}
+	
+}
+
 
 function is_block_cache_found($block_name)
Index: trunk/wp-content/plugins/xpressme/xpressme.php
===================================================================
--- trunk/wp-content/plugins/xpressme/xpressme.php	(revision 100)
+++ trunk/wp-content/plugins/xpressme/xpressme.php	(revision 104)
@@ -54,4 +54,19 @@
 //require_once('../include/custom_functions.php');
 
+//D3Forum Comment Integration
+if ($XPressME->is_use_d3forum){
+	require_once dirname( __FILE__ ).'/include/d3forum_comment_synchro.php' ;
+	add_action("comment_post",	"onaction_comment_post");
+	add_action("edit_comment",	"onaction_edit_comment");
+	add_action("delete_comment","onaction_delete_comment");
+	add_action("delete_post",	"onaction_delete_post");
+	add_action("wp_set_comment_status" , "onaction_comment_apobe");
+	
+	
+	add_filter('comments_template', "disp_d3forum_comments" );
+
+}
+
+
 // SQL debug windows
 add_filter('query', array(&$XPressME, 'xpress_sql_debug'));
Index: trunk/wp-content/plugins/xpressme/xpressme_class.php
===================================================================
--- trunk/wp-content/plugins/xpressme/xpressme_class.php	(revision 100)
+++ trunk/wp-content/plugins/xpressme/xpressme_class.php	(revision 104)
@@ -477,4 +477,38 @@
 		}
 	}	
+	
+	function set_d3forum_external_link_format()
+	{
+		global $xoops_db;
+/*		var $is_use_d3forum;
+		var $d3forum_module_dir;
+		var $d3forum_forum_id;
+		var $d3forum_external_link_format;
+		if ($this->$is_use_d3forum){
+			$content = '';
+			$content .= '<html><head><meta http-equiv="content-type" content="text/html; charset='._CHARSET.'" />';
+			$content .= '<meta http-equiv="content-language" content="'._LANGCODE.'" />' ;
+			$content .= '<title>XPressME SQL DEBUG</title>' ;
+			$content .= '</head><body>';
+			$content .= $GLOBALS['XPress_SQL_Query'];
+			$content .= '<div style="text-align:center;"><input class="formButton" value="CLOSE" type="button" onclick="javascript:window.close();" /></div></body></html>';
+
+			echo '<script type="text/javascript">
+				<!--//
+				xpress_debug_window = window.open("", "xpress_debug", "width=680 , height=600 ,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no");
+				xpress_debug_window.document.clear();
+				xpress_debug_window.focus();
+				';
+			$lines = preg_split("/(\r\n|\r|\n)( *)/", $content);
+			foreach ($lines as $line) {
+				echo 'xpress_debug_window.document.writeln("'.str_replace('"', '\"', $line).'");';
+			}
+			echo '
+				xpress_debug_window.document.close();
+				//-->
+			</script>';
+		}
+*/
+	}	
 
 }
Index: trunk/wp-content/themes/xpress_default/blocks/recent_comments_block_theme.php
===================================================================
--- trunk/wp-content/themes/xpress_default/blocks/recent_comments_block_theme.php	(revision 100)
+++ trunk/wp-content/themes/xpress_default/blocks/recent_comments_block_theme.php	(revision 104)
@@ -94,5 +94,5 @@
 				$item_no++;
 			}
-			$block['data_count'] = $item_no;  //xml unserialise error
+			$block['data_count'] = $item_no;
 		}
 	}
