| 1 |
<?php |
| 2 |
//----------------------------------------------------------------------------- |
| 3 |
// Module to allow users to annotate online documentation. |
| 4 |
// Matthias Braun <[email protected]> |
| 5 |
//----------------------------------------------------------------------------- |
| 6 |
|
| 7 |
// this needs to be writable by Apache |
| 8 |
$basedir="/home/japj/annotations"; |
| 9 |
|
| 10 |
$theme=preg_replace("\" \"","_",$theme); |
| 11 |
$theme=preg_replace("[^a-zA-Z0-9_]","",$theme); |
| 12 |
$file=$basedir."/$theme.xml"; |
| 13 |
$self=$PHP_SELF; |
| 14 |
|
| 15 |
readXMLFile($file); |
| 16 |
print "<hr><a name=\"comments\">\n"; |
| 17 |
|
| 18 |
if ($action=="") { |
| 19 |
printEntries(); |
| 20 |
print "<a href=\"$self?action=showadd#comments\">Add a comment</a><br>\n"; |
| 21 |
} elseif ($action == "showadd") { |
| 22 |
print "<form action=\"$self?action=add#comments\" method=\"post\">\n"; |
| 23 |
print "<b>Name:</b><input name=\"authorname\" value=\"\"><br>\n"; |
| 24 |
print "<b>E-Mail:</b><input name=\"emailname\" value=\"\"><br>\n"; |
| 25 |
print "<textarea cols=44 rows=8 name=\"texttext\" wrap=\"virtual\"></textarea><br>\n"; |
| 26 |
print "<input type=\"submit\" value=\"Add Comment\"><br>\n"; |
| 27 |
print "</form><br>\n"; |
| 28 |
printEntries(); |
| 29 |
} elseif ($action == "add") { |
| 30 |
$newentry = new entry; |
| 31 |
$newentry->author=$authorname; |
| 32 |
$newentry->email=$emailname; |
| 33 |
if ($authorname=="" && $emailname=="") |
| 34 |
die ("<br><h2>Please give name or email!</h2>\n"); |
| 35 |
$newentry->date=time(); |
| 36 |
$newentry->addr=$REMOTE_ADDR; |
| 37 |
$newentry->agent=$HTTP_USER_AGENT; |
| 38 |
$newentry->text=nl2br($texttext); |
| 39 |
$last =count($entries)-1; |
| 40 |
if (!($newentry->author == $entries[$last]->author && |
| 41 |
$newentry->email == $entries[$last]->email && |
| 42 |
$newentry->text == $entries[$last]->text)) { |
| 43 |
$entries[] = $newentry; |
| 44 |
$h= fopen ($file, "w"); |
| 45 |
if (!$h) { |
| 46 |
die ("Couldn't write to disk!<br>\n"); |
| 47 |
} |
| 48 |
writeXMLFile($h); |
| 49 |
include ("mail.php"); |
| 50 |
foreach ($mail as $i) { |
| 51 |
$emailmangled = str_replace("@", " at ", $emailname); |
| 52 |
$emailmangled = str_replace(".", " dot ", $emailmangled); |
| 53 |
mail ($i, "ClanLib documentation annotated by $authorname", |
| 54 |
"Author: $authorname\n". |
| 55 |
"E-Mail: $emailmangled\n". |
| 56 |
"Topic: $theme\n". |
| 57 |
"File: $HTTP_REFERER\n". |
| 58 |
"Time: ".strftime("%a, %d %b %G (%H:%M UTC)",$newentry->date)." (".$newentry->date.")\n". |
| 59 |
"Comment:\n". |
| 60 |
stripslashes($texttext)); |
| 61 |
} |
| 62 |
fclose($h); |
| 63 |
chmod($file, 0666); |
| 64 |
print "<h3>Comment added!</h3>\n"; |
| 65 |
unset($entries); |
| 66 |
readXMLFile($file); |
| 67 |
} |
| 68 |
printEntries(); |
| 69 |
print "<a href=\"$self?action=showadd#comments\">Add a comment</a><br>\n"; |
| 70 |
} |
| 71 |
|
| 72 |
class entry |
| 73 |
{ |
| 74 |
var $author; |
| 75 |
var $email; |
| 76 |
var $text; |
| 77 |
var $date; |
| 78 |
var $addr; |
| 79 |
var $agent; |
| 80 |
|
| 81 |
function entry() |
| 82 |
{ |
| 83 |
$this->author=""; |
| 84 |
$this->email=""; |
| 85 |
$this->text=""; |
| 86 |
$this->addr=""; |
| 87 |
$this->agent=""; |
| 88 |
$this->data=0; |
| 89 |
} |
| 90 |
|
| 91 |
function writeE($h) |
| 92 |
{ |
| 93 |
if ($this->author=="" && $this->email=="") { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
fputs ($h, "<comment>\n"); |
| 98 |
fputs ($h, "<author>".$this->author."</author>\n"); |
| 99 |
fputs ($h, "<email>".$this->email."</email>\n"); |
| 100 |
fputs ($h, "<date>".$this->date."</date>\n"); |
| 101 |
fputs ($h, "<ipaddr>".$this->addr."</ipaddr>\n"); |
| 102 |
fputs ($h, "<agent>".$this->agent."</agent>\n"); |
| 103 |
fputs ($h, "<?text ".$this->text ."?>\n"); |
| 104 |
fputs ($h, "</comment>\n"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
function printEntries() |
| 109 |
{ |
| 110 |
global $entries; |
| 111 |
print "<br><br>\n"; |
| 112 |
|
| 113 |
foreach ($entries as $e) { |
| 114 |
print "<table width=\"100%\" bgcolor=\"#88bbff\" celspacing=\"0\" celpadding=\"0\">\n"; |
| 115 |
print "<tr><td>\n"; |
| 116 |
if ($e->author != "") { |
| 117 |
print $e->author; |
| 118 |
} |
| 119 |
if ($e->email != "") { |
| 120 |
$mail = str_replace("@","-<font color=red>at</font>-",$e->email); |
| 121 |
$mail = str_replace(".","-<font color=red>dot</font>-",$mail); |
| 122 |
if ($e->author != "") |
| 123 |
print " ($mail)"; |
| 124 |
else |
| 125 |
print $mail; |
| 126 |
} |
| 127 |
print "</td>\n"; |
| 128 |
|
| 129 |
print "<td align=\"right\"><b>\n"; |
| 130 |
if ($e->date != 0) |
| 131 |
{ |
| 132 |
print strftime("%a, %d %b %G (%H:%M UTC)", $e->date) . "<br>\n"; |
| 133 |
} |
| 134 |
print "</b></td></tr>\n"; |
| 135 |
print "</table>\n"; |
| 136 |
print "<table width=\"100%\" bgcolor=\"#eeeeee\"><tr><td>"; |
| 137 |
print stripslashes($e->text); |
| 138 |
print "</td></tr></table>\n"; |
| 139 |
print "<br>\n"; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
function readXMLFile($file) |
| 144 |
{ |
| 145 |
global $status; |
| 146 |
global $entries; |
| 147 |
|
| 148 |
if (!file_exists($file)) { |
| 149 |
$newe = new entry; |
| 150 |
$newe->text="<h2>No user comments yet!</h2>\n"; |
| 151 |
$entries[] = $newe; |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$xml_parser = xml_parser_create(); |
| 156 |
xml_set_element_handler($xml_parser, "startElement", "endElement"); |
| 157 |
xml_set_character_data_handler($xml_parser, "characterData"); |
| 158 |
xml_set_processing_instruction_handler($xml_parser, "piHandler"); |
| 159 |
if (!($fp = fopen($file, "r"))) { |
| 160 |
die ("could not open XML input"); |
| 161 |
} |
| 162 |
|
| 163 |
$status="global"; |
| 164 |
|
| 165 |
while ($data = fread($fp, 4096)) { |
| 166 |
if (!xml_parse($xml_parser, $data, feof($fp))) { |
| 167 |
die(sprintf("XML error: %s at line %d", |
| 168 |
xml_error_string(xml_get_error_code($xml_parser)), |
| 169 |
xml_get_current_line_number($xml_parser))); |
| 170 |
} |
| 171 |
} |
| 172 |
xml_parser_free($xml_parser); |
| 173 |
} |
| 174 |
|
| 175 |
function writeXMLFile($h) |
| 176 |
{ |
| 177 |
global $entries; |
| 178 |
|
| 179 |
fputs ($h, "<?xml version=\"1.0\"?>\n"); |
| 180 |
fputs ($h, "<comments>\n"); |
| 181 |
foreach ($entries as $e) { |
| 182 |
$e->writeE($h); |
| 183 |
} |
| 184 |
|
| 185 |
fputs ($h, "</comments>\n"); |
| 186 |
} |
| 187 |
|
| 188 |
function startElement($parser, $name, $attrs) { |
| 189 |
global $status; |
| 190 |
global $aentry; |
| 191 |
|
| 192 |
$xmlglobdata=""; |
| 193 |
switch ($status) { |
| 194 |
case "global": |
| 195 |
if ($name == "COMMENT") { |
| 196 |
$status="comment"; |
| 197 |
$aentry=new entry(); |
| 198 |
} |
| 199 |
|
| 200 |
break; |
| 201 |
case "comment": |
| 202 |
if ($name == "AUTHOR") { |
| 203 |
$status="author"; |
| 204 |
} |
| 205 |
if ($name == "EMAIL") { |
| 206 |
$status="email"; |
| 207 |
} |
| 208 |
if ($name == "DATE") { |
| 209 |
$status="date"; |
| 210 |
} |
| 211 |
if ($name == "IPADDR") { |
| 212 |
$status ="ipaddr"; |
| 213 |
} |
| 214 |
if ($name == "AGENT") { |
| 215 |
$status = "agent"; |
| 216 |
} |
| 217 |
if ($name == "TEXT") { |
| 218 |
$status="text"; |
| 219 |
} |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
function endElement($parser, $name) { |
| 225 |
global $status; |
| 226 |
global $aentry; |
| 227 |
global $xmlglobdata; |
| 228 |
global $entries; |
| 229 |
|
| 230 |
switch ($status) { |
| 231 |
case "comment": |
| 232 |
if ($name == "COMMENT") { |
| 233 |
$status="global"; |
| 234 |
$entries[] = $aentry; |
| 235 |
} |
| 236 |
break; |
| 237 |
case "author": |
| 238 |
$aentry->author = $xmlglobdata; |
| 239 |
$status="comment"; |
| 240 |
break; |
| 241 |
case "email": |
| 242 |
$aentry->email = $xmlglobdata; |
| 243 |
$status="comment"; |
| 244 |
break; |
| 245 |
case "date": |
| 246 |
$aentry->date = (int)$xmlglobdata; |
| 247 |
$status="comment"; |
| 248 |
break; |
| 249 |
case "text": |
| 250 |
if ($name != "TEXT") { break; } |
| 251 |
$aentry->text = $xmlglobdata; |
| 252 |
$status="comment"; |
| 253 |
break; |
| 254 |
case "ipaddr": |
| 255 |
$aentry->addr = $xmlglobaldata; |
| 256 |
$status="comment"; |
| 257 |
break; |
| 258 |
case "agent": |
| 259 |
$anetry->agent = $xmlglobaldata; |
| 260 |
$status="comment"; |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
function characterData($parser, $data) { |
| 266 |
global $xmlglobdata; |
| 267 |
if ($data=="\n") |
| 268 |
$data=""; |
| 269 |
$xmlglobdata=$data; |
| 270 |
} |
| 271 |
|
| 272 |
function piHandler($parser, $name, $data) { |
| 273 |
global $status; |
| 274 |
global $aentry; |
| 275 |
|
| 276 |
if ($status=="comment" && $name=="text") { |
| 277 |
$aentry->text=$data; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
?> |