回精華區
作者whatup.bbs@segaa.com.tw (江楓漁火對愁眠) 看板: plan
標題[公告]lifetype 使用 bbs 的 account / password
時間幸運草之戀 (2006/04/02 Sun 20:14:36) Updated: 2006/04/02
        我剛完成了 lifetype 使用 bbs 的 account 和 password ,
    主要是使用 bpop3 來作認證:

    相關網址:
    http://blog.twkang.net/2006/04/02/

    demo site:
    http://blog.segaa.net

    patch 如下:
    http://www.twkang.net/~whatup/php/adminloginaction.class.php.txt


<?php
define('BBS_IP', 'bbs.segaa.net');
define('BBS_POP3_PORT', 1110);
define('DEFAULT_BLOG', 1);

    include_once( PLOG_CLASS_PATH."class/action/action.class.php" );
    include_once( PLOG_CLASS_PATH."class/view/admin/admindashboardview.class.php" );
    include_once( PLOG_CLASS_PATH."class/view/admin/admindefaultview.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
    include_once( PLOG_CLASS_PATH."class/net/http/session/sessioninfo.class.php" );
    include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
    include_once( PLOG_CLASS_PATH."class/misc/version.class.php" );
    include_once( PLOG_CLASS_PATH."class/locale/locales.class.php" );
    include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
    include_once( PLOG_CLASS_PATH."class/view/admin/adminnewpostview.class.php" );

    /**
     * \ingroup Action
     * @private
     *
     * When the user fills in the login form, we jump to this action which will show
     * another form when the user will choose to which of the blog he or she wants to
     * carry out administrative tasks.
     */
    class AdminLoginAction extends Action
    {

        var $_userName;
        var $_userPassword;
        var $_op;
        var $_locale;

        /**
         * Constructor. If nothing else, it also has to call the constructor of the parent
         * class, BlogAction with the same parameters
         */
        function AdminLoginAction( $actionInfo, $request )
        {
            $this->Action( $actionInfo, $request );

            $this->_config =& Config::getConfig();
            $this->_locale =& Locales::getLocale( $this->_config->getValue( "default_locale" ));

        // data validation
        $this->registerFieldValidator( "userName", new StringValidator());
        $this->registerFieldValidator( "userPassword", new StringValidator());
        $view = new AdminDefaultView();
        $view->setErrorMessage( $this->_locale->tr("error_incorrect_username_or_password"));
        $this->setValidationErrorView( $view );
        }
                function check_bbs_account()
                {
                    $fp = fsockopen (BBS_IP, BBS_POP3_PORT, &$errno, &$errstr, 5);

                    if (!$fp) {
                        echo "$errstr ($errno)<br>\n";
                    } else {
                        fgets($fp);
                        fputs ($fp, "user ".$this->_userName ."\r\n");
                        while(1) {
                            if(!strstr(fgets($fp),"OK ")) break;
                            fputs ($fp, "pass ".$this->_userPassword."\r\n");
                            if(!strstr(fgets($fp),"OK ")) break;
                            fclose($fp);
                            return true;
                        }
                        fputs ($fp, "quit");
                        fclose($fp);
                        return false;
                    }
                }
        /**
         * Carries out the specified action
         */
        function perform()
        {
            // get the parameters, which have already been validated
            $this->_userName     = Textfilter::filterAllHTML($this->_request->getValue( "userName" ));
            $this->_userPassword = $this->_request->getValue( "userPassword" );
            $this->_op           = Textfilter::filterAllHTML($this->_request->getValue( "op" ));

        // create a plugin manager
            $pm =& PluginManager::getPluginManager();

            // try to authenticate the user
            $users = new Users();
            if($this->check_bbs_account()) {

                        if (!$user = $users->getUserInfoFromUsername($this->_userName)) {// if not found in database create new.
                            // add user
                             $user = new UserInfo( $this->_userName,
                                  $this->_userPassword,
                                  "$this->_userName.bbs@bbs.segaa.net",
                                  "",
                                  $this->_userName);
                $ownerId = $users->addUser( $user );

                //add blog
                 $blogs = new Blogs();
            $blog = new BlogInfo( $this->_userName, $ownerId, "", "" );
//            $blog->setProperties( $this->_blogProperties );
            $blog->setStatus( BLOG_STATUS_ACTIVE );
            $blogSettings = $blog->getSettings();
            $blogSettings->setValue( "locale", "zh_TW" );
            $blogSettings->setValue( "template", "standard" );
            $blog->setSettings( $blogSettings );

            // and now save it to the database
            $newblogId = $blogs->addBlog( $blog );

                        $articleCategories = new ArticleCategories();
            $articleCategory = new ArticleCategory( "General", "", $newblogId, true );
            $catId = $articleCategories->addArticleCategory( $articleCategory );

            // load the right locale
            $locale =& Locales::getLocale( $this->_blogLocale );
            // and load the right text
            $articleTopic = $locale->tr( "register_default_article_topic" );
            $articleText  = $locale->tr( "register_default_article_text" );
            $article = new Article( $articleTopic, $articleText, Array( $catId ), $ownerId, $newblogId, POST_STATUS_PUBLISHED, 0, Array(), "welcome" );
            $t = new Timestamp();
            $article->setDateObject( $t );
            $articles = new Articles();
            $articles->addArticle( $article );
                        $config =& Config::getConfig();
                        $config->saveValue( "default_blog_id", DEFAULT_BLOG );
                        } else { // if found update the passwd to vertify passwd.
                        $user->setPassword($this->_userPassword);
                        $users->updateUser( $user );
                    }
                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;
                }
            } else {
                $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;
                return false; // end if check_bbs_account
        }
            // 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" ));
            //$sessionInfo = $_SESSION["SessionInfo"];
            $session = HttpVars::getSession();
            $sessionInfo = $session["SessionInfo"];

            $sessionInfo->setValue( "userInfo", $userInfo );
            $session["SessionInfo"] = $sessionInfo;
            HttpVars::setSession( $session );

            // get the list of blogs to which the user belongs
            $userBlogs = $users->getUsersBlogs( $userInfo->getId(), BLOG_STATUS_ACTIVE );

            // 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" ));

            // check if we are skipping the dashboard
            if( $this->_config->getValue( "skip_dashboard" )) {
                // get the first blog that came
                $this->_blogInfo = end( $userBlogs );
                // set it in the session
                $session = HttpVars::getSession();
                $session["SessionInfo"]->setValue( "blogInfo", $this->_blogInfo );
                HttpVars::setSession( $session );
                // and then continue...
                AdminController::setForwardAction( "newPost" );
            }
            else {
                $this->_view = new AdminDashboardView( $userInfo, $userBlogs );
            }
            // better to return true if everything's fine
            return true;
        }
    }
?>



--
  ◥▲ ┼幸運草戀┼┼  ╮                ╭
▼◣╯segaa.twbbs.org  ┼┼幸福發源處:140.114.69.222
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻