歡迎光臨, 訪客. 請先 登入註冊一個帳號.
三月 28, 2024, 10:42:01 下午
19595 文章 在 3865 主題 由 4580 會員
最新註冊會員: aa123aa1
LifeType 中文開發論壇  |  開發  |  外掛程式  |  外掛程式精華區  |  [分享] 透過 XMLRPC 來管理PLog帳號和 Blog « 上篇主題 下篇主題 »
頁: [1]
作者 主題: [分享] 透過 XMLRPC 來管理PLog帳號和 Blog  (閱讀 27943 次)
AlexD
新手見習
*
文章: 10


檢視個人資料
« 於: 十月 18, 2005, 12:56:10 下午 »

之前在Mark兄的幫忙下, 寫了一些給 XMLRPC 呼叫來管理PLog 的帳號跟Blog 的功能, 現在與大家分享一下, 大概說明一下, 主要是提供[Add User], [Set Password], [Disable User], [Enable User],[Add Blog], [Disable Blog], [Enable Blog] 這幾個功能, 每個功能在呼叫時都必須傳入 Site Admin 的帳號和密碼, 確認是  Site Admin 才可以做這些事情, 我是直接拿 /plog/xmlrpc.php  這個程式來改的, 因為它已經將一些基本的工作都做好了, 在這個程式中  [config object][users object], [articles object][blog object] 都已經建好了, 在Function 中如果需要用到, 則直接利用 global $users, $articles, $blogsG; 這樣的語法就可以直接拿來用了, 蠻簡單的, 不過也是花了一些時間還有請教Mark兄才完成, 以下就是這些Code

// 新增 User
    function myaddUser($args){
        global $users;
        $adminname      = $args[0];  // Site Admin的帳號
        $adminpassword  = $args[1];  // Site Admin 的密碼
        $username       = $args[2];     //New User 帳號
        $userpassword   = $args[3];    //New User 密碼
        $email          = $args[4];          //New User EMail
        $aboutMyself    = $args[5];    //New User 的 AboutMyself
        $fullname       = $args[6];      //New User 的全名


        $erg = $users->getUserInfo(
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {
                        $newUserInfo=new UserInfo($username, $userpassword, $email, $aboutMyself, $fullname, 0, null, 0);
                        $UserID=$users->addUser($newUserInfo);
                        if ($UserID != -1) {
                                return sprintf( "%d", $UserID );
                        }
                        else {
                                return new IXR_Error(-1, 'Error where create a new user!');
                        }
                }
                else {
                        return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }



// 關閉 User
    function mydisableUser($args){
        global $users;
        $adminname      = $args[0];
        $adminpassword  = $args[1];
        $userID       = $args[2];           // 要關閉的 User 的ID

        $erg = $users->getUserInfo(
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {

                        $targetUser = $users->getUserInfoFromId (
                                $userID,
                                null
                        );

                        if ($targetUser != false) {                               
                                if(!($users->disableUser($userID))) {
                                    return new IXR_Error(-1, 'Error when disable the user!');
                                }
                                return true;
                        }
                        else {
                                return new IXR_Error(-1, 'This user doesn\'t exist!');
                        }
                }
                else {
                        return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }



//開啟 User
    function myenableUser($args){
        global $users;
        $adminname      = $args[0];
        $adminpassword  = $args[1];
        $userID       = $args[2];    //要開啟的User ID

        $erg = $users->getUserInfo(
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {

                        $targetUser = $users->getUserInfoFromId (
                                $userID,
                                null
                        );

                        if ($targetUser != false) {
                                $targetUser->setStatus(USER_STATUS_ACTIVE);
                                if(!($users->updateUser( $targetUser))) {
                                    return new IXR_Error(-1, 'Error when enable the user!');
                                }

                                return true;
                        }
                        else {
                                return new IXR_Error(-1, 'This user doesn\'t exist!');
                        }
                }
                else {
                        return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }


// 設定密碼
    function mysetPassword($args){
        global $users;
        $adminname      = $args[0];
        $adminpassword  = $args[1];
        $userID         = $args[2];        //要設定密碼的User ID
        $newpassword    = $args[3];   //新密碼

        $erg = $users->getUserInfo(
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {
                        $targetUser = $users->getUserInfoFromId (
                                $userID,
                                null
                        );

                        if ($targetUser != false) {
                                $targetUser->setPassword($newpassword);
                                if(!($users->updateUser( $targetUser))) {
                                    return new IXR_Error(-1, 'Error when update the user password!');
                                }
                                return true;
                        }
                        else {
                                 return new IXR_Error(-1, 'This user doesn\'t exist!');
                        }
                }
                else {
                        return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }


//新增 Blog
    function myaddBlog($args){
        global $users, $blogsG;
        $adminname   = $args[0];
        $adminpassword   = $args[1];
        $blog = $args[2];                 // New Blog 的名稱
        $owner = $args[3];              // New Blog 的Owner ID, 就是 User ID
        $about = $args[4];              // New Blog 的 About
        $erg = $users->getUserInfo( 
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {
                        $newBlogInfo=new BlogInfo($blog, $owner, $about, null, $id = -1 );
                        $newBlogId= $blogsG->addBlog($newBlogInfo);

                       // add a default category and a default post
                        $articlmyategories = new Articlmyategories();
                        $articlmyategory = new Articlmyategory( "General", "", $newBlogId, true );
                        $catId = $articlmyategories->addArticlmyategory( $articlmyategory );
                        $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 ),
                                    $owner,
                                    $newBlogId,
                                    POST_STATUS_PUBLISHED,
                                    0,
                                    Array(),
                                    "welcome" );
                        $t = new Timestamp();
                        $article->setDateObjmyt( $t );
                        $articles = new Articles();
                        $articles->addArticle( $article );

                        if ($BlogID != -1) {
                                return sprintf( "%d", $newBlogId );
                        }
                        else {
                                return new IXR_Error(-1, 'Error when create a new blog!');
                        }
                }
                else {
                         return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }



//關閉 Blog
    function mydisableBlog($args){
        global $users, $blogsG;
        $adminname      = $args[0];
        $adminpassword  = $args[1];
        $blogID       = $args[2];   //要關閉的 Blog ID

        $erg = $users->getUserInfo(
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {
                        $targetBlog = $blogsG->getBlogInfo (
                                $blogID,
                                null
                        );

                        if ($targetBlog != false) {
                                if(!( $blogsG->disableBlog($blogID))) {
                                    return new IXR_Error(-1, 'Error when disable the blog!');
                                }
                                return true;
                        }
                        else {
                                return new IXR_Error(-1, 'This blog doesn\'t exist!');
                        }
                }
                else {
                        return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }



//開啟 Blog
    function myenableBlog($args){
        global $users, $blogsG;
        $adminname      = $args[0];
        $adminpassword  = $args[1];
        $blogID       = $args[2];  //要開啟的 BLog ID

        $erg = $users->getUserInfo(
            $adminname,
            $adminpassword
        );
        if ($erg != false)
        {
                if ($erg->isSiteAdmin()) {
                        $targetBlog = $blogsG->getBlogInfo (
                                $blogID,
                                null
                        );

                        if ($targetBlog != false) {
                                $targetBlog->setStatus(BLOG_STATUS_ACTIVE);
                                if(!($blogsG->updateBlog($targetBlog->getId(),$targetBlog))) {
                                    return new IXR_Error(-1, 'Error when enable the blog!');
                                }

                                return true;
                        }
                        else {
                                return new IXR_Error(-1, 'This blog doesn\'t exist!');
                        }
                }
                else {
                        return new IXR_Error(-1, 'Sorry! you are not the Site Admin.');
                }
        }
        else {
                return new IXR_Error(-1, 'You did not provide the corrmyt password');
        }
    }


在這個程式的最後一段, 再加上底下黑體的部份, 這樣就可以了

    if ($config->getValue("xmlrpc_api_enabled"))
    {
        $xmlrpc = new IXR_Server(
            array (
                "blogger.newPost"           => "newPost",
                "blogger.getUserInfo"       => "getUserInfo",
                "blogger.getPost"           => "getPost",
                "blogger.editPost"          => "editPost",
                "blogger.deletePost"        => "deletePost",
                "blogger.getRmyentPosts"    => "getRmyentPosts",
                "blogger.getUserInfo"       => "getUserInfo",

                "blogger.addUser"           => "myaddUser",
                "blogger.setPassword"       => "mysetPassword",
                "blogger.disableUser"       => "mydisableUser",
                "blogger.enableUser"       => "myenableUser",
                "blogger.addBlog"           => "myaddBlog",
                "blogger.disableBlog"       => "mydisableBlog",
                "blogger.enableBlog"       => "myenableBlog",


                "blogger.getUsersBlogs"     => "getUsersBlogs",
            "metaWeblog.newPost"        => "metaWeblogNewPost",
            "metaWeblog.editPost"       => "metaWeblogEditPost",
            "metaWeblog.getPost"        => "metaWeblogGetPost",
            "metaWeblog.getRmyentPosts" => "metaWeblogGetRmyentPosts",
            "metaWeblog.getCategories"  => "metaWeblogGetCategories",
            "metaWeblog.newMediaObjmyt" => "metaWeblogNewMediaObjmyt"
            )


補充說明,
1. 有關新增  Blog 的部份, 可以參考 pLog/class/action/admin/adminaddblogaction.class.php 這支程式
2. 有關加入新分類與文章的部分請參考 pLog/class/action/admin/adminaddblogaction.class.php
3. 原討論在 http://forum.lifetype.org.tw/index.php?topic=1255.0
4. 透過API 可以做很多事情, 但是還是要注意一下是否會跟原有設計概念有不一致的地方, 比方說密碼的長度要求, 或是新增一個 BLog 時, 也會要求給這個 Blog 一個分類和建立新的一篇文章等等
5. 如果需要寫 PHP 的 xmlrpc 的 Client, 可以參考  http://phpxmlrpc.sourceforge.net/, 下載下來解開後, 就可以拿裡面的 Sample 來改了, 我改了一個用來測試寫的程式對不對, 這邊也提供給大家參考(如下), 其中
       a. $f=new xmlrpcmsg('blogger.addUser'.... 對應的就是剛剛我們在程式最後一段加上去的給外面呼叫的 interface, 後面就是傳入的參數了, 一一對應上面 function 的參數
       b.  iconv("BIG5", "UTF-8",$HTTP_POST_VARS["aboutmyself"]) 是用來先將 BIG 5 轉成 UTF-8 的格式, 這樣傳入xmlrpc Server 端的程式時, 才不會有中文亂碼的問題


************************************************************************************

<?php
include("xmlrpc.inc");

if ($HTTP_POST_VARS["submit"]=="addUser")
{
    $f=new xmlrpcmsg('blogger.addUser',
    array(new xmlrpcval($HTTP_POST_VARS["adminname"], "string"),new xmlrpcval($HTTP_POST_VARS["adminpassword"], "string"),new xmlrpcval($HTTP_POST_VARS["username"], "string"),new xmlrpcval($HTTP_POST_VARS["userpassword"], "string"), new xmlrpcval($HTTP_POST_VARS["email"], "string"),new xmlrpcval(iconv("BIG5", "UTF-8",$HTTP_POST_VARS["aboutmyself"]), "string"),new xmlrpcval(iconv("BIG5", "UTF-8",$HTTP_POST_VARS["fullname"]), "string")));
    $c=new xmlrpc_client("/plog/plog-1.0.1/myAgent.php", "172.18.100.100", 80);
    $c->setDebug(0);
    $r=$c->send($f);
    $v=$r->value();
    if (!$r->faultCode())
    {
        print "New UserID ". $HTTP_POST_VARS["newusername"] . " is " .
        $v->scalarval() . "<BR>";
        print "<HR>I got this value back<BR><PRE>" .
        htmlentities($r->serialize()). "</PRE><HR>\n";
      }
    else
    {
        print "Fault: ";
        print "Code: " . $r->faultCode() .
        " Reason '" .$r->faultString()."'<BR>";
    }
}

print "<FORM  METHOD=\"POST\">
adminusername <INPUT NAME=\"adminname\" VALUE=\"${adminname}\">
<br>adminpassword <INPUT NAME=\"adminpassword\" VALUE=\"${adminpassword}\"><br><br>

username <INPUT NAME=\"username\" VALUE=\"${username}\"><br>
userpassword <INPUT NAME=\"userpassword\" VALUE=\"${userpassword}\"><br>
email <INPUT NAME=\"email\" VALUE=\"${email}\"><br>
aboutMyself <INPUT NAME=\"aboutmyself\" VALUE=\"${aboutmyself}\"><br>
full name <INPUT NAME=\"fullname\" VALUE=\"${fullname}\"><br><br>

<input type=\"submit\" value=\"addUser\" name=\"submit\">
</FORM><P>
enter a new user";
?>

************************************************************************************

6. 以上如果有不正確的地方還請各位先進不吝指教


« 最後編輯時間: 十月 18, 2005, 12:58:36 下午 由 AlexD » 已記錄
markwu
系統管理員
超級會員
*****
文章: 3928


Mark Wu


檢視個人資料 個人網站
« 回覆文章 #1 於: 十月 18, 2005, 07:25:03 下午 »

Hi Alex:

太謝謝您了!我把這篇文章移進精華區中。

這真的是經典的的 XMLRPC 擴充範例! 很棒

Mark
已記錄

AlexD
新手見習
*
文章: 10


檢視個人資料
« 回覆文章 #2 於: 十月 19, 2005, 08:18:59 上午 »

希望對有需要的人可以提供一些幫助   我只是貢獻小小的心力而已  微笑
已記錄
laughtosky
新手見習
*
文章: 20


檢視個人資料
« 回覆文章 #3 於: 十月 20, 2005, 05:47:49 下午 »

很有用~~
我慢慢x消化

已記錄
wangsan
新手見習
*
文章: 2


檢視個人資料
« 回覆文章 #4 於: 十一月 13, 2005, 01:29:27 下午 »

AlexD 兄:

很感謝您發表了這篇分享,這對我真是非常有用。
但是有些問題想請教,請能撥空說明一下,謝謝。

我是使用ASP.NET開發系統的,由於在系統內被要求必需有Blog的功能。
找來找去找到了這個pLog的程式,目前雖然已經架設起來了,但是一直在
煩腦如何將這個Blog與自己開發的系統作整合。今天無意發現了您所發表的文章,
這正是我所需要的。由於我對PHP只是懂得一點皮毛。(只會下載程式架設起來而已)

想請教的是:

1.是不是開啟在架好的blog目錄下的xmlrpc.php,然後將您所提供的幾個function,加入到該檔案裏面,
然後在該檔案底下的「$xmlrpc = new IXR_Server(......」內加入您所說的黑體的部份。

2.在ASP.NET中如何呼叫這些Function,來操作新增User...,等功能。

因為在您前一篇文章中您也提到,您也是要與ASP.NET開發的系統作整合的,所以才這問您。

謝謝
          
已記錄
AlexD
新手見習
*
文章: 10


檢視個人資料
« 回覆文章 #5 於: 十一月 14, 2005, 05:23:18 下午 »

我已經有回覆 PM 給您了,  但還是在這邊詳細說一下, 如果其它人也有需要, 可以參考一下,

您可以看一下 http://www.xml-rpc.net/, 這是給 ASP.NET 使用的 XML-RPC 的library,
它提供 XML-RPC client 和 XML-RPC Service 的 library, 我只有使用到 ASP.NET  來當 Client,
XML-RPC Service 就是 Copy 原來在架好的blog目錄下的xmlrpc.php 來改(Copy 成另外一個檔案"myAgent.php") ,
再加上我寫的程式, XML-RPC Server 端的部份就算完成了

然後, 為了避免開發時的複雜度, 我先用XML-RPC Server 所在的機器上,
寫個小小的範例在 PHP 上模擬  XML-RPC Client, 我有列在最後面,  你可以 Copy 那段到另外新的PHP 網頁, 
紅色的部份就是您可能要修改對應的地方
    $c=new xmlrpc_client("/plog/plog-1.0.1/myAgent.php", "172.18.100.100", 80);

而在它前一行主是要用來告知要呼叫遠端 Server  的那一個 interface還有要傳進去的參數, 參數的類型等等, 這個可以不需要修改,
這裡只有測試 blogger.addUser的功能是否正常而已,如果你想測試其他的功能, 可以在自行修改
   $f=new xmlrpcmsg('blogger.addUser',
    array(new xmlrpcval($HTTP_POST_VARS["adminname"], "string"),new xmlrpcval($HTTP_POST_VARS["adminpassword"], "string"),new xmlrpcval($HTTP_POST_VARS["username"], "string"),new xmlrpcval($HTTP_POST_VARS["userpassword"], "string"), new xmlrpcval($HTTP_POST_VARS["email"], "string"),new xmlrpcval(iconv("BIG5", "UTF-8",$HTTP_POST_VARS["aboutmyself"]), "string"),new xmlrpcval(iconv("BIG5", "UTF-8",$HTTP_POST_VARS["fullname"]), "string")));

測試如果OK,  您可以再由 ASP.NET  寫程式來取代上面這個 PHP Client 的程式, 參考  http://www.xml-rpc.net/ 的部份, 事實上,
這個也有範例程式, 您也可以拿一個 Client 的範例來改寫, 這樣可能會比較快

順便一提, 最好在XML-RPC Service的PHP 程式裡面去限制只有哪些 IP 可以連進來, 要不然如果放在 Public domain, 還是有可能會讓人入侵
如果還有問題, 歡迎提出來大家討論
已記錄
頁: [1]
LifeType 中文開發論壇  |  開發  |  外掛程式  |  外掛程式精華區  |  [分享] 透過 XMLRPC 來管理PLog帳號和 Blog « 上篇主題 下篇主題 »
    前往: