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