Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / Koha / Config.pm
1 package Koha::Config;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # 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 Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use XML::Simple;
21
22 # Default config file, if none is specified
23 use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
24
25 # path to config file set by installer
26 # __KOHA_CONF_DIR__ is set by rewrite-confg.PL
27 # when Koha is installed in 'standard' or 'single'
28 # mode.  If Koha was installed in 'dev' mode,
29 # __KOHA_CONF_DIR__ is *not* rewritten; instead
30 # developers should set the KOHA_CONF environment variable
31 my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
32
33 # Should not be called outside of C4::Context or Koha::Cache
34 # use C4::Context->config instead
35 sub read_from_file {
36     my ( $class, $file ) = @_;
37
38     return if not defined $file;
39
40     my $xml;
41     eval {
42         $xml = XMLin(
43             $file,
44             keyattr => ['id'],
45             forcearray => ['listen', 'server', 'serverinfo'],
46             suppressempty => ''
47         );
48     };
49
50     if ($@) {
51         die "\nError reading file $file.\nTry running this again as the koha instance user (or use the koha-shell command in debian)\n\n";
52     }
53
54     return $xml;
55 }
56
57 # Koha's main configuration file koha-conf.xml
58 # is searched for according to this priority list:
59 #
60 # 1. Path supplied via use C4::Context '/path/to/koha-conf.xml'
61 # 2. Path supplied in KOHA_CONF environment variable.
62 # 3. Path supplied in INSTALLED_CONFIG_FNAME, as long
63 #    as value has changed from its default of
64 #    '__KOHA_CONF_DIR__/koha-conf.xml', as happens
65 #    when Koha is installed in 'standard' or 'single'
66 #    mode.
67 # 4. Path supplied in CONFIG_FNAME.
68 #
69 # The first entry that refers to a readable file is used.
70
71 sub guess_koha_conf {
72
73     # If the $KOHA_CONF environment variable is set, use
74     # that. Otherwise, use the built-in default.
75     my $conf_fname;
76     if ( exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s $ENV{"KOHA_CONF"} ) {
77         $conf_fname = $ENV{"KOHA_CONF"};
78     } elsif ( $INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME ) {
79         # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above
80         # regex to anything else -- don't want installer to rewrite it
81         $conf_fname = $INSTALLED_CONFIG_FNAME;
82     } elsif ( -s CONFIG_FNAME ) {
83         $conf_fname = CONFIG_FNAME;
84     }
85     return $conf_fname;
86 }
87
88 1;