[Home]DirkJanssen/LoginLogoutPatch

UseModWiki | DirkJanssen | RecentChanges | Preferences

See also: LoginRework

Just dumped this here for now, will come back to this later... The header of the patch explains it pretty well


== Usemod loginlogout patch, v3 ==

This patch solves all problems with logins ... for me :-) 
It incorporates part of the Usemod Login-patch by DavidAndel. Thanks.

Layout of the system: 
* Users have to login to have preferences
* Both login names and old-style login IDs can be used for login
* Users are not required to have passwords
* Users can change their user names, but not their unique IDs
* User names must be unique
* The ID is stored in a cookie (as before), which is stored upon login
* A logout is provided to remove the cookie again

This patch requires Unix 'grep' and 'head'. I think one could easily
remove 'head' from this code, and I would welcome a change for
that. Actually, I thought about it on my way to work and realised that,
if you have duplicate names disabled as in the patch, the 'head' is only there
for security reasons. You can delete it if you don't have it.

Also, I am not 100% sure whether the grep statement will work on Win
platforms, because it does `` grep SOMETHING */*db '' to traverse the
directory structure under $UserDir. An earlier version of this patch
had a slightly different UserDataFilename so that all user files are
directly under $UserDir. This worked fine for me.

dirk janssen, feb 2003

This patch is vs usemod 0.92

*** wiki.org	Sun Feb 16 12:09:59 2003
--- wiki.pl	Tue Feb 18 03:35:35 2003
***************
*** 873,878 ****
--- 873,895 ----
    return &ScriptLink("action=editprefs", T('Preferences'));
  }
  
+ sub GetOptPrefsLink {
+   if ($UserID > 400) {
+       return " | " . &ScriptLink("action=editprefs", T('Preferences'));
+   }
+   return "";
+ }
+ 
+ 
+ sub GetLoginLink {
+   if ($UserID <= 400) {
+     return &ScriptLink("action=login", T('Login'));  
+   } else {
+     return &ScriptLink("action=logout", 
+ 		       Ts('Logout %s ' , &GetParam("username","??")))
+     }
+ }
+ 
  sub GetRandomLink {
    return &ScriptLink("action=random", T('Random Page'));
  }
***************
*** 1094,1100 ****
      $bartext .= " | " . &GetPageLink($main);
    }
    $bartext .= " | " . &GetPageLink($RCName);
!   $bartext .= " | " . &GetPrefsLink();
    if (&GetParam("linkrandom", 0)) {
      $bartext .= " | " . &GetRandomLink();
    }
--- 1111,1118 ----
      $bartext .= " | " . &GetPageLink($main);
    }
    $bartext .= " | " . &GetPageLink($RCName);
!   $bartext .= &GetOptPrefsLink();
!   $bartext .= " | " . &GetLoginLink(); 
    if (&GetParam("linkrandom", 0)) {
      $bartext .= " | " . &GetRandomLink();
    }
***************
*** 1961,1967 ****
  
  sub UserDataFilename {
    my ($id) = @_;
- 
    return ""  if ($id < 1);
    return $UserDir . "/" . ($id % 10) . "/$id.db";
  }
--- 1979,1984 ----
***************
*** 2470,2478 ****
        &DoEditLinks();
      } elsif ($action eq "login") {
        &DoEnterLogin();
      } elsif ($action eq "newlogin") {
!       $UserID = 0;
!       &DoEditPrefs();  # Also creates new ID
      } elsif ($action eq "version") {
        &DoShowVersion();
      } else {
--- 2487,2496 ----
        &DoEditLinks();
      } elsif ($action eq "login") {
        &DoEnterLogin();
+     } elsif ($action eq "logout") {
+       &DoLogout();
      } elsif ($action eq "newlogin") {
!       &DoNewLogin();  # Also creates new ID
      } elsif ($action eq "version") {
        &DoShowVersion();
      } else {
***************
*** 2607,2619 ****
      print T($EditNote) . '<br>';  # Allow translation
    }
    print $q->submit(-name=>'Save', -value=>T('Save')), "\n";
!   $userName = &GetParam("username", "");
!   if ($userName ne "") {
!     print ' (', T('Your user name is'), ' ',
!           &GetPageLink($userName) . ') ';
!   } else {
!     print ' (', Ts('Visit %s to set your user name.', &GetPrefsLink()), ') ';
!   }
    print $q->submit(-name=>'Preview', -value=>T('Preview')), "\n";
  
    if ($isConflict) {
--- 2625,2631 ----
      print T($EditNote) . '<br>';  # Allow translation
    }
    print $q->submit(-name=>'Save', -value=>T('Save')), "\n";
!   print "   ";
    print $q->submit(-name=>'Preview', -value=>T('Preview')), "\n";
  
    if ($isConflict) {
***************
*** 2655,2684 ****
  }
  
  sub DoEditPrefs {
    my ($check, $recentName, %labels);
  
    $recentName = $RCName;
    $recentName =~ s/_/ /g;
-   &DoNewLogin()  if ($UserID < 400);
    print &GetHeader('', T('Editing Preferences'), "");
    print &GetFormStart();
    print GetHiddenValue("edit_prefs", 1), "\n";
    print '<b>' . T('User Information:') . "</b>\n";
!   print '<br>' . Ts('Your User ID number: %s', $UserID) . "\n";
!   print '<br>' . T('UserName:') . ' ', &GetFormText('username', "", 20, 50);
!   print ' ' . T('(blank to remove, or valid page name)');
!   print '<br>' . T('Set Password:') . ' ',
          $q->password_field(-name=>'p_password', -value=>'*', 
!                            -size=>15, -maxlength=>50),
!         ' ', T('(blank to remove password)'), '<br>(',
!         T('Passwords allow sharing preferences between multiple systems.'),
!         ' ', T('Passwords are completely optional.'), ')';
    if ($AdminPass ne '') {
!     print '<br>', T('Administrator Password:'), ' ',
!           $q->password_field(-name=>'p_adminpw', -value=>'*', 
!                              -size=>15, -maxlength=>50),
!           ' ', T('(blank to remove password)'), '<br>',
!           T('(Administrator passwords are used for special maintenance.)');
    }
    if ($EmailNotify) {
      print "<br>";
--- 2667,2709 ----
  }
  
  sub DoEditPrefs {
+   my ($isnewlogin) = @_;
    my ($check, $recentName, %labels);
  
    $recentName = $RCName;
    $recentName =~ s/_/ /g;
    print &GetHeader('', T('Editing Preferences'), "");
+   if ($UserID < 400) {
+       print "<p>" . T("You cannot change your preferences before you have logged in.") . "<p>";
+       print T("Click to ") . &ScriptLink("action=login", T('log in'));
+       print T(" or create a ") . &ScriptLink("action=newlogin", T('new account'));
+       print "<p><hr>";
+       print $UserID . "<p>";
+       print  &GetGotoBar('');
+       print &GetMinimumFooter();
+       return;
+   }
    print &GetFormStart();
    print GetHiddenValue("edit_prefs", 1), "\n";
    print '<b>' . T('User Information:') . "</b>\n";
!   if ($isnewlogin =~ /new/) {
!       print '<br>' . Ts('Your <b>New</b> User ID number: %s', $UserID) . "\n";
!   } else {    
!       print '<br>' . Ts('Your User ID number: %s', $UserID) . "\n"; }
!   print '<br>' . T('UserName:') . ' ', &GetFormText('username', $UserID, 20, 50);
!   # print ' ' . T('(blank to remove, or valid page name)');
!   print '<br>' . T('Set new password:') . ' ',
          $q->password_field(-name=>'p_password', -value=>'*', 
! 			   -size=>15, -maxlength=>50);
!   print '<br>', T('Blank to remove password.');
!   # T('Passwords allow sharing preferences between multiple systems.'),
!   print  " " . T('Passwords are completely optional.');
    if ($AdminPass ne '') {
!      print '<br>', T('Administrator Password:'), ' ',
!            $q->password_field(-name=>'p_adminpw', -value=>'*', 
!                               -size=>15, -maxlength=>50),
!            ' ', T('(blank to remove password)'), '<br>',
!            T('(Administrator passwords are used for special maintenance.)');
    }
    if ($EmailNotify) {
      print "<br>";
***************
*** 2686,2694 ****
            T('Include this address in the site email list.')), ' ',
            T('(Uncheck the box to remove the address.)');
      print '<br>', T('Email Address:'), ' ',
!           &GetFormText('email', "", 30, 60);
    }
-   print "<hr><b>$recentName:</b>\n";
    print '<br>', T('Default days to display:'), ' ',
          &GetFormText('rcdays', $RcDefault, 4, 9);
    print "<br>", &GetFormCheck('rcnewtop', $RecentTop,
--- 2711,2719 ----
            T('Include this address in the site email list.')), ' ',
            T('(Uncheck the box to remove the address.)');
      print '<br>', T('Email Address:'), ' ',
!           &GetFormText('email', "", 30, 60);  
!     print "<hr><b>$recentName:</b>\n"; 
    }
    print '<br>', T('Default days to display:'), ' ',
          &GetFormText('rcdays', $RcDefault, 4, 9);
    print "<br>", &GetFormCheck('rcnewtop', $RecentTop,
***************
*** 2721,2727 ****
    # Note: TZ offset is added by TimeToText, so pre-subtract to cancel.
    print '<br>', T('Server time:'), ' ', &TimeToText($Now-$TimeZoneOffset);
    print '<br>', T('Time Zone offset (hours):'), ' ',
!         &GetFormText('tzoffset', 0, 4, 9);
    print '<br>', &GetFormCheck('editwide', 1,
                                T('Use 100% wide edit area (if supported)'));
    print '<br>',
--- 2746,2752 ----
    # Note: TZ offset is added by TimeToText, so pre-subtract to cancel.
    print '<br>', T('Server time:'), ' ', &TimeToText($Now-$TimeZoneOffset);
    print '<br>', T('Time Zone offset (hours):'), ' ',
!        &GetFormText('tzoffset', 0, 4, 9);
    print '<br>', &GetFormCheck('editwide', 1,
                                T('Use 100% wide edit area (if supported)'));
    print '<br>',
***************
*** 2729,2737 ****
          ' ', T('columns:'),   ' ', &GetFormText('editcols', 65, 4, 4);
  
    print '<br>', &GetFormCheck('toplinkbar', 1,
!                               T('Show link bar on top'));
    print '<br>', &GetFormCheck('linkrandom', 0,
!                               T('Add "Random Page" link to link bar'));
    print '<br>', $q->submit(-name=>'Save', -value=>T('Save')), "\n";
    print "<hr>\n";
    print &GetGotoBar('');
--- 2754,2762 ----
          ' ', T('columns:'),   ' ', &GetFormText('editcols', 65, 4, 4);
  
    print '<br>', &GetFormCheck('toplinkbar', 1,
!                              T('Show link bar on top'));
    print '<br>', &GetFormCheck('linkrandom', 0,
!                              T('Add "Random Page" link to link bar'));
    print '<br>', $q->submit(-name=>'Save', -value=>T('Save')), "\n";
    print "<hr>\n";
    print &GetGotoBar('');
***************
*** 2756,2762 ****
  }
  
  sub DoUpdatePrefs {
!   my ($username, $password);
  
    # All link bar settings should be updated before printing the header
    &UpdatePrefCheckbox("toplinkbar");
--- 2781,2787 ----
  }
  
  sub DoUpdatePrefs {
!   my ($username, $password, $userfile);
  
    # All link bar settings should be updated before printing the header
    &UpdatePrefCheckbox("toplinkbar");
***************
*** 2779,2812 ****
      $username =  &FreeToNormal($username);
      $username =~ s/_/ /g;
    }
!   if ($username eq "") {
!     print T('UserName removed.'), '<br>';
!     undef $UserData{'username'};
!   } elsif ((!$FreeLinks) && (!($username =~ /^$LinkPattern$/))) {
!     print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
!   } elsif ($FreeLinks && (!($username =~ /^$FreeLinkPattern$/))) {
!     print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
!   } elsif (length($username) > 50) {  # Too long
!     print T('UserName must be 50 characters or less. (not saved)'), "<br>\n";
!   } else {
!     print Ts('UserName %s saved.', $username), '<br>';
!     $UserData{'username'} = $username;
    }
    $password = &GetParam("p_password",  "");
!   if ($password eq "") {
!     print T('Password removed.'), '<br>';
!     undef $UserData{'password'};
!   } elsif ($password ne "*") {
!     print T('Password changed.'), '<br>';
!     $UserData{'password'} = $password;
!   }
    if ($AdminPass ne "") {
      $password = &GetParam("p_adminpw",  "");
      if ($password eq "") {
!       print T('Administrator password removed.'), '<br>';
        undef $UserData{'adminpw'};
      } elsif ($password ne "*") {
!       print T('Administrator password changed.'), '<br>';
        $UserData{'adminpw'} = $password;
        if (&UserIsAdmin()) {
          print T('User has administrative abilities.'), '<br>';
--- 2804,2842 ----
      $username =  &FreeToNormal($username);
      $username =~ s/_/ /g;
    }
!   if ($username ne $UserData{'username'}) {
!       $userfile = UserNameDataFilename($username);
!       chomp $userfile;
!       if ($userfile ne '') {
! 	  print Ts('Duplicate Username %s: not saved.', $username), "<br>\n";
!       } elsif ((!$FreeLinks) && (!($username =~ /^$LinkPattern$/))) {
! 	  print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
!       } elsif ($FreeLinks && (!($username =~ /^$FreeLinkPattern$/))) {
! 	  print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
!      } elsif (length($username) > 50) {  # Too long
! 	 print T('UserName must be 50 characters or less. (not saved)'), "<br>\n";
!      } else {
! 	 print Ts('UserName %s saved.', $username), '<br>';
! 	 $UserData{'username'} = $username;
!      }
    }
+ 
    $password = &GetParam("p_password",  "");
!   if (($password ne "*") and ($password ne $UserData{'password'})) {
!       if ($password eq "") {
! 	  print T('Password removed.'), '<br>';
! 	  undef $UserData{'password'};
!       } else  {
! 	  print T('Password changed.'), '<br>';
! 	  $UserData{'password'} = $password;
!       }}
    if ($AdminPass ne "") {
      $password = &GetParam("p_adminpw",  "");
      if ($password eq "") {
! #      print T('Administrator password removed.'), '<br>';
        undef $UserData{'adminpw'};
      } elsif ($password ne "*") {
! #      print T('Administrator password changed.'), '<br>';
        $UserData{'adminpw'} = $password;
        if (&UserIsAdmin()) {
          print T('User has administrative abilities.'), '<br>';
***************
*** 2913,2918 ****
--- 2943,2966 ----
    print &GetCommonFooter();
  }
  
+ sub DoLogout {
+   %SetCookie = ();
+   $TimeZoneOffset = 0;
+   undef $q->{'.cookies'};  # Clear cache if it exists (for SpeedyCGI)
+   $UserID = 111;
+   %UserData = ();   
+ 
+   $SetCookie{'id'} = 0;
+   $SetCookie{'randkey'} = 0;
+   $SetCookie{'rev'} = 1;
+ 
+   print &GetHeader('', &QuoteHtml(T('User logged out')), '');
+   print " <p>T('You were successfully logged out.')<p><hr> ";
+   print &GetGotoBar('');
+   print &GetMinimumFooter();
+ }
+ 
+ 
  # Create a new user file/cookie pair
  sub DoNewLogin {
    # Later consider warning if cookie already exists
***************
*** 2928,2940 ****
    $UserData{'createtime'} = $Now;
    $UserData{'createip'} = $ENV{REMOTE_ADDR};
    &SaveUserData();
  }
  
  sub DoEnterLogin {
    print &GetHeader('', T('Login'), "");
    print &GetFormStart();
    print &GetHiddenValue('enter_login', 1), "\n";
!   print '<br>', T('User ID number:'), ' ',
          $q->textfield(-name=>'p_userid', -value=>'',
                        -size=>15, -maxlength=>50);
    print '<br>', T('Password:'), ' ',
--- 2976,2990 ----
    $UserData{'createtime'} = $Now;
    $UserData{'createip'} = $ENV{REMOTE_ADDR};
    &SaveUserData();
+   &DoEditPrefs('new');
  }
  
  sub DoEnterLogin {
    print &GetHeader('', T('Login'), "");
    print &GetFormStart();
    print &GetHiddenValue('enter_login', 1), "\n";
!   print Ts("If you don't have a User ID yet, go to %s and get one.", &ScriptLink("action=newlogin",'Create new login')), "<br>\n";
!   print '<br>', T('User Name or ID number:'), ' ',
          $q->textfield(-name=>'p_userid', -value=>'',
                        -size=>15, -maxlength=>50);
    print '<br>', T('Password:'), ' ',
***************
*** 2949,2977 ****
  
  sub DoLogin {
    my ($uid, $password, $success);
  
    $success = 0;
    $uid = &GetParam("p_userid", "");
-   $uid =~ s/\D//g;
    $password = &GetParam("p_password",  "");
!   if (($uid > 199) && ($password ne "") && ($password ne "*")) {
!     $UserID = $uid;
!     &LoadUserData();
!     if ($UserID > 199) {
!       if (defined($UserData{'password'}) &&
!           ($UserData{'password'} eq $password)) {
!         $SetCookie{'id'} = $uid;
!         $SetCookie{'randkey'} = $UserData{'randkey'};
!         $SetCookie{'rev'} = 1;
!         $success = 1;
!       }
!     }
    }
    print &GetHeader('', T('Login Results'), '');
    if ($success) {
      print Ts('Login for user ID %s complete.', $uid);
    } else {
      print Ts('Login for user ID %s failed.', $uid);
    }
    print "<hr>\n";
    print &GetGotoBar('');
--- 2999,3047 ----
  
  sub DoLogin {
    my ($uid, $password, $success);
+   my ($userfile, $status, $data);
  
    $success = 0;
+   %UserData = ();
    $uid = &GetParam("p_userid", "");
    $password = &GetParam("p_password",  "");
! 
!   if (!($uid =~ /^[0-9]+$/)) {
!       $userfile = UserNameDataFilename($uid);
!       chomp $userfile;                                           
!       ($status, $data) = &ReadFile($userfile);
!       if (!$status) {
! 	  $UserID = 112;  # Could not open file.  Later warning message?
!       } else {
! 	  %UserData = split(/$FS1/, $data, -1);  # -1 keeps trailing null fields
! 	  $UserID = $UserData{'id'}; }}
!   else {
!       $uid =~ s/\D//g;
!       if ($uid > 199) {
! 	  $UserID = $uid;
! 	  &LoadUserData();
!       }}
! 
!   if ($UserID > 199) {
!       if ((!defined($UserData{'password'})) ||
! 	  ($UserData{'password'} eq $password)) {
! 	  $SetCookie{'id'} = $UserID;
! 	  $SetCookie{'randkey'} = $UserData{'randkey'};
! 	  $SetCookie{'rev'} = 1;
! 	  $success = 1;
!       } else {
! 	  %UserData = ();
! 	  $UserID = 119; }
    }
+ 
    print &GetHeader('', T('Login Results'), '');
    if ($success) {
      print Ts('Login for user ID %s complete.', $uid);
+     print "<br>";
+     print Ts('Hello %s', &GetParam('username', "??"));
    } else {
      print Ts('Login for user ID %s failed.', $uid);
+     print "<br>" . Ts('Note that user names and passwords are case sensitive');
    }
    print "<hr>\n";
    print &GetGotoBar('');


I have updated this code with a modification. It supports spaces and doesn't require grep. Then add the following function:

sub UserNameDataFilename {
  my ($id) = @_;
  my ($userfile);
  my ($status, $data);
  my (%userdata_tmp);

  foreach $userfile (glob("$UserDir/*/*.db")) {
    ($status, $data) = &ReadFile($userfile);
    if ($status) {
      %userdata_tmp = split(/$FS1/, $data, -1);

      if ($userdata_tmp{'username'} eq $id) {
        return $userfile;
      }
    }
  }
}


I've added some more T()'s so my Dutch site won't get any English messages.

-Jurriaan


What do the numbers, like *** 2949,2977 **** mean? Also, the "!" and "+"? Sorry, I am a novice to this stuff.

-Thanks for any help, Andrew


Proposal:
- print &GetHeader('', &QuoteHtml(T('User logged out')), '');
+ print &GetHeader('', T('User logged out'), '');
--JuanmaMP

UseModWiki | DirkJanssen | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited February 17, 2011 4:34 pm by MarkusLude (diff)
Search: