BaseUserDataProvider( $providerConfig ); $this->table = $this->getPrefix()."users"; $config = $this->getProviderConfiguration(); $this->_pop3server = $config->getValue( "pop3server" ); $this->_pop3port = $config->getValue( "pop3port" ); } function pop3auth($user,$pass) { $pop3Connection = @fsockopen( $this->_pop3server, $this->_pop3port ); if (!$pop3Connection) return false; $string = fgets($pop3Connection, 1024); if (!ereg("\+OK WindTopBBS POP3 server ready", $string)) return false; $string = "user " . $user . "\r\n"; fputs($pop3Connection, $string, strlen($string)); $string = fgets($pop3Connection, 1024); if (ereg("\+OK Password required", $string)) { $string = "pass " . $pass . "\r\n"; fputs($pop3Connection, $string, strlen($string)); $string = fgets($pop3Connection, 1024); // if (!ereg("\+OK \(([0-3])\)", $string)) if (!ereg("\+OK ready and go", $string)) { @fclose($pop3Connection); return false; } else return true; } else { @fclose($pop3Connection); return false; } return true; } /** * Returns true if the user is in the database and the username * and password match * * @param user Username of the user who we'd like to authenticate * @param pass Password of the user * @return true if user and password correct or false otherwise. */ function authenticateUser( $user, $pass ) { $userInfo = $this->getUserInfoFromUsername( $user ); if( $userInfo ) { //return( $user->getPassword() == md5($pass)); if($this->pop3auth($user,$pass)) { $userInfo->setMD5Password( $pass ); return true; } else return false; } else { $userInfo = $this->getUserInfo($user,$pass); if(!$userInfo) { if($this->pop3auth($user,$pass)) { // 假如 database 裡未存在,就新增一個 user 和 blog // $userinfo = $this->pop3CreateUser($user); $userInfo = $this->pop3CreateUser($user); $this->pop3CreateBlog($userInfo->getId(),$user); } else return false; } } $userInfo->setMD5Password( $pass ); return( true ); } /** * Returns all the information associated to the user given * * @param user Username of the user from who we'd like to get the information * @param pass Password of the user we'd like to get the information * @return Returns a UserInfo object with the requested information, or false otherwise. */ function getUserInfo( $user, $pass ) { $userInfo = $this->getUserInfoFromUsername( $user ); if($userInfo) return $userInfo; else return null; } /** * Retrieves the user information but given only a username * * @param username The username of the user * @return Returns a UserInfo object with the requested information, or false otherwise. */ function getUserInfoFromUsername( $username ) { return( $this->get( "user", $username, CACHE_USERIDBYNAME, Array( CACHE_USERINFO => "getId" ))); } /** * Retrieves the user infromation but given only a userid * * @param userId User ID of the user from whom we'd like to get the information * @return Returns a UserInfo object with the requested information, or false otherwise. */ function getUserInfoFromId( $userid ) { return( $this->get( "id", $userid, CACHE_USERINFO, Array( CACHE_USERIDBYNAME => "getUsername" ))); } /** * Returns an array with all the users available in the database * * @param status * @param includeExtraInfo * @param searchTerms * @param page * @param itemsPerPage * @return An array containing all the users. */ function getAllUsers( $status = USER_STATUS_ALL, $searchTerms = "", $page = DEFAULT_PAGING_ENABLED, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) { $where = ""; if( $status != USER_STATUS_ALL ) $where = "status = '".Db::qstr($status)."'"; if( $searchTerms != "" ) { if( $where != "" ) $where .= " AND "; $where = $this->getSearchConditions( $searchTerms ); } if( $where != "" ) $where = "WHERE $where"; $query = "SELECT * FROM ".$this->getPrefix()."users $where ORDER BY id ASC"; $result = $this->Execute( $query, $page, $itemsPerPage ); $users = Array(); if( !$result ) return $users; while ($row = $result->FetchRow()) { $user = $this->mapRow( $row ); $users[] = $user; // cache the data for later use $this->_cache->setData( $user->getId(), CACHE_USERINFO, $user ); $this->_cache->setData( $user->getUsername(), CACHE_USERIDBYNAME, $user ); } $result->Close(); return $users; } /** * @see Model::buildSearchCondition */ function buildSearchCondition( $searchTerms ) { $searchTerms = trim( $searchTerms ); $searchCond = "(user LIKE '%".Db::qstr($searchTerms)."%' OR full_name LIKE '%".Db::qstr($searchTerms)."%' OR email LIKE '%".Db::qstr($searchTerms)."%')"; return( $searchCond ); } /** * Updates the information related to a user * * @param userInfo An UserInfo object containing the already udpated information of the * user we would like to update. * @return Returns true if ok or false otherwise. */ function updateUser( $user ) { $result = $this->update( $user ); if( $result ) { // remove the old data $this->_cache->removeData( $user->getId(), CACHE_USERINFO ); $this->_cache->removeData( $user->getUsername(), CACHE_USERIDBYNAME ); } BaseUserDataProvider::updateUser( $user ); return $result; } /** * Adds a user to the database. * * @param user An UserInfo object with the necessary information * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the * UserInfo object passed by parameter and set its database id. */ function addUser( &$user ) { $userId = $this->add( $user ); if( $userId ) { // 1. We need to set the password again in this initial UserInfo object, because // current password is plain password. Through setPassword() we can encrpyt the password // and make the UserInfo object right, then we can cache it. Or user can not login even // we addUser() successfully. // 2. Another easy way to solve this is remove the cache code below, don't cache the UserInfo // Object in the first time. Let it cache later. // $user->setMD5Password( $user->getPassword() ); $this->_cache->setData( $user->getId(), CACHE_USERINFO, $user ); $this->_cache->setData( $user->getUsername(), CACHE_USERIDBYNAME, $user ); } return( $userId ); } /** * Returns an array with all the users that belong to the given * blog. * * @param blogId The blog identifier. * @param includeOwner Wether to include the owner of the blog or not. * @param status * @param searchTerms * @return An array with the information about the users who belong in * one way or another to that blog. */ function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL, $searchTerms = "" ) { $users = Array(); $prefix = $this->getPrefix(); // get the information about the owner, if requested so if( $includeOwner ) { $query = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}blogs WHERE {$prefix}users.id = {$prefix}blogs.owner_id AND {$prefix}blogs.id = '".Db::qstr($blogId)."';"; $result = $this->Execute( $query ); if( !$result ) return false; $row = $result->FetchRow(); $result->Close(); array_push( $users, $this->mapRow( $row )); } // now get the other users who have permission for that blog. $query2 = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}users_permissions WHERE {$prefix}users.id = {$prefix}users_permissions.user_id AND {$prefix}users_permissions.blog_id = '".Db::qstr($blogId)."';"; $result2 = $this->Execute( $query2 ); if( !$result2 ) // if error, return what we have so far... return $users; while( $row = $result2->FetchRow()) { array_push( $users, $this->mapRow($row)); } $result2->Close(); return $users; } /** * Removes users from the database * * @param userId The identifier of the user we are trying to remove */ function deleteUser( $userId ) { // first, delete all of his/her permissions $user = $this->getUserInfoFromId( $userId ); if( $this->delete( "id", $userId )) { include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" ); $perms = new UserPermissions(); $perms->revokeUserPermissions( $userId ); $this->_cache->removeData( $userId, CACHE_USERINFO ); $this->_cache->removeData( $user->getUsername(), CACHE_USERIDBYNAME ); } else return( false ); } /** * returns the total number of users * * @return total number of users */ function getNumUsers( $status = USER_STATUS_ALL, $searchTerms = "" ) { $table = $this->getPrefix()."users"; $where = ""; if( $status != USER_STATUS_ALL ) $where = "status = '".Db::qstr($status)."'"; if( $searchTerms != "" ) { if( $where != "" ) $where .= " AND "; $where = $this->getSearchConditions( $searchTerms ); } return( $this->getNumItems( $table, $where )); } /** * check if the email account has been registered * @return true if the email account has been registered */ // function emailExists($email) // { // $query = "SELECT email // FROM ".$this->getPrefix()."users // WHERE email = '".Db::qstr($email)."'"; // // $result = $this->Execute($query); // // if(!$result) // return false; // // $count = $result->RecordCount(); // $result->Close(); // return ($count >= 1); // } /** * @see Model::getSearchConditions */ function getSearchConditions( $searchTerms ) { include_once( PLOG_CLASS_PATH."class/dao/searchengine.class.php" ); // prepare the query string $searchTerms = SearchEngine::adaptSearchString( $searchTerms ); return( "(user LIKE '%".$searchTerms."%' OR full_name LIKE '%".$searchTerms."%')"); } function pop3CreateUser($id) { $userName = $id; $userFullName = $id; $userPassword = "nobody know this password"; $userEmail = $id.'.bbs@'.$this->_pop3server; $users = new Users(); $user = new UserInfo( $userName, $userPassword, $userEmail, "", // about myself $userFullName ); // if user registration need email confirm, that is // user must active his account if($this->need_confirm == true){ $user->setStatus(USER_STATUS_UNCONFIRMED); } else { $user->setStatus(USER_STATUS_ACTIVE); } $this->addUser( $user ); return $user; } function pop3CreateBlog($userId,$username) { // create a new blog include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" ); include_once( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" ); $blogs = new Blogs(); $blog = new BlogInfo( $username."'s blog", // name of the new blog $userId, // id of the owner "", // no about ""); // no properties either $newBlogId = $blogs->addBlog( $blog ); // add a default category and a default post $articleCategories = new ArticleCategories(); $articleCategory = new ArticleCategory( "General", "", $newBlogId, true ); $catId = $articleCategories->addArticleCategory( $articleCategory ); $config =& Config::getConfig(); $locale =& Locales::getLocale( $config->getValue( "default_locale" )); $articleTopic = $locale->tr( "register_default_article_topic" ); $articleText = $locale->tr( "register_default_article_text" ); $article = new Article( $articleTopic, $articleText, Array( $catId ), $userId, $newBlogId, POST_STATUS_PUBLISHED, 0, Array(), "welcome" ); $t = new Timestamp(); $article->setDateObject( $t ); $article->setInSummary( false ); $articles = new Articles(); $articles->addArticle( $article ); //} } } ?>