| 1 | # PurpleWiki::Database::User::UseMod.pm | ||
| 2 | # vi:sw=4:ts=4:ai:sm:et:tw=0 | ||
| 3 | # | ||
| 4 | # $Id: UseMod.pm 463 2004-08-09 01:31:48Z cdent $ | ||
| 5 | # | ||
| 6 | # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved. | ||
| 7 | # | ||
| 8 | # This file is part of PurpleWiki. PurpleWiki is derived from: | ||
| 9 | # | ||
| 10 | # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001 | ||
| 11 | # AtisWiki v0.3 (c) Markus Denker 1998 | ||
| 12 | # CVWiki CVS-patches (c) Peter Merel 1997 | ||
| 13 | # The Original WikiWikiWeb (c) Ward Cunningham | ||
| 14 | # | ||
| 15 | # PurpleWiki is free software; you can redistribute it and/or modify | ||
| 16 | # it under the terms of the GNU General Public License as published by | ||
| 17 | # the Free Software Foundation; either version 2 of the License, or | ||
| 18 | # (at your option) any later version. | ||
| 19 | # | ||
| 20 | # This program is distributed in the hope that it will be useful, | ||
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | # GNU General Public License for more details. | ||
| 24 | # | ||
| 25 | # You should have received a copy of the GNU General Public License | ||
| 26 | # along with this program; if not, write to the | ||
| 27 | # Free Software Foundation, Inc. | ||
| 28 | # 59 Temple Place, Suite 330 | ||
| 29 | # Boston, MA 02111-1307 USA | ||
| 30 | |||
| 31 | package PurpleWiki::Database::User::UseMod; | ||
| 32 | |||
| 33 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | #use base 'PurpleWiki::Database::User::Base'; | ||
| 35 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 36 | use PurpleWiki::Config; | ||
| 37 | use PurpleWiki::Database; | ||
| 38 | use PurpleWiki::User; | ||
| 39 | |||
| 40 | our $VERSION; | ||
| 41 | $VERSION = sprintf("%d", q$Id: UseMod.pm 463 2004-08-09 01:31:48Z cdent $ =~ /\s(\d+)\s/); | ||
| 42 | |||
| 43 | my @DataFields = ( | ||
| 44 | 'username', 'id', 'randkey', 'rev', 'createtime', 'createip', | ||
| 45 | 'email', 'password', 'notify', 'adminpw', 'linkrandom', | ||
| 46 | 'toplinkbar', 'rcdays', 'rcnewtop', 'rcall', 'rcchangehist', | ||
| 47 | 'editwide', 'norcdiff', 'diffrclink', 'alldiff', 'defaultdiff', | ||
| 48 | 'rcshowedit', 'tzoffset', 'editrows', 'editcols', | ||
| 49 | ); | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my $class = shift; | ||
| 53 | my $self = {}; | ||
| 54 | $self->{config} = PurpleWiki::Config->instance(); | ||
| 55 | |||
| 56 | # create user directories | ||
| 57 | my $userDir = $self->{config}->UserDir; | ||
| 58 | if (!(-d "$userDir/0")) { | ||
| 59 | PurpleWiki::Database::CreateDir($userDir); | ||
| 60 | |||
| 61 | foreach my $n (0..9) { | ||
| 62 | PurpleWiki::Database::CreateDir("$userDir/$n"); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | bless ($self, $class); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub createUser { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | # generate new user ID | ||
| 73 | my $id = 1001; | ||
| 74 | while (-f $self->_userFile($id+1000)) { | ||
| 75 | $id += 1000; | ||
| 76 | } | ||
| 77 | while (-f $self->_userFile($id+100)) { | ||
| 78 | $id += 100; | ||
| 79 | } | ||
| 80 | while (-f $self->_userFile($id+10)) { | ||
| 81 | $id += 10; | ||
| 82 | } | ||
| 83 | |||
| 84 | &PurpleWiki::Database::RequestLock() or die('Could not get user ID lock'); | ||
| 85 | while (-f $self->_userFile($id)) { | ||
| 86 | $id++; | ||
| 87 | } | ||
| 88 | # reserve the user ID | ||
| 89 | &PurpleWiki::Database::WriteStringToFile($self->_userFile($id), "lock"); | ||
| 90 | &PurpleWiki::Database::ReleaseLock(); | ||
| 91 | |||
| 92 | return PurpleWiki::User->new($id); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub loadUser { | ||
| 96 | my $self = shift; | ||
| 97 | my $userId = shift; | ||
| 98 | my $user = PurpleWiki::User->new; | ||
| 99 | |||
| 100 | if ($userId && -f $self->_userFile($userId)) { | ||
| 101 | my $data = PurpleWiki::Database::ReadFileOrDie($self->_userFile($userId)); | ||
| 102 | if ($data !~ /^lock/) { | ||
| 103 | my $regexp = $self->{config}->FS1; | ||
| 104 | my %tempHash = split (/$regexp/, $data, -1); | ||
| 105 | foreach my $key (keys(%tempHash)) { | ||
| 106 | $user->setField($key, $tempHash{$key}); | ||
| 107 | } | ||
| 108 | return $user; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | return undef; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | return undef; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub saveUser { | ||
| 120 | my $self = shift; | ||
| 121 | my $user = shift; | ||
| 122 | |||
| 123 | # serialize data | ||
| 124 | my $separator = $self->{config}->FS1; | ||
| 125 | my $string; | ||
| 126 | foreach my $field (@DataFields) { | ||
| 127 | $string .= $field . $separator . $user->getField($field); | ||
| 128 | if ($field ne $DataFields[$#DataFields]) { | ||
| 129 | $string .= $separator; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | # write user data | ||
| 134 | PurpleWiki::Database::WriteStringToFile($self->_userFile($user->id), $string); | ||
| 135 | |||
| 136 | # update username->id map | ||
| 137 | my %users; | ||
| 138 | my $userDir = $self->{config}->UserDir; | ||
| 139 | &PurpleWiki::Database::RequestLock or die('Could not get usernames.db lock'); | ||
| 140 | tie %users, "DB_File", "$userDir/usernames.db", | ||
| 141 | O_RDWR|O_CREAT, 0666, $DB_HASH; | ||
| 142 | $users{$user->username} = $user->id; | ||
| 143 | untie %users; | ||
| 144 | &PurpleWiki::Database::ReleaseLock; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub deleteUser { | ||
| 148 | my $self = shift; | ||
| 149 | my $userFile = $self->_userFile(shift); | ||
| 150 | |||
| 151 | &PurpleWiki::Database::RequestLock() or die('Could not get user-ID lock'); | ||
| 152 | if (-f $userFile) { | ||
| 153 | unlink $userFile; | ||
| 154 | } | ||
| 155 | &PurpleWiki::Database::ReleaseLock(); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub idFromUsername { | ||
| 159 | my $self = shift; | ||
| 160 | my $userName = shift; | ||
| 161 | my %users; | ||
| 162 | |||
| 163 | my $userDir = $self->{config}->UserDir; | ||
| 164 | tie %users, "DB_File", "$userDir/usernames.db", | ||
| 165 | O_RDONLY, 0444, $DB_HASH; | ||
| 166 | my $id = $users{$userName}; | ||
| 167 | untie %users; | ||
| 168 | return $id; | ||
| 169 | } | ||
| 170 | |||
| 171 | ### private methods | ||
| 172 | |||
| 173 | sub _userFile { | ||
| 174 | my $self = shift; | ||
| 175 | my $userId = shift; | ||
| 176 | |||
| 177 | return if ($userId < 0); | ||
| 178 | return $self->{config}->UserDir . "/" . ($userId % 10) . "/$userId.db"; | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; | ||
| 182 | __END__ | ||