'HandleVoix'));
SDVA($HandleAuth, array('voix' => 'edit'));
SDV($HTMLStylesFmt['voix'], "
a.voixlink { text-decoration: none; padding: 0 .1em; margin: 2px; }
a.voixdown { color: red; border: 1px solid red; background-color: #ffdddd;}
a.voixup { color: green; border: 1px solid green; background-color: #ddffdd;}
");
$MarkupExpr['voix'] = 'MxVoix($pagename, $args[0], $args[1])';
function MxVoix($pagename, $id, $remotepage)
{
global $VoixHTML, $VoixSummary, $VoixPrefix, $HTMLStylesFmt;
SDV($VoixPrefix, 'Voix');
SDV($VoixHTML, "-{neg} +{pos}");
$p = ($remotepage>'')? MakePageName($pagename, $remotepage) : $pagename;
$votes = PageTextVar($p, "{$VoixPrefix}_$id");
preg_match('/(-\d+)?\s*(\+\d+)?/', $votes, $m);
$neg = abs(intval($m[1]));
$pos = intval($m[2]);
$count = $pos+$neg;
$votes_map = array(
'{id}' => $id,
'{neg}' => $neg,
'{pos}' => $pos,
'{count}' => $count,
'{diff}' => $pos-$neg,
'{percent}' => $count ? round($pos/$count*100) : 0,
);
$out = str_replace(array_keys($votes_map), array_values($votes_map), $VoixHTML);
return Keep(FmtPageName($out, $p));
}
function HandleVoix($pagename, $auth="edit")
{
global $ChangeSummary, $Now, $VoixRecalcIP, $VoixPrefix;
SDV($VoixPrefix, 'Voix');
$id = @$_GET['id'];
$vote = @$_GET['vote'];
if(!preg_match('/^\\w+$/', $id)
|| !preg_match('/^(up|down)$/', $vote) )
Abort('Invalid counter ID or unrecognized vote.');
$page = RetrieveAuthPage($pagename, $auth);
if(!$page) Abort('Page could not be retrieved.');
$voix_id = "{$VoixPrefix}_$id";
if(!preg_match("/\(:$voix_id:\s*(-\d+)?\s*(\+\d+)?\s*:\)/", $page['text'], $m))
Abort('Counter ID not found.');
$new = $page;
$search = $m[0];
$neg = abs(intval($m[1]));
$pos = intval($m[2]);
if(IsEnabled($VoixRecalcIP, 1) )
{
$csums = preg_grep("/^csum:/", array_keys($new) );
rsort($csums);
foreach($csums as $csum)
{
if (!preg_match("/^$voix_id :\s+(up|down)\s*$/", $new[$csum], $vmatch)) continue;
$time = substr($csum, 5);
if($new["host:$time"] != $_SERVER['REMOTE_ADDR']) continue;
$prev_vote = $vmatch[1];
if($vote == $prev_vote) # already voted
Redirect($pagename);
if($prev_vote == 'up') $pos--;
elseif($prev_vote == 'down') $neg--;
// else Abort("Doesn't look like vote: '$prev_vote'.");
else continue; # probably not a formatted vote from Voix.
break; # just the latest vote will be replaced.
}
}
if ($vote == 'down') $neg++;
elseif($vote == 'up') $pos++;
$replace = "(:$voix_id:-$neg+$pos:)";
if($replace!=$search)
{
$new['text'] = str_replace($search, $replace, $new['text']);
$csum = "$voix_id : $vote";
$new['csum'] = $new["csum:$Now"] = $ChangeSummary = $csum;
$_POST['diffclass']='minor';
UpdatePage($pagename, $page, $new);
}
Redirect($pagename);
}