1: # The LearningOnline Network
2: # Base routines
3: #
4: # $Id: LONCAPA.pm,v 1.1 2006/05/08 22:05:54 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: package LONCAPA;
31:
32: use strict;
33: require Exporter;
34: our @ISA = qw (Exporter);
35: our @EXPORT = qw(&add_get_param &escape &unescape);
36:
37: # Inputs are a url, adn a hash ref of
38: # form name => value pairs
39: # takes care of properly adding the form name elements and values to the
40: # the url doing proper escaping of the values and joining with ? or & as
41: # needed
42:
43: sub add_get_param {
44: my ($url,$form_data) = @_;
45: my $needs_question_mark = ($url !~ /\?/);
46:
47: while (my ($name,$value) = each(%$form_data)) {
48: if ($needs_question_mark) {
49: $url.='?';
50: $needs_question_mark = 0;
51: } else {
52: $url.='&';
53: }
54: $url.=$name.'='.&escape($form_data->{$name});
55: }
56: return $url;
57: }
58:
59: # -------------------------------------------------------- Escape Special Chars
60:
61: sub escape {
62: my $str=shift;
63: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
64: return $str;
65: }
66:
67: # ----------------------------------------------------- Un-Escape Special Chars
68:
69: sub unescape {
70: my $str=shift;
71: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
72: return $str;
73: }
74:
75: 1;
76:
77: __END__
78:
79: =pod
80:
81: =head1 NAME
82:
83: LONCAPA - Basic routines
84:
85: =head1 SYNOPSIS
86:
87: Generally useful routines
88:
89: =head1 EXPORTED SUBROUTINES
90:
91: =over 4
92:
93: =item *
94:
95: escape() : unpack non-word characters into CGI-compatible hex codes
96:
97: =item *
98:
99: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
100:
101: =item *
102:
103: add_get_param() :
104: Inputs: url (with or without exit GET from parameters), hash ref of
105: form name => value pairs
106:
107: Return: url with properly added the form name elements and values to the
108: the url doing proper escaping of the values and joining with ? or &
109: as needed
110:
111: =back
112:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>