File: parse.pl

package info (click to toggle)
cgiirc 0.5.4-6sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 916 kB
  • ctags: 424
  • sloc: perl: 8,904; sh: 821; ansic: 132; makefile: 54
file content (82 lines) | stat: -rw-r--r-- 2,143 bytes parent folder | download
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
#### Parsing Functions

## Reads a config file from the filename passed to it, returns a reference to
## a hash containing the name=value pairs in the file.
sub parse_config {
   my %config;
   open(CONFIG, "<$_[0]") or error("Opening config file '$_[0]': $!");
   while(<CONFIG>) {
	  s/(\015\012|\012)$//; # Be forgiving for poor windows users
      next if /^\s*[#;]/; # Comments
      next if !/=/;

      my($key,$value) = split(/\s*=\s*/, $_, 2);
      $config{$key} = defined $value ? $value : '';
   }
   close(CONFIG);
   return \%config;
}

## Parses a CGI input, returns a hash reference to the value within it.
## The clever regexp bit is from cgi-lib.pl
## This now also removes certain characters that might not be a good idea.
sub parse_query {
   my($query, $allow) = @_;
   return {} unless defined $query and length $query;

   return {
	  map {
	     s/\+/ /g;
	     my($key, $val) = split(/=/,$_,2);
        $val = "" unless defined $val;

	     $key =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
        $key =~ s/[\r\n\0\001]//g;
	     $val =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
        if(defined $allow and $allow) {
           $val =~ s/[\0\001]//g;
        }else{
           $val =~ s/[\r\n\0\001]//g;
        }

	     $key => $val; # Return a hash element to map.
      } split(/[&;]/, $query)
   };
}

sub parse_cookie {
   if(exists $ENV{HTTP_COOKIE} && $ENV{HTTP_COOKIE} =~ /cgiircauth/) {
	  for(split /;/, $ENV{HTTP_COOKIE}) {
		 s/^\s+//;
		 my($name,$value) = split(/=/,$_,2);
		 return $value if $name eq "cgiircauth";
	  }
   }
   return 0;
}

sub parse_interface_cookie {
   my %tmp = ( );
   if(exists $ENV{HTTP_COOKIE} && $ENV{HTTP_COOKIE} =~ /cgiirc/) {
      for(split /;/, $ENV{HTTP_COOKIE}) {
         s/^\s+//;
         my($name,$value) = split(/=/,$_,2);
         next if $name =~ /[^a-z]/i;
         next unless $name =~ s/^cgiirc//;
         next if $name eq 'auth';
         $tmp{$name} = $value;
      }
   }
   return \%tmp;
}

sub escape_html {
   my($html) = @_;
   $html =~ s/&/&amp;/g;
   $html =~ s/>/&gt;/g;
   $html =~ s/</&lt;/g;
   $html =~ s/"/&quot;/g;
   return $html;
}

1;