summaryrefslogtreecommitdiff
path: root/httemplate/config/config-process.cgi
blob: 4e1c85a0376596442e98c068fa2136d7674f4d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
%if ( scalar(@error) ) {
%
%  my $url = popurl(1)."config.cgi";
%  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
%
%    my $session = int(rand(4294967296)); #XXX
%    my $pref = new FS::access_user_pref({
%      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
%      'prefname'   => "redirect$session",
%      'prefvalue'  => $cgi->query_string,
%      'expiration' => time + 3600, #1h?  1m?
%    });
%    my $pref_error = $pref->insert;
%    if ( $pref_error ) {
%      die "FATAL: couldn't even set redirect cookie: $pref_error".
%          " attempting to set redirect$session to ". $cgi->query_string."\n";
%    }
%
<% $cgi->redirect("$url?redirect=$session") %>
%
%  } else {
%
<% $cgi->redirect("$url?". $cgi->query_string ) %>
%
%  }
%
%} else {
<% header('Configuration set') %>
  <SCRIPT TYPE="text/javascript">
%   my $n = 0;
%   foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
    var configCell = window.top.document.getElementById('<% $agentnum. $i->key. $n %>');
    if ( ! configCell ) {
      window.top.location.reload();
    }
    //alert('found cell ' + configCell);
%     if (    $type eq 'textarea'
%          || $type eq 'editlist'
%          || $type eq 'selectmultiple' ) {
        configCell.innerHTML =
          '<font size="-2"><pre>' + "\n" +
          <% encode_entities(join("\n",
               map { length($_) > 88 ? substr($_,0,88).'...' : $_ }
                   $conf->config($i->key, $agentnum)
             ) )
          |js_string %> +
          '</pre></font>';

%     } elsif ( $type eq 'checkbox' ) {
%       if ( $conf->exists($i->key, $agentnum) ) {
          configCell.style.backgroundColor = '#00ff00';
          configCell.innerHTML = 'YES';
%       } else {
          configCell.style.backgroundColor = '#ff0000';
          configCell.innerHTML = 'NO';
%       }
%     } elsif ( $type eq 'select' && $i->select_hash ) {
%       my %hash;
%       if ( ref($i->select_hash) eq 'ARRAY' ) {
%         tie %hash, 'Tie::IxHash', '' => '', @{ $i->select_hash };
%       } else {
%         tie %hash, 'Tie::IxHash', '' => '', %{ $i->select_hash };
%       }
        configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $hash{ $conf->config($i->key, $agentnum) } : '' |js_string %>;

%     } elsif ( $type eq 'text' || $type eq 'select' ) {
        configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %>;
%     } elsif ( $type =~ /^select-(part_svc|part_pkg|pkg_class)$/ && ! $i->multiple ) {
%       my $table = $1;
%       my $namecol = $namecol{$table};
%       my $pkey = dbdef->table($table)->primary_key;
%       my $key = $conf->config($i->key, $agentnum);
%       my $record = qsearchs($table, { $pkey => $key });
%       my $value = $record ? "$key: ".$record->$namecol() : $key;
        configCell.innerHTML = <% $value |js_string %>;
%     } elsif ( $type eq 'select-sub' && ! $i->multiple ) {
        configCell.innerHTML =
          <% $conf->config($i->key, $agentnum) |js_string %> + ': ' +
          <% &{ $i->option_sub }( $conf->config($i->key, $agentnum) ) |js_string %>;
%     } else {
        //alert('unknown type <% $type %>');
        window.top.location.reload();
%     }

%     $n++;
%   }
    parent.cClick();
  </SCRIPT>
</BODY>
</HTML>
%}
<%once>
#false laziness w/config-view.cgi
my %namecol = (
  'part_svc'  => 'svc',
  'part_pkg'  => 'pkg',
  'pkg_class' => 'classname',
);
</%once>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied\n" unless $curuser->access_right('Configuration');

my $conf = new FS::Conf;

if ( $conf->exists('disable_settings_changes') ) {
  my @changers = split(/\s*,\s*/, $conf->config('disable_settings_changes'));
  my %changers = map { $_=>1 } @changers;
  unless ( $changers{$curuser->username} ) {
    errorpage_popup("Disabled in web demo");
    die "shouldn't be reached";
  }
}

$FS::Conf::DEBUG = 1;
my @config_items = grep { $_->key != ~/^invoice_(html|latex|template)/ }
                        $conf->config_items;
my %confitems = map { $_->key => $_ } $conf->config_items;

my $agentnum = $cgi->param('agentnum');
my $key = $cgi->param('key');
my $i = $confitems{$key};

my @error = ();
my @touch = ();
my @delete = ();
my $n = 0;
foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
  if ( $type eq '' ) {
  } elsif ( $type eq 'textarea' ) {
    if ( $cgi->param($i->key.$n) ne '' ) {
      my $value = $cgi->param($i->key.$n);
      $value =~ s/\r\n/\n/g; #browsers?
      my $error = &{$i->validate}($value, $n) if $i->validate;
      push @error, $error if $error;
      $conf->set($i->key, $value, $agentnum);
    } else {
      $conf->delete($i->key, $agentnum);
    }
  } elsif ( $type eq 'binary' || $type eq 'image' ) {
    if ( defined($cgi->param($i->key.$n)) && $cgi->param($i->key.$n) ) {
      my $fh = $cgi->upload($i->key.$n);
      my $error = &{$i->validate}($fh, $n) if $i->validate;
      push @error, $error if $error;
      if (defined($fh)) {
        local $/;
        $conf->set_binary($i->key, <$fh>, $agentnum);
      }
    }else{
      warn "Condition failed for " . $i->key;
    }
  } elsif ( $type eq 'checkbox' ) {
    if ( defined $cgi->param($i->key.$n) ) {
      push @touch, $i->key;
    } else {
      push @delete, $i->key;
    }
  } elsif (
    $type =~ /^(editlist|selectmultiple)$/
    or ( $type =~ /^select(-(sub|part_svc|part_pkg|pkg_class))?$/
         || $i->multiple )
  ) {
    if ( scalar(@{[ $cgi->param($i->key.$n) ]}) ) {
      my $error = &{$i->validate}([ $cgi->param($i->key.$n) ], $n) if $i->validate;
      push @error, $error if $error;
      $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
    } else {
      $conf->delete($i->key, $agentnum);
    }
  } elsif ( $type =~ /^(text|select(-(sub|part_svc|part_pkg|pkg_class))?)$/ ) {
    if ( $cgi->param($i->key.$n) ne '' ) {
      my $error = &{$i->validate}($cgi->param($i->key.$n), $n) if $i->validate;
      push @error, $error if $error;
      $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
    } else {
      $conf->delete($i->key, $agentnum);
    }
  }
  $n++;
}
# warn @touch;
$conf->touch($_, $agentnum) foreach @touch;
$conf->delete($_, $agentnum) foreach @delete;

if (scalar(@error)) {
  $cgi->param('error', join(' ', @error));
}

</%init>