#------------------------------------------------------------
# Information bay directories
#------------------------------------------------------------

{
    use esmith::AccountsDB;
    my $adb = esmith::AccountsDB->open_ro();
    $OUT = "";
    foreach my $ibay ($adb->ibays)
    {
        my %properties = $ibay->props;
        my $key = $ibay->key;
        #------------------------------------------------------------
        # Figure out which combination of parameters to use. If
        # PublicAccess parameter is present, this is e-smith 4.0.
        # Otherwise, it's e-smith 3.0.
        #------------------------------------------------------------

        my $allow;
        my $pass;
        my $satisfy;

        if ($properties{'PublicAccess'})
        {
            if ($properties{'PublicAccess'} eq 'none')
            {
                next;
            }
            elsif ($properties{'PublicAccess'} eq 'local')
            {
                $allow   = $localAccess;
                $pass    = 0;
                $satisfy = 'all';
            }
            elsif ($properties{'PublicAccess'} eq 'local-pw')
            {
                $allow   = $localAccess;
                $pass    = 1;
                $satisfy = 'all';
            }
            elsif ($properties{'PublicAccess'} eq 'global')
            {
                $allow   = 'all';
                $pass    = 0;
                $satisfy = 'all';
            }
            elsif ($properties{'PublicAccess'} eq 'global-pw')
            {
                $allow   = 'all';
                $pass    = 1;
                $satisfy = 'all';
            }
            elsif ($properties{'PublicAccess'} eq 'global-pw-remote')
            {
                $allow   = $localAccess;
                $pass    = 1;
                $satisfy = 'any';
            }
        }
        elsif ($properties {'ReadAccess'} eq 'global')
        {
            if ($properties {'UsePassword'} eq 'yes')
            {
                $allow   = 'all';
                $pass    = 1;
                $satisfy = 'all';
            }
            else
            {
                $allow   = 'all';
                $pass    = 0;
                $satisfy = 'all';
            }
        }
        else
        {
            if ($properties {'UsePassword'} eq 'yes')
            {
                $allow   = $localAccess;
                $pass    = 1;
                $satisfy = 'all';
            }
            else
            {
                $allow   = $localAccess;
                $pass    = 0;
                $satisfy = 'all';
            }
        }

        my $allowOverride = $properties{'AllowOverride'} || "None";
        my $dynamicContent = $properties{'CgiBin'} || "disabled";
        my $followSymLinks = $properties{'FollowSymLinks'} || "disabled";
        my $indexes = $properties{'Indexes'} || "enabled";
        my $sslRequireSSL = $properties{'SSLRequireSSL'} || "disabled";
        my $ssldb = $properties{'SSL'} || 'disabled';

###php values for each ibay or set 'disabled' if not defined
        my $allowurlfopen = $properties{'AllowUrlfOpen'} || 'disabled';
        my $memoryLimit = $properties{'MemoryLimit'} || 'disabled';
        my $upmaxfileSize = $properties{'UpMaxFileSize'} || 'disabled';
        my $postmaxSize = $properties{'PostMaxSize'} || 'disabled';
        my $maxexecutionTime = $properties{'MaxExecTime'} || 'disabled';
        $maxexecutionTime = '0' if $maxexecutionTime eq 'unlimited';
        
        $OUT .= "\n";
        $OUT .= "#------------------------------------------------------------\n";
        $OUT .= "# $key ibay directories ($properties{'Name'})\n";
        $OUT .= "#------------------------------------------------------------\n";

        $OUT .= "\n";
        $OUT .= "<Directory /home/e-smith/files/ibays/$key/html>\n";
        if (($sslRequireSSL eq 'enabled') || ($ssldb  eq 'enabled'))
        {
            $OUT .="    SSLRequireSSL\n";
        }
        $OUT .= "    Options None\n";
        $OUT .= "    Options +Indexes\n" if ($indexes eq 'enabled');
        $OUT .= "    Options +FollowSymLinks\n" if ($followSymLinks eq 'enabled');
        if ($dynamicContent eq 'enabled')
        {          
            $OUT .= "    Options +Includes\n";
            $OUT .= "    php_admin_flag allow_url_fopen on\n" if ($allowurlfopen eq 'enabled');
            $OUT .= "    php_admin_value memory_limit $memoryLimit\n" if ($memoryLimit ne 'disabled');
            $OUT .= "    php_admin_value upload_max_filesize $upmaxfileSize\n" if ($upmaxfileSize ne 'disabled');
            $OUT .= "    php_admin_value post_max_size $postmaxSize\n" if ($postmaxSize ne 'disabled');
            $OUT .= "    php_admin_value max_execution_time $maxexecutionTime\n" if ($maxexecutionTime ne 'disabled');
        }
        else
        {
            $OUT .= "    DirectoryIndex index.shtml index.htm index.html\n";
            $OUT .= "    Options +IncludesNOEXEC\n";
            $OUT .= "    <FilesMatch \"\\.(php|php3|phtml)\$\">\n";
            $OUT .= "        order deny,allow\n";
            $OUT .= "        Deny from all\n";
            $OUT .= "    </FilesMatch>\n";
        }
        $OUT .= "    AllowOverride $allowOverride\n";
        $OUT .= "    order deny,allow\n";
        $OUT .= "    deny from all\n";
        $OUT .= "    allow from $allow\n";
        if ($pass)
        {
            $OUT .= "    AuthName \"$properties{'Name'}\"\n";
            $OUT .= "    AuthType Basic\n";
            $OUT .= "    AuthBasicProvider external\n";
            $OUT .= "    AuthExternal pwauth\n";
            $OUT .= "    require user $key\n";
            $OUT .= "    Satisfy $satisfy\n";
        }

        if (($properties{PHPRegisterGlobals} || 'disabled') eq 'enabled')
        {
            $OUT .= "    php_flag register_globals on\n";
        }
        $OUT .= "</Directory>\n";

        $OUT .= "\n";
        $OUT .= "<Directory /home/e-smith/files/ibays/$key/cgi-bin>\n";
        if ($dynamicContent eq 'enabled')
        {
            $OUT .= "    Options ExecCGI\n";
        }
        $OUT .= "    AllowOverride None\n";
        $OUT .= "    order deny,allow\n";
        $OUT .= "    deny from all\n";
        $OUT .= "    allow from $allow\n";
        if ($pass)
        {
            $OUT .= "    AuthName \"$properties{'Name'}\"\n";
            $OUT .= "    AuthType Basic\n";
            $OUT .= "    AuthBasicProvider external\n";
            $OUT .= "    AuthExternal pwauth\n";
            $OUT .= "    require user $key\n";
            $OUT .= "    Satisfy $satisfy\n";
        }
        $OUT .= "</Directory>\n";

        $OUT .= "\n";
        $OUT .= "<Directory /home/e-smith/files/ibays/$key/files>\n";
        $OUT .= "    AllowOverride None\n";
        $OUT .= "    order deny,allow\n";
        $OUT .= "    deny from all\n";
        $OUT .= "    allow from $allow\n";
        if ($pass)
        {
            $OUT .= "    AuthName \"$properties{'Name'}\"\n";
            $OUT .= "    AuthType Basic\n";
            $OUT .= "    AuthBasicProvider external\n";
            $OUT .= "    AuthExternal pwauth\n";
            $OUT .= "    require user $key\n";
            $OUT .= "    Satisfy $satisfy\n";
        }
        $OUT .= "</Directory>\n";
    }
}
