<?php if (!defined('PmWiki')) exit();
/*
 * user_last_action - Record the last action for each user so stale accounts
 *                    can be detected.
 * 
 * Author: Frank Graffagnino - frankie@graffagnino.net
 * 
 * Copyright 2007 by Frank Graffagnino
 * 
 * This recipe used actionlog (http://www.pmwiki.org/wiki/Cookbook/ActionLog)
 * and totalcounter (http://www.pmwiki.org/wiki/Cookbook/TotalCounter) recipes
 * as inspiration.  Thanks to the authors of those recipes.
 *
 * 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.

 */
$RecipeInfo['user_last_action']['Version'] = '2007-03-25'; 

# Recipe defaults
SDV($LastUserActionPageName, 'Profiles.{$AuthId}-LastAction');
SDV($LastUserActionIgnoreUsers, array('admin'));
SDV($LastUserActionLineFmt, ' ACTION - [[TARGET]] on %Y-%m-%d %H:%M:%S from REMOTE_ADDR');

# This function records the last action to the page
# for the current authenticated user
function LastUserAction_SetInfo($pagename, $action, $dir='') {
  global $LastUserActionPageName;
  global $LastUserActionIgnoreUsers;
  global $LastUserActionLineFmt;
  global $Now;
 
 ###### Pseudo-code
 # If the current action is from an un-authenticated user, return
 # Get the current authid using priortized sources
 # If the user is in the ignore-list, return
 # If page is being edited but not yet posted, return
 # Set valid target
 # Call ResolvePageName
 # Call FmtPageName on pagename we want to use to track
 # Call MakePageName to fix any capitalization
 # call readpage to read in page (or create structure)
 # perform string substitutions on format
 # Call FmtPageName to convert and perform pmwiki substitutions
 # put resulting string in text element of page structure
 # Call writepage to write out the result

 # If the current action is from an un-authenticated user, return
  if (!isset ($_SESSION['authid'])) { return; }

 # Get the current authid using priortized sources
  # NOTE: I stole this block from totalcounter 1.8a...
  if (isset ($AuthId)) {
      $lastuseraction_user = $AuthId;
  } else {
      if (isset ($Author)) {
          $lastuseraction_user = $Author;
      } else {
          @ session_start();
          if (isset ($_SESSION['authid'])) {
              $lastuseraction_user = $_SESSION['authid'][0];
          } else {
              return;
          }
      }
  }

 # If the user is in the ignore-list, return
  if(isset($LastUserActionIgnoreUsers) && in_array($lastuseraction_user, $LastUserActionIgnoreUsers)) return;

 # If page is being edited but not yet posted, return
  if(!$IsPagePosted && $action == 'edit') return;

 # Set valid target
  $target = '{$FullName}';

 # Call ResolvePageName
  # Not sure why I should do this, but saw that others did --FG
  if(!$pagename) $pagename = ResolvePageName($pagename);
  $pagename = FmtPageName('{$FullName}', $pagename);

 # Call FmtPageName on pagename we want to use to track
  $lastuseraction_pn = FmtPageName($LastUserActionPageName, $pagename);

 # Call MakePageName to fix any capitalization
  $lastuseraction_pn = MakePageName($pagename, $lastuseraction_pn);

 # call readpage to read in page (or create structure)
  if($dir) chdir($dir);
  Lock(2);
  $lastuseraction_page = ReadPage($lastuseraction_pn, READPAGE_CURRENT);
  $lastuseraction_lmf = $LastModFile; $LastModFile = ''; # disable file mod tracking

 # perform string substitutions on format
  $lastuseraction_text = str_replace(array('REMOTE_ADDR',           'ACTION',  'TARGET'),
                                     array($_SERVER['REMOTE_ADDR'],  $action,   $target),
                                     strftime($LastUserActionLineFmt, $Now));

 # Call FmtPageName to convert and perform pmwiki substitutions
  $lastuseraction_text = FmtPageName($lastuseraction_text, $pagename);

 # put resulting string in text element of page structure
  $lastuseraction_page['text'] = $lastuseraction_text;

 # Call writepage to write out the result
  WritePage($lastuseraction_pn, $lastuseraction_page);
  $LastModFile = $lastuseraction_lmf; # restore file mod tracking
  Lock(0);
}


# Register a shutdown function that will log the action after page
# processing is complete
register_shutdown_function('LastUserAction_SetInfo', $pagename, $action, getcwd());