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 '';
}
} 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, $uid){
$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 ID = $uid";
$res = $xoopsDB->query($sql, 0, 0);
if ($res === false){
return false;
} else {
if ($xoopsDB->getRowsNum($res) > 0){
return true;
} else {
return false;
}
}
}
//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 . "'";
$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( __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 = sql_str($xu_row['uname']);
$user_pass_md5 = sql_str( $xu_row['pass']);
$user_email = sql_str( $xu_row['email']);
$user_url = sql_str( $xu_row['url']);
$user_regist_time = sql_str(date('Y-m-d H:i:s' , $xu_row['user_regdate']));
$user_nicename = sql_str( $xu_row['uname']);
$user_status = 0;
$user_display_name =sql_str( empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] );
$user_nickname = sql_str( empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] );
$user_aim = sql_str( $xu_row['user_aim']);
$user_yim = sql_str( $xu_row['user_yim']);
$user_first_name = sql_str( $xu_row['uname']);
$user_last_name = sql_str('');
$user_description = sql_str('');
$user_jabber = sql_str('');
$user_rich_editing = sql_str('true');
$is_update = false;
if (xpress_user_found($mydirname, $user_ID)){
$wu_sql = "UPDATE $db_xpress_users ";
$wu_sql .= 'SET ';
$wu_sql .= "user_login = $user_login_name, ";
$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 (ID = $user_ID )";
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 .= "(ID , user_login , user_pass ,user_email , user_url , user_nicename " ;
$wu_sql .= " , user_registered , user_status , display_name) ";
$wu_sql .= "VALUES ";
$wu_sql .= "($user_ID, $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;
?>