| 1 | <?php | 
|---|
| 2 | /** | 
|---|
| 3 | * XPress - WordPress for XOOPS | 
|---|
| 4 | * | 
|---|
| 5 | * Adding multi-author features to XPressME | 
|---|
| 6 | * | 
|---|
| 7 | * @copyright   The XPressME project | 
|---|
| 8 | * @license             http://www.fsf.org/copyleft/gpl.html GNU public license | 
|---|
| 9 | * @author              toemon | 
|---|
| 10 | * @since               2.05 | 
|---|
| 11 | * @version             $Id$ | 
|---|
| 12 | * @package             module::xpress | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // ***********************************  Start Pluggable Function Edit (wp-include/pluggable.php) ************************************ | 
|---|
| 16 |  | 
|---|
| 17 | if ( !function_exists('get_currentuserinfo') ) : | 
|---|
| 18 | function get_currentuserinfo() { | 
|---|
| 19 | global $current_user; | 
|---|
| 20 | global $xoopsModule,$xoopsUser,$xoopsUserIsAdmin; | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | if ($xoopsModule){ | 
|---|
| 24 | if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) | 
|---|
| 25 | return false; | 
|---|
| 26 |  | 
|---|
| 27 | if (is_object($xoopsUser)){                     // When the user is logging in xoops | 
|---|
| 28 | if ( ! empty($current_user) ){ | 
|---|
| 29 | $xoops_user = $xoopsUser->getVar("uname"); | 
|---|
| 30 | if ($current_user->user_login == $xoops_user){  // If xoops login user and wordpress current user are the same | 
|---|
| 31 | return; | 
|---|
| 32 | } | 
|---|
| 33 | } | 
|---|
| 34 | if (check_xpress_auth_cookie()){        //The cookie is login user's or it checks it | 
|---|
| 35 | if (function_exists('wp_validate_auth_cookie')){ | 
|---|
| 36 | if ( $user = wp_validate_auth_cookie() ) { | 
|---|
| 37 | // When the user meta prefix is different according to the change in the xoops data base prefix, it restores it. | 
|---|
| 38 | if (!check_user_meta_prefix($user)){ | 
|---|
| 39 | repair_user_meta_prefix(); | 
|---|
| 40 | } | 
|---|
| 41 | wp_set_current_user($user); | 
|---|
| 42 | return ; | 
|---|
| 43 | } | 
|---|
| 44 | } else { // for WP2.0 | 
|---|
| 45 | if ( !empty($_COOKIE[USER_COOKIE]) && !empty($_COOKIE[PASS_COOKIE])){ | 
|---|
| 46 | if(wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true) ) { | 
|---|
| 47 | $user_login = $_COOKIE[USER_COOKIE]; | 
|---|
| 48 | wp_set_current_user(0, $user_login); | 
|---|
| 49 | return; | 
|---|
| 50 | } | 
|---|
| 51 | } | 
|---|
| 52 | } | 
|---|
| 53 | } | 
|---|
| 54 | return xpress_login(); | 
|---|
| 55 | } else {                                                        // For the xoops guest | 
|---|
| 56 | wp_set_current_user(0); | 
|---|
| 57 | return false; | 
|---|
| 58 | } | 
|---|
| 59 | } else { | 
|---|
| 60 | // WP original | 
|---|
| 61 | if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) | 
|---|
| 62 | return false; | 
|---|
| 63 |  | 
|---|
| 64 | if ( ! empty($current_user) ) | 
|---|
| 65 | return; | 
|---|
| 66 |  | 
|---|
| 67 | if (function_exists('wp_validate_auth_cookie')){ | 
|---|
| 68 | if ( ! $user = wp_validate_auth_cookie() ) { | 
|---|
| 69 | if ( empty($_COOKIE[LOGGED_IN_COOKIE]) || !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') ) { | 
|---|
| 70 | wp_set_current_user(0); | 
|---|
| 71 | return false; | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 | wp_set_current_user($user); | 
|---|
| 75 | } else { // for WP2.0 | 
|---|
| 76 | if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) || | 
|---|
| 77 | !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true) ) { | 
|---|
| 78 | wp_set_current_user(0); | 
|---|
| 79 | return false; | 
|---|
| 80 | } | 
|---|
| 81 | $user_login = $_COOKIE[USER_COOKIE]; | 
|---|
| 82 | wp_set_current_user(0, $user_login); | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | endif; | 
|---|
| 87 |  | 
|---|
| 88 | if ( !function_exists('xpress_login') ) : | 
|---|
| 89 | function xpress_login(){ | 
|---|
| 90 | global $current_user; | 
|---|
| 91 | global $xoopsModule,$xoopsUser,$xoopsUserIsAdmin; | 
|---|
| 92 |  | 
|---|
| 93 | if(is_object($xoopsUser)){ | 
|---|
| 94 | $u_name = $xoopsUser->getVar("uname"); | 
|---|
| 95 | $u_pass_md5 = $xoopsUser->getVar("pass"); | 
|---|
| 96 | if ( ! empty($u_name) && ! empty($u_pass_md5) ) { | 
|---|
| 97 | include_once dirname( __FILE__ ).'/user_sync_xoops.php'; | 
|---|
| 98 | repair_user_meta_prefix();  //Repair when data base prefix is changed on XOOPS side | 
|---|
| 99 | $messege = ''; | 
|---|
| 100 | $ret = user_sync_to_wordpress($xoopsUser->getVar("uid"),$messege); | 
|---|
| 101 | if ($ret){ | 
|---|
| 102 | $user = new WP_User(0, $u_name); | 
|---|
| 103 | if ( wp_login($u_name, $u_pass_md5) ) { | 
|---|
| 104 | wp_setcookie($u_name, $u_pass_md5, true, '', '', false); | 
|---|
| 105 | do_action('wp_login', $u_name); | 
|---|
| 106 | wp_set_current_user($user->ID); | 
|---|
| 107 | return  true; | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 | if ( ! empty($current_user) ){ | 
|---|
| 113 | wp_set_current_user(0); | 
|---|
| 114 | wp_logout(); | 
|---|
| 115 | wp_clear_auth_cookie(); | 
|---|
| 116 | } | 
|---|
| 117 | return false; | 
|---|
| 118 | } | 
|---|
| 119 | endif; | 
|---|
| 120 |  | 
|---|
| 121 | if ( !function_exists('check_xpress_auth_cookie') ) : | 
|---|
| 122 | function check_xpress_auth_cookie() {           // for wp2.5 | 
|---|
| 123 | if ( empty($_COOKIE[AUTH_COOKIE]) ){ | 
|---|
| 124 | return false; | 
|---|
| 125 | } | 
|---|
| 126 | $cookie = $_COOKIE[AUTH_COOKIE]; | 
|---|
| 127 |  | 
|---|
| 128 | $cookie_elements = explode('|', $cookie); | 
|---|
| 129 | if ( count($cookie_elements) < 3 ){ | 
|---|
| 130 | return false; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | if(is_object($GLOBALS["xoopsModule"])){ | 
|---|
| 134 | //              && WP_BLOG_DIRNAME == $GLOBALS["xoopsModule"]->getVar("dirname")){ | 
|---|
| 135 | if(is_object($GLOBALS["xoopsUser"])){ | 
|---|
| 136 | $u_name = $GLOBALS["xoopsUser"]->getVar("uname"); | 
|---|
| 137 | list($username, $expiration, $hmac) = $cookie_elements; | 
|---|
| 138 | if ($u_name == $username) { | 
|---|
| 139 | return true; | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 | } else { | 
|---|
| 143 | $mydirname = basename( dirname( dirname( __FILE__ ) ) ) ; | 
|---|
| 144 | $org_url = $_SERVER['REQUEST_URI']; | 
|---|
| 145 | $needle = '/modules/' . $mydirname . '/wp-admin/'; | 
|---|
| 146 | if (strstr($org_url , $needle)){ | 
|---|
| 147 | return true; | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 | return false; | 
|---|
| 151 | } | 
|---|
| 152 | endif; | 
|---|
| 153 |  | 
|---|
| 154 | if ( !function_exists('wp_check_password') ) : | 
|---|
| 155 | // for wordpress2.5 | 
|---|
| 156 | function wp_check_password($password, $hash, $user_id = '') { | 
|---|
| 157 | global $wp_hasher; | 
|---|
| 158 | global $xoops_config,$xoops_db; | 
|---|
| 159 |  | 
|---|
| 160 | // For attestation when password has been sent as hash value. (When having logged it in from Xoops and ImpressCMS) | 
|---|
| 161 | if ($hash == $password){ | 
|---|
| 162 | return apply_filters('check_password', true, $password, $hash, $user_id); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | // Password authentication for Xoops | 
|---|
| 166 | if ( strlen($hash) <= 32 ) { | 
|---|
| 167 | $check = ( $hash == md5($password) ); | 
|---|
| 168 | return apply_filters('check_password', $check, $password, $hash, $user_id); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | // Password authentication for ImpressCMS | 
|---|
| 172 | if($xoops_config->is_impress && function_exists('hash')){ | 
|---|
| 173 | $mainSalt = $xoops_config->xoops_db_salt; | 
|---|
| 174 | // get user salt | 
|---|
| 175 | $xpress_user_db = $xoops_config->module_db_prefix . 'users'; | 
|---|
| 176 | $xoops_user_db = $xoops_config->xoops_db_prefix . '_users'; | 
|---|
| 177 | $login_name = $xoops_db->get_var("SELECT user_login FROM $xpress_user_db WHERE ID = $user_id"); | 
|---|
| 178 | $user_salt = $xoops_db->get_var("SELECT salt FROM $xoops_user_db WHERE uname = '$login_name'"); | 
|---|
| 179 | $enc_type = intval( $xoops_db->get_var("SELECT enc_type FROM $xoops_user_db WHERE uname = '$login_name'") ); | 
|---|
| 180 |  | 
|---|
| 181 | // Make Impress hash | 
|---|
| 182 | if($enc_type == 0) {$impress_hash = md5($password);} // no salt used for compatibility with external scripts such as ipb/phpbb etc. | 
|---|
| 183 | elseif($enc_type == 1) {$impress_hash = hash('sha256', $user_salt.md5($password).$mainSalt);} | 
|---|
| 184 | elseif($enc_type == 2) {$impress_hash = hash('sha384', $user_salt.md5($password).$mainSalt);} | 
|---|
| 185 | elseif($enc_type == 3) {$impress_hash = hash('sha512', $user_salt.md5($password).$mainSalt);} | 
|---|
| 186 | elseif($enc_type == 4) {$impress_hash = hash('ripemd128', $user_salt.md5($password).$mainSalt);} | 
|---|
| 187 | elseif($enc_type == 5) {$impress_hash = hash('ripemd160', $user_salt.md5($password).$mainSalt);} | 
|---|
| 188 | elseif($enc_type == 6) {$impress_hash = hash('whirlpool', $user_salt.md5($password).$mainSalt);} | 
|---|
| 189 | elseif($enc_type == 7) {$impress_hash = hash('haval128,4', $user_salt.md5($password).$mainSalt);} | 
|---|
| 190 | elseif($enc_type == 8) {$impress_hash = hash('haval160,4', $user_salt.md5($password).$mainSalt);} | 
|---|
| 191 | elseif($enc_type == 9) {$impress_hash = hash('haval192,4', $user_salt.md5($password).$mainSalt);} | 
|---|
| 192 | elseif($enc_type == 10) {$impress_hash = hash('haval224,4', $user_salt.md5($password).$mainSalt);} | 
|---|
| 193 | elseif($enc_type == 11) {$impress_hash = hash('haval256,4', $user_salt.md5($password).$mainSalt);} | 
|---|
| 194 | elseif($enc_type == 12) {$impress_hash = hash('haval128,5', $user_salt.md5($password).$mainSalt);} | 
|---|
| 195 | elseif($enc_type == 13) {$impress_hash = hash('haval160,5', $user_salt.md5($password).$mainSalt);} | 
|---|
| 196 | elseif($enc_type == 14) {$impress_hash = hash('haval192,5', $user_salt.md5($password).$mainSalt);} | 
|---|
| 197 | elseif($enc_type == 15) {$impress_hash = hash('haval224,5', $user_salt.md5($password).$mainSalt);} | 
|---|
| 198 | elseif($enc_type == 16) {$impress_hash = hash('haval256,5', $user_salt.md5($password).$mainSalt);} | 
|---|
| 199 |  | 
|---|
| 200 | if ($hash == $impress_hash){ | 
|---|
| 201 | return apply_filters('check_password', true, $password, $hash, $user_id); | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | // If the hash is still md5... | 
|---|
| 206 | if ( strlen($hash) <= 32 ) { | 
|---|
| 207 | $check = ( $hash == md5($password) ); | 
|---|
| 208 | /* A new hash is not used because it differs from the hash on the XOOPS password. | 
|---|
| 209 | *              if ( $check && $user_id ) { | 
|---|
| 210 | *                      // Rehash using new hash. | 
|---|
| 211 | *                      wp_set_password($password, $user_id); | 
|---|
| 212 | *                      $hash = wp_hash_password($password); | 
|---|
| 213 | *              } | 
|---|
| 214 | */ | 
|---|
| 215 | return apply_filters('check_password', $check, $password, $hash, $user_id); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | // If the stored hash is longer than an MD5, presume the | 
|---|
| 219 | // new style phpass portable hash. | 
|---|
| 220 | if ( empty($wp_hasher) ) { | 
|---|
| 221 | require_once( ABSPATH . 'wp-includes/class-phpass.php'); | 
|---|
| 222 | // By default, use the portable hash from phpass | 
|---|
| 223 | $wp_hasher = new PasswordHash(8, TRUE); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | $check = $wp_hasher->CheckPassword($password, $hash); | 
|---|
| 227 |  | 
|---|
| 228 | return apply_filters('check_password', $check, $password, $hash, $user_id); | 
|---|
| 229 | } | 
|---|
| 230 | endif; | 
|---|
| 231 |  | 
|---|
| 232 | if ( !function_exists('wp_redirect') ) : | 
|---|
| 233 | function wp_redirect($location, $status = 302) { | 
|---|
| 234 | global $is_IIS,$xoops_config,$action; | 
|---|
| 235 |  | 
|---|
| 236 | if ($location == 'wp-login.php?loggedout=true') $location = $xoops_config->xoops_url.'/user.php?op=logout'; //xoops logout at wp logout | 
|---|
| 237 | if ($location == 'wp-login.php?action=register') $location = $xoops_config->xoops_url."/register.php";  //wp-register to xoops register | 
|---|
| 238 | if ($action == 'logout') $location = $xoops_config->xoops_url.'/user.php?op=logout'; //xoops logout at comment logout | 
|---|
| 239 |  | 
|---|
| 240 | $location = apply_filters('wp_redirect', $location, $status); | 
|---|
| 241 | $status = apply_filters('wp_redirect_status', $status, $location); | 
|---|
| 242 |  | 
|---|
| 243 | if ( !$location ) // allows the wp_redirect filter to cancel a redirect | 
|---|
| 244 | return false; | 
|---|
| 245 |  | 
|---|
| 246 | $location = wp_sanitize_redirect($location); | 
|---|
| 247 |  | 
|---|
| 248 | if (!headers_sent()) { | 
|---|
| 249 | ob_end_clean(); | 
|---|
| 250 | if ( $is_IIS ) { | 
|---|
| 251 | header("Refresh: 0;url=$location"); | 
|---|
| 252 | } else { | 
|---|
| 253 | if ( php_sapi_name() != 'cgi-fcgi' ) | 
|---|
| 254 | status_header($status); // This causes problems on IIS and some FastCGI setups | 
|---|
| 255 | header("Location: $location"); | 
|---|
| 256 | } | 
|---|
| 257 | } else {  // force redirect | 
|---|
| 258 | echo ("<HTML>"); | 
|---|
| 259 | echo("<META http-equiv=\"Refresh\" content=\"0;url=$location\">"); | 
|---|
| 260 | echo ("<BODY onload=\"try {self.location.href='$location' } catch(e) {}\">"); | 
|---|
| 261 | printf(__("If the page does not automatically reload, please click <a href='%s'>here</a>","xpressme"),$location); | 
|---|
| 262 | echo ("</BODY>"); | 
|---|
| 263 | echo ("</HTML>"); | 
|---|
| 264 | } | 
|---|
| 265 | } | 
|---|
| 266 | endif; | 
|---|
| 267 |  | 
|---|
| 268 | if ( !function_exists('wp_hash_password') ) : | 
|---|
| 269 | function wp_hash_password($password) { | 
|---|
| 270 | global $wp_hasher; | 
|---|
| 271 | return md5($password); // A new hash is not used because it differs from the hash on the XOOPS password. | 
|---|
| 272 | /* | 
|---|
| 273 | if ( empty($wp_hasher) ) { | 
|---|
| 274 | require_once( ABSPATH . 'wp-includes/class-phpass.php'); | 
|---|
| 275 | // By default, use the portable hash from phpass | 
|---|
| 276 | $wp_hasher = new PasswordHash(8, TRUE); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | return $wp_hasher->HashPassword($password); | 
|---|
| 280 | */ | 
|---|
| 281 | } | 
|---|
| 282 | endif; | 
|---|
| 283 |  | 
|---|
| 284 | if ( !function_exists('wp_clear_auth_cookie') ) : | 
|---|
| 285 | /** | 
|---|
| 286 | * Removes all of the cookies associated with authentication. | 
|---|
| 287 | * | 
|---|
| 288 | * @since 2.5 | 
|---|
| 289 | */ | 
|---|
| 290 | function wp_clear_auth_cookie() { | 
|---|
| 291 | do_action('clear_auth_cookie'); | 
|---|
| 292 |  | 
|---|
| 293 | @setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN); | 
|---|
| 294 | @setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN); | 
|---|
| 295 | @setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN); | 
|---|
| 296 | @setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN); | 
|---|
| 297 | @setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 298 | @setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 299 |  | 
|---|
| 300 | // Old cookies | 
|---|
| 301 | @setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 302 | @setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 303 | @setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 304 | @setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 305 |  | 
|---|
| 306 | // Even older cookies | 
|---|
| 307 | @setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 308 | @setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 309 | @setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 310 | @setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN); | 
|---|
| 311 |  | 
|---|
| 312 | } | 
|---|
| 313 | endif; | 
|---|
| 314 |  | 
|---|
| 315 | if ( !function_exists('is_user_logged_in') ) : | 
|---|
| 316 | /** | 
|---|
| 317 | * Checks if the current visitor is a logged in user. | 
|---|
| 318 | * | 
|---|
| 319 | * @since 2.0.0 | 
|---|
| 320 | * | 
|---|
| 321 | * @return bool True if user is logged in, false if not logged in. | 
|---|
| 322 | */ | 
|---|
| 323 | function is_user_logged_in() { | 
|---|
| 324 | $user = wp_get_current_user(); | 
|---|
| 325 | if (method_exists($user, 'exists')) { | 
|---|
| 326 | if ( ! $user->exists() ) return false; | 
|---|
| 327 | } else { | 
|---|
| 328 | if ( empty( $user->ID ) ) return false; | 
|---|
| 329 | } | 
|---|
| 330 | return true; | 
|---|
| 331 | } | 
|---|
| 332 | endif; | 
|---|
| 333 |  | 
|---|
| 334 |  | 
|---|
| 335 | // ***********************************  End Of Pluggable Function Edit (wp-include/pluggable.php) ************************************ | 
|---|
| 336 |  | 
|---|
| 337 | ?> | 
|---|