webgui/lib/WebGUI/URL.pm
2002-12-15 02:58:51 +00:00

222 lines
4.5 KiB
Perl

package WebGUI::URL;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2002 Plain Black LLC.
-------------------------------------------------------------------
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 URI::Escape;
use WebGUI::Session;
use WebGUI::Utility;
=head1 NAME
Package WebGUI::URL
=head1 SYNOPSIS
use WebGUI::URL;
$url = WebGUI::URL::append($url,$pairs);
$string = WebGUI::URL::escape($string);
$url = WebGUI::URL::gateway($url,$pairs);
$url = WebGUI::URL::makeCompliant($string);
$url = WebGUI::URL::page($url,$pairs);
$string = WebGUI::URL::unescape($string);
$url = WebGUI::URL::urlize($string);
=head1 DESCRIPTION
This package provides URL writing functionality. It is important that
all WebGUI URLs be written using these methods so that they can contain
any extra information that WebGUI needs to add to the URLs in order
to function properly.
=head1 METHODS
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 append ( url, pairs )
Returns a URL after adding some information to the end of it.
=item url
The URL to append information to.
=item pairs
Name value pairs to add to the URL in the form of:
name1=value1&name2=value2&name3=value3
=cut
sub append {
my ($url);
$url = $_[0];
if ($url =~ /\?/) {
$url .= '&'.$_[1];
} else {
$url .= '?'.$_[1];
}
return $url;
}
#-------------------------------------------------------------------
=head2 escape ( string )
Encodes a string to make it safe to pass in a URL.
NOTE: See WebGUI::URL::unescape()
=item string
The string to escape.
=cut
sub escape {
return uri_escape($_[0]);
}
#-------------------------------------------------------------------
=head2 gateway ( pageURL [ , pairs ] )
Generate a URL based on WebGUI's gateway script.
=item pageURL
The urlized title of a page that you wish to create a URL for.
=item pairs
Name value pairs to add to the URL in the form of:
name1=value1&name2=value2&name3=value3
=cut
sub gateway {
my ($url);
$url = $session{config}{scripturl}.'/'.$_[0];
if ($_[1]) {
$url = append($url,$_[1]);
}
if ($session{setting}{preventProxyCache} == 1) {
$url = append($url,randint(0,1000).';'.time());
}
return $url;
}
#-------------------------------------------------------------------
=head2 makeCompliant ( string )
Returns a string that has made into a WebGUI compliant URL.
=item string
The string to make compliant. This is usually a page title or a
filename.
=cut
sub makeCompliant {
my ($value);
$value = $_[0];
$value =~ s/\s+$//; #removes trailing whitespace
$value =~ s/^\s+//; #removes leading whitespace
$value =~ s/^\\//; #removes leading slash
$value =~ s/ /_/g; #replaces whitespace with underscores
$value =~ s/\.$//; #removes trailing period
$value =~ s/[^A-Za-z0-9\-\.\_\\]//g; #removes all funky characters
return $value;
}
#-------------------------------------------------------------------
=head2 page ( [ pairs ] )
Returns the URL of the current page.
=item pairs
Name value pairs to add to the URL in the form of:
name1=value1&name2=value2&name3=value3
=cut
sub page {
my ($url);
$url = $session{page}{url};
if ($_[0]) {
$url = append($url,$_[0]);
}
if ($session{setting}{preventProxyCache} == 1) {
$url = append($url,randint(0,1000).';'.time());
}
return $url;
}
#-------------------------------------------------------------------
=head2 unescape
Decodes a string that was URL encoded.
NOTE: See WebGUI::URL::escape()
=item string
The string to unescape.
=cut
sub unescape {
return uri_unescape($_[0]);
}
#-------------------------------------------------------------------
=head2 urlize ( string )
Same as makeCompliant except that it also lower-cases the string.
This is mainly meant for WebGUI page URLs.
=item string
The string to urlize.
=cut
sub urlize {
my ($value);
$value = lc($_[0]); #lower cases whole string
$value = makeCompliant($value);
return $value;
}
1;