WIP introduce namespaces; move all global functions and variables to classes
[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         $msgID=php_xmlrpc_decode($m->getParam(0));
72         $dbh=dba_open("/tmp/comments.db", "r", "db2");
73         if($dbh)
74         {
75             $countID="${msgID}_count";
76             if(dba_exists($countID, $dbh))
77             {
78                 $count=dba_fetch($countID, $dbh);
79                 for($i=0; $i<$count; $i++)
80                 {
81                     $name=dba_fetch("${msgID}_name_${i}", $dbh);
82                     $comment=dba_fetch("${msgID}_comment_${i}", $dbh);
83                     // push a new struct onto the return array
84                     $ra[] = array(
85                         "name" => $name,
86                         "comment" => $comment
87                         );
88                 }
89             }
90         }
91         // if we generated an error, create an error return response
92         if($err)
93         {
94             return new xmlrpcresp(0, $xmlrpcerruser, $err);
95         }
96         else
97         {
98             // otherwise, we create the right response
99             // with the state name
100             return new xmlrpcresp(php_xmlrpc_encode($ra));
101         }
102     }
103
104     $s = new xmlrpc_server(array(
105         "discuss.addComment" => array(
106             "function" => "addcomment",
107             "signature" => $addcomment_sig,
108             "docstring" => $addcomment_doc
109         ),
110         "discuss.getComments" => array(
111             "function" => "getcomments",
112             "signature" => $getcomments_sig,
113             "docstring" => $getcomments_doc
114         )
115     ));