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