Changeset 2698


Ignore:
Timestamp:
03/19/10 09:50:13 (23 months ago)
Author:
nuxwin
Message:
  • [ENGINE] Fixed #2265: Protected area's files are regenerated in wrong place

during update

  • [ENGINE] Refactoring and some others changes in the ispcp-htaccess-mngr

manager:

  • Added code documentation
  • Changed dump_el() to pop_el() in main section to avoid log

repetition on error

  • Changed prefixes of all subroutines names according the script

name

  • Merged 'htuser_del_user_system_data()' with

'htuser_del_group_system_data()'

  • Removed useless variables
  • Removed useless call of get_conf() (already performed in common

file)

  • Removed useless conditions in SQL statements
  • Removed useless wc command to improve performances
  • Removed useless sleep statement
  • Rewritten the htuser_mngr_engine() subroutine to avoid code

repetition

  • [GUI] Fixed #2265: Protected area's files are regenerated in wrong place

during update:

  • Added an database update to clean all .htaccess destdir paths
  • Added code to cleanup the submitted .htaccess destdir path
Location:
branches/omega-1.0.5
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/omega-1.0.5/CHANGELOG

    r2697 r2698  
    11ispCP ω 1.0.5 Changelog 
    22~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     3 
     42010-03-19 Laurent Declercq 
     5        - ENGINE: 
     6                * Fixed #2265: Protected area's files are regenerated in wrong place 
     7                        during update 
     8                * Refactoring and some others changes in the ispcp-htaccess-mngr manager: 
     9                        - Added code documentation 
     10                        - Changed `dump_el()` to `pop_el()` in main section to avoid log 
     11                                repetition on error 
     12                        - Changed prefixes of all subroutines names according the script 
     13                                name 
     14                        - Merged 'htuser_del_user_system_data()' with 
     15                                'htuser_del_group_system_data()' 
     16                        - Removed useless variables 
     17                        - Removed useless call of `get_conf()` (already performed in common 
     18                                file) 
     19                        - Removed useless conditions in SQL statements 
     20                        - Removed useless `wc` command to improve performances 
     21                        - Removed useless sleep statement 
     22                        - Rewritten the `htuser_mngr_engine()` subroutine to avoid code 
     23                                repetition 
     24        - GUI: 
     25                * Fixed #2265: Protected area's files are regenerated in wrong place 
     26                        during update: 
     27                        - Added an database update to clean all `.htaccess` destdir paths 
     28                        - Added code to cleanup  the submitted `.htaccess` destdir path 
    329 
    4302010-03-13 Laurent Declercq 
  • branches/omega-1.0.5/engine/ispcp-htaccess-mngr

    r2505 r2698  
    3030# 
    3131 
     32################################################################################ 
     33## Program Short Desciption:                                                  ## 
     34##  htaccess Manager - Create, Update or Delete a protected area              ## 
     35################################################################################ 
     36 
     37use strict; 
     38use warnings; 
     39 
     40# Ignore the warning of this kind: 
     41# Name "varname" used only once: possible typo at ... 
     42no warnings 'once'; 
     43 
    3244use FindBin; 
    3345use lib "$FindBin::Bin/"; 
    3446require 'ispcp_common_code.pl'; 
    3547 
    36 use strict; 
    37  
    38 use warnings; 
    39  
     48# Unique identifier of a database record related to a protected area 
    4049$main::htuser_task_id = undef; 
    4150 
    42 sub htuser_mngr_start_up { 
     51################################################################################ 
     52##                                SUBROUTINES                                 ## 
     53################################################################################ 
     54 
     55################################################################################ 
     56## 
     57## Start Up! 
     58## 
     59sub htaccess_mngr_start_up { 
     60 
     61        push_el(\@main::el, 'htaccess_mngr_start_up()', 'Starting...'); 
    4362 
    4463        my ($rs, $rdata) = (undef, undef); 
    4564 
    46         push_el(\@main::el, 'htuser_mngr_start_up()', 'Starting...'); 
    47  
    48         # checking for master process; 
     65        # Checking for master process; 
    4966        $rs = check_master(); 
    5067        return $rs if ($rs != 0); 
    5168 
    5269        # Let's clear Execution Logs, if any. 
    53  
    5470        if (-e $main::ispcp_htaccess_mngr_el) { 
    55  
    5671                $rs = del_file($main::ispcp_htaccess_mngr_el); 
    57  
    5872                return $rs if ($rs != 0); 
    59  
    60         } 
    61  
    62         # config check; 
    63  
    64         $rs = get_conf(); 
    65  
     73        } 
     74 
     75        # Sql check: 
     76        # FIXME Really required ? 
     77        my $sql = " 
     78                SELECT 
     79                        `admin_id` 
     80                FROM 
     81                        `admin` 
     82                WHERE 
     83                        `admin_id` = 1 
     84                ; 
     85        "; 
     86 
     87        ($rs, $rdata) = doSQL($sql); 
    6688        return $rs if ($rs != 0); 
    6789 
    68         # sql check; 
    69  
    70         # 
    71         # getting initial data also must be done here; 
    72         # 
    73  
    74         my $sql = " 
    75         SELECT 
    76                 domain_id, 
    77                 domain_name, 
    78                 domain_gid, 
    79                 domain_uid, 
    80                 domain_admin_id, 
    81                 domain_created_id, 
    82                 domain_created, 
    83                 domain_last_modified, 
    84                 domain_mailacc_limit, 
    85                 domain_ftpacc_limit, 
    86                 domain_traffic_limit, 
    87                 domain_sqld_limit, 
    88                 domain_sqlu_limit, 
    89                 domain_status, 
    90                 domain_alias_limit, 
    91                 domain_subd_limit, 
    92                 domain_ip_id, 
    93                 domain_disk_limit, 
    94                 domain_disk_usage, 
    95                 domain_php, 
    96                 domain_cgi 
    97         FROM 
    98                 domain 
    99         LIMIT 0,1"; 
    100  
    101         ($rs, $rdata) = doSQL($sql); 
    102  
    103         return $rs if ($rs != 0); 
    104  
    105         # 
    106         # getting task id and domain record id; 
    107         # 
    108  
     90        # Getting task id 
    10991        $main::htuser_task_id = $ARGV[0]; 
    11092 
    111         push_el(\@main::el, 'htuser_mngr_start_up()', 'Ending...'); 
    112  
    113         return 0; 
    114  
    115 } 
    116  
    117 sub htuser_mngr_shut_down { 
    118  
    119         my $rs = undef; 
    120  
    121         push_el(\@main::el, 'htuser_mngr_shut_down()', 'Starting...'); 
    122  
    123         push_el(\@main::el, 'htuser_mngr_shut_down()', 'Ending...'); 
    124  
    125         return 0; 
    126  
    127 } 
    128  
    129 sub htuser_add_user_system_data { 
     93        push_el(\@main::el, 'htaccess_mngr_start_up()', 'Ending...'); 
     94 
     95        0; 
     96} 
     97 
     98################################################################################ 
     99## 
     100## Shut Down! 
     101## 
     102sub htaccess_mngr_shut_down { 
     103 
     104        push_el(\@main::el, 'htaccess_mngr_shut_down()', 'Starting...'); 
     105 
     106        # Nothing todo here ! 
     107 
     108        push_el(\@main::el, 'htaccess_mngr_shut_down()', 'Ending...'); 
     109 
     110        0; 
     111} 
     112 
     113################################################################################ 
     114## 
     115## Create a protected area where the authentication is made by user 
     116## 
     117## Here, a protected area is represented by an directory wich contain a 
     118## `.htaccess` file. The `.htaccess` file must contain the following data: 
     119## 
     120## - The type of authentication - Only Basic authentication is supported 
     121## - The name of the protected area 
     122## - The file path wich contain the users authentication data (login/password) 
     123## - A list of one or more valid users 
     124## 
     125## See rfc 2617 for more information about http authentication specifications 
     126## 
     127sub htaccess_add_user_system_data { 
     128 
     129        push_el(\@main::el, 'htuser_add_user_system_data()', 'Starting...'); 
    130130 
    131131        my ($htuser_data) = @_; 
    132132 
     133        if (!defined($htuser_data) || $htuser_data eq '') { 
     134                push_el( 
     135                        \@main::el, 
     136                        'htaccess_add_user_system_data()', 
     137                        'ERROR: Undefined Input Data...' 
     138                ); 
     139 
     140                return -1; 
     141        } 
     142 
    133143        my ($rs, $rdata, $sql) = (undef, undef, undef); 
    134144 
    135         push_el(\@main::el, 'htuser_add_user_system_data()', 'Starting...'); 
    136  
    137         if (!defined($htuser_data) || $htuser_data eq '') { 
    138  
    139                 push_el(\@main::el, 'htuser_add_user_system_data()', 'ERROR: Undefined Input Data...'); 
     145        my ( 
     146                $dmn_name, 
     147                $user_ids, 
     148                undef, 
     149                $auth_type, 
     150                $auth_name, 
     151                $path, 
     152                $status, 
     153                $domain_uid, 
     154                $domain_gid 
     155        ) = @$htuser_data; 
     156 
     157        my $www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
     158        my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'}; 
     159        my $htaccess_users_file = "$www_dir/$dmn_name/$htaccess_users_file_name"; 
     160 
     161        # See ticket #2265 for the reason of this! 
     162        if(-e "$www_dir/$dmn_name/htdocs.disabled" && $path =~ m@^/htdocs(/.*)?$@) { 
     163                $path = defined $1 ? "/htdocs.disabled$1" : '/htdocs.disabled'; 
     164        } 
     165         
     166        my $htaccess_file = "$www_dir/$dmn_name$path/.htaccess"; 
     167 
     168        my $unames = ''; 
     169 
     170        foreach (split(',', $user_ids)) { 
     171                $sql = " 
     172                        SELECT 
     173                                `uname` 
     174                        FROM 
     175                                `htaccess_users` 
     176                        WHERE 
     177                                `id` = $_ 
     178                        ; 
     179                "; 
     180 
     181                ($rs, $rdata) = doSQL($sql); 
     182                return $rs if ($rs != 0); 
     183 
     184                $unames .= $rdata->[0]->[0] . ' '; 
     185        } 
     186 
     187        # Remove trailing space 
     188        $unames =~ s/\s$//; 
     189 
     190        if(-e $htaccess_file) { 
     191                ($rs, $rdata) = get_file($htaccess_file); 
     192                return $rs if($rs !=0); 
     193        } else { 
     194                $rdata = ''; 
     195        } 
     196 
     197        $rdata = set_zone( 
     198                $rdata, 
     199                "\t\tAuthType $auth_type 
     200                AuthName \"$auth_name\" 
     201                AuthUserFile $htaccess_users_file 
     202                Require user $unames", 
     203                'PROTECTION', 
     204                '#' 
     205        ); 
     206 
     207        if (-e "$www_dir/$dmn_name/$path") { 
     208                $rs = store_file( 
     209                        $htaccess_file, $rdata, $domain_uid, $domain_gid, 0644 
     210                ); 
     211 
     212                return $rs if ($rs != 0); 
     213        } else { 
     214                push_el( 
     215                        \@main::el, 
     216                        'htaccess_add_user_system_data()', 
     217                        "ERROR: The path $www_dir/$dmn_name/$path doesn't exists!" 
     218                ); 
    140219 
    141220                return -1; 
    142  
    143         } 
    144  
    145         my ($dmn_id, $dmn_name, $user_ids, $auth_type, $auth_name, $path, $status, $domain_uid, $domain_gid) = 
    146                 (@$htuser_data[0], @$htuser_data[1], @$htuser_data[2], @$htuser_data[3], @$htuser_data[4], 
    147                  @$htuser_data[5], @$htuser_data[6], @$htuser_data[7], @$htuser_data[8]); 
    148  
    149         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
    150  
    151         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'}; 
    152  
    153         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name"; 
    154  
    155         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess"; 
    156  
    157         my $unames = ''; 
    158  
    159         foreach (split(',', $user_ids)) { 
    160  
    161                 my $id = $_; 
    162  
    163                 $sql = "SELECT uname FROM htaccess_users WHERE id = '$id'"; 
    164  
    165                 ($rs, $rdata) = doSQL($sql); 
    166  
    167                 return $rs if ($rs != 0); 
    168  
    169                 $rdata = @$rdata[0]; 
    170  
    171                 $unames .= @$rdata[0]." "; 
    172  
    173         } 
    174  
    175         my $hfile = (get_file($htaccess_file))[1]; 
    176         if(!defined($hfile)) { 
    177                 $hfile = ""; 
    178         } 
    179  
    180         my $htaccess_content = 
    181         set_zone($hfile, " 
    182         AuthType $auth_type 
    183         AuthName \"$auth_name\" 
    184         AuthUserFile $htaccess_users_file 
    185         Require user $unames 
    186         ", "PROTECTION", "#" ); 
    187  
    188         if (-e "$apache_www_dir/$dmn_name/$path/") { 
    189                 $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644); 
    190                 return $rs if ($rs != 0); 
    191         } 
    192  
    193         push_el(\@main::el, 'htuser_add_user_system_data()', 'Ending...'); 
    194  
    195         return 0; 
    196  
    197 } 
    198  
    199 sub htuser_del_user_system_data { 
    200  
    201         my ($htuser_data) = @_; 
    202  
    203         my ($rs, $rdata, $sql) = (undef, undef, undef); 
    204  
    205         push_el(\@main::el, 'htuser_del_user_system_data()', 'Starting...'); 
    206  
    207         if (!defined($htuser_data) || $htuser_data eq '') { 
    208  
    209                 push_el(\@main::el, 'htuser_del_user_system_data()', 'ERROR: Undefined Input Data...'); 
    210  
    211                 return -1; 
    212  
    213         } 
    214  
    215         my ($dmn_id, $dmn_name, $user_ids, $auth_type, $auth_name, $path, $status, $domain_uid, $domain_gid) = 
    216                 (@$htuser_data[0], @$htuser_data[1], @$htuser_data[2], @$htuser_data[3], @$htuser_data[4], 
    217                  @$htuser_data[5], @$htuser_data[6], @$htuser_data[7], @$htuser_data[8]); 
    218  
    219         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
    220         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'}; 
    221         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name"; 
    222         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess"; 
    223  
    224         foreach (split(',', $dmn_id)) { 
    225  
    226                 my $id = $_; 
    227  
    228                 $sql = "SELECT count(dmn_id) FROM htaccess WHERE dmn_id = '$id' AND path = '$path'"; 
    229  
    230                 ($rs, $rdata) = doSQL($sql); 
    231  
    232                 return $rs if ($rs != 0); 
    233  
    234                 $rdata = @$rdata[0]; 
    235  
    236                 my ($rcounter) = (@$rdata[0]); 
    237  
    238                 if($rcounter eq '1') { 
    239  
    240                         if (-e $htaccess_file) { 
    241  
    242                                 my $htaccess_content = del_zone( (get_file($htaccess_file))[1],  "PROTECTION", "#" ); 
    243  
    244                                 $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644); 
    245  
    246                                 return $rs if ($rs != 0); 
    247  
    248                                 my $cmd_wc = $main::cfg{'CMD_WC'}; 
    249  
    250                                 my $size = `$cmd_wc -c < $htaccess_file`; 
    251  
    252                                 if ($size < 2) { 
    253                                         del_file($htaccess_file); 
    254                                 } 
    255                         } 
    256                 } 
    257         } 
    258  
    259         return $rs if ($rs != 0); 
    260  
    261         push_el(\@main::el, 'htuser_del_user_system_data()', 'Ending...'); 
    262  
    263         return 0; 
    264  
    265 } 
    266  
    267 sub htuser_add_group_system_data { 
    268  
    269         my ($htuser_data) = @_; 
    270  
    271         my ($rs, $rdata, $sql) = (undef, undef, undef); 
     221        } 
     222 
     223        push_el(\@main::el, 'htaccess_add_user_system_data()', 'Ending...'); 
     224 
     225        0; 
     226} 
     227 
     228################################################################################ 
     229## 
     230## Create a protected area where the authentication is made by group 
     231## 
     232## Here, a protected area is represented by an directory wich contain a 
     233## `.htaccess` file. The `.htaccess` file must contain the following data: 
     234## 
     235## - The type of authentication - Only Basic authentication is supported 
     236## - The name of the protected area 
     237## - The file path wich contain the users authentication data 
     238## - The file path wich contain the groups authentication data 
     239## - A list of one or more valid groups 
     240## 
     241## See rfc 2617 for more information about http authentication specifications 
     242## 
     243sub htaccess_add_group_system_data { 
    272244 
    273245        push_el(\@main::el, 'htuser_add_group_system_data()', 'Starting...'); 
    274246 
    275         if (!defined($htuser_data) || $htuser_data eq '') { 
    276  
     247        my ($htgroup_data) = @_; 
     248 
     249        if (!defined($htgroup_data) || $htgroup_data eq '') { 
    277250                push_el( 
    278251                                \@main::el, 
    279                                 'htuser_add_group_system_data()', 'ERROR: Undefined Input Data...' 
    280                            ); 
     252                                'htaccess_add_group_system_data()', 
     253                                'ERROR: Undefined Input Data...' 
     254                ); 
    281255 
    282256                return -1; 
    283  
    284         } 
    285  
    286         my ($dmn_id, $dmn_name, $group_ids, $auth_type, $auth_name, $path, $status,     $domain_uid, $domain_gid) = 
    287                 (@$htuser_data[0], @$htuser_data[1], @$htuser_data[2], @$htuser_data[3], @$htuser_data[4], 
    288                  @$htuser_data[5], @$htuser_data[6], @$htuser_data[7], @$htuser_data[8]); 
     257        } 
     258 
     259        my ($rs, $rdata, $sql) = (undef, undef, undef); 
     260 
     261        my ( 
     262                $dmn_name, 
     263                undef, 
     264                $group_ids, 
     265                $auth_type, 
     266                $auth_name, 
     267                $path, 
     268                $status, 
     269                $domain_uid, 
     270                $domain_gid 
     271        ) = @$htgroup_data; 
     272 
     273        my $www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
     274        my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'}; 
     275        my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'}; 
     276        my $htaccess_users_file = "$www_dir/$dmn_name/$htaccess_users_file_name"; 
     277        my $htaccess_groups_file = "$www_dir/$dmn_name/$htaccess_groups_file_name"; 
     278 
     279        # See ticket #2265 for the reason of this! 
     280        if(-e "$www_dir/$dmn_name/htdocs.disabled" && $path =~ m@^/htdocs(/.*)?$@) { 
     281                $path = defined $1 ? "/htdocs.disabled$1" : '/htdocs.disabled'; 
     282        } 
     283         
     284        my $htaccess_file = "$www_dir/$dmn_name$path/.htaccess"; 
    289285 
    290286        my $ugroups = ''; 
    291287 
    292288        foreach (split(',', $group_ids)) { 
    293  
    294                 my $group_id = $_; 
    295  
    296289                $sql = " 
    297290                        SELECT 
    298                                 ugroup 
     291                                `ugroup` 
    299292                        FROM 
    300                                 htaccess_groups 
     293                                `htaccess_groups` 
    301294                        WHERE 
    302                                 id = '$group_id' 
    303                         AND 
    304                                 dmn_id = '$dmn_id'"; 
     295                                `id` = $_ 
     296                        ; 
     297                "; 
    305298 
    306299                ($rs, $rdata) = doSQL($sql); 
    307  
    308                 $rdata = @$rdata[0]; 
    309  
    310                 my $ugroup = @$rdata[0]; 
    311  
    312                 $ugroups .= "$ugroup "; 
    313  
    314         } 
    315  
    316         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'}; 
    317         my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'}; 
    318         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
    319         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name"; 
    320         my $htaccess_groups_file = "$apache_www_dir/$dmn_name/$htaccess_groups_file_name"; 
    321         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess"; 
    322  
    323         my $hfile = (get_file($htaccess_file))[1]; 
    324         if(!defined($hfile)) { 
    325                 $hfile = ""; 
    326         } 
    327         my $htaccess_content = 
    328         set_zone($hfile, " 
    329         AuthType $auth_type 
    330         AuthName \"$auth_name\" 
    331         AuthUserFile $htaccess_users_file 
    332         AuthGroupFile $htaccess_groups_file 
    333         Require group $ugroups 
    334         ", "PROTECTION", "#" ); 
    335  
    336         $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644); 
    337  
    338         return $rs if ($rs != 0); 
    339  
    340         push_el(\@main::el, 'htuser_add_group_system_data()', 'Ending...'); 
    341  
    342         return 0; 
    343  
    344 } 
    345  
    346 sub htuser_del_group_system_data { 
    347  
    348         my ($htuser_data) = @_; 
    349  
    350         my ($rs, $rdata, $sql) = (undef, undef, undef); 
    351  
    352         push_el(\@main::el, 'htuser_del_group_system_data()', 'Starting...'); 
    353  
    354         if (!defined($htuser_data) || $htuser_data eq '') { 
    355  
    356                 push_el(\@main::el, 'htuser_del_group_system_data()', 'ERROR: Undefined Input Data...'); 
     300                return $rs if($rs != 0); 
     301 
     302                $ugroups .= $rdata->[0]->[0] . ' '; 
     303        } 
     304         
     305        # Remove trailing space 
     306        $ugroups =~ s/\s$//; 
     307 
     308        if(-e $htaccess_file) { 
     309                ($rs, $rdata) = get_file($htaccess_file); 
     310                return $rs if($rs != 0); 
     311        } else { 
     312                $rdata = ''; 
     313        } 
     314 
     315        $rdata = set_zone( 
     316                $rdata, 
     317                "\t\tAuthType $auth_type 
     318                AuthName \"$auth_name\" 
     319                AuthUserFile $htaccess_users_file 
     320                AuthGroupFile $htaccess_groups_file 
     321                Require group $ugroups", 
     322                'PROTECTION', 
     323                '#' 
     324        ); 
     325 
     326        if (-e "$www_dir/$dmn_name/$path") { 
     327                $rs = store_file($htaccess_file, $rdata, $domain_uid, $domain_gid, 0644); 
     328                return $rs if ($rs != 0); 
     329        } else { 
     330                push_el( 
     331                        \@main::el, 
     332                        'htaccess_add_user_system_data()', 
     333                        "ERROR: The path $www_dir/$dmn_name/$path doesn't exists!" 
     334                ); 
    357335 
    358336                return -1; 
    359  
    360         } 
    361  
    362         my ($dmn_id, $dmn_name, $group_ids, $auth_type, $auth_name, $path, $status, $domain_uid, $domain_gid) = 
    363                 (@$htuser_data[0], @$htuser_data[1], @$htuser_data[2], @$htuser_data[3], @$htuser_data[4], 
    364                  @$htuser_data[5], @$htuser_data[6], @$htuser_data[7], @$htuser_data[8]); 
    365  
    366         my $htaccess_users_file_name = $main::cfg{'HTACCESS_USERS_FILE_NAME'}; 
    367         my $htaccess_groups_file_name = $main::cfg{'HTACCESS_GROUPS_FILE_NAME'}; 
    368         my $apache_www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
    369         my $htaccess_users_file = "$apache_www_dir/$dmn_name/$htaccess_users_file_name"; 
    370         my $htaccess_groups_file = "$apache_www_dir/$dmn_name/$htaccess_groups_file_name"; 
    371         my $htaccess_file = "$apache_www_dir/$dmn_name/$path/.htaccess"; 
    372  
    373         foreach (split(',', $dmn_id)) { 
    374  
    375                 my $id = $_; 
    376  
    377                 $sql = "SELECT COUNT(dmn_id) FROM htaccess WHERE dmn_id = '$id' AND path = '$path'"; 
    378  
    379                 ($rs, $rdata) = doSQL($sql); 
    380                 return $rs if ($rs != 0); 
    381  
    382                 $rdata = @$rdata[0]; 
    383  
    384                 my ($rcounter) = (@$rdata[0]); 
    385  
    386                 if($rcounter eq '1') { 
    387  
    388                         if (-e $htaccess_file) { 
    389  
    390                                 my $htaccess_content = del_zone( (get_file($htaccess_file))[1],  "PROTECTION", "#" ); 
    391  
    392                                 $rs = store_file($htaccess_file, $htaccess_content, $domain_uid, $domain_gid, 0644); 
    393                                 return $rs if ($rs != 0); 
    394  
    395                                 my $cmd_wc = $main::cfg{'CMD_WC'}; 
    396  
    397                                 my $size = `$cmd_wc -c < $htaccess_file`; 
    398  
    399                                 if ($size < 2) { 
    400                                         del_file($htaccess_file); 
    401                                 } 
    402                         } 
     337        } 
     338 
     339        push_el(\@main::el, 'htaccess_add_group_system_data()', 'Ending...'); 
     340 
     341        0; 
     342} 
     343 
     344################################################################################ 
     345## 
     346## Delete a protected area 
     347## 
     348sub htaccess_del_protected_area_data { 
     349 
     350        push_el(\@main::el, 'htaccess_del_protected_area_data()', 'Starting...'); 
     351 
     352        if (!@_) { 
     353                push_el( 
     354                        \@main::el, 
     355                        'htaccess_del_protected_area_data()', 
     356                        'ERROR: Undefined Input Data...' 
     357                ); 
     358 
     359                return -1; 
     360        } 
     361 
     362        my ($rs, $rdata) = (undef, undef); 
     363        my($dmn_name, $path, $domain_uid, $domain_gid) = @_; 
     364        my $www_dir = $main::cfg{'APACHE_WWW_DIR'}; 
     365        my $htaccess_file = "$www_dir/$dmn_name/$path/.htaccess"; 
     366 
     367        if(-e $htaccess_file) { 
     368                ($rs, $rdata) = get_file($htaccess_file); 
     369                return $rs if($rs != 0); 
     370 
     371                $rdata = del_zone($rdata, 'PROTECTION', '#'); 
     372 
     373                if(length $rdata < 2) { 
     374                        $rs = del_file($htaccess_file); 
     375                        return $rs if($rs != 0); 
     376                } else { 
     377                        $rs = store_file( 
     378                                $htaccess_file, $rdata, $domain_uid, $domain_gid, 0644 
     379                        ); 
     380 
     381                        return $rs if ($rs != 0); 
    403382                } 
    404383        } 
    405         return $rs if ($rs != 0); 
    406  
    407         push_el(\@main::el, 'htuser_del_group_system_data()', 'Ending...'); 
    408  
    409         return 0; 
    410  
    411 } 
    412  
    413 sub htuser_mngr_engine { 
    414  
    415         my ($rs, $rows, $rdata) = (undef, undef, undef); 
    416  
    417         push_el(\@main::el, 'htuser_mngr_engine()', 'Starting...'); 
    418  
    419         my $id = $main::htuser_task_id; 
     384 
     385        push_el(\@main::el, 'htaccess_del_protected_area_data()', 'Ending...'); 
     386 
     387        0; 
     388} 
     389 
     390################################################################################ 
     391## Create a protected area 
     392## 
     393## A protected area is represented by a directory that contain a `.htaccess` 
     394## file. The authentification can be made by user or group. 
     395## 
     396## See htuser_add_user_system_data() and htuser_add_group_system_data() 
     397## subroutines for more information about the possible content of the `.htaccess` 
     398## file. 
     399## 
     400sub htaccess_mngr_engine { 
     401 
     402        push_el(\@main::el, 'htaccess_mngr_engine()', 'Starting...'); 
     403 
     404        my ($rs, $rows) = (undef, undef); 
     405 
     406        # 
     407        # Get needed data from the database - Begin 
     408        # 
     409 
    420410        my $sql = " 
    421411                SELECT 
    422                         t1.dmn_id, 
    423                         t2.domain_name, 
    424                         t1.user_id, 
    425                         t1.auth_type, 
    426                         t1.auth_name, 
    427                         t1.path, 
    428                         t1.status, 
    429                         t2.domain_uid, 
    430                         t2.domain_gid 
     412                        `domain_name`, 
     413                        `user_id`, 
     414                        `group_id`, 
     415                        `auth_type`, 
     416                        `auth_name`, 
     417                        `path`, 
     418                        `status`, 
     419                        `domain_uid`, 
     420                        `domain_gid` 
    431421                FROM 
    432                         htaccess as t1, 
    433                         domain as t2 
     422                        `domain` 
     423                LEFT JOIN 
     424                        `htaccess` ON `id` =  $main::htuser_task_id 
    434425                WHERE 
    435                         t1.id = '$id' 
    436                 AND 
    437                         t1.user_id != 0 
    438                 AND 
    439                         t1.dmn_id = t2.domain_id 
     426                        `domain_id` = `dmn_id` 
     427                ; 
    440428        "; 
    441429 
    442430        ($rs, $rows) = doSQL($sql); 
    443  
    444431        return $rs if ($rs != 0); 
    445432 
    446         if (scalar(@$rows) == 0) { 
    447  
    448                 # 
    449                 # protect with group; 
    450                 # 
    451  
    452                 $id = $main::htuser_task_id; 
    453                 $sql = " 
    454                         SELECT 
    455                                 t1.dmn_id, 
    456                                 t2.domain_name, 
    457                                 t1.group_id, 
    458                                 t1.auth_type, 
    459                                 t1.auth_name, 
    460                                 t1.path, 
    461                                 t1.status, 
    462                                 t2.domain_uid, 
    463                                 t2.domain_gid 
    464                         FROM 
    465                                 htaccess as t1, 
    466                                 domain as t2 
    467                         WHERE 
    468                                 t1.id = '$id' 
    469                         AND 
    470                                 t1.group_id != 0 
    471                         AND 
    472                                 t1.dmn_id = t2.domain_id 
    473                 "; 
    474  
    475                 ($rs, $rows) = doSQL($sql); 
    476  
     433        # 
     434        # Get needed data from the database - End 
     435        # 
     436 
     437        # The record exists in the database ? 
     438        if (scalar(@$rows) != 0) { 
     439                my $entries = @$rows[0]; 
     440 
     441                my $action = undef; 
     442                # Protected area by user(s) 
     443                if(@$entries[1] ne '0') { 
     444                        $action = \&htaccess_add_user_system_data; 
     445                # protected area by group(s) 
     446                } elsif(@$entries[2] ne '0') { 
     447                        $action = \&htaccess_add_group_system_data; 
     448                } else { 
     449                        push_el( 
     450                                \@main::el, 
     451                                'htaccess_mngr_engine()', 
     452                                'ERROR: The data are corrupted...' 
     453                        ); 
     454 
     455                        return -1; 
     456                } 
     457 
     458                # 
     459                # Perform needed action - Begin 
     460                # 
     461 
     462                # Add or update a protected area 
     463                if(@$entries[6] eq 'toadd' || @$entries[6] eq 'change') { 
     464                        $rs = &$action($entries); 
     465                # Deleting a protected area 
     466                } elsif(@$entries[6] eq 'delete') { 
     467                        $rs = htaccess_del_protected_area_data( 
     468                                @$entries[0], @$entries[5], @$entries[7], @$entries[8] 
     469                        ); 
     470                } else { 
     471                                push_el( 
     472                                        \@main::el, 
     473                                        'htaccess_mngr_engine()', 
     474                                        'ERROR: Unknown action requested...' 
     475                                ); 
     476 
     477                        return -1; 
     478                } 
     479 
     480                # 
     481                # Perform needed action - End 
     482                # 
     483 
     484                # 
     485                # Update database - Begin 
     486                # 
     487 
     488                if($rs == 0) { 
     489                        if(@$entries[6] ne 'delete') { 
     490                                $sql = " 
     491                                        UPDATE 
     492                                                `htaccess` 
     493                                        SET 
     494                                                `status` = 'ok' 
     495                                        WHERE 
     496                                                `id` = $main::htuser_task_id 
     497                                        ; 
     498                                "; 
     499                        } else { 
     500                                $sql = " 
     501                                        DELETE FROM 
     502                                                `htaccess` 
     503                                        WHERE 
     504                                                `id` = $main::htuser_task_id 
     505                                        ; 
     506                                "; 
     507                        } 
     508                # An error was occured during action process 
     509                } else { 
     510                        my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el)); 
     511                        $msg =~ s/\'/\\\'/g; 
     512 
     513                        $sql = " 
     514                                UPDATE 
     515                                        `htaccess` 
     516                                SET 
     517                                        `status` = '$sub_name | $msg' 
     518                                WHERE 
     519                                        `id` = $main::htuser_task_id 
     520                                ; 
     521                        "; 
     522                } 
     523 
     524                # Execute SQL statement now 
     525                ($rs, undef) = doSQL($sql); 
    477526                return $rs if ($rs != 0); 
    478527 
    479                 if (scalar(@$rows) != 0) { 
    480  
    481                         my $entry = @$rows[0]; 
    482  
    483                         my ($htuser_status, 
    484                                 $htuser_id, 
    485                                 $dmn_name, 
    486                                 $path, 
    487                                 $dmn_id) = (@$entry[6], 
    488                                                    $main::htuser_task_id, 
    489                                                    @$entry[1], 
    490                                                    @$entry[5], 
    491                                                    @$entry[0]); 
    492  
    493                         if ($htuser_status eq 'toadd' || $htuser_status eq 'change') { 
    494  
    495                                 $rs = htuser_add_group_system_data($entry); 
    496  
    497                                 if ($rs == 0) { 
    498  
    499                                         $sql = "UPDATE htaccess SET status = 'ok' WHERE id = $htuser_id"; 
    500  
    501                                 } else { 
    502  
    503                                         my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el)); 
    504  
    505                                         $msg =~ s/\'/\\\'/g; 
    506  
    507                                         $sql = "UPDATE htaccess SET status= '$sub_name | $msg' WHERE id = '$htuser_id';"; 
    508  
    509                                 } 
    510  
    511                                 ($rs, $rdata) = doSQL($sql); 
    512  
    513                                 return $rs if ($rs != 0); 
    514  
    515                         } elsif ($htuser_status eq 'delete') { 
    516  
    517                                 $rs = htuser_del_group_system_data($entry); 
    518  
    519                                 if ($rs == 0) { 
    520  
    521                                         $sql = "DELETE FROM htaccess WHERE id = '$htuser_id'"; 
    522  
    523                                 } else { 
    524  
    525                                         my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el)); 
    526  
    527                                         $msg =~ s/\'/\\\'/g; 
    528  
    529                                         $sql = "UPDATE htaccess SET status = '$sub_name | $msg' WHERE id = $htuser_id;"; 
    530  
    531                                 } 
    532  
    533                                 ($rs, $rdata) = doSQL($sql); 
    534  
    535                                 return $rs if ($rs != 0); 
    536  
    537                                 $rs = htuser_del_user_system_data($entry); 
    538  
    539                         } 
    540  
    541                 } 
     528                # 
     529                # Update database - End 
     530                # 
    542531 
    543532        } else { 
    544  
    545                 # 
    546                 # continue with single user; 
    547                 # 
    548  
    549                 my $entry = @$rows[0]; 
    550  
    551                 my ($htuser_status, $htuser_id, $dmn_name, $path) = 
    552  
    553                         (@$entry[6], $main::htuser_task_id, @$entry[1], @$entry[5]); 
    554  
    555                 if ($htuser_status eq 'toadd' || $htuser_status eq 'change') { 
    556  
    557                         $rs = htuser_add_user_system_data($entry); 
    558  
    559                         if ($rs == 0) { 
    560  
    561                                 $sql = "UPDATE htaccess SET status = 'ok' WHERE id = '$htuser_id';"; 
    562  
    563                         } else { 
    564  
    565                                 my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el)); 
    566  
    567                                 $msg =~ s/\'/\\\'/g; 
    568  
    569                                 $sql = "UPDATE htaccess SET status = '$sub_name | $msg' WHERE id = '$htuser_id';"; 
    570  
    571                         } 
    572  
    573                         ($rs, $rdata) = doSQL($sql); 
    574  
    575                         return $rs if ($rs != 0); 
    576  
    577                 } elsif ($htuser_status eq 'delete') { 
    578  
    579                         $rs = htuser_del_user_system_data($entry); 
    580  
    581                         if ($rs == 0) { 
    582  
    583                                 $sql = "DELETE FROM htaccess WHERE id = '$htuser_id';"; 
    584  
    585                         } else { 
    586  
    587                                 my ($sub_name, $msg) = split(/$main::el_sep/, pop_el(\@main::el)); 
    588  
    589                                 $msg =~ s/\'/\\\'/g; 
    590  
    591                                 $sql = "UPDATE htaccess SET status = '$sub_name | $msg' WHERE id = '$htuser_id';"; 
    592  
    593                         } 
    594  
    595                         ($rs, $rdata) = doSQL($sql); 
    596  
    597                         return $rs if ($rs != 0); 
    598  
    599                 } 
    600  
    601         } 
    602  
    603         sleep(1); 
    604  
    605         push_el(\@main::el, 'htuser_mngr_engine()', 'Ending...'); 
    606  
    607         return 0; 
    608  
    609 } 
    610  
    611 my $rs = undef; 
    612  
    613 $rs = htuser_mngr_start_up(); 
     533                push_el( 
     534                        \@main::el, 
     535                        'htaccess_mngr_engine()', 
     536                        "ERROR: No data found for task $main::htuser_task_id ..." 
     537                ); 
     538 
     539                return -1; 
     540        } 
     541 
     542        push_el(\@main::el, 'htaccess_mngr_engine()', 'Ending...'); 
     543 
     544        0; 
     545} 
     546 
     547################################################################################ 
     548##                               MAIN PROGRAM                                 ## 
     549################################################################################ 
     550 
     551my $rs = htaccess_mngr_start_up(); 
    614552 
    615553if ($rs != 0) { 
    616  
    617     dump_el(\@main::el, $main::ispcp_htaccess_mngr_el); 
    618  
    619     htuser_mngr_shut_down(); 
    620  
    621     exit 1; 
    622  
    623 } 
    624  
    625 $rs = htuser_mngr_engine(); 
     554        pop_el(\@main::el, $main::ispcp_htaccess_mngr_el); 
     555        htaccess_mngr_shut_down(); 
     556        exit 1; 
     557} 
     558 
     559$rs = htaccess_mngr_engine(); 
    626560 
    627561if ($rs != 0) { 
    628  
    629     dump_el(\@main::el, $main::ispcp_htaccess_mngr_el); 
    630  
    631     htuser_mngr_shut_down(); 
    632  
    633     exit 1; 
    634  
    635 } 
    636  
    637 $rs = htuser_mngr_shut_down(); 
     562        pop_el(\@main::el, $main::ispcp_htaccess_mngr_el); 
     563        htaccess_mngr_shut_down(); 
     564        exit 1; 
     565} 
     566 
     567$rs = htaccess_mngr_shut_down(); 
    638568 
    639569if ($rs != 0) { 
    640  
    641     dump_el(\@main::el, $main::ispcp_htaccess_mngr_el); 
    642  
    643     exit 1; 
    644  
     570        pop_el(\@main::el, $main::ispcp_htaccess_mngr_el); 
     571        exit 1; 
    645572} 
    646573 
  • branches/omega-1.0.5/gui/client/protected_areas_add.php

    r2599 r2698  
    7777                return; 
    7878        } 
     79 
     80        $path = clean_input($_POST['other_dir'], false); 
     81 
     82        // Cleanup path: 
     83        // Adds a slash as a first char of the path if it doesn't exists 
     84        // Removes the double slashes  
     85        // Remove the trailing slash if it exists 
     86        if($path != '/') { 
     87                $clean_path = array(); 
     88 
     89                foreach(explode(DIRECTORY_SEPARATOR, $path) as $dir) { 
     90                        if($dir != '') { 
     91                                $clean_path[] = $dir; 
     92                        } 
     93                } 
     94         
     95                $path = '/' . implode(DIRECTORY_SEPARATOR, $clean_path); 
     96        } 
     97 
     98        $domain = $_SESSION['user_logged']; 
     99 
    79100        // Check for existing directory 
    80         $path = clean_input($_POST['other_dir'], false); 
    81         $domain = $_SESSION['user_logged']; 
    82101        // We need to use the virtual file system 
    83102        $vfs = new vfs($domain, $sql); 
  • branches/omega-1.0.5/gui/include/class.databaseUpdate.php

    r2599 r2698  
    888888        } 
    889889 
     890        /** 
     891         * Fix for ticket #2265 http://www.isp-control.net/ispcp/ticket/2265 
     892         * 
     893         * This update adding slash as first char if doesn't exists and remove 
     894         * double and trailling slash in the relative paths of `.htaccess` files 
     895         * for convenience reasons in the ispcp-htaccess-mngr engine script. 
     896         * 
     897         * @author Laurent Declercq (nuxwin) <laurent.declercq@ispcp.net> 
     898         * @copyright 2006-2010 by ispCP | http://isp-control.net 
     899         * @since rxxxx 
     900         * 
     901         * @access protected 
     902         * @return string sql statements to be performed 
     903         */ 
     904        protected function _databaseUpdate_29() { 
     905 
     906                $sqlUpd = array(); 
     907                $sql = Database::getInstance(); 
     908 
     909                $query = " 
     910                        SELECT 
     911                                `id`, 
     912                                `path` 
     913                        FROM 
     914                                `htaccess` 
     915                        ; 
     916                "; 
     917 
     918                $rs = exec_query($sql, $query); 
     919 
     920                if ($rs->RecordCount() != 0) { 
     921                        while (!$rs->EOF) { 
     922                                $path = $rs->fields['path']; 
     923 
     924                                $clean_path = array(); 
     925 
     926                                foreach(explode(DIRECTORY_SEPARATOR, $path) as $dir) { 
     927                                        if($dir != '') { 
     928                                                $clean_path[] = $dir; 
     929                                        } 
     930                                } 
     931         
     932                                $path = '/' . implode(DIRECTORY_SEPARATOR, $clean_path); 
     933 
     934                                $sqlUpd[] = " 
     935                                        UPDATE 
     936                                                `htaccess` 
     937                                        SET 
     938                                                `path` = '$path' 
     939                                        WHERE 
     940                                                `id`= '{$rs->fields['id']}' 
     941                                        ; 
     942                                "; 
     943 
     944                                $rs->MoveNext(); 
     945                        } 
     946                } 
     947 
     948                return $sqlUpd; 
     949        } 
     950 
    890951        /* 
    891952         * DO NOT CHANGE ANYTHING BELOW THIS LINE! 
Note: See TracChangeset for help on using the changeset viewer.