---
title: "phpBB开源论坛单点登录整合"
author: "iRoyce"
date: "2008-07-26T09:57:57Z"
tags: []
categories: ["编程语言"]
slug: "phpbb"
draft: false
lastmod: "2008-07-26T09:57:57Z"
id: 219753
url: "https://iroyce.iteye.com/blog/219753"
---

以下文档以实际项目中的整合为例，抽取了phpBB论坛部分代码，转载请注明出处，欢迎转载！

环境（以下环境测试通过）：
	
Operation System	Windows XP
JVM	SUN JDK 1.5
PHP	PHP 5.2.6
Http Server	Apache 2.2.6
Web Server	Tomcat 5.5
CAS Server	JA-SIG CAS Server 3.2.1
CAS PHP Client	PHP CAS Client 1.0.0RC5
Database	mysql-5.0.26-win32

Apache 2.2.6 + PHP 5.2.6为PHP的WEB服务器！
Tomcat 5.5 为CAS Server的WEB服务器！

注意： 本次整合只PHPBB前台用户能使用SSO功能，管理员用户使用不了SSO，代码中$admin 变量代表是否管理员用户！

4. CAS PHP Client集成 
4.1. 下载CAS PHP Client 
总入口：http://www.ja-sig.org/wiki/display/CASC/phpCAS 
源码入口页：http://www.ja-sig.org/wiki/display/CASC/Developing+phpCAS+within+Eclipse 

源码访问： 
SVN URL: https://www.ja-sig.org/svn/cas-clients/phpcas 
Web interface: http://developer.ja-sig.org/source/browse/jasigsvn/cas-clients/phpcas 

总入口页还有实例可供下载。 

4.2. 更改phpBB论坛登录模块 
以下可以参见：svn://192.168.16.225/phpBB/trunk/phpBB-3.0.1 
1. 把CAS PHP Client的源码copy到${phpBB}/includes下，如下图结构； 
![](http://iroyce.iteye.com/upload/attachment/32368/42a30d80-a722-31e0-8ad6-577003dc3fd1.bmp)
图中选中的文件及目录为CAS PHP Client的全部源码。 
2. 更改CAS PHP Clinet对https的限制； 
Down下来的PHP Client必须要使用https连接与CAS Server通信，在实际项目中，为减少部署的麻烦，可能只要http连接就能满足要求，在文件clinet.php中，把$this->_server = 'https://'更改为$this->_server = 'http://'即可，如图所示。 
![](http://www.iteye.com/upload/attachment/32370/6d6aae9c-0e53-3127-b655-c07f4a8df7d7.bmp)
3. 更改phpBB登录模块 
在function.php文件中 
加入CAS类引用语句: 
require($phpbb_root_path . 'includes/CAS.' . $phpEx);


找到： 
if (isset($_POST))
替换成： 
if(!$admin && CAS_ENABLE){ 
// initialize phpCAS 
phpCAS::client(CAS_VERSION_2_0, CAS_SERVER_HOSTNAME, CAS_SERVER_PORT, CAS_SERVER_APP_NAME); 
phpCAS::setNoCasServerValidation(); 
// force CAS authentication 
phpCAS::forceAuthentication(); 
}
if (isset($_POST) || (!$admin && CAS_ENABLE) ) 


找到: 
$result = $auth->login($username, $password, $autologin, $viewonline, $admin);
在上面代码片段前加上： 
if(!$admin && CAS_ENABLE){ 
$username = phpCAS::getUser(); 
$password = ''; 
} 

在auth.php文件中 
加入以下代码 
/** 
*根据当前已登录CAS的用户名获取用户身份信息，封装用户身份信息 
* added by Royce 
*/ 
function login_cas($username){ 
global $db; 

$sql = 'SELECT user_id, username, user_password, user_passchg, user_pass_convert, user_email, user_type, user_login_attempts FROM ' . USERS_TABLE 
. " WHERE username_clean = '" . $username . "'"; 
$result = $db->sql_query($sql); 
$row = $db->sql_fetchrow($result); 
$db->sql_freeresult($result); 

if (!$row) { 
return array( 
'status' => LOGIN_ERROR_USERNAME, 
'error_msg' => 'LOGIN_ERROR_USERNAME', 
'user_row' => array('user_id' => ANONYMOUS), 
); 
} 

if ($row == USER_INACTIVE || $row == USER_IGNORE) { 
return array( 
'status' => LOGIN_ERROR_ACTIVE, 
'error_msg' => 'ACTIVE_ERROR', 
'user_row' => $row, 
); 
} 

// Successful login... set user_login_attempts to zero... 
return array( 
'status' => LOGIN_SUCCESS, 
'error_msg' => false, 
'user_row' => $row, 
); 
} 

找到： 
$login = $method($username, $password); 
替换为： 

```xml
if(!$admin && CAS_ENABLE) 
$login = $this->login_cas($username); 
else 
$login = $method($username, $password);
```
 

在constants.php文件中 
增加： 
//是否启用CAS 
define('CAS_ENABLE', true); 
//CAS Server 主机名 
define('CAS_SERVER_HOSTNAME', 'localhost'); 
//CAS Server 端口号 
define('CAS_SERVER_PORT', 8080); 
//CAS Server应用名 
define('CAS_SERVER_APP_NAME', 'cas');