Refactor the Wrapper class to generate closures by default and avoid usage of 'eval'
[plcapi.git] / demo / client / comment.php
1 <?php
2
3 include_once __DIR__ . "/../../src/Autoloader.php";
4 PhpXmlRpc\Autoloader::register();
5
6 $mydir = "demo/";
7
8 // define some utility functions
9 function bomb()
10 {
11     print "</body></html>";
12     exit();
13 }
14
15 function dispatch($client, $method, $args)
16 {
17     $req = new PhpXmlRpc\Request($method, $args);
18     $resp = $client->send($req);
19     if (!$resp) {
20         print "<p>IO error: " . $client->errstr . "</p>";
21         bomb();
22     }
23     if ($resp->faultCode()) {
24         print "<p>There was an error: " . $resp->faultCode() . " " .
25             $resp->faultString() . "</p>";
26         bomb();
27     }
28
29     $encoder = new PhpXmlRpc\Encoder();
30     return $encoder->decode($resp->value());
31 }
32
33 // create client for discussion server
34 $dclient = new PhpXmlRpc\Client("http://xmlrpc.usefulinc.com/${mydir}discuss.php");
35
36 // check if we're posting a comment, and send it if so
37 @$storyid = $_POST["storyid"];
38 if ($storyid) {
39
40     //    print "Returning to " . $HTTP_POST_VARS["returnto"];
41
42     $res = dispatch($dclient, "discuss.addComment",
43         array(new PhpXmlRpc\Value($storyid),
44             new PhpXmlRpc\Value(stripslashes(@$_POST["name"])),
45             new PhpXmlRpc\Value(stripslashes(@$_POST["commenttext"])),));
46
47     // send the browser back to the originating page
48     Header("Location: ${mydir}/comment.php?catid=" .
49         $_POST["catid"] . "&chanid=" .
50         $_POST["chanid"] . "&oc=" .
51         $_POST["catid"]);
52     exit(0);
53 }
54
55 // now we've got here, we're exploring the story store
56
57 ?>
58 <html>
59 <head><title>meerkat browser</title></head>
60 <body bgcolor="#ffffff">
61 <h2>Meerkat integration</h2>
62 <?php
63 @$catid = $_GET["catid"];
64 if (@$_GET["oc"] == $catid) {
65     @$chanid = $_GET["chanid"];
66 } else {
67     $chanid = 0;
68 }
69
70 $client = new PhpXmlRpc\Client("http://www.oreillynet.com/meerkat/xml-rpc/server.php");
71
72 if (@$_GET["comment"] &&
73     (!@$_GET["cdone"])
74 ) {
75     // we're making a comment on a story,
76     // so display a comment form
77     ?>
78     <h3>Make a comment on the story</h3>
79     <form method="post">
80         <p>Your name:<br/><input type="text" size="30" name="name"/></p>
81
82         <p>Your comment:<br/><textarea rows="5" cols="60"
83                                        name="commenttext"></textarea></p>
84         <input type="submit" value="Send comment"/>
85         <input type="hidden" name="storyid"
86                value="<?php echo @$_GET["comment"];
87                ?>"/>
88         <input type="hidden" name="chanid"
89                value="<?php echo $chanid;
90                ?>"/>
91         <input type="hidden" name="catid"
92                value="<?php echo $catid;
93                ?>"/>
94
95     </form>
96 <?php
97
98 } else {
99     $categories = dispatch($client, "meerkat.getCategories", array());
100     if ($catid) {
101         $sources = dispatch($client, "meerkat.getChannelsByCategory",
102             array(new PhpXmlRpc\Value($catid, "int")));
103     }
104     if ($chanid) {
105         $stories = dispatch($client, "meerkat.getItems",
106             array(new PhpXmlRpc\Value(
107                 array(
108                     "channel" => new PhpXmlRpc\Value($chanid, "int"),
109                     "ids" => new PhpXmlRpc\Value(1, "int"),
110                     "descriptions" => new PhpXmlRpc\Value(200, "int"),
111                     "num_items" => new PhpXmlRpc\Value(5, "int"),
112                     "dates" => new PhpXmlRpc\Value(0, "int"),
113                 ), "struct")));
114     }
115     ?>
116     <form>
117         <p>Subject area:<br/>
118             <select name="catid">
119                 <?php
120                 if (!$catid) {
121                     print "<option value=\"0\">Choose a category</option>\n";
122                 }
123                 while (list($k, $v) = each($categories)) {
124                     print "<option value=\"" . $v['id'] . "\"";
125                     if ($v['id'] == $catid) {
126                         print " selected=\"selected\"";
127                     }
128                     print ">" . $v['title'] . "</option>\n";
129                 }
130                 ?>
131             </select></p>
132         <?php
133         if ($catid) {
134             ?>
135             <p>News source:<br/>
136                 <select name="chanid">
137                     <?php
138                     if (!$chanid) {
139                         print "<option value=\"0\">Choose a source</option>\n";
140                     }
141                     while (list($k, $v) = each($sources)) {
142                         print "<option value=\"" . $v['id'] . "\"";
143                         if ($v['id'] == $chanid) {
144                             print "\" selected=\"selected\"";
145                         }
146                         print ">" . $v['title'] . "</option>\n";
147                     }
148                     ?>
149                 </select>
150             </p>
151
152         <?php
153
154         } // end if ($catid)
155         ?>
156
157         <p><input type="submit" value="Update"/></p>
158         <input type="hidden" name="oc" value="<?php echo $catid;
159         ?>"/>
160     </form>
161
162     <?php
163     if ($chanid) {
164         ?>
165
166         <h2>Stories available</h2>
167         <table>
168             <?php
169             while (list($k, $v) = each($stories)) {
170                 print "<tr>";
171                 print "<td><b>" . $v['title'] . "</b><br />";
172                 print $v['description'] . "<br />";
173                 print "<em><a target=\"_blank\" href=\"" .
174                     $v['link'] . "\">Read full story</a> ";
175                 print "<a href=\"comment.php?catid=${catid}&chanid=${chanid}&" .
176                     "oc=${oc}&comment=" . $v['id'] . "\">Comment on this story</a>";
177                 print "</em>";
178                 print "</td>";
179                 print "</tr>\n";
180                 // now look for existing comments
181                 $res = dispatch($dclient, "discuss.getComments",
182                     array(new PhpXmlRpc\Value($v['id'])));
183                 if (sizeof($res) > 0) {
184                     print "<tr><td bgcolor=\"#dddddd\"><p><b><i>" .
185                         "Comments on this story:</i></b></p>";
186                     for ($i = 0; $i < sizeof($res); $i++) {
187                         $s = $res[$i];
188                         print "<p><b>From:</b> " . htmlentities($s['name']) . "<br />";
189                         print "<b>Comment:</b> " . htmlentities($s['comment']) . "</p>";
190                     }
191                     print "</td></tr>\n";
192                 }
193                 print "<tr><td><hr /></td></tr>\n";
194             }
195             ?>
196         </table>
197
198     <?php
199
200     } // end if ($chanid)
201 } // end if comment
202 ?>
203 <hr/>
204 <p>
205     <a href="http://meerkat.oreillynet.com"><img align="right"
206                                                  src="http://meerkat.oreillynet.com/icons/meerkat-powered.jpg"
207                                                  height="31" width="88" alt="Meerkat powered, yeah!"
208                                                  border="0" hspace="8"/></a>
209 </p>
210 </body>
211 </html>