{
use esmith::AccountsDB;
use esmith::ConfigDB;

my $a = esmith::AccountsDB->open_ro || die "Couldn't open the accounts database";
my $p = esmith::ConfigDB->open_ro('php') || esmith::ConfigDB->create('php');

if ($fastcgi_mod eq 'mod_fastcgi'){
  $OUT .=<<_EOF;
# mod_fastcgi global config
FastCgiIpcDir /var/run/mod_fastcgi
FastCgiWrapper Off
FastCgiConfig -idle-timeout 40 -maxClassProcesses 1
<Location /php-cgi-bin/>
  Allow from all
</Location>

_EOF

  foreach my ver ('', '56, '70', '71'){
    $OUT .=<<_EOF;

Action php$ver-fastcgi /php-cgi-bin/php$ver-wrapper
Alias /php-cgi-bin/php$ver-wrapper /var/www/php-cgi-bin/php$ver-wrapper
FastCgiExternalServer /var/www/php-cgi-bin/php$ver-wrapper -socket /var/run/php-fpm/php$ver.sock -pass-header Authorization -idle-timeout 120

_EOF
  }

  $OUT .= "# Shared Folders\n";

  foreach my $share ($a->get_all_by_prop(type => 'share')){
    next if ($share->prop('DynamicContent' || 'disabled') ne 'enabled');
    my $key = $share->key;
    my $ver = $share->prop('PHPVersion') || '';
    my $socket = "php$ver-$key.sock";
    my $custom = $share->prop('PHPCustomPool') || undef;
    if ($custom){
      $ver = $p->get($custom)->prop('Version') || '';
      $socket = "php$ver-$custom.sock";
    }
    $OUT .=<<_EOF;

Action php$key-fastcgi /php-cgi-bin/php$key-wrapper
Alias /php-cgi-bin/php$key-wrapper /var/www/php-cgi-bin/php$key-wrapper
FastCgiExternalServer /var/www/php-cgi-bin/php$key-wrapper -socket /var/run/php-fpm/$socket -pass-header Authorization -idle-timeout 120

_EOF
}

  $OUT .= "# Custom PHP pools\n";

  foreach my $pool ($p->get_all_by_prop(type => 'pool')){
    next if (($pool->prop('status') || 'enabled') ne 'enabled');
    my $key = $pool->key;
    my $ver = $pool->prop('Version') || '';
    $OUT .=<<_EOF;

Action php$key-fastcgi /php-cgi-bin/php$key-wrapper
Alias /php-cgi-bin/php$key-wrapper /var/www/php-cgi-bin/php$key-wrapper
FastCgiExternalServer /var/www/php-cgi-bin/php$key-wrapper -socket /var/run/php-fpm/php$ver-$key.sock -pass-header Authorization -idle-timeout 120

_EOF
  }
}
else{
  $OUT .= '# Using mod_proxy_fcgi, no additional config required'
}
}
