root/tags/omega-1.0.0-rc3/engine/ispcp-htuser-mngr

Revision 817, 27.6 kB (checked in by raphael, 1 year ago)

Fixed #717: User assignment does not work (thanks to Joximu)
Fixed #635: can't protect a directory with non-standrad characters in its path
Fixed #632: can't add an ftp account to a non-standard characters mount point
Fixed an error where switching reseller->client and swtiching back would display as if we were logged from client's

Line 
1 #!/usr/bin/perl
2
3 # ISPCP ω (OMEGA) - Virtual Hosting Control System | Omega Version
4 # Copyright (c) 2001-2006 by moleSoftware GmbH
5 # http://www.molesoftware.com
6 # Copyright (c) 2006-2007 by isp Control Panel
7 # http://isp-control.net
8 #
9 #
10 # License:
11 #    This program is free software; you can redistribute it and/or
12 #    modify it under the terms of the MPL Mozilla Public License
13 #    as published by the Free Software Foundation; either version 1.1
14 #    of the License, or (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 #    MPL Mozilla Public License for more details.
20 #
21 #    You may have received a copy of the MPL Mozilla Public License
22 #    along with this program.
23 #
24 #    An on-line copy of the MPL Mozilla Public License can be found
25 #    http://www.mozilla.org/MPL/MPL-1.1.html
26 #
27 #
28 # The ISPCP ω Home Page is at:
29 #
30 #    http://isp-control.net
31 #
32
33
34 use FindBin;
35 use lib "$FindBin::Bin/";
36 require 'ispcp_common_code.pl';
37
38 use strict;
39
40 use warnings;
41
42 $main::htuser_task_id = undef;
43
44 sub htuser_mngr_start_up {
45
46         my ($rs, $rdata) = (undef, undef);
47
48         push_el(\@main::el, 'htuser_mngr_start_up()', 'Starting...');
49
50         # checking for master process;
51
52 #       $rs = check_master();
53 #
54 #       return $rs if ($rs != 0);
55
56         # Let's clear Execution Logs, if any.
57
58         if (-e $main::ispcp_htuser_mngr_el) {
59
60                 $rs = del_file($main::ispcp_htuser_mngr_el);
61
62                 return $rs if ($rs != 0);
63
64         }
65
66         # config check;
67
68         $rs = get_conf();
69
70         return $rs if ($rs != 0);
71
72         # sql check;
73
74         #
75         # getting initial data also must be done here;
76         #
77
78         my $sql = "
79                 SELECT
80                         domain_id,
81                         domain_name,
82                         domain_gid,
83                         domain_uid,
84                         domain_admin_id,
85                         domain_created_id,
86                         domain_created,
87                         domain_last_modified,
88                         domain_mailacc_limit,
89                         domain_ftpacc_limit,
90                         domain_traffic_limit,
91                         domain_sqld_limit,
92                         domain_sqlu_limit,
93                         domain_status,
94                         domain_alias_limit,
95                         domain_subd_limit,
96                         domain_ip_id,
97                         domain_disk_limit,
98                         domain_disk_usage,
99                         domain_php,
100                         domain_cgi
101                 FROM
102                         domain
103             LIMIT 0,1
104         ";
105
106         ($rs, $rdata) = doSQL($sql);
107
108         return $rs if ($rs != 0);
109
110         #
111         # getting task id and domain record id;
112         #
113
114         $main::htuser_task_id = $ARGV[0];
115
116         push_el(\@main::el, 'htuser_mngr_start_up()', 'Ending...');
117
118         return 0;
119
120 }
121
122 sub htuser_mngr_shut_down {
123
124         my $rs = undef;
125
126         push_el(\@main::el, 'htuser_mngr_shut_down()', 'Starting...');
127
128         push_el(\@main::el, 'htuser_mngr_shut_down()', 'Ending...');
129
130         return 0;
131
132 }
133
134 #
135 # AuthUserFile add/remove entries;
136 #
137
138 sub htuser_add_user_data {
139
140         push_el(\@main::el, 'htuser_add_user_data()', 'Starting...');
141
142         my ($dmn_name, $uname, $upass) = @_;
143
144         my $rs = undef;
145
146         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'};
147
148         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
149
150         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
151
152         my $htaccess_users_content = '';
153
154         if (-e $htaccess_users_file) {
155
156                 open(F, '<', $htaccess_users_file);
157
158                 while (!eof(F)) {
159
160                         my $line = readline(F);
161
162                         $htaccess_users_content .= $line if ($line !~ /^$uname:/);
163
164
165                 }
166
167                 close(F);
168
169                 $htaccess_users_content .= "$uname:$upass\n";
170
171         } else {
172
173                 $htaccess_users_content = "$uname:$upass\n";
174
175         }
176
177         $rs = store_file($htaccess_users_file, $htaccess_users_content, 'root', 'root', 0644);
178
179         return $rs if ($rs != 0);
180
181         push_el(\@main::el, 'htuser_add_user_data()', 'Ending...');
182
183         return 0;
184
185 }
186
187 sub htuser_del_user_data {
188
189         push_el(\@main::el, 'htuser_del_user_data()', 'Starting...');
190
191         my ($dmn_name, $uname) = @_;
192
193         my $rs = undef;
194
195         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'};
196
197         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
198
199         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
200
201         my $htaccess_users_content = '';
202
203         if (-e $htaccess_users_file) {
204
205                 open(F, '<', $htaccess_users_file);
206
207                 while (!eof(F)) {
208
209                         my $line = readline(F);
210
211                         $htaccess_users_content .= $line if ($line !~ /^$uname:/);
212
213
214                 }
215
216                 close(F);
217
218                 $rs = store_file(
219                                                  $htaccess_users_file,
220                                                  $htaccess_users_content,
221                                                  'root',
222                                                  'root',
223                                                  0644
224                                                 );
225
226                 return $rs if ($rs != 0);
227
228         }
229
230         push_el(\@main::el, 'htuser_del_user_data()', 'Ending...');
231
232         return 0;
233
234 }
235
236 #
237 # AuthGroupFile add/remove entries;
238 #
239
240 sub htuser_add_group_line_data {
241
242         push_el(\@main::el, 'htuser_add_group_line_data()', 'Starting...');
243
244         my ($line, $gname, $uname) = @_;
245
246         if ($line !~ / $uname/) {
247
248                 $line =~ s/$gname://;
249
250                 chop($line);
251
252                 $line = "$gname:$line $uname\n";
253
254         }
255
256         push_el(\@main::el, 'htuser_add_group_line_data()', 'Ending...');
257
258         return $line;
259 }
260
261 sub htuser_add_group_data {
262
263         push_el(\@main::el, 'htuser_add_group_data()', 'Starting...');
264
265         my ($dmn_name, $gname, $uname) = @_;
266
267         my $rs = undef;
268
269         my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'};
270
271         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
272
273         my $htaccess_groups_file = "$apache_www_dir/$dmn_name/$htaccess_groups_file_name";
274
275         my $htaccess_groups_content = '';
276
277
278         if (-e $htaccess_groups_file) {
279
280                 my $gf_edit_flag = 0;
281
282                 open(F, '<', $htaccess_groups_file);
283
284                 while (!eof(F)) {
285
286                         my $line = readline(F);
287
288                         if ($line !~ /^$gname:/) {
289
290                                 $htaccess_groups_content .= $line;
291
292                         } else {
293
294                                 $htaccess_groups_content .= htuser_add_group_line_data($line, $gname, $uname);
295
296                                 $gf_edit_flag++;
297
298                         }
299
300                 }
301
302                 $htaccess_groups_content .= "$gname: $uname\n" if ($gf_edit_flag == 0);
303
304                 close(F);
305
306         } else {
307
308                 $htaccess_groups_content = "$gname: $uname\n";
309
310         }
311
312         $rs = store_file($htaccess_groups_file, $htaccess_groups_content, 'root', 'root', 0644);
313
314         return $rs if ($rs != 0);
315
316         push_el(\@main::el, 'htuser_add_group_data()', 'Ending...');
317
318         return 0;
319
320 }
321
322 sub htuser_del_group_line_data {
323
324         push_el(\@main::el, 'htuser_del_group_line_data()', 'Starting...');
325
326         my ($line, $gname, $uname) = @_;
327
328         if ($line =~ / $uname/) {
329
330                 $line =~ s/$gname://;
331
332                 chop($line);
333
334                 $line =~ s/ $uname//;
335
336                 $line = "$gname:$line\n" if (length($line) > 0);
337
338         }
339
340         push_el(\@main::el, 'htuser_del_group_line_data()', 'Ending...');
341
342         return $line;
343 }
344
345 sub htuser_del_group_data {
346
347         push_el(\@main::el, 'htuser_del_group_data()', 'Starting...');
348
349         my ($dmn_name, $gname, $uname) = @_;
350
351         my $rs = undef;
352
353         my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'};
354
355         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
356
357         my $htaccess_groups_file = "$apache_www_dir/$dmn_name/$htaccess_groups_file_name";
358
359         my $htaccess_groups_content = '';
360
361         if (-e $htaccess_groups_file) {
362
363                 open(F, '<', $htaccess_groups_file);
364
365                 while (!eof(F)) {
366
367                         my $line = readline(F);
368
369                         if ($line !~ /^$gname:/) {
370
371                                 $htaccess_groups_content .= $line;
372
373                         } else {
374
375                                 $htaccess_groups_content .= htuser_del_group_line_data($line, $gname, $uname);
376
377                         }
378
379                 }
380
381                 close(F);
382
383                 $rs = store_file($htaccess_groups_file, $htaccess_groups_content, 'root', 'root', 0644);
384
385                 return $rs if ($rs != 0);
386
387         }
388
389         push_el(\@main::el, 'htuser_del_group_data()', 'Ending...');
390
391         return 0;
392
393 }
394
395 sub htuser_del_user_system_data {
396
397         my ($htuser_data) = @_;
398
399         my ($rs, $rdata, $sql) = (undef, undef, undef);
400
401         push_el(\@main::el, 'htuser_del_user_system_data()', 'Starting...');
402
403         if (!defined($htuser_data) || $htuser_data eq '') {
404
405                 push_el(\@main::el, 'htuser_del_user_system_data()', 'ERROR: Undefined Input Data...');
406
407                 return -1;
408
409         }
410
411         my ($dmn_id, $dmn_name, $user_ids, $auth_type, $auth_name, $path, $status, $domain_uid, $domain_gid) =
412
413                 (@$htuser_data[0],
414                  @$htuser_data[1],
415                  @$htuser_data[2],
416                  @$htuser_data[3],
417                  @$htuser_data[4],
418                  @$htuser_data[5],
419                  @$htuser_data[6],
420                  @$htuser_data[7],
421                  @$htuser_data[8]);
422
423         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
424
425         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'};
426
427         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
428
429         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess";
430
431         foreach (split(',', $user_ids)) {
432
433                 my $id = $_;
434
435                 #$sql = "select uname from htaccess_users where id = $id";
436                 $sql = "SELECT uname, count(htaccess.user_id) as c FROM htaccess_users LEFT JOIN htaccess ON htaccess_users.id=htaccess.user_id WHERE htaccess_users.id=$id GROUP BY htaccess.user_id";
437
438                 ($rs, $rdata) = doSQL($sql);
439
440                 return $rs if ($rs != 0);
441
442                 $rdata = @$rdata[0];
443
444                 my ($uname,$ucounter) = (@$rdata[0],@$rdata[1]);
445
446                 if($ucounter <= 1) {
447
448                         $rs = htuser_del_user_data($dmn_name, $uname);
449
450                         return $rs if ($rs != 0);
451                 }
452
453         }
454
455         foreach (split(',', $dmn_id)) {
456
457                 my $id = $_;
458
459                 $sql = "select count(dmn_id) from htaccess where dmn_id = $id and path='$path'";
460
461                 ($rs, $rdata) = doSQL($sql);
462
463                 return $rs if ($rs != 0);
464
465                 $rdata = @$rdata[0];
466
467                 my ($rcounter) = (@$rdata[0]);
468
469                 if($rcounter eq '1') {
470
471                         if (-e $htaccess_file) {
472
473                                 my $htaccess_content = del_zone( (get_file($htaccess_file))[1],  "PROTECTION", "#" );
474
475                                 $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644);
476
477                                 return $rs if ($rs != 0);
478
479                                 my $cmd_du = $main::cfg{'CMD_DU'};
480
481                                 my $size = `$cmd_du -sb $htaccess_file`;
482
483                                 if ($size<2) {
484
485                                         del_file($htaccess_file);
486
487                                 }
488
489                         }
490
491                 }
492
493         }
494
495
496         return $rs if ($rs != 0);
497
498         push_el(\@main::el, 'htuser_del_user_system_data()', 'Ending...');
499
500         return 0;
501
502 }
503
504 sub htuser_add_user_system_data {
505
506         my ($htuser_data) = @_;
507
508         my ($rs, $rdata, $sql) = (undef, undef, undef);
509
510         push_el(\@main::el, 'htuser_add_user_system_data()', 'Starting...');
511
512         if (!defined($htuser_data) || $htuser_data eq '') {
513
514                 push_el(\@main::el, 'htuser_add_user_system_data()', 'ERROR: Undefined Input Data...');
515
516                 return -1;
517
518         }
519
520         my ($dmn_id, $dmn_name, $user_ids, $auth_type, $auth_name, $path, $status, $domain_uid, $domain_gid) =
521
522                 (@$htuser_data[0],
523                  @$htuser_data[1],
524                  @$htuser_data[2],
525                  @$htuser_data[3],
526                  @$htuser_data[4],
527                  @$htuser_data[5],
528                  @$htuser_data[6],
529                  @$htuser_data[7],
530                  @$htuser_data[8]);
531
532         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
533
534         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'};
535
536         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
537
538         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess";
539
540         my $unames = '';
541
542         foreach (split(',', $user_ids)) {
543
544                 my $id = $_;
545
546                 $sql = "select uname, upass from htaccess_users where id = $id";
547
548                 ($rs, $rdata) = doSQL($sql);
549
550                 return $rs if ($rs != 0);
551
552                 $rdata = @$rdata[0];
553
554                 my ($uname, $upass) = (@$rdata[0], @$rdata[1]);
555
556                 $rs = htuser_add_user_data($dmn_name, $uname, $upass);
557
558                 return $rs if ($rs != 0);
559
560                 $unames .= "$uname ";
561
562         }
563
564         my $htaccess_content =
565         set_zone( (get_file($htaccess_file))[1], "
566         AuthType $auth_type
567         AuthName \"$auth_name\"
568         AuthUserFile $htaccess_users_file
569         Require user $unames
570         ", "PROTECTION", "#" );
571
572         $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644);
573
574         return $rs if ($rs != 0);
575
576         push_el(\@main::el, 'htuser_add_user_system_data()', 'Ending...');
577
578         return 0;
579
580 }
581
582 sub htuser_add_user_to_group {
583
584         my ($dmn_id, $dmn_name, $ugroup, $uid) = @_;
585
586         my ($rs, $rdata) = (undef, undef);
587
588         push_el(\@main::el, 'htuser_add_user_to_group_data()', 'Starting...');
589
590         my $sql = "select uname,upass from htaccess_users where id = $uid and dmn_id = $dmn_id";
591
592         ($rs, $rdata) = doSQL($sql);
593
594         return $rs if ($rs != 0);
595
596         if (scalar(@$rdata) == 1) {
597
598                 my $entry = @$rdata[0];
599
600                 my ($uname, $upass) = (@$entry[0], @$entry[1]);
601
602                 $rs = htuser_add_user_data($dmn_name, $uname, $upass);
603
604                 return $rs if ($rs != 0);
605
606                 $rs = htuser_add_group_data($dmn_name, $ugroup, $uname);
607
608                 return $rs if ($rs != 0);
609
610         }
611
612         push_el(\@main::el, 'htuser_add_user_to_group_data()', 'Ending...');
613
614         return 0;
615
616 }
617
618 sub htuser_add_group_system_data {
619
620         my ($htuser_data) = @_;
621
622         my ($rs, $rdata, $sql) = (undef, undef, undef);
623
624         push_el(\@main::el, 'htuser_add_group_system_data()', 'Starting...');
625
626         if (!defined($htuser_data) || $htuser_data eq '') {
627
628                 push_el(
629                                 \@main::el,
630                                 'htuser_add_group_system_data()', 'ERROR: Undefined Input Data...'
631                            );
632
633                 return -1;
634
635         }
636
637         my ($dmn_id,
638                 $dmn_name,
639                 $group_ids,
640                 $auth_type,
641                 $auth_name,
642                 $path,
643                 $status,
644                 $domain_uid,
645                 $domain_gid) = (@$htuser_data[0],
646                                                 @$htuser_data[1],
647                                                 @$htuser_data[2],
648                                                 @$htuser_data[3],
649                                                 @$htuser_data[4],
650                                                 @$htuser_data[5],
651                                                 @$htuser_data[6],
652                                                 @$htuser_data[7],
653                                                 @$htuser_data[8]);
654
655         my $ugroups = '';
656
657         #
658         # add all user data for this group;
659         #
660
661         foreach (split(',', $group_ids)) {
662
663                 my $group_id = $_;
664
665                 $sql = <<SQL_QUERY;
666
667                         select
668
669                                 ugroup, members
670
671                         from
672
673                                 htaccess_groups
674
675                         where
676
677                                 id = $group_id
678
679                         and
680
681                                 dmn_id = $dmn_id
682 SQL_QUERY
683
684                 ($rs, $rdata) = doSQL($sql);
685
686                 $rdata = @$rdata[0];
687
688                 my ($ugroup, $members) = (@$rdata[0], @$rdata[1]);
689
690                 foreach (split(',', $members)) {
691
692                         my $user_id = $_;
693
694                         $rs = htuser_add_user_to_group($dmn_id, $dmn_name, $ugroup, $user_id);
695
696                         return $rs if ($rs != 0);
697
698                 }
699
700                 $ugroups .= "$ugroup ";
701
702         }
703
704         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'};
705
706         my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'};
707
708         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
709
710         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
711
712         my $htaccess_groups_file = "$apache_www_dir/$dmn_name/$htaccess_groups_file_name";
713
714         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess";
715
716
717         my $htaccess_content =
718         set_zone( (get_file($htaccess_file))[1], "
719         AuthType $auth_type
720         AuthName \"$auth_name\"
721         AuthUserFile $htaccess_users_file
722         AuthGroupFile $htaccess_groups_file
723         Require group $ugroups
724         ", "PROTECTION", "#" );
725
726         $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644);
727
728         return $rs if ($rs != 0);
729
730         push_el(\@main::el, 'htuser_add_group_system_data()', 'Ending...');
731
732         return 0;
733
734 }
735
736 sub htuser_del_group_system_data {
737
738         my ($htuser_data) = @_;
739
740         my ($rs, $rdata, $sql) = (undef, undef, undef);
741
742         push_el(\@main::el, 'htuser_del_group_system_data()', 'Starting...');
743
744         if (!defined($htuser_data) || $htuser_data eq '') {
745
746                 push_el(\@main::el, 'htuser_del_group_system_data()', 'ERROR: Undefined Input Data...');
747
748                 return -1;
749
750         }
751
752         my ($dmn_id, $dmn_name, $group_ids, $auth_type, $auth_name, $path, $status, $domain_uid, $domain_gid) =
753
754                 (@$htuser_data[0],
755                  @$htuser_data[1],
756                  @$htuser_data[2],
757                  @$htuser_data[3],
758                  @$htuser_data[4],
759                  @$htuser_data[5],
760                  @$htuser_data[6],
761                  @$htuser_data[7],
762                  @$htuser_data[8]);
763
764         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'};
765
766         my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'};
767
768         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'};
769
770         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
771
772         my $htaccess_groups_file = "$apache_www_dir/$dmn_name/$htaccess_groups_file_name";
773
774         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess";
775
776         foreach (split(',', $group_ids)) {
777
778                 my $group_id = $_;
779
780 #               $sql = "select ugroup, members from htaccess_groups where id = $group_id and dmn_id = $dmn_id";
781                 $sql = "select ugroup, members, count(htaccess.id) as c from htaccess_groups left join htaccess on htaccess.group_id=htaccess_groups.id where htaccess_groups.id = $group_id and htaccess_groups.dmn_id = $dmn_id group by htaccess.group_id";
782
783                 ($rs, $rdata) = doSQL($sql);
784
785                 $rdata = @$rdata[0];
786
787                 my ($gname, $members, $cgroup) = (@$rdata[0], @$rdata[1],@$rdata[2]);
788
789                 if ($cgroup <=1) {
790
791                         foreach (split(',', $members)) {
792
793                                 my $id = $_;
794
795                                 $sql = "select uname from htaccess_users where id = $id";
796
797                                 ($rs, $rdata) = doSQL($sql);
798
799                                 return $rs if ($rs != 0);
800
801                                 $rdata = @$rdata[0];
802
803                                 my ($uname) = (@$rdata[0]);
804
805                                 $rs = htuser_del_group_data($dmn_name, $gname, $uname);
806
807                                 return $rs if ($rs != 0);
808
809                                 $sql = "SELECT uname, count(htaccess.user_id) as c FROM htaccess_users LEFT JOIN htaccess ON htaccess_users.id=htaccess.user_id WHERE htaccess_users.id=$id GROUP BY htaccess.user_id";
810
811                                 ($rs, $rdata) = doSQL($sql);
812
813                                 return $rs if ($rs != 0);
814
815                                 $rdata = @$rdata[0];
816
817                                 my ($uname,$ucounter) = (@$rdata[0],@$rdata[1]);
818
819                                 if($ucounter == 0) {
820
821                                         $rs = htuser_del_user_data($dmn_name, $uname);
822
823                                         return $rs if ($rs != 0);
824                                 }
825
826                         }
827                 }
828
829                 foreach (split(',', $dmn_id)) {
830
831                         my $id = $_;
832
833                         $sql = "select count(dmn_id) from htaccess where dmn_id = $id and path='$path'";
834
835                         ($rs, $rdata) = doSQL($sql);
836
837                         return $rs if ($rs != 0);
838
839                         $rdata = @$rdata[0];
840
841                         my ($rcounter) = (@$rdata[0]);
842
843                         if($rcounter eq '1') {
844
845                                 if (-e $htaccess_file) {
846
847                                         my $htaccess_content = del_zone( (get_file($htaccess_file))[1],  "PROTECTION", "#" );
848
849                                         $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644);
850
851                                         return $rs if ($rs != 0);
852
853                                         my $cmd_du = $main::cfg{'CMD_DU'};
854
855                                         my $size = `$cmd_du -sb $htaccess_file`;
856
857                                         if ($size<2) {
858
859                                                 del_file($htaccess_file);
860
861                                         }
862
863                                 }
864
865                         }
866
867                 }
868
869
870
871         }
872
873         return $rs if ($rs != 0);
874
875         push_el(\@main::el, 'htuser_del_group_system_data()', 'Ending...');
876
877         return 0;
878
879 }
880
881 %main::cleared = ();
882
883 sub htuser_mngr_engine {
884
885         my ($rs, $rows, $rdata) = (undef, undef, undef);
886
887         push_el(\@main::el, 'htuser_mngr_engine()', 'Starting...');
888
889         my $sql;
890
891         #
892         # user management;
893         #
894
895         $sql = <<SQL_QUERY;
896
897                 select
898
899                         t1.dmn_id,
900                         t2.domain_name,
901                         t1.user_id,
902                         t1.auth_type,
903                         t1.auth_name,
904                         t1.path,
905                         t1.status,
906                         t2.domain_uid,
907                         t2.domain_gid
908
909                 from
910
911                         htaccess as t1,
912                         domain as t2
913
914                 where
915
916                         t1.id = $main::htuser_task_id
917
918                 and
919
920                         t1.user_id != 0
921
922                 and
923
924                         t1.dmn_id = t2.domain_id
925
926 SQL_QUERY
927
928         ($rs, $rows) = doSQL($sql);
929
930         return $rs if ($rs != 0);
931
932         if (scalar(@$rows) == 0) {
933
934                 #
935                 # protect with group;
936                 #
937
938                 $sql = <<SQL_QUERY;
939
940                 select
941
942                         t1.dmn_id,
943                         t2.domain_name,
944                         t1.group_id,
945                         t1.auth_type,
946                         t1.auth_name,
947                         t1.path,
948                         t1.status,
949                         t2.domain_uid,
950                         t2.domain_gid
951
952                 from
953
954                         htaccess as t1,
955                         domain as t2
956
957                 where
958
959                         t1.id = $main::htuser_task_id
960
961                 and
962
963                         t1.group_id != 0
964
965                 and
966
967                         t1.dmn_id = t2.domain_id
968
969 SQL_QUERY
970
971                 ($rs, $rows) = doSQL($sql);
972
973                 return $rs if ($rs != 0);
974
975                 if (scalar(@$rows) != 0) {
976
977                         my $entry = @$rows[0];
978
979                         my ($htuser_status,
980                                 $htuser_id,
981                                 $dmn_name,
982                                 $path,
983                                 $dmn_id) = (@$entry[6],
984                                                    $main::htuser_task_id,
985                                                    @$entry[1],
986                                                    @$entry[5],
987                                                    @$entry[0]);
988
989                         my $htaccess_group_cf = "/tmp/htaccess-group-cf-$dmn_name";
990
991                         if (! -e $htaccess_group_cf) {
992
993                                 print "group removal\n";
994
995                                 $main::cleared{$dmn_name} = 'yes';
996
997                                 sys_command_rs("touch $htaccess_group_cf");
998
999                                 my $htaccess_users_file_name =
1000
1001                                 $main::cfg{'HTACCESS_USERS_FILE_NAME'};
1002
1003                                 my $htaccess_groups_file_name =
1004
1005                                 $main::cfg{'HTACCESS_GROUPS_FILE_NAME'};
1006
1007                                 my $apache_www_dir =
1008
1009                                 $main::cfg{'APACHE_WWW_DIR'};
1010
1011                                 my $htaccess_users_file =
1012
1013                                 "$apache_www_dir/$dmn_name/$htaccess_users_file_name";
1014
1015                                 my $htaccess_groups_file =
1016
1017                                 "$apache_www_dir/$dmn_name/$htaccess_groups_file_name";
1018
1019                                 my $htaccess_file =
1020
1021                                 "$apache_www_dir/$dmn_name/$path/.htaccess";
1022
1023                         }
1024
1025                         if ($htuser_status eq 'toadd' || $htuser_status eq 'change') {
1026
1027                                 $rs = htuser_add_group_system_data($entry);
1028
1029                                 if ($rs == 0) {
1030
1031                                         $sql = "update htaccess set status='ok' where id = $htuser_id";
1032
1033                                 } else {
1034
1035                                         my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el));
1036
1037                                         $msg =~ s/\'/\\\'/g;
1038
1039                                         $sql = "update htaccess set status='$sub_name | $msg' where id = $htuser_id;";
1040
1041                                 }
1042
1043                                 ($rs, $rdata) = doSQL($sql);
1044
1045                                 return $rs if ($rs != 0);
1046
1047                         } elsif ($htuser_status eq 'delete') {
1048
1049                                 $rs = htuser_del_group_system_data($entry);
1050
1051                                 if ($rs == 0) {
1052
1053                                         $sql = "delete from htaccess where id = $htuser_id";
1054
1055                                 } else {
1056
1057                                         my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el));
1058
1059                                         $msg =~ s/\'/\\\'/g;
1060
1061                                         $sql = "update htaccess set status='$sub_name | $msg' where id = $htuser_id;";
1062
1063                                 }
1064
1065                                 ($rs, $rdata) = doSQL($sql);
1066
1067                                 return $rs if ($rs != 0);
1068
1069                                 $rs = htuser_del_user_system_data($entry);
1070
1071                         }
1072
1073                 }
1074
1075         } else {
1076
1077                 #
1078                 # continue with single user;
1079                 #
1080
1081                 my $entry = @$rows[0];
1082
1083                 my ($htuser_status, $htuser_id, $dmn_name, $path) =
1084
1085                         (@$entry[6], $main::htuser_task_id, @$entry[1], @$entry[5]);
1086
1087                 my $htaccess_user_cf = "/tmp/htaccess-user-cf-$dmn_name";
1088
1089                 my $htaccess_group_cf = "/tmp/htaccess-group-cf-$dmn_name";
1090
1091                 if ($htuser_status eq 'toadd' || $htuser_status eq 'change') {
1092
1093                         #print "user removal\n";
1094
1095                         if (! -e $htaccess_user_cf &&
1096                                 ! -e $htaccess_group_cf) {
1097
1098                                 $main::cleared{$dmn_n