SuperTaoer::Thanksgiving

并非人人都有明确的人生目标,可是,一旦确定,并努力去追求它,你就会过上心目中属于你的生活。而且,你会感到幸福。

十月 16, 2005

plog 用户系统研究使用小记

类归于: 程序 技术 — SuperTaoer @ 1:49 上午

前两天给x3zone架了一个plog,一个多用户的blog系统,
然后打算利用plog的会员系统来开发x3zone的其他项目,
于是开始研究plog的代码。
Begin 感叹
请允许我先感叹一句,佩服plog的开发者,面向对象用的真好,
不知道是不是借鉴了java,感觉有些,正如 bluetent 说的那样:架构设计师才是真正的设计师
End 感叹
主要是使用plog的会员部分,所以也就先研究plog的会员的代码了,
入口点是admin.php 因为用户要从此登陆
——- admin.php
核心代码:

  1. // initialize the session
  2.  SessionManager::init();
  3. $controller = new AdminController();

看到这行:

  1. include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );

于是打开userinfo.class.php
没有找到很有用的东西
——- admin.php
这个时候想到用户登陆的时候肯定要去数据库进行验证的,于是先进入数据库查看 `users`表,发现了password字段
于是搜索password在plog中出现的位置,在 class/dao/users.class.php找到

  1. function authenticateUser( $user, $pass )
  2.         {
  3.             $query = "SELECT * FROM ".$this->getPrefix()."users
  4.                       WHERE user = '".Db::qstr($user)."' AND password = '".md5($pass)."'
  5.                             AND status = '".USER_STATUS_ACTIVE."'";
  6.             $result = $this->Execute( $query );
  7.             if( $result == false )
  8.                 return false;
  9.             if( $result->RecordCount() == 1 )
  10.                 return true;
  11.             else
  12.                 return false;
  13.         }

PS:妈妈的不小心碰到了电源,computer重启了,还好我有经常保存的恶习,go on…
然后还发现这个类里面的方法都很有用的,嘿嘿,作为重点保护对象
然后就是看看plog如何维持用户会话了,第一个想到的方法是看看哪里调用了 authenticateUser()这个方法,
于是乎再搜
在 class/action/admin/adminloginaction.class.php 找到了

  1. /**
  2.          * Carries out the specified action
  3.          */
  4.       /* 注释以 ST_开头的是我加入的便于理解的注释*/
  5.         function perform()
  6.         {
  7.             // get the parameters, which have already been validated
  8.             $this->_userName     = $this->_request->getValue( "userName" );        //ST_取得登陆框的用户名
  9.             $this->_userPassword = $this->_request->getValue( "userPassword" );    //ST_取得登陆框的密码
  10.             $this->_op           = $this->_request->getValue( "op" );                //ST_取得名字为op的http值,可能是get,也可能是post
  11.         // create a plugin manager
  12.             $pm =& PluginManager::getPluginManager();   
  13.             // try to authenticate the user
  14.             $users = new Users();        //ST_这里进行用户登陆的验证
  15.             if( !$users->authenticateUser( $this->_userName, $this->_userPassword )) {
  16.                 $this->_view = new AdminDefaultView();
  17.                 $this->_view->setErrorMessage( $this->_locale->tr("error_incorrect_username_or_password"));
  18.                 $this->setCommonData();
  19.                 $pm->notifyEvent( EVENT_LOGIN_FAILURE, Array( "user" => $this->_userName ));
  20.                 return false;
  21.             }
  22.             //ST_验证成功才会从这里向下面进行,这里的getUserInfo会再次的进行验证,从数据库的`users`表中搜索用户名 AND 密码的数据集,如果搜索不到,意为非法用户
  23.             // if the user is correct, get and put his or her information in the session
  24.             $userInfo = $users->getUserInfo( $this->_userName, $this->_userPassword );
  25.             if( !$userInfo ) {
  26.                 $this->_view = new AdminDefaultView();
  27.                 $this->_view->setErrorMessage( $this->_locale->tr("error_incorrect_username_or_password"));
  28.                 $this->setCommonData();
  29.                 $pm->notifyEvent( EVENT_LOGIN_FAILURE, Array( "user" => $this->_userName ));
  30.                 return false;
  31.             }
  32.         $pm->notifyEvent( EVENT_USER_LOADED, Array( "user" => &$userInfo, "from" => "Login" ));
  33.             //ST_重要的是这里,开始记录会话了
  34.             //$sessionInfo = $_SESSION["SessionInfo"];
  35.             $session = HttpVars::getSession();        //ST_这里调用的类在class/net/http/httpvars.class.php
  36.             $sessionInfo = $session["SessionInfo"];
  37.             $sessionInfo->setValue( "userInfo", $userInfo );
  38.             $session["SessionInfo"] = $sessionInfo;
  39.             HttpVars::setSession( $session );
  40.          //ST_上面几行的意思是说,先取出所有的session值放在$session数组里面,然后再从$session中取出索引为"SessionInfo"的放在$sessionInfo
  41.          //ST_下面的就没啥用了,对这段的研究写在后面
  42.             // get the list of blogs to which the user belongs
  43.             $userBlogs = $users->getUsersBlogs( $userInfo->getId());
  44.             // but if he or she does not belong to any yet, we quit
  45.             if( empty($userBlogs)) {
  46.                 $this->_view = new AdminDefaultView();
  47.                 $this->_view->setErrorMessage( $this->_locale->tr("error_dont_belong_to_any_blog"));
  48.                 $this->setCommonData();
  49.                 return false;
  50.             }
  51.             $pm->notifyEvent( EVENT_BLOGS_LOADED, Array( "blogs" => &$userBlogs, "from" => "Login" ));           
  52.             $this->_view = new AdminDashboardView( $userInfo, $userBlogs );
  53.             // better to return true if everything's fine
  54.             return true;
  55.         }

我先开始直接编写了一个php文件来测试session

  1. print_r($_SESSION);
  2. print_r($_COOKIE);

只在$_COOKIE得到了plogsession值,$_SESSION没有任何值,感觉有些费解。
后来依照admin.php文件的写法加入了如下的内容

  1. if (!defined( "PLOG_CLASS_PATH" )) {
  2.     define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
  3. }
  4. include_once( PLOG_CLASS_PATH."class/controller/blogcontroller.class.php" );
  5. include_once( PLOG_CLASS_PATH."class/net/http/session/sessionmanager.class.php" );
  6. include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
  7. include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
  8. include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
  9. ini_set("arg_seperator.output", "&");
  10. ini_set("magic_quotes_runtime", 0 );
  11. SessionManager::init();

然后在刷新页面,OK
想要的都出来了,在$_SESSION数组中打印出来了如下的值,$_COOIKE值不变

  1. Array ( [SessionInfo] => sessioninfo Object (
  2. [_objId] =>
  3. [log] =>
  4. [_props] => Array ( [summaryLang] => zh_CN
  5.                     [userInfo] => userinfo Object ( [_username] => SuperTaoer
  6.                                                 [_password] => 8c2b2eebfeb1868afc7f98ab99cad427
  7.                                                 [_id] => 1
  8.                                                 [_aboutmyself] =>
  9.                                                 [_email] => supertaoer@gmail.com
  10.                                                 [_blogs] =>
  11.                                                 [_siteAdmin] => 1
  12.                                                 [_fullName] => TaoJing
  13.                                                 [_resourcePictureId] => 0
  14.                                                 [_resourcePicture] =>
  15.                                                 [_status] => 1
  16.                                                 [_objId] =>
  17.                                                 [log] =>
  18.                                                 [_properties] => Array ( ) )
  19.                     [blogId] => 1
  20.                     [Year] => 2005
  21.                     [Month] => 10
  22.                     [Day] => 16 ) ) )

呵呵,看到这里一目了然了,想要的都在这里面了
如果再加上下面的4行就可以实现如果用户没有登陆就会显示出登陆页面了(废话,都和admin.php的一样了~~~)

  1. $controller = new AdminController
  2. $pluginManager =& PluginManager::getPluginManager();
  3. $pluginManager->loadPlugins();
  4. $controller->process( HttpVars::getRequest(), "op");

另外,在plog的根目录发现了xmlrpc.php文件中有个函数 function getUserInfo()
可能这个也会有用吧?
好了,研究完了,http://passport.x3zone.com 要开工了,呵呵。



Leave a Message

No Messages

No Messages

RSS feed for comments on this post. TrackBack URI

Leave a Message





Powered by WordPress