Conflicts: docs/gotcha.txt lib/WebGUI.pm lib/WebGUI/Asset.pm lib/WebGUI/Asset/File/GalleryFile/Photo.pm lib/WebGUI/Asset/Post.pm lib/WebGUI/Asset/Story.pm lib/WebGUI/Asset/Template.pm lib/WebGUI/Asset/Wobject/Calendar.pm lib/WebGUI/Asset/Wobject/GalleryAlbum.pm lib/WebGUI/Asset/Wobject/Navigation.pm lib/WebGUI/AssetLineage.pm lib/WebGUI/AssetTrash.pm lib/WebGUI/Config.pm lib/WebGUI/Form/Template.pm lib/WebGUI/Group.pm lib/WebGUI/Inbox.pm lib/WebGUI/Workflow/Activity/DeleteExpiredSessions.pm lib/WebGUI/Workflow/Activity/TrashExpiredEvents.pm sbin/testEnvironment.pl t/AdSpace.t t/AdSpace/Ad.t t/Asset/Asset.t t/Asset/AssetExportHtml.t t/Asset/AssetLineage.t t/Asset/EMSSubmissionForm.t t/Asset/Event.t t/Asset/File/GalleryFile/Photo/00base.t t/Asset/File/GalleryFile/Photo/comment.t t/Asset/File/GalleryFile/Photo/download.t t/Asset/File/GalleryFile/Photo/edit.t t/Asset/File/GalleryFile/Photo/exif.t t/Asset/File/GalleryFile/Photo/makeResolutions.t t/Asset/File/GalleryFile/Photo/makeShortcut.t t/Asset/File/Image/setfile.t t/Asset/File/setfile.t t/Asset/Post.t t/Asset/Post/Thread/getAdjacentThread.t t/Asset/Sku.t t/Asset/Sku/ProductCollateral.t t/Asset/Story.t t/Asset/Template.t t/Asset/Template/HTMLTemplateExpr.t t/Asset/Wobject/Gallery/00base.t t/Asset/Wobject/GalleryAlbum/00base.t t/Asset/Wobject/GalleryAlbum/ajax.t t/Asset/Wobject/GalleryAlbum/delete.t t/Asset/Wobject/Matrix.t t/Asset/Wobject/StoryArchive.t t/Asset/Wobject/Survey/ExpressionEngine.t t/Asset/Wobject/Survey/Reports.t t/AssetAspect/RssFeed.t t/Auth/mech.t t/Config.t t/Group.t t/Help/isa.t t/International.t t/Mail/Send.t t/Operation/AdSpace.t t/Operation/Auth.t t/Pluggable.t t/Session.t t/Session/DateTime.t t/Session/ErrorHandler.t t/Session/Scratch.t t/Session/Stow.t t/Shop/Cart.t t/Shop/Pay.t t/Shop/PayDriver/ITransact.t t/Shop/PayDriver/PayPalStd.t t/Shop/Ship.t t/Shop/ShipDriver.t t/Shop/TaxDriver/EU.t t/Shop/TaxDriver/Generic.t t/Shop/Transaction.t t/Shop/Vendor.t t/VersionTag.t t/Workflow/Activity/ArchiveOldStories.t t/Workflow/Activity/ExpireIncompleteSurveyResponses.t t/lib/WebGUI/Test.pm
172 lines
3.7 KiB
Perl
172 lines
3.7 KiB
Perl
package WebGUI::Session::Id;
|
|
|
|
|
|
=head1 LEGAL
|
|
|
|
-------------------------------------------------------------------
|
|
WebGUI is Copyright 2001-2009 Plain Black Corporation.
|
|
-------------------------------------------------------------------
|
|
Please read the legal notices (docs/legal.txt) and the license
|
|
(docs/license.txt) that came with this distribution before using
|
|
this software.
|
|
-------------------------------------------------------------------
|
|
http://www.plainblack.com info@plainblack.com
|
|
-------------------------------------------------------------------
|
|
|
|
=cut
|
|
|
|
use strict;
|
|
use Digest::MD5 ();
|
|
use Scalar::Util qw( weaken );
|
|
use Time::HiRes qw( gettimeofday usleep );
|
|
use MIME::Base64 qw(encode_base64 decode_base64);
|
|
use Scalar::Util qw(weaken);
|
|
|
|
my $idValidator = qr/^[A-Za-z0-9_-]{22}$/;
|
|
|
|
=head1 NAME
|
|
|
|
Package WebGUI::Session::Id;
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
This package generates global unique ids, sometimes called GUIDs. A global unique ID is guaranteed to be unique everywhere and at everytime.
|
|
|
|
B<NOTE:> There is no such thing as perfectly unique ID's, but the chances of a duplicate ID are so minute that they are effectively unique.
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
my $id = $session->id->generate;
|
|
|
|
=head1 METHODS
|
|
|
|
These methods are available from this class:
|
|
|
|
=cut
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 fromHex ( hexId )
|
|
|
|
Returns the guid corresponding to hexId. Converse of toHex.
|
|
|
|
=head3 hexId
|
|
|
|
Hex value to convert to guid.
|
|
|
|
=cut
|
|
|
|
sub fromHex {
|
|
my $self = shift;
|
|
my $hexId = shift;
|
|
my $binId = pack( 'H2' x 16, unpack( 'A2' x 16, $hexId ) );
|
|
my $id = substr( encode_base64($binId), 0, 22 );
|
|
$id =~ tr{+/}{_-};
|
|
return $id;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 getValidator
|
|
|
|
Get the regular expression used to validate generated GUIDs. This is just to prevent
|
|
regular expressions from being duplicated all over the place.
|
|
|
|
=cut
|
|
|
|
sub getValidator {
|
|
return $idValidator;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 generate
|
|
|
|
This function generates a global unique id.
|
|
|
|
=cut
|
|
|
|
sub generate {
|
|
my $self = shift;
|
|
my($s,$us)=gettimeofday();
|
|
my($v)=sprintf("%09d%06d%10d%06d%255s",rand(999999999),$us,$s,$$,$self->session->config->getFilename);
|
|
my $id = Digest::MD5::md5_base64($v);
|
|
$id =~ tr{+/}{_-};
|
|
return $id;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 new ( session )
|
|
|
|
Constructor.
|
|
|
|
=head3 session
|
|
|
|
A reference to the current session.
|
|
|
|
=cut
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
my $session = shift;
|
|
my $self = bless { _session => $session }, $class;
|
|
weaken $self->{_session};
|
|
return $self;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 session ( )
|
|
|
|
Returns a reference to the current session.
|
|
|
|
=cut
|
|
|
|
sub session {
|
|
my $self = shift;
|
|
return $self->{_session};
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 toHex ( guid )
|
|
|
|
Returns the hex value of a guid. For all GUIDs generated by the generate method, the return value will be 32 characters long. For some manually created invalid GUIDs, it may be 33 characters long.
|
|
|
|
=head3 guid
|
|
|
|
guid to convert to hex value.
|
|
|
|
=cut
|
|
|
|
sub toHex {
|
|
my $self = shift;
|
|
my $id = shift;
|
|
$id =~ tr{_-}{+/};
|
|
$id .= 'AA';
|
|
my $bin_id = decode_base64($id);
|
|
my $hex_id = unpack("H*", $bin_id);
|
|
$hex_id =~ s/0{3,4}$//;
|
|
return $hex_id
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 valid ( $idString )
|
|
|
|
Returns true if $idString is a valid WebGUI guid.
|
|
|
|
=cut
|
|
|
|
sub valid {
|
|
my ($self, $idString) = @_;
|
|
return $idString =~ m/$idValidator/;
|
|
}
|
|
|
|
|
|
1;
|
|
|
|
|