之前在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. 以上如果有不正確的地方還請各位先進不吝指教