深入 Drupal 引导流程 (2)
这段时间一直在研究 Drupal 的源代码,哈哈,少时间来更新技术文章,今天我们再续 Drupal 的引导流程
case DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE:
// Allow specifying special cache handlers in settings.php, like
// using memcached or files for storing cache information.
require_once variable_get('cache_inc', './includes/cache.inc');
// If the page_cache_fastpath is set to TRUE in settings.php and
// page_cache_fastpath (implemented in the special implementation of
// cache.inc) printed the page and indicated this with a returned TRUE
// then we are done.
if (variable_get('page_cache_fastpath', FALSE) && page_cache_fastpath()) {
exit;
}
break;
早期页面缓存阶段,载入了 Drupal 缓存需要使用的三个函数,分别是 cache_get, cache_set 和 cache_clear_all,默认情况下,我们可以在 includes/cache.inc 找到。又或者我们可以自定义一个 cache_inc 脚本文件来重载我们的缓存方法,甚至在这个脚本内实现一个名为 page_cache_fastpath 的函数,来实现整个引导阶段,例如,在 settings.php 末尾增加一行
$conf['cache_inc'] = 'my_cache.inc';
然后在 Drupal 根目录创建一个名为 my_cache.inc 的脚本文件
<?php
$conf['page_cache_fastpath'] = true;
function page_cache_fastpath()
{
echo 'Hello world!';
return true; // 返回 true 会终止 Drupal 引导流程
}
大家会看到 Drupal 只返回 Hello world! 了 :)
case DRUPAL_BOOTSTRAP_DATABASE:
// Initialize the default database.
require_once './includes/database.inc';
db_set_active();
break;
数据库阶段,很简单,就是载入数据库头文件,然后根据配置变量 $db_url 识别出数据库类型(找不到该变量的情况下,就跳转至安装),目前类型支持 3 种,分别是 mysql, mysqli, pgsql,最后打开数据库连接
case DRUPAL_BOOTSTRAP_ACCESS:
// Deny access to hosts which were banned - t() is not yet available.
if (drupal_is_denied('host', ip_address())) {
header('HTTP/1.1 403 Forbidden');
print 'Sorry, '. check_plain(ip_address()) .' has been banned.';
exit();
}
break;
访问权限阶段,是根据客户端 IP 地址来检查用户是否在黑名单内,名单可以在 Administrator > User management > Access rules 管理,Rule type 为 Host,或者可以直接管理数据表 access 的 type 字段为 'host' 的部分。drupal_is_denied (line 931) 的这个权限检查函数比较弱智,它通过 LIKE 方式检查 IP 与 rule 是否匹配,且当这个 IP 同时存在于黑名单和白名单内,那么这次访问也算通过,比如 allow 127.% 且 deny 127.0.0.1,当 127.0.0.1 访问时,结果为 allow,尽管这并非你所要求的结果 :)