1 | <?php |
||
2 | /** |
||
3 | * A basic comment server. Given an ID it will store a list of names and comment texts against it. |
||
4 | * It uses a Berkeley DB database for storage. |
||
5 | */ |
||
6 | |||
7 | require_once __DIR__ . "/_prepend.php"; |
||
8 | |||
9 | use PhpXmlRpc\Value; |
||
10 | |||
11 | 1 | $addComment_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString)); |
|
12 | |||
13 | 1 | $addComment_doc = 'Adds a comment to an item. The first parameter |
|
14 | is the item ID, the second the name of the commenter, and the third |
||
15 | is the comment itself. Returns the number of comments against that |
||
16 | ID.'; |
||
17 | |||
18 | function addComment($req) |
||
19 | { |
||
20 | $err = ""; |
||
21 | // since validation has already been carried out for us, |
||
22 | // we know we got exactly 3 string values |
||
23 | $encoder = new PhpXmlRpc\Encoder(); |
||
24 | $n = $encoder->decode($req); |
||
25 | $msgID = $n[0]; |
||
26 | $name = $n[1]; |
||
27 | $comment = $n[2]; |
||
28 | |||
29 | $dbh = dba_open("/tmp/comments.db", "c", "db2"); |
||
30 | if ($dbh) { |
||
31 | $countID = "${msgID}_count"; |
||
32 | if (dba_exists($countID, $dbh)) { |
||
33 | $count = dba_fetch($countID, $dbh); |
||
34 | } else { |
||
35 | $count = 0; |
||
36 | } |
||
37 | // add the new comment in |
||
38 | dba_insert($msgID . "_comment_${count}", $comment, $dbh); |
||
39 | dba_insert($msgID . "_name_${count}", $name, $dbh); |
||
40 | $count++; |
||
41 | dba_replace($countID, $count, $dbh); |
||
42 | dba_close($dbh); |
||
43 | } else { |
||
44 | $err = "Unable to open comments database."; |
||
45 | } |
||
46 | // if we generated an error, create an error return response |
||
47 | if ($err) { |
||
48 | return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); |
||
49 | } else { |
||
50 | // otherwise, we create the right response |
||
51 | return new PhpXmlRpc\Response(new PhpXmlRpc\Value($count, "int")); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
52 | } |
||
53 | } |
||
54 | |||
55 | 1 | $getComments_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcString)); |
|
56 | |||
57 | 1 | $getComments_doc = 'Returns an array of comments for a given ID, which |
|
58 | is the sole argument. Each array item is a struct containing name |
||
59 | and comment text.'; |
||
60 | |||
61 | function getComments($req) |
||
62 | { |
||
63 | $err = ""; |
||
64 | $ra = array(); |
||
65 | $encoder = new PhpXmlRpc\Encoder(); |
||
66 | $msgID = $encoder->decode($req->getParam(0)); |
||
67 | $dbh = dba_open("/tmp/comments.db", "r", "db2"); |
||
68 | if ($dbh) { |
||
69 | $countID = "${msgID}_count"; |
||
70 | if (dba_exists($countID, $dbh)) { |
||
71 | $count = dba_fetch($countID, $dbh); |
||
72 | for ($i = 0; $i < $count; $i++) { |
||
73 | $name = dba_fetch("${msgID}_name_${i}", $dbh); |
||
74 | $comment = dba_fetch("${msgID}_comment_${i}", $dbh); |
||
75 | // push a new struct onto the return array |
||
76 | $ra[] = array( |
||
77 | "name" => $name, |
||
78 | "comment" => $comment, |
||
79 | ); |
||
80 | } |
||
81 | } |
||
82 | } else { |
||
83 | $err = "Unable to open comments database."; |
||
84 | } |
||
85 | // if we generated an error, create an error return response |
||
86 | if ($err) { |
||
87 | return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); |
||
88 | } else { |
||
89 | // otherwise, we create the right response |
||
90 | return new PhpXmlRpc\Response($encoder->encode($ra)); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | // NB: take care not to output anything else after this call, as it will mess up the responses and it will be hard to |
||
95 | // debug. In case you have to do so, at least re-emit a correct Content-Length http header (requires output buffering) |
||
96 | |||
97 | 1 | $srv = new PhpXmlRpc\Server(array( |
|
98 | "discuss.addComment" => array( |
||
99 | 1 | "function" => "addComment", |
|
100 | 1 | "signature" => $addComment_sig, |
|
101 | 1 | "docstring" => $addComment_doc, |
|
102 | ), |
||
103 | "discuss.getComments" => array( |
||
104 | 1 | "function" => "getComments", |
|
105 | 1 | "signature" => $getComments_sig, |
|
106 | 1 | "docstring" => $getComments_doc, |
|
107 | ), |
||
108 | )); |
||
109 |