00001 <?PHP
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00028 class commentHandler
00029 {
00030 var $commentsOwner;
00031 var $commentLevel;
00032 var $editLevel;
00033 var $comments;
00034 var $thisPageURL;
00035
00036 var $commentsChanged = false;
00037 var $lastCommenter;
00038 var $lastComment;
00039
00040 var $allowNesting = true;
00041
00052 function commentHandler($comments, $commentsOwner, $commentLevel, $editLevel)
00053 {
00054 $this->comments = $comments;
00055 $this->commentsOwner = $commentsOwner;
00056 $this->commentLevel = $commentLevel;
00057 $this->editLevel = $editLevel;
00058 $this->thisPageURL = thisPageURL();
00059
00060 if( userAllows( $this->commentsOwner, $this->editLevel ) ) {
00061 if( $_REQUEST["deletecomment"] != "" )
00062 {
00063 if ($_REQUEST["confirm_delete"] == "true")
00064 $new_comment = $this->deleteComment();
00065 }
00066 else if( $_REQUEST["editcomment"] != "" )
00067 {
00068 if ($_POST["save_comment"])
00069 $new_comment = $this->editComment();
00070 }
00071 else if( $_REQUEST["hideshow"] != "" && $_REQUEST["commentid"] != "" )
00072 {
00073 if( $_REQUEST["hideshow"] == "hide" )
00074 $hidden = "true";
00075 if( $_REQUEST["hideshow"] == "show" )
00076 $hidden = "";
00077
00078 $new_comment = $_REQUEST["commentid"] . "[hidden]=" . $hidden;
00079 $new_comment = $new_comment;
00080
00081 parse_str($new_comment, $new_comment);
00082 $new_comment = $new_comment["comments"];
00083 }
00084
00085 if( ( $_REQUEST["deletecomment"] != "" && $_REQUEST["confirm_delete"] ) || ( $_REQUEST["editcomment"] != "" && $_POST["save_comment"] ) || ($_REQUEST["hideshow"] != "" && $_REQUEST["commentid"] != ""))
00086 {
00087 $this->comments = array_merge_n($this->comments, $new_comment);
00088 $this->comments = $this->cleanComments($this->comments);
00089 $this->commentsChanged = true;
00090 header("Location: " . thisPageURL(true) );
00091 }
00092 }
00093
00094 if( userAllows( $this->commentsOwner, $this->commentLevel ) && $_POST["newname"] != "" && $_POST["newemail"] != "" && $_POST["newcomment"] != "" )
00095 {
00096 $new_comment = $this->addComment();
00097 $this->comments = array_merge_n($this->comments, $new_comment);
00098 $this->comments = $this->cleanComments($this->comments);
00099 $this->lastCommenter = stripslashes($_POST["newname"]);
00100 $this->lastComment = stripslashes($_POST["newcomment"]);
00101 $this->commentsChanged = true;
00102 header("Location: " . thisPageURL(true) );
00103 }
00104 }
00105
00111 function addComment()
00112 {
00113 $replyto = str_replace($_REQUEST["newcommentid"], time(), stripslashes($_REQUEST["replyto"]));
00114
00115 $new_commentname = $replyto . "[name]=" . str_replace("%", "%25", stripslashes($_POST["newname"]));
00116 $new_commentemail = $replyto . "[email]=" . str_replace("%", "%25", stripslashes($_POST["newemail"]));
00117 $new_commentcomment = $replyto ."[comment]=" . str_replace("%", "%25", stripslashes($_POST["newcomment"]));
00118
00119 parse_str($new_commentname, $new_commentname);
00120 parse_str($new_commentemail, $new_commentemail);
00121 parse_str($new_commentcomment, $new_commentcomment);
00122
00123 $new_comment = array();
00124 array_merge_2($new_comment, $new_commentname);
00125 array_merge_2($new_comment, $new_commentemail);
00126 array_merge_2($new_comment, $new_commentcomment);
00127
00128 return $new_comment["comments"];
00129 }
00130
00136 function editComment()
00137 {
00138 $edit_comment = $_REQUEST["editcomment"];
00139
00140 $edit_commentname = $edit_comment . "[name]=" . str_replace("%", "%25", stripslashes($_POST["editcommentname"]));
00141 $edit_commentemail = $edit_comment . "[email]=" . str_replace("%", "%25", stripslashes($_POST["editcommentemail"]));
00142 $edit_commentcomment = $edit_comment ."[comment]=" . str_replace("%", "%25", stripslashes($_POST["editcommentcomment"]));
00143
00144 parse_str($edit_commentname, $edit_commentname);
00145 parse_str($edit_commentemail, $edit_commentemail);
00146 parse_str($edit_commentcomment, $edit_commentcomment);
00147 parse_str($edit_commenthidden, $edit_commenthidden);
00148
00149 $new_comment = array();
00150 array_merge_2($new_comment, $edit_commentname);
00151 array_merge_2($new_comment, $edit_commentemail);
00152 array_merge_2($new_comment, $edit_commentcomment);
00153 array_merge_2($new_comment, $edit_commenthidden);
00154
00155 return $new_comment["comments"];
00156 }
00157
00163 function deleteComment()
00164 {
00165 $new_comment = $_REQUEST["deletecomment"];
00166
00167 $new_commentname = $new_comment . "[name]=";
00168 $new_commentemail = $new_comment . "[email]=";
00169 $new_commentcomment = $new_comment ."[comment]=";
00170 $new_commenthidden = $new_comment . "[hidden]=";
00171
00172 parse_str($new_commentname, $new_commentname);
00173 parse_str($new_commentemail, $new_commentemail);
00174 parse_str($new_commentcomment, $new_commentcomment);
00175 parse_str($new_commenthidden, $new_commenthidden);
00176
00177 $new_comment = array();
00178 array_merge_2($new_comment, $new_commentname);
00179 array_merge_2($new_comment, $new_commentemail);
00180 array_merge_2($new_comment, $new_commentcomment);
00181 array_merge_2($new_comment, $new_commenthidden);
00182
00183 return $new_comment["comments"];
00184 }
00185
00193 function cleanComments($comments)
00194 {
00195 $result = array();
00196 if( is_array( $comments ) )
00197 {
00198 foreach( $comments as $key => $value )
00199 {
00200 $value["name"] = stripslashes($value["name"]);
00201 $value["email"] = stripslashes($value["email"]);
00202 $value["comment"] = stripslashes($value["comment"]);
00203
00204 if ( !( $value["name"] == "" && $value["email"] == "" && $value["comment"] == "" ) )
00205 $result[$key] = $value;
00206
00207 if( is_array( $value["comments"] ) && count( $value["comments"] ) > 0)
00208 $result[$key]["comments"] = $this->cleanComments($value["comments"]);
00209 }
00210 }
00211 return $result;
00212 }
00213
00217 function replyto($comments, $depth = "", $commentlink, $newcommentid)
00218 {
00219 $i = 1;
00220 if( is_array( $comments ) )
00221 {
00222 foreach( $comments as $key => $value )
00223 {
00224 $data .= "<option value=\"{$commentlink}[$key][comments][$newcommentid]\">#$depth$i - " . parse_profilelinks($value['name']) . "</option>";
00225 if( is_array( $value["comments"] ) && count( $value["comments"] ) > 0 )
00226 $data .= $this->replyto($value["comments"], "$depth$i.", $commentlink . "[$key][comments]", $newcommentid);
00227 $i++;
00228 }
00229 }
00230 return $data;
00231 }
00232
00238 function numberOfComments()
00239 {
00240 return $this->countComments($this->comments);
00241 }
00242
00250 function countComments($comments)
00251 {
00252 $data = 0;
00253 if( !is_array($comments) )
00254 return 0;
00255
00256 foreach( $comments as $key => $value )
00257 {
00258 $data++;
00259 if( is_array( $value["comments"] ) && count( $value["comments"] ) > 0 )
00260 $data += $this->countComments($value["comments"]);
00261 }
00262 return $data;
00263 }
00264
00273 function getComment($whatcomment, $comments)
00274 {
00275 foreach( $whatcomment as $key => $value )
00276 {
00277 if( is_array( $value["comments"] ) )
00278 $thecomment = $this->getComment($value["comments"], $comments[$key]["comments"]);
00279 else
00280 $thecomment = $comments[$key];
00281 }
00282 return $thecomment;
00283 }
00284
00290 function renderComments()
00291 {
00292 $listid = 0;
00293 if( count($this->comments) < 1 )
00294 return "";
00295 return $this->renderCommentsActual($this->comments, $listid, "comments", "");
00296 }
00297
00308 function renderCommentsActual($comments, &$listid, $commentlink = "", $depth = "")
00309 {
00310 $data .= "<ul class=\"comments\">";
00311 $i = 1;
00312 foreach($comments as $key => $value)
00313 {
00314 $listid++;
00315 if( ($value["name"] != "" && $value["email"] != "" && $value["comment"] != "") || is_array($value["comments"]) && count($value["comments"]) > 0 )
00316 {
00317 $data .= "
00318 <li class=\"comment\">";
00319
00320
00321 if( $_REQUEST["deletecomment"] == $commentlink . "[$key]" )
00322 $data .= $this->renderCommentsDelete();
00323
00324
00325 if ($_REQUEST["editcomment"] == $commentlink . "[$key]")
00326 $data .= $this->renderCommentEditor();
00327
00328 else
00329 {
00330 $data .= "<div style=\"float: right;\" class=\"comment-commands\">";
00331 if( userAllows( $this->commentsOwner, $this->editLevel ) )
00332 {
00333 $data .= "
00334 <a class=\"command\" href=\"" . $this->thisPageURL . "&deletecomment=" . $commentlink . "[$key]#deletecomment\">[" . i18n("Delete") . "]</a>
00335 ";
00336 if( $value["hidden"] != true )
00337 $data .= "
00338 <a class=\"command\" href=\"" . $this->thisPageURL . "&hideshow=hide&commentid=" . $commentlink . "[$key]#" . $commentlink . "[comments][$key]\">[" . i18n("Hide") . "]</a>";
00339 else
00340 $data .= "
00341 <a class=\"command\" href=\"" . $this->thisPageURL . "&hideshow=show&commentid=" . $commentlink . "[$key]#" . $commentlink . "[comments][$key]\">[" . i18n("Show") . "]</a>";
00342
00343 $data .= "
00344 <a class=\"command\" href=\"" . $this->thisPageURL . "&editcomment=" . $commentlink . "[$key]#editcomment\">[" . i18n("Edit") . "]</a>";
00345 }
00346 if( userAllows( $this->commentsOwner, $this->commentLevel ) )
00347 {
00348 $data .= "
00349 <script language=\"javascript\">
00350 document.write('<a class=\"command\" href=\"#comment\" onClick=\"document.getElementById(\\'replyto\\').selectedIndex=$listid;\">[" . i18n("Reply") . "]</a>');
00351 </script>";
00352 }
00353 $data .= "</div>";
00354 if( $value["hidden"] != true )
00355 {
00356 $data .= parse_page_data($value["comment"]) . "<div class=\"comment-name\">
00357 <a class=\"comment-link\" name=\"comment$depth$i\" href=\"" . $this->thisPageURL . "#comment$depth$i\">#$depth$i</a>
00358 " . parse_profilelinks($value["name"]) . " (" . $value["email"] . ") - " . formatTime( $key ) . "
00359 </div>";
00360 }
00361 else
00362 {
00363 $data .= "<div class=\"comment-name\">
00364 <a class=\"comment-link\" name=\"comment$depth$i\" href=\"" . $this->thisPageURL . "#comment$depth$i\">#$depth$i</a>
00365 <small class=\"comment\">" . i18n("This comment has been hidden") . "</small></div>";
00366 }
00367 }
00368
00369 if( is_array($value["comments"]) && count($value["comments"]) > 0 )
00370 $data .= $this->renderCommentsActual($value["comments"], $listid, $commentlink . "[$key][comments]", "$depth$i.");
00371
00372 $data .= "</li>";
00373 $i++;
00374 }
00375 }
00376 $data .= "</ul>";
00377 return $data;
00378 }
00379
00380 function renderCommentsAdder() {
00381 global $formatting_toolbar, $permissionlevels_array, $systemOptions;
00382 if (userAllows($this->commentsOwner, $this->commentLevel))
00383 {
00384 $commenting_username = currentUser();
00385 if ($commenting_username != "")
00386 {
00387 $commenting_email = str_replace(".", " dot ", str_replace("@", " ad ", getUserInfo($commenting_username, "email")));
00388 $commenting_username = "%%$commenting_username%%";
00389 }
00390 $newcommentid = time() + 1;
00391 $commenting_replyto = "<option value=\"comments[$newcommentid]\">" . i18n("Top level") . "</option>";
00392 if( $this->allowNesting )
00393 $commenting_replyto .= $this->replyto($this->comments, "", "comments", $newcommentid);
00394
00395 $data = "
00396 <form name=\"commentform\" method=\"post\" action=\"" . $this->thisPageURL . "\">
00397 <input type=\"hidden\" name=\"newcommentid\" value=\"$newcommentid\" />
00398 <table class=\"editcomment\">
00399 <tr class=\"editcomment\">
00400 <td class=\"editcomment\" width=\"50%\">" . i18n("Reply to") . ":<a class=\"namedanchor\" name=\"comment\"> </a></td>
00401 <td class=\"editcomment\" width=\"50%\"><select style=\"width: 100%\" id=\"replyto\" name=\"replyto\">$commenting_replyto</select></td>
00402 </tr>
00403 <tr class=\"editcomment\">
00404 <td class=\"editcomment\">" . i18n("Your name") . ":</td>
00405 <td class=\"editcomment\"><input style=\"width: 100%\" type=\"text\" name=\"newname\" value=\"$commenting_username\" /></td>
00406 </tr>
00407 <tr class=\"editcomment\">
00408 <td class=\"editcomment\">" . i18n("Your email address") . ":</td>
00409 <td class=\"editcomment\"><input style=\"width: 100%\" type=\"text\" name=\"newemail\" value=\"$commenting_email\" /></td>
00410 </tr>
00411 <tr class=\"editcomment\">
00412 <td class=\"editcomment\" colspan=\"2\">" . i18n("Your comment") . ":
00413 </tr>
00414 $formatting_toolbar
00415 <tr class=\"editcomment\">
00416 <td class=\"editcomment\" colspan=\"2\">
00417 <textarea rows=\"10\" cols=\"70\" style=\"width: 100%;\" id=\"edit\" name=\"newcomment\">\n\n---\n" . getUserInfo(currentUser(), "signature", getUserInfo(currentUser(), "name", i18n("Posted by Anonymous on ##0##", array(formatTime(time())))) ) . "</textarea>
00418 </td>
00419 </tr>
00420 <tr class=\"editcomment\">
00421 <td class=\"editcommentbottom\"><input type=\"submit\" value=\"" . i18n("Create comment") . "\" /></td>
00422 <td class=\"editcommentbottom wikirightalign\"><input type=\"reset\" value=\"" . i18n("Reset") . "\" /></td>
00423 </tr>
00424 </table>
00425 </form>
00426 ";
00427
00428 $data = renderInformationBox( i18n("New Comment"), $data, false );
00429 }
00430 else if( $systemOptions["permissionmessages"] == 1 )
00431 $data = "<p><small class=\"comment\">" . i18n("##0## has disallowed commenting for users that do not fit the following description", array(getUserInfo($this->commentsOwner, "name"))) . ": <strong>" . $permissionlevels_array[$this->commentLevel] . "</strong></small></p>";
00432
00433 return $data;
00434 }
00435
00441 function renderCommentsDelete()
00442 {
00443 if( !$_REQUEST["confirm_delete"] )
00444 {
00445 $question = "<p><a class=\"namedanchor\" name=\"deletecomment\"> </a>" . i18n("Please confirm that you wish to delete this comment. Please note - this will delete ALL sub-comments. If you wish to allow people to still see the sub-comments, simply hide the comment in stead.") . "</p>
00446 <div class=\"wikicenteralign\">
00447 <a class=\"command\" href=\"" . $this->thisPageURL . "&deletecomment={$_REQUEST['deletecomment']}&confirm_delete=true\">[" . i18n("Confirm delete") . "]</a>
00448 <a class=\"command\" href=\"" . $this->thisPageURL . "\">[" . i18n("Abort delete") . "]</a>
00449 </div>";
00450 $data = renderQuestionBox( i18n("Delete comment?"), $question, false );
00451 }
00452 return $data;
00453 }
00454
00460 function renderCommentEditor()
00461 {
00462 global $formatting_toolbar;
00463 if( !$_POST["save_comment"] )
00464 {
00465 $edit_comment = $_REQUEST["editcomment"];
00466 parse_str($edit_comment, $edit_comment);
00467 $edit_comment = $this->getComment($edit_comment["comments"], $this->comments);
00468
00469 $data .= "
00470 <form action=\"" . $this->thisPageURL . "&editcomment={$_REQUEST['editcomment']}\" method=\"post\">
00471 <table class=\"editcomment\">
00472 <tr class=\"editcomment\"><th class=\"editcomment\" colspan=\"2\"><a class=\"namedanchor\" name=\"editcomment\"> </a>" . i18n("Edit comment") . "</th></tr>
00473 <tr class=\"editcomment\">
00474 <td class=\"editcomment\">" . i18n("Commenter's name") . ":</td>
00475 <td class=\"editcomment\"><input style=\"width: 100%\" type=\"text\" name=\"editcommentname\" value=\"{$edit_comment['name']}\" /></td>
00476 </tr>
00477 <tr class=\"editcomment\">
00478 <td class=\"editcomment\">" . i18n("Commentor's email address") . ":</td>
00479 <td class=\"editcomment\"><input style=\"width: 100%\" type=\"text\" name=\"editcommentemail\" value=\"{$edit_comment['email']}\" /></td>
00480 </tr>
00481 <tr class=\"editcomment\">
00482 <td class=\"editcomment\" colspan=\"2\">" . i18n("The comment") . ":
00483 </tr>
00484 $formatting_toolbar
00485 <tr class=\"editcomment\">
00486 <td class=\"editcomment\" colspan=\"2\">
00487 <textarea rows=\"10\" cols=\"70\" style=\"width: 100%;\" id=\"edit\" name=\"editcommentcomment\">{$edit_comment['comment']}</textarea>
00488 </td>
00489 </tr>
00490 <tr class=\"editcomment\">
00491 <td class=\"editcommentbottom\"><input type=\"submit\" name=\"save_comment\" value=\"" . i18n("Save comment") . "\" /></td>
00492 <td class=\"editcommentbottom wikirightalign\"><input type=\"reset\" value=\"" . i18n("Reset") . "\" /></td>
00493 </tr>
00494 </table>
00495 </form>";
00496 }
00497 return $data;
00498 }
00499 }
00500 ?>