function fun_open_template_redirect() { $redirect = fun_get_redirect_uri(); $die_title = '请重试或报告管理员'; $redirect_text = '<p>' . $die_title . ' </p><p><a href="' . $redirect . '">点击返回</a></p>'; $user_ID = get_current_user_id(); function fun_open_login($openid = '', $token = '', $type = 'qq', $name = '') { $cookie_name = fun_redirect_cookie_name(); $redirect = isset($_COOKIE[$cookie_name]) ? urldecode($_COOKIE[$cookie_name]) : home_url(); $die_title = '请重试或报告管理员'; $redirect_text = '<p>' . $die_title . ' </p><p><a href="' . $redirect . '">点击返回</a></p>'; $user_ID = get_current_user_id(); $id_field = 'fun_' . $type . '_openid'; $token_field = 'fun_' . $type . '_access_token'; global $wpdb; $user_exist = $wpdb->get_var("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='{$id_field}' AND meta_value='{$openid}' "); if (is_user_logged_in()) { if (isset($user_exist) && (int) $user_exist > 0) { wp_die($name . ' 已有绑定账号,请绑定其他账号或先解除原有账号。 ' . $redirect_text, $die_title); } else { update_user_meta($user_ID, $id_field, $openid); update_user_meta($user_ID, $token_field, $token); header('Location:' . $redirect); exit; } } else { if (isset($user_exist) && (int) $user_exist > 0) { $insert_user_id = $user_exist; $is_new_user = 0; } else { $user_login = strtoupper(substr($type, 0, 1)) . $openid; $insert_user_id = wp_insert_user(array('user_login' => $user_login, 'nickname' => $name, 'display_name' => $name, 'user_pass' => wp_generate_password())); $is_new_user = 1; } if (is_wp_error($insert_user_id)) { wp_die('登录失败! ' . $redirect_text, $die_title); } else { update_user_meta($insert_user_id, $id_field, $openid); update_user_meta($insert_user_id, $token_field, $token); if ($type == 'weixin') { update_user_meta($insert_user_id, 'fun_weixin_refresh_token', $refresh_token); update_user_meta($insert_user_id, 'fun_weixin_headimgurl', $headimgurl); } if ($is_new_user) { update_user_meta($insert_user_id, 'fun_avatar', $type); wp_update_user(array('ID' => $insert_user_id, 'role' => ot_get_option('fun_open_role'))); add_fun_message($insert_user_id, 'unread', current_time('mysql'), __('请完善账号信息', 'tinection'), sprintf(__('欢迎来到%1$s,请<a href="%2$s">完善资料</a>,其中电子邮件尤为重要,许多信息都将通过电子邮件通知您!', 'tinection'), get_bloginfo('name'), admin_url('profile.php'))); } update_user_meta($insert_user_id, 'fun_latest_login', current_time('mysql')); wp_set_current_user($insert_user_id, $user_login); wp_set_auth_cookie($insert_user_id); do_action('wp_login', $user_login); header('Location:' . $redirect); exit; } } } function fun_open_logout($type = 'qq') { $redirect = get_edit_profile_url(); if ($type === 'qq') { $type = 'qq'; $name = ' <img src="' . get_bloginfo('template_url') . '/images/qq_32x32.png" > QQ '; } elseif ($type === 'weibo') { $type = 'weibo'; $name = ' <img src="' . get_bloginfo('template_url') . '/images/weibo_32x32.png" > 微博 '; } else { $type = 'weixin'; $name = ' <img src="' . get_bloginfo('template_url') . '/images/weixin_32x32.png" > 微信 '; } if (isset($_GET['wpnonce']) && wp_verify_nonce(trim($_GET['wpnonce']), $type . '_logout')) { $user_ID = get_current_user_id(); if ($type === 'weibo') { $token = get_user_meta($user_ID, 'fun_weibo_access_token', true); $info = wp_remote_retrieve_body(wp_remote_get('https://api.weibo.com/oauth2/revokeoauth2?access_token=' . $token)); } delete_user_meta($user_ID, 'fun_' . $type . '_openid'); delete_user_meta($user_ID, 'fun_' . $type . '_access_token'); header('Location:' . $redirect); exit; } else { wp_die(sprintf(__('你正在试图解除%1$s绑定,确定这样做吗?<a href="%2$s">点击继续</a> <p>不知道这是哪里?<a href="%3$s">点击返回</a></p>', 'tin'), $name, add_query_arg('wpnonce', wp_create_nonce($type . '_logout'), fun_get_current_page_url()), $redirect), __('解除账号绑定', 'tinection')); } } function fun_set_redirect_cookie() { setcookie(fun_redirect_cookie_name(), urlencode(fun_get_redirect_uri()), time() + 3600); } function fun_get_redirect_text() { $cookie_name = fun_redirect_cookie_name(); $redirect = isset($_COOKIE[$cookie_name]) ? urldecode($_COOKIE[$cookie_name]) : fun_get_redirect_uri(); return '<a href="' . $redirect . '">点击返回</a>'; } function fun_connect_check($str = '') { if (empty($str)) { wp_die('服务器无法连接开放平台,请重试或联系管理员!' . fun_get_redirect_text(), '无法连接开发平台'); } return $str; } if (isset($_GET['connect']) && trim($_GET['connect']) === 'qq' && fun_is_open_qq() && (is_home() || is_front_page())) { fun_set_redirect_cookie(); $OPEN_QQ = array('APPID' => ot_get_option('fun_open_qq_id'), 'APPKEY' => ot_get_option('fun_open_qq_key'), 'CALLBACK' => home_url('/?connect=qq')); if (isset($_GET['action'])) { if (trim($_GET['action']) === 'login') { if (is_user_logged_in() && get_user_meta($user_ID, 'fun_qq_openid', TRUE)) { wp_die('你已经绑定了QQ号,一个账号只能绑定一个QQ号,如要更换,请先解绑现有QQ账号,再绑定新的。<p><a href="' . $redirect . '">点击返回</a></p>', '不能绑定多个QQ'); } $state = md5(uniqid(rand(), true)); $params = array('response_type' => 'code', 'client_id' => $OPEN_QQ['APPID'], 'state' => $state, 'scope' => 'get_user_info,get_info,add_t,del_t,add_pic_t,get_repost_list,get_other_info,get_fanslist,get_idollist,add_idol,del_idol', 'redirect_uri' => $OPEN_QQ['CALLBACK']); setcookie('qq_state', md5($state), time() + 600); header('Location:https://graph.qq.com/oauth2.0/authorize?' . http_build_query($params)); exit; } if (trim($_GET['action']) === 'logout' && is_user_logged_in()) { fun_open_logout('qq'); } } if (isset($_GET['code']) && isset($_GET['state']) && isset($_COOKIE['qq_state']) && $_COOKIE['qq_state'] == md5($_GET['state'])) { $params = array('grant_type' => 'authorization_code', 'code' => $_GET['code'], 'client_id' => $OPEN_QQ['APPID'], 'client_secret' => $OPEN_QQ['APPKEY'], 'redirect_uri' => $OPEN_QQ['CALLBACK']); $response = fun_connect_check(wp_remote_retrieve_body(wp_remote_get('https://graph.qq.com/oauth2.0/token?' . http_build_query($params)))); if (strpos($response, "callback") !== false) { $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos - 1); $msg = json_decode($response); if (isset($msg->error)) { wp_die("<b>error</b> " . $msg->error . " <b>msg</b> " . $msg->error_description . $redirect_text, $die_title); } } $params = array(); parse_str($response, $params); $token = $params['access_token']; $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $token; $str = fun_connect_check(wp_remote_retrieve_body(wp_remote_get($graph_url))); if (strpos($str, "callback") !== false) { $lpos = strpos($str, "("); $rpos = strrpos($str, ")"); $str = substr($str, $lpos + 1, $rpos - $lpos - 1); } $user = json_decode($str); if (isset($user->error)) { wp_die("<b>error</b> " . $user->error . " <b>msg</b> " . $user->error_description . $redirect_text, $die_title); } $qq_openid = $user->openid; $info_url = "https://graph.qq.com/user/get_user_info?access_token=" . $token . "&oauth_consumer_key=" . $OPEN_QQ['APPID'] . "&openid=" . $qq_openid; $info = json_decode(fun_connect_check(wp_remote_retrieve_body(wp_remote_get($info_url)))); if ($info->ret) { wp_die("<b>error</b> " . $info->ret . " <b>msg</b> " . $info->msg . $redirect_text, $die_title); } fun_open_login($qq_openid, $token, 'qq', $info->nickname); exit; } } if (isset($_GET['connect']) && trim($_GET['connect']) === 'weibo' && fun_is_open_weibo() && (is_home() || is_front_page())) { fun_set_redirect_cookie(); $OPEN_WEIBO = array('KEY' => ot_get_option('fun_open_weibo_key'), 'SECRET' => ot_get_option('fun_open_weibo_secret'), 'CALLBACK' => home_url('/?connect=weibo')); if (isset($_GET['action'])) { if (trim($_GET['action']) === 'login') { if (is_user_logged_in() && get_user_meta($user_ID, 'fun_weibo_openid', TRUE)) { wp_die('你已经绑定了微博账号,一个账号只能绑定一个微博,如要更换,请先解绑现有微博账号,再绑定新的。<p><a href="' . $redirect . '">点击返回</a></p>', '不能绑定多个微博'); } $params = array('response_type' => 'code', 'client_id' => $OPEN_WEIBO['KEY'], 'redirect_uri' => $OPEN_WEIBO['CALLBACK']); header('Location:https://api.weibo.com/oauth2/authorize?' . http_build_query($params)); exit; } if (trim($_GET['action']) === 'logout' && is_user_logged_in()) { fun_open_logout('weibo'); } } if (isset($_GET['code'])) { $access = fun_connect_check(wp_remote_retrieve_body(wp_remote_post('https://api.weibo.com/oauth2/access_token?', array('body' => array('grant_type' => 'authorization_code', 'client_id' => $OPEN_WEIBO['KEY'], 'client_secret' => $OPEN_WEIBO['SECRET'], 'code' => trim($_GET['code']), 'redirect_uri' => $OPEN_WEIBO['CALLBACK']))))); $access = json_decode($access, true); if (isset($access["error"])) { wp_die("<b>error</b> " . $access["error"] . " <b>msg</b> " . $access["error_description"] . $redirect_text, $die_title); } $openid = $access["uid"]; $token = $access["access_token"]; $info = fun_connect_check(wp_remote_retrieve_body(wp_remote_get('https://api.weibo.com/2/users/show.json?access_token=' . $token . '&uid=' . $openid))); $info = json_decode($info, true); if (isset($info["error"])) { wp_die("<b>error</b> " . $info["error_code"] . " <b>msg</b> " . $info["error"] . $redirect_text, $die_title); } fun_open_login($openid, $token, 'weibo', $info["name"]); exit; } } if (isset($_GET['connect']) && trim($_GET['connect']) === 'weixin' && fun_is_open_weixin() && (is_home() || is_front_page())) { fun_set_redirect_cookie(); $OPEN_WEIXIN = array('APPID' => ot_get_option('fun_open_weixin_id'), 'APPKEY' => ot_get_option('fun_open_weixin_key'), 'CALLBACK' => home_url('/?connect=weixin')); if (isset($_GET['action'])) { if (trim($_GET['action']) === 'login') { if (is_user_logged_in() && get_user_meta($user_ID, 'fun_weixin_openid', TRUE)) { wp_die('你已经绑定了微信号,一个账号只能绑定一个微信号,如要更换,请先解绑现有微信账号,再绑定新的。<p><a href="' . $redirect . '">点击返回</a></p>', '不能绑定多个微信'); } $state = md5(uniqid(rand(), true)); $params = array('response_type' => 'code', 'appid' => $OPEN_WEIXIN['APPID'], 'state' => $state, 'scope' => 'snsapi_login,snsapi_base,snsapi_userinfo', 'redirect_uri' => $OPEN_WEIXIN['CALLBACK']); setcookie('weixin_state', md5($state), time() + 600); header('Location:https://open.weixin.qq.com/connect/qrconnect?' . http_build_query($params) . '#wechat_redirect'); exit; } if (trim($_GET['action']) === 'logout' && is_user_logged_in()) { fun_open_logout('qq'); } } if (isset($_GET['code']) && isset($_GET['state']) && isset($_COOKIE['weixin_state']) && $_COOKIE['weixin_state'] == md5($_GET['state'])) { $weixin_access = get_user_meta($user_ID, 'fun_weixin_access_token', TRUE); $weixin_refresh = get_user_meta($user_ID, 'fun_weixin_refresh_token', TRUE); if ($weixin_refresh) { $params = array('grant_type' => 'refresh_token', 'refresh_token' => $weixin_refresh, 'appid' => $OPEN_WEIXIN['APPID'], 'redirect_uri' => $OPEN_WEIXIN['CALLBACK']); $response = fun_connect_check(wp_remote_retrieve_body(wp_remote_get('https://api.weixin.qq.com/sns/oauth2/refresh_token' . http_build_query($params)))); } else { $params = array('grant_type' => 'authorization_code', 'code' => $_GET['code'], 'appid' => $OPEN_WEIXIN['APPID'], 'secret' => $OPEN_WEIXIN['APPKEY'], 'redirect_uri' => $OPEN_WEIXIN['CALLBACK']); $response = fun_connect_check(wp_remote_retrieve_body(wp_remote_get('https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($params)))); } if (!empty($response)) { $msg = json_decode($response); if (isset($msg->errcode)) { wp_die("<b>error</b> " . $msg->errcode . " <b>msg</b> " . $msg->errmsg . $redirect_text, $die_title); } } $params = json_decode($response); $token = $params->access_token; $refresh_token = $params->refresh_token; $weixin_openid = $params->openid; $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $token . "&openid=" . $weixin_openid; $str = fun_connect_check(wp_remote_retrieve_body(wp_remote_get($info_url))); $user = json_decode($str); if (isset($user->errcode)) { wp_die("<b>error</b> " . $user->errcode . " <b>msg</b> " . $user->errmsg . $redirect_text, $die_title); } $headimgurl = $user->headimgurl; $headimgurl = str_replace('/0', '/96', $headimgurl); fun_open_login($weixin_openid, $token, 'weixin', $user->nickname, $refresh_token, $headimgurl); exit; } } }
/** * * 获取头像类型 * @param int $user_id */ function fun_get_avatar_type($user_id) { $id = intval($user_id); if ($id === 0) { return 'default'; } $avatar = get_user_meta($id, 'fun_avatar', true); $customize = get_user_meta($id, 'fun_customize_avatar', true); if ($avatar == 'qq' && fun_is_open_qq($id)) { return 'qq'; } if ($avatar == 'weibo' && fun_is_open_weibo($id)) { return 'weibo'; } if ($avatar == 'weixin' && fun_is_open_weixin($id)) { return 'weixin'; } if ($customize && !empty($customize)) { return 'customize'; } return 'default'; }
</div> <?php } ?> <?php if ($weibo) { ?> <div class="form-group"> <label class="col-sm-3 control-label"><?php _e('微博账号', 'tin'); ?> </label> <div class="col-sm-9"> <?php if (fun_is_open_weibo($user_info->ID)) { ?> <span class="help-block"><?php _e('已绑定', 'tin'); ?> <a href="<?php echo home_url('/?connect=weibo&action=logout'); ?> "><?php _e('点击解绑', 'tin'); ?> </a></span> <?php echo fun_get_avatar($user_info->ID, '100', 'weibo'); ?> <?php