prefix('modules');
$module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
$module_id = 0;
$sql = "SELECT mid FROM $module_tbl WHERE dirname LIKE '$mydirname'";
$result = $xoopsDB->query($sql, 0, 0);
if ($xoopsDB->getRowsNum($result) > 0){
$row = $xoopsDB->fetchArray($result);
$module_id = $row['mid'];
}
return $module_id;
}
function get_xoops_group_capabillities_name($mydirname , $uid=0){
if ($uid == 0){
return '';
}
global $xoopsModule;
$xoopsDB =& Database::getInstance();
$db_groups_users_link = $xoopsDB->prefix('groups_users_link');
$db_group_permission = $xoopsDB->prefix('group_permission');
$module_tbl = $xoopsDB->prefix('modules');
$module_id = get_xpress_mid($mydirname);
$sql = "SELECT uid FROM $db_groups_users_link WHERE groupid = 1 AND uid = $uid";
$result = $xoopsDB->query($sql, 0, 0);
if ($xoopsDB->getRowsNum($result) > 0){
return 'administrator';
}
$sql = "SELECT Max($db_group_permission.gperm_itemid) AS user_level ,$db_groups_users_link.uid ";
$sql .= "FROM $db_groups_users_link LEFT JOIN $db_group_permission ON $db_groups_users_link.groupid = $db_group_permission.gperm_groupid ";
$sql .= "WHERE $db_group_permission.gperm_modid = $module_id ";
$sql .= "GROUP BY $db_groups_users_link.uid ";
$sql .= "HAVING $db_groups_users_link.uid = $uid";
$result = $xoopsDB->query($sql, 0, 0);
if ($result === false){
return '';
}else{
if ($xoopsDB->getRowsNum($result) > 0){
$row = $xoopsDB->fetchArray($result);
$level = $row['user_level'];
switch($level) {
case 5:
return 'administrator';
case 4:
return 'editor';
case 3:
return 'author';
case 2:
return 'contributor';
case 1:
return 'subscriber';
default:
return 'subscriber';
}
} else {
return '';
}
}
}
//endif;
//if( ! function_exists( 'get_xpress_prefix' ) ) :
function get_xpress_prefix($mydirname){
global $xoopsModule;
if ($mydirname == 'wordpress'){
return 'wp_';
} else {
return $mydirname . '_';
}
}
//endif;
//if( ! function_exists( 'xpress_user_found' ) ) :
function xpress_user_found($mydirname, $login_name){
$wp_prefix = get_xpress_prefix($mydirname);
$xoopsDB =& Database::getInstance();
$db_xpress_users = $xoopsDB->prefix($wp_prefix . 'users');
$sql = "SELECT ID FROM $db_xpress_users WHERE user_login = '$login_name'";
$res = $xoopsDB->query($sql, 0, 0);
if ($res === false){
return 0;
} else {
if ($xoopsDB->getRowsNum($res) > 0){
$row = $xoopsDB->fetchArray($res);
return $row['ID'];
} else {
return 0;
}
}
}
//endif;
//if( ! function_exists( 'xpress_usermeta_sync' ) ) :
function xpress_usermeta_sync($mydirname , $uid,$meta_key,$meta_value, &$message){
$wp_prefix = get_xpress_prefix($mydirname);
$meta_key = "'" . $meta_key . "'";
$meta_value = "'" . $meta_value . "'";
$xoopsDB =& Database::getInstance();
$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
$sql = "SELECT user_id FROM $db_xpress_usermeta WHERE (user_id = $uid) AND (meta_key = $meta_key)";
$res = $xoopsDB->query($sql, 0, 0);
if ($res === false){
$sql2 = "INSERT INTO $db_xpress_usermeta (user_id,meta_key,meta_value) VALUES ($uid,$meta_key,$meta_value)";
}else {
if ($xoopsDB->getRowsNum($res) > 0){
$sql2 = "UPDATE $db_xpress_usermeta SET meta_value = $meta_value WHERE (user_id = $uid) AND (meta_key = $meta_key)";
} else {
$sql2 = "INSERT INTO $db_xpress_usermeta (user_id,meta_key,meta_value) VALUES ($uid,$meta_key,$meta_value)";
}
}
$res = $xoopsDB->queryF($sql2, 0, 0);
if ($res === false) {
$message .= "...ERR($sql2)";
return false;
} else {
return true;
}
}
//endif;
function sql_str($text = ''){
return "'" . $text . "'";
}
//if( ! function_exists( 'xpress_user_sync' ) ) :
function xpress_user_sync($sync_uid = 0, &$message){
$mydirname = basename( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) );
if( is_object( @$GLOBALS["xoopsModule"] ) ) {
$wp_prefix = get_xpress_prefix($mydirname);
$xoopsDB =& Database::getInstance();
$db_xoops_users = $xoopsDB->prefix('users');
$db_xpress_users = $xoopsDB->prefix($wp_prefix . 'users');
$message = 'Do Sync';
if ($sync_uid == 0) {
$xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users";
} else {
$xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users WHERE uid = $sync_uid";
}
$xu_res = $xoopsDB->query($xu_sql, 0, 0);
if ($xu_res === false){
$message .= '...ERR ('. $xu_sql . ')';
return false;
}else {
while($xu_row = $xoopsDB->fetchArray($xu_res)){
$capabillities_name = get_xoops_group_capabillities_name($mydirname , $xu_row['uid']);
if (empty($capabillities_name)){
$message .= "...PASS '" . $xu_row['uname'] ."'[uid=".$xu_row['uid'] ."](not xpress user)";
if ($sync_uid != 0){
$message .= '...NOT XPRESS USER ' . $xu_row['uname'];
return false;
}
} else {
$user_ID = $xu_row['uid'];
if ($capabillities_name != 'administrator' ){
if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
$capabillities_name = get_option('default_role');
}
}
$user_login_name = $xu_row['uname'];
$user_pass_md5 = $xu_row['pass'];
$user_email = $xu_row['email'];
$user_url = $xu_row['url'];
$user_regist_time = date('Y-m-d H:i:s' , $xu_row['user_regdate']);
$user_nicename = $xu_row['uname'];
$user_status = 0;
$user_display_name =empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] ;
$user_nickname = empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] ;
$user_aim = $xu_row['user_aim'];
$user_yim = $xu_row['user_yim'];
$user_first_name = $xu_row['uname'];
$user_last_name = '';
$user_description = '';
$user_jabber = '';
$user_rich_editing = 'true';
$is_update = false;
if (xpress_user_found($mydirname, $user_login_name)){
$wu_sql = "UPDATE $db_xpress_users ";
$wu_sql .= 'SET ';
$wu_sql .= "user_pass = '$user_pass_md5', ";
$wu_sql .= "user_email = '$user_email', ";
$wu_sql .= "user_url = '$user_url', ";
$wu_sql .= "user_nicename = '$user_nicename', ";
$wu_sql .= "user_registered = '$user_regist_time', ";
$wu_sql .= "user_status = 0 ";
$wu_sql .= "WHERE (user_login = '$user_login_name' )";
if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
$message .= '...UPDATE ' . $user_login_name . '(Level Not Change)';
} else {
$message .= '...UPDATE ' . $user_login_name . '(' . $capabillities_name . ')';
}
$is_update = true;
}else{
$wu_sql = "INSERT INTO $db_xpress_users ";
$wu_sql .= "(user_login , user_pass ,user_email , user_url , user_nicename " ;
$wu_sql .= " , user_registered , user_status , display_name) ";
$wu_sql .= "VALUES ";
$wu_sql .= "('$user_login_name', '$user_pass_md5', '$user_email', '$user_url', '$user_nicename' ";
$wu_sql .= " , '$user_regist_time', $user_status, '$user_display_name')";
if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . '-WP Default)';
} else {
$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
}
}
$wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
if ($wu_res === false){
$message .= '...ERR(' . $wu_sql . ')';
return false;
}
if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
if (!$is_update){
$sycc_user = new WP_User($user_ID);
$sycc_user->set_role($capabillities_name);
}
} else {
$sycc_user = new WP_User($user_ID);
$sycc_user->set_role($capabillities_name);
}
// if( $is_update === false){
if(!xpress_usermeta_sync($mydirname, $user_ID,'nickname',$user_nickname,$message) )return false;
if(!xpress_usermeta_sync($mydirname, $user_ID,'first_name',$user_first_name,$message) )return false;
if(!xpress_usermeta_sync($mydirname, $user_ID,'last_name',$user_last_name,$message) )return false;
if(!xpress_usermeta_sync($mydirname, $user_ID,'description',$user_description,$message) )return false;
if(!xpress_usermeta_sync($mydirname, $user_ID,'jabber',$user_jabber,$message) )return false;
// }
if(!xpress_usermeta_sync($mydirname, $user_ID,'aim',$user_aim,$message) )return false;
if(!xpress_usermeta_sync($mydirname, $user_ID,'yim',$user_yim,$message) )return false;
if ($is_update === false ) {
if(!xpress_usermeta_sync($mydirname, $user_ID,'rich_editing',$user_rich_editing,$message) )return false;
}
}
}
}
$message .= "...END";
return true;
}
}
//endif;
//if( ! function_exists( 'check_user_role_prefix' ) ) :
function check_user_role_prefix($mydirname){
$wp_prefix = get_xpress_prefix($mydirname);
$xoopsDB =& Database::getInstance();
$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
$user_meta_prefix =$xoopsDB->prefix($wp_prefix);
$sql = "SELECT * FROM $db_xpress_usermeta WHERE meta_key LIKE '%_" . $wp_prefix . "user_level' OR meta_key LIKE '%_" . $wp_prefix . "capabilities' OR meta_key LIKE '%_" . $wp_prefix . "autosave_draft_ids'";
$result = $xoopsDB->query($sql, 0, 0);
if ($result === false){
return -1;
}else{
$i=0;
while($row = $xoopsDB->fetchArray($result)){
$meta_key = $row['meta_key'];
if (strpos($meta_key,$user_meta_prefix) === false) {
$i++;
}
}
return $i;
}
}
//endif;
//if( ! function_exists( 'same_metakey_delete' ) ) :
function same_metakey_delete($mydirname,$metakey){
$wp_prefix = get_xpress_prefix($mydirname);
$xoopsDB =& Database::getInstance();
$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
$user_meta_prefix =$xoopsDB->prefix($wp_prefix);
$sql = "DELETE FROM $db_xpress_usermeta WHERE meta_key LIKE '$metakey'";
$result = $xoopsDB->query($sql, 0, 0);
if ($result === false){
return false;
}else{
return true;
}
}
//endif;
//if( ! function_exists( 'repair_user_role_prefix' ) ) :
function repair_user_role_prefix($mydirname){
$wp_prefix = get_xpress_prefix($mydirname);
$xoopsDB =& Database::getInstance();
$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
$user_meta_prefix =$xoopsDB->prefix($wp_prefix);
$ret = '';
$sql = "SELECT * FROM $db_xpress_usermeta WHERE meta_key LIKE '%_" . $wp_prefix . "user_level' OR meta_key LIKE '%_" . $wp_prefix . "capabilities' OR meta_key LIKE '%_" . $wp_prefix . "autosave_draft_ids'";
$result = $xoopsDB->queryF($sql, 0, 0);
if ($result === false){
return -1;
}else{
$i=0;
while($row = $xoopsDB->fetchArray($result)){
$meta_key = $row['meta_key'];
$umeta_id = $row['umeta_id'];
if (strpos($meta_key,$user_meta_prefix) === false) {
$new_meta_key = '';
if (strpos($meta_key,$wp_prefix.'user_level')){
$new_meta_key = $xoopsDB->prefix($wp_prefix . 'user_level');
}
if (strpos($meta_key,$wp_prefix.'capabilities')){
$new_meta_key = $xoopsDB->prefix($wp_prefix . 'capabilities');
}
if (strpos($meta_key,$wp_prefix.'autosave_draft_ids')){
$new_meta_key = $xoopsDB->prefix($wp_prefix . 'autosave_draft_ids');
}
if (!empty($new_meta_key)){
same_metakey_delete($mydirname,$new_meta_key);
$repair_sql = "UPDATE $db_xpress_usermeta ";
$repair_sql .= 'SET ';
$repair_sql .= "meta_key = '$new_meta_key' ";
$repair_sql .= "WHERE (umeta_id = $umeta_id )";
$repair_res = $xoopsDB->queryF($repair_sql, 0, 0);
if ($repair_res === false){
$ret .= '...ERR(' . $meta_key . '->' . $new_meta_key . ')
';
} else {
$ret .= '...DO(' . $meta_key . '->' . $new_meta_key . ')
';
}
}
}
}
}
return $ret;
}
//endif;
/*
* Get User ID of XOOPS from the login name.
*/
function get_xoops_user_id($login_name){
global $xoops_db;
$sql = "SELECT uid FROM " . get_xoops_prefix() . "users WHERE uname = '$login_name'";
$uid = $xoops_db->get_var($sql);
return $uid;
}
/*
*The user data of wordpress is written in the xoops user data.
*/
function user_sync_to_xoops($user_ID){
global $xoops_db;
$user_info = get_userdata($user_ID);
$xoops_uid = get_xoops_user_id($user_info->user_login);
$db_xoops_users = get_xoops_prefix() . "users";
$user_regdate = strtotime($user_info->user_registered);
$aim = get_usermeta($user_ID,'aim');
$yim = get_usermeta($user_ID,'yim');
if ($xoops_uid){
$wu_sql = "UPDATE $db_xoops_users ";
$wu_sql .= 'SET ';
$wu_sql .= "uname = '$user_info->user_login', ";
$wu_sql .= "pass = '$user_info->user_pass', ";
$wu_sql .= "email = '$user_info->user_email', ";
$wu_sql .= "url = '$user_info->user_url', ";
$wu_sql .= "name = '$user_info->display_name', ";
$wu_sql .= "user_aim = '$aim', ";
$wu_sql .= "user_yim = '$yim', ";
$wu_sql .= "user_regdate = $user_regdate ";
$wu_sql .= "WHERE (uid = $xoops_uid )";
$xoops_db->query($wu_sql);
}else{
$wu_sql = "INSERT INTO $db_xoops_users ";
$wu_sql .= "(uname , pass ,email , url , name , user_aim , user_yim , user_regdate) " ;
$wu_sql .= "VALUES ";
$wu_sql .= "('$user_info->user_login', '$user_info->user_pass', '$user_info->user_email', '$user_info->user_url', '$user_info->display_name' ";
$wu_sql .= " , '$aim' , '$yim' , $user_regdate )";
$xoops_db->query($wu_sql);
// get xoops users default groupe ID
$db_xoops_group = get_xoops_prefix() . 'groups';
$default_xoops_group_id = $xoops_db->get_var("SELECT groupid FROM $db_xoops_group WHERE group_type = 'User'");
// get insert users ID
$db_xoops_group = get_xoops_prefix() . 'users';
$user_id = $xoops_db->get_var("SELECT uid FROM $db_xoops_group WHERE uname = '$user_info->user_login'");
// insert groups_users_link
$db_xoops_group_users_link = get_xoops_prefix() . 'groups_users_link';
$default_xoops_group_id = $xoops_db->get_var("INSERT INTO $db_xoops_group_users_link (groupid , uid ) VALUES ($default_xoops_group_id , $user_id)");
}
}
?>