| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: ewhutil.pl,v 2.17 2012/01/29 18:32:36 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## Generic utility routines | ||
| 8 | ##---------------------------------------------------------------------------## | ||
| 9 | ## Copyright (C) 1996-2001,2012 Earl Hood, mhonarc@mhonarc.org | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify | ||
| 12 | ## it under the terms of the GNU General Public License as published by | ||
| 13 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 14 | ## (at your option) any later version. | ||
| 15 | ## | ||
| 16 | ## This program is distributed in the hope that it will be useful, | ||
| 17 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | ## GNU General Public License for more details. | ||
| 20 | ## | ||
| 21 | ## You should have received a copy of the GNU General Public License | ||
| 22 | ## along with this program; if not, write to the Free Software | ||
| 23 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 24 | ## 02111-1307, USA | ||
| 25 | ##---------------------------------------------------------------------------## | ||
| 26 | |||
| 27 | package mhonarc; | ||
| 28 | |||
| 29 | my $HTMLSpecials = '"&<>'; | ||
| 30 | my %HTMLSpecials = ( | ||
| 31 | '"' => '"', | ||
| 32 | '&' => '&', | ||
| 33 | '<' => '<', | ||
| 34 | '>' => '>', | ||
| 35 | # '@' => '@', # XXX: Screws up ISO-2022-JP conversion | ||
| 36 | ); | ||
| 37 | |||
| 38 | ##--------------------------------------------------------------------------- | ||
| 39 | ## Remove duplicates in an array. | ||
| 40 | ## Returns list with duplicates removed. | ||
| 41 | ## | ||
| 42 | sub remove_dups { | ||
| 43 | my $a = shift; | ||
| 44 | return () unless scalar(@$a); | ||
| 45 | my %dup = (); | ||
| 46 | grep(!$dup{$_}++, @$a); | ||
| 47 | } | ||
| 48 | |||
| 49 | ##--------------------------------------------------------------------------- | ||
| 50 | ## "Entify" special characters | ||
| 51 | |||
| 52 | sub htmlize { # Older name | ||
| 53 | return '' unless scalar(@_) && defined($_[0]); | ||
| 54 | my $txt = shift; | ||
| 55 | my $txt_r = ref($txt) ? $txt : \$txt; | ||
| 56 | $$txt_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go; | ||
| 57 | $$txt_r; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub entify { # Alternate name | ||
| 61 | return '' unless scalar(@_) && defined($_[0]); | ||
| 62 | my $txt = shift; | ||
| 63 | my $txt_r = ref($txt) ? $txt : \$txt; | ||
| 64 | $$txt_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go; | ||
| 65 | $$txt_r; | ||
| 66 | } | ||
| 67 | |||
| 68 | ## commentize entifies certain characters to avoid problems when a | ||
| 69 | ## string will be included in a comment declaration | ||
| 70 | |||
| 71 | sub commentize { | ||
| 72 | my ($txt) = $_[0]; | ||
| 73 | $txt =~ s/([\-&<])/'&#'.unpack('C',$1).';'/ge; | ||
| 74 | $txt; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub uncommentize { | ||
| 78 | my ($txt) = $_[0]; | ||
| 79 | $txt =~ s/&#(\d+);/pack("C",$1)/ge; | ||
| 80 | $txt; | ||
| 81 | } | ||
| 82 | |||
| 83 | ##--------------------------------------------------------------------------- | ||
| 84 | ## Copy a file. | ||
| 85 | ## | ||
| 86 | sub cp { | ||
| 87 | my ($src, $dst) = @_; | ||
| 88 | open(SRC, $src) || die("ERROR: Unable to open $src\n"); | ||
| 89 | open(DST, "> $dst") || die("ERROR: Unable to create $dst\n"); | ||
| 90 | print DST <SRC>; | ||
| 91 | close(SRC); | ||
| 92 | close(DST); | ||
| 93 | } | ||
| 94 | |||
| 95 | ##--------------------------------------------------------------------------- | ||
| 96 | ## Translate html string back to regular string | ||
| 97 | ## | ||
| 98 | sub dehtmlize { | ||
| 99 | my $str = shift; | ||
| 100 | my $str_r = ref($str) ? $str : \$str; | ||
| 101 | $$str_r =~ s/\</</g; | ||
| 102 | $$str_r =~ s/\>/>/g; | ||
| 103 | $$str_r =~ s/\&#[xX]0*40;/@/g; | ||
| 104 | $$str_r =~ s/\@/@/g; | ||
| 105 | $$str_r =~ s/\"/"/g; | ||
| 106 | $$str_r =~ s/\&/\&/g; | ||
| 107 | $$str_r; | ||
| 108 | } | ||
| 109 | |||
| 110 | ##--------------------------------------------------------------------------- | ||
| 111 | ## Escape special characters in string for URL use. | ||
| 112 | ## | ||
| 113 | sub urlize { | ||
| 114 | my ($url) = shift || ""; | ||
| 115 | my $url_r = ref($url) ? $url : \$url; | ||
| 116 | $$url_r =~ s/([^\w\.\-:])/sprintf("%%%02X",unpack("C",$1))/ge; | ||
| 117 | $$url_r; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub urlize_path { | ||
| 121 | my ($url) = shift || ""; | ||
| 122 | my $url_r = ref($url) ? $url : \$url; | ||
| 123 | $$url_r =~ s/([^\w\.\-:\/])/sprintf("%%%02X",unpack("C",$1))/ge; | ||
| 124 | $$url_r; | ||
| 125 | } | ||
| 126 | |||
| 127 | ##---------------------------------------------------------------------------## | ||
| 128 | ## Perform a "modified" rot13 on a string. This version includes | ||
| 129 | ## the '@' character so addresses can be munged a little better. | ||
| 130 | ## | ||
| 131 | sub mrot13 { | ||
| 132 | my $str = shift; | ||
| 133 | $str =~ tr/@A-Z[a-z/N-Z[@A-Mn-za-m/; | ||
| 134 | $str; | ||
| 135 | } | ||
| 136 | |||
| 137 | ##---------------------------------------------------------------------------## | ||
| 138 | 1; |