wordpress登录代码分析

这几天折腾多站点,被登录问题搞得焦头烂额。在网上找了半天,扒了几篇文章。分析用户登录过程:
单个站点容易一点,如果要多站点,并且支持注册,是一个很大的麻烦。
其实WP本身就是写博客合适,并不适合做大规模的交互站点。要实现多用户功能、注册功能、好友这些的,其实不适合用这个。
还发现一个问题,注册功能不能注册到你创建的一个子站点下面,而是和子站点在等级上是平行的。在这个上面走了弯路。
  1. //form提交表单 提交给了http://www.febbird.net/wp-login.php   
  2. //form提交表单 中 :   
  3. //参数:user_login //用户登陆名   
  4. //user_pass 密码   
  5. //一个隐藏的input名为redirect_to 参数为http://www.febbird.net/wp-admin/ 跳转地址———— -wp - login.php分析—————————————————- -   
  6. //验证$action是否包含‘logout’, ’lostpassword’, ’retrievepassword’, ’resetpass’, ’rp’, ’register’, ’login’,以默认为$action = ’login’; 登录屏幕 行350   
  7.   
  8. if ( !in_array($actionarray(‘logout’, ’lostpassword’, ’retrievepassword’, ’resetpass’, ’rp’, ’register’, ’login’), true) && false == = has_filter(‘login_form_’ . $action) )   
  9.     $action = ’login’;   
  10.   
  11. //判断form 中method值是否等于post 行384   
  12. $http_post = (‘POST’ == $_SERVER['REQUEST_METHOD']);   
  13. switch ($action)   
  14. {   
  15.   
  16.     //当$action=login 和 默认为登陆 行556   
  17. case ’login’ :   
  18. default:   
  19.     $secure_cookie = ;   
  20.     $interim_login = isset($_REQUEST['interim-login']);   
  21.   
  22.     //如果用户希望SSL,但会议没有SSL,强制安全cookie。   
  23.     if ( !emptyempty($_POST['log']) && !force_ssl_admin() )   
  24.     {   
  25.   
  26.         //获取用户名   
  27.         $user_name = sanitize_user($_POST['log']);   
  28.   
  29.         //检索用户信息的登录名。并返回用户对象   
  30.         if ( $user = get_userdatabylogin($user_name) )   
  31.         {   
  32.   
  33.             //get_user_option()检索用户选项,包括global、user或blog。   
  34.             if ( get_user_option(‘use_ssl’, $user->ID) )   
  35.             {   
  36.                 $secure_cookie = true;   
  37.                 force_ssl_admin(true);   
  38.             }   
  39.         }   
  40.     }   
  41.   
  42.     //判断重定向地址是否为空 redirect_to值为http://www.febbird.net/wp-admin/   
  43.     if ( isset( $_REQUEST['redirect_to'] ) )   
  44.     {   
  45.         $redirect_to = $_REQUEST['redirect_to'];   
  46.   
  47.         // Redirect to https if user wants ssl   
  48.         // 重定向到HTTPS如果用户想SSL   
  49.         if ( $secure_cookie && false != = strpos($redirect_to, ’wp-admin’) )   
  50.             $redirect_to = preg_replace(‘|^http://|’, ’https://’, $redirect_to);   
  51.     }   
  52.     else  
  53.     {   
  54.         //admin_url() 返回 管理员后台地址 http://www.febbird.net/wp-admin/   
  55.         $redirect_to = admin_url();   
  56.     }   
  57.     //再次认证 $_REQUEST['reauth'] 为空 $reauth值为false   
  58.     $reauth = emptyempty($_REQUEST['reauth']) ? false : true;   
  59.   
  60.     if ( !$secure_cookie && is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 != = strpos($redirect_to, ’https’) ) && ( 0 == = strpos($redirect_to, ’http’) ) )   
  61.         $secure_cookie = false;   
  62.     //wp_signon授权给用户并可记住该用户名称 $secure_cookie为空   
  63.     $user = wp_signon($secure_cookie);   
  64.   
  65.     //返回 $redirect_to= http://www.febbird.net/wp-admin/   
  66.     $redirect_to = apply_filters(‘login_redirect’, $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : $user);   
  67.   
  68.     // 判断$user是否有错误   
  69.     // $user值为object(WP_Error)#3813 (2) { ["errors"]=> array(0) { } ["error_data"]=> array(0) { } }   
  70.     // $user为空   
  71.     // $reauth值为false   
  72.     if ( !is_wp_error($user) && !$reauth )   
  73.     {   
  74.         if ( $interim_login )   
  75.         {   
  76.             $message = ’<p>’ . __(‘You have logged in successfully.’) . ’</p>’;   
  77.             login_header( $message );   
  78.             ? >   
  79.             < script type = “text/javascript” > setTimeout( function()   
  80.             {   
  81.                 window.close()   
  82.             }, 8000);   
  83.             < / script >   
  84.             < p class = “alignright” >   
  85.                             < input type = “button” class = “button-primary” value = “<?php esc_attr_e(‘Close’); ?>” onclick = “window.close()” / > < / p >   
  86.                                                < / div > < / body > < / html >   
  87.                                                    <? php exit;   
  88.         }   
  89.         //判断 $redirect_to是否为空   
  90.         if ( ( emptyempty$redirect_to ) || $redirect_to == ’wp-admin/’ || $redirect_to == admin_url() ) )   
  91.     {   
  92.   
  93.             // 如果用户不属于一个博客,将它们发送到用户管理员。如果用户不能编辑岗位,送他们到他们的个人资料。   
  94.             // is_multisite()判断是否启用了多站点   
  95.             // get_active_blog_for_user 为用户获取活动的博客-可以是主博客或博客列表   
  96.             if ( is_multisite() && !get_active_blog_for_user($user->id) )   
  97.                 $redirect_to = user_admin_url();   
  98.             elseif ( is_multisite() && !$user->has_cap(‘read’) )   
  99.             $redirect_to = get_dashboard_url( $user->id );   
  100.             elseif ( !$user->has_cap(‘edit_posts’) )   
  101.             $redirect_to = admin_url(‘profile.php’);   
  102.         }   
  103.         //重定向到其它URL的安全方式。保证结果HTTP定位信息头的合法性   
  104.         wp_safe_redirect($redirect_to);   
  105.         exit();   
  106.     }   
  107. }   
  108. }   
  109. }   
  110.   
  111. //判断重定向地址是否为空 redirect_to值为http://www.febbird.net/wp-admin/ 行573   
  112.   
  113. if ( isset( $_REQUEST['redirect_to'] ) )   
  114. {   
  115.     $redirect_to = $_REQUEST['redirect_to'];   
  116.   
  117.     //重定向到HTTPS如果用户想SSL   
  118.     if ( $secure_cookie && false != = strpos($redirect_to, ’wp-admin’) )   
  119.         $redirect_to = preg_replace(‘|^http://|’, ’https://’, $redirect_to);   
  120. }   
  121. else  
  122. {   
  123.   
  124.     //admin_url() 返回 管理员后台地址 http://www.febbird.net/wp-admin/   
  125.     $redirect_to = admin_url();   
  126. }   
  127.   
  128. //判断重定向地址是否为空 redirect_to值为http://www.febbird.net/wp-admin/ 行577   
  129. if ( isset( $_REQUEST['redirect_to'] ) )   
  130. {   
  131.     $redirect_to = $_REQUEST['redirect_to'];   
  132.   
  133.     // 重定向到HTTPS如果用户想SSL   
  134.     if ( $secure_cookie && false != = strpos($redirect_to, ’wp-admin’) )   
  135.         $redirect_to = preg_replace(‘|^http://|’, ’https://’, $redirect_to);   
  136. }   
  137. else  
  138. {   
  139.   
  140.     //admin_url() 返回 管理员后台地址 http://www.febbird.net/wp-admin/   
  141.     $redirect_to = admin_url();   
  142. }   
  143.   
  144. //再次认证 $_REQUEST['reauth'] 为空 $reauth值为false   
  145. $reauth = emptyempty($_REQUEST['reauth']) ? false : true;   
  146.   
  147.           //判断 $redirect_to是否为空 行617   
  148.           if ( ( emptyempty$redirect_to ) || $redirect_to == ’wp-admin/’ || $redirect_to == admin_url() ) )   
  149. {   
  150.   
  151.     // is_multisite()判断是否启用了多站点   
  152.     // get_active_blog_for_user 为用户获取活动的博客-可以是主博客或博客列表   
  153.     if ( is_multisite() && !get_active_blog_for_user($user->id) )   
  154.         $redirect_to = user_admin_url();   
  155.     elseif ( is_multisite() && !$user->has_cap(‘read’) )   
  156.     $redirect_to = get_dashboard_url( $user->id );   
  157.     elseif ( !$user->has_cap(‘edit_posts’) )   
  158.     $redirect_to = admin_url(‘profile.php’);   
  159. }   
  160.   
  161. //重定向到其它URL的安全方式。保证结果HTTP定位信息头的合法性   
  162. wp_safe_redirect($redirect_to);   
  163. exit();   
  164. }   
  165.   
  166. ———— -get_userdatabylogin()函数定义 在 wp - includes / pluggable.php 行214 ——   
  167.   
  168. //$user_login用户信息的登录名。并返回用户对象   
  169. function get_userdatabylogin($user_login)   
  170. {   
  171.   
  172.     return get_user_by(‘login’, $user_login);//定义get_user_by() 在 wp-includes/pluggable.php 行169   
  173. }   
  174.   
  175. //检索一个特定领域的用户   
  176. //定义get_user_by() 在 wp-includes/pluggable.php 行169   
  177. //$field=login 条件 $value=$user_login 登陆名   
  178. function get_user_by($field$value)   
  179. {   
  180.     global $wpdb;   
  181.     switch ($field)   
  182.     {   
  183.     case ’id’:   
  184.         //get_userdata()返回的对象中含有用户相关信息,用户编号被传递   
  185.         return get_userdata($value);   
  186.         break;   
  187.     case ’slug’:   
  188.         $user_id = wp_cache_get($value, ’userslugs’);   
  189.         $field = ’user_nicename’;   
  190.         break;   
  191.     case ’email’:   
  192.         $user_id = wp_cache_get($value, ’useremail’);   
  193.         $field = ’user_email’;   
  194.         break;   
  195.         //登陆   
  196.     case ’login’:   
  197.         //sanitize_user 消除用户名中的不安全字符   
  198.         $value = sanitize_user( $value );   
  199.         // wp_cache_get()如果缓存对象未过期,该函数返回缓存对象的值。否则返回false。   
  200.         //在 wp-includes/cache.php 行90   
  201.         $user_id = wp_cache_get($value, ’userlogins’);   
  202.         $field = ’user_login’;   
  203.         break;   
  204.     default:   
  205.         return false;   
  206.     }   
  207.   
  208.     if ( false != = $user_id )   
  209.         return get_userdata($user_id);   
  210.   
  211.     if ( !$user = $wpdb->get_row( $wpdb->prepare(“SELECT * FROM $wpdb->users WHERE $field = %s”$value) ) )   
  212.         return false;   
  213.   
  214.     _fill_user($user);   
  215.   
  216.     return $user;   
  217. }   
  218.   
  219. // wp_cache_get()如果缓存对象未过期,该函数返回缓存对象的值。否则返回false。   
  220. //在 wp-includes/cache.php 缓存类 行90   
  221. function wp_cache_get($id$flag = )   
  222. {   
  223.     global $wp_object_cache;   
  224.     //get() 在wp-includes/cache.php 行347   
  225.     return $wp_object_cache->get($id$flag);   
  226. }   
  227.   
  228. //取得存在的缓存数据 ,根据id的分组搜索数据,如果数据存在,返回数据   
  229.   
  230. function get($id$group = ’default‘)   
  231. {   
  232.     if ( emptyempty ($group) )   
  233.         $group = ’default‘;   
  234.   
  235.     if ( isset ($this->cache[$group][$id]) )   
  236.     {   
  237.         $this->cache_hits += 1;   
  238.         if ( is_object($this->cache[$group][$id]) )   
  239.             return wp_clone($this->cache[$group][$id]);   
  240.         else  
  241.             return $this->cache[$group][$id];   
  242.     }   
  243.   
  244.     if ( isset ($this->non_existent_objects[$group][$id]) )   
  245.         return false;   
  246.   
  247.     $this->non_existent_objects[$group][$id] = true;   
  248.     $this->cache_misses += 1;   
  249.     return false;   
  250. }   
  251.   
  252. ——————– -get_user_option()在wp - includes / user.php 行250——————– -   
  253.   
  254.                                       // 检索用户选项,包括global、user或blog。   
  255.                                       // $option 用户选项名称 $user 用户id   
  256.                                       // $deprecated 如果之前的用户选项不存在,是否需要在选项表中查找一个选项   
  257.                                       function get_user_option( $option$user = 0, $deprecated =  )   
  258. {   
  259.     global $wpdb;   
  260.     //判断deprecated是否为空   
  261.     if ( !emptyempty$deprecated ) )   
  262.         _deprecated_argument( __FUNCTION__, ’3.0′ );   
  263.     //判断$user对象是否为空,为空是执行以下代码   
  264.     if ( emptyempty($user) )   
  265.     {   
  266.         //在数据库中更新(或创建)用户   
  267.         $user = wp_get_current_user();   
  268.         //返回用户id   
  269.         $user = $user->ID;   
  270.     }   
  271.     //返回的对象中含有用户相关信息,$user用户id   
  272.     $user = get_userdata($user);   
  273.   
  274.     // $key 中不能有-   
  275.     $key = str_replace(‘-’, $option);   
  276.   
  277.     if ( isset( $user-> {$wpdb->prefix . $key} ) ) // Blog specific   
  278.         $result = $user-> {$wpdb->prefix . $key};   
  279.     elseif ( isset( $user-> {$key} ) ) // User specific and cross-blog   
  280.     $result = $user-> {$key};   
  281.     else  
  282.         $result = false;   
  283.     return apply_filters(“get_user_option_{$option}”$result$option$user);   
  284. }   
  285.   
  286. //验证用户使用用户名和密码。 wp-includes/user.php 行 73   
  287. add_filter(‘authenticate’, ’wp_authenticate_username_password’, 20, 3);   
  288.   
  289. //对用户密码相对应的验证如执行对空用户名和密码的检查。 wp-includes/user.php 行 75   
  290. // $user 用户对象 $username 用户名 $password 密码   
  291. function wp_authenticate_username_password($user$username$password)   
  292. {   
  293.   
  294.     //判断$用户对象是否存在,如果存在返回   
  295.     if ( is_a($user, ’WP_User’) )   
  296.     {   
  297.         return $user;   
  298.     }   
  299.   
  300.     //判断用户或密码为空执行   
  301.     if ( emptyempty($username) || emptyempty($password) )   
  302.     {   
  303.         $error = new WP_Error();   
  304.         //判断用户名为空   
  305.         if ( emptyempty($username) )   
  306.             $error->add(‘empty_username’, __(‘<strong>ERROR</strong>: The username field is emptyempty.’));   
  307.         //判断密码为空   
  308.         if ( emptyempty($password) )   
  309.             $error->add(‘empty_password’, __(‘<strong>ERROR</strong>: The password field is emptyempty.’));   
  310.   
  311.         return $error;   
  312.     }   
  313.     $userdata = get_user_by(‘login’, $username);   
  314.     //验证用户名是否正确,如果用户名不正确执行   
  315.     if ( !$userdata )   
  316.         return new WP_Error(‘invalid_username’, sprintf(__(‘<strong>ERROR</strong>: Invalid username. <a href=“%s” title=“Password Lost and Found”>Lost your password</a>?’), site_url(‘wp-login.php?action=lostpassword’, ’login’)));   
  317.     //判断是不是多站点   
  318.     if ( is_multisite() )   
  319.     {   
  320.         //spam= 1 时,提示 您的账户已被标记为垃圾账户。   
  321.         if ( 1 == $userdata->spam)   
  322.             return new WP_Error(‘invalid_username’, __(‘<strong>ERROR</strong>: Your account has been marked as a spammer.’));   
  323.   
  324.         //标识是不是垃圾博文   
  325.         if ( !is_super_admin( $userdata->ID ) && isset($userdata->primary_blog) )   
  326.         {   
  327.             $details = get_blog_details( $userdata->primary_blog );   
  328.             if ( is_object$details ) && $details->spam == 1 )   
  329.                 return new WP_Error(‘blog_suspended’, __(‘Site Suspended.’));   
  330.         }   
  331.     }   
  332.   
  333.     $userdata = apply_filters(‘wp_authenticate_user’, $userdata$password);   
  334.     if ( is_wp_error($userdata) )   
  335.         return $userdata;   
  336.     //验证密码是否正确,如果密码不正确执行   
  337.     if ( !wp_check_password($password$userdata->user_pass, $userdata->ID) )   
  338.         return new WP_Error( ’incorrect_password’, sprintf( __( ’<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href=“%2$s” title=“Password Lost and Found”>Lost your password</a>?’ ),   
  339.                              $username, site_url( ’wp-login.php?action=lostpassword’, ’login’ ) ) );   
  340.     //wp-includes/capabilities.php 创建WP_User类行363   
  341.     //根据 用户id 实例化用户对象 并返回用户数据   
  342.     $user = new WP_User($userdata->ID);   
  343.   
  344.     return $user;   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值