<?php if (!defined('PmWiki')) exit();
/*
 * ActionLog - Maintain a page log of wiki actions
 * Copyright 2005-2006 by D.Faure (dfaure@cpan.org)
 * Inspired from a script from Christophe David (www.christophedavid.org)
 *
 * 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.
 *
 * See http://www.pmwiki.org/wiki/Cookbook/ActionLog for info.
 */
define(ACTIONLOG_VERSION, '20060803');

## The optional $dir parameter allows this function to be called
## via register_shutdown_function (which sometimes changes directories
## on us).
function LogAction($pagename, $action, $target = '', $dir = '') {
  global $ActionLogFilterPattern, $ActionLogPageName, $ActionLogSelfExclude,
         $ActionLogArchive, $ActionLogArchivePageName,
         $Now, $ActionLogLineFmt,
         $ActionLogAppend, $ActionLogLinesMax,
         $LastModFile;
  if(isset($ActionLogFilterPattern) && preg_match($ActionLogFilterPattern, $action, $m)) return;

  if(!$target) {
    global $ActionLogTrackAction, $IsPagePosted;
    if(isset($ActionLogTrackAction) && $action == $ActionLogTrackAction) return;
    if(!$IsPagePosted && $action == 'edit') return;
    $target = '{$FullName}';
  }
  if(!$pagename) $pagename = ResolvePageName($pagename);
  $pagename = FmtPageName('{$FullName}', $pagename);
  SDV($ActionLogPageName, '{$SiteGroup}.ActionLog');
  $alpn = FmtPageName($ActionLogPageName, $pagename);
  if(IsEnabled($ActionLogSelfExclude, 1) && ($pagename == $alpn)) return;
  if($dir) chdir($dir);
  Lock(2);
  $page = ReadPage($alpn, READPAGE_CURRENT);
  $text = $page['text'];
  $lines = substr_count($text, "\n") + 1;
  $lmf = $LastModFile; $LastModFile = ''; # disable file mod tracking
  if(($lines >= $ActionLogLinesMax) && IsEnabled($ActionLogArchive, 0)) {
    SDV($ActionLogArchivePageName, "$ActionLogPageName-$Now");
    $archpn = FmtPageName(strftime($ActionLogArchivePageName, $Now), $pagename);
    WritePage($archpn, $page);
    $text = '';
    $lines = 1;
  }
  SDV($ActionLogLineFmt, ' %Y-%m-%d %H:%M:%S - REMOTE_ADDR - {$AuthId} - {$Author} - ACTION - [[TARGET]]');
  $l = FmtPageName(str_replace(array('REMOTE_ADDR', 'ACTION', 'TARGET'),
                               array($_SERVER['REMOTE_ADDR'], $action, $target),
                               strftime($ActionLogLineFmt, $Now)), $pagename);
  SDV($ActionLogAppend, false);
  $text = $ActionLogAppend ?  "$text\n$l" : "$l\n$text";
  if((@$ActionLogLinesMax > 0) && ($lines >= $ActionLogLinesMax))
    $text = (string)($ActionLogAppend ?
                     substr($text, strpos($text, "\n") + 1) :
                     substr($text, 0, strrpos($text, "\n")));
  $page['text'] = $text;
  WritePage($alpn, $page);
  $LastModFile = $lmf; # restore file mod tracking
  Lock(0);
}

register_shutdown_function('LogAction', $pagename, $action, '', getcwd());

if(IsEnabled($ActionLogTrackUrl, 0)) {
  SDV($ActionLogTrackAction, 'track');
  SDV($ActionLogTrackParam, 'url');
  $HandleActions[$ActionLogTrackAction] = 'LogTrackUrl';
  $UrlLinkFmt = "<a class='urllink' href='?action=$ActionLogTrackAction&$ActionLogTrackParam='\$LinkUrl' rel='nofollow'>\$LinkText</a>";  

  function LogTrackUrl($pagename) {
    global $action;
    $url = $_REQUEST[$ActionLogTrackParam];
    LogAction($pagename, $action, $url);
    header("Location: $url");
    exit;
  }
}