plog 用户系统研究使用小记
前两天给x3zone架了一个plog,一个多用户的blog系统,
然后打算利用plog的会员系统来开发x3zone的其他项目,
于是开始研究plog的代码。
Begin 感叹
请允许我先感叹一句,佩服plog的开发者,面向对象用的真好,
不知道是不是借鉴了java,感觉有些,正如 bluetent 说的那样:架构设计师才是真正的设计师
End 感叹
主要是使用plog的会员部分,所以也就先研究plog的会员的代码了,
入口点是admin.php 因为用户要从此登陆
——- admin.php
核心代码:
- // initialize the session
- SessionManager::init();
- $controller = new AdminController();
看到这行:
- include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
于是打开userinfo.class.php
没有找到很有用的东西
——- admin.php
这个时候想到用户登陆的时候肯定要去数据库进行验证的,于是先进入数据库查看 `users`表,发现了password字段
于是搜索password在plog中出现的位置,在 class/dao/users.class.php找到
- function authenticateUser( $user, $pass )
- {
- $query = "SELECT * FROM ".$this->getPrefix()."users
- WHERE user = '".Db::qstr($user)."' AND password = '".md5($pass)."'
- AND status = '".USER_STATUS_ACTIVE."'";
- $result = $this->Execute( $query );
- if( $result == false )
- return false;
- if( $result->RecordCount() == 1 )
- return true;
- else
- return false;
- }
PS:妈妈的不小心碰到了电源,computer重启了,还好我有经常保存的恶习,go on…
然后还发现这个类里面的方法都很有用的,嘿嘿,作为重点保护对象
然后就是看看plog如何维持用户会话了,第一个想到的方法是看看哪里调用了 authenticateUser()这个方法,
于是乎再搜
在 class/action/admin/adminloginaction.class.php 找到了
- /**
- * Carries out the specified action
- */
- /* 注释以 ST_开头的是我加入的便于理解的注释*/
- function perform()
- {
- // get the parameters, which have already been validated
- $this->_userName = $this->_request->getValue( "userName" ); //ST_取得登陆框的用户名
- $this->_userPassword = $this->_request->getValue( "userPassword" ); //ST_取得登陆框的密码
- $this->_op = $this->_request->getValue( "op" ); //ST_取得名字为op的http值,可能是get,也可能是post
- // create a plugin manager
- $pm =& PluginManager::getPluginManager();
- // try to authenticate the user
- $users = new Users(); //ST_这里进行用户登陆的验证
- if( !$users->authenticateUser( $this->_userName, $this->_userPassword )) {
- $this->_view = new AdminDefaultView();
- $this->_view->setErrorMessage( $this->_locale->tr("error_incorrect_username_or_password"));
- $this->setCommonData();
- $pm->notifyEvent( EVENT_LOGIN_FAILURE, Array( "user" => $this->_userName ));
- return false;
- }
- //ST_验证成功才会从这里向下面进行,这里的getUserInfo会再次的进行验证,从数据库的`users`表中搜索用户名 AND 密码的数据集,如果搜索不到,意为非法用户
- // if the user is correct, get and put his or her information in the session
- $userInfo = $users->getUserInfo( $this->_userName, $this->_userPassword );
- if( !$userInfo ) {
- $this->_view = new AdminDefaultView();
- $this->_view->setErrorMessage( $this->_locale->tr("error_incorrect_username_or_password"));
- $this->setCommonData();
- $pm->notifyEvent( EVENT_LOGIN_FAILURE, Array( "user" => $this->_userName ));
- return false;
- }
- $pm->notifyEvent( EVENT_USER_LOADED, Array( "user" => &$userInfo, "from" => "Login" ));
- //ST_重要的是这里,开始记录会话了
- //$sessionInfo = $_SESSION["SessionInfo"];
- $session = HttpVars::getSession(); //ST_这里调用的类在class/net/http/httpvars.class.php
- $sessionInfo = $session["SessionInfo"];
- $sessionInfo->setValue( "userInfo", $userInfo );
- $session["SessionInfo"] = $sessionInfo;
- HttpVars::setSession( $session );
- //ST_上面几行的意思是说,先取出所有的session值放在$session数组里面,然后再从$session中取出索引为"SessionInfo"的放在$sessionInfo
- //ST_下面的就没啥用了,对这段的研究写在后面
- // get the list of blogs to which the user belongs
- $userBlogs = $users->getUsersBlogs( $userInfo->getId());
- // but if he or she does not belong to any yet, we quit
- if( empty($userBlogs)) {
- $this->_view = new AdminDefaultView();
- $this->_view->setErrorMessage( $this->_locale->tr("error_dont_belong_to_any_blog"));
- $this->setCommonData();
- return false;
- }
- $pm->notifyEvent( EVENT_BLOGS_LOADED, Array( "blogs" => &$userBlogs, "from" => "Login" ));
- $this->_view = new AdminDashboardView( $userInfo, $userBlogs );
- // better to return true if everything's fine
- return true;
- }
我先开始直接编写了一个php文件来测试session
- print_r($_SESSION);
- print_r($_COOKIE);
只在$_COOKIE得到了plogsession值,$_SESSION没有任何值,感觉有些费解。
后来依照admin.php文件的写法加入了如下的内容
- if (!defined( "PLOG_CLASS_PATH" )) {
- define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
- }
- include_once( PLOG_CLASS_PATH."class/controller/blogcontroller.class.php" );
- include_once( PLOG_CLASS_PATH."class/net/http/session/sessionmanager.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
- include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
- ini_set("arg_seperator.output", "&");
- ini_set("magic_quotes_runtime", 0 );
- SessionManager::init();
然后在刷新页面,OK
想要的都出来了,在$_SESSION数组中打印出来了如下的值,$_COOIKE值不变
- Array ( [SessionInfo] => sessioninfo Object (
- [_objId] =>
- [log] =>
- [_props] => Array ( [summaryLang] => zh_CN
- [userInfo] => userinfo Object ( [_username] => SuperTaoer
- [_password] => 8c2b2eebfeb1868afc7f98ab99cad427
- [_id] => 1
- [_aboutmyself] =>
- [_email] => supertaoer@gmail.com
- [_blogs] =>
- [_siteAdmin] => 1
- [_fullName] => TaoJing
- [_resourcePictureId] => 0
- [_resourcePicture] =>
- [_status] => 1
- [_objId] =>
- [log] =>
- [_properties] => Array ( ) )
- [blogId] => 1
- [Year] => 2005
- [Month] => 10
- [Day] => 16 ) ) )
呵呵,看到这里一目了然了,想要的都在这里面了
如果再加上下面的4行就可以实现如果用户没有登陆就会显示出登陆页面了(废话,都和admin.php的一样了~~~)
- $controller = new AdminController
- $pluginManager =& PluginManager::getPluginManager();
- $pluginManager->loadPlugins();
- $controller->process( HttpVars::getRequest(), "op");
另外,在plog的根目录发现了xmlrpc.php文件中有个函数 function getUserInfo()
可能这个也会有用吧?
好了,研究完了,http://passport.x3zone.com 要开工了,呵呵。
