FALSE,
'nodate'=>FALSE,
'below' =>FALSE,
'above' =>FALSE,
'reply' =>FALSE,
'_args' =>array()
);
foreach(func_get_args() as $arg)
plugin_pcomment_check_arg($arg, $params);
$vars_page = isset($vars['page']) ? $vars['page'] : '';
if (isset($params['_args'][0]) && $params['_args'][0] != '') {
$page = $params['_args'][0];
} else {
$raw_vars_page = strip_bracket($vars_page);
$page = sprintf(PLUGIN_PCOMMENT_PAGE, $raw_vars_page);
$raw_page = strip_bracket($page);
if (!is_page($raw_page)) {
// If the page doesn't exist, search backward-compatible page
// If only compatible page exists, set the page as comment target
$page_compat = sprintf(PLUGIN_PCOMMENT_PAGE_COMPATIBLE, $raw_vars_page);
if (is_page(strip_bracket($page_compat))) {
$page = $page_compat;
}
}
}
$count = isset($params['_args'][1]) ? intval($params['_args'][1]) : 0;
if ($count == 0) $count = PLUGIN_PCOMMENT_NUM_COMMENTS;
$_page = get_fullname(strip_bracket($page), $vars_page);
if (!is_pagename($_page))
return sprintf($_pcmt_messages['err_pagename'], htmlsc($_page));
$dir = PLUGIN_PCOMMENT_DIRECTION_DEFAULT;
if ($params['below']) {
$dir = 0;
} elseif ($params['above']) {
$dir = 1;
}
list($comments, $digest) = plugin_pcomment_get_comments($_page, $count, $dir, $params['reply']);
if (PKWK_READONLY) {
$form_start = $form = $form_end = '';
} else {
// Show a form
if ($params['noname']) {
$title = $_pcmt_messages['msg_comment'];
$name = '';
} else {
$title = $_pcmt_messages['btn_name'];
$name = '';
}
$radio = $params['reply'] ?
'' : '';
$comment = '';
$s_page = htmlsc($page);
$s_refer = htmlsc($vars_page);
$s_nodate = htmlsc($params['nodate']);
$s_count = htmlsc($count);
$form_start = '
' . "\n";
}
if (! is_page($_page)) {
$link = make_pagelink($_page);
$recent = $_pcmt_messages['msg_none'];
} else {
$msg = ($_pcmt_messages['msg_all'] != '') ? $_pcmt_messages['msg_all'] : $_page;
$link = make_pagelink($_page, $msg);
$recent = ! empty($count) ? sprintf($_pcmt_messages['msg_recent'], $count) : '';
}
if ($dir) {
return '' .
'
' . $recent . ' ' . $link . '
' . "\n" .
$form_start .
$comments . "\n" .
$form .
$form_end .
'
' . "\n";
} else {
return '' .
$form_start .
$form .
$comments. "\n" .
$form_end .
'
' . $recent . ' ' . $link . '
' . "\n" .
'
' . "\n";
}
}
function plugin_pcomment_insert()
{
global $vars, $now, $_title_updated, $_no_name, $_pcmt_messages;
$refer = isset($vars['refer']) ? $vars['refer'] : '';
$page = isset($vars['page']) ? $vars['page'] : '';
$page = get_fullname($page, $refer);
if (! is_pagename($page))
return array(
'msg' =>'Invalid page name',
'body'=>'Cannot add comment' ,
'collided'=>TRUE
);
check_editable($page, true, true);
$ret = array('msg' => $_title_updated, 'collided' => FALSE);
$msg = str_replace('$msg', rtrim($vars['msg']), PLUGIN_PCOMMENT_FORMAT_MSG);
$name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
$name = ($name == '') ? '' : str_replace('$name', $name, PLUGIN_PCOMMENT_FORMAT_NAME);
$date = (! isset($vars['nodate']) || $vars['nodate'] != '1') ?
str_replace('$now', $now, PLUGIN_PCOMMENT_FORMAT_NOW) : '';
if ($date != '' || $name != '') {
$msg = str_replace("\x08" . 'MSG' . "\x08", $msg, PLUGIN_PCOMMENT_FORMAT_STRING);
$msg = str_replace("\x08" . 'NAME' . "\x08", $name, $msg);
$msg = str_replace("\x08" . 'DATE' . "\x08", $date, $msg);
}
$reply_hash = isset($vars['reply']) ? $vars['reply'] : '';
if ($reply_hash || ! is_page($page)) {
$msg = preg_replace('/^\-+/', '', $msg);
}
$msg = rtrim($msg);
if (! is_page($page)) {
$postdata = '[[' . htmlsc(strip_bracket($refer)) . ']]' . "\n\n" .
'-' . $msg . "\n";
} else {
$postdata = get_source($page);
$count = count($postdata);
$digest = isset($vars['digest']) ? $vars['digest'] : '';
if (md5(join('', $postdata)) !== $digest) {
$ret['msg'] = $_pcmt_messages['title_collided'];
$ret['body'] = $_pcmt_messages['msg_collided'];
}
$start_position = 0;
while ($start_position < $count) {
if (preg_match('/^\-/', $postdata[$start_position])) break;
++$start_position;
}
$end_position = $start_position;
$dir = isset($vars['dir']) ? $vars['dir'] : '';
// Find the comment to reply
$level = 1;
$b_reply = FALSE;
if ($reply_hash != '') {
while ($end_position < $count) {
$matches = array();
if (preg_match('/^(\-{1,2})(?!\-)(.*)$/', $postdata[$end_position++], $matches)
&& md5($matches[2]) === $reply_hash)
{
$b_reply = TRUE;
$level = strlen($matches[1]) + 1;
while ($end_position < $count) {
if (preg_match('/^(\-{1,3})(?!\-)/', $postdata[$end_position], $matches)
&& strlen($matches[1]) < $level) break;
++$end_position;
}
break;
}
}
}
if ($b_reply == FALSE)
$end_position = ($dir == '0') ? $start_position : $count;
// Insert new comment
array_splice($postdata, $end_position, 0, str_repeat('-', $level) . $msg . "\n");
if (PLUGIN_PCOMMENT_AUTO_LOG) {
$_count = isset($vars['count']) ? $vars['count'] : '';
plugin_pcomment_auto_log($page, $dir, $_count, $postdata);
}
$postdata = join('', $postdata);
}
page_write($page, $postdata, PLUGIN_PCOMMENT_TIMESTAMP);
if (PLUGIN_PCOMMENT_TIMESTAMP) {
if ($refer != '') pkwk_touch_file(get_filename($refer));
put_lastmodified();
}
return $ret;
}
// Auto log rotation
function plugin_pcomment_auto_log($page, $dir, $count, & $postdata)
{
if (! PLUGIN_PCOMMENT_AUTO_LOG) return;
$keys = array_keys(preg_grep('/(?:^-(?!-).*$)/m', $postdata));
if (count($keys) < (PLUGIN_PCOMMENT_AUTO_LOG + $count)) return;
if ($dir) {
// Top N comments (N = PLUGIN_PCOMMENT_AUTO_LOG)
$old = array_splice($postdata, $keys[0], $keys[PLUGIN_PCOMMENT_AUTO_LOG] - $keys[0]);
} else {
// Bottom N comments
$old = array_splice($postdata, $keys[count($keys) - PLUGIN_PCOMMENT_AUTO_LOG]);
}
// Decide new page name
$i = 0;
do {
++$i;
$_page = $page . '/' . $i;
} while (is_page($_page));
page_write($_page, '[[' . $page . ']]' . "\n\n" . join('', $old));
// Recurse :)
plugin_pcomment_auto_log($page, $dir, $count, $postdata);
}
// Check arguments
function plugin_pcomment_check_arg($val, & $params)
{
if ($val != '') {
$l_val = strtolower($val);
foreach (array_keys($params) as $key) {
if (strpos($key, $l_val) === 0) {
$params[$key] = TRUE;
return;
}
}
}
$params['_args'][] = $val;
}
function plugin_pcomment_get_comments($page, $count, $dir, $reply)
{
global $_msg_pcomment_restrict;
if (! check_readable($page, false, false))
return array(str_replace('$1', $page, $_msg_pcomment_restrict));
$reply = (! PKWK_READONLY && $reply); // Suprress radio-buttons
$data = get_source($page);
$data = preg_replace('/^#pcomment\(?.*/i', '', $data); // Avoid eternal recurse
if (! is_array($data)) return array('', 0);
$digest = md5(join('', $data));
// Get latest N comments
$num = $cnt = 0;
$cmts = $matches = array();
if ($dir) $data = array_reverse($data);
foreach ($data as $line) {
if ($count > 0 && $dir && $cnt == $count) break;
if (preg_match('/^(\-{1,2})(?!\-)(.+)$/', $line, $matches)) {
if ($count > 0 && strlen($matches[1]) == 1 && ++$cnt > $count) break;
// Ready for radio-buttons
if ($reply) {
++$num;
$cmts[] = $matches[1] . "\x01" . $num . "\x02" .
md5($matches[2]) . "\x03" . $matches[2] . "\n";
continue;
}
}
$cmts[] = $line;
}
$data = $cmts;
if ($dir) $data = array_reverse($data);
unset($cmts, $matches);
// Remove lines before comments
while (! empty($data) && substr($data[0], 0, 1) != '-')
array_shift($data);
$comments = convert_html($data);
unset($data);
// Add radio buttons
if ($reply)
$comments = preg_replace('/' . "\x01" . '(\d+)' . "\x02" . '(.*)' . "\x03" . '/',
'',
$comments);
return array($comments, $digest);
}