X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=demo%2Fserver%2Fdiscuss.php;h=ca5dd7d7dace87a2c325f804a5f7219702a74ad4;hb=b59f82d97356e3777e2630cb6640382f42ad9670;hp=70723059bab34ffa8ef0fb0fcd2b6d4dbb849700;hpb=05102eb7d8c0faad30ae3719e2045c1af72b9e9d;p=plcapi.git diff --git a/demo/server/discuss.php b/demo/server/discuss.php index 7072305..ca5dd7d 100644 --- a/demo/server/discuss.php +++ b/demo/server/discuss.php @@ -1,124 +1,104 @@ decode($req); + $msgID = $n[0]; + $name = $n[1]; + $comment = $n[2]; - $dbh=dba_open("/tmp/comments.db", "c", "db2"); - if($dbh) - { - $countID="${msgID}_count"; - if(dba_exists($countID, $dbh)) - { - $count=dba_fetch($countID, $dbh); - } - else - { - $count=0; - } - // add the new comment in - dba_insert($msgID . "_comment_${count}", $comment, $dbh); - dba_insert($msgID . "_name_${count}", $name, $dbh); - $count++; - dba_replace($countID, $count, $dbh); - dba_close($dbh); - } - else - { - $err="Unable to open comments database."; - } - // if we generated an error, create an error return response - if($err) - { - return new xmlrpcresp(0, $xmlrpcerruser, $err); - } - else - { - // otherwise, we create the right response - // with the state name - return new xmlrpcresp(new xmlrpcval($count, "int")); - } - } + $dbh = dba_open("/tmp/comments.db", "c", "db2"); + if ($dbh) { + $countID = "${msgID}_count"; + if (dba_exists($countID, $dbh)) { + $count = dba_fetch($countID, $dbh); + } else { + $count = 0; + } + // add the new comment in + dba_insert($msgID . "_comment_${count}", $comment, $dbh); + dba_insert($msgID . "_name_${count}", $name, $dbh); + $count++; + dba_replace($countID, $count, $dbh); + dba_close($dbh); + } else { + $err = "Unable to open comments database."; + } + // if we generated an error, create an error return response + if ($err) { + return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); + } else { + // otherwise, we create the right response + return new PhpXmlRpc\Response(new PhpXmlRpc\Value($count, "int")); + } +} - $getcomments_sig=array(array($xmlrpcArray, $xmlrpcString)); +$getComments_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcString)); - $getcomments_doc='Returns an array of comments for a given ID, which +$getComments_doc = 'Returns an array of comments for a given ID, which is the sole argument. Each array item is a struct containing name and comment text.'; - function getcomments($m) - { - global $xmlrpcerruser; - $err=""; - $ra=array(); - // get the first param - if(XMLRPC_EPI_ENABLED == '1') - { - $msgID=xmlrpc_decode($m->getParam(0)); - } - else - { - $msgID=php_xmlrpc_decode($m->getParam(0)); - } - $dbh=dba_open("/tmp/comments.db", "r", "db2"); - if($dbh) - { - $countID="${msgID}_count"; - if(dba_exists($countID, $dbh)) - { - $count=dba_fetch($countID, $dbh); - for($i=0; $i<$count; $i++) - { - $name=dba_fetch("${msgID}_name_${i}", $dbh); - $comment=dba_fetch("${msgID}_comment_${i}", $dbh); - // push a new struct onto the return array - $ra[] = array( - "name" => $name, - "comment" => $comment - ); - } - } - } - // if we generated an error, create an error return response - if($err) - { - return new xmlrpcresp(0, $xmlrpcerruser, $err); - } - else - { - // otherwise, we create the right response - // with the state name - return new xmlrpcresp(php_xmlrpc_encode($ra)); - } - } +function getComments($req) +{ + $err = ""; + $ra = array(); + $encoder = new PhpXmlRpc\Encoder(); + $msgID = $encoder->decode($req->getParam(0)); + $dbh = dba_open("/tmp/comments.db", "r", "db2"); + if ($dbh) { + $countID = "${msgID}_count"; + if (dba_exists($countID, $dbh)) { + $count = dba_fetch($countID, $dbh); + for ($i = 0; $i < $count; $i++) { + $name = dba_fetch("${msgID}_name_${i}", $dbh); + $comment = dba_fetch("${msgID}_comment_${i}", $dbh); + // push a new struct onto the return array + $ra[] = array( + "name" => $name, + "comment" => $comment, + ); + } + } + } + // if we generated an error, create an error return response + if ($err) { + return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); + } else { + // otherwise, we create the right response + return new PhpXmlRpc\Response($encoder->encode($ra)); + } +} + +// NB: take care not to output anything else after this call, as it will mess up the responses and it will be hard to +// debug. In case you have to do so, at least re-emit a correct Content-Length http header (requires output buffering) + +$srv = new PhpXmlRpc\Server(array( + "discuss.addComment" => array( + "function" => "addComment", + "signature" => $addComment_sig, + "docstring" => $addComment_doc, + ), + "discuss.getComments" => array( + "function" => "getComments", + "signature" => $getComments_sig, + "docstring" => $getComments_doc, + ), +)); - $s = new xmlrpc_server(array( - "discuss.addComment" => array( - "function" => "addcomment", - "signature" => $addcomment_sig, - "docstring" => $addcomment_doc - ), - "discuss.getComments" => array( - "function" => "getcomments", - "signature" => $getcomments_sig, - "docstring" => $getcomments_doc - ) - )); -?> +require_once __DIR__ . "/_append.php";