File: Punycode.cgi

package info (click to toggle)
opensrs-client 2.9.5-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,712 kB
  • ctags: 982
  • sloc: perl: 24,323; makefile: 61
file content (194 lines) | stat: -rwxr-xr-x 4,796 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl

#       .Copyright (C)  2004 Tucows Inc.
#       .Created:       04/02/2004
#       .Contactid:     <admin@opensrs.org>
#       .Url:           http://www.opensrs.org
#       .Authors:       Vlad Jebelev, Tom Lovasic, Steve Knipe, Vedran Vego
#
#
#       This program is free software; you can redistribute it and/or
#       modify it under the terms of the GNU Lesser General Public
#       License as published by the Free Software Foundation; either
#       version 2.1 of the License, or (at your option) any later version.
#
#       This program 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
#       Lesser General Public License for more details.
#
#       You should have received a copy of the GNU Lesser General Public
#       License along with this program; if not, write to the Free Software
#       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



use vars qw($cgi $path_templates %in $flag_header_sent %strings);

BEGIN {

    $path_to_config = "<path-to-conf>";
    do "$path_to_config/OpenSRS.conf";
}

use warnings;
use strict;

use lib $PATH_LIB;
use CGI ':cgi-lib';
use HTML::Template;
use RACE;
use OpenSRS::Util::Common;
use OpenSRS::Language qw/native_to_puny puny_to_native/;
RACE::Initialise(%RACESETTINGS);

$cgi = $ENV{SCRIPT_NAME};
$path_templates = "$PATH_TEMPLATES/PUNYCODE";
$flag_header_sent = 0;
%strings = ();

%in = ();
ReadParse(\%in);
foreach (keys %in) {
    $in{$_} =~ s/(^\s+)|(\s+$)//g;
}

start_up();

my $type = $in{type};
my $text = $in{input};

if ( $text ) {
    if ($type eq 'NATIVE') {

	$strings{native} = $text;

	eval { $strings{race} = native_to_race($text)};
	if ( $@ ) { $strings{race} = undef; $strings{error_race} = $@; }

	eval { $strings{punycode} = native_to_puny($text, \%OPENSRS)};
	if ( $@ ) { $strings{punycode} = undef; $strings{error_punycode} = $@; }

    } elsif ($type eq 'RACE') {

	eval { $strings{native} = race_to_native($text)};
	if ( $@ ) { $strings{native} = undef; $strings{error_native} = $@; }
	
	$strings{race} = $text;

	eval { my $native = race_to_native($text);
	       $strings{punycode} = native_to_puny($native, \%OPENSRS)};
	if ( $@ ) { $strings{punycode} = undef; $strings{error_punycode} = $@; }

    } elsif ($type eq 'PUNYCODE') {

	eval { $strings{native} = puny_to_native($text, \%OPENSRS)};
	if ( $@ ) { $strings{native} = undef; $strings{error_native} = $@; }

	eval { my $native = puny_to_native($text, \%OPENSRS);
	       $strings{race} = native_to_race($native)};
	if ( $@ ) { $strings{race} = undef; $strings{error_race} = $@; }

	$strings{punycode} = $text;
    }
}

main_menu();
exit;

sub native_to_race {
    my ( $str ) = @_;

    my $conv = RACE::DoRACE( Domain => $str, EncodingType => $OPENSRS{IDN_ENCODING_TYPE});

    if ($conv->{Error}){
	die $conv->{Error}."\n";
    }

    return $conv->{ConvertedDomain};
}

sub race_to_native {
    my ( $str ) = @_;
    
    my $conv = RACE::UndoRACE( Domain => $str, EncodingType => $OPENSRS{IDN_ENCODING_TYPE});

    if ($conv->{Error}){
	die $conv->{Error}."\n";
    }

    return $conv->{OriginalDomain};
}

sub main_menu {
    my (%HTML);
    
    print_form(template => "$path_templates/base.html",
	       data => {
		%in,
		%strings,
		encoding => uc $OPENSRS{IDN_ENCODING_TYPE},
	        show_results => (scalar(keys %strings) > 0 ? 1 : 0),
		show_race => ($in{type} ne 'RACE' && exists $strings{race}),
		show_native => ($in{type} ne 'NATIVE' && exists $strings{native}),
		show_punycode => ($in{type} ne 'PUNYCODE' && exists $strings{punycode}),
	       });
}

sub print_form {
    my %args = @_;
    
    $args{title} = $args{title} || 'IDN Converter';
    
    my $template = HTML::Template->new(
	cache => 1,
	filename => $args{template},
	die_on_bad_params => 0,
    );
    
    $template->param(
	CGI => $cgi,
	%{ $args{data} },
    );
    
    if (not $args{not_framed}) {
	my $content = $template->output;
	$template = HTML::Template->new(
	    cache => 1,
	    filename => "$path_templates/base.html",
	    die_on_bad_params => 0,
	);
	$template->param(CONTENT => $content);
    }
    
    $template->param(
	CGI => $cgi,
	%{ $args{data} },
    );

    print_header();
    print $template->output;
}

# print html header
sub print_header {
    my %cookies = @_;

    return if $flag_header_sent;
    
    print "Content-type: text/html\r\n";
    foreach my $key (keys %cookies) {
	printf "Set-Cookie: %s=%s; PATH=;\r\n", $key, $cookies{$key};
    }
    print "\r\n";
    
    $flag_header_sent = 1;
}

sub start_up {
    OpenSRS::Util::Common::initialize(
	path_templates => $PATH_TEMPLATES,
	mail_settings => \%MAIL_SETTINGS
    );
}