disable comments
$NB_COM_CLOSE = 2592000; # number of seconds after which comments are closed
$NB_COM_MAX_SIZE = 2000; # max number of chars in a comment
$o_umask = umask(0); # for mkdir()
# Display the comment form
function form($id) {
global $NB_COM;
$me = $_SERVER['PHP_SELF'];
$author = $_POST['author'];
$url = $_POST['url'];
$com = $_POST['comment'];
$epoch = id2epoch($id);
if ($NB_COM == 0) {
echo "
Comments are temporary closed
";
return;
}
if (closed($epoch)) {
echo "Comments are closed
";
echo "If you really, really want to comment, please ";
echo "mail miek@miek.nl";
return;
}
if (strlen($url) == 0) {
$url = "http://";
}
echo <<Leave a Comment
EOF;
}
# submit a comment
function submit() {
global $NB_COM_BASE;
global $NB_COM;
$link = "" . $_POST['author'] . "";
$com = bb_tags(strip_html_tags($_POST['comment']));
$dir = "$NB_COM_BASE/" . $_POST['comment_id'] . "/new/";
$ok = "$NB_COM_BASE/" . $_POST['comment_id'] . "/ok/";
if ($NB_COM == 1) {
if (!file_exists($dir)) {
mkdir($dir, 0775, TRUE);
}
if (!file_exists($ok)) {
mkdir($ok, 0775, TRUE);
}
}
$prefix = time() . "_";
$err = form_check();
$author = $_POST['author'];
$date = strftime($NB_TIME, time());
if (!empty($err)) {
# errors
echo '';
foreach($err as $e) {
echo $e, "
";
}
echo '
';
} else {
echo '';
echo "Your comment is submitted and awaits moderation";
if ($NB_COM != 1) {
echo "Sorry, commenting is temporary disabled";
}
echo '
';
$_POST['author'] = "";
$_POST['url'] = "";
$_POST['comment'] = "";
if ($NB_COM == 1) {
$file = tempnam($dir, $prefix);
chmod($file, 0640);
file_put_contents($file, "$link\n$com");
}
}
echo <<
Submitted
$author
$date
$com
EOF;
}
# preview a submit
function preview() {
global $NB_TIME;
$date = strftime($NB_TIME, time());
$author = "" . $_POST['author'] . "";
$com = bb_tags(strip_html_tags($_POST['comment']));
$err = form_check();
if (!empty($err)) {
# errors
echo '';
foreach($err as $e) {
echo $e, "
";
}
echo '
';
}
echo <<
Preview
$author
$date
$com
EOF;
}
# check $website starts with http:// does not contain ../'s
function form_check() {
global $NB_COM_MAX_SIZE;
$err = array();
if (strlen($_POST['author']) == 0) {
$err[] = "A name is required";
}
if (strlen($_POST['comment']) > $NB_COM_MAX_SIZE) {
$err[] = "Comment is too large";
$_POST['comment'] = substr($text, 0, $NB_COM_MAX_SIZE);
}
return $err;
}
# Check the comment directory for this article
# return an array with the commens
function gather($id) {
global $NB_COM_BASE;
global $NB_TIME;
$dir="$NB_COM_BASE/$id/ok";
$comment = array();
$matched = array();
$contents = array();
$i = 0;
foreach (glob("$dir/*") as $com) {
# $matched[1] has epoch
preg_match("/.*ok\/(.*)_/", $com, $matched);
if (strlen($matched[1]) != 0) {
$contents = explode("\n", file_get_contents($com));
$comment[$i]['author'] = $contents[0];
$comment[$i]['date'] = strftime($NB_TIME, $matched[1]);
$comment[$i]['comment'] = implode("\n", array_slice($contents, 1));
$i++;
}
}
return $comment;
}
# Give back the html of the given comments
function show($comment) {
$i = 0;
echo '';
foreach ($comment as $com) {
echo "";
if ($i % 2 == 0) {
echo '
"; echo $com['date']; echo '
$1", $text); # [code] - code examples $text = preg_replace("/\[code\]((.|\n)*?)\[\/code\]/", "$1
", $text); # [quote] - quote something $text = preg_replace("/\[quote\]((.|\n)*?)\[\/quote\]/", " ", $text); # [url]link[/url] $text = preg_replace("/\[url\]((.|\n)*?)\[\/url\]/", "$1", $text); # [url=domain]linkname[/url] $text = preg_replace("/\[url=(.*)\]((.|\n)*?)\[\/url\]/", "$2", $text); $text = nl2br($text); return $text; } # convert an article ID to epoch function id2epoch($id) { $m = array(); preg_match("/e(.*?)-(.*?)-(.*?)T(.*?)_(.*?)_(.*?)\.txt/", $id, $m); return strtotime($m[1] . "-" . $m[2] . "-" . $m[3] . " " . $m[4] . ":" . $m[5] . ":" . $m[6]); } # return FALSE when comments are OK, otherwise TRUE function closed($t) { global $NB_COM_CLOSE; if (time() - $t > $NB_COM_CLOSE) { return TRUE; } return FALSE; } # return a comment string in HTML # the string is with strike through if # commenting is closed otherwise it is # normal function commentstr($id, $permalink) { global $NB_COM; $epoch = id2epoch($id); $link = ""; if ($NB_COM != 1) { return "$linkComments: "; } if (closed($epoch)) { return "$linkComments: "; } else { return "${link}Comments: "; } } ?>