root/trunk/engine/ispcp-als-mngr

Revision 1367, 59.2 kB (checked in by scitech, 2 months ago)

Fixed #1080: Active web folders should not be deletable by customer. Fixed #1554: MySQL quota calculation (thanks Feg)

Line 
1 #!/usr/bin/perl
2 # ispCP ω (OMEGA) a Virtual Hosting Control Panel
3 # Copyright (c) 2001-2006 by moleSoftware GmbH
4 # http://www.molesoftware.com
5 # Copyright (c) 2006-2008 by isp Control Panel
6 # http://isp-control.net
7 #
8 #
9 # License:
10 #    This program is free software; you can redistribute it and/or
11 #    modify it under the terms of the MPL Mozilla Public License
12 #    as published by the Free Software Foundation; either version 1.1
13 #    of the License, or (at your option) any later version.
14 #
15 #    This program 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 #    MPL Mozilla Public License for more details.
19 #
20 #    You may have received a copy of the MPL Mozilla Public License
21 #    along with this program.
22 #
23 #    An on-line copy of the MPL Mozilla Public License can be found
24 #    http://www.mozilla.org/MPL/MPL-1.1.html
25 #
26 #
27 # The ISPCP ω Home Page is at:
28 #
29 #    http://isp-control.net
30 #
31
32 use FindBin;
33 use lib "$FindBin::Bin/";
34 require 'ispcp_common_code.pl';
35 use strict;
36 use warnings;
37 $main::als_task_id = undef;
38
39 ################################################################################
40 ################################################################################
41 ##
42 ##
43 ##
44 sub als_mngr_start_up {
45         my ($rs, $rdata) = (undef, undef);
46
47         push_el(\@main::el, 'als_mngr_start_up()', 'Starting...');
48
49         # checking for master process;
50         $rs = check_master();
51         return $rs if ($rs != 0);
52
53         # Let's clear Execution Logs, if any.
54         if (-e $main::ispcp_als_mngr_el) {
55                 $rs = del_file($main::ispcp_als_mngr_el);
56                 return $rs if ($rs != 0);
57         }
58
59         # config check;
60         $rs = get_conf();
61         return $rs if ($rs != 0);
62
63         # sql check;
64         #
65         # getting initial data also must be done here;
66         #
67         my $sql = "
68                 SELECT
69                         domain_id,
70                         domain_name,
71                         domain_gid,
72                         domain_uid,
73                         domain_admin_id,
74                         domain_created_id,
75                         domain_created,
76                         domain_last_modified,
77                         domain_mailacc_limit,
78                         domain_ftpacc_limit,
79                         domain_traffic_limit,
80                         domain_sqld_limit,
81                         domain_sqlu_limit,
82                         domain_status,
83                         domain_alias_limit,
84                         domain_subd_limit,
85                         domain_ip_id,
86                         domain_disk_limit,
87                         domain_disk_usage,
88                         domain_php,
89                         domain_cgi
90                 FROM
91                         domain
92         ";
93         ($rs, $rdata) = doSQL($sql);
94         return $rs if ($rs != 0);
95
96         $rs = get_domain_ids();
97         return $rs if ($rs != 0);
98
99         $rs = get_subdom_ids();
100         return $rs if ($rs != 0);
101
102         $rs = get_alias_ids();
103         return $rs if ($rs != 0);
104
105         $rs = get_ip_nums();
106         return $rs if ($rs != 0);
107
108         #
109         # getting task id and domain record id;
110         #
111         $main::als_task_id = $ARGV[0];
112
113         push_el(\@main::el, 'als_mngr_start_up()', 'Ending...');
114         return 0;
115 }
116
117 ################################################################################
118 ################################################################################
119 ##
120 ##
121 ##
122 sub als_mngr_shut_down {
123         my $rs = undef;
124
125         push_el(\@main::el, 'als_mngr_shut_down()', 'Starting...');
126         push_el(\@main::el, 'als_mngr_shut_down()', 'Ending...');
127
128         return 0;
129 }
130
131 ####################################################
132 ##
133 ## domain named data managment;
134 ##
135 ####################################################
136
137 ################################################################################
138 ################################################################################
139 ##
140 ##
141 ##
142 sub als_add_named_cfg_data {
143         my ($als_data)   = @_;
144         my ($rs, $rdata) = (undef, undef);
145
146         push_el(\@main::el, 'als_add_named_cfg_data()', 'Starting...');
147         if (!defined($als_data) || $als_data eq '') {
148                 push_el(\@main::el, 'als_add_named_cfg_data()', 'ERROR: Undefined Input Data...');
149                 return -1;
150         }
151
152         #
153         # Initial data we need;
154         #
155         my $als_name     = @$als_data[2];
156         my $conf_dir     = $main::cfg{'CONF_DIR'};
157         my $sys_cfg      = $main::cfg{'BIND_CONF_FILE'};
158         my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
159         my $tpl_dir      = "$conf_dir/bind/parts";
160         my $backup_dir   = "$conf_dir/bind/backup";
161         my $working_dir  = "$conf_dir/bind/working";
162         my $timestamp    = time;
163         my $backup_cfg   = "$backup_dir/named.conf.$timestamp";
164         my $working_cfg  = "$working_dir/named.conf";
165
166         #
167         #  BEGIN/END tags, and templates needed for this config;
168         #
169         my ($dta_b, $dta_e, $entry_b, $entry_e, $entry) = ('', '', '', '', '');
170         (
171                 $rs,
172                 $dta_b,
173                 $dta_e,
174                 $entry_b,
175                 $entry_e,
176                 $entry
177         ) = get_tpl(
178                 $tpl_dir,
179                 'cfg_dta_b.tpl',
180                 'cfg_dta_e.tpl',
181                 'cfg_entry_b.tpl',
182                 'cfg_entry_e.tpl',
183                 'cfg_entry.tpl'
184         );
185         return $rs if ($rs != 0);
186
187         #
188         # Let's construct needed tags and entries;
189         #
190         my %tag_hash = (
191                 '{DMN_NAME}' => $als_name,
192                 '{DB_DIR}'   => $named_db_dir
193         );
194         my ($entry_b_val, $entry_e_val, $entry_val) = ('', '', '');
195         (
196                 $rs,
197                 $entry_b_val,
198                 $entry_e_val,
199                 $entry_val
200         ) = prep_tpl(
201                 \%tag_hash,
202                 $entry_b,
203                 $entry_e,
204                 $entry
205         );
206         return $rs if ($rs != 0);
207
208         #
209         # Let's get System and Working config files;
210         #
211         my ($sys, $working) = ('', '');
212         ($rs, $sys) = get_file($sys_cfg);
213         return $rs  if ($rs != 0);
214
215         ($rs, $working) = get_file($working_cfg);
216         return $rs  if ($rs != 0);
217
218         ($rs, $rdata) = get_tag($dta_b, $dta_e, $working);
219         return $rs if ($rs != 0);
220
221         #
222         # Does the new domain entry exists ?
223         #
224         ($rs, $rdata) = get_tag($entry_b_val, $entry_e_val, $working);
225         if ($rs == 0) {
226                 # Yes it exists ! Then we must delete it !
227                 ($rs, $working) = del_tag($entry_b_val, "$entry_e_val\n", $working);
228                 return $rs if ($rs != 0);
229         }
230         ($rs, $rdata) = get_tag($entry_b, $entry_e, $working);
231         return $rs if ($rs != 0);
232
233         #
234         # Let's construct the replacement and do it;
235         #
236         my $entry_repl = "$entry_b_val$entry_val$entry_e_val\n$entry_b$entry_e";
237         ($rs, $working) = repl_tag($entry_b, $entry_e, $working, $entry_repl, "als_add_named_cfg_data");
238         return $rs if ($rs != 0);
239
240         #
241         # Here we'll backup production config file;
242         #
243         $rs = sys_command("cp -p $sys_cfg $backup_cfg");
244         return $rs if ($rs != 0);
245
246         #
247         # Let's save working copy;
248         #
249         $rs = store_file($working_cfg, $working, $main::cfg{'ROOT_USER'}, $main::cfg{'ROOT_GROUP'}, 0644);
250         return $rs if ($rs != 0);
251
252         #
253         # Here we'll replace data in production config file with data in working
254         # config file. A little workaround will be done. If working copy data does not exist
255         # in production config then we will add it;
256         #
257         ($rs, $rdata) = get_tag($dta_b, $dta_e, $sys);
258         if ($rs == 0) { # YES ! Data is here ! /in production config file/;
259                 ($rs, $sys) = repl_tag($dta_b, $dta_e, $sys, $working, "als_add_named_cfg_data");
260                 return $rs if ($rs != 0);
261         } elsif ($rs == -5) {
262                 $sys .= $working;
263         } else {
264                 return $rs;
265         }
266         $rs = store_file($sys_cfg, $sys, $main::cfg{'ROOT_USER'}, $main::cfg{'ROOT_GROUP'}, 0644);
267         return $rs if ($rs != 0);
268
269         push_el(\@main::el, 'als_add_named_cfg_data()', 'Ending...');
270         return 0;
271 }
272
273
274 ################################################################################
275 ################################################################################
276 ##
277 ##
278 ##
279 sub als_del_named_cfg_data {
280         my ($als_data)   = @_;
281         my ($rs, $rdata) = (undef, undef);
282
283         push_el(\@main::el, 'als_del_named_cfg_data()', 'Starting...');
284         if (!defined($als_data) || $als_data eq '') {
285                 push_el(\@main::el, 'als_del_named_cfg_data()', 'ERROR: Undefined Input Data...');
286                 return -1;
287         }
288
289         #
290         # Initial data we need;
291         #
292         my $als_name     = @$als_data[2];
293         my $conf_dir     = $main::cfg{'CONF_DIR'};
294         my $sys_cfg      = $main::cfg{'BIND_CONF_FILE'};
295         my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
296         my $tpl_dir      = "$conf_dir/bind/parts";
297         my $backup_dir   = "$conf_dir/bind/backup";
298         my $working_dir  = "$conf_dir/bind/working";
299         my $timestamp    = time;
300         my $backup_cfg   = "$backup_dir/named.conf.$timestamp";
301         my $working_cfg  = "$working_dir/named.conf";
302         #
303         #  BEGIN/END tags, and templates needed for this config;
304         #
305         my ($dta_b, $dta_e, $entry_b, $entry_e, $entry) = ('', '', '', '', '');
306         (
307                 $rs,
308                 $dta_b,
309                 $dta_e,
310                 $entry_b,
311                 $entry_e,
312                 $entry
313         ) = get_tpl(
314                 $tpl_dir,
315                 'cfg_dta_b.tpl',
316                 'cfg_dta_e.tpl',
317                 'cfg_entry_b.tpl',
318                 'cfg_entry_e.tpl',
319                 'cfg_entry.tpl'
320         );
321         return $rs if ($rs != 0);
322
323         #
324         # Let's construct needed tags and entries;
325         #
326         my %tag_hash = (
327                 '{DMN_NAME}' => $als_name,
328                 '{DB_DIR}'   => $named_db_dir
329         );
330         my ($entry_b_val, $entry_e_val, $entry_val) = ('', '', '');
331         (
332                 $rs,
333                 $entry_b_val,
334                 $entry_e_val,
335                 $entry_val
336         ) = prep_tpl(
337                 \%tag_hash,
338                 $entry_b,
339                 $entry_e,
340                 $entry
341         );
342         return $rs if ($rs != 0);
343
344         #
345         # Let's get Sytem and Working config files;
346         #
347         my ($sys, $working) = ('', '');
348         ($rs, $sys) = get_file($sys_cfg);
349         return $rs  if ($rs != 0);
350
351         ($rs, $working) = get_file($working_cfg);
352         return $rs  if ($rs != 0);
353
354         ($rs, $rdata) = get_tag($dta_b, $dta_e, $working);
355         return $rs if ($rs != 0);
356
357         #
358         # Does the new domain entry exists ?
359         #
360         ($rs, $rdata) = get_tag($entry_b_val, $entry_e_val, $working);
361         if ($rs == 0) {
362                 # Yes it exists ! Then we must delete it !
363                 ($rs, $working) = del_tag($entry_b_val, "$entry_e_val\n", $working);
364                 return $rs if ($rs != 0);
365         }
366         ($rs, $rdata) = get_tag($entry_b, $entry_e, $working);
367         return $rs if ($rs != 0);
368
369         #
370         # Let's construct the replacement and do it;
371         #
372         #
373         #my $entry_repl = "$entry_b_val$entry_val$entry_e_val\n$entry_b$entry_e";
374         #
375         #($rs, $working) = repl_tag($entry_b, $entry_e, $working, $entry_repl);
376         #
377         #return $rs if ($rs != 0);
378
379         #
380         #
381         # Here we'll backup production config file;
382         #
383         $rs = sys_command("cp -p $sys_cfg $backup_cfg");
384         return $rs if ($rs != 0);
385
386         #
387         # Let's save working copy;
388         #
389         $rs = store_file($working_cfg, $working, $main::cfg{'ROOT_USER'}, $main::cfg{'ROOT_GROUP'}, 0644);
390         return $rs if ($rs != 0);
391
392         #
393         # Here we'll replace data in production config file with data in working
394         # confing file. A little workaround will be done. If working copy data does not exist
395         # in production config then we will add it;
396         #
397         ($rs, $rdata) = get_tag($dta_b, $dta_e, $sys);
398         if ($rs == 0) { # YES ! Data is here ! /in production config file/;
399                 ($rs, $sys) = repl_tag($dta_b, $dta_e, $sys, $working, "als_del_named_cfg_data");
400                 return $rs if ($rs != 0);
401         } elsif ($rs == -5) {
402                 $sys .= $working;
403         } else {
404                 return $rs;
405         }
406         $rs = store_file($sys_cfg, $sys, $main::cfg{'ROOT_USER'}, $main::cfg{'ROOT_GROUP'}, 0644);
407         return $rs if ($rs != 0);
408
409         push_el(\@main::el, 'als_del_named_cfg_data()', 'Ending...');
410         return 0;
411 }
412
413 ################################################################################
414 ################################################################################
415 ##
416 ##
417 ##
418
419 sub als_add_named_db_data {
420         my ($als_data)   = @_;
421         my ($rs, $rdata) = (undef, undef);
422
423         push_el(\@main::el, 'als_add_named_db_data()', 'Starting...');
424
425         if (!defined($als_data) || $als_data eq '') {
426                 push_el(\@main::el, 'als_add_named_db_data()', 'ERROR: Undefined Input Data...');
427                 return -1;
428         }
429
430         #
431         # Initial data we need;
432         #
433         my $als_name     = @$als_data[2];
434         my $als_ip_id    = @$als_data[5];
435         my $als_ip       = $main::ip_id_num{$als_ip_id};
436         my $conf_dir     = $main::cfg{'CONF_DIR'};
437         my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
438         my $base_svr_ip  = $main::cfg{'BASE_SERVER_IP'};
439         my $sec_dns_ip   = $main::cfg{'SECONDARY_DNS'};
440
441         #
442         # Any secondary DNS defined;
443         #
444         if (!$sec_dns_ip) {
445                 $sec_dns_ip = $base_svr_ip;
446         }
447
448         my $tpl_dir     = "$conf_dir/bind/parts";
449         my $backup_dir  = "$conf_dir/bind/backup";
450         my $working_dir = "$conf_dir/bind/working";
451         my $db_fname    = "$als_name.db";
452         my $sys_cfg     = "$named_db_dir/$db_fname";
453         my $working_cfg = "$working_dir/$db_fname";
454
455         #
456         # Let's get needed tags and templates;
457         #
458         my ($entry, $dns2_b, $dns2_e) = ('', '', '');
459         ($rs, $entry, $dns2_b, $dns2_e) = get_tpl(
460                 $tpl_dir,
461                 'db_e.tpl',
462                 'db_dns2_b.tpl',
463                 'db_dns2_e.tpl'
464         );
465         return $rs if ($rs != 0);
466
467         my $seq = 0;
468         #
469         # RFC 1912
470         #
471         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
472         my $time2 = sprintf "%4d%02d%02d00",$year+1900,$mon+1,$mday,$seq;
473
474         #
475         # Let's prepare them;
476         #
477         my %tag_hash = (
478                 '{DMN_NAME}'         => $als_name,
479                 '{DMN_IP}'           => $als_ip,
480                 '{BASE_SERVER_IP}'   => $base_svr_ip,
481                 '{SECONDARY_DNS_IP}' => $sec_dns_ip,
482                 '{TIMESTAMP}'        => $time2
483         );
484         ($rs, $entry, $dns2_b, $dns2_e) = prep_tpl(
485                 \%tag_hash,
486                 $entry,
487                 $dns2_b,
488                 $dns2_e
489         );
490         return $rs if ($rs != 0);
491
492         #if ($main::cfg{'SECONDARY_DNS'}) {
493         #   my $dns2 = undef;
494         #    $dns2 = "\t\t \t NS \t $main::cfg{'SECONDARY_DNS'}.\n";
495         #    $dns2 = "$dns2_b$dns2$dns2_e";
496         #    ($rs, $entry) = repl_tag($dns2_b, $dns2_e, $entry, $dns2);
497         #    return $rs if ($rs != 0);
498         #} else {
499         #    ($rs, $entry) = del_tag($dns2_b, $dns2_e, $entry);
500         #    return $rs if ($rs != 0);
501         #}
502
503         #
504         # Let's store generated data;
505         #
506         $rs = store_file($working_cfg, $entry, $main::cfg{'ROOT_USER'}, $main::cfg{'ROOT_GROUP'}, 0644);
507         return $rs if ($rs != 0);
508
509         $rs = store_file($sys_cfg, $entry, $main::cfg{'ROOT_USER'}, $main::cfg{'ROOT_GROUP'}, 0644);
510         return $rs if ($rs != 0);
511
512         push_el(\@main::el, 'als_add_named_db_data()', 'Ending...');
513         return 0;
514 }
515
516
517 ################################################################################
518 ################################################################################
519 ##
520 ##
521 ##
522 sub als_del_named_db_data {
523         my ($als_data)   = @_;
524         my ($rs, $rdata) = (undef, undef);
525
526         push_el(\@main::el, 'als_del_named_db_data()', 'Starting...');
527         if (!defined($als_data) || $als_data eq '') {
528                 push_el(\@main::el, 'als_del_named_db_data()', 'ERROR: Undefined Input Data...');
529                 return -1;
530         }
531
532         #
533         # Initial data we need;
534         #
535         my $als_name     = @$als_data[2];
536         my $als_ip_id    = @$als_data[5];
537         my $als_ip       = $main::ip_id_num{$als_ip_id};
538         my $conf_dir     = $main::cfg{'CONF_DIR'};
539         my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
540         my $tpl_dir      = "$conf_dir/bind/parts";
541         my $backup_dir   = "$conf_dir/bind/backup";
542         my $working_dir  = "$conf_dir/bind/working";
543         my $db_fname     = "$als_name.db";
544         my $sys_cfg      = "$named_db_dir/$db_fname";
545         my $working_cfg  = "$working_dir/$db_fname";
546         #
547         # Let's remove .db files for this domain;
548         #
549         $rs = del_file($working_cfg);
550         return $rs if ($rs != 0);
551
552         $rs = del_file($sys_cfg);
553         return $rs if ($rs != 0);
554
555         push_el(\@main::el, 'als_del_named_db_data()', 'Ending...');
556         return 0;
557 }
558
559
560 ################################################################################
561 ################################################################################
562 ##
563 ##
564 ##
565 sub als_add_named_data {
566         my ($als_data) = @_;
567         my $rs         = undef;
568
569         push_el(\@main::el, 'als_add_named_data()', 'Starting...');
570         if (!defined($als_data) || $als_data eq '') {
571                 push_el(\@main::el, 'als_add_named_data()', 'ERROR: Undefined Input Data...');
572                 return -1;
573         }
574         return 0 if ($main::cfg{'CMD_NAMED'} eq 'no');
575
576         $rs = als_add_named_cfg_data($als_data);
577         return $rs if ($rs != 0);
578
579         $rs = als_add_named_db_data($als_data);
580         return $rs if ($rs != 0);
581
582         push_el(\@main::el, 'als_add_named_data()', 'Ending...');
583         return 0;
584 }
585
586
587 ################################################################################
588 ################################################################################
589 ##
590 ##
591 ##
592 sub als_change_named_data {
593         my ($als_data) = @_;
594         my $rs         = undef;
595
596         push_el(\@main::el, 'als_change_named_data()', 'Starting...');
597         if (!defined($als_data) || $als_data eq '') {
598                 push_el(\@main::el, 'als_change_named_data()', 'ERROR: Undefined Input Data...');
599                 return -1;
600         }
601         return 0 if ($main::cfg{'CMD_NAMED'} eq 'no');
602
603         $rs = als_add_named_data($als_data);
604         return $rs if ($rs != 0);
605
606         push_el(\@main::el, 'als_change_named_data()', 'Ending...');
607         return 0;
608 }
609
610
611 ################################################################################
612 ################################################################################
613 ##
614 ##
615 ##
616 sub als_del_named_data {
617         my ($als_data) = @_;
618         my $rs         = undef;
619
620         push_el(\@main::el, 'als_del_named_data()', 'Starting...');
621         if (!defined($als_data) || $als_data eq '') {
622                 push_el(\@main::el, 'als_del_named_data()', 'ERROR: Undefined Input Data...');
623                 return -1;
624         }
625         return 0 if ($main::cfg{'CMD_NAMED'} eq 'no');
626
627         $rs = als_del_named_cfg_data($als_data);
628         return $rs if ($rs != 0);
629
630         $rs = als_del_named_db_data($als_data);
631         return $rs if ($rs != 0);
632
633         push_el(\@main::el, 'als_del_named_data()', 'Ending...');
634         return 0;
635 }
636
637 ########################################
638 ##
639 ## alias httpd data managment;
640 ##
641 ########################################
642
643 ################################################################################
644 ################################################################################
645 ##
646 ##
647 ##
648 sub gen_httpd_als_entry {
649         my ($als_data) = @_;
650
651         push_el(\@main::el, 'gen_httpd_als_entry()', 'Starting...');
652         if (!defined($als_data) || $als_data eq '') {
653                 push_el(\@main::el, 'gen_httpd_als_entry()', "ERORR: Undefined input data...");
654                 return (-1, '');
655         }
656         my ($rs, $rdata)        = (undef, undef);
657         my $als_name            = @$als_data[2];
658         my $als_ip_id           = @$als_data[5];
659         my $als_ip              = $main::ip_id_num{$als_ip_id};
660         my $dmn_id              = @$als_data[7];
661         my $dmn_name            = @$als_data[8];
662         my $mount_point         = @$als_data[4];
663         my ($als_php, $als_cgi) = (@$als_data[26], @$als_data[27]);
664         my $url_forward         = @$als_data[6];
665         my $conf_dir            = $main::cfg{'CONF_DIR'};
666         my $tpl_dir             = "$conf_dir/apache/parts";
667         my (
668                 $als_b,
669                 $als_entry,
670                 $als_e,
671                 $als_rdr_b,
672                 $als_rdr_entry,
673                 $als_rdr_e,
674                 $als_cgi_b,
675                 $als_cgi_entry,
676                 $als_cgi_e,
677                 $als_php_b,
678                 $als_php_entry,
679                 $als_php_e,
680                 $als_php2_b,
681                 $als_php2_entry,
682                 $als_php2_e,
683                 $als_custom
684         ) = ('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
685         (
686                 $rs,
687                 $als_b,
688                 $als_entry,
689                 $als_e,
690                 $als_rdr_b,
691                 $als_rdr_entry,
692                 $als_rdr_e,
693                 $als_cgi_b,
694                 $als_cgi_entry,
695                 $als_cgi_e,
696                 $als_php_b,
697                 $als_php_entry,
698                 $als_php_e,
699                 $als_php2_b,
700                 $als_php2_entry,
701                 $als_php2_e,
702                 $als_custom
703         ) = get_tpl (
704                 $tpl_dir,
705                 'als_b.tpl',
706                 'als_entry.tpl',
707                 'als_e.tpl',
708                 'als_rdr_b.tpl',
709                 'als_rdr_entry.tpl',
710                 'als_rdr_e.tpl',
711                 'als_cgi_b.tpl',
712                 'als_cgi_entry.tpl',
713                 'als_cgi_e.tpl',
714                 'als_php_b.tpl',
715                 'als_php_entry.tpl',
716                 'als_php_e.tpl',
717                 'als_php2_b.tpl',
718                 'als_php2_entry.tpl',
719                 'als_php2_e.tpl',
720                 'custom.conf.tpl'
721         );
722         return ($rs, '') if ($rs != 0);
723
724         push_el(\@main::el, 'als_entry:', "$als_e");
725
726         if ($mount_point eq "/") {
727                 $mount_point = "";
728         }
729
730         my ($suexec_uid, $suexec_gid)    = get_dmn_suexec_user($dmn_id);
731         my $suexec_user_pref             = $main::cfg{'APACHE_SUEXEC_USER_PREF'};
732         my ($suexec_user, $suexec_group) = ("$suexec_user_pref$suexec_uid", "$suexec_user_pref$suexec_gid");
733         my %tag_hash =
734         (
735                 '{DMN_NAME}'                => $dmn_name,
736                 '{DMN_GRP}'                 => $dmn_name,
737                 '{ALS_NAME}'                => $als_name,
738                 '{SELF}'                    => $als_name,
739                 '{ALS_IP}'                  => $als_ip,
740                 '{URI}'                     => $url_forward,
741                 '{MOUNT_POINT}'             => $mount_point,
742                 '{STARTER_DIR}'             => $main::cfg{'PHP_STARTER_DIR'},
743                 '{PHP_VERSION}'             => $main::cfg{'PHP_VERSION'},
744                 '{WWW_DIR}'                 => $main::cfg{'APACHE_WWW_DIR'},
745                 '{APACHE_LOG_DIR}'          => $main::cfg{'APACHE_LOG_DIR'},
746                 '{GUI_ROOT_DIR}'            => $main::cfg{'GUI_ROOT_DIR'},
747                 '{PEAR_DIR}'                => $main::cfg{'PEAR_DIR'},
748                 '{APACHE_USERS_LOG_DIR}'    => $main::cfg{'APACHE_USERS_LOG_DIR'},
749                 '{CUSTOM_SITES_CONFIG_DIR}' => $main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'},
750                 '{SUEXEC_USER}'             => $suexec_user,
751                 '{SUEXEC_GROUP}'            => $suexec_group
752         );
753         (
754                 $rs,
755                 $als_b,
756                 $als_entry,
757                 $als_e,
758                 $als_rdr_entry,
759                 $als_cgi_entry,
760                 $als_php_entry,
761                 $als_php2_entry,
762                 $als_custom
763         ) = prep_tpl(
764                 \%tag_hash,
765                 $als_b,
766                 $als_entry,
767                 $als_e,
768                 $als_rdr_entry,
769                 $als_cgi_entry,
770                 $als_php_entry,
771                 $als_php2_entry,
772                 $als_custom
773         );
774         return ($rs, '') if ($rs != 0);
775
776         #
777         # Any CGI entry?
778         #
779         my $cgi_entry = undef;
780
781         if ($als_cgi eq 'yes') {
782                 $cgi_entry = "$als_cgi_b$als_cgi_entry$als_cgi_e";
783         } else {
784                 $cgi_entry = "$als_cgi_b$als_cgi_e";
785         }
786         ($rs, $als_entry) = repl_tag(
787                 $als_cgi_b,
788                 $als_cgi_e,
789                 $als_entry,
790                 $cgi_entry,
791                 "gen_httpd_als_entry"
792         );
793         return ($rs, '') if ($rs != 0);
794
795         #
796         # Any PHP entry?
797         #
798         my $php_entry = undef;
799         if ($als_php eq 'no') {
800                 $php_entry = "$als_php_b$als_php_entry$als_php_e";
801         } else {
802                 $php_entry     = "$als_php_b$als_php_e";
803                 my $php2_entry = "$als_php2_b$als_php2_entry$als_php2_e";
804                 ($rs, $als_entry) = repl_tag(
805                         $als_php2_b,
806                         $als_php2_e,
807                         $als_entry,
808                         $php2_entry,
809                         "gen_httpd_als_entry"
810                 );
811                 return ($rs, '') if ($rs != 0);
812         }
813         ($rs, $als_entry) = repl_tag(
814                 $als_php_b,
815                 $als_php_e,
816                 $als_entry,
817                 $php_entry,
818                 "gen_httpd_als_entry"
819         );
820         return ($rs, '') if ($rs != 0);
821
822         #
823         # Any REDIRECT entry?
824         #
825         if ($url_forward ne 'no') {
826                 my $rdr_entry = "$als_rdr_b$als_rdr_entry$als_rdr_e";
827                 ($rs, $als_entry) = repl_tag(
828                         $als_rdr_b,
829                         $als_rdr_e,
830                         $als_entry,
831                         $rdr_entry,
832                         "gen_httpd_als_entry"
833                 );
834                 return ($rs, '') if ($rs != 0);
835         }
836         my $als_result_entry = "$als_b$als_entry$als_e";
837
838         #
839         # Custom domain config file
840         #
841         if ( ! -e "$main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'}/$als_name.conf" ){
842                 $rs = store_file(
843                         "$main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'}/$als_name.conf",
844                         $als_custom,
845                         $main::cfg{'ROOT_USER'},
846                         $main::cfg{'ROOT_GROUP'},
847                         0644
848                 );
849                 return $rs if ($rs != 0);
850         }
851         push_el(\@main::el, 'gen_httpd_als_entry()', "\n$als_result_entry");
852         return (0, $als_result_entry);
853 }
854
855
856 ################################################################################
857 ################################################################################
858 ##
859 ##
860 ##
861 sub als_add_httpd_cfg_data {
862         my ($als_data)   = @_;
863         my ($rs, $rdata) = (undef, undef);
864
865         push_el(\@main::el, 'als_add_httpd_cfg_data()', 'Starting...');
866         if (!defined($als_data) || $als_data eq '') {
867                 push_el(\@main::el, 'als_add_httpd_cfg_data()', 'ERROR: Undefined Input Data...');
868                 return -1;
869         }
870
871         my $als_name    = @$als_data[2];
872         my $als_ip_id   = @$als_data[5];
873         my $als_ip      = $main::ip_id_num{$als_ip_id};
874         my $conf_dir    = $main::cfg{'CONF_DIR'};
875         my $tpl_dir     = "$conf_dir/apache/parts";
876         my $working_dir = "$conf_dir/apache/working";
877         my $backup_dir  = "$conf_dir/apache/backup";
878         my $sys_cfg     = "$main::cfg{'APACHE_SITES_DIR'}/ispcp.conf";
879         my $working_cfg = "$working_dir/ispcp.conf";
880         my $timestamp   = time;
881         my $backup_cfg  = "$backup_dir/httpd.conf.$timestamp";
882
883         #
884         # Getting needed templates;
885         #
886         my (
887                 $cfg_b,
888                 $cfg_e,
889                 $vh_b,
890                 $vh_entry,
891                 $vh_e,
892                 $als_b,
893                 $als_e
894         ) = ('', '', '', '', '', '', '');
895         (
896                 $rs,
897                 $cfg_b,
898                 $cfg_e,
899                 $vh_b,
900                 $vh_entry,
901                 $vh_e,
902                 $als_b,
903                 $als_e
904         ) = get_tpl (
905                 $tpl_dir,
906                 'cfg_b.tpl',
907                 'cfg_e.tpl',
908                 'vh_b.tpl',
909                 'vh_entry.tpl',
910                 'vh_e.tpl',
911                 'als_b.tpl',
912                 'als_e.tpl'
913         );
914         return $rs if ($rs != 0);
915
916         #
917         # Preparing templates;
918         #
919         my (
920                 $vh_b_val,
921                 $vh_entry_val,
922                 $vh_e_val
923         ) = ('', '', '');
924         my %tag_hash = ('{IP}' => $als_ip);
925         (
926                 $rs,
927                 $vh_b_val,
928                 $vh_entry_val,
929                 $vh_e_val
930         ) = prep_tpl (
931                 \%tag_hash,
932                 $vh_b,
933                 $vh_entry,
934                 $vh_e
935         );
936         return $rs if ($rs != 0);
937         my (
938                 $als_b_val,
939                 $als_e_val
940         ) = ('', '');
941         %tag_hash = ('{ALS_NAME}' => $als_name);
942         (
943                 $rs,
944                 $als_b_val,
945                 $als_e_val
946         ) = prep_tpl (
947                 \%tag_hash,
948                 $als_b,
949                 $als_e
950         );
951         return $rs if ($rs != 0);
952         #
953         # Let's construct alias entry.
954         #
955         my $als_entry_contents = '';
956         ($rs, $als_entry_contents) = gen_httpd_als_entry($als_data);
957         return $rs if ($rs != 0);
958
959         #
960         # Let's get some configs;
961         #
962         my ($sys, $working) = ('', '');
963
964         ($rs, $sys) = get_file($sys_cfg);
965         return $rs if ($rs != 0);
966
967         ($rs, $working) = get_file($working_cfg);
968         return $rs if ($rs != 0);
969
970         #
971         # Check for $cfg_b, $cfg_e in working config;
972         #
973         ($rs, $rdata) = get_tag($cfg_b, $cfg_e, $working);
974         return $rs if ($rs != 0);
975
976         #
977         # Check for $vh_b_val, $vh_e_val in working config;
978         # Have we valued virtual host entry in working config?
979         #
980         my $vh_entry_contents = '';
981         ($rs, $vh_entry_contents) = get_tag($vh_b_val, $vh_e_val, $working);
982
983         push_el(\@main::el, 'als_add_httpd_cfg_data()', "\n$vh_entry_contents");
984
985         if ($rs == -5) { # No, we have not! We must add it here.
986                 my $repl = "$als_entry_contents\n$als_b$als_e";
987                 ($rs, $vh_entry_val) = repl_tag(
988                         $als_b,
989                         $als_e,
990                         $vh_entry_val,
991                         $repl,
992                         "als_add_httpd_cfg_data"
993                 );
994                 return $rs if ($rs != 0);
995
996                 $repl = "$vh_b_val$vh_entry_val$vh_e_val\n";
997                 $repl .= "$vh_b$vh_e";
998                 ($rs, $working) = repl_tag(
999                         $vh_b,
1000                         $vh_e,
1001                         $working,
1002                         $repl,
1003                         "als_add_httpd_cfg_data "
1004                 );
1005                 return $rs if ($rs != 0);
1006                 # ?!
1007                 ($rs, $rdata) = get_tag($vh_b_val, $vh_e_val, $working);
1008                 return $rs if ($rs != 0);
1009
1010         } elsif ($rs == 0) { # Yes, we have! We must edit it here.
1011                 # Check for valued Domain Group Entry in this Virtual Host Entry;
1012                 ($rs, $rdata) = get_tag(
1013                         $als_b_val,
1014                         $als_e_val,
1015                         $vh_entry_contents
1016                 );
1017                 if ($rs == 0) {
1018                         # We have one ! We must delete it!
1019                         ($rs, $vh_entry_contents) = del_tag(
1020                         $als_b_val,
1021                         "$als_e_val\n",
1022                         $vh_entry_contents
1023                         );
1024                         return $rs if ($rs != 0);
1025                 }
1026
1027                 # Tag check.
1028                 ($rs, $rdata) = get_tag($als_b, $als_e, $vh_entry_contents);
1029                 return $rs if ($rs != 0);
1030
1031                 # Constructing als entry.
1032                 my $repl = "$als_entry_contents\n$als_b$als_e";
1033                 ($rs, $vh_entry_contents) = repl_tag(
1034                         $als_b,
1035                         $als_e,
1036                         $vh_entry_contents,
1037                         $repl,
1038                         "als_add_httpd_cfg_data"
1039                 );