PHP开源系统学习之fluxbb_1

最近一直忙于作项目,虽然说作了点新东西。感受本身进步不是很大,整体水平仍是跟半年前差很少,想到的东西跟之前差很少,写出来的东西也跟之前差很少。只是如今作的东西多些,比之前敢作了。php

近期准备利用点时间,读读一些开源系统,以前一直想学习下discuz,无奈屡次放弃。仍是对老外的感兴趣,虽然本身英语差的不行,以前也作过wordpress的二次开发,此次准备对fluxbb下手。啥也不说了,直接上场。html

总体结构是面向过程写的,本身也喜欢这样的风格,老外很多优秀的开源系统都是这样的风格,wordpress也是。mysql

在fluxbb中引入的文件通常都省略“?>”结束sql

首先分析的是common.php及相关文件(定义常量、引入函数库、创建数据库链接、环境检查等)数据库

//关闭魔术引用(如已开启),php 5.2及如下默认开启数组

//过滤POST、GET等,此处用到array_map(),若是是上传文件另作处理cookie

 1 // Turn off magic_quotes_runtime
 2 if (get_magic_quotes_runtime())
 3     set_magic_quotes_runtime(0);
 4 
 5 // Strip slashes from GET/POST/COOKIE/REQUEST/FILES (if magic_quotes_gpc is enabled)
 6 if (!defined('FORUM_DISABLE_STRIPSLASHES') && get_magic_quotes_gpc())
 7 {
 8     function stripslashes_array($array)
 9     {
10         return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
11     }
12 
13     $_GET = stripslashes_array($_GET);
14     $_POST = stripslashes_array($_POST);
15     $_COOKIE = stripslashes_array($_COOKIE);
16     $_REQUEST = stripslashes_array($_REQUEST);
17     if (is_array($_FILES))
18     {
19         // Don't strip valid slashes from tmp_name path on Windows
20         foreach ($_FILES AS $key => $value)
21             $_FILES[$key]['tmp_name'] = str_replace('\\', '\\\\', $value['tmp_name']);
22         $_FILES = stripslashes_array($_FILES);
23     }
24 }

//创建数据库链接app

1 // Load DB abstraction layer and connect
2 require PUN_ROOT.'include/dblayer/common_db.php';
3 
4 // Start a transaction
5 $db->start_transaction();

//common_db.php经过判断$db_type来引入不一样的db class,$db_type在根目录下config.php文件中已定义,common.php引入的config.php文件wordpress

//这样有个好处,之后换数据库,操做起来方便,不过不一样数据库的sql语句语法不一样,后期仍是得改sql语句,fluxbb基本上直接用的是select * from ....函数

 1 // Load the appropriate DB layer class
 2 switch ($db_type)
 3 {
 4     case 'mysql':
 5         require_once PUN_ROOT.'include/dblayer/mysql.php';
 6         break;
 7 
 8     case 'mysql_innodb':
 9         require_once PUN_ROOT.'include/dblayer/mysql_innodb.php';
10         break;
11 
12     case 'mysqli':
13         require_once PUN_ROOT.'include/dblayer/mysqli.php';
14         break;
15 
16     case 'mysqli_innodb':
17         require_once PUN_ROOT.'include/dblayer/mysqli_innodb.php';
18         break;
19 
20     case 'pgsql':
21         require_once PUN_ROOT.'include/dblayer/pgsql.php';
22         break;
23 
24     case 'sqlite':
25         require_once PUN_ROOT.'include/dblayer/sqlite.php';
26         break;
27 
28     default:
29         error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__);
30         break;
31 }

//读取cache_config.php配置文件,避免重复查询数据库

//首先引入cache_config.php,常量PUN_CONFIG_LOADED其实在cache_config.php已有定义。若是没定义的话,就从新生成文件

 1 // Load cached config
 2 if (file_exists(FORUM_CACHE_DIR.'cache_config.php'))
 3     include FORUM_CACHE_DIR.'cache_config.php';
 4 
 5 if (!defined('PUN_CONFIG_LOADED'))
 6 {
 7     if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
 8         require PUN_ROOT.'include/cache.php';
 9 
10     generate_config_cache();
11     require FORUM_CACHE_DIR.'cache_config.php';
12 }

//cache.php文件

//从数据库查取而后写入到cache_config.php文件中(前提是PUN_CONFIG_LOADED常量未定义)

//如下代码中有个函数用的很到位,var_export()跟var_dump()相似,不一样的是前者返回一个串,后者直接输出。C语言中printf()跟sprintf()的区别。

 1 //
 2 // Load some information about the latest registered users
 3 //
 4 function generate_users_info_cache()
 5 {
 6     global $db;
 7 
 8     $stats = array();
 9 
10     $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());
11     $stats['total_users'] = $db->result($result);
12 
13     $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());
14     $stats['last_user'] = $db->fetch_assoc($result);
15 
16     // Output users info as PHP code
17     $content = '<?php'."\n\n".'define(\'PUN_USERS_INFO_LOADED\', 1);'."\n\n".'$stats = '.var_export($stats, true).';'."\n\n".'?>';
18     fluxbb_write_cache_file('cache_users_info.php', $content);
19 }

//写入文件

//考虑的比较全面,先flock()锁定该文件,而后ftruncate()清空,写入fwrite(),后解锁flock(),再关闭文件流。

 1 //
 2 // Safely write out a cache file.
 3 //
 4 function fluxbb_write_cache_file($file, $content)
 5 {
 6     $fh = @fopen(FORUM_CACHE_DIR.$file, 'wb');
 7     if (!$fh)
 8         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__);
 9 
10     flock($fh, LOCK_EX);
11     ftruncate($fh, 0);
12 
13     fwrite($fh, $content);
14 
15     flock($fh, LOCK_UN);
16     fclose($fh);
17 
18     if (function_exists('apc_delete_file'))
19         @apc_delete_file(FORUM_CACHE_DIR.$file);
20 }

fluxbb在查询函数中,通常是定义$db为全局,$config为全局。

//如下几个函数主要是检查用户信息及状态,都在funcions.php中定义的

1 // Check/update/set cookie and fetch user info
2 $pun_user = array();
3 check_cookie($pun_user);    //传引用
4 
5 // Check if current user is banned
6 check_bans();
7 
8 // Update online list
9 update_users_online();

//同读取cache_config.php配置文件,只是如今我还不知道这个是做什么用的,之后再补上。数据库表bans暂为空,写入的cache_bans.php为一空数组

 1 // Load cached bans
 2 if (file_exists(FORUM_CACHE_DIR.'cache_bans.php'))
 3     include FORUM_CACHE_DIR.'cache_bans.php';
 4 
 5 if (!defined('PUN_BANS_LOADED'))
 6 {
 7     if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
 8         require PUN_ROOT.'include/cache.php';
 9 
10     generate_bans_cache();
11     require FORUM_CACHE_DIR.'cache_bans.php';
12 }

//extension_loaded()判断扩展是否已导入

1 if ($pun_config['o_gzip'] && extension_loaded('zlib'))
2         ob_start('ob_gzhandler');
3     else
4         ob_start();

 

common.php文件基本是这些了。后续会做其余文件或功能模块的简单分析,备忘!

 补:刚在fluxbb一些涉及到数据库的函数,都经过全局变量$db_type来做判断,而后分别写不一样的Sql语句,目前主要支持mysql和sqlite,特来赞一个!

 

原文做者:lltong,博客园地址:http://www.cnblogs.com/lltong/

相关文章
相关标签/搜索