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