| 1 | package App::ZofCMS::Plugin::StyleSwitcher; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.001010'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base 'App::ZofCMS::Plugin::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub _key { 'plug_style_switcher' } | ||||||||||||||||||||||||||
|
Subroutine
_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub _defaults { | ||||||||||||||||||||||||||
|
Subroutine
_defaults: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 13 | return ( | ||||||||||||||||||||||||||||
| 14 | 0 | q_name => 'style', | |||||||||||||||||||||||||||
| 15 | q_ajax_name => 'plug_style_switcher_ajax', | ||||||||||||||||||||||||||||
| 16 | t_prefix => 'style_switcher_', | ||||||||||||||||||||||||||||
| 17 | table => 'style_switcher', | ||||||||||||||||||||||||||||
| 18 | max_time => 2678400, # one month | ||||||||||||||||||||||||||||
| 19 | default_style => 'main', | ||||||||||||||||||||||||||||
| 20 | xhtml => 0, | ||||||||||||||||||||||||||||
| 21 | # styles => {} | ||||||||||||||||||||||||||||
| 22 | dsn => "DBI:mysql:database=test;host=localhost", | ||||||||||||||||||||||||||||
| 23 | #user => 'test', | ||||||||||||||||||||||||||||
| 24 | #pass => 'test', | ||||||||||||||||||||||||||||
| 25 | create_table => 0, | ||||||||||||||||||||||||||||
| 26 | opt => { RaiseError => 1, AutoCommit => 1 }, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub _do { | ||||||||||||||||||||||||||
|
Subroutine
_do: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 30 | 0 | my ( $self, $conf, $template, $query, $config ) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $dbh = DBI->connect_cached( | ||||||||||||||||||||||||||||
| 33 | 0 | @$conf{ qw/dsn user pass opt/ }, | |||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | 0 | $self->dbh( $dbh ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ( $conf->{create_table} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $dbh->do( | |||||||||||||||||||||||||||
| 39 | "CREATE TABLE $conf->{table} ( host VARCHAR(200), style TEXT, time VARCHAR(10) );", | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ( defined $query->{ $conf->{q_name} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $$query{$conf->{"q_name"}} and length $$query{$conf->{"q_name"}}
MC/DC0%0/2
defined $$query{$conf->{"q_name"}} and length $$query{$conf->{"q_name"}}
defined $$query{$conf->{"q_name"}}
length $$query{$conf->{"q_name"}}
Truth table0%0/3
A and B
A= defined $$query{$conf->{"q_name"}}B= length $$query{$conf->{"q_name"}}
| |||||||||||||||||||||||||||||
| 44 | and length $query->{ $conf->{q_name} } | ||||||||||||||||||||||||||||
| 45 | ) { | ||||||||||||||||||||||||||||
| 46 | 0 | $self->_set_style( $conf, $config->cgi, $query->{ $conf->{q_name} } ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $style = $self->_get_current_style( $conf, $config->cgi ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $tp = $conf->{t_prefix}; | |||||||||||||||||||||||||||
| 52 | 0 | $template->{t}{ $tp . 'style' } = $self->_make_style_html( $conf, $style ); | |||||||||||||||||||||||||||
| 53 | $template->{t}{ $tp . 'toggle' } = $self->_make_toggle_html( | ||||||||||||||||||||||||||||
| 54 | $conf, | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | map +(defined() ? $_ : ''), @$query{qw/page dir/}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | $style, | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | 0 | $dbh->disconnect; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | print "Set-Cookie: plug_style_switcher=$style\n"; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( $query->{ $conf->{q_ajax_name} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | print "Content-type: text/plain\n\n"; | |||||||||||||||||||||||||||
| 64 | 0 | exit; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub _make_toggle_html { | ||||||||||||||||||||||||||
|
Subroutine
_make_toggle_html: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 69 | 0 | my ( $self, $conf, $page, $dir, $style ) = @_; | |||||||||||||||||||||||||||
| 70 | 0 | my $styles_ref = $conf->{styles}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $next; | |||||||||||||||||||||||||||
| 73 | my $last; | ||||||||||||||||||||||||||||
| 74 | 0 | for ( sort keys %$styles_ref ) { | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | $next = $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | unless defined $next; | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( defined $last and $last eq $style ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $last and $last eq $style
MC/DC0%0/2
defined $last and $last eq $style
defined $last
$last eq $style
Truth table0%0/3
A and B
A= defined $lastB= $last eq $style
| |||||||||||||||||||||||||||||
| 78 | 0 | $next = $_; | |||||||||||||||||||||||||||
| 79 | 0 | last; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | $last = $_; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | return qq|<a id="plug_style_switcher" | | |||||||||||||||||||||||||||
| 85 | . qq|href="/index.pl?page=$page&dir=$dir&$conf->{q_name}=$next">| | ||||||||||||||||||||||||||||
| 86 | . qq|toggle style</a>\n|; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _make_style_html { | ||||||||||||||||||||||||||
|
Subroutine
_make_style_html: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 90 | 0 | my ( $self, $conf, $style ) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | my $styles_ref = $conf->{styles}; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $ending = $conf->{xhtml} ? '/' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my @out; | |||||||||||||||||||||||||||
| 95 | 0 | for my $key ( sort keys %$styles_ref ) { | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $rel = $key eq $style ? 'stylesheet' : 'alternate stylesheet'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | my $styles = $styles_ref->{ $key }; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | $styles = [ $styles ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | unless ref $styles eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 101 | 0 | for my $style ( @$styles ) { | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ( '[IE]' eq substr $style, 0, 4 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $style = substr $style, 0, 4; | |||||||||||||||||||||||||||
| 104 | 0 | push @out, | |||||||||||||||||||||||||||
| 105 | qq|<!--[if IE]><link rel="$rel" type="text/css" href="$style" media="screen,tv,projection"$ending><![endif]-->|; | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||
| 108 | 0 | push @out, | |||||||||||||||||||||||||||
| 109 | qq|<link rel="$rel" type="text/css" href="$style" media="screen,tv,projection"$ending>|; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | return join "\n", @out; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub _set_style { | ||||||||||||||||||||||||||
|
Subroutine
_set_style: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 118 | 0 | my ( $self, $conf, $cgi, $style ) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | my $host = $self->_make_host( $cgi ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | $style = $conf->{default_style} | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | unless exists $conf->{styles}{ $style }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||
| 125 | $dbh->do( | ||||||||||||||||||||||||||||
| 126 | "DELETE FROM $conf->{table} WHERE host = ? OR time < ?;", | ||||||||||||||||||||||||||||
| 127 | undef, | ||||||||||||||||||||||||||||
| 128 | $host, | ||||||||||||||||||||||||||||
| 129 | time() - $conf->{max_time}, | ||||||||||||||||||||||||||||
| 130 | 0 | ); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | $dbh->do( | |||||||||||||||||||||||||||
| 133 | "INSERT INTO $conf->{table} VALUES (?, ?, ?);", | ||||||||||||||||||||||||||||
| 134 | undef, | ||||||||||||||||||||||||||||
| 135 | $host, | ||||||||||||||||||||||||||||
| 136 | $style, | ||||||||||||||||||||||||||||
| 137 | time(), | ||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _get_current_style { | ||||||||||||||||||||||||||
|
Subroutine
_get_current_style: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 143 | 0 | my ( $self, $conf, $cgi ) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my $host = $self->_make_host( $cgi ); | |||||||||||||||||||||||||||
| 146 | 0 | my $user_style = $self->dbh->selectall_arrayref( | |||||||||||||||||||||||||||
| 147 | "SELECT * FROM $conf->{table} WHERE host = ?;", | ||||||||||||||||||||||||||||
| 148 | { Slice => {} }, | ||||||||||||||||||||||||||||
| 149 | $host, | ||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | $self->dbh->do( | |||||||||||||||||||||||||||
| 153 | "UPDATE $conf->{table} SET time = ? WHERE host = ?;", | ||||||||||||||||||||||||||||
| 154 | undef, | ||||||||||||||||||||||||||||
| 155 | time(), | ||||||||||||||||||||||||||||
| 156 | $host, | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | return $conf->{default_style} | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | unless @$user_style; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | return $user_style->[0]{style}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub _make_host { | ||||||||||||||||||||||||||
|
Subroutine
_make_host: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 166 | 0 | my ( $self, $cgi ) = @_; | |||||||||||||||||||||||||||
| 167 | 0 | my $host = $cgi->remote_host . $ENV{HTTP_USER_AGENT}; | |||||||||||||||||||||||||||
| 168 | 0 | $host =~ s/[^\w.]+//g; | |||||||||||||||||||||||||||
| 169 | 0 | $host = substr $host, 0, 200; | |||||||||||||||||||||||||||
| 170 | 0 | return $host; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub dbh { | ||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 174 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | @_ and $self->{DBH} = shift; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | return $self->{DBH}; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | 1; | |||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||