File Coverage

blib/lib/PheMail/Vhost.pm
Criterion Covered Total %
statement 15 83 18.0
branch 0 38 0.0
condition 0 3 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 20 137 14.6


line stmt bran cond sub pod time code
1             package PheMail::Vhost;
2              
3 1     1   255536 use 5.006;
  1         5  
  1         46  
4 1     1   6 use strict;
  1         2  
  1         39  
5 1     1   6 use warnings;
  1         7  
  1         44  
6 1     1   2539 use DBI;
  1         28552  
  1         111  
7 1         2385 use vars qw($sth $dbh $sname $sadmin $droot $id $extensions @sextensions $redirect
8             $soptions $i $htaccess $sdomain $servername $users %SQL $hoster $eredirect
9 1     1   15 $safe_mode $open_basedir $magic_quotes $enableauth $authname $disablefunc);
  1         2  
10              
11             require Exporter;
12              
13             our @ISA = qw(Exporter);
14              
15             # Items to export into callers namespace by default. Note: do not export
16             # names by default without a very good reason. Use EXPORT_OK instead.
17             # Do not simply export all your public functions/methods/constants.
18              
19             # This allows declaration use PheMail::Vhost ':all';
20             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21             # will save memory.
22             our %EXPORT_TAGS = ( 'all' => [ qw(
23            
24             ) ] );
25              
26             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27              
28             our @EXPORT = qw(
29             LoadVhosts
30             ReportSql
31             alterSqlFromString
32             alterSql
33             );
34             our $VERSION = '0.14';
35              
36              
37             # Preloaded methods go here.
38             # alter the SQL interface from the outside.
39             sub alterSqlFromString($$$$$$) {
40 0     0 0   ($SQL{'backend'},$SQL{'user'},$SQL{'pass'},$SQL{'database'},$SQL{'hostname'},$SQL{'whoami'}) = @_;
41             }
42             sub RandSalt() {
43             # function to create a random 2-char salt. Thanks Kimusan.
44 0     0 0   my @chars = ('a'..'z','A'..'Z',0..9);
45 0           return join '', map $chars[rand @chars], 1..2;
46             }
47             sub ReportSql {
48 0     0 0   print "SQL Information:\n---------------\n";
49 0           foreach my $foo (keys %SQL) {
50 0           print $foo." = ".$SQL{$foo}."\n";
51             }
52             }
53             sub LoadVhosts($) {
54 0     0 0   my $VirtualHost = shift;
55 0           $i = 0;
56             # $dbh = DBI->connect("DBI:".$SQL{'backend'}.":".$SQL{'database'}.":".$SQL{'hostname'},$SQL{'user'},$SQL{'pass'}); # for MySQL
57 0 0         $dbh = DBI->connect("DBI:".$SQL{'backend'}.":dbname=".$SQL{'database'}.";host=".$SQL{'hostname'},$SQL{'user'},$SQL{'pass'})
58             or die("DBI Error: ".DBI::errstr);
59 0           $sth = $dbh->prepare("SELECT * FROM vhosts WHERE hoster='".$SQL{'whoami'}."'");
60 0           $sth->execute();
61 0           while (($id,
62             $hoster,
63             $sname,
64             $droot,
65             $sadmin,
66             $sdomain,
67             $soptions,
68             $htaccess,
69             $users,
70             $extensions,
71             $redirect,
72             $eredirect,
73             $open_basedir,
74             $safe_mode,
75             $magic_quotes,
76             $enableauth,
77             $authname,
78             $disablefunc) = $sth->fetchrow_array()) {
79 0           $i++;
80 0           $droot =~ s/^\///;
81 0 0         $servername = $sname ? $sname.".".$sdomain : $sdomain;
82 0 0         if (-d "/home/customers/$sdomain/wwwroot/$droot") {
83 0 0         if ($htaccess) {
84 0 0         open(HT,"> /home/customers/$sdomain/wwwroot/$droot/.htaccess")
85             or die("Couldn't open: $!");
86 0           print HT $htaccess;
87 0           close(HT);
88             } else {
89 0 0         system("/bin/rm /home/customers/$sdomain/wwwroot/$droot/.htaccess")
90             if (-e "/home/customers/$sdomain/wwwroot/$droot/.htaccess");
91             }
92             } else {
93 0 0         if (!$redirect) {
94 0           warn "PheMail::Vhost: Warning: ".$servername."'s documentroot does not exist.\n";
95 0           next;
96             }
97             }
98 0 0         @sextensions = split("\n",$extensions) if $extensions;
99 0           my $lamext;
100 0           push @$lamext, [ "image/x-icon", ".ico" ]; # just to have something for default AddType so it won't fail.
101 0           foreach my $grasp (@sextensions) {
102 0           chomp($grasp); # remove the latter \n
103 0           my($dotext,$handler) = split(/:/,$grasp);
104 0 0         $handler =~ s/\r//g if $handler; # obviously this created some errors in the arrayref push
105 0 0 0       push @$lamext, [ $handler, $dotext ] if ($dotext && $handler); # push in the new extensions
106             }
107 0           my $php_modes; my $php_flags;
108 0           push @$php_modes, [ "sendmail_from", $sadmin ]; # default values in the modes, just to have something
109 0           push @$php_modes, [ "include_path", "/usr/home/customers/$sdomain/wwwroot/$droot:/usr/local/share/pear" ]; # include path
110             # disable functions?
111 0 0         if ($disablefunc) {
112 0           push @$php_modes, [ "disable_functions", $disablefunc ];
113             }
114             # -- /disable functions --
115 0 0         if ($open_basedir > 0) {
116 0           push @$php_modes, [ "open_basedir", "/usr/home/customers/".$sdomain."/wwwroot/".$droot ];
117             }
118 0 0         if ($safe_mode > 0) {
119 0           push @$php_flags, [ "safe_mode", 1 ];
120             } else {
121 0           push @$php_flags, [ "safe_mode", 0 ];
122             }
123 0 0         if ($magic_quotes > 0) {
124 0           push @$php_flags, [ "magic_quotes_gpc", 1 ];
125             } else {
126 0           push @$php_flags, [ "magic_quotes_gpc", 0 ];
127             }
128             # prepare auth-directory thingie
129 0           my %Location; # decius: reset every time
130 0 0         if ($enableauth) {
131 0           print "[+] Enabled HTTP-Auth for vhost $servername..";
132 0           $authname =~ s/\s/_/g;
133 0 0         $Location{'/'} = {
134             "Limit" => {
135             "METHODS" => "post get", # no idea why you need this, won't work without though.
136             "require" => "valid-user",
137             },
138             "AuthType" => "Basic",
139             "AuthName" => ($authname ? $authname : "PheMail"),
140             "AuthUserFile" => "/usr/home/customers/$sdomain/wwwroot/$droot/.htpasswd",
141             };
142 0           print ". done.\n";
143             }
144             # write users here
145             # decius: problem with enabling auth. I'll take a look.
146 0 0         if ($enableauth) {
147 0 0         open(FOOPWD,"> /usr/home/customers/$sdomain/wwwroot/$droot/.htpasswd") or die("Unable to open .htpasswd: $!");
148 0           print "Writing file for $servername..\n";
149 0           my @rusers = split(/\n/,$users);
150 0           foreach my $user (@rusers) {
151 0           $user =~ s/\r//g;
152 0           my($username,$password) = split(/:/,$user);
153 0           print FOOPWD $username.":".crypt($password,&RandSalt)."\n";
154             }
155 0           close(FOOPWD);
156             }
157             # enable redirect here
158 0 0         if ($eredirect) {
159 0           push @{$VirtualHost->{'*'}}, {
  0            
160             ServerName => $servername,
161             ServerAdmin => $sadmin,
162             ErrorLog => "/usr/home/customers/$sdomain/log/httpd-error.log",
163             TransferLog => "/usr/home/customers/$sdomain/log/httpd-access.log",
164             Redirect => [ "/", $redirect ],
165             };
166             } else { # no redirect? oh well, write the normal one.
167 0           push @{$VirtualHost->{'*'}}, {
  0            
168             ServerName => $servername,
169             ServerAdmin => $sadmin,
170             DocumentRoot => "/usr/home/customers/$sdomain/wwwroot/$droot",
171             ErrorLog => "/usr/home/customers/$sdomain/log/httpd-error.log",
172             TransferLog => "/usr/home/customers/$sdomain/log/httpd-access.log",
173             AddType => $lamext,
174             php_admin_value => $php_modes,
175             php_admin_flag => $php_flags,
176             Directory => {
177             "/usr/home/customers/$sdomain/wwwroot/$droot" => {
178             Options => $soptions,
179             AllowOverride => "All",
180             },
181             },
182             Location => {
183             %Location,
184             },
185             };
186             }
187             }
188 0           printf("PheMail: Done loading %d vhosts.\n",$i);
189 0           $sth->finish();
190 0           $dbh->disconnect();
191             }
192             1;
193             __END__