最近一直忙于做项目,虽说做了点新东西。感觉自己进步不是很大,总体水平还是跟半年前差不多,想到的东西跟以前差不多,写出来的东西也跟以前差不多。只是现在做的东西多些,比以前敢做了。

近期准备利用点时间,读读一些开源系统,之前一直想学习下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/

PHP开源系统学习之fluxbb_1的更多相关文章

  1. PHP开源系统学习之fluxbb_2

    谴责下某位同学,转载了我的上一篇文章,也不给个原文地址,希望这次再来转时能加上. //检查登录,在common.php判断 //cookie串: 2|dc4fab5bb354be5104bae0aff ...

  2. GitHub 上 57 款最流行的开源深度学习项目

    转载:https://www.oschina.net/news/79500/57-most-popular-deep-learning-project-at-github GitHub 上 57 款最 ...

  3. Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包

    Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包 用 CNTK 搞深度学习 (一) 入门 Computational Network Toolk ...

  4. Hibernate的系统 学习

    Hibernate的系统 学习 一.Hibernate的介绍 1.什么是Hibernate? 首先,hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多比如:iBATIS,myBat ...

  5. Ubuntu LTS 系统学习使用体会和实用工具软件汇总 6.04 8.04 10.04 12.04 14.04 16.04

    Ubuntu LTS 系统学习体会和工具软件汇总 6.04 8.04 10.04 12.04 14.04 16.04 ubuntu入门必备pdf:http://download.csdn.net/de ...

  6. MySQL如何系统学习

    MySQL是当下互联网最流行的开源数据库.不管你使用或者学习何种编程语言,都将会使用到数据库,而MySQL则是应用最为广泛的数据库,没有之一! 之前在我的博客上也发布过一些MySQL优化配置项,都收到 ...

  7. 开源深度学习架构Caffe

    Caffe 全称为 Convolutional Architecture for Fast Feature Embedding,是一个被广泛使用的开源深度学习框架(在 TensorFlow 出现之前一 ...

  8. 一种基于python的人脸识别开源系统

    今天在搜索人脸识别的文章时,无意中搜到一个比较开源代码,介绍说是这个系统人脸的识别率 是比较高的,可以达到:99.38%.这么高的识别率,着实把我吓了一跳.抱着实事求是的态度.个人 就做了一些验证和研 ...

  9. GitHub 上 57 款最流行的开源深度学习项目【转】

    GitHub 上 57 款最流行的开源深度学习项目[转] 2017-02-19 20:09 334人阅读 评论(0) 收藏 举报 分类: deeplearning(28) from: https:// ...

随机推荐

  1. 大话设计模式--原型模式 Prototype -- C++实现

    1. 原型模式: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象... 注意: 拷贝的时候是浅拷贝 还是 深拷贝, 来考虑是否需要重写拷贝构造函数. 关键在于: virtual Pro ...

  2. codevs1279 Guard 的无聊

    题目描述 Description 在那楼梯那边数实里面,有一只 guard,他活泼又聪明,他卖萌又霸气.他每天刷题虐 场 D 人考上了 PKU,如果无聊就去数一数质数~~ 有一天 guard 在纸上写 ...

  3. python3 字符串属性(一)

    python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...

  4. Delphi webservices 传数据

    数据集数据转换为XML function ReplaceString(AString: string): string; begin Result := StringReplace(AString, ...

  5. C++(六)— 输入方式

    1.输入包含空格的字符串 使用 getline(cin, str)读取一行字符串,遇到换行符停止:cin>>str,是遇到空格就停止. 实现:输入两个字符,在第一个字符中删除第二个字符中出 ...

  6. hdu 2041 超级楼梯(简单dp)

    超级楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. cssParser

    //cssParser.h #include<iostream> using namespace std;struct MyAttribute{ MyAttribute*  next; s ...

  8. BEC listen and translation exercise 43

    Reach for the stars so if you fall you land on a cloud.飞向星空吧,就算坠落,接住你的也是云彩. Anyway, exam failure can ...

  9. codeforces 653C C. Bear and Up-Down(乱搞题)

    题目链接: C. Bear and Up-Down time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  10. codeforces 627B B. Factory Repairs(线段树)

    B. Factory Repairs time limit per test 4 seconds memory limit per test 256 megabytes input standard ...