[pmwiki-users] Password protect page with editors' password action

Peter Bowers pbowers at pobox.com
Sun Jul 15 12:36:21 CDT 2012


On Sun, Jul 15, 2012 at 12:53 AM, Alex Eftimiades
<alexeftimiades at gmail.com> wrote:
> I have been trying to make a button that password protects a page with the
> editors' password, or un-protects it if it is already protected.
>
> I tried putting this at the bottom of config.php, but it just seemed to mess
> up my pages:
> $HandleActions['switchauth'] = 'switchauth';  # if url contains
> action=myaction call HandleMyAction timely
> $HandleAuth['switchauth'] = ' ';              # authorization level $auth
> for HandleMyAction
>
> function switchauth($pagename, $auth) {     # parameters (signature) of
> handler function required by PMWiki
> #  Lock(2);
>   $pagestuff = RetrieveAuthPage($pagename, 'attr', false);
>   $old=$pagestuff;
>   if($pagestuff['protected']==true){
>     $pagestuff['passwdread'] = 'test';
>     $pagestuff['protected']=false;
>   }
>   else{
>     $pagestuff['passwdread'] = 'test';
>     $pagestuff['protected']=true;
>   }
>   UpdatePage($pagename, $old, $pagestuff);
>   Redirect($pagename);
> #  Lock(0);
> }
>
> Can someone please help me?

Try this:

===(snip)===
$HandleActions['switchauth'] = 'switchauth';  # if url contains
action=myaction call HandleMyAction timely
$HandleAuth['switchauth'] = 'attr';              # authorization level
$auth for HandleMyAction

function switchauth($pagename, $auth) {     # parameters (signature)
of handler function required by PMWiki
    Lock(2);
    $pagestuff = RetrieveAuthPage($pagename, 'attr', false);
    $old=$pagestuff;
    if($pagestuff['protected']=='true'){
        // (Ideally we would strip out the exact passwd rather than blanking
        //  the whole thing.)
        $pagestuff['passwdread'] = ''; // set it to no passwd (loses
any other passwd - sorry)
        $pagestuff['protected']='false';
        #echo "DEBUG: UNprotecting<br>\n";
    } else {
        // (Ideally we would append the passwd rather than replacing what may
        //  have existed before.)
        $pagestuff['passwdread'] = crypt('test');
        $pagestuff['protected']='true';
        #echo "DEBUG: Protecting<br>\n";
    }
    UpdatePage($pagename, $old, $pagestuff);
    #echo "DEBUG: Updated<br>\n";
    Redirect($pagename);
    Lock(0);
}

$FmtPV['$protected'] = 'PageIsProtected($pagename,"edit")';
function PageIsProtected($pagename, $level)
{
  global $PCache, $PasswdVarAuth, $FmtV;
  $page = $PCache[$pagename];
  if (!isset($page['=passwd'][$level])) {
    $page = RetrieveAuthPage($pagename, 'ALWAYS', false, READPAGE_CURRENT);
    if ($page) PCache($pagename, $page);
  }
  $protected = @$page['protected']; // either "true" or something else
  return ($protected);
}

===(snip)===

Note that if someone sets another password on a page (pmwiki allows
multiple passwords) and then this action is used on that page then the
additional password will be lost.  I've referred to that problem in
the comments -- not a big deal to fix, but I don't remember if a space
or something else is the separator between multiple passwords.

Since you've got the convenient $page['protected'] in there I went
ahead and used that to set the {$protected} PV and used it in
Site.PageActions like this (accesskey is pretty non-standard):

===(snip)===
(:if auth attr:)
* %item rel=nofollow class=switchauth accesskey="s"%
[[{*$FullName}?action=switchauth | (:if1 equal {$protected} "true":)
Unprotect (:else1:) Protect (:if1end:) ]]
(:ifend:)
===(snip)===

If this ends up working for you, please publish it as a recipe in the
cookbook -- it seems like something that someone else may be able to
use in various contexts...

-Peter



More information about the pmwiki-users mailing list