phpbb3源码分析-/includes/ucp/ucp_register.php(v0.5) - 拳不离手、曲不离口 - Speak with your code, my friend, not your word.
phpbb3源码分析-/includes/ucp/ucp_register.php(v0.5)
clem
posted @ 2010年3月09日 01:37
in Language
, 9749 阅读
这个页面负责用户的register事宜。
<?php /** * * @package ucp * @version $Id: ucp_register.php 10095 2009-09-03 20:06:57Z Kellanved $ * @copyright (c) 2005 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ /** * @ignore */ if (!defined('IN_PHPBB')) { exit; } /** * ucp_registeruuyj * Board registration * @package ucp */ class ucp_register { var $u_action; function main($id, $mode) { global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx; // if ($config['require_activation'] == USER_ACTIVATION_DISABLE) //判断配置文件里是否配置成“屏蔽用户注册”, { //如果屏蔽,则显示论坛目前无法注册。 trigger_error('UCP_REGISTER_DISABLE'); } include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); //把$phpbb3_dir/includes/functions_profile.php用作头文件。 $coppa = (isset($_REQUEST['coppa'])) ? ((!empty($_REQUEST['coppa'])) ? 1 : 0) : false; //coppa是美国针对未成年人上网所设定的一种规定 $agreed = (!empty($_POST['agreed'])) ? 1 : 0; $submit = (isset($_POST['submit'])) ? true : false; $change_lang = request_var('change_lang', ''); $user_lang = request_var('lang', $user->lang_name); if ($agreed) { add_form_key('ucp_register'); } else { add_form_key('ucp_register_terms'); } if ($change_lang || $user_lang != $config['default_lang']) { $use_lang = ($change_lang) ? basename($change_lang) : basename($user_lang); if (file_exists($user->lang_path . $use_lang . '/')) { if ($change_lang) { $submit = false; // Setting back agreed to let the user view the agreement in his/her language $agreed = (empty($_GET['change_lang'])) ? 0 : $agreed; } $user->lang_name = $user_lang = $use_lang; $user->lang = array(); $user->data['user_lang'] = $user->lang_name; $user->add_lang(array('common', 'ucp')); } else { $change_lang = ''; $user_lang = $user->lang_name; } } $cp = new custom_profile(); //注册时用户需要填入的自定义信息(bb没有规定的,但是bb的使用者需要从用户处获得的信息) $error = $cp_data = $cp_error = array(); //$error是用来装用户在注册过程中引起的输入错误,比如非法注册信息、错误验正码等) if (!$agreed || ($coppa === false && $config['coppa_enable']) || ($coppa && !$config['coppa_enable']))//先不去看coppa这个概念,一般的论坛$coppa都是false。那么这个判断就是用来有没有agree过,没有就刷agreement页面,有就不刷agreement页面。 { $add_lang = ($change_lang) ? '&change_lang=' . urlencode($change_lang) : ''; $add_coppa = ($coppa !== false) ? '&coppa=' . $coppa : ''; $s_hidden_fields = array( 'change_lang' => $change_lang, ); // If we change the language, we want to pass on some more possible parameter. if ($change_lang) //如果用户在agreement页面换了语言,那么他之前可能会填了一些注册内容在cookie里,也把它们记录下来。 { // We do not include the password $s_hidden_fields = array_merge($s_hidden_fields, array( 'username' => utf8_normalize_nfc(request_var('username', '', true)), 'email' => strtolower(request_var('email', '')), 'email_confirm' => strtolower(request_var('email_confirm', '')), 'lang' => $user->lang_name, 'tz' => request_var('tz', (float) $config['board_timezone']), )); } // Checking amount of available languages //看看bb已经装了几种语言,如果超过一种就需要languague_select。 $sql = 'SELECT lang_id FROM ' . LANG_TABLE; $result = $db->sql_query($sql); $lang_row = array(); while ($row = $db->sql_fetchrow($result)) { $lang_row[] = $row; } $db->sql_freeresult($result); //释放$result占的内存 if ($coppa === false && $config['coppa_enable']) { $now = getdate(); $coppa_birthday = $user->format_date(mktime($now['hours'] + $user->data['user_dst'], $now['minutes'], $now['seconds'], $now['mon'], $now['mday'] - 1, $now['year'] - 13), $user->lang['DATE_FORMAT']); unset($now); $template->assign_vars(array( 'S_LANG_OPTIONS' => (sizeof($lang_row) > 1) ? language_select($user_lang) : '', 'L_COPPA_NO' => sprintf($user->lang['UCP_COPPA_BEFORE'], $coppa_birthday), 'L_COPPA_YES' => sprintf($user->lang['UCP_COPPA_ON_AFTER'], $coppa_birthday), 'U_COPPA_NO' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register&coppa=0' . $add_lang), 'U_COPPA_YES' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register&coppa=1' . $add_lang), 'S_SHOW_COPPA' => true, 'S_HIDDEN_FIELDS' => build_hidden_fields($s_hidden_fields), 'S_UCP_ACTION' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register' . $add_lang), )); } else //不管coppa的话,执行这个。 { $template->assign_vars(array( 'S_LANG_OPTIONS' => (sizeof($lang_row) > 1) ? language_select($user_lang) : '', 'L_TERMS_OF_USE' => sprintf($user->lang['TERMS_OF_USE_CONTENT'], $config['sitename'], generate_board_url()), 'S_SHOW_COPPA' => false, 'S_REGISTRATION' => true, 'S_HIDDEN_FIELDS' => build_hidden_fields($s_hidden_fields), 'S_UCP_ACTION' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register' . $add_lang . $add_coppa), ) ); //把agreement内容以及其它信息填入template,构造它。 } unset($lang_row); //释放变量。 $this->tpl_name = 'ucp_agreement'; return; //终止本函数,返回上级程序。 } //以上代码负责显示注册模块的agreement,bb中显示网页用到了模板,同时也用到了模块,模块比模板更高级,比如说,agreement就像是一个模板,而ucp_register就像是一个模块,一个模块中有若干个模板。用模块封装模板的这种技术可以更接近于项目设计。 // The CAPTCHA kicks in here. We can't help that the information gets lost on language change. if ($config['enable_confirm']) //注册信息页面的验证码生成。 { include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); $captcha =& phpbb_captcha_factory::get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_REG); } // Try to manually determine the timezone and adjust the dst if the server date/time complies with the default setting +/- 1 $timezone = date('Z') / 3600; //取时差偏移,中国为8 $is_dst = date('I'); //是否为夏令时 if ($config['board_timezone'] == $timezone || $config['board_timezone'] == ($timezone - 1)) //Host的时差是否和bb设的时差一致。 { $timezone = ($is_dst) ? $timezone - 1 : $timezone; if (!isset($user->lang['tz_zones'][(string) $timezone])) { $timezone = $config['board_timezone']; } } else { $is_dst = $config['board_dst']; //如果不一致,以bb为准。 $timezone = $config['board_timezone']; } $data = array( 'username' => utf8_normalize_nfc(request_var('username', '', true)), 'new_password' => request_var('new_password', '', true), 'password_confirm' => request_var('password_confirm', '', true), 'email' => strtolower(request_var('email', '')), 'email_confirm' => strtolower(request_var('email_confirm', '')), 'lang' => basename(request_var('lang', $user->lang_name)), 'tz' => request_var('tz', (float) $timezone), );//将传过来的数据塞入$data // Check and initialize some variables if needed if ($submit) //(1)注册发起页面-〉agreement页面 在上面return的地方就跳走了 (2)agreement页面-〉注册信息填写页面 由于没有submit,所以不会执行这里的程序块。(3)注册信息填写页面-〉回应页面,当然会执行这里。 { $error = validate_data($data, array( //验证注册信息 'username' => array( array('string', false, $config['min_name_chars'], $config['max_name_chars']), array('username', '')), 'new_password' => array( array('string', false, $config['min_pass_chars'], $config['max_pass_chars']), array('password')), 'password_confirm' => array('string', false, $config['min_pass_chars'], $config['max_pass_chars']), 'email' => array( array('string', false, 6, 60), array('email')), 'email_confirm' => array('string', false, 6, 60), 'tz' => array('num', false, -14, 14), 'lang' => array('match', false, '#^[a-z_\-]{2,}$#i'), )); if (!check_form_key('ucp_register')) //检查一下本次注册是否通过非法途径?? { $error[] = $user->lang['FORM_INVALID']; } // Replace "error" strings with their real, localised form (TODO) $error = preg_replace('#^([A-Z_]+)$#e', "(!empty(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '\\1'", $error); if ($config['enable_confirm']) //对验证码的验证 { $vc_response = $captcha->validate($data); if ($vc_response !== false) { $error[] = $vc_response; } if ($config['max_reg_attempts'] && $captcha->get_attempt_count() > $config['max_reg_attempts']) { $error[] = $user->lang['TOO_MANY_REGISTERS']; } } // DNSBL check //验证该用户是否已被列入了黑名单 if ($config['check_dnsbl']) { if (($dnsbl = $user->check_dnsbl('register')) !== false) { $error[] = sprintf($user->lang['IP_BLACKLISTED'], $user->ip, $dnsbl[1]); } } // validate custom profile fields //验证自定义注册信息 $cp->submit_cp_field('register', $user->get_iso_lang_id(), $cp_data, $error); if (!sizeof($error)) //看验证过程中是否有错误,没错误则进行重复输入内容验证 { if ($data['new_password'] != $data['password_confirm']) { $error[] = $user->lang['NEW_PASSWORD_ERROR']; } if ($data['email'] != $data['email_confirm']) { $error[] = $user->lang['NEW_EMAIL_ERROR']; } } if (!sizeof($error)) //用户信息可用于注册,下面进行真正的注册过程。 { $server_url = generate_board_url(); //生成url // Which group by default? $group_name = ($coppa) ? 'REGISTERED_COPPA' : 'REGISTERED'; $sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = '" . $db->sql_escape($group_name) . "' AND group_type = " . GROUP_SPECIAL; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); $db->sql_freeresult($result); if (!$row) { trigger_error('NO_GROUP'); } $group_id = $row['group_id']; //以上都是用来取用户的组。 if (($coppa || $config['require_activation'] == USER_ACTIVATION_SELF || $config['require_activation'] == USER_ACTIVATION_ADMIN) && $config['email_enable']) //用户注册是否需要认证,有两种认证方式,自己认证,管理员认证(TODO:区别) { $user_actkey = gen_rand_string(10); $key_len = 54 - (strlen($server_url)); $key_len = ($key_len < 6) ? 6 : $key_len; $user_actkey = substr($user_actkey, 0, $key_len); $user_type = USER_INACTIVE; $user_inactive_reason = INACTIVE_REGISTER; $user_inactive_time = time(); } else //如果不需要,定义一些值 { $user_type = USER_NORMAL; $user_actkey = ''; $user_inactive_reason = 0; $user_inactive_time = 0; } $user_row = array( 'username' => $data['username'], 'user_password' => phpbb_hash($data['new_password']), 'user_email' => $data['email'], 'group_id' => (int) $group_id, 'user_timezone' => (float) $data['tz'], 'user_dst' => $is_dst, 'user_lang' => $data['lang'], 'user_type' => $user_type, 'user_actkey' => $user_actkey, 'user_ip' => $user->ip, 'user_regdate' => time(), 'user_inactive_reason' => $user_inactive_reason, 'user_inactive_time' => $user_inactive_time, ); //构造用户注册信息数组。 if ($config['new_member_post_limit']) //新用户是否有发帖限制。 { $user_row['user_new'] = 1; } // Register user... $user_id = user_add($user_row, $cp_data); //准备完毕,添加用户。 // This should not happen, because the required variables are listed above... if ($user_id === false) //看看添加是否成功 { trigger_error('NO_USER', E_USER_ERROR); } if ($coppa && $config['email_enable']) //如果添加成功,根据不同的注册认证方式,刷出欢迎页面。 { $message = $user->lang['ACCOUNT_COPPA']; $email_template = 'coppa_welcome_inactive'; } else if ($config['require_activation'] == USER_ACTIVATION_SELF && $config['email_enable']) { $message = $user->lang['ACCOUNT_INACTIVE']; $email_template = 'user_welcome_inactive'; } else if ($config['require_activation'] == USER_ACTIVATION_ADMIN && $config['email_enable']) { $message = $user->lang['ACCOUNT_INACTIVE_ADMIN']; $email_template = 'admin_welcome_inactive'; } else { $message = $user->lang['ACCOUNT_ADDED']; $email_template = 'user_welcome'; } if ($config['email_enable']) //是否注册需要邮件验证 { include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); $messenger = new messenger(false); $messenger->template($email_template, $data['lang']); $messenger->to($data['email'], $data['username']); $messenger->headers('X-AntiAbuse: Board servername - ' . $config['server_name']); $messenger->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']); $messenger->headers('X-AntiAbuse: Username - ' . $user->data['username']); $messenger->headers('X-AntiAbuse: User IP - ' . $user->ip); $messenger->assign_vars(array( 'WELCOME_MSG' => htmlspecialchars_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename'])), 'USERNAME' => htmlspecialchars_decode($data['username']), 'PASSWORD' => htmlspecialchars_decode($data['new_password']), 'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey") ); if ($coppa) { $messenger->assign_vars(array( 'FAX_INFO' => $config['coppa_fax'], 'MAIL_INFO' => $config['coppa_mail'], 'EMAIL_ADDRESS' => $data['email']) ); } $messenger->send(NOTIFY_EMAIL); if ($config['require_activation'] == USER_ACTIVATION_ADMIN) { // Grab an array of user_id's with a_user permissions ... these users can activate a user $admin_ary = $auth->acl_get_list(false, 'a_user', false); $admin_ary = (!empty($admin_ary[0]['a_user'])) ? $admin_ary[0]['a_user'] : array(); // Also include founders $where_sql = ' WHERE user_type = ' . USER_FOUNDER; if (sizeof($admin_ary)) { $where_sql .= ' OR ' . $db->sql_in_set('user_id', $admin_ary); } $sql = 'SELECT user_id, username, user_email, user_lang, user_jabber, user_notify_type FROM ' . USERS_TABLE . ' ' . $where_sql; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $messenger->template('admin_activate', $row['user_lang']); $messenger->to($row['user_email'], $row['username']); $messenger->im($row['user_jabber'], $row['username']); $messenger->assign_vars(array( 'USERNAME' => htmlspecialchars_decode($data['username']), 'U_USER_DETAILS' => "$server_url/memberlist.$phpEx?mode=viewprofile&u=$user_id", 'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey") ); $messenger->send($row['user_notify_type']); } $db->sql_freeresult($result); } } $message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>'); trigger_error($message); //注册信息填写页面-〉回应页面,将会在此触发消息事件,从而跳走。 } } //下面的内容基本上只和“agreement页面”到“注册信息填写页面”之间的转换有关,那么我们来看这次转换需要做一些什么事呢? //为显示“注册信息填写页面”做准备对不对,显示 是一个函数display(),要显示一张页面需要什么?至少需要知道页面的路径,当然还有一些其它的参数,这些东西都是在下面形成的。 $s_hidden_fields = array( 'agreed' => 'true', 'change_lang' => 0, ); if ($config['coppa_enable']) { $s_hidden_fields['coppa'] = $coppa; } if ($config['enable_confirm']) { $s_hidden_fields = array_merge($s_hidden_fields, $captcha->get_hidden_fields()); } $s_hidden_fields = build_hidden_fields($s_hidden_fields); $confirm_image = ''; //TODO:莫名其妙的东西,完全脱离上下文 // Visual Confirmation - Show images if ($config['enable_confirm']) { $template->assign_vars(array( 'CAPTCHA_TEMPLATE' => $captcha->get_template(), )); } // $l_reg_cond = ''; switch ($config['require_activation']) { case USER_ACTIVATION_SELF: $l_reg_cond = $user->lang['UCP_EMAIL_ACTIVATE']; break; case USER_ACTIVATION_ADMIN: $l_reg_cond = $user->lang['UCP_ADMIN_ACTIVATE']; break; } //以上的东西就是在构造3个变量,$s_hidden_fields,$confirm_image,$l_reg_cond,以及构造$template中的一个member。其实前面构造的3个变量除了第2个比较诡异之外,其余都是为了下面构造$template进行的热身。display函数需要的参数主要就是这个template,所以这边需要去构造它。 $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('<br />', $error) : '', 'USERNAME' => $data['username'], 'PASSWORD' => $data['new_password'], 'PASSWORD_CONFIRM' => $data['password_confirm'], 'EMAIL' => $data['email'], 'EMAIL_CONFIRM' => $data['email_confirm'], 'L_REG_COND' => $l_reg_cond, 'L_USERNAME_EXPLAIN' => sprintf($user->lang[$config['allow_name_chars'] . '_EXPLAIN'], $config['min_name_chars'], $config['max_name_chars']), 'L_PASSWORD_EXPLAIN' => sprintf($user->lang[$config['pass_complex'] . '_EXPLAIN'], $config['min_pass_chars'], $config['max_pass_chars']), 'S_LANG_OPTIONS' => language_select($data['lang']), 'S_TZ_OPTIONS' => tz_select($data['tz']), 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_REGISTRATION' => true, 'S_COPPA' => $coppa, 'S_HIDDEN_FIELDS' => $s_hidden_fields, 'S_UCP_ACTION' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register'), )); //TODO:构造自定义的注册所需填写内容,如何用display显示呢? $user->profile_fields = array(); // Generate profile fields -> Template Block Variable profile_fields $cp->generate_profile_fields('register', $user->get_iso_lang_id()); $this->tpl_name = 'ucp_register'; //指出要显示的module的模板文件位置,也就是某某文件夹下的ucp_register.html文件。 $this->page_title = 'UCP_REGISTRATION'; //TODO:display这个module不需要page_title,但不知道为什么这里也会去指定值,诡异。 } } ?>