<?php if (!defined('PmWiki')) exit();
/*
        * Copyright *
        Copyright 2006-2011, by Antony Templier (z3l@free.fr)
        Also, a lot of lines of code come from Patrick R. Michaud (authuser.php)

        Version 0.5 ( works with pmwiki-2.2.29 )

        * License *
        Same as PmWiki (GNU GPL)

        * Description *
        For sites with authenticated users (with AuthUser), this script
        allow you to use author name ($Author) stored in the $SiteGroup.AuthUser
        page and to automatically set the author name in the author field
        in case it's blank.

        * Installation Instructions *
        1- Install authuser.php before
           See -  http://www.pmwiki.org/wiki/Cookbook/AuthUser
        2- Put this script (storedauthname.php) in your cookbook directory
        3- Add the line below in your local/config.php :

            include_once($FarmD.'cookbook/storedauthname.php');

        * Usage *
        To set an author name:

        Edit your AuthUser page :
        -Before PmWiki 2.2.0-beta58 : $SiteGroup.AuthUser
                                      (usually Site.AuthUser)
        -With and after PmWiki 2.2.0-beta58 : $SiteAdminGroup.AuthUser
                                              (usually SiteAdmin.AuthUser)

        and add lines like this one:

            userid::authorname

        Where userid is $AuthId and authorname is $Author separated
        by two colons.
        The script set automatically the author name during edition.
        If no author name is found, the author name is set to the user id.
        To enforce author tracking for authenticated users, you can set the
        variable $ForceAuthorTracking in your local/config.php just before
        the include_once function, like this :

            $ForceAuthorTracking=1; # 1 to enable, 0 (default) to disable
            include_once(($FarmD.'cookbook/storedauthname.php');

*/
$RecipeInfo['StoredAuthName']['Version'] = '2011-08-05';

SDV($ForceAuthorTracking,0);

$page = RetrieveAuthPage($pagename, 'read', false, READPAGE_CURRENT);

if (
##$action == 'edit' &&
   ##$page['=auth']['edit'] &&
   $page['=auth']['read'] &&
   ($ForceAuthorTracking || !@$_COOKIE['author']) ){
    SetAuthorName($pagename);
}

function SetAuthorName($pagename) {

    global $Author, $AuthUserPageFmt, $AuthId;
    global $SiteGroup, $SiteAdminGroup;

    @session_start();
    if (@$_SESSION['authid']){

        SDV($AuthUserPageFmt, '$SiteAdminGroup.AuthUser');
        $pn = FmtPageName($AuthUserPageFmt, $pagename);

        # For PmWiki < 2.2.0-beta58, the AuthUser page is in $Site group,
        # instead of $SiteAdminGroup for now.
        if(!PageExists($pn)) {
            SDV($AuthUserPageFmt, '$SiteGroup.AuthUser');
            $pn = FmtPageName($AuthUserPageFmt, $pagename);
        }

        $apage = ReadPage($pn, READPAGE_CURRENT);

        if ($apage && preg_match_all("/\n\\s*([@\\w][^\\s:]*):{2,2}(.*)/",
                                $apage['text'], $matches, PREG_SET_ORDER)) {
             foreach($matches as $m){
                #if ($m[1] == @$_SESSION['authid']){
                if ($m[1] == $AuthId) {
                    $Author = $m[2];
                    break;
                }
            }
        }

        if (!$Author){
            #$Author = @$_SESSION['authid'];
            $Author = $AuthId;
        }

        setcookie('author',$Author,0,'/');
    }
}
?>