LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / Config.pm
blob48759c6a3fab352d488f4d9718150901077cf888
1 # this is a module to handle the configuration of a LJ server
2 package LJ::Config;
4 use strict;
5 use warnings;
7 use Carp qw();
9 # force this one to load to prevent warnings;
10 # we may happen to redefine Readonly::croak in this module
11 # and if Readonly is loaded after that, that'd cause
12 # a re-definition warning
13 use Readonly qw();
15 $LJ::CONFIG_LOADED = 0;
16 $LJ::CACHE_CONFIG_MODTIME = 0;
18 sub import {
19 my ( $class, @args ) = @_;
21 foreach my $arg (@args) {
22 if ( $arg eq ':load' ) {
23 $class->load;
24 next;
27 Carp::croak "Unknown import: $arg";
30 return;
33 # loads all configurations from scratch
34 sub load {
35 my $class = shift;
36 my %opts = @_;
37 return if ! $opts{force} && $LJ::CONFIG_LOADED;
39 # 1. Load ljconfig
40 # 2. Load policy configs
41 # 3. Load database-backed config overrides
42 # 4. Load ljoverrides.pl
43 # 5. Load ljdefaults.pl (designed to not clobber stuff)
45 __PACKAGE__->load_ljconfig;
46 __PACKAGE__->load_policy;
47 __PACKAGE__->load_overrides;
48 __PACKAGE__->load_defaults;
50 $LJ::CONFIG_LOADED = 1;
53 sub reload {
54 __PACKAGE__->load( force => 1 );
56 eval {
57 # these need to be loaded after ljconfig
59 $LJ::DBIRole->set_sources(\%LJ::DBINFO);
60 LJ::MemCache::reload_conf();
61 LJ::ExternalSite->forget_site_objs;
62 LJ::EventLogSink->forget_sink_objs;
63 LJ::AccessLogSink->forget_sink_objs;
65 # reload MogileFS config
66 if (LJ::mogclient()) {
67 LJ::mogclient()->reload
68 ( domain => $LJ::MOGILEFS_CONFIG{domain},
69 root => $LJ::MOGILEFS_CONFIG{root},
70 hosts => $LJ::MOGILEFS_CONFIG{hosts},
71 readonly => $LJ::DISABLE_MEDIA_UPLOADS,
72 timeout => $LJ::MOGILEFS_CONFIG{timeout} || 3 );
73 LJ::mogclient()->set_pref_ip(\%LJ::MOGILEFS_PREF_IP)
74 if %LJ::MOGILEFS_PREF_IP;
78 warn "Errors reloading config: $@" if $@;
81 # load user-supplied config changes
82 sub load_ljconfig {
83 do "$ENV{'LJHOME'}/etc/ljconfig.pl";
84 $LJ::CACHE_CONFIG_MODTIME_LASTCHECK = time();
87 # load defaults (should not clobber any existing configs)
88 sub load_defaults {
89 my $load_res = do "$ENV{'LJHOME'}/cgi-bin/ljdefaults.pl";
90 die $@ unless $load_res;
93 # loads policy configuration
94 sub load_policy {
95 no warnings 'redefine';
97 my $policyconfig = "$ENV{LJHOME}/etc/policyconfig.pl";
98 return unless -e $policyconfig;
100 local *Readonly::croak = sub {};
101 do "$policyconfig" or die $@;
104 # load config overrides
105 sub load_overrides {
106 if (-e "$ENV{LJHOME}/cgi-bin/ljconfig.pl") {
107 warn "You are still using cgi-bin/ljconfig.pl. This has been deprecated, please use etc/ljconfig.pl and etc/ljoverrides.pl instead.";
109 # but ignore ljconfig if both exist.
110 unless (-e "$ENV{LJHOME}/etc/ljconfig.pl") {
111 do "$ENV{LJHOME}/cgi-bin/ljconfig.pl";
115 my $overrides = "$ENV{LJHOME}/etc/ljoverrides.pl";
116 return unless -e $overrides;
117 do $overrides;
120 # handle reloading at the start of a new web request
121 sub start_request_reload {
122 # check the modtime of ljconfig.pl and reload if necessary
123 # only do a stat every 10 seconds and then only reload
124 # if the file has changed
125 my $now = time();
126 if ($now - $LJ::CACHE_CONFIG_MODTIME_LASTCHECK > 10) {
127 my $modtime = (stat("$ENV{'LJHOME'}/etc/ljconfig.pl"))[9]
128 || (stat("$ENV{'LJHOME'}/cgi-bin/ljconfig.pl"))[9];
129 if (!$LJ::CACHE_CONFIG_MODTIME || $modtime > $LJ::CACHE_CONFIG_MODTIME) {
130 # reload config and update cached modtime
131 $LJ::CACHE_CONFIG_MODTIME = $modtime;
132 __PACKAGE__->reload;
133 $LJ::DEBUG_HOOK{'pre_save_bak_stats'}->() if $LJ::DEBUG_HOOK{'pre_save_bak_stats'};
135 $LJ::IMGPREFIX_BAK = $LJ::IMGPREFIX;
136 $LJ::STATPREFIX_BAK = $LJ::STATPREFIX;
137 $LJ::USERPICROOT_BAK = $LJ::USERPIC_ROOT;
139 $LJ::LOCKER_OBJ = undef;
140 if ($modtime > $now - 60) {
141 # show to stderr current reloads. won't show
142 # reloads happening from new apache children
143 # forking off the parent who got the inital config loaded
144 # hours/days ago and then the "updated" config which is
145 # a different hours/days ago.
147 # only print when we're in web-context
148 print STDERR "[$$] ljconfig.pl reloaded\n"
149 if LJ::Request->is_inited;
152 $LJ::CACHE_CONFIG_MODTIME_LASTCHECK = $now;