PHP开源系统学习之fluxbb_1
最近一直忙于做项目,虽说做了点新东西。感觉自己进步不是很大,总体水平还是跟半年前差不多,想到的东西跟以前差不多,写出来的东西也跟以前差不多。只是现在做的东西多些,比以前敢做了。
近期准备利用点时间,读读一些开源系统,之前一直想学习下discuz,无奈多次放弃。还是对老外的感兴趣,虽然自己英语差的不行,之前也做过wordpress的二次开发,这次准备对fluxbb下手。啥也不说了,直接上场。
整体结构是面向过程写的,自己也喜欢这样的风格,老外不少优秀的开源系统都是这样的风格,wordpress也是。
在fluxbb中引入的文件一般都省略“?>”结束
首先分析的是common.php及相关文件(定义常量、引入函数库、建立数据库连接、环境检查等)
//关闭魔术引用(如已开启),php 5.2及以下默认开启
//过滤POST、GET等,此处用到array_map(),如果是上传文件另做处理
// Turn off magic_quotes_runtime
if (get_magic_quotes_runtime())
set_magic_quotes_runtime(0); // Strip slashes from GET/POST/COOKIE/REQUEST/FILES (if magic_quotes_gpc is enabled)
if (!defined('FORUM_DISABLE_STRIPSLASHES') && get_magic_quotes_gpc())
{
function stripslashes_array($array)
{
return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
} $_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
$_COOKIE = stripslashes_array($_COOKIE);
$_REQUEST = stripslashes_array($_REQUEST);
if (is_array($_FILES))
{
// Don't strip valid slashes from tmp_name path on Windows
foreach ($_FILES AS $key => $value)
$_FILES[$key]['tmp_name'] = str_replace('\\', '\\\\', $value['tmp_name']);
$_FILES = stripslashes_array($_FILES);
}
}
//建立数据库连接
// Load DB abstraction layer and connect
require PUN_ROOT.'include/dblayer/common_db.php'; // Start a transaction
$db->start_transaction();
//common_db.php通过判断$db_type来引入不同的db class,$db_type在根目录下config.php文件中已定义,common.php引入的config.php文件
//这样有个好处,以后换数据库,操作起来方便,不过不同数据库的sql语句语法不同,后期还是得改sql语句,fluxbb基本上直接用的是select * from ....
// Load the appropriate DB layer class
switch ($db_type)
{
case 'mysql':
require_once PUN_ROOT.'include/dblayer/mysql.php';
break; case 'mysql_innodb':
require_once PUN_ROOT.'include/dblayer/mysql_innodb.php';
break; case 'mysqli':
require_once PUN_ROOT.'include/dblayer/mysqli.php';
break; case 'mysqli_innodb':
require_once PUN_ROOT.'include/dblayer/mysqli_innodb.php';
break; case 'pgsql':
require_once PUN_ROOT.'include/dblayer/pgsql.php';
break; case 'sqlite':
require_once PUN_ROOT.'include/dblayer/sqlite.php';
break; default:
error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__);
break;
}
//读取cache_config.php配置文件,避免重复查询数据库
//首先引入cache_config.php,常量PUN_CONFIG_LOADED其实在cache_config.php已有定义。如果没定义的话,就重新生成文件
// Load cached config
if (file_exists(FORUM_CACHE_DIR.'cache_config.php'))
include FORUM_CACHE_DIR.'cache_config.php'; if (!defined('PUN_CONFIG_LOADED'))
{
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php'; generate_config_cache();
require FORUM_CACHE_DIR.'cache_config.php';
}
//cache.php文件
//从数据库查取然后写入到cache_config.php文件中(前提是PUN_CONFIG_LOADED常量未定义)
//以下代码中有个函数用的很到位,var_export()跟var_dump()类似,不同的是前者返回一个串,后者直接输出。C语言中printf()跟sprintf()的区别。
//
// Load some information about the latest registered users
//
function generate_users_info_cache()
{
global $db; $stats = array(); $result = $db->query('SELECT COUNT(id)-1 FROM '.$db->prefix.'users WHERE group_id!='.PUN_UNVERIFIED) or error('Unable to fetch total user count', __FILE__, __LINE__, $db->error());
$stats['total_users'] = $db->result($result); $result = $db->query('SELECT id, username FROM '.$db->prefix.'users WHERE group_id!='.PUN_UNVERIFIED.' ORDER BY registered DESC LIMIT 1') or error('Unable to fetch newest registered user', __FILE__, __LINE__, $db->error());
$stats['last_user'] = $db->fetch_assoc($result); // Output users info as PHP code
$content = '<?php'."\n\n".'define(\'PUN_USERS_INFO_LOADED\', 1);'."\n\n".'$stats = '.var_export($stats, true).';'."\n\n".'?>';
fluxbb_write_cache_file('cache_users_info.php', $content);
}
//写入文件
//考虑的比较全面,先flock()锁定该文件,然后ftruncate()清空,写入fwrite(),后解锁flock(),再关闭文件流。
//
// Safely write out a cache file.
//
function fluxbb_write_cache_file($file, $content)
{
$fh = @fopen(FORUM_CACHE_DIR.$file, 'wb');
if (!$fh)
error('Unable to write cache file '.pun_htmlspecialchars($file).' to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__); flock($fh, LOCK_EX);
ftruncate($fh, 0); fwrite($fh, $content); flock($fh, LOCK_UN);
fclose($fh); if (function_exists('apc_delete_file'))
@apc_delete_file(FORUM_CACHE_DIR.$file);
}
fluxbb在查询函数中,一般是定义$db为全局,$config为全局。
//以下几个函数主要是检查用户信息及状态,都在funcions.php中定义的
// Check/update/set cookie and fetch user info
$pun_user = array();
check_cookie($pun_user); //传引用 // Check if current user is banned
check_bans(); // Update online list
update_users_online();
//同读取cache_config.php配置文件,只是现在我还不知道这个是作什么用的,以后再补上。数据库表bans暂为空,写入的cache_bans.php为一空数组
// Load cached bans
if (file_exists(FORUM_CACHE_DIR.'cache_bans.php'))
include FORUM_CACHE_DIR.'cache_bans.php'; if (!defined('PUN_BANS_LOADED'))
{
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php'; generate_bans_cache();
require FORUM_CACHE_DIR.'cache_bans.php';
}
//extension_loaded()判断扩展是否已导入
if ($pun_config['o_gzip'] && extension_loaded('zlib'))
ob_start('ob_gzhandler');
else
ob_start();
common.php文件基本是这些了。后续会作其他文件或功能模块的简单分析,备忘!
补:刚在fluxbb一些涉及到数据库的函数,都通过全局变量$db_type来作判断,然后分别写不同的Sql语句,目前主要支持mysql和sqlite,特来赞一个!
原文作者:lltong,博客园地址:http://www.cnblogs.com/lltong/
最新文章
- keycode
- JSon转化为DaTable
- jQuery下通过$.browser来判断浏览器
- PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
- 同时使用Twitter nlp 和stanford parser的解决方法
- 30 分钟快快乐乐学 SQL Performance Tuning
- Redis 笔记与总结6 Redis 高级应用之 事务处理、持久化操作、pub_sub、虚拟内存
- winform之自定义控件
- UCOS-信号量(学习笔记)
- JavaScript计算日期间隔以及结果错误(少一天)的解决方法
- 第十三章 调试及安全性(In .net4.5) 之 验证程序输入
- HighCharts开发说明及属性详解
- Memcached 缓存个体,对象,泛型,表
- 知识总结: Activity的四种启动模式
- Description:一根高筋拉面,中间切一刀,可以得到2根面条。如果先对折1次,中间切一刀,可以得到3根面条。如果连续对折2次,中间切一刀,可以得到5根面条。Input:你的程序需要解决的问题是,输入连续对折的次数。NOutput输出中间切一刀,可以得到多少根面条。
- xcode 资源管理
- JqGrid 显示表
- zookeeper配置记录
- display:inline-block,block,inline的区别与用法
- 作业二:Git的安装与使用
热门文章
- Codeforces 294B Shaass and Bookshelf:dp
- java:eclipse安装tomcat插件及配置tomcat
- Guid 使用记录
- win2008server R2 x64 部署.net core到IIS
- Spring MVC表单提交
- VC++6.0注释快捷键的添加使用
- 标准模板库(STL)学习指南之sort排序
- [转]由Tencent://Message协议想到的一个解决方案
- Linux CentOS安装Azure Cli工具
- JS开发中的一些小技巧和方法