File:  [LON-CAPA] / loncom / LONCAPA.pm
Revision 1.1: download - view: text, annotated - select for diffs
Mon May 8 22:05:54 2006 UTC (17 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding the LONCAPA.pm base

# The LearningOnline Network
# Base routines
#
# $Id: LONCAPA.pm,v 1.1 2006/05/08 22:05:54 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
###

package LONCAPA;

use strict;
require Exporter;
our @ISA = qw (Exporter);
our @EXPORT = qw(&add_get_param &escape &unescape);

# Inputs are a url, adn a hash ref of
# form name => value pairs
# takes care of properly adding the form name elements and values to the 
# the url doing proper escaping of the values and joining with ? or & as 
# needed

sub add_get_param {
    my ($url,$form_data) = @_;
    my $needs_question_mark = ($url !~ /\?/);

    while (my ($name,$value) = each(%$form_data)) {
	if ($needs_question_mark) {
	    $url.='?';
	    $needs_question_mark = 0;
	} else { 
	    $url.='&';
	}
	$url.=$name.'='.&escape($form_data->{$name});
    }
    return $url;
}

# -------------------------------------------------------- Escape Special Chars

sub escape {
    my $str=shift;
    $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
    return $str;
}

# ----------------------------------------------------- Un-Escape Special Chars

sub unescape {
    my $str=shift;
    $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
    return $str;
}

1;

__END__

=pod

=head1 NAME

LONCAPA - Basic routines

=head1 SYNOPSIS

Generally useful routines

=head1 EXPORTED SUBROUTINES

=over 4

=item *

escape() : unpack non-word characters into CGI-compatible hex codes

=item *

unescape() : pack CGI-compatible hex codes into actual non-word ASCII character

=item *

add_get_param() :
 Inputs:  url (with or without exit GET from parameters), hash ref of
              form name => value pairs

 Return: url with properly added the form name elements and values to the 
         the url doing proper escaping of the values and joining with ? or &
         as needed

=back


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>