MyBB代码浅析(1)-- 核心文件init.php
时间:2010-05-06
来源:互联网
本人才疏学浅,但是由于对开源程序的爱好,也可以使国内PHP爱好者们(特别是新手)接触到国外一流的程序,所以开办了这个网站www.PHPig.net,PHP猪。我们只研究非主流代码,当然这里的非主流是针对国内的情况。目标是通过研究这些代码,而提高大家的PHP水平。这里申明一下:本人绝对是PHP的菜鸟,在剖析代码的同时,也是学习的过程,所以大家有任何意见可以随时提出,大家互相学习。
国内的PHP发展还是很蓬勃的,优秀的产品很多,包括康盛系列,PHPWIND,还有各大CMS等,而框架也有THINKPHP。国内PHP爱好者们已经习惯了这种产品的代码,思维和用户习惯。而了解国外的同类型产品,无论从代码还是操作都会觉得不习惯,但是,这也是个人认为是非常值得体验的。
MyBB是国际上非常优秀的免费论坛软件,最大的特色是简单但是功能却出奇的强大。支持多国语言,可以分别设置前台后台的语言,每个用户也可以设置自己使用何种语言访问论坛包括自己的时区等,自定义功能强大到没有做不到只有想不到。这对于建立国际性的大型论坛必不可少,正因为如此,优异的程序结构,代码和数据库设计以及多重处理方法使得占用资源更少,负载更强,速度更快。
国内的支持网站为www.mybbchina.net,也是由他们负责汉化的。最新中文版本MyBB中文版 1.4.13. 代码不包括注释和空行10万行。发布日期: 2010年4月19日,可在该网站下载。
这里的init.php为mybb的核心文件,他加载系统的各种底层类和初始化各种变量。本人才疏学浅,这里就尝试浅析一下这个核心文件,肯定有理解错误的,也请大家提醒指点,大家共同学习。
作者: garygay 发布时间: 2010-05-06
PHPCHINA的高亮插件有BUG,从WORD文档复制过来的话,会带样式。所以我直接贴了,想看高亮美化版的,可以到此地址
http://www.phpig.net/showthread.php?tid=58
/**
代码:MyBB,官网http://www.mybboard.net
代码浅析:GaryGay, 首发于http://www.PHPig.net
申明:尽量翻译官方英文含义,并且根据个人水平添加注释说明。
转载请保留以上信息。
**/
if(!defined("IN_MYBB")) //:如果没有通过index来访问这个文件,是要报错的哦
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
if(function_exists("unicode_decode"))
{
//:在PHP6.0推出的Unicode扩展
error_reporting(E_ALL ^ E_DEPRECATED ^ E_NOTICE ^ E_STRICT);
}
elseif(defined("E_DEPRECATED"))
{
// E_DEPRECATED introduced in 5.3
error_reporting(E_ALL ^ E_DEPRECATED ^ E_NOTICE);
}
else
{
error_reporting(E_ALL & ~E_NOTICE);
}
/* :定义MyBB的根目录
如果你遇到问题,取消下面的注释并手动设置路径.
一般在路径末尾都添加斜杠
* 你的MyBB路径
* "./"
*/
//define('MYBB_ROOT', "./");
// 尝试自动检测
if(!defined('MYBB_ROOT'))
{
define('MYBB_ROOT', dirname(dirname(__FILE__))."/"); // 定义服务器根目录,如果你的MyBB不直接在服务器根目录下的话,可能会有问题。
}
define("TIME_NOW", time()); // 把现在的时候定义为常量
require_once MYBB_ROOT."inc/functions_compat.php"; //函数兼容,如果运行的是老版本的PHP,那么就用MyBB给你写的同名函数,替代高版本的PHP函数。
require_once MYBB_ROOT."inc/class_error.php"; //出现erro时,采用的类。
$error_handler = new errorHandler(); //初始化错误处理程序
require_once MYBB_ROOT."inc/functions.php"; //各种常用函数
require_once MYBB_ROOT."inc/class_timers.php"; //时间计算类
$maintimer = new timer(); //开始计算时间
require_once MYBB_ROOT."inc/class_core.php"; //加载核心类,初始化MyBB的各种值
$mybb = new MyBB;
if(!file_exists(MYBB_ROOT."inc/config.php"))
{
$mybb->trigger_generic_error("board_not_installed");
}
// Include the required core files
require_once MYBB_ROOT."inc/config.php";
$mybb->config = &$config; //把配置信息存入数组
if(!isset($config['database']))
{
$mybb->trigger_generic_error("board_not_installed");
}
if(!is_array($config['database']))
{
$mybb->trigger_generic_error("board_not_upgraded");
}
if(empty($config['admin_dir']))
{
$config['admin_dir'] = "admin";
}
// 判断是否已经安装了MyBB
if(is_dir(MYBB_ROOT."install") && !file_exists(MYBB_ROOT."install/lock"))
{
$mybb->trigger_generic_error("install_directory");
}
require_once MYBB_ROOT."inc/db_".$config['database']['type'].".php";
switch($config['database']['type']) //根据数据库类型,建立不同的实例。
{
case "sqlite3":
$db = new DB_SQLite3;
break;
case "sqlite2":
$db = new DB_SQLite2;
break;
case "pgsql":
$db = new DB_PgSQL;
break;
case "mysqli":
$db = new DB_MySQLi;
break;
default:
$db = new DB_MySQL;
}
// 检测DB扩展是否加载
if(!extension_loaded($db->engine))
{
// Throw our super awesome db loading error
$mybb->trigger_generic_error("sql_load_error");
}
require_once MYBB_ROOT."inc/class_templates.php"; //加载模板类
$templates = new templates;
require_once MYBB_ROOT."inc/class_datacache.php"; //加载缓存类
$cache = new datacache;
require_once MYBB_ROOT."inc/class_plugins.php"; //加载插件类
$plugins = new pluginSystem;
// Include our base data handler class
require_once MYBB_ROOT."inc/datahandler.php";
// Connect to Database
define("TABLE_PREFIX", $config['database']['table_prefix']); //数据库表前缀常量,这里定义
$db->connect($config['database']);
$db->set_table_prefix(TABLE_PREFIX);
$db->type = $config['database']['type'];
// Language initialisation
require_once MYBB_ROOT."inc/class_language.php";
$lang = new MyLanguage;
$lang->set_path(MYBB_ROOT."inc/languages"); //定义语言包路径
/** 读取缓存,可以选择缓存存储方式,默认存储在数据库中,可以选择 'files', 'memcache' or 'eaccelerator' from 'db'.
默认把datacache表的数据存到$cache数组中
*/
$cache->cache();
// Load Settings
if(file_exists(MYBB_ROOT."inc/settings.php"))
{
require_once MYBB_ROOT."inc/settings.php";
}
if(!file_exists(MYBB_ROOT."inc/settings.php") || !$settings)
{
if(function_exists('rebuild_settings'))
{
rebuild_settings();
}
else
{
$options = array(
"order_by" => "title",
"order_dir" => "ASC"
);
$query = $db->simple_select("settings", "value, name", "", $options);
while($setting = $db->fetch_array($query))
{
$setting['value'] = str_replace("\"", "\\\"", $setting['value']);
$settings[$setting['name']] = $setting['value']; //读setting表,把相关信息存到数组里
}
$db->free_result($query); //释放内存
}
}
$settings['wolcutoff'] = $settings['wolcutoffmins']*60;
$settings['bbname_orig'] = $settings['bbname'];
$settings['bbname'] = strip_tags($settings['bbname']);
$settings['orig_bblanguage'] = $settings['bblanguage'];
作者: garygay 发布时间: 2010-05-06
if(substr($settings['bburl'], -1) == "/")
{
$settings['bburl'] = my_substr($settings['bburl'], 0, -1);
}
$settings['internal'] = $cache->read("internal_settings"); //这里读取148行左右生成的cache数组的值
if(!$settings['internal']['encryption_key'])
{
$cache->update("internal_settings", array('encryption_key' => random_str(32))); //生成一段加密KEY
$settings['internal'] = $cache->read("internal_settings");
}
$mybb->settings = &$settings;
$mybb->parse_cookies(); //生成COOKIES,Cookie的前缀的含义。对于在一个域名使用多个MyBB或者和其它软件存在冲突时非常有用。如果不指定则不使用前缀。
$mybb->cache = &$cache;
if($mybb->settings['useshutdownfunc'] != 0)
{
$mybb->use_shutdown = true;
register_shutdown_function(array(&$mybb, "__destruct")); //脚本结束时运行回调函数?未明白。
}
// Load plugins
if(!defined("NO_PLUGINS"))
{
$plugins->load();
}
// Set up any shutdown functions we need to run globally
add_shutdown('send_mail_queue'); //这里暂不明白。
/* 定义URL规则 */
if($mybb->settings['seourls'] == "yes" || ($mybb->settings['seourls'] == "auto" && $_SERVER['SEO_SUPPORT'] == 1))
{
define('FORUM_URL', "forum-{fid}.html");
define('FORUM_URL_PAGED', "forum-{fid}-page-{page}.html");
define('THREAD_URL', "thread-{tid}.html");
define('THREAD_URL_PAGED', "thread-{tid}-page-{page}.html");
define('THREAD_URL_ACTION', 'thread-{tid}-{action}.html');
define('THREAD_URL_POST', 'thread-{tid}-post-{pid}.html');
define('POST_URL', "post-{pid}.html");
define('PROFILE_URL', "user-{uid}.html");
define('ANNOUNCEMENT_URL', "announcement-{aid}.html");
define('CALENDAR_URL', "calendar-{calendar}.html");
define('CALENDAR_URL_YEAR', 'calendar-{calendar}-year-{year}.html');
define('CALENDAR_URL_MONTH', 'calendar-{calendar}-year-{year}-month-{month}.html');
define('CALENDAR_URL_DAY', 'calendar-{calendar}-year-{year}-month-{month}-day-{day}.html');
define('CALENDAR_URL_WEEK', 'calendar-{calendar}-week-{week}.html');
define('EVENT_URL', "event-{eid}.html");
define('INDEX_URL', "index.php");
}
else
{
define('FORUM_URL', "forumdisplay.php?fid={fid}");
define('FORUM_URL_PAGED', "forumdisplay.php?fid={fid}&page={page}");
define('THREAD_URL', "showthread.php?tid={tid}");
define('THREAD_URL_PAGED', "showthread.php?tid={tid}&page={page}");
define('THREAD_URL_ACTION', 'showthread.php?tid={tid}&action={action}');
define('THREAD_URL_POST', 'showthread.php?tid={tid}&pid={pid}');
define('POST_URL', "showthread.php?pid={pid}");
define('PROFILE_URL', "member.php?action=profile&uid={uid}");
define('ANNOUNCEMENT_URL', "announcements.php?aid={aid}");
define('CALENDAR_URL', "calendar.php?calendar={calendar}");
define('CALENDAR_URL_YEAR', "calendar.php?action=yearview&calendar={calendar}&year={year}");
define('CALENDAR_URL_MONTH', "calendar.php?calendar={calendar}&year={year}&month={month}");
define('CALENDAR_URL_DAY', 'calendar.php?action=dayview&calendar={calendar}&year={year}&month={month}&day={day}');
define('CALENDAR_URL_WEEK', 'calendar.php?action=weekview&calendar={calendar}&week={week}');
define('EVENT_URL', "calendar.php?action=event&eid={eid}");
define('INDEX_URL', "index.php");
}
// 日期格式化,用于用户自己选择格式。
$date_formats = array(
1 => "m-d-Y",
2 => "m-d-y",
3 => "m.d.Y",
4 => "m.d.y",
5 => "d-m-Y",
6 => "d-m-y",
7 => "d.m.Y",
8 => "d.m.y",
9 => "F jS, Y",
10 => "l, F jS, Y",
11 => "jS F, Y",
12 => "l, jS F, Y"
);
//时间格式化。
$time_formats = array(
1 => "h:i a",
2 => "h:i A",
3 => "H:i"
);
?>
重点浅析:
缓存类很有玄机,可以选择缓存存储方式,默认存储在数据库中,可以选择 'files', 'memcache' or 'eaccelerator' from 'db'.
以后会详细分析class_datacache.php,了解各种缓存机制。
作者: garygay 发布时间: 2010-05-06
作者: spser 发布时间: 2010-05-06
作者: garygay 发布时间: 2010-05-07
作者: gently 发布时间: 2010-05-07
作者: uit88 发布时间: 2010-05-07
作者: jacker 发布时间: 2010-05-09
作者: 寂寞流星 发布时间: 2010-06-09
作者: light0008 发布时间: 2010-08-10
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28