Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / php / phpxmlrpc / demo / server / discuss.php
1 <?php
2
3 include_once __DIR__ . "/../../vendor/autoload.php";
4
5 use PhpXmlRpc\Value;
6
7 $addComment_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString));
8
9 $addComment_doc = 'Adds a comment to an item. The first parameter
10 is the item ID, the second the name of the commenter, and the third
11 is the comment itself. Returns the number of comments against that
12 ID.';
13
14 function addComment($req)
15 {
16     $err = "";
17     // since validation has already been carried out for us,
18     // we know we got exactly 3 string values
19     $encoder = new PhpXmlRpc\Encoder();
20     $n = $encoder->decode($req);
21     $msgID = $n[0];
22     $name = $n[1];
23     $comment = $n[2];
24
25     $dbh = dba_open("/tmp/comments.db", "c", "db2");
26     if ($dbh) {
27         $countID = "${msgID}_count";
28         if (dba_exists($countID, $dbh)) {
29             $count = dba_fetch($countID, $dbh);
30         } else {
31             $count = 0;
32         }
33         // add the new comment in
34         dba_insert($msgID . "_comment_${count}", $comment, $dbh);
35         dba_insert($msgID . "_name_${count}", $name, $dbh);
36         $count++;
37         dba_replace($countID, $count, $dbh);
38         dba_close($dbh);
39     } else {
40         $err = "Unable to open comments database.";
41     }
42     // if we generated an error, create an error return response
43     if ($err) {
44         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
45     } else {
46         // otherwise, we create the right response
47         return new PhpXmlRpc\Response(new PhpXmlRpc\Value($count, "int"));
48     }
49 }
50
51 $getComments_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcString));
52
53 $getComments_doc = 'Returns an array of comments for a given ID, which
54 is the sole argument. Each array item is a struct containing name
55 and comment text.';
56
57 function getComments($req)
58 {
59     $err = "";
60     $ra = array();
61     $encoder = new PhpXmlRpc\Encoder();
62     $msgID = $encoder->decode($req->getParam(0));
63     $dbh = dba_open("/tmp/comments.db", "r", "db2");
64     if ($dbh) {
65         $countID = "${msgID}_count";
66         if (dba_exists($countID, $dbh)) {
67             $count = dba_fetch($countID, $dbh);
68             for ($i = 0; $i < $count; $i++) {
69                 $name = dba_fetch("${msgID}_name_${i}", $dbh);
70                 $comment = dba_fetch("${msgID}_comment_${i}", $dbh);
71                 // push a new struct onto the return array
72                 $ra[] = array(
73                     "name" => $name,
74                     "comment" => $comment,
75                 );
76             }
77         }
78     }
79     // if we generated an error, create an error return response
80     if ($err) {
81         return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
82     } else {
83         // otherwise, we create the right response
84         return new PhpXmlRpc\Response($encoder->encode($ra));
85     }
86 }
87
88 $srv = new PhpXmlRpc\Server(array(
89     "discuss.addComment" => array(
90         "function" => "addComment",
91         "signature" => $addComment_sig,
92         "docstring" => $addComment_doc,
93     ),
94     "discuss.getComments" => array(
95         "function" => "getComments",
96         "signature" => $getComments_sig,
97         "docstring" => $getComments_doc,
98     ),
99 ));