Make WordPress Core


Ignore:
Timestamp:
05/11/2006 11:05:45 PM (21 years ago)
Author:
ryan
Message:

Backport nonces and pluggable cookies.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0/wp-includes/pluggable-functions.php

    r3739 r3771  
    66if ( !function_exists('set_current_user') ) :
    77function set_current_user($id, $name = '') {
    8         global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity, $current_user;
    9 
    10         $current_user   = '';
    11 
    12         $current_user   = new WP_User($id, $name);
    13 
    14         $userdata       = get_userdatabylogin($user_login);
    15 
    16         $user_login     = $userdata->user_login;
    17         $user_level     = $userdata->user_level;
    18         $user_ID        = $userdata->ID;
    19         $user_email     = $userdata->user_email;
    20         $user_url       = $userdata->user_url;
    21         $user_pass_md5  = md5($userdata->user_pass);
    22         $user_identity  = $userdata->display_name;
     8        return wp_set_current_user($id, $name);
     9}
     10endif;
     11
     12if ( !function_exists('wp_set_current_user') ) :
     13function wp_set_current_user($id, $name = '') {
     14        global $current_user;
     15
     16        if ( isset($current_user) && ($id == $current_user->ID) )
     17                return $current_user;
     18
     19        $current_user = new WP_User($id, $name);
     20
     21        setup_userdata($current_user->ID);
    2322
    2423        do_action('set_current_user');
    … …  
    2827endif;
    2928
     29if ( !function_exists('current_user') ) :
     30function wp_get_current_user() {
     31        global $current_user;
     32
     33        get_currentuserinfo();
     34
     35        return $current_user;
     36}
     37endif;
    3038
    3139if ( !function_exists('get_currentuserinfo') ) :
    3240function get_currentuserinfo() {
    33         global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity, $current_user;
     41        global $current_user;
    3442
    3543        if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
    3644                return false;
     45
     46        if ( ! empty($current_user) )
     47                return;
    3748
    3849        if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) ||
    3950                !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true) ) {
    40                 $current_user = new WP_User(0);
    41                 return false;
    42         }
    43         $user_login  = $_COOKIE[USER_COOKIE];
    44         $userdata    = get_userdatabylogin($user_login);
    45         $user_level  = $userdata->user_level;
    46         $user_ID     = $userdata->ID;
    47         $user_email  = $userdata->user_email;
    48         $user_url    = $userdata->user_url;
    49         $user_pass_md5 = md5($userdata->user_pass);
    50         $user_identity = $userdata->display_name;
    51 
    52         if ( empty($current_user) )
    53                 $current_user = new WP_User($user_ID);
     51                wp_set_current_user(0);
     52                return false;
     53        }
     54
     55        $user_login = $_COOKIE[USER_COOKIE];
     56        wp_set_current_user(0, $user_login);
    5457}
    5558endif;
    … …  
    202205if ( !function_exists('is_user_logged_in') ) :
    203206function is_user_logged_in() {
    204         global $current_user;
    205        
    206         if ( $current_user->id == 0 )
    207                 return false;
     207        $user = wp_get_current_user();
     208       
     209        if ( $user->id == 0 )
     210                return false;
     211
    208212        return true;
    209213}
    … …  
    225229
    226230if ( !function_exists('check_admin_referer') ) :
    227 function check_admin_referer() {
     231function check_admin_referer($action = -1) {
     232        global $pagenow;
    228233        $adminurl = strtolower(get_settings('siteurl')).'/wp-admin';
    229234        $referer = strtolower($_SERVER['HTTP_REFERER']);
    230         if (!strstr($referer, $adminurl))
    231                 die(__('Sorry, you need to <a href="http://codex.wordpress.org/Enable_Sending_Referrers">enable sending referrers</a> for this feature to work.'));
     235        if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) ) {
     236                $html  = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'>\n\n";
     237                $html .= "<head>\n\t<title>" . __('WordPress Confirmation') . "</title>\n";
     238                $html .= "</head>\n<body>\n";
     239                if ( $_POST ) {
     240                        $q = http_build_query($_POST);
     241                        $q = explode( ini_get('arg_separator.output'), $q);
     242                        $html .= "\t<form method='post' action='$pagenow'>\n";
     243                        foreach ( (array) $q as $a ) {
     244                                $v = substr(strstr($a, '='), 1);
     245                                $k = substr($a, 0, -(strlen($v)+1));
     246                                $html .= "\t\t<input type='hidden' name='" . wp_specialchars( urldecode($k), 1 ) . "' value='" . wp_specialchars( urldecode($v), 1 ) . "' />\n";
     247                        }
     248                        $html .= "\t\t<input type='hidden' name='_wpnonce' value='" . wp_create_nonce($action) . "' />\n";
     249                        $html .= "\t\t<p>" . __('Are you sure you want to do this?') . "</p>\n\t\t<p><a href='$adminurl'>No</a> <input type='submit' value='" . __('Yes') . "' /></p>\n\t</form>\n";
     250                } else {
     251                        $html .= "\t<p>" . __('Are you sure you want to do this?') . "</p>\n\t\t<p><a href='$adminurl'>No</a> <a href='" . add_query_arg( '_wpnonce', wp_create_nonce($action), $_SERVER['REQUEST_URI'] ) . "'>" . __('Yes') . "</a></p>\n";
     252                }
     253                $html .= "</body>\n</html>";
     254
     255                die($html);
     256        }
    232257        do_action('check_admin_referer');
    233 }
    234 endif;
     258}endif;
    235259
    236260if ( !function_exists('check_ajax_referer') ) :
    … …  
    262286                header("Location: $location");
    263287}
     288endif;
     289
     290if ( !function_exists('wp_get_cookie_login') ):
     291function wp_get_cookie_login() {
     292        if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
     293                return false;
     294
     295        return array('login' => $_COOKIE[USER_COOKIE],  'password' => $_COOKIE[PASS_COOKIE]);
     296}
     297
    264298endif;
    265299
    … …  
    445479endif;
    446480
     481if ( !function_exists('wp_verify_nonce') ) :
     482function wp_verify_nonce($nonce, $action = -1) {
     483        $user = wp_get_current_user();
     484        $uid = $user->id;
     485
     486        $i = ceil(time() / 43200);
     487
     488        //Allow for expanding range, but only do one check if we can
     489        if( substr(md5($i . DB_PASSWORD . $action . $uid), -12, 10) == $nonce || substr(md5(($i - 1) . DB_PASSWORD . $action . $uid), -12, 10) == $nonce )
     490                return true;
     491        return false;
     492}
     493endif;
     494
     495if ( !function_exists('wp_create_nonce') ) :
     496function wp_create_nonce($action = -1) {
     497        $user = wp_get_current_user();
     498        $uid = $user->id;
     499
     500        $i = ceil(time() / 43200);
     501       
     502        return substr(md5($i . DB_PASSWORD . $action . $uid), -12, 10);
     503}
     504endif;
     505
    447506?>
Note: See TracChangeset for help on using the changeset viewer.