phpbb3源码分析-/ucp.php(v0.06) - 拳不离手、曲不离口 - Speak with your code, my friend, not your word.
phpbb3源码分析-/ucp.php(v0.06)
clem
posted @ 2010年4月25日 13:39
in Language
, 9169 阅读
<?php /** * * @package ucp * @version $Id: ucp.php 10090 2009-09-03 09:25:16Z acydburn $ * @copyright (c) 2005 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ /** * @ignore */ define('IN_PHPBB', true); //可以把这个文件分为三大部分ABC,以下是A。你看,基本上都在搞定义阿、包含阿,赋值阿等等,这是在做准备工作,为处理用户提交的操作做准备。 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); require($phpbb_root_path . 'common.' . $phpEx); require($phpbb_root_path . 'includes/functions_user.' . $phpEx); require($phpbb_root_path . 'includes/functions_module.' . $phpEx); // Basic parameter data $id = request_var('i', ''); $mode = request_var('mode', ''); if ($mode == 'login' || $mode == 'logout' || $mode == 'confirm') { define('IN_LOGIN', true); } // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup('ucp'); // Setting a variable to let the style designer know where he is... $template->assign_var('S_IN_UCP', true); $module = new p_master(); $default = false; // Basic "global" modes //以下是B,核心是B,用户通常的一些操作都在这里,就一个switch,很简单吧。 switch ($mode) //用户到一个论坛,想注册了,就会点击“注册”这个链接,在php里其实就是去执行“注册“这个php文件,并且你可以在链接的后面加上几个参数,比如这里的mode就是一个从链接传入的参数。 { case 'activate': $module->load('ucp', 'activate'); $module->display($user->lang['UCP_ACTIVATE']); redirect(append_sid("{$phpbb_root_path}index.$phpEx")); break; case 'resend_act': $module->load('ucp', 'resend'); $module->display($user->lang['UCP_RESEND']); break; case 'sendpassword': $module->load('ucp', 'remind'); $module->display($user->lang['UCP_REMIND']); break; case 'register': //用户点了”注册“,他需要注册了。 if ($user->data['is_registered'] || isset($_REQUEST['not_agreed'])) //于是先判断这个用户有没有注册呢?这个$user从哪里冒出来的?先不管,你只需要知道不管你有没有注册,在这个网站有没有记录,你都是一个user,bb3里就是用这个变量去表示你的存在。看看你的资料集(data数组)里有没有注册声明,显然如果你真的是来注册的,不会有这个东西。另外的一个条件是什么?有可能论坛关闭注册是不是?这里通过判断预设变量$_REQUEST[]来判断,那么可以想象,在屏蔽注册功能的同时,需要改写一下预设变量的值,这也很容易做到。 { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); //TODO:这个用来跳回页面,但是关于session id暂时不讲。 } $module->load('ucp', 'register'); //模块机制。 $module->display($user->lang['REGISTER']); break; case 'confirm': $module->load('ucp', 'confirm'); break; case 'login': if ($user->data['is_registered']) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } login_box(request_var('redirect', "index.$phpEx")); break; case 'logout': if ($user->data['user_id'] != ANONYMOUS && isset($_GET['sid']) && !is_array($_GET['sid']) && $_GET['sid'] === $user->session_id) { $user->session_kill(); $user->session_begin(); $message = $user->lang['LOGOUT_REDIRECT']; } else { $message = ($user->data['user_id'] == ANONYMOUS) ? $user->lang['LOGOUT_REDIRECT'] : $user->lang['LOGOUT_FAILED']; } meta_refresh(3, append_sid("{$phpbb_root_path}index.$phpEx")); $message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a> '); trigger_error($message); break; case 'terms': case 'privacy': $message = ($mode == 'terms') ? 'TERMS_OF_USE_CONTENT' : 'PRIVACY_POLICY'; $title = ($mode == 'terms') ? 'TERMS_USE' : 'PRIVACY'; if (empty($user->lang[$message])) { if ($user->data['is_registered']) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } login_box(); } $template->set_filenames(array( 'body' => 'ucp_agreement.html') ); // Disable online list page_header($user->lang[$title], false); $template->assign_vars(array( 'S_AGREEMENT' => true, 'AGREEMENT_TITLE' => $user->lang[$title], 'AGREEMENT_TEXT' => sprintf($user->lang[$message], $config['sitename'], generate_board_url()), 'U_BACK' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'), 'L_BACK' => $user->lang['BACK_TO_LOGIN'], )); page_footer(); break; case 'delete_cookies': // Delete Cookies with dynamic names (do NOT delete poll cookies) if (confirm_box(true)) { $set_time = time() - 31536000; foreach ($_COOKIE as $cookie_name => $cookie_data) { // Only delete board cookies, no other ones... if (strpos($cookie_name, $config['cookie_name'] . '_') !== 0) { continue; } $cookie_name = str_replace($config['cookie_name'] . '_', '', $cookie_name); // Polls are stored as {cookie_name}_poll_{topic_id}, cookie_name_ got removed, therefore checking for poll_ if (strpos($cookie_name, 'poll_') !== 0) { $user->set_cookie($cookie_name, '', $set_time); } } $user->set_cookie('track', '', $set_time); $user->set_cookie('u', '', $set_time); $user->set_cookie('k', '', $set_time); $user->set_cookie('sid', '', $set_time); // We destroy the session here, the user will be logged out nevertheless $user->session_kill(); $user->session_begin(); meta_refresh(3, append_sid("{$phpbb_root_path}index.$phpEx")); $message = $user->lang['COOKIES_DELETED'] . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>'); trigger_error($message); } else { confirm_box(false, 'DELETE_COOKIES', ''); } redirect(append_sid("{$phpbb_root_path}index.$phpEx")); break; case 'switch_perm': $user_id = request_var('u', 0); $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . (int) $user_id; $result = $db->sql_query($sql); $user_row = $db->sql_fetchrow($result); $db->sql_freeresult($result); if (!$auth->acl_get('a_switchperm') || !$user_row || $user_id == $user->data['user_id'] || !check_link_hash(request_var('hash', ''), 'switchperm')) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } include($phpbb_root_path . 'includes/acp/auth.' . $phpEx); $auth_admin = new auth_admin(); if (!$auth_admin->ghost_permissions($user_id, $user->data['user_id'])) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } add_log('admin', 'LOG_ACL_TRANSFER_PERMISSIONS', $user_row['username']); $message = sprintf($user->lang['PERMISSIONS_TRANSFERRED'], $user_row['username']) . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>'); trigger_error($message); break; case 'restore_perm': if (!$user->data['user_perm_from'] || !$auth->acl_get('a_switchperm')) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } $auth->acl_cache($user->data); $sql = 'SELECT username FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user->data['user_perm_from']; $result = $db->sql_query($sql); $username = $db->sql_fetchfield('username'); $db->sql_freeresult($result); add_log('admin', 'LOG_ACL_RESTORE_PERMISSIONS', $username); $message = $user->lang['PERMISSIONS_RESTORED'] . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>'); trigger_error($message); break; default: $default = true; break; } // We use this approach because it does not impose large code changes if (!$default) //以上的众多mode基本上能应付用户的操作了,但难免有“漏网之鱼”,这里就是在判断是否漏网了,漏网了,pass,没漏网,return,跳出本文件。 { return true; } //以下是C,用来处理过来的“漏网之鱼” // Only registered users can go beyond this point //只有注册用户才能通过这里。 if (!$user->data['is_registered']) { if ($user->data['is_bot']) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } login_box('', $user->lang['LOGIN_EXPLAIN_UCP']); } //所以只有注册用户的某些特殊操作才能来到这里有幸被处理。 // Instantiate module system and generate list of available modules $module->list_modules('ucp'); // Check if the zebra module is set if ($module->is_active('zebra', 'friends')) { // Output listing of friends online $update_time = $config['load_online_time'] * 60; $sql = $db->sql_build_query('SELECT_DISTINCT', array( 'SELECT' => 'u.user_id, u.username, u.username_clean, u.user_colour, MAX(s.session_time) as online_time, MIN(s.session_viewonline) AS viewonline', 'FROM' => array( USERS_TABLE => 'u', ZEBRA_TABLE => 'z' ), 'LEFT_JOIN' => array( array( 'FROM' => array(SESSIONS_TABLE => 's'), 'ON' => 's.session_user_id = z.zebra_id' ) ), 'WHERE' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.friend = 1 AND u.user_id = z.zebra_id', 'GROUP_BY' => 'z.zebra_id, u.user_id, u.username_clean, u.user_colour, u.username', 'ORDER_BY' => 'u.username_clean ASC', )); $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $which = (time() - $update_time < $row['online_time'] && ($row['viewonline'] || $auth->acl_get('u_viewonline'))) ? 'online' : 'offline'; $template->assign_block_vars("friends_{$which}", array( 'USER_ID' => $row['user_id'], 'U_PROFILE' => get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']), 'USER_COLOUR' => get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']), 'USERNAME' => get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']), 'USERNAME_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'])) ); } $db->sql_freeresult($result); } // Do not display subscribed topics/forums if not allowed if (!$config['allow_topic_notify'] && !$config['allow_forum_notify']) { $module->set_display('main', 'subscribed', false); } // Select the active module $module->set_active($id, $mode); // Load and execute the relevant module $module->load_active(); // Assign data to the template engine for the list of modules $module->assign_tpl_vars(append_sid("{$phpbb_root_path}ucp.$phpEx")); // Generate the page, do not display/query online list $module->display($module->get_page_title(), false); //本文件到此结束。 /** * Function for assigning a template var if the zebra module got included */ function _module_zebra($mode, &$module_row) { global $template; $template->assign_var('S_ZEBRA_ENABLED', true); if ($mode == 'friends') { $template->assign_var('S_ZEBRA_FRIENDS_ENABLED', true); } if ($mode == 'foes') { $template->assign_var('S_ZEBRA_FOES_ENABLED', true); } } ?>