View | Details | Raw Unified | Return to bug 34478
Collapse All | Expand All

(-)a/csrf_add_missing_csrf.pl (-1 / +71 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use File::Find;
5
use File::Slurp;
6
use Data::Dumper;
7
8
my @themes;
9
10
# OPAC themes
11
my $opac_dir  = 'koha-tmpl/opac-tmpl';
12
opendir ( my $dh, $opac_dir ) or die "can't opendir $opac_dir: $!";
13
for my $theme ( grep { not /^\.|lib|js|xslt/ } readdir($dh) ) {
14
    push @themes, "$opac_dir/$theme/en";
15
}
16
close $dh;
17
18
# STAFF themes
19
my $staff_dir = 'koha-tmpl/intranet-tmpl';
20
opendir ( $dh, $staff_dir ) or die "can't opendir $staff_dir: $!";
21
for my $theme ( grep { not /^\.|lib|js/ } readdir($dh) ) {
22
    push @themes, "$staff_dir/$theme/en";
23
}
24
close $dh;
25
26
my @files;
27
sub wanted {
28
    my $name = $File::Find::name;
29
    push @files, $name
30
        if $name =~ m[\.(tt|inc)$] and -f $name;
31
}
32
33
find({ wanted => \&wanted, no_chdir => 1 }, @themes );
34
35
my @errors;
36
for my $file ( @files ) {
37
    add_csrf_token($file);
38
}
39
40
sub add_csrf_token {
41
    my ( $file ) = @_;
42
43
    my $csrf_include = "[% INCLUDE 'csrf-token.inc' %]\n";
44
    my @lines = read_file($file);
45
    my @new_lines = @lines;
46
    return unless grep { $_ =~ m|<form| } @lines;
47
    my ( $in_form, $closed_form, $line_open_form, $has_csrf_token );
48
    my ( $line_number, $number_replaced_forms ) = (0, 0);
49
    my $spacing = q{};
50
    for my $line (@lines) {
51
        $line_number++;
52
        if ( $line =~ m{^(\s*)<form.*method=('|")post('|")}i ) {
53
            $spacing = $1 . "    ";
54
            $in_form = 1;
55
            $line_open_form = $line_number;
56
        }
57
        if ( $in_form && $line =~ m{name="csrf_token"}) {
58
            $has_csrf_token = 1;
59
        }
60
        if ( $in_form && $line =~ m{</form} ) {
61
            $closed_form = 0;
62
            unless ( $has_csrf_token ) {
63
                splice @new_lines, $line_open_form + $number_replaced_forms, 0, $spacing . $csrf_include;
64
                $number_replaced_forms++;
65
            }
66
            $in_form = 0;
67
            $has_csrf_token = 0;
68
        }
69
    }
70
    write_file($file, @new_lines);
71
}

Return to bug 34478