added to sql a btrim_lcolumns function
[migration-tools.git] / opac_settings.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009-2012, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 # USAGE: ./opac_settings.pl --bootstrap /openils/conf/opensrf_core.xml --schema m_sage
20 # Produces output that can be pasted into /etc/apache2/sites-available/eg.conf
21
22 use strict;
23 use DBI;
24 use FileHandle;
25 use Getopt::Long;
26 use OpenSRF::EX qw/:try/;
27 use OpenSRF::Utils::Logger qw/$logger/;
28 use OpenSRF::System;
29 use OpenSRF::AppSession;
30 use OpenSRF::Utils::SettingsClient;
31
32 use Data::Dumper;
33
34 use open ':utf8';
35
36 binmode(STDIN, ':utf8');
37 binmode(STDOUT, ':utf8');
38
39 $| = 1;
40
41 my ($config, $schema);
42
43 GetOptions(
44         "bootstrap=s"   => \$config,
45         "schema=s" => \$schema,
46 );
47
48 OpenSRF::System->bootstrap_client( config_file => $config );
49
50 # XXX Get this stuff from the settings server
51 my $sc = OpenSRF::Utils::SettingsClient->new;
52 my $db_driver = $sc->config_value( reporter => setup => database => 'driver' );
53 my $db_host = $sc->config_value( reporter => setup => database => 'host' );
54 my $db_port = $sc->config_value( reporter => setup => database => 'port' );
55 my $db_name = $sc->config_value( reporter => setup => database => 'db' );
56 if (!$db_name) {
57     $db_name = $sc->config_value( reporter => setup => database => 'name' );
58     print STDERR "WARN: <database><name> is a deprecated setting for database name. For future compatibility, you should use <database><db> instead." if $db_name; 
59 }
60 my $db_user = $sc->config_value( reporter => setup => database => 'user' );
61 my $db_pw = $sc->config_value( reporter => setup => database => 'pw' );
62
63 die "Unable to retrieve database connection information from the settings server" unless ($db_driver && $db_host && $db_port && $db_name && $db_user);
64
65 my $dsn = "dbi:" . $db_driver . ":dbname=" . $db_name .';host=' . $db_host . ';port=' . $db_port;
66
67 my $dbh = DBI->connect($dsn,$db_user,$db_pw, {AutoCommit => 1, pg_enable_utf8 => 1, RaiseError => 1});
68
69 my $SQL = 'SELECT DISTINCT ou.id, ou.name FROM actor.org_unit ou, '.$schema.'.opac_settings os WHERE os.org_unit = ou.id ORDER BY ou.name';
70
71 my $ous = $dbh->selectcol_arrayref($SQL);
72
73 print "NameVirtualHost *:80\n";
74 print "NameVirtualHost *:443\n\n";
75
76 for my $ou_id ( @$ous ) {
77
78   $SQL = "SELECT * FROM actor.org_unit WHERE id = $ou_id";
79   my $ou = $dbh->selectrow_hashref( $SQL, {} );
80   
81   $SQL = "SELECT * FROM ".$schema.".opac_settings WHERE org_unit = $ou_id";
82   my $settings = $dbh->selectall_hashref( $SQL, 'id' );
83
84   my %s = {};
85  
86   foreach my $key (keys %$settings) {
87     $$settings{$key}->{value} =~ s/^"//;
88     $$settings{$key}->{value} =~ s/"$//;
89     if ($$settings{$key}->{name} eq 'opac.server_alias') {
90       push @{$s{'opac.server_alias'}}, $$settings{$key}->{value};
91     } else {
92       $s{$$settings{$key}->{name}} = $$settings{$key}->{value};
93     } 
94   }
95
96   print "# " . "-"x70 . "\n";
97   print "# $$ou{name} (shortname '$$ou{shortname}', id $$ou{id})\n";
98   print "# " . "-"x70 . "\n";
99
100   print "\n";
101   print "<VirtualHost *:80>\n";
102   print "  ServerName $s{'opac.server_name'}\n";
103   foreach my $alias (@{$s{'opac.server_alias'}}) { print "  ServerAlias $alias\n"; }
104   print "  DocumentRoot /openils/var/web/\n";  
105   print "  DirectoryIndex index.xml index.html index.xhtml\n";
106   print "  RedirectMatch ^/\$ http://$s{'opac.server_name'}/opac/en-US/skin/$s{'opac.directory'}/xml/index.xml?ol=$ou_id\n";
107   print "  RedirectMatch default/css/colors.css\$ http://$s{'opac.server_name'}/opac/theme/$s{'opac.directory'}/css/colors.css\n";
108   print "  RedirectMatch ^/favicon.ico\$ http://$s{'opac.server_name'}/opac/images/$s{'opac.directory'}/favicon.ico\n";
109   print "  RedirectMatch images/main_logo.jpg\$ http://$s{'opac.server_name'}/opac/images/$s{'opac.directory'}/main_logo.jpg\n";
110   print "  RedirectMatch images/small_logo.jpg\$ http://$s{'opac.server_name'}/opac/images/$s{'opac.directory'}/small_logo.jpg\n";
111   print "  RedirectMatch en-US/extras/slimpac/(start|advanced).html http://$s{'opac.server_name'}/opac/en-US/extras/slimpac/\$1.html\n";
112   print "  Include eg_vhost.conf\n";
113   print "</VirtualHost>\n";
114
115   print "\n";
116   print "<VirtualHost *:443>\n";
117   print "  ServerName $s{'opac.server_name'}\n";
118   foreach my $alias (@{$s{'opac.server_alias'}}) { print "  ServerAlias $alias\n"; }
119   print "  DocumentRoot /openils/var/web/\n";  
120   print "  DirectoryIndex index.xml index.html index.xhtml\n";
121   print "  RedirectMatch ^/\$ https://$s{'opac.server_name'}/opac/en-US/skin/$s{'opac.directory'}/xml/index.xml?ol=$ou_id\n";
122   print "  RedirectMatch default/css/colors.css\$ https://$s{'opac.server_name'}/opac/theme/$s{'opac.directory'}/css/colors.css\n";
123   print "  RedirectMatch ^/favicon.ico\$ https://$s{'opac.server_name'}/opac/images/$s{'opac.directory'}/favicon.ico\n";
124   print "  RedirectMatch images/main_logo.jpg\$ https://$s{'opac.server_name'}/opac/images/$s{'opac.directory'}/main_logo.jpg\n";
125   print "  RedirectMatch images/small_logo.jpg\$ https://$s{'opac.server_name'}/opac/images/$s{'opac.directory'}/small_logo.jpg\n";
126   print "  RedirectMatch en-US/extras/slimpac/(start|advanced).html https://$s{'opac.server_name'}/opac/en-US/extras/slimpac/\$1.html\n";
127   print "  SSLEngine on\n";
128   print "  SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL\n";
129   print "  SSLCertificateFile $s{'opac.ssl_cert'}\n";
130   print "  SSLCertificateKeyFile $s{'opac.ssl_key'}\n";
131   print "  SSLCertificateChainFile ssl/gd_bundle.crt\n";
132   print "  Include eg_vhost.conf\n";
133   print "  BrowserMatch \".*MSIE.*\" \\\n";
134   print "    nokeepalive ssl-unclean-shutdown \\\n";
135   print "    downgrade-1.0 force-response-1.0\n";
136   print "</VirtualHost>\n";
137
138   print "\n";
139
140 }