<?php if (!defined('PmWiki')) exit();
/*
 * TraceTrail3 - Sets a trail with the recent visited wiki pages
 * Copyright 2006 by Americo Albuquerque (aalbuquerque@lanowar.sytes.net)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * 2007-01-07: Minor modification adding an option to skip the current page  
 * and leave it out of the trail.  Bill Reveile, Austin TX
 * Put $TraceSkipCurr=1; in config.php to enable it.
 * 2008-06-23: Major modifications Hans Bracker:
 * added cookie to preserve tracetrail from session to session;
 * added $EnableTraceHistory, default true; set to 0 to disable trace history in cookie;
 * reworking link format;
 * added {$Trace0}, {$Trace1}, {$Trace2}, {$Trace3} etc page variables for trace pagenames;
 * added ?action=cleartrace for deleting trace history (in session and cookie)
 */

$RecipeInfo['TraceTrail']['Version'] = '2008-06-23';

SDV($TraceLinkFmt, '* [[{$PageName}|+]]');
SDV($TraceSepFmt, "\n");
SDV($TraceCount, 5);
SDV($TraceSkipCurr, 1);
SDV($TraceCookie, $CookiePrefix.'history');
SDV($TraceCookieExpires,$Now+60*60*24*30);
SDV($TraceCookieDir,'/');

$TracePages = array();
@session_start();	
if (isset($_SESSION['history']))
	$TracePages = explode(',', $_SESSION['history']);
else if (@$_COOKIE[$TraceCookie])
	 $TracePages = explode(',', $_COOKIE[$TraceCookie]);
foreach ($TracePages as $i => $t)
	$FmtPV['$Trace'.$i] = "'$t'";
	
$TracePages = array_reverse(array_unique(array_reverse(array_merge($TracePages,array($pagename)))));
if (count($TracePages) > $TraceCount)
	$TracePages = array_slice($TracePages, count($TracePages)-$TraceCount, $TraceCount);

$_SESSION['history'] = $histcookie = implode(',', $TracePages);
if (IsEnabled($EnableTraceHistory,1))
	setcookie ($TraceCookie, $histcookie, $TraceCookieExpires, $TraceCookieDir);
else if (@$_COOKIE[$TraceCookie])
	setcookie ($TraceCookie, "", time()-60000, $TraceCookieDir);

Markup('tracetrails', '<links', '/\(:tracetrails?:\)/se', "TraceTrail(\$pagename)");
	
function TraceTrail($pagename) {
	global $TraceLinkFmt, $TraceSepFmt, $TracePages, $TraceSkipCurr;
	$sc = (isset($TraceSkipCurr) ?  1 : 0);
	$trace = array_slice($TracePages, 0, count($TracePages) - $sc);
	$out = '';
	foreach ($trace as $t) {
		$t = str_replace('{$PageName}', $t, $TraceLinkFmt);
		$out .= $TraceSepFmt.$t;
	}
	return  Keep(MarkupToHTML($pagename, $out));
}

$HandleActions['cleartrace'] = 'ClearTraceTrail';
function ClearTraceTrail($pagename) {
	global $TraceCookie, $TraceCookieDir;
	$_SESSION['history'] = "";
	setcookie ($TraceCookie, "", time()-60000, $TraceCookieDir);
	Redirect($pagename);
	exit;
}